2bd77c021465a6cc3220780106c131fbc066d3fc
[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').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().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
101     gridToggled = params.toggle;
102 });
103
104 commands.register('newNodeRequested', function(canvas, params) {
105     var cursor = canvas.getCursor(),
106         selectionStart = cursor.getSelectionStart(),
107         selectionEnd = cursor.getSelectionEnd(),
108         wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
109
110     if(cursor.isSelecting()) {
111         if(cursor.isSelectingSiblings()) {
112             if(cursor.isSelectingWithinElement()) {
113                 wlxmlNode = selectionStart.element.data('wlxmlNode');
114                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
115
116                 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
117                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
118                 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
119             }
120             else {
121                 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
122                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
123
124                 wrapper = wlxmlNode.wrapText({
125                     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
126                     offsetStart: selectionStart.offset,
127                     offsetEnd: selectionEnd.offset,
128                     textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
129                 });
130                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
131                 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
132             }
133         } else {
134             var node1 = selectionStart.element.data('wlxmlNode'),
135                 node2 = selectionEnd.element.data('wlxmlNode'),
136                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
137
138             if(siblingParents) {
139                 canvas.wlxmlDocument.wrapNodes({
140                     node1: siblingParents.node1,
141                     node2: siblingParents.node2,
142                     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
143                 });
144             }
145         }
146     } else if(canvas.getCurrentNodeElement()) {
147         wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
148         wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
149         canvas.setCurrentElement(utils.findCanvasElement(wrapper));
150     }
151
152
153 });
154
155 commands.register('footnote', function(canvas, params) {
156     void(params);
157     var cursor = canvas.getCursor(),
158         position = cursor.getPosition(),
159         asideNode, asideElement;
160         
161
162     if(cursor.isSelectingWithinElement()) {
163         asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
164     } else {
165         asideNode = position.element.data('wlxmlNode').divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
166         asideNode.append({text: ''});
167     }
168
169     asideElement = utils.findCanvasElement(asideNode);
170     asideElement.toggle(true);
171     canvas.setCurrentElement(asideElement);
172 });
173
174 commands.register('take-away-node', function(canvas) {
175     var position = canvas.getCursor().getPosition(),
176         element = position.element,
177         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
178
179     if(!nodeElement || !(nodeElement.parent())) {
180         return;
181     }
182
183     var range = nodeElement.data('wlxmlNode').unwrapContent();
184
185     if(element) {
186         var elementIsFirstChild = nodeElement.childIndex(element);
187         if(element.bound()) {
188             canvas.setCurrentElement(element, {caretTo: position.offset});
189         } else {
190             if(elementIsFirstChild) {
191                 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
192             } else {
193                 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
194             }
195         }
196     } else {
197         canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
198     }
199
200 });
201
202
203 return {
204     run: function(name, params, canvas) {
205         return commands.run(name, params, canvas);
206     }
207 };
208
209 });