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.transaction(function() {
98 doc.createList({node1: node1, node2: node2, klass: params.meta === 'num' ? 'list.enum' : 'list'});
100 success: function() {
101 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
106 commands.register('toggle-grid', function(canvas, params) {
107 canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
108 gridToggled = params.toggle;
111 commands.register('newNodeRequested', function(canvas, params, user) {
112 var cursor = canvas.getCursor(),
113 selectionStart = cursor.getSelectionStart(),
114 selectionEnd = cursor.getSelectionEnd(),
115 wlxmlNode, caretTo, wrapperCanvasElement;
117 var insertNode = function(insertion, callback) {
118 var doc = canvas.wlxmlDocument,
119 metadata, creator, dialog;
121 var execCallback = function(node) {
127 if(params.wlxmlTag === 'aside' && params.wlxmlClass === 'comment') {
128 doc.transaction(function() {
129 var node = insertion();
133 creator += ' (' + user.email + ')';
136 creator = 'anonymous';
139 metadata = node.getMetadata();
140 metadata.add({key: 'creator', value: creator});
141 metadata.add({key: 'date', value: datetime.currentStrfmt()});
144 success: execCallback
146 } else if(params.wlxmlClass === 'link') {
147 dialog = Dialog.create({
148 title: gettext('Create link'),
149 executeButtonText: gettext('Apply'),
150 cancelButtonText: gettext('Cancel'),
152 {label: gettext('Link'), name: 'href', type: 'input'}
155 dialog.on('execute', function(event) {
156 doc.transaction(function() {
157 var node = insertion();
158 node.setAttr('href', event.formData.href);
162 success: execCallback
167 doc.transaction(function() {
169 }, {success: execCallback});
173 if(cursor.isSelecting()) {
174 if(cursor.isSelectingSiblings()) {
175 if(cursor.isSelectingWithinElement()) {
176 wlxmlNode = selectionStart.element.wlxmlNode;
177 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
181 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
184 wrapperCanvasElement = utils.findCanvasElement(wrapper);
185 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
190 wlxmlNode = selectionStart.element.wlxmlNode.parent();
191 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
195 return wlxmlNode.wrapText({
196 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
197 offsetStart: selectionStart.offset,
198 offsetEnd: selectionEnd.offset,
199 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.wlxmlNode), wlxmlNode.indexOf(selectionEnd.element.wlxmlNode)] //parent.childIndex(selectionEnd.element)]
203 wrapperCanvasElement = utils.findCanvasElement(wrapper);
204 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
209 var node1 = selectionStart.element.wlxmlNode,
210 node2 = selectionEnd.element.wlxmlNode,
211 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
216 return canvas.wlxmlDocument.wrapNodes({
217 node1: siblingParents.node1,
218 node2: siblingParents.node2,
219 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
225 } else if(canvas.getCurrentNodeElement()) {
226 wlxmlNode = canvas.getCurrentNodeElement().wlxmlNode;
228 var linkFound = [wlxmlNode].concat(wlxmlNode.parents()).some(function(node) {
229 if(node.getClass() === 'link') {
230 var dialog = Dialog.create({
231 title: gettext('Edit link'),
232 executeButtonText: gettext('Apply'),
233 cancelButtonText: gettext('Cancel'),
235 {label: gettext('Link'), name: 'href', type: 'input', initialValue: node.getAttr('href')},
238 dialog.on('execute', function(event) {
239 canvas.wlxmlDocument.transaction(function() {
240 node.setAttr('href', event.formData.href);
243 canvas.wlxmlDocument.endTransaction();
256 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
259 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
265 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
266 node.append({text:''});
268 }, function(wrapper) {
269 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
276 commands.register('footnote', function(canvas, params) {
278 var cursor = canvas.getCursor(),
279 position = cursor.getPosition(),
280 asideNode, asideElement, node;
283 if(cursor.isSelectingWithinElement()) {
284 asideNode = position.element.wlxmlNode.wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
286 node = position.element.wlxmlNode;
287 node.document.transaction(function() {
288 asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
289 asideNode.append({text: ''});
293 asideElement = utils.findCanvasElement(asideNode);
294 asideElement.toggle(true);
295 canvas.setCurrentElement(asideElement);
298 commands.register('take-away-node', function(canvas) {
299 var position = canvas.getCursor().getPosition(),
300 element = position.element,
301 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
303 if(!nodeElement || !(nodeElement.parent())) {
307 var range = nodeElement.wlxmlNode.unwrapContent();
310 var elementIsFirstChild = nodeElement.childIndex(element);
311 if(element.bound()) {
312 canvas.setCurrentElement(element, {caretTo: position.offset});
314 if(elementIsFirstChild) {
315 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
317 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
321 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
328 run: function(name, params, canvas, user) {
329 return commands.run(name, params, canvas, user);