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})) {
30 var selectionAnchor = cursor.getSelectionAnchor();
31 canvas.list.extractItems({element1: parent1, element2: parent2});
32 canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
33 } else if(!cursor.isSelecting()) {
34 var toUnwrap = cursor.getPosition().element,
35 parent = toUnwrap.unwrap();
36 canvas.setCurrentElement(parent);
40 commands.register('wrap-node', function(canvas) {
41 var cursor = canvas.getCursor(),
42 selectionStart = cursor.getSelectionStart(),
43 selectionEnd = cursor.getSelectionEnd(),
44 parent1 = selectionStart.element.parent() || undefined,
45 parent2 = selectionEnd.element.parent() || undefined;
47 if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
48 canvas.list.create({element1: parent1, element2: parent2});
52 commands.register('list', function(canvas, params) {
53 var cursor = canvas.getCursor(),
54 selectionStart = cursor.getSelectionStart(),
55 selectionEnd = cursor.getSelectionEnd(),
56 parent1 = selectionStart.element.parent() || undefined,
57 parent2 = selectionEnd.element.parent() || undefined;
59 var selectionFocus = cursor.getSelectionFocus();
61 if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
65 canvas.list.create({element1: parent1, element2: parent2});
67 canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
70 commands.register('toggle-grid', function(canvas, params) {
71 canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
72 gridToggled = params.toggle;
75 commands.register('newNodeRequested', function(canvas, params) {
76 var cursor = canvas.getCursor(),
77 selectionStart = cursor.getSelectionStart(),
78 selectionEnd = cursor.getSelectionEnd();
80 if(cursor.isSelecting()) {
81 if(cursor.isSelectingSiblings()) {
82 if(cursor.isSelectingWithinElement()) {
83 var wlxmlNode = selectionStart.element.data('wlxmlNode'),
84 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
85 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
86 wrapperCanvasElement = utils.findCanvasElement(wrapper);
87 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
90 var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
91 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
93 var wrapper = wlxmlNode.wrapText({
94 _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
95 offsetStart: selectionStart.offset,
96 offsetEnd: selectionEnd.offset,
97 textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
99 wrapperCanvasElement = utils.findCanvasElement(wrapper);
100 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
103 var node1 = selectionStart.element.data('wlxmlNode'),
104 node2 = selectionEnd.element.data('wlxmlNode'),
105 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
108 canvas.wlxmlDocument.wrapNodes({
109 element1: siblingParents.node1,
110 element2: siblingParents.node2,
111 _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
115 } else if(canvas.getCurrentNodeElement()) {
116 var node = findCanvasElement(canvas.getCurrentNodeElement),
117 wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
118 canvas.setCurrentElement(findCanvasElement(wrapper));
124 commands.register('footnote', function(canvas, params) {
125 var cursor = canvas.getCursor(),
126 position = cursor.getPosition(),
130 if(cursor.isSelectingWithinElement()) {
131 asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
133 asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
134 asideElement.append({text: ''});
137 asideElement.toggle(true);
138 canvas.setCurrentElement(asideElement);
141 commands.register('take-away-node', function(canvas) {
142 var position = canvas.getCursor().getPosition(),
143 element = position.element,
144 nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
146 if(!nodeElement || !(nodeElement.parent()))
150 var range = nodeElement.unwrapContents();
153 var elementIsFirstChild = nodeElement.childIndex(element);
154 if(element.bound()) {
155 canvas.setCurrentElement(element, {caretTo: position.offset});
157 if(elementIsFirstChild) {
158 canvas.setCurrentElement(range.element1, {caretTo: 'end'});
160 canvas.setCurrentElement(range.element2, {caretTo: 'end'});
164 canvas.setCurrentElement(range.element1, {caretTo: 'start'});
171 run: function(name, params, canvas) {
172 return commands.run(name, params, canvas);