Skip to content

Commit 9685a4a

Browse files
committed
2 parents bf0345c + 9300f5b commit 9685a4a

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

custom/MarkdownEditor.vue

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,50 @@ async function uploadFileAndGetMarkdownTag(file: File): Promise<string | undefin
447447
return;
448448
}
449449
}
450+
function getSelectedPlainText(): string {
451+
if (!editor || !model) return '';
452+
453+
const sels = editor.getSelections() || [];
454+
if (!sels.length) return '';
455+
return sels
456+
.map((sel) => model!.getValueInRange(sel))
457+
.join('\n');
458+
}
459+
460+
const onEditorCopy = (e: ClipboardEvent) => {
461+
if (!editor || !model) return;
462+
if (!e.clipboardData) return;
463+
464+
if (!(editor.hasTextFocus?.() || isFocused.value)) return;
465+
466+
const text = getSelectedPlainText();
467+
if (!text) return;
468+
469+
e.clipboardData.setData('text/plain', text);
470+
e.clipboardData.setData('text/html', '');
471+
e.preventDefault();
472+
e.stopPropagation();
473+
};
474+
475+
const onEditorCut = (e: ClipboardEvent) => {
476+
if (!editor || !model) return;
477+
if (!e.clipboardData) return;
478+
if (!(editor.hasTextFocus?.() || isFocused.value)) return;
479+
480+
const text = getSelectedPlainText();
481+
if (!text) return;
482+
483+
e.clipboardData.setData('text/plain', text);
484+
e.clipboardData.setData('text/html', '');
485+
e.preventDefault();
486+
e.stopPropagation();
487+
488+
const sels = editor.getSelections() || [];
489+
editor.executeEdits(
490+
'cut',
491+
sels.map((range) => ({ range, text: '' })),
492+
);
493+
};
450494
451495
onMounted(async () => {
452496
if (!editorContainer.value) return;
@@ -577,7 +621,8 @@ onMounted(async () => {
577621
insertAtCursor(`${markdownTags.join('\n\n')}\n`);
578622
}
579623
};
580-
624+
domNode.addEventListener('copy', onEditorCopy, true);
625+
domNode.addEventListener('cut', onEditorCut, true);
581626
domNode.addEventListener('dragover', onDragOver, true);
582627
domNode.addEventListener('drop', onDrop, true);
583628
removeDragOverListener = () => domNode.removeEventListener('dragover', onDragOver, true);

0 commit comments

Comments
 (0)