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