remove dead cursor code
This commit is contained in:
parent
b3867429ee
commit
9aa0548b1f
@ -2,10 +2,6 @@ define([
|
|||||||
'/common/treesome.js',
|
'/common/treesome.js',
|
||||||
'/bower_components/rangy/rangy-core.min.js'
|
'/bower_components/rangy/rangy-core.min.js'
|
||||||
], function (Tree, Rangy, saveRestore) {
|
], function (Tree, Rangy, saveRestore) {
|
||||||
//window.Rangy = Rangy;
|
|
||||||
//window.Tree = Tree;
|
|
||||||
// do some function for the start and end of the cursor
|
|
||||||
|
|
||||||
var log = function (x) { console.log(x); };
|
var log = function (x) { console.log(x); };
|
||||||
var error = function (x) { console.log(x); };
|
var error = function (x) { console.log(x); };
|
||||||
var verbose = function (x) { if (window.verboseMode) { console.log(x); } };
|
var verbose = function (x) { if (window.verboseMode) { console.log(x); } };
|
||||||
@ -27,108 +23,6 @@ define([
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO deprecate
|
|
||||||
// assumes a negative index
|
|
||||||
var seekLeft /* = cursor.seekLeft*/ = function (el, delta, current) {
|
|
||||||
var textLength;
|
|
||||||
var previous;
|
|
||||||
|
|
||||||
// normalize
|
|
||||||
|
|
||||||
if (-delta >= current) {
|
|
||||||
delta += current;
|
|
||||||
current = 0;
|
|
||||||
} else {
|
|
||||||
current += delta;
|
|
||||||
delta = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (delta) {
|
|
||||||
previous = el;
|
|
||||||
el = Tree.previousNode(el, inner);
|
|
||||||
if (el) {
|
|
||||||
textLength = el.textContent.length;
|
|
||||||
if (-delta > textLength) {
|
|
||||||
delta -= textLength;
|
|
||||||
} else {
|
|
||||||
current = textLength + delta;
|
|
||||||
delta = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
el: previous,
|
|
||||||
offset: 0,
|
|
||||||
error: "out of bounds"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
el: el,
|
|
||||||
offset: current
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO deprecate
|
|
||||||
// seekRight assumes a positive delta
|
|
||||||
var seekRight = /* cursor.seekRight = */ function (el, delta, current) {
|
|
||||||
var textLength;
|
|
||||||
var previous;
|
|
||||||
|
|
||||||
// normalize
|
|
||||||
delta += current;
|
|
||||||
current = 0;
|
|
||||||
|
|
||||||
while (delta) {
|
|
||||||
if (el) {
|
|
||||||
textLength = el.textContent.length;
|
|
||||||
if (delta >= textLength) {
|
|
||||||
delta -= textLength;
|
|
||||||
previous = el;
|
|
||||||
el = Tree.nextNode(el, inner);
|
|
||||||
} else {
|
|
||||||
current = delta;
|
|
||||||
delta = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// don't ever return a negative index
|
|
||||||
if (previous.textContent.length) {
|
|
||||||
textLength = previous.textContent.length - 1;
|
|
||||||
} else {
|
|
||||||
textLength = 0;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
el: previous,
|
|
||||||
offset: textLength,
|
|
||||||
error: "out of bounds"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
el: el,
|
|
||||||
offset: current
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO deprecate
|
|
||||||
var seekToDelta = /* cursor.seekToDelta = */ function (el, delta, current) {
|
|
||||||
var result = null;
|
|
||||||
if (el) {
|
|
||||||
if (delta < 0) {
|
|
||||||
return seekLeft(el, delta, current);
|
|
||||||
} else if (delta > 0) {
|
|
||||||
return seekRight(el, delta, current);
|
|
||||||
} else {
|
|
||||||
result = {
|
|
||||||
el: el,
|
|
||||||
offset: current
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error("[cursor.seekToDelta] el is undefined");
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* cursor.update takes notes about wherever the cursor was last seen
|
/* cursor.update takes notes about wherever the cursor was last seen
|
||||||
in the event of a cursor loss, the information produced by side
|
in the event of a cursor loss, the information produced by side
|
||||||
effects of this function should be used to recover the cursor
|
effects of this function should be used to recover the cursor
|
||||||
@ -262,109 +156,6 @@ define([
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/* getLength assumes that both nodes exist inside of the active editor. */
|
|
||||||
// unused currently
|
|
||||||
var getLength = cursor.getLength = function () {
|
|
||||||
if (Range.start.el === Range.end.el) {
|
|
||||||
if (Range.start.offset === Range.end.offset) { return 0; }
|
|
||||||
if (Range.start.offset < Range.end.offset) {
|
|
||||||
return Range.end.offset - Range.start.offset;
|
|
||||||
} else {
|
|
||||||
return Range.start.offset - Range.end.offset;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var order = Tree.orderOfNodes(Range.start.el, Range.end.el, inner);
|
|
||||||
var L;
|
|
||||||
var cur;
|
|
||||||
|
|
||||||
/* we know that the cursor elements are different, and that we
|
|
||||||
must traverse to find the total length. We also know the
|
|
||||||
order of the nodes (probably 1 or -1) */
|
|
||||||
if (order === 1) {
|
|
||||||
L = (Range.start.el.textContent.length - Range.start.offset);
|
|
||||||
cur = Tree.nextNode(Range.start.el, inner);
|
|
||||||
while (cur && cur !== Range.end.el) {
|
|
||||||
L += cur.textContent.length;
|
|
||||||
cur = Tree.nextNode(cur, inner);
|
|
||||||
}
|
|
||||||
L += Range.end.offset;
|
|
||||||
return L;
|
|
||||||
} else if (order === -1) {
|
|
||||||
L = (Range.end.el.textContent - Range.end.offset);
|
|
||||||
cur = Tree.nextNode(Range.end.el, inner);
|
|
||||||
while (cur && cur !== Range.start.el) {
|
|
||||||
L += cur.textContent.length;
|
|
||||||
cur = Tree.nextNode(cur, inner);
|
|
||||||
}
|
|
||||||
L += Range.start.offset;
|
|
||||||
return -L;
|
|
||||||
} else {
|
|
||||||
console.error("unexpected ordering of nodes...");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// previously used for testing
|
|
||||||
// TODO deprecate
|
|
||||||
var delta = /* cursor.delta = */ function (delta1, delta2) {
|
|
||||||
var sel = Rangy.getSelection(inner);
|
|
||||||
delta2 = (typeof delta2 !== 'undefined') ? delta2 : delta1;
|
|
||||||
|
|
||||||
// update returns errors if there are problems
|
|
||||||
// and updates the persistent Range object
|
|
||||||
var err = cursor.update(sel, inner);
|
|
||||||
if (err) { return err; }
|
|
||||||
|
|
||||||
// create a range to modify
|
|
||||||
var range = Rangy.createRange();
|
|
||||||
|
|
||||||
/*
|
|
||||||
The assumption below is that Range.(start|end).el
|
|
||||||
actually exists. This might not be the case.
|
|
||||||
TODO check if start and end elements are defined
|
|
||||||
*/
|
|
||||||
|
|
||||||
// using infromation about wherever you were last...
|
|
||||||
// move both parts by some delta
|
|
||||||
var start = seekToDelta(Range.start.el, delta1, Range.start.offset);
|
|
||||||
var end = seekToDelta(Range.end.el, delta2, Range.end.offset);
|
|
||||||
|
|
||||||
/* if range is backwards, cursor.delta fails
|
|
||||||
so check if they're in the expected order
|
|
||||||
before setting the new range */
|
|
||||||
|
|
||||||
var order = Tree.orderOfNodes(start.el, end.el, inner);
|
|
||||||
var backward;
|
|
||||||
|
|
||||||
// this could all be one line but nobody would be able to read it
|
|
||||||
if (order === -1) {
|
|
||||||
// definitely backward
|
|
||||||
backward = true;
|
|
||||||
} else if (order === 0) {
|
|
||||||
// might be backward, check offsets to know for sure
|
|
||||||
backward = (start.offset > end.offset);
|
|
||||||
} else {
|
|
||||||
// definitely not backward
|
|
||||||
backward = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (backward) {
|
|
||||||
range.setStart(end.el, end.offset);
|
|
||||||
range.setEnd(start.el, start.offset);
|
|
||||||
} else {
|
|
||||||
range.setStart(start.el, start.offset);
|
|
||||||
range.setEnd(end.el, end.offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
// actually set the cursor to the new range
|
|
||||||
sel.setSingleRange(range);
|
|
||||||
return {
|
|
||||||
startError: start.error,
|
|
||||||
endError: end.error
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
cursor.brFix = function () {
|
cursor.brFix = function () {
|
||||||
cursor.update();
|
cursor.update();
|
||||||
var start = Range.start;
|
var start = Range.start;
|
||||||
|
|||||||
@ -1,91 +1,6 @@
|
|||||||
define([], function () {
|
define([], function () {
|
||||||
var tree = {};
|
var tree = {};
|
||||||
|
|
||||||
// FIXME this isn't being used
|
|
||||||
var someElement = tree.some = function (root, predicate) {
|
|
||||||
// take the index of the last element in the current root
|
|
||||||
var last = root.childElementCount - 1;
|
|
||||||
|
|
||||||
// it might be a leaf node
|
|
||||||
if (last < 0) { return false; }
|
|
||||||
|
|
||||||
// otherwise it has children
|
|
||||||
while (last >= 0) {
|
|
||||||
// check from back to front
|
|
||||||
|
|
||||||
// check the node's children (depth first)
|
|
||||||
// if the predicate tests true, return true
|
|
||||||
if (tree.some(root.children[last], predicate)) {
|
|
||||||
return true;
|
|
||||||
} // otherwise none of the nodes inside it matched.
|
|
||||||
|
|
||||||
// check the node itself
|
|
||||||
if (predicate(root.children[last], last)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
last--;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
// FIXME this isn't being used
|
|
||||||
var someText = tree.someIncludingText = function (root, predicate) {
|
|
||||||
// take the index of the last element in the current root
|
|
||||||
var last = root.childNodes.length - 1;
|
|
||||||
|
|
||||||
// it might be a leaf node
|
|
||||||
if (last < 0) { return false; }
|
|
||||||
|
|
||||||
// otherwise it has children
|
|
||||||
while (last >= 0) {
|
|
||||||
// check from back to front
|
|
||||||
|
|
||||||
// check the node's children (depth first)
|
|
||||||
// if the predicate tests true, return true
|
|
||||||
if (tree.someIncludingText(root.childNodes[last], predicate)) {
|
|
||||||
return true;
|
|
||||||
} // otherwise none of the nodes inside it matched.
|
|
||||||
|
|
||||||
// check the node itself
|
|
||||||
if (predicate(root.childNodes[last], last)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
last--;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
// FIXME not being used
|
|
||||||
tree.findSameHierarchy = function (list, ancestor) {
|
|
||||||
var i = 0;
|
|
||||||
var success = true;
|
|
||||||
var last = list.length - 1;
|
|
||||||
var el;
|
|
||||||
|
|
||||||
tree.someIncludingText(ancestor, function (e) {
|
|
||||||
// don't out of bounds
|
|
||||||
if (i > last) {
|
|
||||||
// unsuccessful
|
|
||||||
success = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (list[i] === (e.tagName||e.nodeName)) {
|
|
||||||
|
|
||||||
if (i === last) {
|
|
||||||
el = e;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
} else {
|
|
||||||
// hierarchy has changed, what should we do?
|
|
||||||
success = false;
|
|
||||||
return true; // terminate
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return success? el: false;
|
|
||||||
};
|
|
||||||
|
|
||||||
var indexOfNode = tree.indexOfNode = function (el) {
|
var indexOfNode = tree.indexOfNode = function (el) {
|
||||||
if (!(el && el.parentNode)) {
|
if (!(el && el.parentNode)) {
|
||||||
console.log("No parentNode found!");
|
console.log("No parentNode found!");
|
||||||
@ -107,13 +22,6 @@ define([], function () {
|
|||||||
return el.childNodes.length;
|
return el.childNodes.length;
|
||||||
};
|
};
|
||||||
|
|
||||||
var parentsOf = tree.parentsOf = function (el, root) {
|
|
||||||
var P = [];
|
|
||||||
var p = el;
|
|
||||||
while (p !== root) { P.push((p = p.parentNode)); }
|
|
||||||
return P;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* rightmost and leftmost return the deepest right and left
|
/* rightmost and leftmost return the deepest right and left
|
||||||
leaf nodes of a tree
|
leaf nodes of a tree
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user