96b7c701bd2574e2f44c63d4626948e38c0ed9ae
[fnpeditor.git] / modules / documentCanvas / commands.js
1 define([
2
3 ], function() {
4     
5 'use strict';
6
7
8 var gridToggled = false;
9
10 var commands = {
11     _cmds: {},
12     register: function(name, command) {
13         this._cmds[name] = command;
14     },
15
16     run: function(name, params, canvas) {
17         return this._cmds[name](canvas, params);
18     }
19 };
20
21 commands.register('unwrap-node', function(canvas) {
22     var cursor = canvas.getCursor(),
23         selectionStart = cursor.getSelectionStart(),
24         selectionEnd = cursor.getSelectionEnd(),
25         parent1 = selectionStart.element.parent() || undefined,
26         parent2 = selectionEnd.element.parent() || undefined;
27
28     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
29         var selectionAnchor = cursor.getSelectionAnchor();
30         canvas.list.extractItems({element1: parent1, element2: parent2});
31         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
32     } else if(!cursor.isSelecting()) {
33         var toUnwrap = cursor.getPosition().element,
34             parent = toUnwrap.unwrap();
35         canvas.setCurrentElement(parent);
36     }
37 });
38
39 commands.register('wrap-node', function(canvas) {
40     var cursor = canvas.getCursor(),
41         selectionStart = cursor.getSelectionStart(),
42         selectionEnd = cursor.getSelectionEnd(),
43         parent1 = selectionStart.element.parent() || undefined,
44         parent2 = selectionEnd.element.parent() || undefined;
45
46     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
47         canvas.list.create({element1: parent1, element2: parent2});
48     }
49 });
50
51 commands.register('toggle-list', function(canvas, params) {
52     var cursor = canvas.getCursor(),
53         selectionStart = cursor.getSelectionStart(),
54         selectionEnd = cursor.getSelectionEnd(),
55         parent1 = selectionStart.element.parent() || undefined,
56         parent2 = selectionEnd.element.parent() || undefined;
57
58     if(params.toggle) {
59         canvas.list.create({element1: parent1, element2: parent2});
60     } else {
61         if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
62             canvas.list.extractItems({element1: parent1, element2: parent2, merge: false});
63         } 
64     }
65 });
66
67 commands.register('toggle-grid', function(canvas, params) {
68     canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
69     gridToggled = params.toggle;
70 });
71
72 commands.register('newNodeRequested', function(canvas, params) {
73     var cursor = canvas.getCursor(),
74         selectionStart = cursor.getSelectionStart(),
75         selectionEnd = cursor.getSelectionEnd();
76
77     if(cursor.isSelecting() && cursor.isSelectingSiblings()) {
78         if(cursor.isSelectingWithinElement()) {
79             var newElement = selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset}),
80                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
81             canvas.setCurrentElement(newElement.children()[0], {caretTo: caretTo});
82         }
83         else {
84             var parent = selectionStart.element.parent(),
85                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
86
87             var wrapper = canvas.wrapText({
88                 inside: parent,
89                 _with: {tag: params.wlxmlTag, klass: params.wlxmlClass},
90                 offsetStart: selectionStart.offset,
91                 offsetEnd: selectionEnd.offset,
92                 textNodeIdx: [parent.childIndex(selectionStart.element), parent.childIndex(selectionEnd.element)]
93             });
94
95             canvas.setCurrentElement(wrapper.children()[caretTo === 0 ? 0 : wrapper.children().length - 1], {caretTo: caretTo});
96         }
97     }
98 });
99
100
101 return {
102     run: function(name, params, canvas) {
103         return commands.run(name, params, canvas);
104     }
105 };
106
107 });