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