Saving and restoring local draft of a document
[fnpeditor.git] / src / editor / modules / documentCanvas / commands.js
1 define([
2 'modules/documentCanvas/canvas/documentElement',
3 './canvas/utils'
4 ], function(documentElement, utils) {
5     
6 'use strict';
7
8
9 var gridToggled = false;
10
11 var commands = {
12     _cmds: {},
13     register: function(name, command) {
14         this._cmds[name] = command;
15     },
16
17     run: function(name, params, canvas, user) {
18         return this._cmds[name](canvas, params, user);
19     }
20 };
21
22 commands.register('undo', function(canvas) {
23     var doc = canvas.wlxmlDocument;
24
25     doc.undo();
26 });
27
28 commands.register('redo', function(canvas) {
29     var doc = canvas.wlxmlDocument;
30
31     doc.redo();
32 });
33
34 commands.register('remove-node', function(canvas) {
35     canvas.getCurrentNodeElement().data('wlxmlNode').detach();
36 });
37
38 commands.register('unwrap-node', function(canvas) {
39     var cursor = canvas.getCursor(),
40         selectionStart = cursor.getSelectionStart(),
41         selectionEnd = cursor.getSelectionEnd(),
42         parent1 = selectionStart.element.parent() || undefined,
43         parent2 = selectionEnd.element.parent() || undefined;
44
45     var selectionAnchor = cursor.getSelectionAnchor(),
46         node1 = parent1.data('wlxmlNode'),
47         node2 = parent2.data('wlxmlNode'),
48         doc = node1.document;
49     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
50         doc.extractItems({item1: node1, item2: node2});
51         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
52     } else if(!cursor.isSelecting()) {
53         var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
54             parentNode = nodeToUnwrap.unwrap();
55         if(parentNode) {
56             canvas.setCurrentElement(utils.findCanvasElement(parentNode));
57         }
58     }
59 });
60
61 commands.register('wrap-node', function(canvas) {
62     var cursor = canvas.getCursor(),
63         selectionStart = cursor.getSelectionStart(),
64         selectionEnd = cursor.getSelectionEnd(),
65         parent1 = selectionStart.element.parent() || undefined,
66         parent2 = selectionEnd.element.parent() || undefined;
67
68     var node1 = parent1.data('wlxmlNode'),
69         node2 = parent2.data('wlxmlNode'),
70         doc = node1.document;
71
72     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
73         doc.createList({node1: node1, node2: node2});
74     }
75 });
76
77 commands.register('list', function(canvas, params) {
78     void(params);
79     var cursor = canvas.getCursor(),
80         selectionStart = cursor.getSelectionStart(),
81         selectionEnd = cursor.getSelectionEnd(),
82         parent1 = selectionStart.element.parent() || undefined,
83         parent2 = selectionEnd.element.parent() || undefined;
84
85     var selectionFocus = cursor.getSelectionFocus();
86
87     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
88         return;
89     }
90
91     var node1 = parent1.data('wlxmlNode'),
92         node2 = parent2.data('wlxmlNode'),
93         doc = node1.document;
94     
95     doc.createList({node1: node1, node2: node2});
96     canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
97 });
98
99 commands.register('toggle-grid', function(canvas, params) {
100     canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
101     gridToggled = params.toggle;
102 });
103
104 commands.register('newNodeRequested', function(canvas, params, user) {
105     var cursor = canvas.getCursor(),
106         selectionStart = cursor.getSelectionStart(),
107         selectionEnd = cursor.getSelectionEnd(),
108         wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
109
110     var insertNode = function(insertion) {
111         var doc = canvas.wlxmlDocument,
112             node, metadata, creator, currentDate, dt;
113
114         var pad = function(number) {
115             if(number < 10) {
116                 number = '0' + number;
117             }
118             return number;
119         };
120
121         doc.startTransaction();
122         node = insertion();
123         if(node.getTagName() === 'aside' && node.getClass() === 'comment') {
124             if(user) {
125                 creator = user.name;
126                 if(user.email) {
127                     creator += ' (' + user.email + ')';
128                 }
129             } else {
130                 creator = 'anonymous';
131             }
132
133             currentDate = new Date();
134             dt = pad(currentDate.getDate()) + '-' +
135                             pad((currentDate.getMonth() + 1))  + '-' +
136                             pad(currentDate.getFullYear()) + ' ' +
137                             pad(currentDate.getHours()) + ':' +
138                             pad(currentDate.getMinutes()) + ':' +
139                             pad(currentDate.getSeconds());
140
141             metadata = node.getMetadata();
142             metadata.add({key: 'creator', value: creator});
143             metadata.add({key: 'date', value: dt});
144         }
145         doc.endTransaction();
146         return node;
147     };
148
149
150     if(cursor.isSelecting()) {
151         if(cursor.isSelectingSiblings()) {
152             if(cursor.isSelectingWithinElement()) {
153                 wlxmlNode = selectionStart.element.data('wlxmlNode');
154                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
155
156                 wrapper = insertNode(function() {
157                     return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
158                 });
159                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
160                 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
161             }
162             else {
163                 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
164                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
165
166                 wrapper = insertNode(function() {
167                     return wlxmlNode.wrapText({
168                         _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
169                         offsetStart: selectionStart.offset,
170                         offsetEnd: selectionEnd.offset,
171                         textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
172                     });
173                 });
174                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
175                 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
176             }
177         } else {
178             var node1 = selectionStart.element.data('wlxmlNode'),
179                 node2 = selectionEnd.element.data('wlxmlNode'),
180                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
181
182             if(siblingParents) {
183                 insertNode(function() {
184                     return canvas.wlxmlDocument.wrapNodes({
185                         node1: siblingParents.node1,
186                         node2: siblingParents.node2,
187                         _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
188                     });
189                 });
190             }
191         }
192     } else if(canvas.getCurrentNodeElement()) {
193         wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
194         if(params.ctrlKey) {
195             wrapper = insertNode(function() {
196                 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
197             });
198         } else {
199             wrapper = insertNode(function() {
200                 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
201                 node.append({text:''});
202                 return node;
203             });
204         }
205         canvas.setCurrentElement(utils.findCanvasElement(wrapper));
206     }
207
208
209 });
210
211 commands.register('footnote', function(canvas, params) {
212     void(params);
213     var cursor = canvas.getCursor(),
214         position = cursor.getPosition(),
215         asideNode, asideElement, node;
216         
217
218     if(cursor.isSelectingWithinElement()) {
219         asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
220     } else {
221         node = position.element.data('wlxmlNode');
222         node.document.transaction(function() {
223             asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
224             asideNode.append({text: ''});
225         });
226     }
227
228     asideElement = utils.findCanvasElement(asideNode);
229     asideElement.toggle(true);
230     canvas.setCurrentElement(asideElement);
231 });
232
233 commands.register('take-away-node', function(canvas) {
234     var position = canvas.getCursor().getPosition(),
235         element = position.element,
236         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
237
238     if(!nodeElement || !(nodeElement.parent())) {
239         return;
240     }
241
242     var range = nodeElement.data('wlxmlNode').unwrapContent();
243
244     if(element) {
245         var elementIsFirstChild = nodeElement.childIndex(element);
246         if(element.bound()) {
247             canvas.setCurrentElement(element, {caretTo: position.offset});
248         } else {
249             if(elementIsFirstChild) {
250                 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
251             } else {
252                 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
253             }
254         }
255     } else {
256         canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
257     }
258
259 });
260
261
262 return {
263     run: function(name, params, canvas, user) {
264         return commands.run(name, params, canvas, user);
265     }
266 };
267
268 });