From 156867fe4787b49eec3953a0d05df2a01271836b Mon Sep 17 00:00:00 2001 From: KMcB Date: Thu, 5 Apr 2018 13:22:44 +0200 Subject: [PATCH 1/2] fix multi panes opening by associating each pane with the source file pane id --- lib/mjml-preview.js | 12 ++++++---- lib/mjml-view.js | 58 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/lib/mjml-preview.js b/lib/mjml-preview.js index aa35f30..00e29ea 100644 --- a/lib/mjml-preview.js +++ b/lib/mjml-preview.js @@ -4,6 +4,7 @@ import url from 'url' import { CompositeDisposable } from 'atom' import MJMLView from './mjml-view' + let MJMLPaneView = null export default { @@ -48,9 +49,8 @@ export default { } if (currentEditor.id === atom.workspace.getActiveTextEditor().id) { - const uri = 'mjml-preview://file' + const uri = `mjml-preview://editor/${currentEditor.id}` const fileGrammar = currentEditor.getGrammar() - if (fileGrammar.scopeName !== 'text.mjml.basic') { return; } @@ -58,7 +58,11 @@ export default { const previousActivePane = atom.workspace.getActivePane() atom.workspace.open(uri, { split: 'right', searchAllPanes: true }) - .then(() => MJMLPaneView.render(currentEditor)) + .then((view) => { + if (view instanceof MJMLView) { + return view.render(currentEditor) + } + }) .done(() => previousActivePane.activate()) } }, @@ -72,7 +76,7 @@ export default { const filePath = decodeURI(pathname) - MJMLPaneView = new MJMLView(filePath) + MJMLPaneView = new MJMLView({editorId: filePath.substring(1), filePath}) return MJMLPaneView } catch (e) { diff --git a/lib/mjml-view.js b/lib/mjml-view.js index dec0d62..5da5fa8 100644 --- a/lib/mjml-view.js +++ b/lib/mjml-view.js @@ -7,26 +7,47 @@ import { $, ScrollView } from 'atom-space-pen-views' import { mjml2html } from 'mjml' class MJMLView extends ScrollView { - static serialiaze() { + static serialize() { return { - deserializer: 'AtomHtmlPreviewView', + deserializer: 'MJMLView', filePath: this.filePath, + editorId: this.editorId } } - static deserialize ({ filePath }) { - return new MJMLView(filePath) + static deserialize (state) { + return new MJMLView(state) } static content () { return MJMLView.div({ 'class': 'atom-html-preview native-key-bindings', 'tabindex': -1 }) } - constructor(filePath) { + constructor({editorId, filePath}) { super() + this.webViewLoaded = false + this.reloadLater = false this.filePath = filePath + this.editorId = editorId atom.deserializers.add(this) + + if (this.editorId) { + this.editor = this.editorForId(this.editorId) + } + this.createWebView() + + this.addReadyListener() + } + + addReadyListener() { + this.webview.addEventListener('dom-ready', () => { + this.webViewLoaded = true + if (this.reloadLater) { + this.reloadLater = false + this.webview.reload() + } + }) } createWebView() { @@ -36,10 +57,19 @@ class MJMLView extends ScrollView { this.append($(this.webview)) } + editorForId(editorId) { + const editors = atom.workspace.getTextEditors() + for (let i = 0 ; i < editors.length ; i++) { + const editor = editors[i] + if (editor && editor.id === parseInt(editorId)) return editor + } + } + renderMJML(TextEditor, done) { const mjmlTempPath = path.resolve(path.join(os.tmpdir(), `${TextEditor.getTitle()}.html`)) const outputHTML = mjml2html(TextEditor.getText(), { level: 'skip', disableMinify: true, filePath: TextEditor.getPath() }).html + fs.writeFile(mjmlTempPath, outputHTML, (err) => { if (err) { throw err @@ -52,20 +82,26 @@ class MJMLView extends ScrollView { render(TextEditor) { this.renderMJML(TextEditor, (file) => { this.webview.src = file - try { - this.webview.reload() - } catch (error) { - return null + + + if (this.webViewLoaded) { + try { + this.webview.reload() + } catch (error) { + return null + } + } else { + this.reloadLater = true } }) } getTitle() { - return 'MJML Preview' + return `PREVIEW - ${this.editor.getTitle()}` } getURI() { - return `mjml-preview://file` + return `mjml-preview://editor/${this.editorId}` } } From a7c3c5b1b0088ac072a30093e648ccae5aae78a7 Mon Sep 17 00:00:00 2001 From: KMcB Date: Thu, 5 Apr 2018 13:27:06 +0200 Subject: [PATCH 2/2] remove some empty lines --- lib/mjml-preview.js | 1 - lib/mjml-view.js | 3 --- 2 files changed, 4 deletions(-) diff --git a/lib/mjml-preview.js b/lib/mjml-preview.js index 00e29ea..60757f9 100644 --- a/lib/mjml-preview.js +++ b/lib/mjml-preview.js @@ -4,7 +4,6 @@ import url from 'url' import { CompositeDisposable } from 'atom' import MJMLView from './mjml-view' - let MJMLPaneView = null export default { diff --git a/lib/mjml-view.js b/lib/mjml-view.js index 5da5fa8..732f920 100644 --- a/lib/mjml-view.js +++ b/lib/mjml-view.js @@ -36,7 +36,6 @@ class MJMLView extends ScrollView { } this.createWebView() - this.addReadyListener() } @@ -69,7 +68,6 @@ class MJMLView extends ScrollView { const mjmlTempPath = path.resolve(path.join(os.tmpdir(), `${TextEditor.getTitle()}.html`)) const outputHTML = mjml2html(TextEditor.getText(), { level: 'skip', disableMinify: true, filePath: TextEditor.getPath() }).html - fs.writeFile(mjmlTempPath, outputHTML, (err) => { if (err) { throw err @@ -83,7 +81,6 @@ class MJMLView extends ScrollView { this.renderMJML(TextEditor, (file) => { this.webview.src = file - if (this.webViewLoaded) { try { this.webview.reload()