Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

[WIP] Add options to automatically open and close previews #516

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports =
activate: ->
@disposables = new CompositeDisposable()
@commandSubscriptions = new CompositeDisposable()
@destroyedItems = new Set()

@disposables.add atom.config.observe 'markdown-preview.grammars', (grammars) =>
@commandSubscriptions.dispose()
Expand Down Expand Up @@ -59,6 +60,40 @@ 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
# 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 editor? and atom.config.get('markdown-preview.automaticallyOpenPreview')
uri = @uriForEditor(editor)
return if atom.workspace.paneForURI(uri)? # Preview already exists

openPreview = true
@destroyedItems.forEach (destroyedItem) ->
return unless isMarkdownPreviewView(destroyedItem) and openPreview
# 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

grammars = atom.config.get('markdown-preview.automaticPreviewGrammars') ? []
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')
@removePreviewForEditor(item) if atom.workspace.isTextEditor(item)

deactivate: ->
@disposables.dispose()
@commandSubscriptions.dispose()
Expand Down Expand Up @@ -88,14 +123,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')
Expand Down
47 changes: 37 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -55,11 +79,14 @@
],
"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."
"automaticPreviewGrammars": {
"type": "array",
"order": 8,
"default": [
"source.gfm",
"source.litcoffee"
],
"description": "Only automatically open previews for the given list of scopes."
}
}
}