2 'modules/documentCanvas/canvas/documentElement',
4 ], function(documentElement, utils) {
9 var gridToggled = false;
13 register: function(name, command) {
14 this._cmds[name] = command;
17 run: function(name, params, canvas) {
18 return this._cmds[name](canvas, params);
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 var cursor = canvas.getCursor(),
36 selectionStart = cursor.getSelectionStart(),
37 selectionEnd = cursor.getSelectionEnd(),
38 parent1 = selectionStart.element.parent() || undefined,
39 parent2 = selectionEnd.element.parent() || undefined;
41 // canvas.wlxmlDocument.transform('detach2', {node:canvas.getCurrentNodeElement().data('wlxmlNode')});
42 canvas.getCurrentNodeElement().data('wlxmlNode').transform('smartxml.detach');
46 commands.register('unwrap-node', function(canvas) {
47 var cursor = canvas.getCursor(),
48 selectionStart = cursor.getSelectionStart(),
49 selectionEnd = cursor.getSelectionEnd(),
50 parent1 = selectionStart.element.parent() || undefined,
51 parent2 = selectionEnd.element.parent() || undefined;
53 var selectionAnchor = cursor.getSelectionAnchor(),
54 node1 = parent1.data('wlxmlNode'),
55 node2 = parent2.data('wlxmlNode'),
57 if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
60 doc.transform('extractItems', {item1: node1, item2: node2});
62 //canvas.list.extractItems({element1: parent1, element2: parent2});
63 canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
64 } else if(!cursor.isSelecting()) {
65 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
66 parentNode = nodeToUnwrap.unwrap();
68 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
73 commands.register('wrap-node', function(canvas) {
74 var cursor = canvas.getCursor(),
75 selectionStart = cursor.getSelectionStart(),
76 selectionEnd = cursor.getSelectionEnd(),
77 parent1 = selectionStart.element.parent() || undefined,
78 parent2 = selectionEnd.element.parent() || undefined;
80 var node1 = parent1.data('wlxmlNode'),
81 node2 = parent2.data('wlxmlNode'),
84 if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
85 //canvas.list.create({element1: parent1, element2: parent2});
86 //doc.transform('createList', {node1: node1, node2: node2});
87 doc.createList({node1: node1, node2: node2});
91 commands.register('list', function(canvas, params) {
92 var cursor = canvas.getCursor(),
93 selectionStart = cursor.getSelectionStart(),
94 selectionEnd = cursor.getSelectionEnd(),
95 parent1 = selectionStart.element.parent() || undefined,
96 parent2 = selectionEnd.element.parent() || undefined;
98 var selectionFocus = cursor.getSelectionFocus();
100 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
104 var node1 = parent1.data('wlxmlNode'),
105 node2 = parent2.data('wlxmlNode'),
106 doc = node1.document;
108 //doc.transform('createList', {node1: node1, node2: node2});
109 doc.createList({node1: node1, node2: node2});
112 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
115 commands.register('toggle-grid', function(canvas, params) {
116 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
117 gridToggled = params.toggle;
120 commands.register('newNodeRequested', function(canvas, params) {
121 var cursor = canvas.getCursor(),
122 selectionStart = cursor.getSelectionStart(),
123 selectionEnd = cursor.getSelectionEnd();
125 if(cursor.isSelecting()) {
126 if(cursor.isSelectingSiblings()) {
127 if(cursor.isSelectingWithinElement()) {
128 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
129 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
130 //wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
131 //wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
132 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
134 var wrapperCanvasElement = utils.findCanvasElement(wrapper);
135 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
138 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
139 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
141 // var wrapper = wlxmlNode.wrapText({
142 // _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
143 // offsetStart: selectionStart.offset,
144 // offsetEnd: selectionEnd.offset,
145 // textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
147 var wrapper = wlxmlNode.transform('smartxml.wrapText', {
148 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
149 offsetStart: selectionStart.offset,
150 offsetEnd: selectionEnd.offset,
151 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
153 wrapperCanvasElement = utils.findCanvasElement(wrapper);
154 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
157 var node1 = selectionStart.element.data('wlxmlNode'),
158 node2 = selectionEnd.element.data('wlxmlNode'),
159 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
162 // canvas.wlxmlDocument.wrapNodes({
163 // element1: siblingParents.node1,
164 // element2: siblingParents.node2,
165 // _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
167 canvas.wlxmlDocument.transform('smartxml.wrapNodes', {
168 node1: siblingParents.node1,
169 node2: siblingParents.node2,
170 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
174 } else if(canvas.getCurrentNodeElement()) {
175 var node = canvas.getCurrentNodeElement().data('wlxmlNode'),
176 // wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
177 wrapper = node.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
178 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
184 commands.register('footnote', function(canvas, params) {
185 var cursor = canvas.getCursor(),
186 position = cursor.getPosition(),
190 if(cursor.isSelectingWithinElement()) {
191 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
193 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
194 asideElement.append({text: ''});
197 asideElement.toggle(true);
198 canvas.setCurrentElement(asideElement);
201 commands.register('take-away-node', function(canvas) {
202 var position = canvas.getCursor().getPosition(),
203 element = position.element,
204 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
206 if(!nodeElement || !(nodeElement.parent()))
210 var range = nodeElement.data('wlxmlNode').unwrapContent();
213 var elementIsFirstChild = nodeElement.childIndex(element);
214 if(element.bound()) {
215 canvas.setCurrentElement(element, {caretTo: position.offset});
217 if(elementIsFirstChild) {
218 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'end'});
220 canvas.setCurrentElement(findCanvasElement(range.element2), {caretTo: 'end'});
224 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'start'});
231 run: function(name, params, canvas) {
232 return commands.run(name, params, canvas);