define([\r
'libs/jquery-1.9.1.min',\r
-'./wlxmlNode'\r
-], function($, wlxmlNode) {\r
+'./canvasNode'\r
+], function($, canvasNode) {\r
\r
'use strict';\r
\r
+var getCursorPosition = function() {\r
+ var selection = window.getSelection();\r
+ var anchorNode = $(selection.anchorNode);\r
+ var parent = anchorNode.parent();\r
+ return {\r
+ textNode: anchorNode,\r
+ textNodeOffset: selection.anchorOffset,\r
+ textNodeIndex: parent.contents().index(anchorNode),\r
+ parentNode: parent,\r
+ focusNode: $(selection.focusNode).parent(),\r
+ isAtEnd: selection.anchorOffset === anchorNode.text().length\r
+ }\r
+};\r
+\r
var Manager = function(canvas, sandbox) {\r
this.canvas = canvas;\r
this.sandbox = sandbox;\r
\r
canvas.dom.on('mouseover', '[wlxml-tag]', function(e) {\r
e.stopPropagation();\r
- manager.sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target)));\r
+ manager.sandbox.publish('nodeHovered', canvasNode.create($(e.target)));\r
});\r
canvas.dom.on('mouseout', '[wlxml-tag]', function(e) {\r
e.stopPropagation();\r
- manager.sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target)));\r
+ manager.sandbox.publish('nodeBlured', canvasNode.create($(e.target)));\r
});\r
canvas.dom.on('click', '[wlxml-tag]', function(e) {\r
e.stopPropagation();\r
console.log('clicked node type: '+e.target.nodeType);\r
- manager.selectNode(new wlxmlNode.Node($(e.target)));\r
+ manager.selectNode(canvasNode.create($(e.target)));\r
});\r
\r
canvas.dom.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
var anchor = $(window.getSelection().anchorNode);\r
+ \r
if(anchor[0].nodeType === Node.TEXT_NODE)\r
anchor = anchor.parent();\r
if(!anchor.is('[wlxml-tag]'))\r
return;\r
- manager.selectNode(new wlxmlNode.Node(anchor));\r
+ manager.selectNode(canvasNode.create(anchor));\r
});\r
\r
canvas.dom.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
if(e.which === 13) { \r
- e.preventDefault();\r
- manager.insertNewNode(null, null);\r
+ manager.onEnterKey(e);\r
}\r
if(e.which === 8) {\r
- var anchor = window.getSelection().anchorNode;\r
- var len = anchor.length;\r
- console.log(len);\r
- if(len === 1) {\r
- e.preventDefault();\r
- $(anchor).parent().text('');\r
- }\r
+ manager.onBackspaceKey(e);\r
}\r
});\r
\r
}\r
};\r
\r
-Manager.prototype.selectNode = function(wlxmlNode, options) {\r
+Manager.prototype.selectNode = function(cnode, options) {\r
options = options || {};\r
- var nodeElement = this.getNodeElement(wlxmlNode)\r
+ var nodeElement = this.getNodeElement(cnode)\r
\r
- this.dimNode(wlxmlNode);\r
+ this.dimNode(cnode);\r
\r
this.canvas.dom.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
\r
if(options.movecaret) {\r
- this.movecaretToNode(nodeElement);\r
+ this.movecaretToNode(nodeElement, options.movecaret);\r
}\r
\r
- this.currentNode = wlxmlNode;\r
- this.sandbox.publish('nodeSelected', wlxmlNode);\r
+ this.currentNode = cnode;\r
+ this.sandbox.publish('nodeSelected', cnode);\r
};\r
\r
-Manager.prototype.getNodeElement = function(wlxmlNode) {\r
- return this.canvas.dom.find('#'+wlxmlNode.id);\r
+Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {\r
+ var selection = window.getSelection();\r
+\r
+ if(selection.getRangeAt(0).collapsed) {\r
+ \r
+ } else {\r
+ var offsetStart = selection.anchorOffset;\r
+ var offsetEnd = selection.focusOffset;\r
+ if(offsetStart > offsetEnd) {\r
+ var tmp = offsetStart;\r
+ offsetStart = offsetEnd;\r
+ offsetEnd = tmp;\r
+ }\r
+ \r
+ \r
+ var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
+ var parent = $(selection.anchorNode).parent();\r
+ \r
+ var textNodeIdx = parent.contents().index($(selection.anchorNode));\r
+ if(selection.anchorNode != selection.focusNode) {\r
+ textNodeIdx = [textNodeIdx, parent.contents().index($(selection.focusNode))];\r
+ }\r
+ this.canvas.nodeWrap({inside: canvasNode.create(parent),\r
+ _with: wrapper,\r
+ offsetStart: offsetStart,\r
+ offsetEnd: offsetEnd,\r
+ textNodeIdx: textNodeIdx\r
+ });\r
+ this.selectNode(wrapper, {movecaret: 'end'});\r
+ }\r
+ \r
+ \r
+}\r
+\r
+Manager.prototype.getNodeElement = function(cnode) {\r
+ return this.canvas.dom.find('#'+cnode.getId());\r
};\r
\r
-Manager.prototype.highlightNode = function(wlxmlNode) {\r
- var nodeElement = this.getNodeElement(wlxmlNode);\r
+Manager.prototype.highlightNode = function(cnode) {\r
+ var nodeElement = this.getNodeElement(cnode);\r
if(!this.gridToggled) {\r
nodeElement.addClass('rng-common-hoveredNode');\r
var label = nodeElement.attr('wlxml-tag');\r
}\r
};\r
\r
-Manager.prototype.dimNode = function(wlxmlNode) {\r
- var nodeElement = this.getNodeElement(wlxmlNode);\r
+Manager.prototype.dimNode = function(cnode) {\r
+ var nodeElement = this.getNodeElement(cnode);\r
if(!this.gridToggled) {\r
nodeElement.removeClass('rng-common-hoveredNode');\r
nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();\r
else {\r
node = this.canvas.dom.find('[wlxml-class|="p"]')\r
}\r
- this.selectNode(new wlxmlNode.Node(node), {movecaret: true});\r
+ this.selectNode(canvasNode.create(node), {movecaret: true});\r
};\r
\r
-Manager.prototype.movecaretToNode = function(nodeElement) {\r
+Manager.prototype.movecaretToNode = function(nodeElement, where) {\r
+ if(!nodeElement.length)\r
+ return;\r
var range = document.createRange();\r
range.selectNodeContents(nodeElement[0]);\r
- range.collapse(false);\r
+ \r
+ var collapseArg = true;\r
+ if(where === 'end')\r
+ collapseArg = false;\r
+ range.collapse(collapseArg);\r
var selection = document.getSelection();\r
selection.removeAllRanges()\r
selection.addRange(range);\r
this.gridToggled = toggle;\r
};\r
\r
-Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {\r
- //TODO: Insert inline\r
- var anchor = $(window.getSelection().anchorNode);\r
- var anchorOffset = window.getSelection().anchorOffset;\r
- \r
- var parent = anchor.parent();\r
- var idx = parent.contents().index(anchor);\r
- \r
- if(anchorOffset < anchor.text().length) {\r
- var newNode = this.canvas.splitNode({node: {id: parent.attr('id')}, textNodeIdx: idx, offset: anchorOffset});\r
- this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
- }\r
+Manager.prototype.onEnterKey = function(e) {\r
+ e.preventDefault();\r
+ var pos = getCursorPosition();\r
+ var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id'));\r
+ var newNode;\r
\r
- \r
- \r
+ if(pos.isAtEnd) {\r
+ newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});\r
+ this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))});\r
+ } else {\r
+ newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});\r
+ }\r
+ if(newNode)\r
+ this.selectNode(newNode, {movecaret: true});\r
this.sandbox.publish('contentChanged');\r
};\r
\r
+Manager.prototype.onBackspaceKey = function(e) {\r
+ var pos = getCursorPosition();\r
+ var len = pos.textNode.text().length;\r
+ if(len === 1) {\r
+ // Prevent deleting node by browser after last character removed;\r
+ e.preventDefault();\r
+ pos.parentNode.text('');\r
+ }\r
+ if(len === 0) {\r
+ e.preventDefault();\r
+ var toRemove = canvasNode.create(pos.textNode);\r
+ var prevNode = this.canvas.getPrecedingNode({node:toRemove});\r
+ this.canvas.nodeRemove({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem\r
+ this.selectNode(prevNode, {movecaret: 'end'});\r
+ }\r
+}\r
+\r
+Manager.prototype.command = function(command, meta) {\r
+ var pos = getCursorPosition();\r
+ \r
+ if(command === 'createList') {\r
+ var node = canvasNode.create(pos.parentNode);\r
+ if(window.getSelection().getRangeAt(0).collapsed && this.canvas.nodeInsideList({node: node})) {\r
+ this.canvas.listRemove({pointer: node});\r
+ this.selectNode(node, {movecaret: 'end'});\r
+ this.sandbox.publish('contentChanged');\r
+ }\r
+ else {\r
+ //if(!this.canvas.nodeInsideList({node: node})) {\r
+ this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode), type: meta});\r
+ this.selectNode(node, {movecaret: 'end'});\r
+ this.sandbox.publish('contentChanged');\r
+ //}\r
+ }\r
+ }\r
+\r
+}\r
+\r
+\r
return Manager;\r
\r
});
\ No newline at end of file