From a5bdd26feb9644e3fdd9726e0ec6cbcd461d4bf7 Mon Sep 17 00:00:00 2001
From: Wliu <50Wliu@users.noreply.github.com>
Date: Mon, 20 Nov 2017 22:36:31 +0100
Subject: [PATCH 1/2] Do not hijack core:save-as
---
lib/main.coffee | 17 +++----
lib/markdown-preview-view.coffee | 61 ++++++++++++++------------
spec/markdown-preview-spec.coffee | 6 ++-
spec/markdown-preview-view-spec.coffee | 7 ++-
4 files changed, 47 insertions(+), 44 deletions(-)
diff --git a/lib/main.coffee b/lib/main.coffee
index f3f5c52..c700829 100644
--- a/lib/main.coffee
+++ b/lib/main.coffee
@@ -125,9 +125,9 @@ module.exports =
atom.clipboard.write(html)
saveAsHTML: ->
- activePane = atom.workspace.getActivePaneItem()
- if isMarkdownPreviewView(activePane)
- activePane.saveAs()
+ activePaneItem = atom.workspace.getActivePaneItem()
+ if isMarkdownPreviewView(activePaneItem)
+ atom.workspace.getActivePane().saveItemAs(activePaneItem)
return
editor = atom.workspace.getActiveTextEditor()
@@ -138,12 +138,7 @@ module.exports =
uri = @uriForEditor(editor)
markdownPreviewPane = atom.workspace.paneForURI(uri)
- return unless markdownPreviewPane?
+ markdownPreviewPaneItem = markdownPreviewPane?.itemForURI(uri)
- previousActivePane = atom.workspace.getActivePane()
- markdownPreviewPane.activate()
- activePane = atom.workspace.getActivePaneItem()
-
- if isMarkdownPreviewView(activePane)
- activePane.saveAs().then ->
- previousActivePane.activate()
+ if isMarkdownPreviewView(markdownPreviewPaneItem)
+ markdownPreviewPane.saveItemAs(markdownPreviewPaneItem)
diff --git a/lib/markdown-preview-view.coffee b/lib/markdown-preview-view.coffee
index 7e00ee9..6b3d7fb 100644
--- a/lib/markdown-preview-view.coffee
+++ b/lib/markdown-preview-view.coffee
@@ -111,9 +111,6 @@ class MarkdownPreviewView
@disposables.add atom.grammars.onDidUpdateGrammar -> lazyRenderMarkdown()
atom.commands.add @element,
- 'core:save-as': (event) =>
- event.stopPropagation()
- @saveAs()
'core:copy': (event) =>
event.stopPropagation()
@copyToClipboard()
@@ -312,35 +309,41 @@ class MarkdownPreviewView
else
atom.clipboard.write(html)
- saveAs: ->
- return if @loading
+ getSaveDialogOptions: ->
+ defaultPath = @getPath()
+ if defaultPath
+ defaultPath += '.html'
+ else
+ defaultPath = 'untitled.md.html'
+ if projectPath = atom.project.getPaths()[0]
+ defaultPath = path.join(projectPath, defaultPath)
+
+ return {defaultPath}
+
+ saveAs: (htmlFilePath) ->
+ if @loading
+ atom.notifications.addWarning('Please wait until the Markdown Preview has finished loading before saving')
+ return
filePath = @getPath()
title = 'Markdown to HTML'
if filePath
title = path.parse(filePath).name
- filePath += '.html'
- else
- filePath = 'untitled.md.html'
- if projectPath = atom.project.getPaths()[0]
- filePath = path.join(projectPath, filePath)
-
- if htmlFilePath = atom.showSaveDialogSync(filePath)
- @getHTML (error, htmlBody) =>
- if error?
- atom.notifications.addError('Saving Markdown as HTML failed', {dismissable: true, detail: error.message})
- else
- html = """
-
-
-
-
- #{title}
-
-
- #{htmlBody}
- """ + "\n" # Ensure trailing newline
-
- fs.writeFileSync(htmlFilePath, html)
- atom.workspace.open(htmlFilePath)
+ @getHTML (error, htmlBody) =>
+ if error?
+ throw error
+ else
+ html = """
+
+
+
+
+ #{title}
+
+
+ #{htmlBody}
+ """ + "\n" # Ensure trailing newline
+
+ fs.writeFileSync(htmlFilePath, html)
+ atom.workspace.open(htmlFilePath)
diff --git a/spec/markdown-preview-spec.coffee b/spec/markdown-preview-spec.coffee
index d33ad0a..2fe2e71 100644
--- a/spec/markdown-preview-spec.coffee
+++ b/spec/markdown-preview-spec.coffee
@@ -507,7 +507,8 @@ describe "Markdown Preview", ->
expect(fs.existsSync(outputPath)).toBe false
runs ->
- spyOn(atom, 'showSaveDialogSync').andReturn(outputPath)
+ spyOn(preview, 'getSaveDialogOptions').andReturn({defaultPath: outputPath})
+ spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake((options, callback) -> callback(options.defaultPath))
atom.commands.dispatch atom.workspace.getActiveTextEditor().getElement(), 'markdown-preview:save-as-html'
waitsFor ->
@@ -524,7 +525,8 @@ describe "Markdown Preview", ->
expect(fs.existsSync(outputPath)).toBe false
runs ->
- spyOn(atom, 'showSaveDialogSync').andReturn(outputPath)
+ spyOn(preview, 'getSaveDialogOptions').andReturn({defaultPath: outputPath})
+ spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake((options, callback) -> callback(options.defaultPath))
atom.commands.dispatch editorPane.getActiveItem().getElement(), 'markdown-preview:save-as-html'
waitsFor ->
diff --git a/spec/markdown-preview-view-spec.coffee b/spec/markdown-preview-view-spec.coffee
index f3d424f..fddd54b 100644
--- a/spec/markdown-preview-view-spec.coffee
+++ b/spec/markdown-preview-view-spec.coffee
@@ -272,7 +272,9 @@ describe "MarkdownPreviewView", ->
preview.destroy()
filePath = atom.project.getDirectories()[0].resolve('subdir/code-block.md')
preview = new MarkdownPreviewView({filePath})
- jasmine.attachToDOM(preview.element)
+ # Add to workspace for core:save-as command to be propagated up to the workspace
+ waitsForPromise -> atom.workspace.open(preview)
+ runs -> jasmine.attachToDOM(atom.views.getView(atom.workspace))
it "saves the rendered HTML and opens it", ->
outputPath = fs.realpathSync(temp.mkdirSync()) + 'output.html'
@@ -310,7 +312,8 @@ describe "MarkdownPreviewView", ->
preview.renderMarkdown()
runs ->
- spyOn(atom, 'showSaveDialogSync').andReturn(outputPath)
+ spyOn(preview, 'getSaveDialogOptions').andReturn({defaultPath: outputPath})
+ spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake((options, callback) -> callback(options.defaultPath))
spyOn(preview, 'getDocumentStyleSheets').andReturn(markdownPreviewStyles)
spyOn(preview, 'getTextEditorStyles').andReturn(atomTextEditorStyles)
atom.commands.dispatch preview.element, 'core:save-as'
From e44a5d2247ca302b9f6834a664534ed48df3f9a7 Mon Sep 17 00:00:00 2001
From: Wliu <50Wliu@users.noreply.github.com>
Date: Mon, 20 Nov 2017 22:55:35 +0100
Subject: [PATCH 2/2] Backwards-compatible specs
---
spec/markdown-preview-spec.coffee | 12 ++++++++++--
spec/markdown-preview-view-spec.coffee | 6 +++++-
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/spec/markdown-preview-spec.coffee b/spec/markdown-preview-spec.coffee
index 2fe2e71..ef4765b 100644
--- a/spec/markdown-preview-spec.coffee
+++ b/spec/markdown-preview-spec.coffee
@@ -508,7 +508,11 @@ describe "Markdown Preview", ->
runs ->
spyOn(preview, 'getSaveDialogOptions').andReturn({defaultPath: outputPath})
- spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake((options, callback) -> callback(options.defaultPath))
+ spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake (options, callback) ->
+ callback?(options.defaultPath)
+ # TODO: When https://github.com/atom/atom/pull/16245 lands remove the return
+ # and the existence check on the callback
+ return options.defaultPath
atom.commands.dispatch atom.workspace.getActiveTextEditor().getElement(), 'markdown-preview:save-as-html'
waitsFor ->
@@ -526,7 +530,11 @@ describe "Markdown Preview", ->
runs ->
spyOn(preview, 'getSaveDialogOptions').andReturn({defaultPath: outputPath})
- spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake((options, callback) -> callback(options.defaultPath))
+ spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake (options, callback) ->
+ callback?(options.defaultPath)
+ # TODO: When https://github.com/atom/atom/pull/16245 lands remove the return
+ # and the existence check on the callback
+ return options.defaultPath
atom.commands.dispatch editorPane.getActiveItem().getElement(), 'markdown-preview:save-as-html'
waitsFor ->
diff --git a/spec/markdown-preview-view-spec.coffee b/spec/markdown-preview-view-spec.coffee
index fddd54b..2d4f8a4 100644
--- a/spec/markdown-preview-view-spec.coffee
+++ b/spec/markdown-preview-view-spec.coffee
@@ -313,7 +313,11 @@ describe "MarkdownPreviewView", ->
runs ->
spyOn(preview, 'getSaveDialogOptions').andReturn({defaultPath: outputPath})
- spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake((options, callback) -> callback(options.defaultPath))
+ spyOn(atom.applicationDelegate, 'showSaveDialog').andCallFake (options, callback) ->
+ callback?(options.defaultPath)
+ # TODO: When https://github.com/atom/atom/pull/16245 lands remove the return
+ # and the existence check on the callback
+ return options.defaultPath
spyOn(preview, 'getDocumentStyleSheets').andReturn(markdownPreviewStyles)
spyOn(preview, 'getTextEditorStyles').andReturn(atomTextEditorStyles)
atom.commands.dispatch preview.element, 'core:save-as'