Skip to content

Commit e2c0f57

Browse files
[TASK] Better snippet insertion by Sidebar and Dibba (#211)
#211 * fixed reappearing deleted content on multiple websites; Signed-off-by: Sahil Shubham <[email protected]> * better pasting mechansim; Signed-off-by: Sahil Shubham <[email protected]> * fixed onChange trigger when copying to clipboard; Signed-off-by: Sahil Shubham <[email protected]> * added changeset for this and previous PR Signed-off-by: Sahil Shubham <[email protected]>
1 parent 935f63b commit e2c0f57

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed

.changeset/dull-papayas-lick.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'mexit': minor
3+
'mexit-webapp': patch
4+
---
5+
6+
Better mechanism for pasting snippets. Also screenshot action with cropping.

apps/extension/src/Components/Dibba/index.tsx

+68-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ interface PublicNode {
4747
content: NodeEditorContent
4848
}
4949

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) => {
5159
const text = convertContentToRawText(item.content, '\n')
5260

5361
let html = text
@@ -103,6 +111,21 @@ function getUpcomingData(selection: Selection) {
103111
return { range, text }
104112
}
105113

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+
106129
// TODO: whether or not to enable dibba should be a user's preference
107130
// we don't users to move away because they have doubts of us forcing a 'keylogger' on them
108131
export default function Dibba() {
@@ -205,9 +228,50 @@ export default function Dibba() {
205228
console.log(error)
206229
}
207230

231+
const originMatch = supportedDomains[window.location.origin]
232+
208233
switch (item.type) {
209234
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+
}
211275
break
212276
}
213277
case 'Links': {
@@ -257,6 +321,7 @@ export default function Dibba() {
257321
setDibbaState({ visualState: VisualState.hidden })
258322
} else if (['Tab', 'Enter', ' ', ']'].includes(event.key)) {
259323
event.preventDefault()
324+
event.stopPropagation()
260325

261326
handleClick(results[activeIndex])
262327
}
@@ -356,7 +421,7 @@ export default function Dibba() {
356421
{listItem?.content && (
357422
<ComboSeperator>
358423
<section>
359-
<EditorPreviewRenderer noMouseEvents content={listItem.content} editorId={listItem.id} />
424+
<EditorPreviewRenderer noMouseEvents content={listItem.content} editorId={listItem.id} readOnly={true} />
360425
</section>
361426
</ComboSeperator>
362427
)}

apps/extension/src/Components/Sidebar/SnippetsInfoBar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import useRaju from '../../Hooks/useRaju'
1313
import { useSnippets } from '../../Hooks/useSnippets'
1414
import { useSnippetStore } from '../../Stores/useSnippetStore'
1515
import { getElementById } from '../../contentScript'
16-
import { insertSnippet } from '../Dibba'
16+
import { copySnippetToClipboard } from '../Dibba'
1717
import SnippetCard from './SnippetCard'
1818

1919
export const SnippetsInfoBar = () => {
@@ -31,7 +31,7 @@ export const SnippetsInfoBar = () => {
3131
const onInsertSnippet = (snippetId: string) => {
3232
const snippet = getSnippet(snippetId)
3333

34-
insertSnippet(snippet)
34+
copySnippetToClipboard(snippet)
3535
}
3636

3737
const onSearch = async (newSearchTerm: string) => {

apps/extension/src/Hooks/useActionExecutor.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
/* eslint-disable no-case-declarations */
2-
import { createPlateEditor, createPlateUI, serializeHtml } from '@udecode/plate'
32
import toast from 'react-hot-toast'
43

54
import {
65
ActionType,
76
CategoryType,
8-
convertContentToRawText,
9-
convertToCopySnippet,
107
createNodeWithUid,
11-
defaultCopyConverter,
12-
defaultCopyFilter,
138
ELEMENT_PARAGRAPH,
14-
ELEMENT_TAG,
159
getBlockMetadata,
1610
getNewDraftKey,
1711
ILink,
@@ -22,11 +16,7 @@ import {
2216
SingleNamespace
2317
} from '@mexit/core'
2418

25-
import Action from '../Components/Action'
26-
import { insertSnippet } from '../Components/Dibba'
27-
import { StyledInput } from '../Components/Search/styled'
28-
import { CopyTag } from '../Editor/components/Tags/CopyTag'
29-
import getPlugins from '../Editor/plugins/index'
19+
import { copySnippetToClipboard } from '../Components/Dibba'
3020
import useDataStore from '../Stores/useDataStore'
3121
import { useLayoutStore } from '../Stores/useLayoutStore'
3222
import { useSputlitStore } from '../Stores/useSputlitStore'
@@ -114,7 +104,7 @@ export function useActionExecutor() {
114104

115105
case QuickLinkType.snippet: {
116106
const snippet = getSnippet(item.id)
117-
insertSnippet(snippet)
107+
copySnippetToClipboard(snippet)
118108
setVisualState(VisualState.animatingOut)
119109
break
120110
}

0 commit comments

Comments
 (0)