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 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})) {
52 doc.extractItems({item1: node1, item2: node2});
54 //canvas.list.extractItems({element1: parent1, element2: parent2});
55 canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
56 } else if(!cursor.isSelecting()) {
57 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
58 parentNode = nodeToUnwrap.unwrap();
60 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
65 commands.register('wrap-node', function(canvas) {
66 var cursor = canvas.getCursor(),
67 selectionStart = cursor.getSelectionStart(),
68 selectionEnd = cursor.getSelectionEnd(),
69 parent1 = selectionStart.element.parent() || undefined,
70 parent2 = selectionEnd.element.parent() || undefined;
72 var node1 = parent1.data('wlxmlNode'),
73 node2 = parent2.data('wlxmlNode'),
76 if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
77 //canvas.list.create({element1: parent1, element2: parent2});
78 //doc.transform('createList', {node1: node1, node2: node2});
79 doc.createList({node1: node1, node2: node2});
83 commands.register('list', function(canvas, params) {
85 var cursor = canvas.getCursor(),
86 selectionStart = cursor.getSelectionStart(),
87 selectionEnd = cursor.getSelectionEnd(),
88 parent1 = selectionStart.element.parent() || undefined,
89 parent2 = selectionEnd.element.parent() || undefined;
91 var selectionFocus = cursor.getSelectionFocus();
93 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
97 var node1 = parent1.data('wlxmlNode'),
98 node2 = parent2.data('wlxmlNode'),
101 //doc.transform('createList', {node1: node1, node2: node2});
102 doc.createList({node1: node1, node2: node2});
105 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
108 commands.register('toggle-grid', function(canvas, params) {
109 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
110 gridToggled = params.toggle;
113 commands.register('newNodeRequested', function(canvas, params) {
114 var cursor = canvas.getCursor(),
115 selectionStart = cursor.getSelectionStart(),
116 selectionEnd = cursor.getSelectionEnd(),
117 wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
119 if(cursor.isSelecting()) {
120 if(cursor.isSelectingSiblings()) {
121 if(cursor.isSelectingWithinElement()) {
122 wlxmlNode = selectionStart.element.data('wlxmlNode');
123 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
124 //wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
125 //wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
126 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
127 wrapperCanvasElement = utils.findCanvasElement(wrapper);
128 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
131 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
132 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
134 // var wrapper = wlxmlNode.wrapText({
135 // _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
136 // offsetStart: selectionStart.offset,
137 // offsetEnd: selectionEnd.offset,
138 // textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
140 wrapper = wlxmlNode.wrapText({
141 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
142 offsetStart: selectionStart.offset,
143 offsetEnd: selectionEnd.offset,
144 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
146 wrapperCanvasElement = utils.findCanvasElement(wrapper);
147 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
150 var node1 = selectionStart.element.data('wlxmlNode'),
151 node2 = selectionEnd.element.data('wlxmlNode'),
152 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
155 // canvas.wlxmlDocument.wrapNodes({
156 // element1: siblingParents.node1,
157 // element2: siblingParents.node2,
158 // _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
160 canvas.wlxmlDocument.wrapNodes({
161 node1: siblingParents.node1,
162 node2: siblingParents.node2,
163 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
167 } else if(canvas.getCurrentNodeElement()) {
168 wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
169 // wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
170 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
171 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
177 commands.register('footnote', function(canvas, params) {
179 var cursor = canvas.getCursor(),
180 position = cursor.getPosition(),
181 asideNode, asideElement;
184 if(cursor.isSelectingWithinElement()) {
185 asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
187 asideNode = position.element.data('wlxmlNode').divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
188 asideNode.append({text: ''});
191 asideElement = utils.findCanvasElement(asideNode);
192 asideElement.toggle(true);
193 canvas.setCurrentElement(asideElement);
196 commands.register('take-away-node', function(canvas) {
197 var position = canvas.getCursor().getPosition(),
198 element = position.element,
199 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
201 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(utils.findCanvasElement(range.element1), {caretTo: 'end'});
216 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
220 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
227 run: function(name, params, canvas) {
228 return commands.run(name, params, canvas);