-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorPreviewRenderer.tsx
153 lines (143 loc) · 4.1 KB
/
EditorPreviewRenderer.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
import {
ComboboxConfig,
ComboboxKey,
ELEMENT_ILINK,
ELEMENT_TAG,
MexEditor,
useDataStore
} from '@workduck-io/mex-editor'
import React, { useEffect } from 'react'
import styled from 'styled-components'
import { useBlockHighlightStore, useFocusBlock } from '../Stores/useFocusBlock'
import { EditorStyles } from '@mexit/shared'
import { ELEMENT_MEDIA_EMBED, ELEMENT_TABLE } from '@udecode/plate'
import { commands } from './Editor/Editor'
import { useEditorChange } from '@mexit/shared'
import { MexEditorOptions } from 'libs/mex-editor/src/lib/types/editor'
interface EditorPreviewRendererProps {
content: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
editorId: string
noStyle?: boolean
/**
* Block that will be focused on render
*/
blockId?: string
noMouseEvents?: boolean
onDoubleClick?: (ev: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
}
const PreviewStyles = styled(EditorStyles)<{ noMouseEvents: boolean }>`
${({ noMouseEvents }) => noMouseEvents && 'pointer-events: none;'};
user-select: none;
font-size: 14px;
`
/* ${TodoContainer}, button, input, textarea, select, option {
pointer-events: none;
} */
const EditorPreviewRenderer = ({
content,
editorId,
blockId,
noStyle,
noMouseEvents,
onDoubleClick
}: EditorPreviewRendererProps) => {
const tags = useDataStore((store) => store.tags)
const addTag = useDataStore((store) => store.addTag)
const ilinks = useDataStore((store) => store.ilinks)
const addILink = useDataStore((store) => store.addILink)
// TODO: remove the need of combobox config if editor is readonly
const comboboxConfig: ComboboxConfig = {
onKeyDownConfig: {
keys: {
ilink: {
// @ts-ignore
slateElementType: ELEMENT_ILINK,
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'
}
}
},
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'
}
}
}
const editorOptions: MexEditorOptions = {
editableProps: {
readOnly: true,
placeholder: "Let's try something here..."
},
focusOptions: {
edge: 'start',
focus: true
},
withBalloonToolbar: false
}
useEditorChange(editorId, content)
// We get memoized plugins
const setHighlights = useBlockHighlightStore((s) => s.setHighlightedBlockIds)
const { selectBlock } = useFocusBlock()
useEffect(() => {
const timeoutId = setTimeout(() => {
if (blockId) {
// mog('editorPreviewRenderer', { blockId, editorId })
selectBlock(blockId, editorId)
setHighlights([blockId], 'preview')
}
}, 300)
return () => {
clearTimeout(timeoutId)
}
}, [blockId, editorId, content])
return (
<PreviewStyles
noMouseEvents={noMouseEvents}
onClick={(ev) => {
// ev.preventDefault()
// ev.stopPropagation()
if (onDoubleClick && ev.detail === 2) {
onDoubleClick(ev)
}
}}
>
<MexEditor comboboxConfig={comboboxConfig} options={editorOptions} editorId={editorId} value={content} />
</PreviewStyles>
)
}
export default EditorPreviewRenderer