4 ], function(utils, datetime) {
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().parent().toggleClass('grid-on', 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;
114 doc.startTransaction();
116 if(node.getTagName() === 'aside' && node.getClass() === 'comment') {
120 creator += ' (' + user.email + ')';
123 creator = 'anonymous';
126 metadata = node.getMetadata();
127 metadata.add({key: 'creator', value: creator});
128 metadata.add({key: 'date', value: datetime.currentStrfmt()});
130 doc.endTransaction();
135 if(cursor.isSelecting()) {
136 if(cursor.isSelectingSiblings()) {
137 if(cursor.isSelectingWithinElement()) {
138 wlxmlNode = selectionStart.element.data('wlxmlNode');
139 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
141 wrapper = insertNode(function() {
142 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
144 wrapperCanvasElement = utils.findCanvasElement(wrapper);
145 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
148 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
149 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
151 wrapper = insertNode(function() {
152 return wlxmlNode.wrapText({
153 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
154 offsetStart: selectionStart.offset,
155 offsetEnd: selectionEnd.offset,
156 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
159 wrapperCanvasElement = utils.findCanvasElement(wrapper);
160 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
163 var node1 = selectionStart.element.data('wlxmlNode'),
164 node2 = selectionEnd.element.data('wlxmlNode'),
165 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
168 insertNode(function() {
169 return canvas.wlxmlDocument.wrapNodes({
170 node1: siblingParents.node1,
171 node2: siblingParents.node2,
172 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
177 } else if(canvas.getCurrentNodeElement()) {
178 wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
180 wrapper = insertNode(function() {
181 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
184 wrapper = insertNode(function() {
185 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
186 node.append({text:''});
190 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
196 commands.register('footnote', function(canvas, params) {
198 var cursor = canvas.getCursor(),
199 position = cursor.getPosition(),
200 asideNode, asideElement, node;
203 if(cursor.isSelectingWithinElement()) {
204 asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
206 node = position.element.data('wlxmlNode');
207 node.document.transaction(function() {
208 asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
209 asideNode.append({text: ''});
213 asideElement = utils.findCanvasElement(asideNode);
214 asideElement.toggle(true);
215 canvas.setCurrentElement(asideElement);
218 commands.register('take-away-node', function(canvas) {
219 var position = canvas.getCursor().getPosition(),
220 element = position.element,
221 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
223 if(!nodeElement || !(nodeElement.parent())) {
227 var range = nodeElement.data('wlxmlNode').unwrapContent();
230 var elementIsFirstChild = nodeElement.childIndex(element);
231 if(element.bound()) {
232 canvas.setCurrentElement(element, {caretTo: position.offset});
234 if(elementIsFirstChild) {
235 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
237 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
241 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
248 run: function(name, params, canvas, user) {
249 return commands.run(name, params, canvas, user);