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

Do not hijack core:save-as #521

Merged
merged 2 commits into from
Jan 8, 2018
Merged
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
17 changes: 6 additions & 11 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
61 changes: 32 additions & 29 deletions lib/markdown-preview-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>#{title}</title>
<style>#{@getMarkdownPreviewCSS()}</style>
</head>
<body class='markdown-preview' data-use-github-style>#{htmlBody}</body>
</html>""" + "\n" # Ensure trailing newline

fs.writeFileSync(htmlFilePath, html)
atom.workspace.open(htmlFilePath)
@getHTML (error, htmlBody) =>
if error?
throw error
else
html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>#{title}</title>
<style>#{@getMarkdownPreviewCSS()}</style>
</head>
<body class='markdown-preview' data-use-github-style>#{htmlBody}</body>
</html>""" + "\n" # Ensure trailing newline

fs.writeFileSync(htmlFilePath, html)
atom.workspace.open(htmlFilePath)
14 changes: 12 additions & 2 deletions spec/markdown-preview-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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 ->
Expand Down
11 changes: 9 additions & 2 deletions spec/markdown-preview-view-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down