diff --git a/www/common/cursor.js b/www/common/cursor.js index 7614e44c4..07e705244 100644 --- a/www/common/cursor.js +++ b/www/common/cursor.js @@ -11,7 +11,7 @@ define([ var verbose = function (x) { if (window.verboseMode) { console.log(x); } }; /* accepts the document used by the editor */ - return function (inner) { + var Cursor = function (inner) { var cursor = {}; // there ought to only be one cursor at a time, so let's just @@ -387,6 +387,58 @@ define([ } }; + cursor.lastTextNode = function () { + var lastEl = Tree.rightmostNode(inner); + if (lastEl && lastEl.nodeType === 3) { return lastEl; } + + var firstEl = Tree.leftmostNode(inner); + + while (lastEl !== firstEl) { + lastEl = Tree.previousNode(lastEl, inner); + if (lastEl && lastEl.nodeType === 3) { return lastEl; } + } + + return lastEl; + }; + + cursor.firstTextNode = function () { + var firstEl = Tree.leftmostNode(inner); + if (firstEl && firstEl.nodeType === 3) { return firstEl; } + + var lastEl = Tree.rightmostNode(inner); + + while (firstEl !== lastEl) { + firstEl = Tree.nextNode(firstEl, inner); + if (firstEl && firstEl.nodeType === 3) { return firstEl; } + } + return firstEl; + }; + + cursor.setToStart = function () { + var el = cursor.firstTextNode(); + if (!el) { return; } + fixStart(el, 0); + fixEnd(el, 0); + fixSelection(makeSelection(), makeRange()); + return el; + }; + + cursor.setToEnd = function () { + var el = cursor.lastTextNode(); + if (!el) { return; } + + var offset = el.textContent.length; + + fixStart(el, offset); + fixEnd(el, offset); + fixSelection(makeSelection(), makeRange()); + return el; + }; + return cursor; }; + + Cursor.Tree = Tree; + + return Cursor; }); diff --git a/www/pad/main.js b/www/pad/main.js index df854fd04..9d997aa7d 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -64,6 +64,7 @@ define([ logFights: true, fights: [], Cryptpad: Cryptpad, + Cursor: Cursor, }; var emitResize = module.emitResize = function () { @@ -131,7 +132,7 @@ define([ color: '#fff', }); - var cursor = window.cursor = Cursor(inner); + var cursor = module.cursor = Cursor(inner); var setEditable = module.setEditable = function (bool) { if (bool) { @@ -718,6 +719,13 @@ define([ realtimeOptions.onLocal(); module.$userNameButton.click(); } + + editor.focus(); + if (newPad) { + cursor.setToEnd(); + } else { + cursor.setToFix(); + } }); };