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});
90 commands.register('list', function(canvas, params) {
91 var cursor = canvas.getCursor(),
92 selectionStart = cursor.getSelectionStart(),
93 selectionEnd = cursor.getSelectionEnd(),
94 parent1 = selectionStart.element.parent() || undefined,
95 parent2 = selectionEnd.element.parent() || undefined;
97 var selectionFocus = cursor.getSelectionFocus();
99 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
103 var node1 = parent1.data('wlxmlNode'),
104 node2 = parent2.data('wlxmlNode'),
105 doc = node1.document;
107 doc.transform('createList', {node1: node1, node2: node2});
109 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
112 commands.register('toggle-grid', function(canvas, params) {
113 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
114 gridToggled = params.toggle;
117 commands.register('newNodeRequested', function(canvas, params) {
118 var cursor = canvas.getCursor(),
119 selectionStart = cursor.getSelectionStart(),
120 selectionEnd = cursor.getSelectionEnd();
122 if(cursor.isSelecting()) {
123 if(cursor.isSelectingSiblings()) {
124 if(cursor.isSelectingWithinElement()) {
125 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
126 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
127 //wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
128 wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
130 var wrapperCanvasElement = utils.findCanvasElement(wrapper);
131 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
134 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
135 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
137 // var wrapper = wlxmlNode.wrapText({
138 // _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
139 // offsetStart: selectionStart.offset,
140 // offsetEnd: selectionEnd.offset,
141 // textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
143 var wrapper = wlxmlNode.transform('smartxml.wrapText', {
144 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
145 offsetStart: selectionStart.offset,
146 offsetEnd: selectionEnd.offset,
147 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
149 wrapperCanvasElement = utils.findCanvasElement(wrapper);
150 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
153 var node1 = selectionStart.element.data('wlxmlNode'),
154 node2 = selectionEnd.element.data('wlxmlNode'),
155 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
158 // canvas.wlxmlDocument.wrapNodes({
159 // element1: siblingParents.node1,
160 // element2: siblingParents.node2,
161 // _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
163 canvas.wlxmlDocument.transform('smartxml.wrapNodes', {
164 node1: siblingParents.node1,
165 node2: siblingParents.node2,
166 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
170 } else if(canvas.getCurrentNodeElement()) {
171 var node = canvas.getCurrentNodeElement().data('wlxmlNode'),
172 // wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
173 wrapper = node.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
174 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
180 commands.register('footnote', function(canvas, params) {
181 var cursor = canvas.getCursor(),
182 position = cursor.getPosition(),
186 if(cursor.isSelectingWithinElement()) {
187 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
189 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
190 asideElement.append({text: ''});
193 asideElement.toggle(true);
194 canvas.setCurrentElement(asideElement);
197 commands.register('take-away-node', function(canvas) {
198 var position = canvas.getCursor().getPosition(),
199 element = position.element,
200 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
202 if(!nodeElement || !(nodeElement.parent()))
206 var range = nodeElement.data('wlxmlNode').unwrapContent();
209 var elementIsFirstChild = nodeElement.childIndex(element);
210 if(element.bound()) {
211 canvas.setCurrentElement(element, {caretTo: position.offset});
213 if(elementIsFirstChild) {
214 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'end'});
216 canvas.setCurrentElement(findCanvasElement(range.element2), {caretTo: 'end'});
220 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'start'});
227 run: function(name, params, canvas) {
228 return commands.run(name, params, canvas);