editor: minor usability tweak
[fnpeditor.git] / src / editor / modules / documentCanvas / documentCanvas.js
1 // Module that implements main WYSIWIG edit area
2
3 define([
4 'libs/jquery',
5 'libs/underscore',
6 'fnpjs/logging/logging',
7 './canvas/canvas',
8 'libs/text!./template.html'], function($, _, logging, canvas3, template) {
9
10 'use strict';
11
12
13 var logger = logging.getLogger('documentCanvas');
14
15 return function(sandbox) {
16
17     var canvas = canvas3.fromXMLDocument(null);
18     var canvasWrapper = $(template);
19     var shownAlready = false;
20     var scrollbarPosition = 0,
21         actionHandlers = {},
22         cursorPosition;
23         
24     
25     canvas.on('selectionChanged', function(selection) {
26         sandbox.publish('selectionChanged', selection);
27     });
28
29     canvasWrapper.onShow = function() {
30         if(!shownAlready) {
31             shownAlready = true;
32             canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
33         } else {
34             canvas.setCursorPosition(cursorPosition);
35             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
36         }
37     };
38     
39     canvasWrapper.onHide = function() {
40        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
41        cursorPosition = canvas.getCursor().getPosition();
42     };
43
44     /* public api */
45     return {
46         start: function() {
47             sandbox.getPlugins().forEach(function(plugin) {
48                 var handlers;
49                 if(plugin.canvas) {
50                     handlers = plugin.canvas.actionHandlers;
51                     if(handlers && !_.isArray(handlers)) {
52                         handlers = [handlers];
53                     }
54                     actionHandlers[plugin.name] = handlers;
55                 }
56             });
57             sandbox.publish('ready');
58         },
59         getView: function() {
60             return canvasWrapper;
61         },
62         getCanvas: function() {
63             return canvas;
64         },
65         setDocument: function(wlxmlDocument) {
66             canvas.loadWlxmlDocument(wlxmlDocument);
67             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
68         },
69         highlightElement: function(node) {
70             canvas.toggleElementHighlight(node, true);
71         },
72         dimElement: function(node) {
73             canvas.toggleElementHighlight(node, false);
74         },
75         jumpToElement: function(node) {
76             canvas.setCurrentElement(node);
77         },
78         onAfterActionExecuted: function(action, ret) {
79             if(ret && ret instanceof canvas.wlxmlDocument.CaretFragment && ret.isValid()) {
80                 logger.debug('The action returned a valid fragment');
81                 canvas.setCurrentElement(ret.node, {caretTo: ret.offset});
82                 return;
83             }
84             logger.debug('No valid fragment returned from the action');
85
86             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
87                 handler(canvas, action, ret);
88             });
89         }
90     };
91     
92 };
93
94 });