1
1
mirror of https://github.com/github/semantic.git synced 2024-12-19 12:51:52 +03:00
semantic/prototype/UI/diff.js

237 lines
6.6 KiB
JavaScript
Raw Normal View History

2015-10-27 18:11:45 +03:00
function diffFromJSON(json) {
if (json.pure != null) {
return new Diff({
pure: new Patch(json.pure)
});
}
if (json.roll != null) {
return new Diff({
roll: {
extract: json.roll.extract,
unwrap: new Syntax(json.roll.unwrap, function(x) {
return diffFromJSON(x);
})
}
});
}
}
function Diff(object) {
if (object.pure != null) {
this.pure = object.pure;
}
if (object.roll != null) {
this.roll = object.roll;
}
return this;
}
// forall a b. Diff a -> (a -> b) -> Diff b
Diff.prototype.map = function(transform) {
if (this.pure != null) {
return new Diff({
pure: transform(this.pure)
});
}
if (this.roll != null) {
return new Diff({
roll: {
extract: this.roll.extract,
unwrap: this.roll.unwrap.map(function(x) {
return x.map(transform);
})
}
});
}
}
// forall a. Diff a -> (Syntax a -> a) -> a
Diff.prototype.cata = function(transform) {
if (this.pure != null) {
return this.pure;
}
if (this.roll != null) {
return transform(this.roll.unwrap.map(function(diff) {
return diff.cata(transform);
}))
}
}
2015-10-29 22:11:18 +03:00
function wrap(tagName, element) {
if (element == null) {
return null;
}
var node = document.createElement(tagName);
node.appendChild(element);
return node;
}
/// String -> Syntax a -> Range -> (a -> Range) -> (a -> DOM) -> DOM
function rangeAndSyntaxToDOM(source, syntax, range, getRange, recur) {
recur = recur || function(term) {
return rangeAndSyntaxToDOM(source, term.unwrap, term.range);
}
var element;
if (syntax.leaf != null) {
element = document.createElement("span");
element.textContent = source.substr(range[0], range[1]);
} else if (syntax.indexed != null || syntax.fixed != null) {
var values = syntax.indexed || syntax.fixed;
2015-10-29 23:27:41 +03:00
element = document.createElement("ul");
2015-10-29 22:11:18 +03:00
var previous = range[0];
for (i in values) {
var child = values[i];
if (child.pure == "") continue;
var childRange = getRange(child);
if (childRange.before != null) {
var beforeRange = childRange.before;
element.appendChild(document.createTextNode(source.substr(previous, beforeRange[0] - previous)));
element.appendChild(wrap("li", recur(child)));
previous = beforeRange[0] + beforeRange[1];
}
if (childRange.after != null) {
var afterRange = childRange.before;
element.appendChild(document.createTextNode(source.substr(previous, afterRange[0] - previous)));
element.appendChild(wrap("td", recur(child)));
previous = afterRange[0] + afterRange[1];
}
}
element.appendChild(document.createTextNode(source.substr(previous, range[0] + range[1] - previous)));
} else if (syntax.keyed != null) {
element = document.createElement("dl");
var values = [];
for (k in syntax.keyed.values) {
if (syntax.keyed.values[k].pure == "") continue;
values.push([k, syntax.keyed.values[k]]);
}
values.sort(function(a, b) {
if (getRange(a[1])[0] < getRange(b[1])[0]) {
return -1;
} else if (getRange(a[1])[0] > getRange(b[1])[0]) {
return 1;
}
return 0;
});
var previous = range[0];
for (i in values) {
var pair = values[i];
var key = pair[0];
var child = pair[1];
var childRange = getRange(child);
element.appendChild(document.createTextNode(source.substr(previous, childRange[0] - previous)));
element.appendChild(wrap("dt", document.createTextNode(key)));
element.appendChild(wrap("dd", recur(child)));
previous = childRange[0] + childRange[1];
}
element.appendChild(document.createTextNode(source.substr(previous, range[0] + range[1] - previous)));
}
element["data-range"] = range;
return element;
}
/// Diff -> String -> DOM
2015-10-29 23:27:41 +03:00
function diffToDOM(diff, sources) {
function getRange(diffOrTerm) {
if (diffOrTerm.pure != null) {
var beforeRange, afterRange;
if (diffOrTerm.pure.before != null) {
beforeRange = diffOrTerm.pure.before.extract
}
if (diffOrTerm.pure.after != null) {
afterRange = diffOrTerm.pure.after.extract
}
return { "before": beforeRange, "after": afterRange };
}
if (diffOrTerm.roll != null) {
return diffOrTerm.roll.extract;
}
if (diffOrTerm.extract != null) {
return diffOrTerm.extract;
}
}
2015-10-27 18:11:45 +03:00
if (diff.pure != null) {
2015-10-29 23:27:41 +03:00
return pureToDOM(sources, diff.pure, getRange, function(diff) {
return diffToDOM(diff, sources);
})
2015-10-27 18:11:45 +03:00
}
2015-10-29 23:27:41 +03:00
return rollToDOM(sources, diff.roll, getRange, function(diff) {
return diffToDOM(diff, sources);
})
}
function pureToDOM(sources, patch, getRangeFun, diffToDOMFun) {
var elementA, elementB;
if (patch.before != null) {
elementA = rangeAndSyntaxToDOM(sources.before, patch.before.unwrap, patch.before.extract, getRangeFun, diffToDOMFun);
elementA.classList.add("delete");
if (patch.after != null) {
elementA.classList.add("replace");
2015-10-27 18:11:45 +03:00
}
2015-10-29 23:27:41 +03:00
}
if (patch.after != null) {
elementB = rangeAndSyntaxToDOM(sources.after, patch.after.unwrap, patch.after.extract, getRangeFun, diffToDOMFun);
elementB.classList.add("insert");
if (patch.before != null) {
elementB.classList.add("replace");
2015-10-27 18:11:45 +03:00
}
}
2015-10-29 23:27:41 +03:00
return { "before": elementA || "", "after": elementB || "" };
2015-10-27 18:11:45 +03:00
}
2015-10-29 22:11:18 +03:00
2015-10-29 23:27:41 +03:00
function rollToDOM(sources, rollOrTerm, getRangeFun, diffToDOMFun) {
var syntax = rollOrTerm.unwrap
var range = rollOrTerm.extract
2015-10-29 22:11:18 +03:00
var elementA;
var elementB;
if (syntax.leaf != null) {
elementA = document.createElement("span");
2015-10-29 23:27:41 +03:00
elementA.textContent = sources.before.substr(range.before[0], range.before[1]);
2015-10-29 22:11:18 +03:00
elementB = document.createElement("span");
2015-10-29 23:27:41 +03:00
elementB.textContent = sources.after.substr(range.after[0], range.after[1]);
2015-10-29 22:11:18 +03:00
} else if (syntax.indexed != null || syntax.fixed != null) {
var values = syntax.indexed || syntax.fixed;
elementA = document.createElement("ul");
elementB = document.createElement("ul");
var previousBefore = range.before[0];
var previousAfter = range.before[0];
for (i in values) {
var child = values[i];
if (child.pure == "") continue;
var childRange = getRangeFun(child);
2015-10-29 23:27:41 +03:00
var beforeAfterChild = diffToDOMFun(child)
2015-10-29 22:11:18 +03:00
if (childRange.before != null) {
var beforeRange = childRange.before;
elementA.appendChild(wrap("li", beforeAfterChild.before));
previousBefore = beforeRange[0] + beforeRange[1];
}
if (childRange.after != null) {
var afterRange = childRange.after;
elementB.appendChild(wrap("li", beforeAfterChild.after));
2015-10-29 23:27:41 +03:00
previousAfter = afterRange[0] + afterRange[1];
2015-10-29 22:11:18 +03:00
}
}
2015-10-29 23:27:41 +03:00
var beforeText = sources.before.substr(previousBefore, range.before[0] + range.before[1] - previousBefore);
var afterText = sources.after.substr(previousAfter, range.after[0] + range.after[1] - previousAfter);
2015-10-29 22:11:18 +03:00
elementA.appendChild(document.createTextNode(beforeText));
elementB.appendChild(document.createTextNode(afterText));
} else if (syntax.keyed != null) {
}
return { "before": elementA, "after": elementB }
}