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();
38 canvas.setCurrentElement(utils.findCanvasElement(parentNode));
42 // commands.register('wrap-node', function(canvas) {
43 // var cursor = canvas.getCursor(),
44 // selectionStart = cursor.getSelectionStart(),
45 // selectionEnd = cursor.getSelectionEnd(),
46 // parent1 = selectionStart.element.parent() || undefined,
47 // parent2 = selectionEnd.element.parent() || undefined;
49 // if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
50 // canvas.list.create({element1: parent1, element2: parent2});
54 commands.register('list', function(canvas, params) {
55 var cursor = canvas.getCursor(),
56 selectionStart = cursor.getSelectionStart(),
57 selectionEnd = cursor.getSelectionEnd(),
58 parent1 = selectionStart.element.parent() || undefined,
59 parent2 = selectionEnd.element.parent() || undefined;
61 var selectionFocus = cursor.getSelectionFocus();
63 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
67 canvas.list.create({element1: parent1, element2: parent2});
69 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
72 commands.register('toggle-grid', function(canvas, params) {
73 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
74 gridToggled = params.toggle;
77 commands.register('newNodeRequested', function(canvas, params) {
78 var cursor = canvas.getCursor(),
79 selectionStart = cursor.getSelectionStart(),
80 selectionEnd = cursor.getSelectionEnd();
82 if(cursor.isSelecting()) {
83 if(cursor.isSelectingSiblings()) {
84 if(cursor.isSelectingWithinElement()) {
85 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
86 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
87 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
88 wrapperCanvasElement = utils.findCanvasElement(wrapper);
89 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
92 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
93 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
95 var wrapper = wlxmlNode.wrapText({
96 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
97 offsetStart: selectionStart.offset,
98 offsetEnd: selectionEnd.offset,
99 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
101 wrapperCanvasElement = utils.findCanvasElement(wrapper);
102 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
105 var node1 = selectionStart.element.data('wlxmlNode'),
106 node2 = selectionEnd.element.data('wlxmlNode'),
107 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
110 canvas.wlxmlDocument.wrapNodes({
111 element1: siblingParents.node1,
112 element2: siblingParents.node2,
113 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
117 } else if(canvas.getCurrentNodeElement()) {
118 var node = findCanvasElement(canvas.getCurrentNodeElement),
119 wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
120 canvas.setCurrentElement(findCanvasElement(wrapper));
126 commands.register('footnote', function(canvas, params) {
127 var cursor = canvas.getCursor(),
128 position = cursor.getPosition(),
132 if(cursor.isSelectingWithinElement()) {
133 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
135 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
136 asideElement.append({text: ''});
139 asideElement.toggle(true);
140 canvas.setCurrentElement(asideElement);
143 commands.register('take-away-node', function(canvas) {
144 var position = canvas.getCursor().getPosition(),
145 element = position.element,
146 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
148 if(!nodeElement || !(nodeElement.parent()))
152 var range = nodeElement.unwrapContents();
155 var elementIsFirstChild = nodeElement.childIndex(element);
156 if(element.bound()) {
157 canvas.setCurrentElement(element, {caretTo: position.offset});
159 if(elementIsFirstChild) {
160 canvas.setCurrentElement(range.element1, {caretTo: 'end'});
162 canvas.setCurrentElement(range.element2, {caretTo: 'end'});
166 canvas.setCurrentElement(range.element1, {caretTo: 'start'});
173 run: function(name, params, canvas) {
174 return commands.run(name, params, canvas);