5 ], function(utils, Dialog, datetime) {
11 var gridToggled = false;
15 register: function(name, command) {
16 this._cmds[name] = command;
19 run: function(name, params, canvas, user) {
20 return this._cmds[name](canvas, params, user);
24 commands.register('undo', function(canvas) {
25 var doc = canvas.wlxmlDocument;
30 commands.register('redo', function(canvas) {
31 var doc = canvas.wlxmlDocument;
36 commands.register('remove-node', function(canvas) {
37 canvas.getCurrentNodeElement().wlxmlNode.detach();
40 commands.register('unwrap-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;
47 var selectionAnchor = cursor.getSelectionAnchor(),
48 node1 = parent1.wlxmlNode,
49 node2 = parent2.wlxmlNode,
51 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
52 doc.extractItems({item1: node1, item2: node2});
53 canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
54 } else if(!cursor.isSelecting()) {
55 var nodeToUnwrap = cursor.getPosition().element.wlxmlNode,
56 parentNode = nodeToUnwrap.unwrap();
58 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
63 commands.register('wrap-node', function(canvas) {
64 var cursor = canvas.getCursor(),
65 selectionStart = cursor.getSelectionStart(),
66 selectionEnd = cursor.getSelectionEnd(),
67 parent1 = selectionStart.element.parent() || undefined,
68 parent2 = selectionEnd.element.parent() || undefined;
70 var node1 = parent1.wlxmlNode,
71 node2 = parent2.wlxmlNode,
74 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
75 doc.createList({node1: node1, node2: node2});
79 commands.register('list', function(canvas, params) {
81 var cursor = canvas.getCursor(),
82 selectionStart = cursor.getSelectionStart(),
83 selectionEnd = cursor.getSelectionEnd(),
84 parent1 = selectionStart.element.parent() || undefined,
85 parent2 = selectionEnd.element.parent() || undefined;
87 var selectionFocus = cursor.getSelectionFocus();
89 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
93 var node1 = parent1.wlxmlNode,
94 node2 = parent2.wlxmlNode,
97 doc.createList({node1: node1, node2: node2});
98 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
101 commands.register('toggle-grid', function(canvas, params) {
102 canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
103 gridToggled = params.toggle;
106 commands.register('newNodeRequested', function(canvas, params, user) {
107 var cursor = canvas.getCursor(),
108 selectionStart = cursor.getSelectionStart(),
109 selectionEnd = cursor.getSelectionEnd(),
110 wlxmlNode, caretTo, wrapperCanvasElement;
112 var insertNode = function(insertion, callback) {
113 var doc = canvas.wlxmlDocument,
114 node, metadata, creator, dialog;
116 var execCallback = function(node) {
122 if(params.wlxmlTag === 'aside' && params.wlxmlClass === 'comment') {
123 doc.transaction(function() {
128 creator += ' (' + user.email + ')';
131 creator = 'anonymous';
134 metadata = node.getMetadata();
135 metadata.add({key: 'creator', value: creator});
136 metadata.add({key: 'date', value: datetime.currentStrfmt()});
139 } else if(params.wlxmlClass === 'link') {
140 dialog = Dialog.create({
141 title: gettext('Create link'),
142 executeButtonText: gettext('Apply'),
143 cancelButtonText: gettext('Cancel'),
145 {label: gettext('Link'), name: 'href', type: 'input'}
148 dialog.on('execute', function(event) {
149 doc.transaction(function() {
151 node.setAttr('href', event.formData.href);
158 doc.transaction(function() {
165 if(cursor.isSelecting()) {
166 if(cursor.isSelectingSiblings()) {
167 if(cursor.isSelectingWithinElement()) {
168 wlxmlNode = selectionStart.element.wlxmlNode;
169 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
173 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
176 wrapperCanvasElement = utils.findCanvasElement(wrapper);
177 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
182 wlxmlNode = selectionStart.element.wlxmlNode.parent();
183 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
187 return wlxmlNode.wrapText({
188 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
189 offsetStart: selectionStart.offset,
190 offsetEnd: selectionEnd.offset,
191 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.wlxmlNode), wlxmlNode.indexOf(selectionEnd.element.wlxmlNode)] //parent.childIndex(selectionEnd.element)]
195 wrapperCanvasElement = utils.findCanvasElement(wrapper);
196 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
201 var node1 = selectionStart.element.wlxmlNode,
202 node2 = selectionEnd.element.wlxmlNode,
203 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
208 return canvas.wlxmlDocument.wrapNodes({
209 node1: siblingParents.node1,
210 node2: siblingParents.node2,
211 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
217 } else if(canvas.getCurrentNodeElement()) {
218 wlxmlNode = canvas.getCurrentNodeElement().wlxmlNode;
220 var linkFound = [wlxmlNode].concat(wlxmlNode.parents()).some(function(node) {
221 if(node.getClass() === 'link') {
222 var dialog = Dialog.create({
223 title: gettext('Edit link'),
224 executeButtonText: gettext('Apply'),
225 cancelButtonText: gettext('Cancel'),
227 {label: gettext('Link'), name: 'href', type: 'input', initialValue: node.getAttr('href')},
230 dialog.on('execute', function(event) {
231 canvas.wlxmlDocument.transaction(function() {
232 node.setAttr('href', event.formData.href);
235 canvas.wlxmlDocument.endTransaction();
248 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
251 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
257 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
258 node.append({text:''});
260 }, function(wrapper) {
261 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
268 commands.register('footnote', function(canvas, params) {
270 var cursor = canvas.getCursor(),
271 position = cursor.getPosition(),
272 asideNode, asideElement, node;
275 if(cursor.isSelectingWithinElement()) {
276 asideNode = position.element.wlxmlNode.wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
278 node = position.element.wlxmlNode;
279 node.document.transaction(function() {
280 asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
281 asideNode.append({text: ''});
285 asideElement = utils.findCanvasElement(asideNode);
286 asideElement.toggle(true);
287 canvas.setCurrentElement(asideElement);
290 commands.register('take-away-node', function(canvas) {
291 var position = canvas.getCursor().getPosition(),
292 element = position.element,
293 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
295 if(!nodeElement || !(nodeElement.parent())) {
299 var range = nodeElement.wlxmlNode.unwrapContent();
302 var elementIsFirstChild = nodeElement.childIndex(element);
303 if(element.bound()) {
304 canvas.setCurrentElement(element, {caretTo: position.offset});
306 if(elementIsFirstChild) {
307 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
309 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
313 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
320 run: function(name, params, canvas, user) {
321 return commands.run(name, params, canvas, user);