Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #445 from adobe/glenn/save-focused-editor
Browse files Browse the repository at this point in the history
Make File > Save work on the currently focused editor
  • Loading branch information
peterflynn committed Mar 13, 2012
2 parents 9c023e4 + f201ad6 commit 57b04c6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
32 changes: 30 additions & 2 deletions src/EditorManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 };
}


Expand Down Expand Up @@ -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 && 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);
Expand All @@ -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;
});
9 changes: 8 additions & 1 deletion src/FileCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,14 @@ define(function (require, exports, module) {
doc = commandData.doc;
}
if (!doc) {
doc = DocumentManager.getCurrentDocument();
var focusedEditor = EditorManager.getFocusedEditor();

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);
Expand Down
24 changes: 24 additions & 0 deletions test/spec/Editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div id='editor'/>");
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);
});
});
});
});

0 comments on commit 57b04c6

Please sign in to comment.