diff --git a/lib/Toc.coffee b/lib/Toc.coffee index db89079..040b2f0 100644 --- a/lib/Toc.coffee +++ b/lib/Toc.coffee @@ -12,13 +12,6 @@ class Toc updateOnSave: 1 # updateOnSave orderedList: 0 # orderedList - at = @ - @editor.getBuffer().onWillSave () -> - if at.options.updateOnSave is 1 - if at._hasToc() - at._deleteToc() - at.editor.setTextInBufferRange [[at.open,0], [at.open,0]], at._createToc() - # ---------------------------------------------------------------------------- # main methods (highest logic level) @@ -45,7 +38,7 @@ class Toc autosave: -> - if @_hasToc() + if @_hasToc() && @.options.updateOnSave is 1 @_deleteToc() @editor.setTextInBufferRange [[@open,0], [@open,0]], @_createToc() diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index aa30d5d..09e3092 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,20 +1,34 @@ Toc = require './Toc' -module.exports = +atom.workspace.observeTextEditors (editor) -> + if atom.config.get('markdown-toc.automatically-update') + @toc = new Toc(editor) + @toc.autosave() + + editor.onDidSave () -> + if atom.config.get('markdown-toc.automatically-update') + @toc = new Toc(editor) + @toc.autosave() +module.exports = + config: + 'automatically-update': + title: 'Automatically update TOC', + type: 'boolean' + default: false activate: (state) -> atom.commands.add 'atom-workspace', 'markdown-toc:create': => - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.create() + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.create() atom.commands.add 'atom-workspace', 'markdown-toc:update': => - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.update() + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.update() atom.commands.add 'atom-workspace', 'markdown-toc:delete': => - @toc = new Toc(atom.workspace.getActivePaneItem()); - @toc.delete() + @toc = new Toc(atom.workspace.getActivePaneItem()); + @toc.delete() atom.commands.add 'atom-workspace', 'markdown-toc:toggle': => - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.toggle() + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.toggle() - # deactivate: -> - # @toc.destroy() +# deactivate: -> +# @toc.destroy() diff --git a/package.json b/package.json index 0ade8d1..266dcf3 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,6 @@ "main": "./lib/markdown-toc", "version": "0.4.1", "description": "Generate TOC (table of contents) of headlines from parsed markdown file", - "activationCommands": { - "atom-workspace": [ - "markdown-toc:toggle", - "markdown-toc:create", - "markdown-toc:update", - "markdown-toc:delete" - ] - }, "repository": { "type": "git", "url": "https://github.com/nok/markdown-toc"