Skip to content

Commit

Permalink
Merge pull request #56 from shd101wyy/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
shd101wyy authored Nov 6, 2017
2 parents 6ec89a2 + f9444ec commit 059d7c2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 42 deletions.
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -142,6 +154,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,
Expand Down Expand Up @@ -370,7 +387,7 @@
"package": "vsce package"
},
"dependencies": {
"@shd101wyy/mume": "^0.2.3"
"@shd101wyy/mume": "^0.2.4"
},
"devDependencies": {
"@types/jquery": "^2.0.46",
Expand All @@ -381,4 +398,4 @@
"typescript": "^2.0.3",
"vscode": "^1.0.0"
}
}
}
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -67,6 +68,7 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig {
this.enableScriptExecution = config.get<boolean>('enableScriptExecution')

this.scrollSync = config.get<boolean>('scrollSync')
this.liveUpdate = config.get<boolean>('liveUpdate')
}

public isEqualTo(otherConfig: MarkdownPreviewEnhancedConfig) {
Expand Down
111 changes: 76 additions & 35 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -60,6 +60,45 @@ export function activate(context: vscode.ExtensionContext) {
})
}

function toggleScrollSync() {
const config = vscode.workspace.getConfiguration('markdown-preview-enhanced')
const scrollSync = !config.get<boolean>('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<boolean>('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<boolean>('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))
Expand Down Expand Up @@ -134,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)
}

Expand All @@ -147,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)
Expand All @@ -160,69 +197,69 @@ 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));
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)
}

Expand Down Expand Up @@ -274,7 +311,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)
Expand All @@ -289,7 +326,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]
Expand Down Expand Up @@ -321,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,
{
Expand Down Expand Up @@ -386,7 +421,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))

Expand Down Expand Up @@ -463,18 +504,18 @@ 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)
.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)
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/image-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`)
Expand Down
8 changes: 5 additions & 3 deletions src/preview-content-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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 || ''
})
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 059d7c2

Please sign in to comment.