@@ -2,6 +2,28 @@ import { Command, Extension } from "@tiptap/core";
22import { Plugin } from '@tiptap/pm/state' ;
33import { DOMParser } from '@tiptap/pm/model' ;
44import { __ } from "@/utils/i18n" ;
5+ import { EditorView } from "@tiptap/pm/view" ;
6+
7+ function dispatchCopy ( view : EditorView ) {
8+ const clipboardData = new DataTransfer ( ) ;
9+ const event = new ClipboardEvent ( 'copy' , {
10+ bubbles : true ,
11+ cancelable : true ,
12+ clipboardData,
13+ } ) ;
14+
15+ view . dom . dispatchEvent ( event ) ;
16+
17+ const clipboardItem = new ClipboardItem ( {
18+ 'text/html' : clipboardData . getData ( 'text/html' ) ,
19+ 'text/plain' : clipboardData . getData ( 'text/plain' ) ,
20+ } ) ;
21+
22+ navigator . clipboard . write ( [ clipboardItem ] ) . then ( ( ) => {
23+ } ) . catch ( err => {
24+ alert ( __ ( 'sharp::errors.failed_to_write_to_clipboard' ) ) ;
25+ } ) ;
26+ }
527
628export const Clipboard = Extension . create ( {
729 name : 'clipboard' ,
@@ -36,6 +58,28 @@ export const Clipboard = Extension.create({
3658 }
3759 return html ;
3860 } ,
61+
62+ handleKeyDown ( view , event ) {
63+ // fix bug when copy isn't working in chrome https://github.com/ProseMirror/prosemirror/issues/884
64+ if ( ( event . metaKey || event . ctrlKey ) && event . key === 'c' ) {
65+ let copied = false ;
66+ view . dom . addEventListener ( 'copy' , ( ) => {
67+ copied = true ;
68+ } , { once : true } ) ;
69+ setTimeout ( ( ) => {
70+ if ( ! copied ) {
71+ dispatchCopy ( view ) ;
72+ }
73+ } , 50 ) ;
74+
75+ }
76+ } ,
77+
78+ handleDOMEvents : {
79+ copy ( view , event ) {
80+ console . log ( event ) ;
81+ }
82+ }
3983 } ,
4084 } )
4185 ]
@@ -45,24 +89,7 @@ export const Clipboard = Extension.create({
4589 copyNode : ( pos : number ) : Command => ( { editor, dispatch } ) => {
4690 if ( dispatch ) {
4791 editor . commands . setNodeSelection ( pos ) ;
48- const clipboardData = new DataTransfer ( ) ;
49- const event = new ClipboardEvent ( 'copy' , {
50- bubbles : true ,
51- cancelable : true ,
52- clipboardData,
53- } ) ;
54-
55- editor . view . dom . dispatchEvent ( event ) ;
56-
57- const clipboardItem = new ClipboardItem ( {
58- 'text/html' : clipboardData . getData ( 'text/html' ) ,
59- 'text/plain' : clipboardData . getData ( 'text/plain' ) ,
60- } ) ;
61-
62- navigator . clipboard . write ( [ clipboardItem ] ) . then ( ( ) => {
63- } ) . catch ( err => {
64- alert ( __ ( 'sharp::errors.failed_to_write_to_clipboard' ) ) ;
65- } ) ;
92+ dispatchCopy ( editor . view ) ;
6693 }
6794
6895 return true ;
0 commit comments