From a9c6ca3da7216cc48500c71d3b087dc19a945c90 Mon Sep 17 00:00:00 2001 From: Wliu <50Wliu@users.noreply.github.com> Date: Sat, 28 Oct 2017 12:13:05 +0200 Subject: [PATCH 1/4] Add options to automatically open and close previews --- lib/main.coffee | 47 ++++++++++++++++++++++++++++++++++++++++++++--- package.json | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/lib/main.coffee b/lib/main.coffee index 875a93e..343be66 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -9,6 +9,10 @@ isMarkdownPreviewView = (object) -> object instanceof MarkdownPreviewView module.exports = + destroyedItems: new Set() + lastActiveEditor: null + lastRemovedPreview: null + activate: -> @disposables = new CompositeDisposable() @commandSubscriptions = new CompositeDisposable() @@ -59,6 +63,42 @@ module.exports = else @createMarkdownPreviewView(filePath: path) + @disposables.add atom.workspace.getCenter().onDidStopChangingActivePaneItem (item) => + editor = atom.workspace.getActiveTextEditor() + return if @lastActiveEditor is editor + + if atom.config.get('markdown-preview.automaticallyClosePreview') and @lastActiveEditor + unless isMarkdownPreviewView(item) and item.editorId is "#{@lastActiveEditor.id}" + @removePreviewForEditor(@lastActiveEditor) + + @lastActiveEditor = editor + + if atom.config.get('markdown-preview.automaticallyOpenPreview') + return unless editor? + + uri = @uriForEditor(editor) + return if atom.workspace.paneForURI(uri)? + + openPreview = true + @destroyedItems.forEach (destroyedItem) -> + return unless isMarkdownPreviewView(destroyedItem) and openPreview + openPreview = false if destroyedItem.editorId is String(editor.id) + + @destroyedItems.clear() + return unless openPreview + + grammars = atom.config.get('markdown-preview.grammars') ? [] + return unless editor.getGrammar().scopeName in grammars + + @addPreviewForEditor(editor, atom.workspace.paneForItem(item)) + + @disposables.add atom.workspace.getCenter().onDidDestroyPaneItem ({item}) => + @destroyedItems.add(item) unless item is @lastRemovedPreview + + if atom.config.get('markdown-preview.automaticallyClosePreview') + if atom.workspace.isTextEditor(item) + @removePreviewForEditor(item) + deactivate: -> @disposables.dispose() @commandSubscriptions.dispose() @@ -88,14 +128,15 @@ module.exports = uri = @uriForEditor(editor) previewPane = atom.workspace.paneForURI(uri) if previewPane? - previewPane.destroyItem(previewPane.itemForURI(uri)) + @lastRemovedPreview = previewPane.itemForURI(uri) + previewPane.destroyItem(@lastRemovedPreview) true else false - addPreviewForEditor: (editor) -> + addPreviewForEditor: (editor, previousActivePane) -> uri = @uriForEditor(editor) - previousActivePane = atom.workspace.getActivePane() + previousActivePane ?= atom.workspace.getActivePane() options = searchAllPanes: true if atom.config.get('markdown-preview.openPreviewInSplitPane') diff --git a/package.json b/package.json index 6bd5878..b30d6e5 100644 --- a/package.json +++ b/package.json @@ -23,28 +23,52 @@ "MarkdownPreviewView": "createMarkdownPreviewView" }, "configSchema": { - "breakOnSingleNewline": { - "type": "boolean", - "default": false, - "description": "In Markdown, a single newline character doesn't cause a line break in the generated HTML. In GitHub Flavored Markdown, that is not true. Enable this config option to insert line breaks in rendered HTML for single newlines in Markdown source." - }, "liveUpdate": { "type": "boolean", + "order": 0, "default": true, "description": "Re-render the preview as the contents of the source changes, without requiring the source buffer to be saved. If disabled, the preview is re-rendered only when the buffer is saved to disk." }, + "useGitHubStyle": { + "title": "Use GitHub.com style", + "type": "boolean", + "order": 1, + "default": false, + "description": "Use the same CSS styles for preview as the ones used on GitHub.com." + }, "openPreviewInSplitPane": { "type": "boolean", + "order": 2, "default": true, "description": "Open the preview in a split pane. If disabled, the preview is opened in a new tab in the same pane." }, + "automaticallyOpenPreview": { + "type": "boolean", + "order": 3, + "default": false, + "description": "Automatically open the preview when a Markdown file is focused." + }, + "automaticallyClosePreview": { + "type": "boolean", + "order": 4, + "default": false, + "description": "Automatically close the preview when its corresponding file is no longer focused." + }, + "breakOnSingleNewline": { + "type": "boolean", + "order": 5, + "default": false, + "description": "In Markdown, a single newline character doesn't cause a line break in the generated HTML. In GitHub Flavored Markdown, that is not true. Enable this config option to insert line breaks in rendered HTML for single newlines in Markdown source." + }, "allowUnsafeProtocols": { "type": "boolean", + "order": 6, "default": false, "description": "Allow HTML attributes to use protocols normally considered unsafe such as `file://` and absolute paths on Windows." }, "grammars": { "type": "array", + "order": 7, "default": [ "source.gfm", "source.litcoffee", @@ -54,12 +78,6 @@ "text.plain.null-grammar" ], "description": "List of scopes for languages for which previewing is enabled. See [this README](https://github.com/atom/spell-check#spell-check-package-) for more information on finding the correct scope for a specific language." - }, - "useGitHubStyle": { - "title": "Use GitHub.com style", - "type": "boolean", - "default": false, - "description": "Use the same CSS styles for preview as the ones used on GitHub.com." } } } From aa8cd01425d5f494d5f7f171491c0d819eaa1915 Mon Sep 17 00:00:00 2001 From: Wliu <50Wliu@users.noreply.github.com> Date: Sat, 28 Oct 2017 15:05:23 +0200 Subject: [PATCH 2/4] :memo: --- lib/main.coffee | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/main.coffee b/lib/main.coffee index 343be66..03c10e7 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -68,21 +68,21 @@ module.exports = return if @lastActiveEditor is editor if atom.config.get('markdown-preview.automaticallyClosePreview') and @lastActiveEditor - unless isMarkdownPreviewView(item) and item.editorId is "#{@lastActiveEditor.id}" + # Do not close preview if we change focus from the editor to the preview itself + unless isMarkdownPreviewView(item) and item.editorId is @lastActiveEditor.id.toString() @removePreviewForEditor(@lastActiveEditor) @lastActiveEditor = editor - if atom.config.get('markdown-preview.automaticallyOpenPreview') - return unless editor? - + if editor? and atom.config.get('markdown-preview.automaticallyOpenPreview') uri = @uriForEditor(editor) - return if atom.workspace.paneForURI(uri)? + return if atom.workspace.paneForURI(uri)? # Preview already exists openPreview = true @destroyedItems.forEach (destroyedItem) -> return unless isMarkdownPreviewView(destroyedItem) and openPreview - openPreview = false if destroyedItem.editorId is String(editor.id) + # Do not open preview for a text editor whose preview we just closed + openPreview = false if destroyedItem.editorId is editor.id.toString() @destroyedItems.clear() return unless openPreview @@ -94,10 +94,8 @@ module.exports = @disposables.add atom.workspace.getCenter().onDidDestroyPaneItem ({item}) => @destroyedItems.add(item) unless item is @lastRemovedPreview - if atom.config.get('markdown-preview.automaticallyClosePreview') - if atom.workspace.isTextEditor(item) - @removePreviewForEditor(item) + @removePreviewForEditor(item) if atom.workspace.isTextEditor(item) deactivate: -> @disposables.dispose() From 9eaa4a95e97a64e472616a751f11c77ccfcf2948 Mon Sep 17 00:00:00 2001 From: Wliu <50Wliu@users.noreply.github.com> Date: Sat, 28 Oct 2017 15:10:04 +0200 Subject: [PATCH 3/4] :art: --- lib/main.coffee | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/main.coffee b/lib/main.coffee index 03c10e7..29367d1 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -9,13 +9,10 @@ isMarkdownPreviewView = (object) -> object instanceof MarkdownPreviewView module.exports = - destroyedItems: new Set() - lastActiveEditor: null - lastRemovedPreview: null - activate: -> @disposables = new CompositeDisposable() @commandSubscriptions = new CompositeDisposable() + @destroyedItems = new Set() @disposables.add atom.config.observe 'markdown-preview.grammars', (grammars) => @commandSubscriptions.dispose() From 19331b94b554d8dcb9893f2f7d2b9f69dfa80b6e Mon Sep 17 00:00:00 2001 From: Wliu <50Wliu@users.noreply.github.com> Date: Sat, 28 Oct 2017 17:08:29 +0200 Subject: [PATCH 4/4] Add a new setting for list of grammars to automatically preview --- lib/main.coffee | 2 +- package.json | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/main.coffee b/lib/main.coffee index 29367d1..aa8ab5f 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -84,7 +84,7 @@ module.exports = @destroyedItems.clear() return unless openPreview - grammars = atom.config.get('markdown-preview.grammars') ? [] + grammars = atom.config.get('markdown-preview.automaticPreviewGrammars') ? [] return unless editor.getGrammar().scopeName in grammars @addPreviewForEditor(editor, atom.workspace.paneForItem(item)) diff --git a/package.json b/package.json index b30d6e5..6d600cb 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,15 @@ "text.plain.null-grammar" ], "description": "List of scopes for languages for which previewing is enabled. See [this README](https://github.com/atom/spell-check#spell-check-package-) for more information on finding the correct scope for a specific language." + }, + "automaticPreviewGrammars": { + "type": "array", + "order": 8, + "default": [ + "source.gfm", + "source.litcoffee" + ], + "description": "Only automatically open previews for the given list of scopes." } } }