Skip to content

Commit

Permalink
better tag a click event.
Browse files Browse the repository at this point in the history
  • Loading branch information
shd101wyy committed Jun 20, 2017
1 parent 2f6f70d commit 77cb654
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ export function activate(context: vscode.ExtensionContext) {
type: 'run-code-chunk'
})
}

function clickTagA(uri, href) {
const sourceUri = vscode.Uri.parse(decodeURIComponent(uri));
if (['.pdf', '.xls', '.xlsx', '.doc', '.ppt', '.docx', '.pptx'].indexOf(path.extname(href)) >= 0) {
utility.openFile(href)
} else if (href.match(/^file\:\/\/\//)) {
// openFilePath = href.slice(8) # remove protocal
let openFilePath = href.replace(/(\s*)[\#\?](.+)$/, '') // remove #anchor and ?params...
openFilePath = decodeURI(openFilePath)
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(openFilePath), vscode.ViewColumn.One)
} else {
utility.openFile(href)
}
}


context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
Expand Down Expand Up @@ -364,6 +378,8 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(vscode.commands.registerCommand('_markdown-preview-enhanced.runAllCodeChunks', runAllCodeChunks))

context.subscriptions.push(vscode.commands.registerCommand('_markdown-preview-enhanced.clickTagA', clickTagA))

context.subscriptions.push(contentProviderRegistration)
}

Expand Down
23 changes: 23 additions & 0 deletions src/markdown-preview-enhanced-webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,27 @@ async function initEvents() {
mpe.refreshingIcon.style.display = "none"
}

function bindTagAClickEvent() {
const as = mpe.previewElement.getElementsByTagName('a')
for (let i = 0; i < as.length; i++) {
const a = as[i]
const href = a.getAttribute('href')
if (href && href[0] === '#') {
// anchor, do nothing
} else {
a.onclick = (event)=> {
event.preventDefault()
event.stopPropagation()

window.parent.postMessage({
command: 'did-click-link', // <= this has to be `did-click-link` to post message
data: `command:_markdown-preview-enhanced.clickTagA?${JSON.stringify([sourceUri, href])}`
}, 'file://')
}
}
}
}

/**
* update previewElement innerHTML content
* @param html
Expand Down Expand Up @@ -695,6 +716,8 @@ function updateHTML(html) {
// init several events
initEvents().then(()=> {
mpe.scrollMap = null

bindTagAClickEvent()

// scroll to initial position
if (!mpe.doneLoadingPreview) {
Expand Down

0 comments on commit 77cb654

Please sign in to comment.