-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.tsx
170 lines (158 loc) · 4.49 KB
/
Editor.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
160
161
162
163
164
165
166
167
168
169
170
import React from 'react'
import styled from 'styled-components'
import {
MexEditor,
ELEMENT_ILINK,
ELEMENT_TAG,
ComboboxKey,
ComboboxConfig,
useDataStore
} from '@workduck-io/mex-editor'
import { ELEMENT_MEDIA_EMBED, ELEMENT_TABLE, ELEMENT_LINK, withProps } from '@udecode/plate'
import { MexEditorOptions } from 'libs/mex-editor/src/lib/types/editor'
import { useDebouncedCallback } from 'use-debounce'
import { LinkElement, MediaEmbedElement, TableWrapper } from '@mexit/shared'
import { ILinkElement } from './ILinkElement'
import TagWrapper from './TagWrapper'
import BallonMarkToolbarButtons from './BalloonToolbar/EditorBalloonToolbar'
import { ELEMENT_TODO_LI } from '@mexit/core'
import Todo from '../Todo'
import { useEditorChange } from '@mexit/shared'
import { EditorStyles } from '@mexit/shared'
const EditorWrapper = styled(EditorStyles)`
flex: 1;
max-width: 800px;
margin: 1rem;
padding: 1rem;
`
interface EditorProps {
content: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
nodePath?: string
nodeUID: string
readOnly?: boolean
onChange?: any // eslint-disable-line @typescript-eslint/no-explicit-any
autoFocus?: boolean
}
export const commands = [
{
command: 'table',
text: 'Insert Table',
icon: 'ri:table-line',
type: 'Quick Actions'
},
// {
// command: 'canvas',
// text: 'Insert Drawing canvas',
// icon: 'ri:markup-line',
// type: 'Quick Actions'
// },
{
command: 'webem',
text: 'Insert Web embed',
icon: 'ri:global-line',
type: 'Quick Actions'
}
]
const Editor: React.FC<EditorProps> = ({ nodeUID, nodePath, content, readOnly, onChange, autoFocus }) => {
const tags = useDataStore((store) => store.tags)
const addTag = useDataStore((store) => store.addTag)
const ilinks = useDataStore((store) => store.ilinks)
const addILink = useDataStore((store) => store.addILink)
const comboboxConfig: ComboboxConfig = {
onKeyDownConfig: {
keys: {
ilink: {
slateElementType: ELEMENT_ILINK,
// @ts-expect-error Mex Space requires it in this format
newItemHandler: (ilink: string, parentId?: string) => addILink(ilink, null, parentId)
},
tag: {
slateElementType: ELEMENT_TAG,
newItemHandler: (tag: string) => addTag(tag)
},
slash_command: {
slateElementType: 'slash_command',
newItemHandler: () => undefined
}
},
slashCommands: {
webem: {
slateElementType: ELEMENT_MEDIA_EMBED,
command: 'webem',
options: {
url: 'http://example.com/'
}
},
table: {
slateElementType: ELEMENT_TABLE,
command: 'table'
}
}
},
// TODO: fix the value of ilink, it should be nodeId rather than the path
onChangeConfig: {
ilink: {
cbKey: ComboboxKey.ILINK,
trigger: '[[',
data: ilinks.map((l) => ({ ...l, value: l.path, text: l.path })),
icon: 'add-something-here'
},
tag: {
cbKey: ComboboxKey.TAG,
trigger: '#',
data: tags,
icon: 'ri:hashtag'
},
slash_command: {
cbKey: ComboboxKey.SLASH_COMMAND,
trigger: '/',
data: commands.map((l) => ({ ...l, value: l.command })),
icon: 'ri:flask-line'
}
}
}
useEditorChange(nodeUID, content)
const editorOptions: MexEditorOptions = {
editableProps: {
readOnly: readOnly,
placeholder: "Let's try something here...",
autoFocus: true
},
focusOptions: {
edge: 'start',
focus: true
},
withBalloonToolbar: true
}
const debounced = useDebouncedCallback((value) => {
const f = !readOnly && typeof onChange === 'function' ? onChange : () => undefined
f(value)
}, 1000)
return (
<EditorWrapper>
<MexEditor
comboboxConfig={comboboxConfig}
components={{
[ELEMENT_ILINK]: ILinkElement,
[ELEMENT_TAG]: TagWrapper,
[ELEMENT_MEDIA_EMBED]: MediaEmbedElement,
[ELEMENT_TABLE]: TableWrapper,
[ELEMENT_TODO_LI]: Todo,
[ELEMENT_LINK]: withProps(LinkElement, {
as: 'a'
})
}}
meta={{
path: nodePath
}}
BalloonMarkToolbarButtons={<BallonMarkToolbarButtons />}
// debug
onChange={debounced}
options={editorOptions}
editorId={nodeUID}
value={content}
/>
</EditorWrapper>
)
}
export default Editor