2 'modules/documentCanvas/canvas/documentElement',
4 ], function(documentElement, utils) {
9 var gridToggled = false;
13 register: function(name, command) {
14 this._cmds[name] = command;
17 run: function(name, params, canvas, user) {
18 return this._cmds[name](canvas, params, user);
22 commands.register('undo', function(canvas) {
23 var doc = canvas.wlxmlDocument;
28 commands.register('redo', function(canvas) {
29 var doc = canvas.wlxmlDocument;
34 commands.register('remove-node', function(canvas) {
35 canvas.getCurrentNodeElement().data('wlxmlNode').detach();
38 commands.register('unwrap-node', function(canvas) {
39 var cursor = canvas.getCursor(),
40 selectionStart = cursor.getSelectionStart(),
41 selectionEnd = cursor.getSelectionEnd(),
42 parent1 = selectionStart.element.parent() || undefined,
43 parent2 = selectionEnd.element.parent() || undefined;
45 var selectionAnchor = cursor.getSelectionAnchor(),
46 node1 = parent1.data('wlxmlNode'),
47 node2 = parent2.data('wlxmlNode'),
49 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
50 doc.extractItems({item1: node1, item2: node2});
51 canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
52 } else if(!cursor.isSelecting()) {
53 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
54 parentNode = nodeToUnwrap.unwrap();
56 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
61 commands.register('wrap-node', function(canvas) {
62 var cursor = canvas.getCursor(),
63 selectionStart = cursor.getSelectionStart(),
64 selectionEnd = cursor.getSelectionEnd(),
65 parent1 = selectionStart.element.parent() || undefined,
66 parent2 = selectionEnd.element.parent() || undefined;
68 var node1 = parent1.data('wlxmlNode'),
69 node2 = parent2.data('wlxmlNode'),
72 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
73 doc.createList({node1: node1, node2: node2});
77 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;
85 var selectionFocus = cursor.getSelectionFocus();
87 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
91 var node1 = parent1.data('wlxmlNode'),
92 node2 = parent2.data('wlxmlNode'),
95 doc.createList({node1: node1, node2: node2});
96 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
99 commands.register('toggle-grid', function(canvas, params) {
100 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
101 gridToggled = params.toggle;
104 commands.register('newNodeRequested', function(canvas, params, user) {
105 var cursor = canvas.getCursor(),
106 selectionStart = cursor.getSelectionStart(),
107 selectionEnd = cursor.getSelectionEnd(),
108 wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
110 var insertNode = function(insertion) {
111 var doc = canvas.wlxmlDocument,
112 node, metadata, creator, currentDate, dt;
114 var pad = function(number) {
116 number = '0' + number;
121 doc.startTransaction();
123 if(node.getTagName() === 'aside' && node.getClass() === 'comment') {
127 creator += ' (' + user.email + ')';
130 creator = 'anonymous';
133 currentDate = new Date();
134 dt = pad(currentDate.getDate()) + '-' +
135 pad((currentDate.getMonth() + 1)) + '-' +
136 pad(currentDate.getFullYear()) + ' ' +
137 pad(currentDate.getHours()) + ':' +
138 pad(currentDate.getMinutes()) + ':' +
139 pad(currentDate.getSeconds());
141 metadata = node.getMetadata();
142 metadata.add({key: 'creator', value: creator});
143 metadata.add({key: 'date', value: dt});
145 doc.endTransaction();
150 if(cursor.isSelecting()) {
151 if(cursor.isSelectingSiblings()) {
152 if(cursor.isSelectingWithinElement()) {
153 wlxmlNode = selectionStart.element.data('wlxmlNode');
154 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
156 wrapper = insertNode(function() {
157 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
159 wrapperCanvasElement = utils.findCanvasElement(wrapper);
160 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
163 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
164 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
166 wrapper = insertNode(function() {
167 return wlxmlNode.wrapText({
168 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
169 offsetStart: selectionStart.offset,
170 offsetEnd: selectionEnd.offset,
171 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
174 wrapperCanvasElement = utils.findCanvasElement(wrapper);
175 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
178 var node1 = selectionStart.element.data('wlxmlNode'),
179 node2 = selectionEnd.element.data('wlxmlNode'),
180 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
183 insertNode(function() {
184 return canvas.wlxmlDocument.wrapNodes({
185 node1: siblingParents.node1,
186 node2: siblingParents.node2,
187 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
192 } else if(canvas.getCurrentNodeElement()) {
193 wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
195 wrapper = insertNode(function() {
196 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
199 wrapper = insertNode(function() {
200 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
201 node.append({text:''});
205 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
211 commands.register('footnote', function(canvas, params) {
213 var cursor = canvas.getCursor(),
214 position = cursor.getPosition(),
215 asideNode, asideElement;
218 if(cursor.isSelectingWithinElement()) {
219 asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
221 asideNode = position.element.data('wlxmlNode').divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
222 asideNode.append({text: ''});
225 asideElement = utils.findCanvasElement(asideNode);
226 asideElement.toggle(true);
227 canvas.setCurrentElement(asideElement);
230 commands.register('take-away-node', function(canvas) {
231 var position = canvas.getCursor().getPosition(),
232 element = position.element,
233 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
235 if(!nodeElement || !(nodeElement.parent())) {
239 var range = nodeElement.data('wlxmlNode').unwrapContent();
242 var elementIsFirstChild = nodeElement.childIndex(element);
243 if(element.bound()) {
244 canvas.setCurrentElement(element, {caretTo: position.offset});
246 if(elementIsFirstChild) {
247 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
249 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
253 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
260 run: function(name, params, canvas, user) {
261 return commands.run(name, params, canvas, user);