-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidgetMarkdownEditor.tsx
145 lines (136 loc) · 5.75 KB
/
WidgetMarkdownEditor.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
import { ViewerBoxRouter } from '@content-ui/md-mui/ViewerBoxRouter'
import { ContentParser } from '@content-ui/md/parser/ContentParser'
import { ContentSelectionProvider } from '@content-ui/react/ContentSelectionContext'
import { UIMetaReadContextType } from '@ui-schema/ui-schema/UIMetaReadContext'
import React from 'react'
import { TransTitle, WidgetProps, WithScalarValue } from '@ui-schema/ui-schema'
import { SettingsProvider } from '@content-ui/react/LeafSettings'
import Box from '@mui/material/Box'
import FormLabel from '@mui/material/FormLabel'
import { IconButtonTooltip } from '@content-ui/md-mui/MuiComponents/IconButtonTooltip'
import Badge from '@mui/material/Badge'
import IcCheck from '@mui/icons-material/Check'
import IcWarning from '@mui/icons-material/Warning'
import IcEdit from '@mui/icons-material/Edit'
import IcPreview from '@mui/icons-material/Preview'
import { CustomCodeMirror, getHighlight } from '../CustomCodeMirror'
import { ContentInput } from '@content-ui/input/ContentInput'
import { useContentEditor } from '@content-ui/input/useContentEditor'
import { useContent } from '@content-ui/react/useContent'
import { ContentFileProvider } from '@content-ui/react/ContentFileContext'
export const WidgetMarkdownEditor: React.ComponentType<WidgetProps & WithScalarValue & { readOnly?: boolean } & UIMetaReadContextType> = (
{
storeKeys, schema, value,
valid, required, showValidity,
readActive, readDense, readOnly,
onChange,
},
) => {
const [lintWarnings, setLintWarnings] = React.useState<number | null>(0)
const refWarningBox = React.useRef<HTMLDivElement | null>(null)
const [preview, setPreview] = React.useState<boolean | undefined>(undefined)
const onChangeText = React.useCallback(
(newValue: string) =>
onChange({
type: 'set',
storeKeys: storeKeys,
schema: schema,
scopes: ['value'],
data: {
value: newValue,
},
}),
[onChange, storeKeys, schema],
)
const {
textValue,
handleOnChange,
editorSelection,
bigSize, autoProcess, setAutoProcess,
} = useContentEditor(
typeof value === 'string' ? value : '',
onChangeText,
)
const {processing, root, file} = useContent({
textValue,
parseDelay: textValue.length > 10000 ? 460 : 280,
autoProcess,
onMount: true,
processor: ContentParser,
})
const warnings = file?.messages.length
React.useEffect(() => {
setLintWarnings?.(typeof warnings === 'number' ? warnings : null)
}, [warnings, setLintWarnings])
const extensions = React.useMemo(() => {
const highlight = getHighlight('md')
return [
...(highlight ? [highlight] : []),
]
}, [])
const hideTitle = schema?.getIn(['view', 'hideTitle'])
const dense = schema?.getIn(['view', 'dense']) as boolean
return <>
<SettingsProvider
followEditor
headlineLinkable
headlineSelectable headlineSelectableOnHover
headlineOffset={1}
dense={dense || (readActive && readDense)}
>
<Box mb={0.5} style={{display: 'flex', alignItems: 'center'}}>
<FormLabel error={(!valid && showValidity)} style={{marginRight: 'auto'}}>
{hideTitle ? null : <>
<TransTitle storeKeys={storeKeys} schema={schema}/>
{required ? ' *' : null}
</>}
</FormLabel>
<IconButtonTooltip
tooltip={lintWarnings === null ? 'processing text...' : (lintWarnings === 0 ? 'no ' : '') + 'lint warnings'}
onClick={() => setTimeout(() => refWarningBox?.current?.scrollIntoView({behavior: 'smooth'}), 20)}
disabled={lintWarnings === 0}
size={'small'} sx={{mr: 0.5}}
disableInteractive
>
<Badge badgeContent={lintWarnings || 0} hidden={lintWarnings === null} overlap="circular" max={99}>
{lintWarnings === 0 ?
<IcCheck/> :
<IcWarning
color={lintWarnings === null ? 'secondary' : 'warning'}
/>}
</Badge>
</IconButtonTooltip>
<IconButtonTooltip
tooltip={preview ? 'edit' : 'preview'}
onClick={() => setPreview(s => !s)}
size={'small'} sx={{mr: 0.5}}
disableInteractive
>
{preview ? <IcEdit/> : <IcPreview/>}
</IconButtonTooltip>
</Box>
<ContentFileProvider
root={root}
file={file}
>
<ContentSelectionProvider selection={editorSelection}>
<ContentInput
preview={preview}
refWarningBox={refWarningBox}
CodeMirror={CustomCodeMirror}
ViewerBox={ViewerBoxRouter}
onChange={readOnly ? undefined : handleOnChange}
extensions={extensions}
textValue={textValue}
bigSize={bigSize}
processing={processing}
autoProcess={autoProcess}
setAutoProcess={setAutoProcess}
valid={valid}
mb={1}
/>
</ContentSelectionProvider>
</ContentFileProvider>
</SettingsProvider>
</>
}