@@ -47,7 +47,15 @@ interface PublicNode {
47
47
content : NodeEditorContent
48
48
}
49
49
50
- export const insertSnippet = async ( item : Snippet ) => {
50
+ // TODO: add more domains and their supported types
51
+ // Also a better matching for the domains
52
+ const supportedDomains : Record < string , 'plain' | 'html' > = {
53
+ 'https://mail.google.com' : 'html' ,
54
+ 'https://www.figma.com' : 'plain' ,
55
+ 'https://keep.google.com' : 'plain'
56
+ }
57
+
58
+ export const copySnippetToClipboard = async ( item : Snippet ) => {
51
59
const text = convertContentToRawText ( item . content , '\n' )
52
60
53
61
let html = text
@@ -103,6 +111,21 @@ function getUpcomingData(selection: Selection) {
103
111
return { range, text }
104
112
}
105
113
114
+ function simulateOnChange ( ) {
115
+ const inputEvent = new InputEvent ( 'input' , {
116
+ bubbles : true ,
117
+ cancelable : false
118
+ } )
119
+
120
+ const changeEvent = new Event ( 'change' , {
121
+ bubbles : true ,
122
+ cancelable : false
123
+ } )
124
+
125
+ document . activeElement . dispatchEvent ( inputEvent )
126
+ document . activeElement . dispatchEvent ( changeEvent )
127
+ }
128
+
106
129
// TODO: whether or not to enable dibba should be a user's preference
107
130
// we don't users to move away because they have doubts of us forcing a 'keylogger' on them
108
131
export default function Dibba ( ) {
@@ -205,9 +228,50 @@ export default function Dibba() {
205
228
console . log ( error )
206
229
}
207
230
231
+ const originMatch = supportedDomains [ window . location . origin ]
232
+
208
233
switch ( item . type ) {
209
234
case QuickLinkType . snippet : {
210
- await insertSnippet ( item as Snippet )
235
+ if ( originMatch === 'html' ) {
236
+ const filterdContent = convertToCopySnippet ( item . content )
237
+ const convertedContent = convertToCopySnippet ( filterdContent , {
238
+ filter : defaultCopyFilter ,
239
+ converter : defaultCopyConverter
240
+ } )
241
+
242
+ const tempEditor = createPlateEditor ( {
243
+ plugins : getPlugins (
244
+ createPlateUI ( {
245
+ [ ELEMENT_TAG ] : CopyTag as any
246
+ } ) ,
247
+ {
248
+ exclude : { dnd : true }
249
+ }
250
+ )
251
+ } )
252
+
253
+ const html = serializeHtml ( tempEditor , {
254
+ nodes : convertedContent
255
+ } )
256
+
257
+ const node = new DOMParser ( ) . parseFromString ( html , 'text/html' ) . body
258
+ dibbaState . extra . range . insertNode ( node )
259
+ dibbaState . extra . range . collapse ( false )
260
+
261
+ // Combining the inserted text node into one
262
+ document . activeElement . normalize ( )
263
+ simulateOnChange ( )
264
+ } else if ( originMatch === 'plain' ) {
265
+ dibbaState . extra . range . insertNode ( document . createTextNode ( parseSnippet ( item ) . text ) )
266
+ dibbaState . extra . range . collapse ( false )
267
+
268
+ // Combining the inserted text node into one
269
+ document . activeElement . normalize ( )
270
+ simulateOnChange ( )
271
+ } else {
272
+ simulateOnChange ( )
273
+ await copySnippetToClipboard ( item as Snippet )
274
+ }
211
275
break
212
276
}
213
277
case 'Links' : {
@@ -257,6 +321,7 @@ export default function Dibba() {
257
321
setDibbaState ( { visualState : VisualState . hidden } )
258
322
} else if ( [ 'Tab' , 'Enter' , ' ' , ']' ] . includes ( event . key ) ) {
259
323
event . preventDefault ( )
324
+ event . stopPropagation ( )
260
325
261
326
handleClick ( results [ activeIndex ] )
262
327
}
@@ -356,7 +421,7 @@ export default function Dibba() {
356
421
{ listItem ?. content && (
357
422
< ComboSeperator >
358
423
< section >
359
- < EditorPreviewRenderer noMouseEvents content = { listItem . content } editorId = { listItem . id } />
424
+ < EditorPreviewRenderer noMouseEvents content = { listItem . content } editorId = { listItem . id } readOnly = { true } />
360
425
</ section >
361
426
</ ComboSeperator >
362
427
) }
0 commit comments