wlxml: Document's root is always a context root
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.js
index 6f648ec..0259452 100644 (file)
@@ -12,8 +12,9 @@ define([
 'modules/documentCanvas/canvas/nullElement',
 'modules/documentCanvas/canvas/gutter',
 'modules/documentCanvas/canvas/selection',
+'modules/documentCanvas/canvas/keyEvent',
 'libs/text!./canvas.html'
-], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter, selection, canvasTemplate) {
+], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter, selection, keyEvent, canvasTemplate) {
     
 'use strict';
 /* global document:false, window:false, Node:false, gettext */
@@ -146,15 +147,20 @@ $.extend(Canvas.prototype, Backbone.Events, {
         this.rootWrapper.append(this.rootElement.dom);
     },
 
+
+    triggerKeyEvent: function(keyEvent, selection) {
+        selection = selection || this.getSelection();
+        if(selection && (selection.type === 'caret' || selection.type === 'textSelection') && selection.toDocumentFragment().isValid()) {
+            keyboard.handleKeyEvent(keyEvent, selection);
+        }
+    },
+
     setupEventHandling: function() {
         var canvas = this;
 
         /* globals document */
         $(document.body).on('keydown', function(e) {
-            var cursor = canvas.getCursor();
-            if(cursor.isSelecting() || Object.keys(cursor.getPosition()).length) {
-                keyboard.handleKey(e, canvas);
-            }
+            canvas.triggerKeyEvent(keyEvent.fromNativeEvent(e));
         });
 
         this.rootWrapper.on('mouseup', function() {
@@ -350,6 +356,13 @@ $.extend(Canvas.prototype, Backbone.Events, {
         }
     },
 
+    setSelection: function(selection) {
+        this.select(this, selection.toDocumentFragment());
+    },
+
+    createSelection: function(params) {
+        return selection.fromParams(this, params);
+    },
     setCurrentElement: function(element, params) {
         if(!element) {
             logger.debug('Invalid element passed to setCurrentElement: ' + element);