2 'modules/documentCanvas/canvas/documentElement',
3 'modules/documentCanvas/canvas/utils'
4 ], function(documentElement, utils) {
20 var handleKey = function(event, canvas) {
21 handlers.some(function(handler) {
22 if(handles(handler, event) && handler[event.type]) {
23 handler[event.type](event, canvas);
29 var handles = function(handler, event) {
30 if(handler.key === event.which) {
33 if(handler.keys && handler.keys.indexOf(event.which) !== -1) {
42 handlers.push({key: KEYS.ENTER,
43 keydown: function(event, canvas) {
44 event.preventDefault();
45 var cursor = canvas.getCursor(),
46 position = cursor.getPosition(),
47 element = position.element;
49 if(Object.keys(cursor.getPosition()).length === 0) {
50 var currentElement = canvas.getCurrentNodeElement();
51 if(currentElement && !currentElement.wlxmlNode.isRoot()) {
52 canvas.wlxmlDocument.transaction(function() {
53 var added = currentElement.wlxmlNode.after({
54 tagName: currentElement.wlxmlNode.getTagName() || 'div',
55 attrs: {'class': currentElement.wlxmlNode.getClass() || 'p'}
57 added.append({text:''});
61 description: gettext('Splitting text')
63 success: function(ret) {
64 canvas.setCurrentElement(utils.getElementForNode(ret), {caretTo: 'start'});
72 if(!cursor.isSelecting()) {
74 if(element instanceof documentElement.DocumentTextElement) {
75 element = element.parent();
78 canvas.wlxmlDocument.transaction(function() {
79 var added = element.wlxmlNode.after(
80 {tagName: element.wlxmlNode.getTagName() || 'div', attrs: {'class': element.wlxmlNode.getClass() || 'p'}}
82 added.append({text: ''});
86 description: gettext('Splitting text')
88 success: function(ret) {
89 canvas.setCurrentElement(utils.getElementForNode(ret), {caretTo: 'start'});
95 if(!(element.parent().parent())) {
96 return false; // top level element is unsplittable
99 var node = position.element.wlxmlNode,
100 result, goto, gotoOptions;
102 node.document.transaction(function() {
103 result = position.element.wlxmlNode.breakContent({offset: position.offset});
106 description: gettext('Splitting text')
110 if(result.emptyText) {
111 goto = result.emptyText;
114 goto = result.second;
115 gotoOptions = {caretTo: 'start'};
118 canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
124 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
125 keydown: function(event, canvas) {
126 var position = canvas.getCursor().getPosition(),
127 element = position.element;
128 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
129 var direction, caretTo;
130 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
137 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom[0]));
139 canvas.setCurrentElement(el, {caretTo: caretTo});
143 keyup: function(event, canvas) {
144 var element = canvas.getCursor().getPosition().element,
149 var moves = [{direction: 'above', caretTo: 'end'}, {direction: 'below', caretTo: 'start'}];
150 if(event.which === KEYS.ARROW_RIGHT || event.which === KEYS.ARROW_DOWN) {
153 moves.some(function(move) {
155 var targetNode = utils.nearestInDocumentOrder('[document-text-element]:visible', move.direction, window.getSelection().focusNode);
157 element = canvas.getDocumentElement(targetNode);
158 caretTo = move.caretTo;
159 return true; // break
164 canvas.setCurrentElement(element, {caretTo: caretTo});
170 var selectsWholeTextElement = function(cursor) {
171 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
177 handlers.push({key: KEYS.X,
178 keydown: function(event, canvas) {
179 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
180 event.preventDefault();
185 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
186 keydown: function(event, canvas) {
187 var cursor = canvas.getCursor(),
188 position = canvas.getCursor().getPosition(),
189 element = position.element,
190 node = element ? element.wlxmlNode : null,
195 if(!element || !node) {
199 if(event.which === KEYS.DELETE) {
204 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
205 event.preventDefault();
206 var start = cursor.getSelectionStart(),
207 end = cursor.getSelectionEnd();
209 if(direction === 'above') {
210 if(start.offsetAtBeginning) {
211 goto = canvas.getNearestTextElement('above', start.element);
214 goto = start.element;
215 caretTo = start.offset;
218 if(end.offsetAtEnd) {
219 goto = canvas.getNearestTextElement('below', start.element);
227 canvas.wlxmlDocument.deleteText({
229 node: start.element.wlxmlNode,
233 node: end.element.wlxmlNode,
238 canvas.setCurrentElement(goto, {caretTo: caretTo});
243 var cursorAtOperationEdge = position.offsetAtBeginning;
244 if(event.which === KEYS.DELETE) {
245 cursorAtOperationEdge = position.offsetAtEnd;
248 var willDeleteWholeText = function() {
249 return element.getText().length === 1 || selectsWholeTextElement(cursor);
252 canvas.wlxmlDocument.transaction(function() {
253 if(willDeleteWholeText()) {
254 event.preventDefault();
257 else if(element.isEmpty()) {
258 event.preventDefault();
259 var parent = element.parent(),
260 grandParent = parent ? parent.parent() : null;
261 if(!grandParent && parent.children().length === 1) {
264 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
265 if(grandParent && grandParent.children().length === 1) {
266 goto = grandParent.wlxmlNode.append({text: ''});
268 goto = canvas.getNearestTextElement(direction, element);
270 parent.wlxmlNode.detach();
272 goto = canvas.getNearestTextElement(direction, element);
273 element.wlxmlNode.detach();
275 canvas.setCurrentElement(goto, {caretTo: caretTo});
277 else if(cursorAtOperationEdge) {
278 if(direction === 'below') {
279 element = canvas.getNearestTextElement(direction, element);
282 goto = element.wlxmlNode.mergeContentUp();
284 canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
287 event.preventDefault();
291 description: gettext('Remove text')