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