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,
86 selectionFocus = cursor.getSelectionFocus(),
87 node1 = parent1.wlxmlNode,
88 node2 = parent2.wlxmlNode,
91 if(cursor.isSelecting()) {
92 doc.transaction(function() {
93 doc.createList({node1: node1, node2: node2, klass: params.meta === 'num' ? 'list.enum' : 'list'});
96 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
101 if(node1.isInside('list')) {
102 list = node1.getParent('list');
103 if((params.meta === 'num' && list.getClass() === 'list.enum') || params.meta !== 'num' && list.getClass() === 'list') {
104 list.object.extractAllItems();
106 list.setClass(params.meta === 'num' ? 'list.enum' : 'list');
112 commands.register('toggle-grid', function(canvas, params) {
113 canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
114 gridToggled = params.toggle;
117 commands.register('newNodeRequested', function(canvas, params, user) {
118 var cursor = canvas.getCursor(),
119 selectionStart = cursor.getSelectionStart(),
120 selectionEnd = cursor.getSelectionEnd(),
121 wlxmlNode, caretTo, wrapperCanvasElement;
123 var insertNode = function(insertion, callback) {
124 var doc = canvas.wlxmlDocument,
125 metadata, creator, dialog;
127 var execCallback = function(node) {
133 if(params.wlxmlTag === 'aside' && params.wlxmlClass === 'comment') {
134 doc.transaction(function() {
135 var node = insertion();
139 creator += ' (' + user.email + ')';
142 creator = 'anonymous';
145 metadata = node.getMetadata();
146 metadata.add({key: 'creator', value: creator});
147 metadata.add({key: 'date', value: datetime.currentStrfmt()});
150 success: execCallback
152 } else if(params.wlxmlClass === 'link') {
153 dialog = Dialog.create({
154 title: gettext('Create link'),
155 executeButtonText: gettext('Apply'),
156 cancelButtonText: gettext('Cancel'),
158 {label: gettext('Link'), name: 'href', type: 'input'}
161 dialog.on('execute', function(event) {
162 doc.transaction(function() {
163 var node = insertion();
164 node.setAttr('href', event.formData.href);
168 success: execCallback
173 doc.transaction(function() {
175 }, {success: execCallback});
179 if(cursor.isSelecting()) {
180 if(cursor.isSelectingSiblings()) {
181 if(cursor.isSelectingWithinElement()) {
182 wlxmlNode = selectionStart.element.wlxmlNode;
183 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
187 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
190 wrapperCanvasElement = utils.findCanvasElement(wrapper);
191 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
196 wlxmlNode = selectionStart.element.wlxmlNode.parent();
197 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
201 return wlxmlNode.wrapText({
202 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
203 offsetStart: selectionStart.offset,
204 offsetEnd: selectionEnd.offset,
205 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.wlxmlNode), wlxmlNode.indexOf(selectionEnd.element.wlxmlNode)] //parent.childIndex(selectionEnd.element)]
209 wrapperCanvasElement = utils.findCanvasElement(wrapper);
210 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
215 var node1 = selectionStart.element.wlxmlNode,
216 node2 = selectionEnd.element.wlxmlNode,
217 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
222 return canvas.wlxmlDocument.wrapNodes({
223 node1: siblingParents.node1,
224 node2: siblingParents.node2,
225 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
231 } else if(canvas.getCurrentNodeElement()) {
232 wlxmlNode = canvas.getCurrentNodeElement().wlxmlNode;
234 var linkFound = [wlxmlNode].concat(wlxmlNode.parents()).some(function(node) {
235 if(node.getClass() === 'link') {
236 var dialog = Dialog.create({
237 title: gettext('Edit link'),
238 executeButtonText: gettext('Apply'),
239 cancelButtonText: gettext('Cancel'),
241 {label: gettext('Link'), name: 'href', type: 'input', initialValue: node.getAttr('href')},
244 dialog.on('execute', function(event) {
245 canvas.wlxmlDocument.transaction(function() {
246 node.setAttr('href', event.formData.href);
261 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
264 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
270 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
271 node.append({text:''});
273 }, function(wrapper) {
274 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
281 commands.register('footnote', function(canvas, params) {
283 var cursor = canvas.getCursor(),
284 position = cursor.getPosition(),
285 asideNode, asideElement, node;
288 if(cursor.isSelectingWithinElement()) {
289 asideNode = position.element.wlxmlNode.wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
291 node = position.element.wlxmlNode;
292 node.document.transaction(function() {
293 asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
294 asideNode.append({text: ''});
298 asideElement = utils.findCanvasElement(asideNode);
299 asideElement.toggle(true);
300 canvas.setCurrentElement(asideElement);
303 commands.register('take-away-node', function(canvas) {
304 var position = canvas.getCursor().getPosition(),
305 element = position.element,
306 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
308 if(!nodeElement || !(nodeElement.parent())) {
312 var range = nodeElement.wlxmlNode.unwrapContent();
315 var elementIsFirstChild = nodeElement.childIndex(element);
316 if(element.bound()) {
317 canvas.setCurrentElement(element, {caretTo: position.offset});
319 if(elementIsFirstChild) {
320 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
322 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
326 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
333 run: function(name, params, canvas, user) {
334 return commands.run(name, params, canvas, user);