From 679bb699705a3eb689b108f79aa60b838073e6c1 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Tue, 13 Mar 2012 13:35:15 -0700 Subject: [PATCH 1/3] Wire up Save command to work on the currently focused editor. --- src/Editor.js | 6 ++++++ src/EditorManager.js | 32 ++++++++++++++++++++++++++++++-- src/FileCommandHandlers.js | 6 +++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Editor.js b/src/Editor.js index 4b3f98ce389..7f1807481d8 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -451,6 +451,12 @@ define(function (require, exports, module) { this._codeMirror.focus(); }; + /** Returns true if the editor has focus */ + Editor.prototype.hasFocus = function () { + // The CodeMirror instance wrapper has a "CodeMirror-focused" class set when focused + return $(this._codeMirror.getWrapperElement()).hasClass("CodeMirror-focused"); + }; + /** * Refreshes the editor control */ diff --git a/src/EditorManager.js b/src/EditorManager.js index 235a4be0ca3..1e41cbe4dab 100644 --- a/src/EditorManager.js +++ b/src/EditorManager.js @@ -198,7 +198,7 @@ define(function (require, exports, module) { * @param {!FileEntry} sourceFile The file from which the text was drawn. Ties the inline editor * back to the full editor from which edits can be saved; also determines the editor's mode. * - * @returns {{content:DOMElement, height:Number, onAdded:function(inlineId:Number)}} + * @returns {{content:DOMElement, editor:Editor, source: FileEntry, height:Number, onAdded:function(inlineId:Number)}} */ function createInlineEditorFromText(hostEditor, text, range, sourceFile) { // Container to hold editor & render its stylized frame @@ -289,7 +289,7 @@ define(function (require, exports, module) { inlineEditor.focus(); } - return { content: inlineContent, editor: inlineEditor, height: 0, onAdded: afterAdded }; + return { content: inlineContent, editor: inlineEditor, source: sourceFile, height: 0, onAdded: afterAdded }; } @@ -451,6 +451,33 @@ define(function (require, exports, module) { _editorHolder = holder; } + /** + * Returns the currently focused editor instance. + * @returns {{editor:Editor, source:FileEntry}} + */ + function getFocusedEditor() { + if (_currentEditor) { + var focusedInline; + + // See if any inlines have focus + _currentEditor.getInlineWidgets().forEach(function (widget) { + if (widget.data.editor.hasFocus()) { + focusedInline = { editor: widget.data.editor, source: widget.data.source }; + } + }); + + if (focusedInline) { + return focusedInline; + } + + if (_currentEditor.hasFocus()) { + return { editor: _currentEditor, source: _currentEditorsDocument.file }; + } + } + + return null; + } + // Initialize: register listeners $(DocumentManager).on("currentDocumentChange", _onCurrentDocumentChange); $(DocumentManager).on("workingSetRemove", _onWorkingSetRemove); @@ -463,6 +490,7 @@ define(function (require, exports, module) { exports.createFullEditorForDocument = createFullEditorForDocument; exports.createInlineEditorFromText = createInlineEditorFromText; exports.focusEditor = focusEditor; + exports.getFocusedEditor = getFocusedEditor; exports.resizeEditor = resizeEditor; exports.registerInlineEditProvider = registerInlineEditProvider; }); diff --git a/src/FileCommandHandlers.js b/src/FileCommandHandlers.js index f0de5fd8803..09bc8ace553 100644 --- a/src/FileCommandHandlers.js +++ b/src/FileCommandHandlers.js @@ -297,7 +297,11 @@ define(function (require, exports, module) { doc = commandData.doc; } if (!doc) { - doc = DocumentManager.getCurrentDocument(); + var focusedEditor = EditorManager.getFocusedEditor(); + + if (focusedEditor) { + doc = DocumentManager.getDocumentForFile(focusedEditor.source); + } } return doSave(doc); From c5464699c084bb4b8df0fb8b8640d3c133dec565 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Tue, 13 Mar 2012 13:37:06 -0700 Subject: [PATCH 2/3] Whitespace --- src/EditorManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EditorManager.js b/src/EditorManager.js index 1e41cbe4dab..456b658c422 100644 --- a/src/EditorManager.js +++ b/src/EditorManager.js @@ -198,7 +198,7 @@ define(function (require, exports, module) { * @param {!FileEntry} sourceFile The file from which the text was drawn. Ties the inline editor * back to the full editor from which edits can be saved; also determines the editor's mode. * - * @returns {{content:DOMElement, editor:Editor, source: FileEntry, height:Number, onAdded:function(inlineId:Number)}} + * @returns {{content:DOMElement, editor:Editor, source:FileEntry, height:Number, onAdded:function(inlineId:Number)}} */ function createInlineEditorFromText(hostEditor, text, range, sourceFile) { // Container to hold editor & render its stylized frame From f201ad62105778a1a4358334a0a36c4af59e84d6 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Tue, 13 Mar 2012 15:56:24 -0700 Subject: [PATCH 3/3] Add unit test for Editor.hasFocus(). Respond to code review comments. --- src/EditorManager.js | 2 +- src/FileCommandHandlers.js | 3 +++ test/spec/Editor-test.js | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/EditorManager.js b/src/EditorManager.js index 456b658c422..acf7a305ed9 100644 --- a/src/EditorManager.js +++ b/src/EditorManager.js @@ -461,7 +461,7 @@ define(function (require, exports, module) { // See if any inlines have focus _currentEditor.getInlineWidgets().forEach(function (widget) { - if (widget.data.editor.hasFocus()) { + if (widget.data.editor && widget.data.editor.hasFocus()) { focusedInline = { editor: widget.data.editor, source: widget.data.source }; } }); diff --git a/src/FileCommandHandlers.js b/src/FileCommandHandlers.js index 09bc8ace553..4b746c080ab 100644 --- a/src/FileCommandHandlers.js +++ b/src/FileCommandHandlers.js @@ -302,6 +302,9 @@ define(function (require, exports, module) { if (focusedEditor) { doc = DocumentManager.getDocumentForFile(focusedEditor.source); } + + // The doSave() method called below does a null check on doc and makes sure the + // document is dirty before saving. } return doSave(doc); diff --git a/test/spec/Editor-test.js b/test/spec/Editor-test.js index 4f2a660c69c..2c26f8c6a53 100644 --- a/test/spec/Editor-test.js +++ b/test/spec/Editor-test.js @@ -59,5 +59,29 @@ define(function (require, exports, module) { expect(mode).toEqual(""); }); }); + + describe("Focus", function () { + var myEditor; + + beforeEach(function () { + // init Editor instance (containing a CodeMirror instance) + $("body").append("
"); + myEditor = new Editor(content, "", $("#editor").get(0), {}); + }); + + afterEach(function () { + $("#editor").remove(); + myEditor = null; + }); + + it("should not have focus until explicitly set", function () { + expect(myEditor.hasFocus()).toBe(false); + }); + + it("should be able to detect when it has focus", function () { + myEditor.focus(); + expect(myEditor.hasFocus()).toBe(true); + }); + }); }); });