Restore focus to the event trap if reset by an operation

With the event trap now being inside the cursor, any repositioning of
the cursor causes the document.activeElement and window selection to
change. This causes the document to stop being editable after actions
such as bolding current text.

Now, the focus state of the event trap is checked prior to each
operation, and will be reset back to the event trap if disrupted by an
operation. Note, checks are in place to prevent stealing focus if the
document is not active when an operation is executed.

Fixes issue #298
This commit is contained in:
Philip Peitsch
2014-01-08 21:32:10 +11:00
parent f87a9e16d3
commit 8de8dde612
6 changed files with 56 additions and 24 deletions
+2 -2
View File
@@ -454,7 +454,7 @@ gui.DirectTextStyler = function DirectTextStyler(session, inputMemberId) {
odtDocument.unsubscribe(ops.OdtDocument.signalCursorMoved, onCursorMoved);
odtDocument.unsubscribe(ops.OdtDocument.signalParagraphStyleModified, onParagraphStyleModified);
odtDocument.unsubscribe(ops.OdtDocument.signalParagraphChanged, onParagraphChanged);
odtDocument.unsubscribe(ops.OdtDocument.signalOperationExecuted, clearCursorStyle);
odtDocument.unsubscribe(ops.OdtDocument.signalOperationEnd, clearCursorStyle);
callback();
};
@@ -464,7 +464,7 @@ gui.DirectTextStyler = function DirectTextStyler(session, inputMemberId) {
odtDocument.subscribe(ops.OdtDocument.signalCursorMoved, onCursorMoved);
odtDocument.subscribe(ops.OdtDocument.signalParagraphStyleModified, onParagraphStyleModified);
odtDocument.subscribe(ops.OdtDocument.signalParagraphChanged, onParagraphChanged);
odtDocument.subscribe(ops.OdtDocument.signalOperationExecuted, clearCursorStyle);
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, clearCursorStyle);
updatedCachedValues();
}
+21 -13
View File
@@ -153,12 +153,26 @@ runtime.loadClass("ops.OdtCursor");
return odtDocument.getOdfCanvas().getElement();
}
/**
* Set the local cursor's current composition state. If there is no local cursor,
* this function will do nothing
* @param {!boolean} state
* @returns {undefined}
*/
function setCursorComposing(state) {
if (localCursor) {
if (state) {
localCursor.getNode().setAttributeNS(cursorns, "composing", "true");
} else {
localCursor.getNode().removeAttributeNS(cursorns, "composing");
}
}
}
function flushEvent() {
var cursorNode;
if (pendingEvent) {
pendingEvent = false;
cursorNode = localCursor.getNode();
cursorNode.removeAttributeNS(cursorns, "composing");
setCursorComposing(false);
events.emit(gui.InputMethodEditor.signalCompositionEnd, {data: pendingData});
pendingData = "";
}
@@ -214,10 +228,6 @@ runtime.loadClass("ops.OdtCursor");
textNode.replaceData(0, textNode.length, FAKE_CONTENT);
}
// Obtain document focus again after a cursor update
// This is necessary because the cursor currently removes then adds itself back into the DOM
// breaking the focus on the event trap as a result
eventManager.focus();
selection.collapse(eventTrap.firstChild, 0);
if (selection.extend) {
selection.extend(eventTrap, eventTrap.childNodes.length);
@@ -225,13 +235,12 @@ runtime.loadClass("ops.OdtCursor");
}
function compositionStart() {
var cursorNode = localCursor.getNode();
lastCompositionData = undefined;
// Some IMEs will stack end & start requests back to back.
// Aggregate these as a group and report them in a single request once all are
// complete to avoid the selection being reset
processUpdates.cancel();
cursorNode.setAttributeNS(cursorns, "composing", "true");
setCursorComposing(true);
if (!pendingEvent) {
events.emit(gui.InputMethodEditor.signalCompositionStart, {data: ""});
}
@@ -261,13 +270,12 @@ runtime.loadClass("ops.OdtCursor");
if (cursor.getMemberId() === inputMemberId) {
hasFocus = eventManager.hasFocus();
localCursor = cursor;
localCursor.subscribe(ops.OdtCursor.signalCursorUpdated, resetWindowSelection);
cursorNode = localCursor.getNode();
cursorNode.insertBefore(eventTrap, cursorNode.firstChild);
if (hasFocus) {
// Relocating the event trap will reset the window selection
// Restore this again if the document previously had focus
resetWindowSelection();
eventManager.focus();
}
}
};
@@ -280,13 +288,12 @@ runtime.loadClass("ops.OdtCursor");
var hasFocus;
if (localCursor && memberid === inputMemberId) {
hasFocus = eventManager.hasFocus();
localCursor.unsubscribe(ops.OdtCursor.signalCursorUpdated, resetWindowSelection);
localCursor = null;
getCanvasElement().appendChild(eventTrap);
if (hasFocus) {
// Relocating the event trap will reset the window selection
// Restore this again if the document previously had focus
resetWindowSelection();
eventManager.focus();
}
}
};
@@ -326,6 +333,7 @@ runtime.loadClass("ops.OdtCursor");
eventManager.subscribe('keypress', flushEvent);
eventManager.subscribe('mousedown', suppressFocus);
eventManager.subscribe('mouseup', restoreFocus);
eventManager.subscribe('focus', resetWindowSelection);
filters.push(new DetectSafariCompositionError(eventManager));
cleanup = filters.map(function(filter) { return filter.destroy; });
+25 -4
View File
@@ -118,11 +118,30 @@ gui.SessionController = (function () {
selectionController = new gui.SelectionController(session, inputMemberId),
modifier = gui.KeyboardHandler.Modifier,
keyCode = gui.KeyboardHandler.KeyCode,
isMacOS = window.navigator.appVersion.toLowerCase().indexOf("mac") !== -1;
isMacOS = window.navigator.appVersion.toLowerCase().indexOf("mac") !== -1,
hadFocus;
runtime.assert(window !== null,
"Expected to be run in an environment which has a global window, like a browser.");
function saveFocus() {
hadFocus = eventManager.hasFocus();
}
/**
* Execution of an operation can cause the focus to be lost if the local cursor
* shifts around. Restore focus back to the event trap if it was in focus before
* the operation.
*/
function restoreFocus() {
if (hadFocus) {
// Only restore focus if previously in focus to prevent
// stealing focus when a remote operation occurs
eventManager.focus();
}
hadFocus = undefined;
}
function getTarget(e) {
// e.srcElement because IE10 likes to be different...
return e.target || e.srcElement;
@@ -601,7 +620,7 @@ gui.SessionController = (function () {
eventManager.subscribe("paste", handlePaste);
// start maintaining the cursor selection now
odtDocument.subscribe(ops.OdtDocument.signalOperationExecuted, updateUndoStack);
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, updateUndoStack);
if (undoManager) {
// For most undo managers, the initial state is a clean document *with* a cursor present
@@ -670,7 +689,7 @@ gui.SessionController = (function () {
undoManager.resetInitialState();
}
odtDocument.unsubscribe(ops.OdtDocument.signalOperationExecuted, updateUndoStack);
odtDocument.unsubscribe(ops.OdtDocument.signalOperationEnd, updateUndoStack);
inputMethodEditor.unsubscribe(gui.InputMethodEditor.signalCompositionStart, textManipulator.removeCurrentSelection);
inputMethodEditor.unsubscribe(gui.InputMethodEditor.signalCompositionEnd, insertNonEmptyData);
@@ -898,9 +917,11 @@ gui.SessionController = (function () {
eventManager.subscribe("dragend", handleDragEnd);
eventManager.subscribe("click", hyperlinkClickHandler.handleClick);
odtDocument.subscribe(ops.OdtDocument.signalOperationExecuted, redrawRegionSelectionTask.trigger);
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, redrawRegionSelectionTask.trigger);
odtDocument.subscribe(ops.OdtDocument.signalCursorAdded, inputMethodEditor.registerCursor);
odtDocument.subscribe(ops.OdtDocument.signalCursorRemoved, inputMethodEditor.removeCursor);
odtDocument.subscribe(ops.OdtDocument.signalOperationStart, saveFocus);
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, restoreFocus);
}
init();
+5 -3
View File
@@ -75,7 +75,8 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
ops.OdtDocument.signalCommonStyleCreated,
ops.OdtDocument.signalCommonStyleDeleted,
ops.OdtDocument.signalTableAdded,
ops.OdtDocument.signalOperationExecuted,
ops.OdtDocument.signalOperationStart,
ops.OdtDocument.signalOperationEnd,
ops.OdtDocument.signalUndoStackChanged,
ops.OdtDocument.signalStepsInserted,
ops.OdtDocument.signalStepsRemoved
@@ -843,7 +844,7 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
stepsTranslator = new ops.StepsTranslator(getRootNode, gui.SelectionMover.createPositionIterator, filter, 500);
eventNotifier.subscribe(ops.OdtDocument.signalStepsInserted, stepsTranslator.handleStepsInserted);
eventNotifier.subscribe(ops.OdtDocument.signalStepsRemoved, stepsTranslator.handleStepsRemoved);
eventNotifier.subscribe(ops.OdtDocument.signalOperationExecuted, handleOperationExecuted);
eventNotifier.subscribe(ops.OdtDocument.signalOperationEnd, handleOperationExecuted);
}
init();
};
@@ -859,7 +860,8 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
/**@const*/ops.OdtDocument.signalCommonStyleCreated = "style/created";
/**@const*/ops.OdtDocument.signalCommonStyleDeleted = "style/deleted";
/**@const*/ops.OdtDocument.signalParagraphStyleModified = "paragraphstyle/modified";
/**@const*/ops.OdtDocument.signalOperationExecuted = "operation/executed";
/**@const*/ops.OdtDocument.signalOperationStart = "operation/start";
/**@const*/ops.OdtDocument.signalOperationEnd = "operation/end";
/**@const*/ops.OdtDocument.signalUndoStackChanged = "undo/changed";
/**@const*/ops.OdtDocument.signalStepsInserted = "steps/inserted";
/**@const*/ops.OdtDocument.signalStepsRemoved = "steps/removed";
+2 -1
View File
@@ -74,8 +74,9 @@ ops.Session = function Session(odfCanvas) {
this.setOperationRouter = function (opRouter) {
operationRouter = opRouter;
opRouter.setPlaybackFunction(function (op) {
odtDocument.emit(ops.OdtDocument.signalOperationStart, op);
if (op.execute(odtDocument)) {
odtDocument.emit(ops.OdtDocument.signalOperationExecuted, op);
odtDocument.emit(ops.OdtDocument.signalOperationEnd, op);
return true;
}
return false;
+1 -1
View File
@@ -263,7 +263,7 @@ ops.OperationTests = function OperationTests(runner) {
op = factory.create(test.ops[i]);
op.execute(t.odtDocument);
if (metabefore) {
t.odtDocument.emit(ops.OdtDocument.signalOperationExecuted, op);
t.odtDocument.emit(ops.OdtDocument.signalOperationEnd, op);
}
}