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();
52 canvas.wlxmlDocument.transaction(function() {
53 var added = currentElement.wlxmlNode.after({
54 tagName: currentElement.getWlxmlTag() || 'div',
55 attrs: {'class': currentElement.getWlxmlClass() || 'p'}
57 added.append({text:''});
61 description: gettext('Splitting text')
63 success: function(ret) {
64 canvas.setCurrentElement(utils.findCanvasElement(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.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
82 added.append({text: ''});
86 description: gettext('Splitting text')
88 success: function(ret) {
89 canvas.setCurrentElement(utils.findCanvasElement(ret), {caretTo: 'start'});
95 if(!(element.parent().parent())) {
96 return false; // top level element is unsplittable
99 //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
100 // var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}),
105 // if(position.offsetAtBeginning)
106 // newEmpty = nodes.first;
107 // else if(position.offsetAtEnd)
108 // newEmpty = nodes.second;
111 // //goto = newEmpty.append({text: ''});
114 // goto = nodes.second;
115 // gotoOptions = {caretTo: 'start'};
117 var node = position.element.wlxmlNode,
118 result, goto, gotoOptions;
120 node.document.transaction(function() {
121 result = position.element.wlxmlNode.breakContent({offset: position.offset});
124 description: gettext('Splitting text')
128 if(result.emptyText) {
129 goto = result.emptyText;
132 goto = result.second;
133 gotoOptions = {caretTo: 'start'};
136 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
142 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
143 keydown: function(event, canvas) {
144 var position = canvas.getCursor().getPosition(),
145 element = position.element;
146 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
147 var direction, caretTo;
148 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
155 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom[0]));
157 canvas.setCurrentElement(el, {caretTo: caretTo});
161 keyup: function(event, canvas) {
162 var element = canvas.getCursor().getPosition().element,
167 var moves = [{direction: 'above', caretTo: 'end'}, {direction: 'below', caretTo: 'start'}];
168 if(event.which === KEYS.ARROW_RIGHT || event.which === KEYS.ARROW_DOWN) {
171 moves.some(function(move) {
173 var targetNode = utils.nearestInDocumentOrder('[document-text-element]:visible', move.direction, window.getSelection().focusNode);
175 element = canvas.getDocumentElement(targetNode);
176 caretTo = move.caretTo;
177 return true; // break
182 canvas.setCurrentElement(element, {caretTo: caretTo});
188 var selectsWholeTextElement = function(cursor) {
189 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
195 handlers.push({key: KEYS.X,
196 keydown: function(event, canvas) {
197 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
198 event.preventDefault();
203 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
204 keydown: function(event, canvas) {
205 var cursor = canvas.getCursor(),
206 position = canvas.getCursor().getPosition(),
207 element = position.element,
208 node = element ? element.wlxmlNode : null,
213 if(!element || !node) {
217 if(event.which === KEYS.DELETE) {
222 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
223 event.preventDefault();
224 var start = cursor.getSelectionStart(),
225 end = cursor.getSelectionEnd();
227 if(direction === 'above') {
228 if(start.offsetAtBeginning) {
229 goto = canvas.getNearestTextElement('above', start.element);
232 goto = start.element;
233 caretTo = start.offset;
236 if(end.offsetAtEnd) {
237 goto = canvas.getNearestTextElement('below', start.element);
245 canvas.wlxmlDocument.deleteText({
247 node: start.element.wlxmlNode,
251 node: end.element.wlxmlNode,
256 canvas.setCurrentElement(goto, {caretTo: caretTo});
261 var cursorAtOperationEdge = position.offsetAtBeginning;
262 if(event.which === KEYS.DELETE) {
263 cursorAtOperationEdge = position.offsetAtEnd;
266 var willDeleteWholeText = function() {
267 return element.getText().length === 1 || selectsWholeTextElement(cursor);
270 canvas.wlxmlDocument.transaction(function() {
271 if(willDeleteWholeText()) {
272 event.preventDefault();
275 else if(element.isEmpty()) {
276 event.preventDefault();
277 var parent = element.parent(),
278 grandParent = parent ? parent.parent() : null;
279 if(!grandParent && parent.children().length === 1) {
282 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
283 if(grandParent && grandParent.children().length === 1) {
284 goto = grandParent.wlxmlNode.append({text: ''});
286 goto = canvas.getNearestTextElement(direction, element);
288 parent.wlxmlNode.detach();
290 goto = canvas.getNearestTextElement(direction, element);
291 element.wlxmlNode.detach();
293 canvas.setCurrentElement(goto, {caretTo: caretTo});
295 else if(cursorAtOperationEdge) {
296 if(direction === 'below') {
297 element = canvas.getNearestTextElement(direction, element);
300 goto = element.wlxmlNode.mergeContentUp();
302 canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
305 event.preventDefault();
309 description: gettext('Remove text')