From 274ca5a188279d5a4d93f542adb915b552e13dc0 Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sat, 4 Nov 2017 14:43:54 -0500 Subject: [PATCH 1/7] Supported `Live Update` --- package.json | 13 +++++++++++-- src/config.ts | 2 ++ src/preview-content-provider.ts | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1b8cca3..836e1a3 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,10 @@ "command": "markdown-preview-enhanced.openPreview", "title": "Markdown Preview Enhanced: Open Preview" }, + { + "command": "markdown-preview-enhanced.toggleLiveUpdate", + "title": "Markdown Preview Enhanced: Toggle Live Update" + }, { "command": "markdown-preview-enhanced.openImageHelper", "title": "Markdown Preview Enhanced: Image Helper" @@ -142,6 +146,11 @@ "default": true, "type": "boolean" }, + "markdown-preview-enhanced.liveUpdate": { + "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.", + "default": true, + "type": "boolean" + }, "markdown-preview-enhanced.singlePreview": { "description": "Open Only One Preview", "default": true, @@ -370,7 +379,7 @@ "package": "vsce package" }, "dependencies": { - "@shd101wyy/mume": "^0.2.3" + "@shd101wyy/mume": "file:../mume" }, "devDependencies": { "@types/jquery": "^2.0.46", @@ -381,4 +390,4 @@ "typescript": "^2.0.3", "vscode": "^1.0.0" } -} \ No newline at end of file +} diff --git a/src/config.ts b/src/config.ts index 37d90f6..aff3207 100644 --- a/src/config.ts +++ b/src/config.ts @@ -35,6 +35,7 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig { // preview config public readonly scrollSync: boolean + public readonly liveUpdate: boolean private constructor() { const config = vscode.workspace.getConfiguration('markdown-preview-enhanced') @@ -67,6 +68,7 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig { this.enableScriptExecution = config.get('enableScriptExecution') this.scrollSync = config.get('scrollSync') + this.liveUpdate = config.get('liveUpdate') } public isEqualTo(otherConfig: MarkdownPreviewEnhancedConfig) { diff --git a/src/preview-content-provider.ts b/src/preview-content-provider.ts index bf1f22e..b63e2c7 100644 --- a/src/preview-content-provider.ts +++ b/src/preview-content-provider.ts @@ -447,6 +447,8 @@ export class MarkdownPreviewEnhancedView implements vscode.TextDocumentContentPr } public update(sourceUri: Uri) { + if (!this.config.liveUpdate) return + // console.log('update') if (!this._waiting) { this._waiting = true; From 344328cfaeb5210aa727a57dc39f00afb4a92df3 Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sat, 4 Nov 2017 15:08:22 -0500 Subject: [PATCH 2/7] Removed `toggle` command from list. --- package.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/package.json b/package.json index 836e1a3..2e9cac9 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,6 @@ "command": "markdown-preview-enhanced.openPreview", "title": "Markdown Preview Enhanced: Open Preview" }, - { - "command": "markdown-preview-enhanced.toggleLiveUpdate", - "title": "Markdown Preview Enhanced: Toggle Live Update" - }, { "command": "markdown-preview-enhanced.openImageHelper", "title": "Markdown Preview Enhanced: Image Helper" From 99409b4bc62b6750d052cf2de5b050d4c48e878a Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sat, 4 Nov 2017 19:57:05 -0500 Subject: [PATCH 3/7] Added `toggleScrollSync`, `toggleLiveUpdate`, and `toggleBreakOnSingleNewLine` commands. --- package.json | 12 ++++++++++++ src/extension.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2e9cac9..b125659 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,18 @@ "command": "markdown-preview-enhanced.openPreview", "title": "Markdown Preview Enhanced: Open Preview" }, + { + "command": "markdown-preview-enhanced.toggleScrollSync", + "title": "Markdown Preview Enhanced: Toggle Scroll Sync" + }, + { + "command": "markdown-preview-enhanced.toggleLiveUpdate", + "title": "Markdown Preview Enhanced: Toggle Live Update" + }, + { + "command": "markdown-preview-enhanced.toggleBreakOnSingleNewLine", + "title": "Markdown Preview Enhanced: Toggle Break On Single New Line" + }, { "command": "markdown-preview-enhanced.openImageHelper", "title": "Markdown Preview Enhanced: Image Helper" diff --git a/src/extension.ts b/src/extension.ts index 3be997a..c3d8cda 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -60,6 +60,45 @@ export function activate(context: vscode.ExtensionContext) { }) } + function toggleScrollSync() { + const config = vscode.workspace.getConfiguration('markdown-preview-enhanced') + const scrollSync = !config.get('scrollSync') + config.update('scrollSync', scrollSync, true).then(()=> { + contentProvider.updateConfiguration() + if (scrollSync) { + vscode.window.showInformationMessage('Scroll Sync is enabled') + } else { + vscode.window.showInformationMessage('Scroll Sync is disabled') + } + }) + } + + function toggleLiveUpdate() { + const config = vscode.workspace.getConfiguration('markdown-preview-enhanced') + const liveUpdate = !config.get('liveUpdate') + config.update('liveUpdate', liveUpdate, true).then(()=> { + contentProvider.updateConfiguration() + if (liveUpdate) { + vscode.window.showInformationMessage('Live Update is enabled') + } else { + vscode.window.showInformationMessage('Live Update is disabled') + } + }) + } + + function toggleBreakOnSingleNewLine() { + const config = vscode.workspace.getConfiguration('markdown-preview-enhanced') + const breakOnSingleNewLine = !config.get('breakOnSingleNewLine') + config.update('breakOnSingleNewLine', breakOnSingleNewLine, true).then(()=> { + contentProvider.updateConfiguration() + if (breakOnSingleNewLine) { + vscode.window.showInformationMessage('Break On Single New Line is enabled') + } else { + vscode.window.showInformationMessage('Break On Single New Line is disabled') + } + }) + } + function customizeCSS() { const globalStyleLessFile = utility.addFileProtocol(path.resolve(utility.extensionConfigDirectoryPath, './style.less')) vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(globalStyleLessFile)) @@ -386,7 +425,13 @@ export function activate(context: vscode.ExtensionContext) { })) */ - context.subscriptions.push(vscode.commands.registerCommand('markdown-preview-enhanced.openPreview', openPreview)) + context.subscriptions.push(vscode.commands.registerCommand('markdown-preview-enhanced.openPreview', openPreview)) + + context.subscriptions.push(vscode.commands.registerCommand('markdown-preview-enhanced.toggleScrollSync', toggleScrollSync)) + + context.subscriptions.push(vscode.commands.registerCommand('markdown-preview-enhanced.toggleLiveUpdate', toggleLiveUpdate)) + + context.subscriptions.push(vscode.commands.registerCommand('markdown-preview-enhanced.toggleBreakOnSingleNewLine', toggleBreakOnSingleNewLine)) context.subscriptions.push(vscode.commands.registerCommand('markdown-preview-enhanced.openImageHelper', openImageHelper)) From 2d0d420a805043cf0ded2d68ebed9fc6ba81c957 Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sat, 4 Nov 2017 20:03:34 -0500 Subject: [PATCH 4/7] Fixed issue #672 --- src/image-helper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/image-helper.ts b/src/image-helper.ts index f57d54c..ed1d425 100644 --- a/src/image-helper.ts +++ b/src/image-helper.ts @@ -61,8 +61,8 @@ export function pasteImageFile(sourceUri: any, imageFilePath: string) { vscode.window.showInformationMessage(`Image ${imageFileName} has been copied to folder ${assetDirectoryPath}`) let url = `${imageFolderPath}/${imageFileName}` - if (url.indexOf(' ') >= 0) - url = `<${url}>` + if (url.indexOf(' ') >= 0) + url = url.replace(/ /g, '%20') editor.edit((textEditorEdit) => { textEditorEdit.insert(editor.selection.active, `![${description}](${url})`) From ce21e4fc53a47f6b752914c408e47b0d4d6ce036 Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sat, 4 Nov 2017 21:12:50 -0500 Subject: [PATCH 5/7] Fixed issue 667 --- src/extension.ts | 41 ++++++++++++++++----------------- src/preview-content-provider.ts | 6 ++--- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index c3d8cda..6427241 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -173,10 +173,8 @@ export function activate(context: vscode.ExtensionContext) { contentProvider.openImageHelper(vscode.window.activeTextEditor.document.uri) } - function webviewFinishLoading(sourceUri) { - sourceUri = vscode.Uri.parse(sourceUri) - // contentProvider.initMarkdownEngine(sourceUri) - // contentProvider.update(sourceUri) + function webviewFinishLoading(uri) { + const sourceUri = vscode.Uri.parse(uri) contentProvider.updateMarkdown(sourceUri) } @@ -186,7 +184,7 @@ export function activate(context: vscode.ExtensionContext) { * @param imageUrl: url of image to be inserted */ function insertImageUrl(uri, imageUrl) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); vscode.window.visibleTextEditors .filter(editor => isMarkdownFile(editor.document) && editor.document.uri.fsPath === sourceUri.fsPath) @@ -199,69 +197,70 @@ export function activate(context: vscode.ExtensionContext) { } function refreshPreview(uri) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.refreshPreview(sourceUri) } function openInBrowser(uri) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + console.log('openInBrowser: ', uri) + const sourceUri = vscode.Uri.parse(uri); contentProvider.openInBrowser(sourceUri) } function htmlExport(uri, offline) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.htmlExport(sourceUri, offline) } function chromeExport(uri, type) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.chromeExport(sourceUri, type) } function phantomjsExport(uri, type) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.phantomjsExport(sourceUri, type) } function princeExport(uri) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.princeExport(sourceUri) } function eBookExport(uri, fileType) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.eBookExport(sourceUri, fileType) } function pandocExport(uri) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.pandocExport(sourceUri) } function markdownExport(uri) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.markdownExport(sourceUri) } /* function cacheSVG(uri, code, svg) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.cacheSVG(sourceUri, code, svg) } */ function cacheCodeChunkResult(uri, id, result) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.cacheCodeChunkResult(sourceUri, id, result) } function runCodeChunk(uri, codeChunkId) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.runCodeChunk(sourceUri, codeChunkId) } function runAllCodeChunks(uri) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); contentProvider.runAllCodeChunks(sourceUri) } @@ -313,7 +312,7 @@ export function activate(context: vscode.ExtensionContext) { } function clickTagA(uri, href) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); href = decodeURIComponent(href) if (['.pdf', '.xls', '.xlsx', '.doc', '.ppt', '.docx', '.pptx'].indexOf(path.extname(href)) >= 0) { utility.openFile(href) @@ -328,7 +327,7 @@ export function activate(context: vscode.ExtensionContext) { } function clickTaskListCheckbox(uri, dataLine) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)); + const sourceUri = vscode.Uri.parse(uri); const visibleTextEditors = vscode.window.visibleTextEditors for (let i = 0; i < visibleTextEditors.length; i++) { const editor = visibleTextEditors[i] @@ -508,7 +507,7 @@ export function activate(context: vscode.ExtensionContext) { function revealLine(uri, line) { - const sourceUri = vscode.Uri.parse(decodeURIComponent(uri)) + const sourceUri = vscode.Uri.parse(uri) vscode.window.visibleTextEditors .filter(editor => isMarkdownFile(editor.document) && editor.document.uri.fsPath === sourceUri.fsPath) diff --git a/src/preview-content-provider.ts b/src/preview-content-provider.ts index b63e2c7..5263118 100644 --- a/src/preview-content-provider.ts +++ b/src/preview-content-provider.ts @@ -235,8 +235,8 @@ export class MarkdownPreviewEnhancedView implements vscode.TextDocumentContentPr return engine.generateHTMLTemplateForPreview({ inputString: text, config: { - previewUri: previewUri.toString(), - sourceUri: sourceUri.toString(), + previewUri: encodeURIComponent(previewUri.toString()), + sourceUri: encodeURIComponent(sourceUri.toString()), initialLine: initialLine, vscode: true }, @@ -281,7 +281,7 @@ export class MarkdownPreviewEnhancedView implements vscode.TextDocumentContentPr html: html, tocHTML: tocHTML, totalLineCount: document.lineCount, - sourceUri: sourceUri.toString(), + sourceUri: encodeURIComponent(sourceUri.toString()), id: yamlConfig.id || '', class: yamlConfig.class || '' }) From a11c20c8f3e10ad70dea3d9ee17aef9be3ca18a2 Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sun, 5 Nov 2017 19:47:48 -0600 Subject: [PATCH 6/7] Upgraded mume to version 0.2.4. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b125659..530373d 100644 --- a/package.json +++ b/package.json @@ -387,7 +387,7 @@ "package": "vsce package" }, "dependencies": { - "@shd101wyy/mume": "file:../mume" + "@shd101wyy/mume": "^0.2.4" }, "devDependencies": { "@types/jquery": "^2.0.46", From f9444ec99f0fafc87ccd0c34a1ed25211ee25597 Mon Sep 17 00:00:00 2001 From: shd101wyy Date: Sun, 5 Nov 2017 19:56:14 -0600 Subject: [PATCH 7/7] Refactor code. --- src/extension.ts | 55 +++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 6427241..32c474f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -15,15 +15,15 @@ export function activate(context: vscode.ExtensionContext) { // assume only one preview supported. const extensionPath = context.extensionPath - const contentProvider = new MarkdownPreviewEnhancedView(context); - const contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider('markdown-preview-enhanced', contentProvider); + const contentProvider = new MarkdownPreviewEnhancedView(context) + const contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider('markdown-preview-enhanced', contentProvider) function openPreview(uri?: vscode.Uri) { - let resource = uri; + let resource = uri if (!(resource instanceof vscode.Uri)) { if (vscode.window.activeTextEditor) { // we are relaxed and don't check for markdown files - resource = vscode.window.activeTextEditor.document.uri; + resource = vscode.window.activeTextEditor.document.uri } } @@ -184,7 +184,7 @@ export function activate(context: vscode.ExtensionContext) { * @param imageUrl: url of image to be inserted */ function insertImageUrl(uri, imageUrl) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) vscode.window.visibleTextEditors .filter(editor => isMarkdownFile(editor.document) && editor.document.uri.fsPath === sourceUri.fsPath) @@ -197,48 +197,47 @@ export function activate(context: vscode.ExtensionContext) { } function refreshPreview(uri) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.refreshPreview(sourceUri) } function openInBrowser(uri) { - console.log('openInBrowser: ', uri) - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.openInBrowser(sourceUri) } function htmlExport(uri, offline) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.htmlExport(sourceUri, offline) } function chromeExport(uri, type) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.chromeExport(sourceUri, type) } function phantomjsExport(uri, type) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.phantomjsExport(sourceUri, type) } function princeExport(uri) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.princeExport(sourceUri) } function eBookExport(uri, fileType) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.eBookExport(sourceUri, fileType) } function pandocExport(uri) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.pandocExport(sourceUri) } function markdownExport(uri) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.markdownExport(sourceUri) } @@ -250,17 +249,17 @@ export function activate(context: vscode.ExtensionContext) { */ function cacheCodeChunkResult(uri, id, result) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.cacheCodeChunkResult(sourceUri, id, result) } function runCodeChunk(uri, codeChunkId) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.runCodeChunk(sourceUri, codeChunkId) } function runAllCodeChunks(uri) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) contentProvider.runAllCodeChunks(sourceUri) } @@ -312,7 +311,7 @@ export function activate(context: vscode.ExtensionContext) { } function clickTagA(uri, href) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) href = decodeURIComponent(href) if (['.pdf', '.xls', '.xlsx', '.doc', '.ppt', '.docx', '.pptx'].indexOf(path.extname(href)) >= 0) { utility.openFile(href) @@ -327,7 +326,7 @@ export function activate(context: vscode.ExtensionContext) { } function clickTaskListCheckbox(uri, dataLine) { - const sourceUri = vscode.Uri.parse(uri); + const sourceUri = vscode.Uri.parse(uri) const visibleTextEditors = vscode.window.visibleTextEditors for (let i = 0; i < visibleTextEditors.length; i++) { const editor = visibleTextEditors[i] @@ -359,19 +358,17 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => { if (isMarkdownFile(event.document)) { - contentProvider.update(event.document.uri); + contentProvider.update(event.document.uri) } })) context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => { - contentProvider.updateConfiguration(); + contentProvider.updateConfiguration() })) context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(event => { if (isMarkdownFile(event.textEditor.document)) { - const previewUri = getPreviewUri(event.textEditor.document.uri); - // logger.log('updatePreviewForSelection', { markdownFile: markdownFile.toString() }); - // console.log('onDidChangeTextEditorSelection', markdownFile) + const previewUri = getPreviewUri(event.textEditor.document.uri) vscode.commands.executeCommand('_workbench.htmlPreview.postMessage', previewUri, { @@ -513,12 +510,12 @@ function revealLine(uri, line) { .filter(editor => isMarkdownFile(editor.document) && editor.document.uri.fsPath === sourceUri.fsPath) .forEach(editor => { const sourceLine = Math.min(Math.floor(line), editor.document.lineCount - 1) - const fraction = line - sourceLine; - const text = editor.document.lineAt(sourceLine).text; - const start = Math.floor(fraction * text.length); + const fraction = line - sourceLine + const text = editor.document.lineAt(sourceLine).text + const start = Math.floor(fraction * text.length) editor.revealRange( new vscode.Range(sourceLine, start, sourceLine + 1, 0), - vscode.TextEditorRevealType.InCenter); + vscode.TextEditorRevealType.InCenter) }) }