043d6b77898419de6e0b67bb7170324e9f5d07c2
[fnpeditor.git] / modules / documentCanvas / commands.js
1 define([
2 'modules/documentCanvas/canvas/documentElement'
3 ], function(documentElement) {
4     
5 'use strict';
6
7
8 var gridToggled = false;
9
10 var commands = {
11     _cmds: {},
12     register: function(name, command) {
13         this._cmds[name] = command;
14     },
15
16     run: function(name, params, canvas) {
17         return this._cmds[name](canvas, params);
18     }
19 };
20
21 commands.register('unwrap-node', function(canvas) {
22     var cursor = canvas.getCursor(),
23         selectionStart = cursor.getSelectionStart(),
24         selectionEnd = cursor.getSelectionEnd(),
25         parent1 = selectionStart.element.parent() || undefined,
26         parent2 = selectionEnd.element.parent() || undefined;
27
28     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
29         var selectionAnchor = cursor.getSelectionAnchor();
30         canvas.list.extractItems({element1: parent1, element2: parent2});
31         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
32     } else if(!cursor.isSelecting()) {
33         var toUnwrap = cursor.getPosition().element,
34             parent = toUnwrap.unwrap();
35         canvas.setCurrentElement(parent);
36     }
37 });
38
39 commands.register('wrap-node', function(canvas) {
40     var cursor = canvas.getCursor(),
41         selectionStart = cursor.getSelectionStart(),
42         selectionEnd = cursor.getSelectionEnd(),
43         parent1 = selectionStart.element.parent() || undefined,
44         parent2 = selectionEnd.element.parent() || undefined;
45
46     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
47         canvas.list.create({element1: parent1, element2: parent2});
48     }
49 });
50
51 commands.register('list', function(canvas, params) {
52     var cursor = canvas.getCursor(),
53         selectionStart = cursor.getSelectionStart(),
54         selectionEnd = cursor.getSelectionEnd(),
55         parent1 = selectionStart.element.parent() || undefined,
56         parent2 = selectionEnd.element.parent() || undefined;
57
58     var selectionFocus = cursor.getSelectionFocus();
59
60     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
61         return;
62     }
63
64     canvas.list.create({element1: parent1, element2: parent2});
65
66     canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
67 });
68
69 commands.register('toggle-grid', function(canvas, params) {
70     canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
71     gridToggled = params.toggle;
72 });
73
74 commands.register('newNodeRequested', function(canvas, params) {
75     var cursor = canvas.getCursor(),
76         selectionStart = cursor.getSelectionStart(),
77         selectionEnd = cursor.getSelectionEnd();
78
79     if(cursor.isSelecting()) {
80         if(cursor.isSelectingSiblings()) {
81             if(cursor.isSelectingWithinElement()) {
82                 var newElement = selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset}),
83                     caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
84                 canvas.setCurrentElement(newElement.children()[0], {caretTo: caretTo});
85             }
86             else {
87                 var parent = selectionStart.element.parent(),
88                     caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
89
90                 var wrapper = canvas.wrapText({
91                     inside: parent,
92                     _with: {tag: params.wlxmlTag, klass: params.wlxmlClass},
93                     offsetStart: selectionStart.offset,
94                     offsetEnd: selectionEnd.offset,
95                     textNodeIdx: [parent.childIndex(selectionStart.element), parent.childIndex(selectionEnd.element)]
96                 });
97
98                 canvas.setCurrentElement(wrapper.children()[caretTo === 0 ? 0 : wrapper.children().length - 1], {caretTo: caretTo});
99             }
100         } else {
101             var siblingParents = canvas.getSiblingParents({element1: selectionStart.element, element2: selectionEnd.element})
102             if(siblingParents) {
103                 canvas.wrapElements({
104                     element1: siblingParents.element1,
105                     element2: siblingParents.element2,
106                     _with: {tag: params.wlxmlTag, klass: params.wlxmlClass}
107                 });
108             }
109         }
110     } else if(canvas.getCurrentNodeElement()) {
111         var el = canvas.getCurrentNodeElement().wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass});
112         canvas.setCurrentElement(el);
113     }
114
115
116 });
117
118 commands.register('footnote', function(canvas, params) {
119     var cursor = canvas.getCursor(),
120         position = cursor.getPosition(),
121         asideElement;
122         
123
124     if(cursor.isSelectingWithinElement()) {
125         asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
126     } else {
127         asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
128         asideElement.append({text: ''});
129     }
130
131     asideElement.toggle(true);
132     canvas.setCurrentElement(asideElement);
133 });
134
135 commands.register('take-away-node', function(canvas) {
136     var position = canvas.getCursor().getPosition(),
137         element = position.element,
138         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
139
140     if(!nodeElement || !(nodeElement.parent()))
141         return;
142
143
144     var range = nodeElement.unwrapContents();
145
146     if(element) {
147         var elementIsFirstChild = nodeElement.childIndex(element);
148         if(element.bound()) {
149             canvas.setCurrentElement(element, {caretTo: position.offset});
150         } else {
151             if(elementIsFirstChild) {
152                 canvas.setCurrentElement(range.element1, {caretTo: 'end'});
153             } else {
154                 canvas.setCurrentElement(range.element2, {caretTo: 'end'});
155             }
156         }
157     } else {
158         canvas.setCurrentElement(range.element1, {caretTo: 'start'});
159     }
160
161 });
162
163
164 return {
165     run: function(name, params, canvas) {
166         return commands.run(name, params, canvas);
167     }
168 };
169
170 });