};
// todo: whileRemoveWholetext
var keyEventHandlers = [
- {
+ { // ctrl+x - prevented (?)
applies: function(e, s) {
return e.ctrlKey &&
e.key === KEYS.X &&
}
}
},
- {
+ { // backspace removing the last character in a span
applies: function(e, s) {
return s.type === 'caret' &&
s.element.wlxmlNode.parent().is({tagName: 'span'}) &&
{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) ||
);
},
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;
}
- e.preventDefault();
+ 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
+ // - this is a temporary solution until key events handling get refactored into something more sane.
+ return;
+ }
s.canvas.wlxmlDocument.transaction(function() {
if(element.wlxmlNode.getIndex() === 0) {
}
},
- {
+ { // 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);
},
}, {
success: function() {
if(goto) {
+ if(!doc.containsNode(goto.wlxmlNode)) {
+ goto = s.startElement;
+ caretTo = s.startOffset;
+ }
s.canvas.setCurrentElement(goto, {caretTo: caretTo});
}
}
}
},
- {
+ { // 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(),
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) {
});
}
},
- {
+ { // enter - split node
applies: function(e, s) {
return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
},
run: function(e, s) {
- var result, goto, gotoOptions;
+ var parent = s.element.parent(),
+ children = parent.children(),
+ result, goto, gotoOptions;
void(e);
e.preventDefault();
+
+ if(children.length === 1 && s.element.isEmpty()) {
+ return;
+ }
+
s.canvas.wlxmlDocument.transaction(function() {
result = s.element.wlxmlNode.breakContent({offset: s.offset});
}, {
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();
+ },
+ run: function (e, s) {
+ var parent = s.element.parent(),
+ children = parent.children(),
+ result, goto, gotoOptions;
+ e.preventDefault();
+
+ s.canvas.wlxmlDocument.transaction(function() {
+ result = s.element.wlxmlNode.insertNewNode();
+ }, {
+ metadata: {
+ description: gettext('Inserting node'),
+ fragment: s.toDocumentFragment()
+ }
+ });
+
+ s.canvas.setCurrentElement(utils.getElementForNode(result), {caretTo: 'start'});
+ }
}
];