Serialize command panel history

Retains up to 100 commands between reloads
This commit is contained in:
Kevin Sawicki
2012-09-20 13:34:03 -07:00
parent c42c824f0a
commit ea77b60b15
2 changed files with 44 additions and 4 deletions

View File

@@ -13,15 +13,23 @@ describe "CommandPanel", ->
editor = rootView.getActiveEditor()
buffer = editor.activeEditSession.buffer
commandPanel = requireExtension('command-panel')
commandPanel.history = []
commandPanel.historyIndex = 0
afterEach ->
rootView.deactivate()
describe "serialization", ->
it "preserves the command panel's mini-editor text, visibility, and focus across reloads", ->
it "preserves the command panel's mini-editor text, visibility, focus, and history across reloads", ->
rootView.attachToDom()
rootView.trigger 'command-panel:toggle'
expect(commandPanel.miniEditor.isFocused).toBeTruthy()
commandPanel.execute('/test')
expect(commandPanel.history.length).toBe(1)
expect(commandPanel.history[0]).toBe('/test')
expect(commandPanel.historyIndex).toBe(1)
rootView.trigger 'command-panel:toggle'
expect(commandPanel.miniEditor.isFocused).toBeTruthy()
commandPanel.miniEditor.insertText 'abc'
rootView2 = RootView.deserialize(rootView.serialize())
rootView.deactivate()
@@ -31,6 +39,9 @@ describe "CommandPanel", ->
expect(rootView2.find('.command-panel')).toExist()
expect(commandPanel.miniEditor.getText()).toBe 'abc'
expect(commandPanel.miniEditor.isFocused).toBeTruthy()
expect(commandPanel.history.length).toBe(1)
expect(commandPanel.history[0]).toBe('/test')
expect(commandPanel.historyIndex).toBe(1)
rootView2.focus()
expect(commandPanel.miniEditor.isFocused).toBeFalsy()
@@ -42,6 +53,31 @@ describe "CommandPanel", ->
expect(commandPanel.miniEditor.isFocused).toBeFalsy()
rootView3.deactivate()
it "only retains the configured max serialized history size", ->
rootView.attachToDom()
commandPanel.maxSerializedHistorySize = 2
commandPanel.execute('/test1')
commandPanel.execute('/test2')
commandPanel.execute('/test3')
expect(commandPanel.history.length).toBe(3)
expect(commandPanel.history[0]).toBe('/test1')
expect(commandPanel.history[1]).toBe('/test2')
expect(commandPanel.history[2]).toBe('/test3')
expect(commandPanel.historyIndex).toBe(3)
rootView2 = RootView.deserialize(rootView.serialize())
rootView.deactivate()
rootView2.attachToDom()
commandPanel = rootView2.activateExtension(CommandPanel)
expect(commandPanel.history.length).toBe(2)
expect(commandPanel.history[0]).toBe('/test2')
expect(commandPanel.history[1]).toBe('/test3')
expect(commandPanel.historyIndex).toBe(2)
rootView2.deactivate()
describe "when command-panel:close is triggered on the command panel", ->
it "detaches the command panel", ->
commandPanel.attach()

View File

@@ -24,9 +24,10 @@ class CommandPanel extends View
text: @instance.miniEditor.getText()
visible: @instance.hasParent()
miniEditorFocused: @instance.miniEditor.isFocused
history: @instance.history[-@instance.maxSerializedHistorySize..]
@deserialize: (state, rootView) ->
commandPanel = new CommandPanel(rootView)
commandPanel = new CommandPanel(rootView, state.history)
commandPanel.attach(state.text, focus: false) if state.visible
commandPanel.miniEditor.focus() if state.miniEditorFocused
commandPanel
@@ -41,10 +42,13 @@ class CommandPanel extends View
commandInterpreter: null
history: null
historyIndex: 0
maxSerializedHistorySize: 100
initialize: (@rootView)->
initialize: (@rootView, @history) ->
@commandInterpreter = new CommandInterpreter(@rootView.project)
@history = []
@history ?= []
@historyIndex = @history.length
@on 'command-panel:unfocus', => @rootView.focus()
@on 'command-panel:close', => @detach()