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