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')});
44 commands.register('unwrap-node', function(canvas) {
45 var cursor = canvas.getCursor(),
46 selectionStart = cursor.getSelectionStart(),
47 selectionEnd = cursor.getSelectionEnd(),
48 parent1 = selectionStart.element.parent() || undefined,
49 parent2 = selectionEnd.element.parent() || undefined;
51 if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
54 // var selectionAnchor = cursor.getSelectionAnchor();
55 // canvas.list.extractItems({element1: parent1, element2: parent2});
56 // canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
57 } else if(!cursor.isSelecting()) {
58 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
59 parentNode = nodeToUnwrap.unwrap();
61 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
66 // commands.register('wrap-node', function(canvas) {
67 // var cursor = canvas.getCursor(),
68 // selectionStart = cursor.getSelectionStart(),
69 // selectionEnd = cursor.getSelectionEnd(),
70 // parent1 = selectionStart.element.parent() || undefined,
71 // parent2 = selectionEnd.element.parent() || undefined;
73 // if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
74 // canvas.list.create({element1: parent1, element2: parent2});
78 commands.register('list', function(canvas, params) {
79 var cursor = canvas.getCursor(),
80 selectionStart = cursor.getSelectionStart(),
81 selectionEnd = cursor.getSelectionEnd(),
82 parent1 = selectionStart.element.parent() || undefined,
83 parent2 = selectionEnd.element.parent() || undefined;
85 var selectionFocus = cursor.getSelectionFocus();
87 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
91 canvas.list.create({element1: parent1, element2: parent2});
93 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
96 commands.register('toggle-grid', function(canvas, params) {
97 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
98 gridToggled = params.toggle;
101 commands.register('newNodeRequested', function(canvas, params) {
102 var cursor = canvas.getCursor(),
103 selectionStart = cursor.getSelectionStart(),
104 selectionEnd = cursor.getSelectionEnd();
106 if(cursor.isSelecting()) {
107 if(cursor.isSelectingSiblings()) {
108 if(cursor.isSelectingWithinElement()) {
109 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
110 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
111 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
112 wrapperCanvasElement = utils.findCanvasElement(wrapper);
113 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
116 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
117 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
119 var wrapper = wlxmlNode.wrapText({
120 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
121 offsetStart: selectionStart.offset,
122 offsetEnd: selectionEnd.offset,
123 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
125 wrapperCanvasElement = utils.findCanvasElement(wrapper);
126 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
129 var node1 = selectionStart.element.data('wlxmlNode'),
130 node2 = selectionEnd.element.data('wlxmlNode'),
131 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
134 canvas.wlxmlDocument.wrapNodes({
135 element1: siblingParents.node1,
136 element2: siblingParents.node2,
137 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
141 } else if(canvas.getCurrentNodeElement()) {
142 var node = findCanvasElement(canvas.getCurrentNodeElement),
143 wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
144 canvas.setCurrentElement(findCanvasElement(wrapper));
150 commands.register('footnote', function(canvas, params) {
151 var cursor = canvas.getCursor(),
152 position = cursor.getPosition(),
156 if(cursor.isSelectingWithinElement()) {
157 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
159 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
160 asideElement.append({text: ''});
163 asideElement.toggle(true);
164 canvas.setCurrentElement(asideElement);
167 commands.register('take-away-node', function(canvas) {
168 var position = canvas.getCursor().getPosition(),
169 element = position.element,
170 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
172 if(!nodeElement || !(nodeElement.parent()))
176 var range = nodeElement.data('wlxmlNode').unwrapContent();
179 var elementIsFirstChild = nodeElement.childIndex(element);
180 if(element.bound()) {
181 canvas.setCurrentElement(element, {caretTo: position.offset});
183 if(elementIsFirstChild) {
184 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'end'});
186 canvas.setCurrentElement(findCanvasElement(range.element2), {caretTo: 'end'});
190 canvas.setCurrentElement(findCanvasElement(range.element1), {caretTo: 'start'});
197 run: function(name, params, canvas) {
198 return commands.run(name, params, canvas);