b38c720033773b66880780b0c8d0ac6d326468e2
[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) {
18         return this._cmds[name](canvas, params);
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').transform('smartxml.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
51
52         doc.transform('extractItems', {item1: node1, item2: node2});
53
54         //canvas.list.extractItems({element1: parent1, element2: parent2});
55         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
56     } else if(!cursor.isSelecting()) {
57         var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
58             parentNode = nodeToUnwrap.unwrap();
59         if(parentNode) {
60             canvas.setCurrentElement(utils.findCanvasElement(parentNode));
61         }
62     }
63 });
64
65 commands.register('wrap-node', function(canvas) {
66     var cursor = canvas.getCursor(),
67         selectionStart = cursor.getSelectionStart(),
68         selectionEnd = cursor.getSelectionEnd(),
69         parent1 = selectionStart.element.parent() || undefined,
70         parent2 = selectionEnd.element.parent() || undefined;
71
72     var node1 = parent1.data('wlxmlNode'),
73         node2 = parent2.data('wlxmlNode'),
74         doc = node1.document;
75
76     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
77         //canvas.list.create({element1: parent1, element2: parent2});
78         //doc.transform('createList', {node1: node1, node2: node2});
79         doc.createList({node1: node1, node2: node2});
80     }
81 });
82
83 commands.register('list', function(canvas, params) {
84     void(params);
85     var cursor = canvas.getCursor(),
86         selectionStart = cursor.getSelectionStart(),
87         selectionEnd = cursor.getSelectionEnd(),
88         parent1 = selectionStart.element.parent() || undefined,
89         parent2 = selectionEnd.element.parent() || undefined;
90
91     var selectionFocus = cursor.getSelectionFocus();
92
93     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
94         return;
95     }
96
97     var node1 = parent1.data('wlxmlNode'),
98         node2 = parent2.data('wlxmlNode'),
99         doc = node1.document;
100     
101     //doc.transform('createList', {node1: node1, node2: node2});
102     doc.createList({node1: node1, node2: node2});
103
104
105     canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
106 });
107
108 commands.register('toggle-grid', function(canvas, params) {
109     canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
110     gridToggled = params.toggle;
111 });
112
113 commands.register('newNodeRequested', function(canvas, params) {
114     var cursor = canvas.getCursor(),
115         selectionStart = cursor.getSelectionStart(),
116         selectionEnd = cursor.getSelectionEnd(),
117         wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
118
119     if(cursor.isSelecting()) {
120         if(cursor.isSelectingSiblings()) {
121             if(cursor.isSelectingWithinElement()) {
122                 wlxmlNode = selectionStart.element.data('wlxmlNode');
123                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
124                     //wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
125                     //wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
126                 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
127                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
128                 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
129             }
130             else {
131                 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
132                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
133
134                 // var wrapper = wlxmlNode.wrapText({
135                 //     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
136                 //     offsetStart: selectionStart.offset,
137                 //     offsetEnd: selectionEnd.offset,
138                 //     textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
139                 // }),
140                 wrapper = wlxmlNode.transform('smartxml.wrapText', {
141                     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
142                     offsetStart: selectionStart.offset,
143                     offsetEnd: selectionEnd.offset,
144                     textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
145                 });
146                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
147                 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
148             }
149         } else {
150             var node1 = selectionStart.element.data('wlxmlNode'),
151                 node2 = selectionEnd.element.data('wlxmlNode'),
152                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
153
154             if(siblingParents) {
155                 // canvas.wlxmlDocument.wrapNodes({
156                 //     element1: siblingParents.node1,
157                 //     element2: siblingParents.node2,
158                 //     _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
159                 // });
160                 canvas.wlxmlDocument.transform('smartxml.wrapNodes', {
161                     node1: siblingParents.node1,
162                     node2: siblingParents.node2,
163                     _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
164                 });
165             }
166         }
167     } else if(canvas.getCurrentNodeElement()) {
168         wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
169             // wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
170         wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
171         canvas.setCurrentElement(utils.findCanvasElement(wrapper));
172     }
173
174
175 });
176
177 commands.register('footnote', function(canvas, params) {
178     void(params);
179     var cursor = canvas.getCursor(),
180         position = cursor.getPosition(),
181         asideElement;
182         
183
184     if(cursor.isSelectingWithinElement()) {
185         asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
186     } else {
187         asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
188         asideElement.append({text: ''});
189     }
190
191     asideElement.toggle(true);
192     canvas.setCurrentElement(asideElement);
193 });
194
195 commands.register('take-away-node', function(canvas) {
196     var position = canvas.getCursor().getPosition(),
197         element = position.element,
198         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
199
200     if(!nodeElement || !(nodeElement.parent())) {
201         return;
202     }
203
204
205     var range = nodeElement.data('wlxmlNode').unwrapContent();
206
207     if(element) {
208         var elementIsFirstChild = nodeElement.childIndex(element);
209         if(element.bound()) {
210             canvas.setCurrentElement(element, {caretTo: position.offset});
211         } else {
212             if(elementIsFirstChild) {
213                 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'end'});
214             } else {
215                 canvas.setCurrentElement(findCanvasElement(range.element2), {caretTo: 'end'});
216             }
217         }
218     } else {
219         canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'start'});
220     }
221
222 });
223
224
225 return {
226     run: function(name, params, canvas) {
227         return commands.run(name, params, canvas);
228     }
229 };
230
231 });