-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippetsInfoBar.tsx
159 lines (136 loc) · 4.82 KB
/
SnippetsInfoBar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { useRef, useState, useEffect } from 'react'
import searchLine from '@iconify/icons-ri/search-line'
import { Icon } from '@iconify/react'
import { createPlateEditor, createPlateUI, serializeHtml } from '@udecode/plate'
import { debounce } from 'lodash'
import toast from 'react-hot-toast'
import { Infobox } from '@workduck-io/mex-components'
import {
convertToCopySnippet,
defaultCopyConverter,
defaultCopyFilter,
ELEMENT_TAG,
mog,
parseSnippet,
Snippet
} from '@mexit/core'
import { SnippetCards, Input, SidebarListFilter, SidebarListFilterWrapper, SnippetSidebarHelp } from '@mexit/shared'
import { CopyTag } from '../../Editor/components/Tags/CopyTag'
import { generateEditorPluginsWithComponents } from '../../Editor/plugins/index'
import useRaju from '../../Hooks/useRaju'
import { useSnippets } from '../../Hooks/useSnippets'
import { useSnippetStore } from '../../Stores/useSnippetStore'
import { copySnippetToClipboard, simulateOnChange, supportedDomains } from '../../Utils/pasteUtils'
import { getElementById } from '../../contentScript'
import SnippetCard from './SnippetCard'
export const SnippetsInfoBar = () => {
const [search, setSearch] = useState('')
const snippets = useSnippetStore((state) => state.snippets)
const getSnippet = useSnippets().getSnippet
const inputRef = useRef<HTMLInputElement>(null)
const { dispatch } = useRaju()
const [searchedSnippets, setSearchedSnippets] = useState<Snippet[]>(snippets)
const onSearchChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
setSearch(e.target.value)
}
const onInsertSnippet = async (snippetId: string) => {
const snippet = getSnippet(snippetId)
const originMatch = supportedDomains[window.location.origin]
if (originMatch) {
toast.loading('Click where you want to insert the snippet')
window.addEventListener(
'click',
(event) => {
const element = event.target
const range = window.getSelection().getRangeAt(0)
mog('check', { element, range })
if (originMatch === 'html') {
const filterdContent = convertToCopySnippet(snippet.content)
const convertedContent = convertToCopySnippet(filterdContent, {
filter: defaultCopyFilter,
converter: defaultCopyConverter
})
const tempEditor = createPlateEditor({
plugins: generateEditorPluginsWithComponents(
createPlateUI({
[ELEMENT_TAG]: CopyTag as any
}),
{
exclude: { dnd: true }
}
)
})
const html = serializeHtml(tempEditor, {
nodes: convertedContent
})
const node = new DOMParser().parseFromString(html, 'text/html').body
range.insertNode(node)
range.collapse(false)
// Combining the inserted text node into one
document.activeElement.normalize()
simulateOnChange()
} else if (originMatch === 'plain') {
range.insertNode(document.createTextNode(parseSnippet(snippet).text))
range.collapse(false)
// Combining the inserted text node into one
document.activeElement.normalize()
simulateOnChange()
}
toast.dismiss()
},
{ once: true }
)
} else {
await copySnippetToClipboard(snippet)
}
}
const onSearch = async (newSearchTerm: string) => {
const res = await dispatch('SEARCH', ['template', 'snippet'], newSearchTerm)
if (newSearchTerm === '' && res.length === 0) {
setSearchedSnippets(snippets)
} else {
const searched = res
.map((r) => {
const snippet = snippets.find((snippet) => snippet.id === r.id)
return snippet
})
.filter((s) => s !== undefined) as Snippet[]
setSearchedSnippets(searched)
}
}
useEffect(() => {
if (search && search !== '') {
onSearch(search)
}
if (search === '') {
setSearchedSnippets(snippets)
}
}, [search, snippets])
return (
<SnippetCards>
<SidebarListFilterWrapper>
<SidebarListFilter>
<Icon icon={searchLine} />
<Input
autoFocus
placeholder={'Search snippets'}
onChange={debounce((e) => onSearchChange(e), 250)}
ref={inputRef}
/>
</SidebarListFilter>
<Infobox
text={SnippetSidebarHelp}
// root={getElementById('ext-side-nav')}
/>
</SidebarListFilterWrapper>
{searchedSnippets?.map((snippet) => (
<SnippetCard
key={snippet?.id}
keyStr={snippet?.id}
snippet={snippet}
onClick={() => onInsertSnippet(snippet.id)}
/>
))}
</SnippetCards>
)
}