diff --git a/CHANGELOG.md b/CHANGELOG.md index c3d858c..cadc691 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## 0.3.2 - Third Release, multiTabs partial support +* Added partial support for multiple panes (Saves the files opened, not the splitted panes) +* Avoid saving and filling the localStorage with empty elements for branches without tabs opened. +* TODO: activeTab is not properly loaded, code cleanup, multi-pane support, tests. + +## 0.3.1 - Second Release, localStorage +* Added localStorage to save the tabs +* TODO: activeTab is not properly loaded, code cleanup. + ## 0.1.0 - First Release * Every feature added * Every bug fixed diff --git a/lib/git-tabs.coffee b/lib/git-tabs.coffee index aabf8fa..c0443f9 100644 --- a/lib/git-tabs.coffee +++ b/lib/git-tabs.coffee @@ -31,21 +31,24 @@ module.exports = @activeBranch = @git.getCurrentBranch() # Set up storageFolder - @storageFolder = new StorageFolder getStorageDir() - if not fs.existsSync(@storageFolder.getPath() + "/#{@projectName}.json") - @storageFolder.store(@projectName, {}) + @storageFolder = new StorageFolder + @branchTabsName = [@projectName, @activeBranch].join('-') # Set up local 'cache' for tabs @tabs = {} # Set up the active pane shortcut @activePane = atom.workspace.paneContainer.activePane + @activeTabIndex = 0 # Load tabs if the user has already stored them in a previous session - if @storageFolder.exists(@activeBranch) + @storedBranchTabs = @storageFolder.load(@activeBranch) + if @storedBranchTabsName @clearTabs() @loadTabs(@activeBranch) + @setActiveTab() + # Save the current tabs to cache @saveTabs() @@ -69,6 +72,7 @@ module.exports = subscribeToRepositories: -> @git.onDidChangeBranch (data) => @handleBranchChange(data) + @setActiveTab() handleNewTab: (event) -> @saveTab(event.item, event.index) @@ -91,33 +95,33 @@ module.exports = # Save all tabs to local 'cache' saveTabs: -> - for tab, i in @activePane.items + @tabs[@activeBranch] = {} + tabs = atom.workspace.paneContainer.getPaneItems() + for tab, i in tabs @saveTab(tab, i) # Save a tab to the local 'cache' saveTab: (tab, index) -> isActive = atom.workspace.getActivePaneItem() == tab - if not @tabs[@activeBranch] - @tabs[@activeBranch] = {} # the item path is the most unique way to hash it. @tabs[@activeBranch][getItemPath tab] = 'index': index 'active': isActive - 'tab': tab.serialize() # Store tabs as JSON storeTabs: -> @saveTabs() - @storageFolder.store(@projectName, @tabs) + @storageFolder.store(@activeBranch, @tabs[@activeBranch]) # Load tabs from JSON loadTabs: (branch) -> - @tabs = @storageFolder.load(@projectName) + @tabs[branch] = @storageFolder.load(branch) + workspace = atom.workspace + for id, item of @tabs[branch] - deserializedTab = atom.deserializers.deserialize item.tab - @activePane.addItem deserializedTab, item.index + workspace.open(id) if item.active - @activePane.setActiveItem(deserializedTab) + @activeTabIndex = item.index # Remove a tab from the local 'cache' unsaveTab: (tab) -> @@ -128,3 +132,9 @@ module.exports = if item.index < tab.index @tabs[branch][id][index]-- delete @tabs[branch]?[tab.id] + + # Set the correct active tab. + # Still not working because Atom has little to none management of this available (or haven't found it yet) + setActiveTab: -> + # activeTab = atom.workspace.paneContainer.activePane.itemAtIndex(@activeTabIndex) + # @activePane.setActiveItem(activeTab) diff --git a/lib/storage-folder.coffee b/lib/storage-folder.coffee index 936594f..814dd68 100644 --- a/lib/storage-folder.coffee +++ b/lib/storage-folder.coffee @@ -5,28 +5,32 @@ fs = require "fs-plus" module.exports = class StorageFolder - constructor: (containingPath) -> - @path = path.join(containingPath, "storage") store: (name, object) -> - fs.writeFileSync(@pathForKey(name + '.json'), JSON.stringify(object), 'utf8') + @projectName = path.basename(atom.project.getPaths()?[0]) + branchedName = [@projectName, name].join('-') + + if object + delete object["undefined"] + stringifiedObject = JSON.stringify(object) + if stringifiedObject == "{}" + localStorage.removeItem(branchedName) + else + localStorage.setItem(branchedName, stringifiedObject) load: (name) -> - statePath = @pathForKey(name + '.json') + @projectName = path.basename(atom.project.getPaths()?[0]) + branchedName = [@projectName, name].join('-') + retrievedItem = null + try - stateString = fs.readFileSync(statePath, 'utf8') + retrievedItem = localStorage.getItem(branchedName) catch error unless error.code is 'ENOENT' - console.warn "Error reading state file: #{statePath}", error.stack, error + console.warn "Local storage could not find an element for: #{branchedName}", error.stack, error return undefined try - JSON.parse(stateString) + return JSON.parse(retrievedItem) catch error - console.warn "Error parsing state file: #{statePath}", error.stack, error - - exists: (name) -> - return fs.exists(@pathForKey(name + '.json')) - - pathForKey: (name) -> path.join(@getPath(), name) - getPath: -> @path + console.warn "Error parsing retrieved item: #{retrievedItem}", error.stack, error diff --git a/package.json b/package.json index ef48f3c..256e800 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "git-tabs", "main": "./lib/git-tabs", - "version": "0.3.0", + "version": "0.3.2", "description": "Preserve your tabs on a per-branch and per-repo basis", "keywords": [], "activationCommands": {},