From dcc1193b498e80196ac2f38fb667d7f5eee82b38 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 26 Jul 2012 11:26:38 -0600 Subject: [PATCH] Preserve focus state of command panel's mini-editor on refresh --- spec/extensions/command-panel-spec.coffee | 23 +++++++++++++++---- .../command-panel/command-panel.coffee | 9 +++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/spec/extensions/command-panel-spec.coffee b/spec/extensions/command-panel-spec.coffee index e11359bef..76c10e262 100644 --- a/spec/extensions/command-panel-spec.coffee +++ b/spec/extensions/command-panel-spec.coffee @@ -18,16 +18,29 @@ describe "CommandPanel", -> rootView.deactivate() describe "serialization", -> - it "preserves the command panel's mini editor text and visibility across reloads", -> + it "preserves the command panel's mini-editor text, visibility, and focus across reloads", -> + rootView.attachToDom() rootView.trigger 'command-panel:toggle' + expect(commandPanel.miniEditor.isFocused).toBeTruthy() commandPanel.miniEditor.insertText 'abc' - newRootView = RootView.deserialize(rootView.serialize()) + rootView2 = RootView.deserialize(rootView.serialize()) + rootView.deactivate() + rootView2.attachToDom() - commandPanel = newRootView.activateExtension(CommandPanel) - expect(newRootView.find('.command-panel')).toExist() + commandPanel = rootView2.activateExtension(CommandPanel) + expect(rootView2.find('.command-panel')).toExist() expect(commandPanel.miniEditor.getText()).toBe 'abc' + expect(commandPanel.miniEditor.isFocused).toBeTruthy() - newRootView.remove() + rootView2.focus() + expect(commandPanel.miniEditor.isFocused).toBeFalsy() + rootView3 = RootView.deserialize(rootView2.serialize()) + rootView2.deactivate() + rootView3.attachToDom() + commandPanel = rootView3.activateExtension(CommandPanel) + + expect(commandPanel.miniEditor.isFocused).toBeFalsy() + rootView3.deactivate() describe "when command-panel:close is triggered on the command panel", -> it "detaches the command panel", -> diff --git a/src/extensions/command-panel/command-panel.coffee b/src/extensions/command-panel/command-panel.coffee index bf00695d0..50e817c2e 100644 --- a/src/extensions/command-panel/command-panel.coffee +++ b/src/extensions/command-panel/command-panel.coffee @@ -23,10 +23,12 @@ class CommandPanel extends View @serialize: -> text: @instance.miniEditor.getText() visible: @instance.hasParent() + miniEditorFocused: @instance.miniEditor.isFocused @deserialize: (state, rootView) -> commandPanel = new CommandPanel(rootView) - commandPanel.attach(state.text) if state.visible + commandPanel.attach(state.text, focus: false) if state.visible + commandPanel.miniEditor.focus() if state.miniEditorFocused commandPanel @content: (rootView) -> @@ -85,9 +87,10 @@ class CommandPanel extends View else @miniEditor.focus() - attach: (text='') -> + attach: (text='', options={}) -> + focus = options.focus ? true @rootView.vertical.append(this) - @miniEditor.focus() + @miniEditor.focus() if focus @miniEditor.setText(text) @miniEditor.setCursorBufferPosition([0, Infinity])