1 // Module that implements main WYSIWIG edit area
\r
4 'libs/underscore-min',
\r
5 './transformations',
\r
9 'libs/text!./template.html'], function(_, transformations, wlxmlNode, Canvas, CanvasManager, template) {
\r
13 return function(sandbox) {
\r
15 var canvas = new Canvas.Canvas();
\r
17 var manager = new CanvasManager(canvas, sandbox);
\r
22 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
23 //TODO: Insert inline
\r
24 var anchor = $(window.getSelection().anchorNode);
\r
25 var anchorOffset = window.getSelection().anchorOffset;
\r
27 if(anchor[0].nodeType === Node.TEXT_NODE) {
\r
28 textLen = anchor.text().length;
\r
29 anchor = anchor.parent();
\r
31 if(anchor.text() === '') {
\r
33 anchor = anchor.parent();
\r
36 if(anchorOffset > 0 && anchorOffset < textLen) {
\r
37 if(wlxmlTag === null && wlxmlClass === null) {
\r
38 return this.splitWithNewNode(anchor);
\r
40 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
42 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
43 if(anchorOffset === 0)
\r
44 anchor.before(newNode)
\r
46 anchor.after(newNode);
\r
47 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});
\r
49 sandbox.publish('contentChanged');
\r
51 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
52 var selection = window.getSelection();
\r
53 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
54 var startOffset = selection.anchorOffset;
\r
55 var endOffset = selection.focusOffset;
\r
56 if(startOffset > endOffset) {
\r
57 var tmp = startOffset;
\r
58 startOffset = endOffset;
\r
61 var node = selection.anchorNode;
\r
62 var prefix = node.data.substr(0, startOffset);
\r
63 var suffix = node.data.substr(endOffset);
\r
64 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
65 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
66 newNode.text(core || 'test');
\r
67 $(node).replaceWith(newNode);
\r
68 newNode.before(prefix);
\r
69 newNode.after(suffix);
\r
71 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});
\r
73 sandbox.publish('contentChanged');
\r
76 splitWithNewNode: function(node) {
\r
77 var selection = window.getSelection();
\r
78 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
79 var startOffset = selection.anchorOffset;
\r
80 var endOffset = selection.focusOffset;
\r
81 if(startOffset > endOffset) {
\r
82 var tmp = startOffset;
\r
83 startOffset = endOffset;
\r
86 var anchor = selection.anchorNode;
\r
87 var prefix = anchor.data.substr(0, startOffset);
\r
88 var suffix = anchor.data.substr(endOffset);
\r
89 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
90 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
91 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
92 prefixNode.text(prefix);
\r
93 suffixNode.text(suffix);
\r
94 node.replaceWith(newNode);
\r
95 newNode.before(prefixNode);
\r
96 newNode.after(suffixNode);
\r
98 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});
\r
100 sandbox.publish('contentChanged');
\r
109 start: function() { sandbox.publish('ready'); },
\r
110 getView: function() {
\r
113 setDocument: function(xml) {
\r
114 canvas.setXML(xml);
\r
115 sandbox.publish('documentSet');
\r
117 getDocument: function() {
\r
118 return canvas.toXML();
\r
120 modifyCurrentNode: function(attr, value) {
\r
121 if(manager.currentNode) {
\r
122 manager.getNodeElement(manager.currentNode).attr('wlxml-'+attr, value);
\r
123 sandbox.publish('contentChanged');
\r
126 highlightNode: function(wlxmlNode) {
\r
127 manager.highlightNode(wlxmlNode);
\r
129 dimNode: function(wlxmlNode) {
\r
130 manager.dimNode(wlxmlNode);
\r
132 selectNode: function(wlxmlNode) {
\r
133 if(!wlxmlNode.is(manager.currentNode))
\r
134 manager.selectNode(wlxmlNode, {movecaret: true});
\r
136 toggleGrid: function(toggle) {
\r
137 manager.toggleGrid(toggle);
\r
139 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
140 manager.insertNewNode(wlxmlTag, wlxmlClass);
\r
142 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
143 manager.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r