/
RichTextEditor.story.tsx
307 lines (271 loc) · 8.51 KB
/
RichTextEditor.story.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import React from 'react';
import { useEditor, FloatingMenu, BubbleMenu } from '@tiptap/react';
import { AppShell, Header } from '@mantine/core';
import Highlight from '@tiptap/extension-highlight';
import { Color } from '@tiptap/extension-color';
import TextStyle from '@tiptap/extension-text-style';
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
import StarterKit from '@tiptap/starter-kit';
import Underline from '@tiptap/extension-underline';
import TextAlign from '@tiptap/extension-text-align';
import Superscript from '@tiptap/extension-superscript';
import SubScript from '@tiptap/extension-subscript';
import Placeholder from '@tiptap/extension-placeholder';
import { lowlight } from 'lowlight';
import css from 'highlight.js/lib/languages/css';
import js from 'highlight.js/lib/languages/javascript';
import ts from 'highlight.js/lib/languages/typescript';
import html from 'highlight.js/lib/languages/xml';
import { IconColorPicker } from '@tabler/icons';
import { Link } from './extensions/Link';
import { RichTextEditor, RichTextEditorProps } from './RichTextEditor';
import { RichTextEditorToolbarProps } from './Toolbar/Toolbar';
lowlight.registerLanguage('html', html);
lowlight.registerLanguage('css', css);
lowlight.registerLanguage('js', js);
lowlight.registerLanguage('ts', ts);
export default { title: 'TipTap' };
const lorem =
'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Veritatis necessitatibus, impedit tempora, provident voluptate aliquid eos eveniet expedita iusto mollitia accusamus illum sunt fugiat quisquam tempore molestias nulla voluptatem cumque!';
const htmlContent = Array.from({ length: 10 }).fill(`<p>${lorem}</p>`).join('');
function BasicEditor({
editorProps,
content,
toolbarProps,
}: {
editorProps?: Partial<RichTextEditorProps>;
content?: string;
toolbarProps?: Partial<RichTextEditorToolbarProps>;
}) {
const editor = useEditor({
extensions: [
StarterKit,
Underline,
Link,
Superscript,
SubScript,
TextStyle,
Color,
Highlight,
TextAlign.configure({
types: ['heading', 'paragraph'],
}),
],
content: content || htmlContent,
});
return (
<div style={{ padding: 40 }}>
<RichTextEditor editor={editor} {...editorProps}>
<RichTextEditor.Toolbar {...toolbarProps}>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Blockquote />
<RichTextEditor.Bold />
<RichTextEditor.Italic />
<RichTextEditor.Underline />
<RichTextEditor.Strikethrough />
<RichTextEditor.ClearFormatting />
<RichTextEditor.Highlight />
<RichTextEditor.ColorPicker
colors={[
'#25262b',
'#868e96',
'#fa5252',
'#e64980',
'#be4bdb',
'#7950f2',
'#4c6ef5',
'#228be6',
'#15aabf',
'#12b886',
'#40c057',
'#82c91e',
'#fab005',
'#fd7e14',
]}
/>
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.H1 />
<RichTextEditor.H2 />
<RichTextEditor.H3 />
<RichTextEditor.H4 />
<RichTextEditor.H5 />
<RichTextEditor.H6 />
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.BulletList />
<RichTextEditor.OrderedList />
<RichTextEditor.Hr />
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Link />
<RichTextEditor.Unlink />
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.AlignLeft />
<RichTextEditor.AlignCenter />
<RichTextEditor.AlignJustify />
<RichTextEditor.AlignRight />
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Subscript />
<RichTextEditor.Superscript />
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Code />
<RichTextEditor.CodeBlock />
</RichTextEditor.ControlsGroup>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Control interactive={false}>
<IconColorPicker size={16} stroke={1.5} />
</RichTextEditor.Control>
<RichTextEditor.Color color="#F03E3E" />
<RichTextEditor.Color color="#7048E8" />
<RichTextEditor.Color color="#1098AD" />
<RichTextEditor.Color color="#37B24D" />
<RichTextEditor.Color color="#F59F00" />
<RichTextEditor.UnsetColor />
</RichTextEditor.ControlsGroup>
</RichTextEditor.Toolbar>
<RichTextEditor.Content />
</RichTextEditor>
</div>
);
}
export function Usage() {
return <BasicEditor />;
}
function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
const code =
escapeHtml(`// Valid braces Kata – https://www.codewars.com/kata/5277c8a221e209d3f6000b56
const pairs: Record<string, string> = {
'[': ']',
'{': '}',
'(': ')',
};
const openBraces = Object.keys(pairs);
export function validBraces(braces: string) {
const opened: string[] = [];
for (let i = 0; i < braces.length; i += 1) {
const brace = braces[i];
if (openBraces.includes(brace)) {
opened.push(brace);
continue;
}
if (pairs[opened[opened.length - 1]] !== brace) {
return false
}
opened.pop();
}
return opened.length === 0;
}`);
export function CodeHighlight() {
const editor = useEditor({
extensions: [
StarterKit,
CodeBlockLowlight.configure({
lowlight,
}),
],
content: `<p>
That’s a boring paragraph followed by a fenced code block:
</p>
<pre><code>${code}</code></pre>
<p>
Press Command/Ctrl + Enter to leave the fenced code block and continue typing in boring paragraphs.
</p>`,
});
return (
<div style={{ padding: 40 }}>
<RichTextEditor editor={editor}>
<RichTextEditor.Toolbar>
<RichTextEditor.ControlsGroup>
<RichTextEditor.CodeBlock />
</RichTextEditor.ControlsGroup>
</RichTextEditor.Toolbar>
<RichTextEditor.Content />
</RichTextEditor>
</div>
);
}
const largeContent = Array(20)
.fill(0)
.map(() => `<p>${lorem}</p>`)
.join('');
const scrollContent = Array(10)
.fill(0)
.map((_, index) => <p key={index}>{lorem}</p>);
export function StickyToolbar() {
return (
<AppShell header={<Header height={60}>Header</Header>}>
{scrollContent}
<BasicEditor content={largeContent} toolbarProps={{ sticky: true, stickyOffset: 60 }} />
{scrollContent}
{scrollContent}
{scrollContent}
</AppShell>
);
}
export function Floating() {
const editor = useEditor({
extensions: [StarterKit],
content: '<p>Hello World!</p>',
});
return (
<RichTextEditor editor={editor}>
{editor && (
<FloatingMenu editor={editor}>
<RichTextEditor.ControlsGroup>
<RichTextEditor.H1 />
<RichTextEditor.H2 />
<RichTextEditor.BulletList />
</RichTextEditor.ControlsGroup>
</FloatingMenu>
)}
<RichTextEditor.Content />
</RichTextEditor>
);
}
export function Bubble() {
const editor = useEditor({
extensions: [StarterKit, Link],
content: '<p>Hello World!</p>',
});
return (
<RichTextEditor editor={editor}>
{editor && (
<BubbleMenu editor={editor}>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Bold />
<RichTextEditor.Italic />
<RichTextEditor.Link />
</RichTextEditor.ControlsGroup>
</BubbleMenu>
)}
<RichTextEditor.Content />
</RichTextEditor>
);
}
export function Unstyled() {
return <BasicEditor editorProps={{ unstyled: true }} />;
}
export function PlaceholderExtension() {
const editor = useEditor({
extensions: [StarterKit, Placeholder.configure({ placeholder: 'This is placeholder' })],
content: '',
});
return (
<div style={{ padding: 40 }}>
<RichTextEditor editor={editor}>
<RichTextEditor.Content />
</RichTextEditor>
</div>
);
}