mirror of
https://github.com/ozavodny/obsidian-copy-inline-code-plugin.git
synced 2026-07-22 08:10:25 +00:00
commit
ff3acb5b6d
4 changed files with 2804 additions and 82 deletions
2842
package-lock.json
generated
2842
package-lock.json
generated
File diff suppressed because it is too large
Load diff
12
package.json
12
package.json
|
|
@ -6,20 +6,28 @@
|
|||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json",
|
||||
"commit": "cz"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@codemirror/language": "^6.8.0",
|
||||
"@types/node": "^16.11.6",
|
||||
"@types/node": "^16.111.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.29.0",
|
||||
"@typescript-eslint/parser": "5.29.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
"commitizen": "^4.3.0",
|
||||
"cz-conventional-changelog": "^3.3.0",
|
||||
"esbuild": "0.17.3",
|
||||
"obsidian": "latest",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
"config": {
|
||||
"commitizen": {
|
||||
"path": "./node_modules/cz-conventional-changelog"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,13 @@ export class CopyWidget extends WidgetType {
|
|||
const icon = createSpan({cls: "copy-to-clipboard-icon", text: " 📋"})
|
||||
|
||||
icon.onclick = (event) => {
|
||||
const textToCopy = (event.target as HTMLElement)?.parentNode?.querySelector('.cm-inline-code:not(.cm-formatting)')?.innerHTML
|
||||
const element = (event.target as HTMLElement)
|
||||
let previousElement = element.previousElementSibling
|
||||
while(previousElement && !previousElement.matches('.cm-inline-code:not(.cm-formatting)')) {
|
||||
previousElement = previousElement.previousElementSibling
|
||||
}
|
||||
|
||||
const textToCopy = previousElement?.innerHTML
|
||||
if(textToCopy) {
|
||||
navigator.clipboard.writeText(textToCopy)
|
||||
new Notice(`Copied '${textToCopy}' to clipboard!`);
|
||||
|
|
|
|||
24
src/main.ts
24
src/main.ts
|
|
@ -1,8 +1,30 @@
|
|||
import { Plugin } from 'obsidian';
|
||||
import { Notice, Plugin } from 'obsidian';
|
||||
import { copyPlugin as copyInlineCodePlugin } from './copy-inline-code-view-plugin';
|
||||
|
||||
export default class CopyInlineCodePlugin extends Plugin {
|
||||
|
||||
async onload() {
|
||||
this.registerEditorExtension([copyInlineCodePlugin]);
|
||||
this.registerMarkdownPostProcessor((element, context) => {
|
||||
const inlineCodes = element.querySelectorAll("*:not(pre) > code");
|
||||
|
||||
inlineCodes.forEach(code => {
|
||||
if(code.querySelector('span.copy-to-clipboard-icon')) {
|
||||
return
|
||||
}
|
||||
|
||||
const icon = createSpan({cls: "copy-to-clipboard-icon", text: " 📋"})
|
||||
const textToCopy = code.textContent
|
||||
|
||||
icon.onclick = () => {
|
||||
if(textToCopy) {
|
||||
navigator.clipboard.writeText(textToCopy)
|
||||
new Notice(`Copied '${textToCopy}' to clipboard!`);
|
||||
}
|
||||
}
|
||||
|
||||
code.appendChild(icon)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue