mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
feat(ckeditor5/codeblock): implement copy to clipboard function
This commit is contained in:
parent
fc83f67d7c
commit
a77d89f4c7
@ -43,7 +43,29 @@ export class CopyToClipboardEditing extends Plugin {
|
|||||||
export class CopyToClipboardCommand extends Command {
|
export class CopyToClipboardCommand extends Command {
|
||||||
|
|
||||||
execute(...args: Array<unknown>) {
|
execute(...args: Array<unknown>) {
|
||||||
console.log("Copy to clipboard!");
|
const editor = this.editor;
|
||||||
|
const model = editor.model;
|
||||||
|
const selection = model.document.selection;
|
||||||
|
|
||||||
|
const codeBlockEl = selection.getFirstPosition()?.findAncestor("codeBlock");
|
||||||
|
if (!codeBlockEl) {
|
||||||
|
console.warn("Unable to find code block element to copy from.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const codeText = Array.from(codeBlockEl.getChildren())
|
||||||
|
.map(child => "data" in child ? child.data : "\n")
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
if (codeText) {
|
||||||
|
navigator.clipboard.writeText(codeText).then(() => {
|
||||||
|
console.log('Code block copied to clipboard');
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Failed to copy code block', err);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.warn('No code block selected or found.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user