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..ef4765b 100644 --- a/spec/markdown-preview-spec.coffee +++ b/spec/markdown-preview-spec.coffee @@ -507,7 +507,12 @@ 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) + # 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 -> @@ -524,7 +529,12 @@ 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) + # 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 f3d424f..2d4f8a4 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,12 @@ 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) + # 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'