Skip to content

Commit

Permalink
feat: right-click the panel header to copy the markdown link (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqroot committed May 27, 2023
1 parent 8db9cc2 commit 249b067
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ joplin.plugins.register({
} else if (message.name === 'contextMenu') {
const noteId = (await joplin.workspace.selectedNoteIds())[0];
const noteTitle = (await joplin.data.get(['notes', noteId], { fields: ['title'] })).title;
const innerLink = `[${noteTitle}#${message.content}](:/${noteId}#${message.hash})`;
let innerLink:string;
if (message.hash === '') {
innerLink = `[${noteTitle}](:/${noteId})`;
} else {
innerLink = `[${noteTitle}#${message.content}](:/${noteId}#${message.hash})`;
}

const input = document.createElement('input');
input.setAttribute('value', innerLink);
Expand Down
2 changes: 1 addition & 1 deletion src/panelHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ${userStyleFromFile}
${userStyle}
</style></head>
<body><div class="outline-content">
<a id="header" href="javascript:;" onclick="scrollToTop()">OUTLINE</a>
<a id="header" href="javascript:;" onclick="scrollToTop()" oncontextmenu="copyInnerLink('', '')">OUTLINE</a>
<div class="container">
${itemHtmlList.join('\n')}
</div>
Expand Down
18 changes: 13 additions & 5 deletions src/webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ function tocItemLinkClicked(dataset) {
}

function copyInnerLink(dataset, text) {
webviewApi.postMessage({
name: 'contextMenu',
hash: dataset.slug,
content: text.trim(),
});
if (dataset === '') {
webviewApi.postMessage({
name: 'contextMenu',
hash: '',
content: '',
});
} else {
webviewApi.postMessage({
name: 'contextMenu',
hash: dataset.slug,
content: text.trim(),
});
}

document.getElementById('header').innerHTML = 'Copy successful!';
setTimeout(() => {
Expand Down

0 comments on commit 249b067

Please sign in to comment.