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 if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
56 // var selectionAnchor = cursor.getSelectionAnchor();
57 // canvas.list.extractItems({element1: parent1, element2: parent2});
58 // canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
59 } else if(!cursor.isSelecting()) {
60 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
61 parentNode = nodeToUnwrap.unwrap();
63 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
68 // commands.register('wrap-node', function(canvas) {
69 // var cursor = canvas.getCursor(),
70 // selectionStart = cursor.getSelectionStart(),
71 // selectionEnd = cursor.getSelectionEnd(),
72 // parent1 = selectionStart.element.parent() || undefined,
73 // parent2 = selectionEnd.element.parent() || undefined;
75 // if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
76 // canvas.list.create({element1: parent1, element2: parent2});
80 commands.register('list', function(canvas, params) {
81 var cursor = canvas.getCursor(),
82 selectionStart = cursor.getSelectionStart(),
83 selectionEnd = cursor.getSelectionEnd(),
84 parent1 = selectionStart.element.parent() || undefined,
85 parent2 = selectionEnd.element.parent() || undefined;
87 var selectionFocus = cursor.getSelectionFocus();
89 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
93 canvas.list.create({element1: parent1, element2: parent2});
95 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
98 commands.register('toggle-grid', function(canvas, params) {
99 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
100 gridToggled = params.toggle;
103 commands.register('newNodeRequested', function(canvas, params) {
104 var cursor = canvas.getCursor(),
105 selectionStart = cursor.getSelectionStart(),
106 selectionEnd = cursor.getSelectionEnd();
108 if(cursor.isSelecting()) {
109 if(cursor.isSelectingSiblings()) {
110 if(cursor.isSelectingWithinElement()) {
111 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
112 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
113 //wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
114 wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
116 var wrapperCanvasElement = utils.findCanvasElement(wrapper);
117 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
120 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
121 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
123 // var wrapper = wlxmlNode.wrapText({
124 // _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
125 // offsetStart: selectionStart.offset,
126 // offsetEnd: selectionEnd.offset,
127 // textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
129 var wrapper = wlxmlNode.transform('smartxml.wrapText', {
130 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
131 offsetStart: selectionStart.offset,
132 offsetEnd: selectionEnd.offset,
133 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
135 wrapperCanvasElement = utils.findCanvasElement(wrapper);
136 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
139 var node1 = selectionStart.element.data('wlxmlNode'),
140 node2 = selectionEnd.element.data('wlxmlNode'),
141 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
144 // canvas.wlxmlDocument.wrapNodes({
145 // element1: siblingParents.node1,
146 // element2: siblingParents.node2,
147 // _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
149 canvas.wlxmlDocument.transform('smartxml.wrapNodes', {
150 node1: siblingParents.node1,
151 node2: siblingParents.node2,
152 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
156 } else if(canvas.getCurrentNodeElement()) {
157 var node = canvas.getCurrentNodeElement().data('wlxmlNode'),
158 // wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
159 wrapper = node.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
160 canvas.setCurrentElement(utils.findCanvasElement(wrapper));
166 commands.register('footnote', function(canvas, params) {
167 var cursor = canvas.getCursor(),
168 position = cursor.getPosition(),
172 if(cursor.isSelectingWithinElement()) {
173 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
175 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
176 asideElement.append({text: ''});
179 asideElement.toggle(true);
180 canvas.setCurrentElement(asideElement);
183 commands.register('take-away-node', function(canvas) {
184 var position = canvas.getCursor().getPosition(),
185 element = position.element,
186 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
188 if(!nodeElement || !(nodeElement.parent()))
192 var range = nodeElement.data('wlxmlNode').unwrapContent();
195 var elementIsFirstChild = nodeElement.childIndex(element);
196 if(element.bound()) {
197 canvas.setCurrentElement(element, {caretTo: position.offset});
199 if(elementIsFirstChild) {
200 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'end'});
202 canvas.setCurrentElement(findCanvasElement(range.element2), {caretTo: 'end'});
206 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'start'});
213 run: function(name, params, canvas) {
214 return commands.run(name, params, canvas);