5htp-core 0.4.7 → 0.4.8-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -1
- package/src/client/assets/css/components/table.less +2 -0
- package/src/client/assets/css/text/titres.less +1 -1
- package/src/client/assets/css/utils/layouts.less +8 -2
- package/src/client/components/Dialog/Manager.tsx +65 -29
- package/src/client/components/Dialog/card.tsx +3 -1
- package/src/client/components/Dialog/index.less +1 -2
- package/src/client/components/Select/ChoiceSelector.tsx +7 -5
- package/src/client/components/Select/index.tsx +162 -130
- package/src/client/components/index.ts +2 -1
- package/src/client/components/inputv3/Rte/ExampleTheme.tsx +42 -0
- package/src/client/components/inputv3/Rte/ToolbarPlugin.tsx +167 -0
- package/src/client/components/inputv3/Rte/icons/LICENSE.md +5 -0
- package/src/client/components/inputv3/Rte/icons/arrow-clockwise.svg +4 -0
- package/src/client/components/inputv3/Rte/icons/arrow-counterclockwise.svg +4 -0
- package/src/client/components/inputv3/Rte/icons/journal-text.svg +5 -0
- package/src/client/components/inputv3/Rte/icons/justify.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/text-center.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/text-left.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/text-paragraph.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/text-right.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/type-bold.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/type-italic.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/type-strikethrough.svg +3 -0
- package/src/client/components/inputv3/Rte/icons/type-underline.svg +3 -0
- package/src/client/components/inputv3/Rte/index.tsx +163 -0
- package/src/client/components/inputv3/Rte/style.less +428 -0
- package/src/client/components/inputv3/base.less +20 -33
- package/src/client/components/inputv3/base.tsx +36 -2
- package/src/client/components/inputv3/file/index.tsx +11 -5
- package/src/client/components/inputv3/index.tsx +45 -44
- package/src/common/data/rte/index.ts +66 -0
- package/src/common/data/rte/nodes.ts +20 -0
- package/src/common/validation/validators.ts +49 -4
- package/src/server/services/auth/index.ts +0 -2
- package/src/server/services/database/index.ts +1 -1
- package/src/client/components/input/Rte/index.less +0 -13
- package/src/client/components/input/Rte/index.tsx +0 -143
- package/src/client/components/input/Rte/selection.ts +0 -34
- package/src/common/data/rte.tsx +0 -11
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export default {
|
|
9
|
+
code: 'editor-code',
|
|
10
|
+
heading: {
|
|
11
|
+
h1: 'editor-heading-h1',
|
|
12
|
+
h2: 'editor-heading-h2',
|
|
13
|
+
h3: 'editor-heading-h3',
|
|
14
|
+
h4: 'editor-heading-h4',
|
|
15
|
+
h5: 'editor-heading-h5',
|
|
16
|
+
},
|
|
17
|
+
image: 'editor-image',
|
|
18
|
+
link: 'editor-link',
|
|
19
|
+
list: {
|
|
20
|
+
listitem: 'editor-listitem',
|
|
21
|
+
nested: {
|
|
22
|
+
listitem: 'editor-nested-listitem',
|
|
23
|
+
},
|
|
24
|
+
ol: 'editor-list-ol',
|
|
25
|
+
ul: 'editor-list-ul',
|
|
26
|
+
},
|
|
27
|
+
ltr: 'ltr',
|
|
28
|
+
paragraph: 'editor-paragraph',
|
|
29
|
+
placeholder: 'editor-placeholder',
|
|
30
|
+
quote: 'editor-quote',
|
|
31
|
+
rtl: 'rtl',
|
|
32
|
+
text: {
|
|
33
|
+
bold: 'editor-text-bold',
|
|
34
|
+
code: 'editor-text-code',
|
|
35
|
+
hashtag: 'editor-text-hashtag',
|
|
36
|
+
italic: 'editor-text-italic',
|
|
37
|
+
overflowed: 'editor-text-overflowed',
|
|
38
|
+
strikethrough: 'editor-text-strikethrough',
|
|
39
|
+
underline: 'editor-text-underline',
|
|
40
|
+
underlineStrikethrough: 'editor-text-underlineStrikethrough',
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
2
|
+
import { mergeRegister } from '@lexical/utils';
|
|
3
|
+
import {
|
|
4
|
+
$getSelection,
|
|
5
|
+
$isRangeSelection,
|
|
6
|
+
CAN_REDO_COMMAND,
|
|
7
|
+
CAN_UNDO_COMMAND,
|
|
8
|
+
FORMAT_ELEMENT_COMMAND,
|
|
9
|
+
FORMAT_TEXT_COMMAND,
|
|
10
|
+
REDO_COMMAND,
|
|
11
|
+
SELECTION_CHANGE_COMMAND,
|
|
12
|
+
UNDO_COMMAND,
|
|
13
|
+
} from 'lexical';
|
|
14
|
+
|
|
15
|
+
import React from 'react';
|
|
16
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
17
|
+
|
|
18
|
+
const LowPriority = 1;
|
|
19
|
+
|
|
20
|
+
function Divider() {
|
|
21
|
+
return <div className="divider" />;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default function ToolbarPlugin() {
|
|
25
|
+
const [editor] = useLexicalComposerContext();
|
|
26
|
+
const toolbarRef = useRef(null);
|
|
27
|
+
const [canUndo, setCanUndo] = useState(false);
|
|
28
|
+
const [canRedo, setCanRedo] = useState(false);
|
|
29
|
+
const [isBold, setIsBold] = useState(false);
|
|
30
|
+
const [isItalic, setIsItalic] = useState(false);
|
|
31
|
+
const [isUnderline, setIsUnderline] = useState(false);
|
|
32
|
+
const [isStrikethrough, setIsStrikethrough] = useState(false);
|
|
33
|
+
|
|
34
|
+
const $updateToolbar = useCallback(() => {
|
|
35
|
+
const selection = $getSelection();
|
|
36
|
+
if ($isRangeSelection(selection)) {
|
|
37
|
+
// Update text format
|
|
38
|
+
setIsBold(selection.hasFormat('bold'));
|
|
39
|
+
setIsItalic(selection.hasFormat('italic'));
|
|
40
|
+
setIsUnderline(selection.hasFormat('underline'));
|
|
41
|
+
setIsStrikethrough(selection.hasFormat('strikethrough'));
|
|
42
|
+
}
|
|
43
|
+
}, []);
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
return mergeRegister(
|
|
47
|
+
editor.registerUpdateListener(({ editorState }) => {
|
|
48
|
+
editorState.read(() => {
|
|
49
|
+
$updateToolbar();
|
|
50
|
+
});
|
|
51
|
+
}),
|
|
52
|
+
editor.registerCommand(
|
|
53
|
+
SELECTION_CHANGE_COMMAND,
|
|
54
|
+
(_payload, _newEditor) => {
|
|
55
|
+
$updateToolbar();
|
|
56
|
+
return false;
|
|
57
|
+
},
|
|
58
|
+
LowPriority,
|
|
59
|
+
),
|
|
60
|
+
editor.registerCommand(
|
|
61
|
+
CAN_UNDO_COMMAND,
|
|
62
|
+
(payload) => {
|
|
63
|
+
setCanUndo(payload);
|
|
64
|
+
return false;
|
|
65
|
+
},
|
|
66
|
+
LowPriority,
|
|
67
|
+
),
|
|
68
|
+
editor.registerCommand(
|
|
69
|
+
CAN_REDO_COMMAND,
|
|
70
|
+
(payload) => {
|
|
71
|
+
setCanRedo(payload);
|
|
72
|
+
return false;
|
|
73
|
+
},
|
|
74
|
+
LowPriority,
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}, [editor, $updateToolbar]);
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div className="toolbar" ref={toolbarRef}>
|
|
81
|
+
<button
|
|
82
|
+
disabled={!canUndo}
|
|
83
|
+
onClick={() => {
|
|
84
|
+
editor.dispatchCommand(UNDO_COMMAND, undefined);
|
|
85
|
+
}}
|
|
86
|
+
className="toolbar-item spaced"
|
|
87
|
+
aria-label="Undo">
|
|
88
|
+
<i className="format undo" />
|
|
89
|
+
</button>
|
|
90
|
+
<button
|
|
91
|
+
disabled={!canRedo}
|
|
92
|
+
onClick={() => {
|
|
93
|
+
editor.dispatchCommand(REDO_COMMAND, undefined);
|
|
94
|
+
}}
|
|
95
|
+
className="toolbar-item"
|
|
96
|
+
aria-label="Redo">
|
|
97
|
+
<i className="format redo" />
|
|
98
|
+
</button>
|
|
99
|
+
<Divider />
|
|
100
|
+
<button
|
|
101
|
+
onClick={() => {
|
|
102
|
+
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'bold');
|
|
103
|
+
}}
|
|
104
|
+
className={'toolbar-item spaced ' + (isBold ? 'active' : '')}
|
|
105
|
+
aria-label="Format Bold">
|
|
106
|
+
<i className="format bold" />
|
|
107
|
+
</button>
|
|
108
|
+
<button
|
|
109
|
+
onClick={() => {
|
|
110
|
+
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'italic');
|
|
111
|
+
}}
|
|
112
|
+
className={'toolbar-item spaced ' + (isItalic ? 'active' : '')}
|
|
113
|
+
aria-label="Format Italics">
|
|
114
|
+
<i className="format italic" />
|
|
115
|
+
</button>
|
|
116
|
+
<button
|
|
117
|
+
onClick={() => {
|
|
118
|
+
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'underline');
|
|
119
|
+
}}
|
|
120
|
+
className={'toolbar-item spaced ' + (isUnderline ? 'active' : '')}
|
|
121
|
+
aria-label="Format Underline">
|
|
122
|
+
<i className="format underline" />
|
|
123
|
+
</button>
|
|
124
|
+
<button
|
|
125
|
+
onClick={() => {
|
|
126
|
+
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'strikethrough');
|
|
127
|
+
}}
|
|
128
|
+
className={'toolbar-item spaced ' + (isStrikethrough ? 'active' : '')}
|
|
129
|
+
aria-label="Format Strikethrough">
|
|
130
|
+
<i className="format strikethrough" />
|
|
131
|
+
</button>
|
|
132
|
+
<Divider />
|
|
133
|
+
<button
|
|
134
|
+
onClick={() => {
|
|
135
|
+
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'left');
|
|
136
|
+
}}
|
|
137
|
+
className="toolbar-item spaced"
|
|
138
|
+
aria-label="Left Align">
|
|
139
|
+
<i className="format left-align" />
|
|
140
|
+
</button>
|
|
141
|
+
<button
|
|
142
|
+
onClick={() => {
|
|
143
|
+
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'center');
|
|
144
|
+
}}
|
|
145
|
+
className="toolbar-item spaced"
|
|
146
|
+
aria-label="Center Align">
|
|
147
|
+
<i className="format center-align" />
|
|
148
|
+
</button>
|
|
149
|
+
<button
|
|
150
|
+
onClick={() => {
|
|
151
|
+
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'right');
|
|
152
|
+
}}
|
|
153
|
+
className="toolbar-item spaced"
|
|
154
|
+
aria-label="Right Align">
|
|
155
|
+
<i className="format right-align" />
|
|
156
|
+
</button>
|
|
157
|
+
<button
|
|
158
|
+
onClick={() => {
|
|
159
|
+
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'justify');
|
|
160
|
+
}}
|
|
161
|
+
className="toolbar-item"
|
|
162
|
+
aria-label="Justify Align">
|
|
163
|
+
<i className="format justify-align" />
|
|
164
|
+
</button>{' '}
|
|
165
|
+
</div>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"/>
|
|
3
|
+
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"/>
|
|
4
|
+
</svg>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-counterclockwise" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M8 3a5 5 0 1 1-4.546 2.914.5.5 0 0 0-.908-.417A6 6 0 1 0 8 2v1z"/>
|
|
3
|
+
<path d="M8 4.466V.534a.25.25 0 0 0-.41-.192L5.23 2.308a.25.25 0 0 0 0 .384l2.36 1.966A.25.25 0 0 0 8 4.466z"/>
|
|
4
|
+
</svg>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-journal-text" viewBox="0 0 16 16">
|
|
2
|
+
<path d="M5 10.5a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1h-2a.5.5 0 0 1-.5-.5zm0-2a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm0-2a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm0-2a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z"/>
|
|
3
|
+
<path d="M3 0h10a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2v-1h1v1a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H3a1 1 0 0 0-1 1v1H1V2a2 2 0 0 1 2-2z"/>
|
|
4
|
+
<path d="M1 5v-.5a.5.5 0 0 1 1 0V5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1zm0 3v-.5a.5.5 0 0 1 1 0V8h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1zm0 3v-.5a.5.5 0 0 1 1 0v.5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1H1z"/>
|
|
5
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-justify" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M2 12.5a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-center" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M4 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-left" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M2 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-paragraph" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M2 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm4-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-right" viewBox="0 0 16 16">
|
|
2
|
+
<path fill-rule="evenodd" d="M6 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-4-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm4-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-4-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-bold" viewBox="0 0 16 16">
|
|
2
|
+
<path d="M8.21 13c2.106 0 3.412-1.087 3.412-2.823 0-1.306-.984-2.283-2.324-2.386v-.055a2.176 2.176 0 0 0 1.852-2.14c0-1.51-1.162-2.46-3.014-2.46H3.843V13H8.21zM5.908 4.674h1.696c.963 0 1.517.451 1.517 1.244 0 .834-.629 1.32-1.73 1.32H5.908V4.673zm0 6.788V8.598h1.73c1.217 0 1.88.492 1.88 1.415 0 .943-.643 1.449-1.832 1.449H5.907z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-italic" viewBox="0 0 16 16">
|
|
2
|
+
<path d="M7.991 11.674 9.53 4.455c.123-.595.246-.71 1.347-.807l.11-.52H7.211l-.11.52c1.06.096 1.128.212 1.005.807L6.57 11.674c-.123.595-.246.71-1.346.806l-.11.52h3.774l.11-.52c-1.06-.095-1.129-.211-1.006-.806z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-strikethrough" viewBox="0 0 16 16">
|
|
2
|
+
<path d="M6.333 5.686c0 .31.083.581.27.814H5.166a2.776 2.776 0 0 1-.099-.76c0-1.627 1.436-2.768 3.48-2.768 1.969 0 3.39 1.175 3.445 2.85h-1.23c-.11-1.08-.964-1.743-2.25-1.743-1.23 0-2.18.602-2.18 1.607zm2.194 7.478c-2.153 0-3.589-1.107-3.705-2.81h1.23c.144 1.06 1.129 1.703 2.544 1.703 1.34 0 2.31-.705 2.31-1.675 0-.827-.547-1.374-1.914-1.675L8.046 8.5H1v-1h14v1h-3.504c.468.437.675.994.675 1.697 0 1.826-1.436 2.967-3.644 2.967z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-type-underline" viewBox="0 0 16 16">
|
|
2
|
+
<path d="M5.313 3.136h-1.23V9.54c0 2.105 1.47 3.623 3.917 3.623s3.917-1.518 3.917-3.623V3.136h-1.23v6.323c0 1.49-.978 2.57-2.687 2.57-1.709 0-2.687-1.08-2.687-2.57V3.136zM12.5 15h-9v-1h9v1z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/*----------------------------------
|
|
2
|
+
- DEPENDANCES
|
|
3
|
+
----------------------------------*/
|
|
4
|
+
|
|
5
|
+
// Npm
|
|
6
|
+
import React from 'react';
|
|
7
|
+
|
|
8
|
+
import { EditorState, $getRoot } from 'lexical';
|
|
9
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
10
|
+
import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin';
|
|
11
|
+
import { LexicalComposer } from '@lexical/react/LexicalComposer';
|
|
12
|
+
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
|
|
13
|
+
import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
|
|
14
|
+
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
|
|
15
|
+
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
|
|
16
|
+
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
|
|
17
|
+
|
|
18
|
+
// Core libs
|
|
19
|
+
import { useInput, InputBaseProps, InputWrapper } from '../base';
|
|
20
|
+
import editorNodes from '@common/data/rte/nodes';
|
|
21
|
+
|
|
22
|
+
// Special componets
|
|
23
|
+
import ExampleTheme from './ExampleTheme';
|
|
24
|
+
import ToolbarPlugin from './ToolbarPlugin';
|
|
25
|
+
import './style.less';
|
|
26
|
+
|
|
27
|
+
const EMPTY_STATE = '{"root":{"children":[{"children":[],"direction":null,"format":"","indent":0,"type":"paragraph","version":1}],"direction":null,"format":"","indent":0,"type":"root","version":1}}';
|
|
28
|
+
|
|
29
|
+
/*----------------------------------
|
|
30
|
+
- TYPES
|
|
31
|
+
----------------------------------*/
|
|
32
|
+
|
|
33
|
+
export type Props = {
|
|
34
|
+
onPressEnter?: (value: string) => void,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const ValueControlPlugin = ({ props, value }) => {
|
|
38
|
+
|
|
39
|
+
const [editor] = useLexicalComposerContext();
|
|
40
|
+
|
|
41
|
+
React.useEffect(() => {
|
|
42
|
+
if (props.value && props.value !== value) {
|
|
43
|
+
|
|
44
|
+
const initialEditorState = editor.parseEditorState(props.value)
|
|
45
|
+
editor.setEditorState(initialEditorState);
|
|
46
|
+
}
|
|
47
|
+
}, [props.value]);
|
|
48
|
+
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/*----------------------------------
|
|
53
|
+
- COMPOSANT
|
|
54
|
+
----------------------------------*/
|
|
55
|
+
export default (props: Props & InputBaseProps<{}>) => {
|
|
56
|
+
|
|
57
|
+
let {
|
|
58
|
+
// Decoration
|
|
59
|
+
required, size, title, className = '',
|
|
60
|
+
// State
|
|
61
|
+
inputRef, errors,
|
|
62
|
+
// Actions
|
|
63
|
+
onPressEnter
|
|
64
|
+
} = props;
|
|
65
|
+
|
|
66
|
+
/*----------------------------------
|
|
67
|
+
- INIT
|
|
68
|
+
----------------------------------*/
|
|
69
|
+
|
|
70
|
+
const [{ value }, setValue] = useInput(props, EMPTY_STATE, true);
|
|
71
|
+
|
|
72
|
+
// Trigger onchange oly when finished typing
|
|
73
|
+
const refCommit = React.useRef<NodeJS.Timeout | null>(null);
|
|
74
|
+
|
|
75
|
+
className += ' input rte';
|
|
76
|
+
|
|
77
|
+
/*----------------------------------
|
|
78
|
+
- ACTIONS
|
|
79
|
+
----------------------------------*/
|
|
80
|
+
|
|
81
|
+
const onChange = (editorState: EditorState) => {
|
|
82
|
+
editorState.read(() => {
|
|
83
|
+
|
|
84
|
+
const stateJson = editorState.toJSON();
|
|
85
|
+
|
|
86
|
+
if (refCommit.current !== null)
|
|
87
|
+
clearTimeout(refCommit.current);
|
|
88
|
+
|
|
89
|
+
refCommit.current = setTimeout(() => {
|
|
90
|
+
setValue(stateJson);
|
|
91
|
+
}, 100);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/*----------------------------------
|
|
96
|
+
- RENDER
|
|
97
|
+
----------------------------------*/
|
|
98
|
+
return (
|
|
99
|
+
<InputWrapper {...props}>
|
|
100
|
+
<div class={className}>
|
|
101
|
+
|
|
102
|
+
{typeof window !== 'undefined' && (
|
|
103
|
+
<LexicalComposer initialConfig={{
|
|
104
|
+
namespace: 'React.js Demo',
|
|
105
|
+
editorState: value || EMPTY_STATE,
|
|
106
|
+
nodes: editorNodes,
|
|
107
|
+
// Handling of errors during update
|
|
108
|
+
onError(error: Error) { throw error; },
|
|
109
|
+
// The editor theme
|
|
110
|
+
theme: ExampleTheme,
|
|
111
|
+
}}>
|
|
112
|
+
<div className="editor-container">
|
|
113
|
+
<ToolbarPlugin />
|
|
114
|
+
<div className="editor-inner">
|
|
115
|
+
<RichTextPlugin
|
|
116
|
+
contentEditable={
|
|
117
|
+
<ContentEditable
|
|
118
|
+
className="editor-input"
|
|
119
|
+
aria-placeholder={"Type text here ..."}
|
|
120
|
+
placeholder={
|
|
121
|
+
<div className="editor-placeholder">Type text here ...</div>
|
|
122
|
+
}
|
|
123
|
+
/>
|
|
124
|
+
}
|
|
125
|
+
ErrorBoundary={LexicalErrorBoundary}
|
|
126
|
+
/>
|
|
127
|
+
<HistoryPlugin />
|
|
128
|
+
{/* <AutoFocusPlugin /> */}
|
|
129
|
+
<OnChangePlugin onChange={onChange} />
|
|
130
|
+
<ValueControlPlugin props={props} value={value} />
|
|
131
|
+
</div>
|
|
132
|
+
</div>
|
|
133
|
+
</LexicalComposer>
|
|
134
|
+
)}
|
|
135
|
+
|
|
136
|
+
{/* <Tag {...fieldProps}
|
|
137
|
+
|
|
138
|
+
placeholder={props.title}
|
|
139
|
+
|
|
140
|
+
// @ts-ignore: Property 'ref' does not exist on type 'IntrinsicAttributes'
|
|
141
|
+
ref={refInput}
|
|
142
|
+
value={value}
|
|
143
|
+
onFocus={() => setState({ focus: true })}
|
|
144
|
+
onBlur={() => setState({ focus: false })}
|
|
145
|
+
onChange={(e) => updateValue(e.target.value)}
|
|
146
|
+
onKeyDown={(e: KeyboardEvent) => {
|
|
147
|
+
|
|
148
|
+
if (onPressEnter && e.key === 'Enter' && value !== undefined) {
|
|
149
|
+
commitValue();
|
|
150
|
+
onPressEnter(value)
|
|
151
|
+
}
|
|
152
|
+
}}
|
|
153
|
+
/> */}
|
|
154
|
+
|
|
155
|
+
{errors?.length && (
|
|
156
|
+
<div class="bubble bg error bottom">
|
|
157
|
+
{errors.join('. ')}
|
|
158
|
+
</div>
|
|
159
|
+
)}
|
|
160
|
+
</div>
|
|
161
|
+
</InputWrapper>
|
|
162
|
+
)
|
|
163
|
+
}
|