Make only EventManager handle eventTrap in the

DOM.
Restore the eventTrap and focus on undo.
This commit is contained in:
Aditya Bhatt
2014-02-20 17:35:09 +01:00
parent 3ebb3ed1e1
commit 67c1c23c24
3 changed files with 32 additions and 46 deletions
+16 -14
View File
@@ -57,11 +57,11 @@ gui.EventManager = function EventManager(odtDocument) {
// Epiphany 3.6.1 requires this to allow the paste event to fire
"beforepaste": true
},
// Events that should be bound to the global window rather than the canvas element
// Events that should be bound to the global window rather than the sizer element
bindToWindow = {
// Capture selections that start outside the canvas element and end within the canvas element
// Capture selections that start outside the sizer element and end within the sizer element
"mousedown": true,
// Capture selections that start inside the canvas element and end outside of the element or even window
// Capture selections that start inside the sizer element and end outside of the element or even window
"mouseup": true,
// Focus is a non-bubbling event, and we'll usually pass focus to the event trap
"focus": true
@@ -72,14 +72,14 @@ gui.EventManager = function EventManager(odtDocument) {
eventTrap;
/**
* Get the current canvas element. The current trivial undo manager replaces the root element
* Get the current sizer element. The current trivial undo manager replaces the root element
* of the ODF container with a clone from a previous state. This results in the root element
* being changed. As such, it can't be stored, and should be queried on each use.
* @return {!Element}
*/
function getCanvasElement() {
function getSizerElement() {
// TODO Remove when a proper undo manager arrives
return odtDocument.getOdfCanvas().getElement();
return odtDocument.getOdfCanvas().getSizer();
}
/**
@@ -188,9 +188,9 @@ gui.EventManager = function EventManager(odtDocument) {
*/
function getDelegateForEvent(eventName, shouldCreate) {
var delegate = eventDelegates[eventName] || null,
canvasElement;
sizerElement;
if (!delegate && shouldCreate) {
canvasElement = getCanvasElement();
sizerElement = getSizerElement();
delegate = eventDelegates[eventName] = new EventDelegate();
if (bindToWindow[eventName]) {
// Internet explorer will only supply mouse up & down on the window object
@@ -198,8 +198,8 @@ gui.EventManager = function EventManager(odtDocument) {
listenEvent(window, eventName, delegate.handleEvent);
}
listenEvent(eventTrap, eventName, delegate.handleEvent);
// TODO this needs to be rebound if canvasElement changes
listenEvent(canvasElement, eventName, delegate.handleEvent);
// TODO this needs to be rebound if sizerElement changes
listenEvent(sizerElement, eventName, delegate.handleEvent);
}
return delegate;
}
@@ -318,7 +318,7 @@ gui.EventManager = function EventManager(odtDocument) {
var eventTrapParentNode = eventTrap.parentNode;
// InputMethodEditor moves the eventTrap around,
// so do not rely on still being a child of the current canvas element
// so do not rely on still being a child of the current sizer element
if (eventTrapParentNode) {
eventTrapParentNode.removeChild(eventTrap);
}
@@ -330,13 +330,15 @@ gui.EventManager = function EventManager(odtDocument) {
};
function init() {
var canvasElement = getCanvasElement(),
doc = canvasElement.ownerDocument;
var sizerElement = getSizerElement(),
doc = sizerElement.ownerDocument;
runtime.assert(Boolean(window), "EventManager requires a window object to operate correctly");
eventTrap = /**@type{!HTMLDivElement}*/(doc.createElement("div"));
eventTrap.id = "eventTrap";
canvasElement.appendChild(eventTrap);
// Negative tab index still allows focus, but removes accessibility by keyboard
eventTrap.setAttribute("tabindex", -1);
sizerElement.appendChild(eventTrap);
}
init();
};
+1 -28
View File
@@ -109,10 +109,9 @@
* @constructor
* @implements {core.Destroyable}
* @param {!string} inputMemberId
* @param {!ops.OdtDocument} odtDocument
* @param {!gui.EventManager} eventManager
*/
gui.InputMethodEditor = function InputMethodEditor(inputMemberId, odtDocument, eventManager) {
gui.InputMethodEditor = function InputMethodEditor(inputMemberId, eventManager) {
var window = runtime.getWindow(),
cursorns = "urn:webodf:names:cursor",
/**@type{ops.OdtCursor}*/
@@ -123,7 +122,6 @@
/**@type{!Element}*/
compositionElement,
async = new core.Async(),
domUtils = new core.DomUtils(),
FAKE_CONTENT = "b",
/**@type{!core.ScheduledTask}*/
processUpdates,
@@ -149,17 +147,6 @@
*/
this.unsubscribe = events.unsubscribe;
/**
* Get the current sizer element. The current trivial undo manager replaces the root element
* of the ODF container with a clone from a previous state. This results in the root element
* being changed. As such, it can't be stored, and should be queried on each use.
* @return {!Element}
*/
function getSizerElement() {
// TODO Remove when a proper undo manager arrives
return odtDocument.getOdfCanvas().getSizer();
}
/**
* Set the local cursor's current composition state. If there is no local cursor,
* this function will do nothing
@@ -203,16 +190,6 @@
textNode;
flushEvent();
if (!domUtils.containsNode(getSizerElement(), eventTrap)) {
// TODO Remove when a proper undo manager arrives
// The undo manager can replace the root element, discarding the original.
// The event trap node is still valid, and simply needs to be re-attached
// after this occurs.
// Don't worry about the local caret yet. The event trap will eventually be moved to
// a new valid local caret when it is registered upon cursor re-registration
getSizerElement().appendChild(eventTrap);
}
while (eventTrap.childNodes.length > 1) {
// Repeated text entry events can result in lots of empty text nodes
@@ -390,10 +367,6 @@
}
cleanup = filters.map(getDestroy);
// Negative tab index still allows focus, but removes accessibility by keyboard
getSizerElement().appendChild(eventTrap);
eventTrap.setAttribute("tabindex", -1);
// Initialize the composition element
compositionElement = doc.createElement('span');
compositionElement.setAttribute('id', 'composer');
+15 -4
View File
@@ -84,7 +84,7 @@
/**@type{!core.ScheduledTask}*/
redrawRegionSelectionTask,
pasteHandler = new gui.PlainTextPasteboard(odtDocument, inputMemberId),
inputMethodEditor = new gui.InputMethodEditor(inputMemberId, odtDocument, eventManager),
inputMethodEditor = new gui.InputMethodEditor(inputMemberId, eventManager),
/**@type{number}*/
clickCount = 0,
hyperlinkClickHandler = new gui.HyperlinkClickHandler(odtDocument.getRootNode),
@@ -293,12 +293,23 @@
* @return {!boolean}
*/
function undo() {
var hadFocusBefore;
var eventTrap = eventManager.getEventTrap(),
sizer,
hadFocusBefore;
if (undoManager) {
hadFocusBefore = eventManager.hasFocus();
undoManager.moveBackward(1);
if (hadFocusBefore) {
eventManager.focus();
sizer = odtDocument.getOdfCanvas().getSizer();
if (!domUtils.containsNode(sizer, eventTrap)) {
// TODO Remove when a proper undo manager arrives
// The undo manager can replace the root element, discarding the original.
// The event trap node is still valid, and simply needs to be re-attached
// after this occurs.
sizer.appendChild(eventTrap);
if (hadFocusBefore) {
eventManager.focus();
}
}
return true;
}