ac56a313a98dd7f58622430e147c67c4924a057f
[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('unwrap-node', function(canvas) {
23     var cursor = canvas.getCursor(),
24         selectionStart = cursor.getSelectionStart(),
25         selectionEnd = cursor.getSelectionEnd(),
26         parent1 = selectionStart.element.parent() || undefined,
27         parent2 = selectionEnd.element.parent() || undefined;
28
29     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
30         return;
31         // TODO
32         // var selectionAnchor = cursor.getSelectionAnchor();
33         // canvas.list.extractItems({element1: parent1, element2: parent2});
34         // canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
35     } else if(!cursor.isSelecting()) {
36         var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
37             parentNode = nodeToUnwrap.unwrap();
38         if(parentNode) {
39             canvas.setCurrentElement(utils.findCanvasElement(parentNode));
40         }
41     }
42 });
43
44 // commands.register('wrap-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 //         canvas.list.create({element1: parent1, element2: parent2});
53 //     }
54 // });
55
56 commands.register('list', function(canvas, params) {
57     var cursor = canvas.getCursor(),
58         selectionStart = cursor.getSelectionStart(),
59         selectionEnd = cursor.getSelectionEnd(),
60         parent1 = selectionStart.element.parent() || undefined,
61         parent2 = selectionEnd.element.parent() || undefined;
62
63     var selectionFocus = cursor.getSelectionFocus();
64
65     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
66         return;
67     }
68
69     canvas.list.create({element1: parent1, element2: parent2});
70
71     canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
72 });
73
74 commands.register('toggle-grid', function(canvas, params) {
75     canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
76     gridToggled = params.toggle;
77 });
78
79 commands.register('newNodeRequested', function(canvas, params) {
80     var cursor = canvas.getCursor(),
81         selectionStart = cursor.getSelectionStart(),
82         selectionEnd = cursor.getSelectionEnd();
83
84     if(cursor.isSelecting()) {
85         if(cursor.isSelectingSiblings()) {
86             if(cursor.isSelectingWithinElement()) {
87                 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
88                     caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
89                     wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
90                     wrapperCanvasElement = utils.findCanvasElement(wrapper);
91                 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
92             }
93             else {
94                 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
95                     caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
96
97                 var wrapper = wlxmlNode.wrapText({
98                     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
99                     offsetStart: selectionStart.offset,
100                     offsetEnd: selectionEnd.offset,
101                     textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
102                 }),
103                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
104                 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
105             }
106         } else {
107             var node1 = selectionStart.element.data('wlxmlNode'),
108                 node2 = selectionEnd.element.data('wlxmlNode'),
109                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
110
111             if(siblingParents) {
112                 canvas.wlxmlDocument.wrapNodes({
113                     element1: siblingParents.node1,
114                     element2: siblingParents.node2,
115                     _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
116                 });
117             }
118         }
119     } else if(canvas.getCurrentNodeElement()) {
120         var node = findCanvasElement(canvas.getCurrentNodeElement),
121             wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
122         canvas.setCurrentElement(findCanvasElement(wrapper));
123     }
124
125
126 });
127
128 commands.register('footnote', function(canvas, params) {
129     var cursor = canvas.getCursor(),
130         position = cursor.getPosition(),
131         asideElement;
132         
133
134     if(cursor.isSelectingWithinElement()) {
135         asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
136     } else {
137         asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
138         asideElement.append({text: ''});
139     }
140
141     asideElement.toggle(true);
142     canvas.setCurrentElement(asideElement);
143 });
144
145 commands.register('take-away-node', function(canvas) {
146     var position = canvas.getCursor().getPosition(),
147         element = position.element,
148         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
149
150     if(!nodeElement || !(nodeElement.parent()))
151         return;
152
153
154     var range = nodeElement.data('wlxmlNode').unwrapContent();
155
156     if(element) {
157         var elementIsFirstChild = nodeElement.childIndex(element);
158         if(element.bound()) {
159             canvas.setCurrentElement(element, {caretTo: position.offset});
160         } else {
161             if(elementIsFirstChild) {
162                 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'end'});
163             } else {
164                 canvas.setCurrentElement(findCanvasElement(range.element2), {caretTo: 'end'});
165             }
166         }
167     } else {
168         canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'start'});
169     }
170
171 });
172
173
174 return {
175     run: function(name, params, canvas) {
176         return commands.run(name, params, canvas);
177     }
178 };
179
180 });