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('unwrap-node', function(canvas) {
23 var cursor = canvas.getCursor(),
24 selectionStart = cursor.getSelectionStart(),
25 selectionEnd = cursor.getSelectionEnd(),
26 parent1 = selectionStart.element.parent() || undefined,
27 parent2 = selectionEnd.element.parent() || undefined;
29 if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
32 // var selectionAnchor = cursor.getSelectionAnchor();
33 // canvas.list.extractItems({element1: parent1, element2: parent2});
34 // canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
35 } else if(!cursor.isSelecting()) {
36 var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
37 parentNode = nodeToUnwrap.unwrap();
39 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
44 // commands.register('wrap-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})) {
52 // canvas.list.create({element1: parent1, element2: parent2});
56 commands.register('list', function(canvas, params) {
57 var cursor = canvas.getCursor(),
58 selectionStart = cursor.getSelectionStart(),
59 selectionEnd = cursor.getSelectionEnd(),
60 parent1 = selectionStart.element.parent() || undefined,
61 parent2 = selectionEnd.element.parent() || undefined;
63 var selectionFocus = cursor.getSelectionFocus();
65 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
69 canvas.list.create({element1: parent1, element2: parent2});
71 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
74 commands.register('toggle-grid', function(canvas, params) {
75 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
76 gridToggled = params.toggle;
79 commands.register('newNodeRequested', function(canvas, params) {
80 var cursor = canvas.getCursor(),
81 selectionStart = cursor.getSelectionStart(),
82 selectionEnd = cursor.getSelectionEnd();
84 if(cursor.isSelecting()) {
85 if(cursor.isSelectingSiblings()) {
86 if(cursor.isSelectingWithinElement()) {
87 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
88 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
89 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
90 wrapperCanvasElement = utils.findCanvasElement(wrapper);
91 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
94 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
95 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
97 var wrapper = wlxmlNode.wrapText({
98 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
99 offsetStart: selectionStart.offset,
100 offsetEnd: selectionEnd.offset,
101 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
103 wrapperCanvasElement = utils.findCanvasElement(wrapper);
104 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
107 var node1 = selectionStart.element.data('wlxmlNode'),
108 node2 = selectionEnd.element.data('wlxmlNode'),
109 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
112 canvas.wlxmlDocument.wrapNodes({
113 element1: siblingParents.node1,
114 element2: siblingParents.node2,
115 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
119 } else if(canvas.getCurrentNodeElement()) {
120 var node = findCanvasElement(canvas.getCurrentNodeElement),
121 wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
122 canvas.setCurrentElement(findCanvasElement(wrapper));
128 commands.register('footnote', function(canvas, params) {
129 var cursor = canvas.getCursor(),
130 position = cursor.getPosition(),
134 if(cursor.isSelectingWithinElement()) {
135 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
137 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
138 asideElement.append({text: ''});
141 asideElement.toggle(true);
142 canvas.setCurrentElement(asideElement);
145 commands.register('take-away-node', function(canvas) {
146 var position = canvas.getCursor().getPosition(),
147 element = position.element,
148 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
150 if(!nodeElement || !(nodeElement.parent()))
154 var range = nodeElement.unwrapContents();
157 var elementIsFirstChild = nodeElement.childIndex(element);
158 if(element.bound()) {
159 canvas.setCurrentElement(element, {caretTo: position.offset});
161 if(elementIsFirstChild) {
162 canvas.setCurrentElement(range.element1, {caretTo: 'end'});
164 canvas.setCurrentElement(range.element2, {caretTo: 'end'});
168 canvas.setCurrentElement(range.element1, {caretTo: 'start'});
175 run: function(name, params, canvas) {
176 return commands.run(name, params, canvas);