From 32a23d8cabd42f9bb50f435a8fe55b987d9e685f Mon Sep 17 00:00:00 2001 From: Jared Reisinger Date: Thu, 11 Feb 2016 12:59:57 -0800 Subject: [PATCH 1/6] Add configurable pane direction. Addresses #99 and part of #328. Does _not_ address any kind of "opposite" setting. Migrates existing true/false (pane to the right or not) config settings into "right"/"none" equivalent value. Note: In order to migrate an exiting setting, we have to run code very early when the module loads, _before_ it activates. This is unfortunate from a performance standpoint. If we're willing to simply lose any previous configuration setting (falling back to the default, "right"), we can avoid the perf impact of doing work during module load. I'm completely open to the wisdom of the Atom community here; I just wanted to start with an already-backward-compatible option. --- README.md | 6 ++++-- lib/main.coffee | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9c83b03..2ac0fba 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # Markdown Preview package [![Build Status](https://travis-ci.org/atom/markdown-preview.svg?branch=master)](https://travis-ci.org/atom/markdown-preview) -Show the rendered HTML markdown to the right of the current editor using -`ctrl-shift-m`. +Show the rendered HTML markdown next to the the current editor using +`ctrl-shift-m`. You can choose in which direction to show the preview (`right` +and `down` being the most common), or even to show the preview in a new tab in +the current pane. It can be activated from the editor using the `ctrl-shift-m` key-binding and is currently enabled for `.markdown`, `.md`, `.mdown`, `.mkd`, `.mkdown`, `.ron`, and `.txt` files. diff --git a/lib/main.coffee b/lib/main.coffee index 8719d78..aeee3ac 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -4,6 +4,19 @@ fs = require 'fs-plus' MarkdownPreviewView = null # Defer until used renderer = null # Defer until used +# Migrate any old openPreviewInSplitPane setting from a boolean to the new +# string value. The migration is 'false' --> 'none', and 'true' --> 'right'. +# It would be nice to wait and do this on-demand, but config schema validation +# runs even before the pacakge is activated, so we *have* to do this at module +# load time if we want to accurately migrate the setting. +atom.config.transact -> + splitKey = 'markdown-preview.openPreviewInSplitPane' + origSplit = atom.config.getRawValue(splitKey) + if origSplit? and typeof(origSplit) is 'boolean' + newSplit = if origSplit then 'right' else 'none' + console.log 'migrating', splitKey, 'from', origSplit, 'to', newSplit + atom.config.set(splitKey, newSplit) + createMarkdownPreviewView = (state) -> MarkdownPreviewView ?= require './markdown-preview-view' new MarkdownPreviewView(state) @@ -23,9 +36,10 @@ module.exports = 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.' openPreviewInSplitPane: - type: 'boolean' - default: true - description: 'Open the preview in a split pane. If disabled, the preview is opened in a new tab in the same pane.' + type: 'string' + default: 'right' + enum: ['none', 'right', 'down', 'left', 'up'] + description: 'Where to open the preview, whether in a new tab in the same pane (`none`), or in a new pane in the specified direction.' grammars: type: 'array' default: [ @@ -116,8 +130,9 @@ module.exports = previousActivePane = atom.workspace.getActivePane() options = searchAllPanes: true - if atom.config.get('markdown-preview.openPreviewInSplitPane') - options.split = 'right' + splitPane = atom.config.get('markdown-preview.openPreviewInSplitPane') + if splitPane isnt 'none' + options.split = splitPane atom.workspace.open(uri, options).then (markdownPreviewView) -> if isMarkdownPreviewView(markdownPreviewView) previousActivePane.activate() From a275d9c106b84893efb698b047c0649698763c5a Mon Sep 17 00:00:00 2001 From: Jared Reisinger Date: Mon, 15 Feb 2016 22:26:45 -0800 Subject: [PATCH 2/6] Switch back to original flag for split pane, separate string for direction --- README.md | 2 +- lib/main.coffee | 26 ++++++++------------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 2ac0fba..6097e56 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Show the rendered HTML markdown next to the the current editor using `ctrl-shift-m`. You can choose in which direction to show the preview (`right` -and `down` being the most common), or even to show the preview in a new tab in +or `down`), or even to show the preview in a new tab in the current pane. It can be activated from the editor using the `ctrl-shift-m` key-binding and is diff --git a/lib/main.coffee b/lib/main.coffee index aeee3ac..d061386 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -4,19 +4,6 @@ fs = require 'fs-plus' MarkdownPreviewView = null # Defer until used renderer = null # Defer until used -# Migrate any old openPreviewInSplitPane setting from a boolean to the new -# string value. The migration is 'false' --> 'none', and 'true' --> 'right'. -# It would be nice to wait and do this on-demand, but config schema validation -# runs even before the pacakge is activated, so we *have* to do this at module -# load time if we want to accurately migrate the setting. -atom.config.transact -> - splitKey = 'markdown-preview.openPreviewInSplitPane' - origSplit = atom.config.getRawValue(splitKey) - if origSplit? and typeof(origSplit) is 'boolean' - newSplit = if origSplit then 'right' else 'none' - console.log 'migrating', splitKey, 'from', origSplit, 'to', newSplit - atom.config.set(splitKey, newSplit) - createMarkdownPreviewView = (state) -> MarkdownPreviewView ?= require './markdown-preview-view' new MarkdownPreviewView(state) @@ -36,10 +23,14 @@ module.exports = 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.' openPreviewInSplitPane: + type: 'boolean' + default: true + description: 'Open the preview in a split pane. If disabled, the preview is opened in a new tab in the same pane.' + splitPaneDirection: type: 'string' default: 'right' - enum: ['none', 'right', 'down', 'left', 'up'] - description: 'Where to open the preview, whether in a new tab in the same pane (`none`), or in a new pane in the specified direction.' + enum: ['right', 'down'] + description: 'Direction in which to open the split pane (when **Open Preview In Split Pane** is enabled).' grammars: type: 'array' default: [ @@ -130,9 +121,8 @@ module.exports = previousActivePane = atom.workspace.getActivePane() options = searchAllPanes: true - splitPane = atom.config.get('markdown-preview.openPreviewInSplitPane') - if splitPane isnt 'none' - options.split = splitPane + if atom.config.get('markdown-preview.openPreviewInSplitPane') + options.split = atom.config.get('markdown-preview.splitPaneDirection') atom.workspace.open(uri, options).then (markdownPreviewView) -> if isMarkdownPreviewView(markdownPreviewView) previousActivePane.activate() From 2a9aedf4b0e35ffc2c244bdb92a386eeec42b8b2 Mon Sep 17 00:00:00 2001 From: Jared Reisinger Date: Mon, 15 Feb 2016 22:29:27 -0800 Subject: [PATCH 3/6] Revert "Switch back to original flag for split pane, separate string for direction" This reverts commit a275d9c106b84893efb698b047c0649698763c5a. --- README.md | 2 +- lib/main.coffee | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6097e56..2ac0fba 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Show the rendered HTML markdown next to the the current editor using `ctrl-shift-m`. You can choose in which direction to show the preview (`right` -or `down`), or even to show the preview in a new tab in +and `down` being the most common), or even to show the preview in a new tab in the current pane. It can be activated from the editor using the `ctrl-shift-m` key-binding and is diff --git a/lib/main.coffee b/lib/main.coffee index d061386..aeee3ac 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -4,6 +4,19 @@ fs = require 'fs-plus' MarkdownPreviewView = null # Defer until used renderer = null # Defer until used +# Migrate any old openPreviewInSplitPane setting from a boolean to the new +# string value. The migration is 'false' --> 'none', and 'true' --> 'right'. +# It would be nice to wait and do this on-demand, but config schema validation +# runs even before the pacakge is activated, so we *have* to do this at module +# load time if we want to accurately migrate the setting. +atom.config.transact -> + splitKey = 'markdown-preview.openPreviewInSplitPane' + origSplit = atom.config.getRawValue(splitKey) + if origSplit? and typeof(origSplit) is 'boolean' + newSplit = if origSplit then 'right' else 'none' + console.log 'migrating', splitKey, 'from', origSplit, 'to', newSplit + atom.config.set(splitKey, newSplit) + createMarkdownPreviewView = (state) -> MarkdownPreviewView ?= require './markdown-preview-view' new MarkdownPreviewView(state) @@ -23,14 +36,10 @@ module.exports = 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.' openPreviewInSplitPane: - type: 'boolean' - default: true - description: 'Open the preview in a split pane. If disabled, the preview is opened in a new tab in the same pane.' - splitPaneDirection: type: 'string' default: 'right' - enum: ['right', 'down'] - description: 'Direction in which to open the split pane (when **Open Preview In Split Pane** is enabled).' + enum: ['none', 'right', 'down', 'left', 'up'] + description: 'Where to open the preview, whether in a new tab in the same pane (`none`), or in a new pane in the specified direction.' grammars: type: 'array' default: [ @@ -121,8 +130,9 @@ module.exports = previousActivePane = atom.workspace.getActivePane() options = searchAllPanes: true - if atom.config.get('markdown-preview.openPreviewInSplitPane') - options.split = atom.config.get('markdown-preview.splitPaneDirection') + splitPane = atom.config.get('markdown-preview.openPreviewInSplitPane') + if splitPane isnt 'none' + options.split = splitPane atom.workspace.open(uri, options).then (markdownPreviewView) -> if isMarkdownPreviewView(markdownPreviewView) previousActivePane.activate() From bd0253d047188073b899cc545df872465eeefc9f Mon Sep 17 00:00:00 2001 From: Jared Reisinger Date: Mon, 15 Feb 2016 23:28:38 -0800 Subject: [PATCH 4/6] Add unit tests for split direction. Check to see that split direction `right` causes a `horizontal` PaneAxis orientation, and a `down` direction causes a `vertical` orientation. --- spec/markdown-preview-spec.coffee | 34 ++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/spec/markdown-preview-spec.coffee b/spec/markdown-preview-spec.coffee index 7173c2d..a9718d7 100644 --- a/spec/markdown-preview-spec.coffee +++ b/spec/markdown-preview-spec.coffee @@ -6,7 +6,7 @@ MarkdownPreviewView = require '../lib/markdown-preview-view' {$} = require 'atom-space-pen-views' describe "Markdown preview package", -> - [workspaceElement, preview] = [] + [workspaceElement, previewPane, preview] = [] beforeEach -> fixturesPath = path.join(__dirname, 'fixtures') @@ -25,22 +25,46 @@ describe "Markdown preview package", -> waitsForPromise -> atom.packages.activatePackage('language-gfm') - expectPreviewInSplitPane = -> + expectPreviewInSplitPane = (orientation='horizontal')-> runs -> expect(atom.workspace.getPanes()).toHaveLength 2 waitsFor "markdown preview to be created", -> - preview = atom.workspace.getPanes()[1].getActiveItem() + previewPane = atom.workspace.getPanes()[1] + preview = previewPane.getActiveItem() runs -> expect(preview).toBeInstanceOf(MarkdownPreviewView) expect(preview.getPath()).toBe atom.workspace.getActivePaneItem().getPath() + expect(previewPane.getParent().getOrientation()).toBe orientation describe "when a preview has not been created for the file", -> - it "displays a markdown preview in a split pane", -> + it "displays a markdown preview in a split pane (right by default)", -> waitsForPromise -> atom.workspace.open("subdir/file.markdown") runs -> atom.commands.dispatch workspaceElement, 'markdown-preview:toggle' - expectPreviewInSplitPane() + expectPreviewInSplitPane('horizontal') + + runs -> + [editorPane] = atom.workspace.getPanes() + expect(editorPane.getItems()).toHaveLength 1 + expect(editorPane.isActive()).toBe true + + it "displays a markdown preview in a split pane (right)", -> + atom.config.set 'markdown-preview.openPreviewInSplitPane', 'right' + waitsForPromise -> atom.workspace.open("subdir/file.markdown") + runs -> atom.commands.dispatch workspaceElement, 'markdown-preview:toggle' + expectPreviewInSplitPane('horizontal') + + runs -> + [editorPane] = atom.workspace.getPanes() + expect(editorPane.getItems()).toHaveLength 1 + expect(editorPane.isActive()).toBe true + + it "displays a markdown preview in a split pane (down)", -> + atom.config.set 'markdown-preview.openPreviewInSplitPane', 'down' + waitsForPromise -> atom.workspace.open("subdir/file.markdown") + runs -> atom.commands.dispatch workspaceElement, 'markdown-preview:toggle' + expectPreviewInSplitPane('vertical') runs -> [editorPane] = atom.workspace.getPanes() From 8e115c97a68d325d5bb529476732b4d6db99da83 Mon Sep 17 00:00:00 2001 From: Jared Reisinger Date: Mon, 15 Feb 2016 23:45:21 -0800 Subject: [PATCH 5/6] Fix function arrow spacing --- spec/markdown-preview-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/markdown-preview-spec.coffee b/spec/markdown-preview-spec.coffee index a9718d7..594a811 100644 --- a/spec/markdown-preview-spec.coffee +++ b/spec/markdown-preview-spec.coffee @@ -25,7 +25,7 @@ describe "Markdown preview package", -> waitsForPromise -> atom.packages.activatePackage('language-gfm') - expectPreviewInSplitPane = (orientation='horizontal')-> + expectPreviewInSplitPane = (orientation='horizontal') -> runs -> expect(atom.workspace.getPanes()).toHaveLength 2 From eb69fc35a627671fa682a7ebdac292ed23396708 Mon Sep 17 00:00:00 2001 From: Jared Reisinger Date: Thu, 10 Mar 2016 17:42:10 -0800 Subject: [PATCH 6/6] Rename option to 'splitPaneDirection' --- lib/main.coffee | 2 +- package.json | 2 +- spec/markdown-preview-spec.coffee | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/main.coffee b/lib/main.coffee index b4cd6f0..2d6c100 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -86,7 +86,7 @@ module.exports = previousActivePane = atom.workspace.getActivePane() options = searchAllPanes: true - splitPane = atom.config.get('markdown-preview.openPreviewInSplitPane') + splitPane = atom.config.get('markdown-preview.splitPaneDirection') if splitPane isnt 'none' options.split = splitPane atom.workspace.open(uri, options).then (markdownPreviewView) -> diff --git a/package.json b/package.json index 7ede10c..e4723c8 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "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." }, - "openPreviewInSplitPane": { + "splitPaneDirection": { "type": "string", "default": "right", "enum": ["none", "right", "down"], diff --git a/spec/markdown-preview-spec.coffee b/spec/markdown-preview-spec.coffee index 594a811..52046d0 100644 --- a/spec/markdown-preview-spec.coffee +++ b/spec/markdown-preview-spec.coffee @@ -50,7 +50,7 @@ describe "Markdown preview package", -> expect(editorPane.isActive()).toBe true it "displays a markdown preview in a split pane (right)", -> - atom.config.set 'markdown-preview.openPreviewInSplitPane', 'right' + atom.config.set 'markdown-preview.splitPaneDirection', 'right' waitsForPromise -> atom.workspace.open("subdir/file.markdown") runs -> atom.commands.dispatch workspaceElement, 'markdown-preview:toggle' expectPreviewInSplitPane('horizontal') @@ -61,7 +61,7 @@ describe "Markdown preview package", -> expect(editorPane.isActive()).toBe true it "displays a markdown preview in a split pane (down)", -> - atom.config.set 'markdown-preview.openPreviewInSplitPane', 'down' + atom.config.set 'markdown-preview.splitPaneDirection', 'down' waitsForPromise -> atom.workspace.open("subdir/file.markdown") runs -> atom.commands.dispatch workspaceElement, 'markdown-preview:toggle' expectPreviewInSplitPane('vertical')