From 1acb3e4c5cfaa43183a5f5af16b51415da32cace Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 12:53:30 +0200 Subject: [PATCH 01/12] Added an option: Automatically update TOC when I open a `.md` file --- lib/markdown-toc.coffee | 34 +++++++++++++++++++++++----------- menus/markdown-toc.cson | 7 +++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index aa30d5d..dbf5643 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,20 +1,32 @@ Toc = require './Toc' -module.exports = +atom.workspace.observeTextEditors (editor) -> + + editor.onDidStopChanging (item) -> + if atom.config.get('markdown-toc.automatically-update') + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.autosave() +module.exports = + config: + 'automatically-update': + title: 'Automatically update TOC', + type: 'boolean' + default: false activate: (state) -> + console.log 'activate' 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/menus/markdown-toc.cson b/menus/markdown-toc.cson index 6e71d95..edc400b 100644 --- a/menus/markdown-toc.cson +++ b/menus/markdown-toc.cson @@ -28,6 +28,13 @@ 'label': 'Remove TOC' 'command': 'markdown-toc:delete' } + { + type: 'separator' + }, + { + 'label': 'Automatically update TOC' + 'command': 'markdown-toc:automatically-update' + } ] ] } From 05616c8f84fa969647708770eb4e99a2c00b2963 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 13:20:45 +0200 Subject: [PATCH 02/12] Cosmetics --- lib/markdown-toc.coffee | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index dbf5643..dca9816 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,7 +1,6 @@ Toc = require './Toc' atom.workspace.observeTextEditors (editor) -> - editor.onDidStopChanging (item) -> if atom.config.get('markdown-toc.automatically-update') @toc = new Toc(atom.workspace.getActivePaneItem()) @@ -14,7 +13,6 @@ module.exports = type: 'boolean' default: false activate: (state) -> - console.log 'activate' atom.commands.add 'atom-workspace', 'markdown-toc:create': => @toc = new Toc(atom.workspace.getActivePaneItem()) @toc.create() From 09f45526ac46d5ba80859f94664003000efed2c9 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 13:21:47 +0200 Subject: [PATCH 03/12] Not need --- menus/markdown-toc.cson | 7 ------- 1 file changed, 7 deletions(-) diff --git a/menus/markdown-toc.cson b/menus/markdown-toc.cson index edc400b..6e71d95 100644 --- a/menus/markdown-toc.cson +++ b/menus/markdown-toc.cson @@ -28,13 +28,6 @@ 'label': 'Remove TOC' 'command': 'markdown-toc:delete' } - { - type: 'separator' - }, - { - 'label': 'Automatically update TOC' - 'command': 'markdown-toc:automatically-update' - } ] ] } From 418e682503b2149ad97ef268e0a6fc4ee219ffb5 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 20:25:54 +0200 Subject: [PATCH 04/12] Timeout 1 second --- lib/markdown-toc.coffee | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index dca9816..8a282fa 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,10 +1,14 @@ Toc = require './Toc' atom.workspace.observeTextEditors (editor) -> + @timeout = null editor.onDidStopChanging (item) -> if atom.config.get('markdown-toc.automatically-update') - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.autosave() + clearTimeout @timeout + @timeout = setTimeout -> + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.autosave() + , 1000 module.exports = config: From 1969f9895db51efddd97737fe9ff20617e328048 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Tue, 6 Sep 2016 07:36:25 +0200 Subject: [PATCH 05/12] Update toc after save and after open document --- lib/markdown-toc.coffee | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index 8a282fa..09e3092 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,14 +1,14 @@ Toc = require './Toc' atom.workspace.observeTextEditors (editor) -> - @timeout = null - editor.onDidStopChanging (item) -> + if atom.config.get('markdown-toc.automatically-update') + @toc = new Toc(editor) + @toc.autosave() + + editor.onDidSave () -> if atom.config.get('markdown-toc.automatically-update') - clearTimeout @timeout - @timeout = setTimeout -> - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.autosave() - , 1000 + @toc = new Toc(editor) + @toc.autosave() module.exports = config: From d97df730f5b2da4b5a0e03e69dab7b3cc10ab46e Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 12:53:30 +0200 Subject: [PATCH 06/12] Added an option: Automatically update TOC when I open a `.md` file --- lib/markdown-toc.coffee | 34 +++++++++++++++++++++++----------- menus/markdown-toc.cson | 7 +++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index aa30d5d..dbf5643 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,20 +1,32 @@ Toc = require './Toc' -module.exports = +atom.workspace.observeTextEditors (editor) -> + + editor.onDidStopChanging (item) -> + if atom.config.get('markdown-toc.automatically-update') + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.autosave() +module.exports = + config: + 'automatically-update': + title: 'Automatically update TOC', + type: 'boolean' + default: false activate: (state) -> + console.log 'activate' 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/menus/markdown-toc.cson b/menus/markdown-toc.cson index 6e71d95..edc400b 100644 --- a/menus/markdown-toc.cson +++ b/menus/markdown-toc.cson @@ -28,6 +28,13 @@ 'label': 'Remove TOC' 'command': 'markdown-toc:delete' } + { + type: 'separator' + }, + { + 'label': 'Automatically update TOC' + 'command': 'markdown-toc:automatically-update' + } ] ] } From 35caf935b4f483ae5dd4d4196a8962ac9e1981a4 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 13:20:45 +0200 Subject: [PATCH 07/12] Cosmetics --- lib/markdown-toc.coffee | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index dbf5643..dca9816 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,7 +1,6 @@ Toc = require './Toc' atom.workspace.observeTextEditors (editor) -> - editor.onDidStopChanging (item) -> if atom.config.get('markdown-toc.automatically-update') @toc = new Toc(atom.workspace.getActivePaneItem()) @@ -14,7 +13,6 @@ module.exports = type: 'boolean' default: false activate: (state) -> - console.log 'activate' atom.commands.add 'atom-workspace', 'markdown-toc:create': => @toc = new Toc(atom.workspace.getActivePaneItem()) @toc.create() From 118a804a10aba3923f33dfdab4044f8c45492a96 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 13:21:47 +0200 Subject: [PATCH 08/12] Not need --- menus/markdown-toc.cson | 7 ------- 1 file changed, 7 deletions(-) diff --git a/menus/markdown-toc.cson b/menus/markdown-toc.cson index edc400b..6e71d95 100644 --- a/menus/markdown-toc.cson +++ b/menus/markdown-toc.cson @@ -28,13 +28,6 @@ 'label': 'Remove TOC' 'command': 'markdown-toc:delete' } - { - type: 'separator' - }, - { - 'label': 'Automatically update TOC' - 'command': 'markdown-toc:automatically-update' - } ] ] } From 7dfbb09f35e765aece74124e32bb5ae4a9bc80f5 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sun, 4 Sep 2016 20:25:54 +0200 Subject: [PATCH 09/12] Timeout 1 second --- lib/markdown-toc.coffee | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/markdown-toc.coffee b/lib/markdown-toc.coffee index dca9816..8a282fa 100644 --- a/lib/markdown-toc.coffee +++ b/lib/markdown-toc.coffee @@ -1,10 +1,14 @@ Toc = require './Toc' atom.workspace.observeTextEditors (editor) -> + @timeout = null editor.onDidStopChanging (item) -> if atom.config.get('markdown-toc.automatically-update') - @toc = new Toc(atom.workspace.getActivePaneItem()) - @toc.autosave() + clearTimeout @timeout + @timeout = setTimeout -> + @toc = new Toc(atom.workspace.getActivePaneItem()) + @toc.autosave() + , 1000 module.exports = config: From 91fab28debd99e32e99a385bec848d836bea8ed4 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Fri, 9 Sep 2016 23:18:57 +0200 Subject: [PATCH 10/12] It called too often --- lib/Toc.coffee | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/Toc.coffee b/lib/Toc.coffee index 01e32f6..a26d4d6 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 @.options.updateOnSave is 1 && @_hasToc() @_deleteToc() @editor.setTextInBufferRange [[@open,0], [@open,0]], @_createToc() From a762318b578c0fdece7799cf296af99c100a490b Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Fri, 9 Sep 2016 23:40:11 +0200 Subject: [PATCH 11/12] Fix --- lib/Toc.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Toc.coffee b/lib/Toc.coffee index a26d4d6..eb7efba 100644 --- a/lib/Toc.coffee +++ b/lib/Toc.coffee @@ -38,7 +38,7 @@ class Toc autosave: -> - if @.options.updateOnSave is 1 && @_hasToc() + if @_hasToc() && @.options.updateOnSave is 1 @_deleteToc() @editor.setTextInBufferRange [[@open,0], [@open,0]], @_createToc() From 32d166d6c42853607287b04c7f97eca225e046d8 Mon Sep 17 00:00:00 2001 From: Ilya Zelenin Date: Sat, 10 Sep 2016 09:34:49 +0200 Subject: [PATCH 12/12] Plugin initialized when the editor is started --- package.json | 8 -------- 1 file changed, 8 deletions(-) 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"