X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/8f44492501cad481f4d4ddcd58f7e9b26e2a7721..ab1445aee3c066a08d0602dd8b2e6996646d986f:/src/editor/modules/documentCanvas/canvas/keyboard.js diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js index 48fa0f9..99201bc 100644 --- a/src/editor/modules/documentCanvas/canvas/keyboard.js +++ b/src/editor/modules/documentCanvas/canvas/keyboard.js @@ -75,7 +75,7 @@ var handleKeyEvent = function(e, s) { }; // todo: whileRemoveWholetext var keyEventHandlers = [ - { + { // ctrl+x - prevented (?) applies: function(e, s) { return e.ctrlKey && e.key === KEYS.X && @@ -212,7 +212,7 @@ var keyEventHandlers = [ } } }, - { + { // backspace removing the last character in a span applies: function(e, s) { return s.type === 'caret' && s.element.wlxmlNode.parent().is({tagName: 'span'}) && @@ -230,7 +230,7 @@ var keyEventHandlers = [ {caretTo: params.ret ? params.ret.previousLen : (prevTextNode ? prevTextNode.getText().length : 0)}); } }, - { + { // backspace/delete through an edge (behaves weirdly at spans) applies: function(e, s) { return s.type === 'caret' && ( (s.isAtBeginning() && e.key === KEYS.BACKSPACE) || @@ -238,26 +238,21 @@ var keyEventHandlers = [ ); }, run: function(e,s) { - var direction, caretTo, cursorAtOperationEdge, goto, element; + var goto, element; if(e.key === KEYS.BACKSPACE) { - direction = 'above'; - caretTo = 'end'; - cursorAtOperationEdge = s.isAtBeginning(); element = s.element; } else { - direction = 'below'; - caretTo = 'start'; - cursorAtOperationEdge = s.isAtEnd(); - element = cursorAtOperationEdge && s.canvas.getNearestTextElement(direction, s.element); + element = s.canvas.getNearestTextElement('below', s.element); } - if(!cursorAtOperationEdge || !element) { + e.preventDefault(); + + if(!element) { return; } - var parent = element.wlxmlNode.parent(); if(element.wlxmlNode.getIndex() === 0 && parent.isContextRoot() && (!parent.is('item') || parent.getIndex() === 0)) { // Don't even try to do anything at the edge of a context root, except for non-first items @@ -265,8 +260,6 @@ var keyEventHandlers = [ return; } - e.preventDefault(); - s.canvas.wlxmlDocument.transaction(function() { if(element.wlxmlNode.getIndex() === 0) { goto = element.wlxmlNode.parent().moveUp(); @@ -284,7 +277,7 @@ var keyEventHandlers = [ } }, - { + { // backspace/delete last character in a node - why is it needed? applies: function(e,s) { return s.type === 'caret' && s.element.getText().length === 1 && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE); }, @@ -356,12 +349,14 @@ var keyEventHandlers = [ } }, - { + { // enter on an empty list item - creates paragraph after list applies: function(e, s) { var parent = s.element && s.element.wlxmlNode.parent(), parentIsItem = parent && parent.is('item'), - itemIsOnList = parent && parent.parent() && parent.parent().is('list'); - return s.type === 'caret' && e.key === KEYS.ENTER && s.element.isEmpty() && parentIsItem && itemIsOnList; + itemIsOnList = parent && parent.parent() && parent.parent().is('list'), + onlyChild = parent.contents().length === 1; + return s.type === 'caret' && e.key === KEYS.ENTER && s.element.isEmpty() && onlyChild && + parentIsItem && itemIsOnList; }, run: function(e, s) { var item = s.element.wlxmlNode.parent(), @@ -371,6 +366,9 @@ var keyEventHandlers = [ var p = list.after({tagName: 'div', attrs: {'class': 'p'}}); p.append({text: ''}); item.detach(); + if(list.contents().length === 0) { + list.detach(); + } return p; }, { success: function(p) { @@ -379,7 +377,7 @@ var keyEventHandlers = [ }); } }, - { + { // enter - split node applies: function(e, s) { return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement(); }, @@ -414,7 +412,7 @@ var keyEventHandlers = [ s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions); } }, - { + { // enter - new paragraph after image/video applies: function (e, s) { return s.type === 'nodeSelection' && e.key === KEYS.ENTER && !s.element.isRootElement(); }, @@ -428,7 +426,7 @@ var keyEventHandlers = [ result = s.element.wlxmlNode.insertNewNode(); }, { metadata: { - description: gettext('Splitting node'), + description: gettext('Inserting node'), fragment: s.toDocumentFragment() } });