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 if(cursor.isSelecting()) {
98 doc.transaction(function() {
99 doc.createList({node1: node1, node2: node2, klass: params.meta === 'num' ? 'list.enum' : 'list'});
101 success: function() {
102 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
107 if(node1.isInside('list')) {
108 list = node1.getParent('list');
109 if((params.meta === 'num' && list.getClass() === 'list.enum') || params.meta !== 'num' && list.getClass() === 'list') {
110 list.object.extractAllItems();
112 list.setClass(params.meta === 'num' ? 'list.enum' : 'list');
118 commands.register('toggle-grid', function(canvas, params) {
119 canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
120 gridToggled = params.toggle;
123 commands.register('newNodeRequested', function(canvas, params, user) {
124 var cursor = canvas.getCursor(),
125 selectionStart = cursor.getSelectionStart(),
126 selectionEnd = cursor.getSelectionEnd(),
127 wlxmlNode, caretTo, wrapperCanvasElement;
129 var insertNode = function(insertion, callback) {
130 var doc = canvas.wlxmlDocument,
131 metadata, creator, dialog;
133 var execCallback = function(node) {
139 if(params.wlxmlTag === 'aside' && params.wlxmlClass === 'comment') {
140 doc.transaction(function() {
141 var node = insertion();
145 creator += ' (' + user.email + ')';
148 creator = 'anonymous';
151 metadata = node.getMetadata();
152 metadata.add({key: 'creator', value: creator});
153 metadata.add({key: 'date', value: datetime.currentStrfmt()});
156 success: execCallback
158 } else if(params.wlxmlClass === 'link') {
159 dialog = Dialog.create({
160 title: gettext('Create link'),
161 executeButtonText: gettext('Apply'),
162 cancelButtonText: gettext('Cancel'),
164 {label: gettext('Link'), name: 'href', type: 'input'}
167 dialog.on('execute', function(event) {
168 doc.transaction(function() {
169 var node = insertion();
170 node.setAttr('href', event.formData.href);
174 success: execCallback
179 doc.transaction(function() {
181 }, {success: execCallback});
185 if(cursor.isSelecting()) {
186 if(cursor.isSelectingSiblings()) {
187 if(cursor.isSelectingWithinElement()) {
188 wlxmlNode = selectionStart.element.wlxmlNode;
189 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
193 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
196 wrapperCanvasElement = utils.findCanvasElement(wrapper);
197 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
202 wlxmlNode = selectionStart.element.wlxmlNode.parent();
203 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
207 return wlxmlNode.wrapText({
208 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
209 offsetStart: selectionStart.offset,
210 offsetEnd: selectionEnd.offset,
211 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.wlxmlNode), wlxmlNode.indexOf(selectionEnd.element.wlxmlNode)] //parent.childIndex(selectionEnd.element)]
215 wrapperCanvasElement = utils.findCanvasElement(wrapper);
216 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
221 var node1 = selectionStart.element.wlxmlNode,
222 node2 = selectionEnd.element.wlxmlNode,
223 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
228 return canvas.wlxmlDocument.wrapNodes({
229 node1: siblingParents.node1,
230 node2: siblingParents.node2,
231 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
237 } else if(canvas.getCurrentNodeElement()) {
238 wlxmlNode = canvas.getCurrentNodeElement().wlxmlNode;
240 var linkFound = [wlxmlNode].concat(wlxmlNode.parents()).some(function(node) {
241 if(node.getClass() === 'link') {
242 var dialog = Dialog.create({
243 title: gettext('Edit link'),
244 executeButtonText: gettext('Apply'),
245 cancelButtonText: gettext('Cancel'),
247 {label: gettext('Link'), name: 'href', type: 'input', initialValue: node.getAttr('href')},
250 dialog.on('execute', function(event) {
251 canvas.wlxmlDocument.transaction(function() {
252 node.setAttr('href', event.formData.href);
255 canvas.wlxmlDocument.endTransaction();
268 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
271 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
277 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
278 node.append({text:''});
280 }, function(wrapper) {
281 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
288 commands.register('footnote', function(canvas, params) {
290 var cursor = canvas.getCursor(),
291 position = cursor.getPosition(),
292 asideNode, asideElement, node;
295 if(cursor.isSelectingWithinElement()) {
296 asideNode = position.element.wlxmlNode.wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
298 node = position.element.wlxmlNode;
299 node.document.transaction(function() {
300 asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
301 asideNode.append({text: ''});
305 asideElement = utils.findCanvasElement(asideNode);
306 asideElement.toggle(true);
307 canvas.setCurrentElement(asideElement);
310 commands.register('take-away-node', function(canvas) {
311 var position = canvas.getCursor().getPosition(),
312 element = position.element,
313 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
315 if(!nodeElement || !(nodeElement.parent())) {
319 var range = nodeElement.wlxmlNode.unwrapContent();
322 var elementIsFirstChild = nodeElement.childIndex(element);
323 if(element.bound()) {
324 canvas.setCurrentElement(element, {caretTo: position.offset});
326 if(elementIsFirstChild) {
327 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
329 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
333 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
340 run: function(name, params, canvas, user) {
341 return commands.run(name, params, canvas, user);