feat: improved clipboard event determination conditions

This commit is contained in:
QiShaoXuan
2022-08-04 19:18:45 +08:00
parent fbe9add924
commit eee90f1a5c
2 changed files with 22 additions and 15 deletions

View File

@@ -17,6 +17,18 @@ import { MarkdownParser } from './markdown-parse';
// todo needs to be a switch
const support_markdown_paste = true;
const filterNodes = ['INPUT', 'SELECT', 'TEXTAREA'];
const shouldHandlerContinue = (event: Event, editor: Editor) => {
if (event.defaultPrevented) {
return false;
}
if (filterNodes.includes((event.target as HTMLElement)?.tagName)) {
return false;
}
return editor.selectionManager.currentSelectInfo.type !== 'None';
};
enum ClipboardAction {
COPY = 'copy',
@@ -72,8 +84,7 @@ class BrowserClipboard {
}
private handle_copy(e: Event) {
//@ts-ignore
if (e.defaultPrevented || e.target.nodeName === 'INPUT') {
if (!shouldHandlerContinue(e, this.editor)) {
return;
}
@@ -84,33 +95,31 @@ class BrowserClipboard {
}
private handle_cut(e: Event) {
//@ts-ignore
if (e.defaultPrevented || e.target.nodeName === 'INPUT') {
if (!shouldHandlerContinue(e, this.editor)) {
return;
}
this.dispatch_clipboard_event(ClipboardAction.CUT, e as ClipboardEvent);
}
private handle_paste(e: Event) {
//@ts-ignore TODO should be handled more scientifically here, whether to trigger the paste time, also need some whitelist mechanism
if (e.defaultPrevented || e.target.nodeName === 'INPUT') {
if (!shouldHandlerContinue(e, this.editor)) {
return;
}
e.stopPropagation();
const clipboardData = (e as ClipboardEvent).clipboardData;
const isPureFile = this.is_pure_file_in_clipboard(clipboardData);
if (!isPureFile) {
this.paste_content(clipboardData);
} else {
if (isPureFile) {
this.paste_file(clipboardData);
} else {
this.paste_content(clipboardData);
}
// this.editor.selectionManager
// .getSelectInfo()
// .then(selectionInfo => console.log(selectionInfo));
e.stopPropagation();
}
private paste_content(clipboardData: any) {

View File

@@ -115,10 +115,8 @@ export default class ClipboardParse {
const block_utils = this.editor.getView(
ClipboardParse.block_types[i]
);
const blocks =
block_utils &&
block_utils.html2block &&
block_utils.html2block(el, this.parse_dom);
const blocks = block_utils?.html2block?.(el, this.parse_dom);
if (blocks) {
return blocks;
}