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