8 var gridToggled = false;
12 register: function(name, command) {
13 this._cmds[name] = command;
16 run: function(name, params, canvas, user) {
17 return this._cmds[name](canvas, params, user);
21 commands.register('undo', function(canvas) {
22 var doc = canvas.wlxmlDocument;
27 commands.register('redo', function(canvas) {
28 var doc = canvas.wlxmlDocument;
33 commands.register('remove-node', function(canvas) {
34 canvas.getCurrentNodeElement().data('wlxmlNode').detach();
37 commands.register('unwrap-node', function(canvas) {
38 var cursor = canvas.getCursor(),
39 selectionStart = cursor.getSelectionStart(),
40 selectionEnd = cursor.getSelectionEnd(),
41 parent1 = selectionStart.element.parent() || undefined,
42 parent2 = selectionEnd.element.parent() || undefined;
44 var selectionAnchor = cursor.getSelectionAnchor(),
45 node1 = parent1.data('wlxmlNode'),
46 node2 = parent2.data('wlxmlNode'),
48 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
49 doc.extractItems({item1: node1, item2: node2});
50 canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
51 } else if(!cursor.isSelecting()) {
52 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
53 parentNode = nodeToUnwrap.unwrap();
55 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
60 commands.register('wrap-node', function(canvas) {
61 var cursor = canvas.getCursor(),
62 selectionStart = cursor.getSelectionStart(),
63 selectionEnd = cursor.getSelectionEnd(),
64 parent1 = selectionStart.element.parent() || undefined,
65 parent2 = selectionEnd.element.parent() || undefined;
67 var node1 = parent1.data('wlxmlNode'),
68 node2 = parent2.data('wlxmlNode'),
71 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
72 doc.createList({node1: node1, node2: node2});
76 commands.register('list', function(canvas, params) {
78 var cursor = canvas.getCursor(),
79 selectionStart = cursor.getSelectionStart(),
80 selectionEnd = cursor.getSelectionEnd(),
81 parent1 = selectionStart.element.parent() || undefined,
82 parent2 = selectionEnd.element.parent() || undefined;
84 var selectionFocus = cursor.getSelectionFocus();
86 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
90 var node1 = parent1.data('wlxmlNode'),
91 node2 = parent2.data('wlxmlNode'),
94 doc.createList({node1: node1, node2: node2});
95 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
98 commands.register('toggle-grid', function(canvas, params) {
99 canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
100 gridToggled = params.toggle;
103 commands.register('newNodeRequested', function(canvas, params, user) {
104 var cursor = canvas.getCursor(),
105 selectionStart = cursor.getSelectionStart(),
106 selectionEnd = cursor.getSelectionEnd(),
107 wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
109 var insertNode = function(insertion) {
110 var doc = canvas.wlxmlDocument,
111 node, metadata, creator, currentDate, dt;
113 var pad = function(number) {
115 number = '0' + number;
120 doc.startTransaction();
122 if(node.getTagName() === 'aside' && node.getClass() === 'comment') {
126 creator += ' (' + user.email + ')';
129 creator = 'anonymous';
132 currentDate = new Date();
133 dt = pad(currentDate.getDate()) + '-' +
134 pad((currentDate.getMonth() + 1)) + '-' +
135 pad(currentDate.getFullYear()) + ' ' +
136 pad(currentDate.getHours()) + ':' +
137 pad(currentDate.getMinutes()) + ':' +
138 pad(currentDate.getSeconds());
140 metadata = node.getMetadata();
141 metadata.add({key: 'creator', value: creator});
142 metadata.add({key: 'date', value: dt});
144 doc.endTransaction();
149 if(cursor.isSelecting()) {
150 if(cursor.isSelectingSiblings()) {
151 if(cursor.isSelectingWithinElement()) {
152 wlxmlNode = selectionStart.element.data('wlxmlNode');
153 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
155 wrapper = insertNode(function() {
156 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
158 wrapperCanvasElement = utils.findCanvasElement(wrapper);
159 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
162 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
163 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
165 wrapper = insertNode(function() {
166 return wlxmlNode.wrapText({
167 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
168 offsetStart: selectionStart.offset,
169 offsetEnd: selectionEnd.offset,
170 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
173 wrapperCanvasElement = utils.findCanvasElement(wrapper);
174 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
177 var node1 = selectionStart.element.data('wlxmlNode'),
178 node2 = selectionEnd.element.data('wlxmlNode'),
179 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
182 insertNode(function() {
183 return canvas.wlxmlDocument.wrapNodes({
184 node1: siblingParents.node1,
185 node2: siblingParents.node2,
186 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
191 } else if(canvas.getCurrentNodeElement()) {
192 wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
194 wrapper = insertNode(function() {
195 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
198 wrapper = insertNode(function() {
199 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
200 node.append({text:''});
204 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
210 commands.register('footnote', function(canvas, params) {
212 var cursor = canvas.getCursor(),
213 position = cursor.getPosition(),
214 asideNode, asideElement, node;
217 if(cursor.isSelectingWithinElement()) {
218 asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
220 node = position.element.data('wlxmlNode');
221 node.document.transaction(function() {
222 asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
223 asideNode.append({text: ''});
227 asideElement = utils.findCanvasElement(asideNode);
228 asideElement.toggle(true);
229 canvas.setCurrentElement(asideElement);
232 commands.register('take-away-node', function(canvas) {
233 var position = canvas.getCursor().getPosition(),
234 element = position.element,
235 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
237 if(!nodeElement || !(nodeElement.parent())) {
241 var range = nodeElement.data('wlxmlNode').unwrapContent();
244 var elementIsFirstChild = nodeElement.childIndex(element);
245 if(element.bound()) {
246 canvas.setCurrentElement(element, {caretTo: position.offset});
248 if(elementIsFirstChild) {
249 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
251 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
255 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
262 run: function(name, params, canvas, user) {
263 return commands.run(name, params, canvas, user);