Skip to content

Commit

Permalink
feat(vscodePlugin): open web link and open file link
Browse files Browse the repository at this point in the history
  • Loading branch information
RSS1102 committed Sep 24, 2024
1 parent 4466bc1 commit 50d2a04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions vscodePlugin/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ const initCherryPanelEvent = () => {
}
});
break;
case 'open-url':
console.log('open-url', data);
// http/https协议的链接,直接打开
if (/^(http|https):\/\//.test(data)) {
console.log('http: ', data);
vscode.env.openExternal(vscode.Uri.parse(data));
}
// vscode协议的链接,打开vscode
console.log('vscode: ', data);
vscode.env.openExternal(vscode.Uri.parse(data));
console.log('open-url完成');
break;
}
});
cherryPanel?.onDidDispose(() => {
Expand Down
24 changes: 24 additions & 0 deletions vscodePlugin/web-resources/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ function changeTheme(theme) {
)} theme__${theme}`;
}

/** 处理 a 链接跳转问题 */
const onClickLink = (target) => {
console.log('onClickLink', target);
console.log('onClickLink', target.href);
vscode.postMessage({
type: 'open-url',
data: target.href,
});
};

const basicConfig = {
id: 'markdown',
externals: {
Expand Down Expand Up @@ -295,6 +305,7 @@ const basicConfig = {
// eslint-disable-next-line no-undef
changeString2Pinyin: pinyin,
beforeImageMounted(srcProp, srcValue) {
console.log('beforeImageMounted', srcProp, srcValue);
if (isHttpUrl(srcValue) || isDataUrl(srcValue)) {
return {
src: srcValue,
Expand All @@ -306,6 +317,19 @@ const basicConfig = {
src: path.join(basePath, srcValue),
};
},
onClickPreview: (e) => {
const { target } = e;
switch (target?.nodeName) {
case 'SPAN':
if (target?.parentElement?.nodeName === 'A') {
onClickLink(target?.parentElement);
}
break;
case 'A':
onClickLink(target);
break;
};
},
},
};

Expand Down

0 comments on commit 50d2a04

Please sign in to comment.