fluentui-tiptap-editor 1.0.0
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/README.md +125 -0
- package/lib/EditorStyle.css +1019 -0
- package/lib/TiptapEditor.d.ts +67 -0
- package/lib/TiptapEditor.js +866 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/package.json +77 -0
|
@@ -0,0 +1,866 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { DefaultButton, PrimaryButton } from '@fluentui/react';
|
|
3
|
+
import { Button, Checkbox, Field, Input, Popover, PopoverSurface, PopoverTrigger, Toolbar, ToolbarButton, Tooltip } from '@fluentui/react-components';
|
|
4
|
+
import { ArrowDownloadRegular, ArrowLeftRegular, ArrowRightRegular, ArrowUploadRegular, ChevronDownRegular, DeleteRegular, DocumentPageBreakRegular, HighlightRegular, LinkDismissRegular, LinkRegular, PaintBrushRegular, TableRegular, TextAlignCenterRegular, TextAlignJustifyRegular, TextAlignLeftRegular, TextAlignRightRegular, TextBoldRegular, TextBulletListRegular, TextCaseLowercaseRegular, TextCaseUppercaseRegular, TextColorRegular, TextItalicRegular, TextNumberListLtrRegular, TextQuoteRegular, TextStrikethroughRegular, TextSubscriptRegular, TextSuperscriptRegular, TextUnderlineRegular, } from '@fluentui/react-icons';
|
|
5
|
+
import { Extension, mergeAttributes, Node } from '@tiptap/core';
|
|
6
|
+
import FontFamily from '@tiptap/extension-font-family';
|
|
7
|
+
import Highlight from '@tiptap/extension-highlight';
|
|
8
|
+
import Image from '@tiptap/extension-image';
|
|
9
|
+
import Link from '@tiptap/extension-link';
|
|
10
|
+
import Placeholder from '@tiptap/extension-placeholder';
|
|
11
|
+
import Subscript from '@tiptap/extension-subscript';
|
|
12
|
+
import Superscript from '@tiptap/extension-superscript';
|
|
13
|
+
import { TableKit } from '@tiptap/extension-table';
|
|
14
|
+
import TextAlign from '@tiptap/extension-text-align';
|
|
15
|
+
import { Color, TextStyleKit } from '@tiptap/extension-text-style';
|
|
16
|
+
import Underline from '@tiptap/extension-underline';
|
|
17
|
+
import { EditorContent, useEditor, useEditorState } from '@tiptap/react';
|
|
18
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
19
|
+
import { DOMParser as PMDOMParser } from 'prosemirror-model';
|
|
20
|
+
import { Plugin } from 'prosemirror-state';
|
|
21
|
+
import { forwardRef, memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
22
|
+
import './EditorStyle.css';
|
|
23
|
+
export var ContentEditorLevel;
|
|
24
|
+
(function (ContentEditorLevel) {
|
|
25
|
+
ContentEditorLevel["None"] = "none";
|
|
26
|
+
ContentEditorLevel["Basic"] = "basic";
|
|
27
|
+
ContentEditorLevel["Standard"] = "standard";
|
|
28
|
+
ContentEditorLevel["Pro"] = "pro";
|
|
29
|
+
})(ContentEditorLevel || (ContentEditorLevel = {}));
|
|
30
|
+
const HEADING_OPTIONS = {
|
|
31
|
+
paragraph: 'Normal',
|
|
32
|
+
heading1: 'Heading 1',
|
|
33
|
+
heading2: 'Heading 2',
|
|
34
|
+
heading3: 'Heading 3',
|
|
35
|
+
heading4: 'Heading 4',
|
|
36
|
+
heading5: 'Heading 5',
|
|
37
|
+
heading6: 'Heading 6',
|
|
38
|
+
};
|
|
39
|
+
const FONT_OPTIONS = ['Arial', 'Courier New', 'Comic Sans MS', 'Georgia', 'Times New Roman', 'Verdana', 'Lucida Serif', 'Trebuchet MS', 'Tahoma'];
|
|
40
|
+
const FONT_SIZE_OPTIONS = Array.from({ length: 69 }, (_, i) => `${i + 8}px`);
|
|
41
|
+
const BlockWrapper = Node.create({
|
|
42
|
+
name: 'blockWrapper',
|
|
43
|
+
group: 'block',
|
|
44
|
+
content: 'block+',
|
|
45
|
+
defining: true,
|
|
46
|
+
addAttributes() {
|
|
47
|
+
return {
|
|
48
|
+
'data-block': {
|
|
49
|
+
default: null,
|
|
50
|
+
parseHTML: (element) => element.getAttribute('data-block'),
|
|
51
|
+
renderHTML: (attrs) => (attrs['data-block'] ? { 'data-block': attrs['data-block'] } : {}),
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
parseHTML() {
|
|
56
|
+
return [{ tag: 'div[data-block]' }];
|
|
57
|
+
},
|
|
58
|
+
renderHTML({ HTMLAttributes }) {
|
|
59
|
+
return ['div', mergeAttributes(HTMLAttributes), 0];
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
const TextTransformExtension = Extension.create({
|
|
63
|
+
name: 'textTransform',
|
|
64
|
+
addOptions() {
|
|
65
|
+
return {
|
|
66
|
+
types: ['textStyle'],
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
addGlobalAttributes() {
|
|
70
|
+
return [
|
|
71
|
+
{
|
|
72
|
+
types: this.options.types,
|
|
73
|
+
attributes: {
|
|
74
|
+
textTransform: {
|
|
75
|
+
default: null,
|
|
76
|
+
parseHTML: (element) => element.style.textTransform?.replace(/['"]+/g, '') || null,
|
|
77
|
+
renderHTML: (attributes) => {
|
|
78
|
+
if (!attributes.textTransform)
|
|
79
|
+
return {};
|
|
80
|
+
return {
|
|
81
|
+
style: `text-transform: ${attributes.textTransform}`,
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
const getWordCount = (text) => {
|
|
91
|
+
const trimmed = text.trim();
|
|
92
|
+
if (!trimmed)
|
|
93
|
+
return 0;
|
|
94
|
+
return trimmed.split(/\s+/).length;
|
|
95
|
+
};
|
|
96
|
+
const WordLimitExtension = Extension.create({
|
|
97
|
+
name: 'characterLimit',
|
|
98
|
+
addOptions() {
|
|
99
|
+
return {
|
|
100
|
+
limit: null,
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
addProseMirrorPlugins() {
|
|
104
|
+
const extension = this;
|
|
105
|
+
return [
|
|
106
|
+
new Plugin({
|
|
107
|
+
filterTransaction: (tr, state) => {
|
|
108
|
+
const limit = extension.options.limit;
|
|
109
|
+
if (!limit)
|
|
110
|
+
return true;
|
|
111
|
+
if (!tr.docChanged)
|
|
112
|
+
return true;
|
|
113
|
+
const oldText = state.doc.textContent;
|
|
114
|
+
const newText = tr.doc.textContent;
|
|
115
|
+
const oldWordCount = getWordCount(oldText);
|
|
116
|
+
const newWordCount = getWordCount(newText);
|
|
117
|
+
// Allow delete/backspace/replacements reducing count
|
|
118
|
+
if (newWordCount <= oldWordCount) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
// Block new input after limit
|
|
122
|
+
if (newWordCount > limit) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
];
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const EDITOR_EXTENSIONS = [
|
|
132
|
+
StarterKit,
|
|
133
|
+
TextStyleKit.configure({ types: ['textStyle'] }),
|
|
134
|
+
TextTransformExtension,
|
|
135
|
+
WordLimitExtension,
|
|
136
|
+
FontFamily,
|
|
137
|
+
Subscript,
|
|
138
|
+
Superscript,
|
|
139
|
+
Color,
|
|
140
|
+
Underline,
|
|
141
|
+
Image.configure({ allowBase64: true }),
|
|
142
|
+
TableKit.configure({ table: { resizable: true } }),
|
|
143
|
+
Link.configure({ openOnClick: false, autolink: true }),
|
|
144
|
+
Highlight.configure({ multicolor: true }),
|
|
145
|
+
TextAlign.configure({ types: ['heading', 'paragraph'] }),
|
|
146
|
+
BlockWrapper,
|
|
147
|
+
];
|
|
148
|
+
// Helper function for colors
|
|
149
|
+
function cpHexToRgb(hex) {
|
|
150
|
+
let h = hex.replace('#', '');
|
|
151
|
+
if (h.length === 3)
|
|
152
|
+
h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
|
|
153
|
+
if (h.length !== 6 || !/^[0-9a-fA-F]{6}$/.test(h))
|
|
154
|
+
return [0, 0, 0];
|
|
155
|
+
const n = parseInt(h, 16);
|
|
156
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
157
|
+
}
|
|
158
|
+
function cpRgbToHex(r, g, b) {
|
|
159
|
+
return ('#' +
|
|
160
|
+
[r, g, b]
|
|
161
|
+
.map((v) => Math.max(0, Math.min(255, Math.round(v)))
|
|
162
|
+
.toString(16)
|
|
163
|
+
.padStart(2, '0'))
|
|
164
|
+
.join(''));
|
|
165
|
+
}
|
|
166
|
+
function cpRgbToHsv(r, g, b) {
|
|
167
|
+
r /= 255;
|
|
168
|
+
g /= 255;
|
|
169
|
+
b /= 255;
|
|
170
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min;
|
|
171
|
+
let h = 0;
|
|
172
|
+
if (d > 0) {
|
|
173
|
+
if (max === r)
|
|
174
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
175
|
+
else if (max === g)
|
|
176
|
+
h = (b - r) / d + 2;
|
|
177
|
+
else
|
|
178
|
+
h = (r - g) / d + 4;
|
|
179
|
+
h *= 60;
|
|
180
|
+
}
|
|
181
|
+
return [h, max === 0 ? 0 : d / max, max];
|
|
182
|
+
}
|
|
183
|
+
function cpHsvToRgb(h, s, v) {
|
|
184
|
+
const c = v * s, x = c * (1 - Math.abs(((h / 60) % 2) - 1)), m = v - c;
|
|
185
|
+
let r = 0, g = 0, b = 0;
|
|
186
|
+
if (h < 60) {
|
|
187
|
+
r = c;
|
|
188
|
+
g = x;
|
|
189
|
+
}
|
|
190
|
+
else if (h < 120) {
|
|
191
|
+
r = x;
|
|
192
|
+
g = c;
|
|
193
|
+
}
|
|
194
|
+
else if (h < 180) {
|
|
195
|
+
g = c;
|
|
196
|
+
b = x;
|
|
197
|
+
}
|
|
198
|
+
else if (h < 240) {
|
|
199
|
+
g = x;
|
|
200
|
+
b = c;
|
|
201
|
+
}
|
|
202
|
+
else if (h < 300) {
|
|
203
|
+
r = x;
|
|
204
|
+
b = c;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
r = c;
|
|
208
|
+
b = x;
|
|
209
|
+
}
|
|
210
|
+
return [Math.round((r + m) * 255), Math.round((g + m) * 255), Math.round((b + m) * 255)];
|
|
211
|
+
}
|
|
212
|
+
const CP_VALID_HEX = (h) => /^#[0-9a-fA-F]{6}$/.test(h);
|
|
213
|
+
const CP_PRESETS = [
|
|
214
|
+
'#000000', '#434343', '#666666', '#999999', '#b7b7b7', '#cccccc', '#d9d9d9', '#ffffff', '#980000',
|
|
215
|
+
'#ff0000', '#ff9900', '#ffff00', '#00ff00', '#00ffff', '#4a86e8', '#0000ff', '#9900ff', '#ff00ff'
|
|
216
|
+
];
|
|
217
|
+
function findBlockPositions(editor, kind) {
|
|
218
|
+
const result = [];
|
|
219
|
+
editor.state.doc.forEach((node, offset) => {
|
|
220
|
+
if (node.type.name === 'blockWrapper' && node.attrs['data-block'] === kind) {
|
|
221
|
+
result.push({ pos: offset, size: node.nodeSize });
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
function getTableDimensions(editor) {
|
|
227
|
+
const { state } = editor;
|
|
228
|
+
let rows = 0;
|
|
229
|
+
let cols = 0;
|
|
230
|
+
state.doc.nodesBetween(state.selection.from, state.selection.to, (node) => {
|
|
231
|
+
if (node.type.name === 'table') {
|
|
232
|
+
rows = node.childCount;
|
|
233
|
+
if (rows > 0) {
|
|
234
|
+
cols = node.child(0).childCount;
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
return { rows, cols };
|
|
240
|
+
}
|
|
241
|
+
function parseHTMLToNode(editor, html) {
|
|
242
|
+
const container = document.createElement('div');
|
|
243
|
+
container.innerHTML = html;
|
|
244
|
+
return PMDOMParser.fromSchema(editor.schema).parse(container);
|
|
245
|
+
}
|
|
246
|
+
const TiptapEditor = forwardRef(({ content, onChange, onChangeJSON, onFocus, onBlur, readOnly = false, autoFocus = false, placeholder, className, width, height, margin, contentHeight, level = ContentEditorLevel.Standard, wordLimit, onWordLimitExceeded, errorMessage, }, ref) => {
|
|
247
|
+
const [charCount, setCharCount] = useState(0);
|
|
248
|
+
const [wordCount, setWordCount] = useState(0);
|
|
249
|
+
const [isEmpty, setIsEmpty] = useState(true);
|
|
250
|
+
const previousOverLimitRef = useRef(false);
|
|
251
|
+
const handleUpdate = useCallback(({ editor }) => {
|
|
252
|
+
onChange?.(editor.getHTML());
|
|
253
|
+
onChangeJSON?.(editor.getJSON());
|
|
254
|
+
const text = editor.getText();
|
|
255
|
+
setCharCount(text.length);
|
|
256
|
+
setWordCount(getWordCount(text));
|
|
257
|
+
setIsEmpty(editor.isEmpty);
|
|
258
|
+
}, [onChange, onChangeJSON]);
|
|
259
|
+
const isAutoFocus = autoFocus === true;
|
|
260
|
+
const editor = useEditor({
|
|
261
|
+
extensions: [
|
|
262
|
+
...EDITOR_EXTENSIONS,
|
|
263
|
+
Placeholder.configure({
|
|
264
|
+
placeholder: ({ editor }) => {
|
|
265
|
+
return editor.customPlaceholder || 'Type something...';
|
|
266
|
+
},
|
|
267
|
+
}),
|
|
268
|
+
],
|
|
269
|
+
content,
|
|
270
|
+
editable: !readOnly,
|
|
271
|
+
autofocus: isAutoFocus ? 'start' : false,
|
|
272
|
+
onUpdate: handleUpdate,
|
|
273
|
+
onFocus: () => onFocus?.(),
|
|
274
|
+
onBlur: () => onBlur?.(),
|
|
275
|
+
onCreate: ({ editor }) => {
|
|
276
|
+
editor.customPlaceholder = placeholder;
|
|
277
|
+
const text = editor.getText();
|
|
278
|
+
setCharCount(text.length);
|
|
279
|
+
setWordCount(getWordCount(text));
|
|
280
|
+
setIsEmpty(editor.isEmpty);
|
|
281
|
+
if (isAutoFocus && !readOnly) {
|
|
282
|
+
requestAnimationFrame(() => {
|
|
283
|
+
editor.commands.focus('start');
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
editorProps: {
|
|
288
|
+
attributes: {
|
|
289
|
+
class: 'tiptap-prosemirror',
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
immediatelyRender: false,
|
|
293
|
+
});
|
|
294
|
+
useEffect(() => {
|
|
295
|
+
editor?.setEditable(!readOnly);
|
|
296
|
+
}, [editor, readOnly]);
|
|
297
|
+
useEffect(() => {
|
|
298
|
+
if (!editor || readOnly)
|
|
299
|
+
return;
|
|
300
|
+
const isAutoFocusProp = autoFocus === true;
|
|
301
|
+
if (isAutoFocusProp) {
|
|
302
|
+
requestAnimationFrame(() => {
|
|
303
|
+
if (!editor.isFocused) {
|
|
304
|
+
editor.commands.focus('start');
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
requestAnimationFrame(() => {
|
|
310
|
+
if (editor.isFocused) {
|
|
311
|
+
editor.commands.blur();
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}, [editor, autoFocus, readOnly]);
|
|
316
|
+
useEffect(() => {
|
|
317
|
+
if (!editor || content === undefined)
|
|
318
|
+
return;
|
|
319
|
+
const currentHTML = editor.getHTML();
|
|
320
|
+
if (currentHTML !== content) {
|
|
321
|
+
const isPropEmpty = content === '' || content === '<p></p>';
|
|
322
|
+
const isEditorEmpty = currentHTML === '' || currentHTML === '<p></p>';
|
|
323
|
+
const text = editor.getText();
|
|
324
|
+
if (isPropEmpty && isEditorEmpty) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
editor.commands.setContent(content, { emitUpdate: false });
|
|
328
|
+
setCharCount(text.length);
|
|
329
|
+
setWordCount(getWordCount(text));
|
|
330
|
+
setIsEmpty(editor.isEmpty);
|
|
331
|
+
}
|
|
332
|
+
}, [editor, content]);
|
|
333
|
+
useEffect(() => {
|
|
334
|
+
if (!editor)
|
|
335
|
+
return;
|
|
336
|
+
editor.customPlaceholder = placeholder;
|
|
337
|
+
editor.view.dispatch(editor.state.tr);
|
|
338
|
+
}, [editor, placeholder]);
|
|
339
|
+
useEffect(() => {
|
|
340
|
+
if (!editor)
|
|
341
|
+
return;
|
|
342
|
+
const limitExtension = editor.extensionManager.extensions.find((ext) => ext.name === 'characterLimit');
|
|
343
|
+
if (limitExtension) {
|
|
344
|
+
limitExtension.options.limit = wordLimit ?? null;
|
|
345
|
+
}
|
|
346
|
+
}, [editor, wordLimit]);
|
|
347
|
+
const isOverLimit = wordLimit !== undefined &&
|
|
348
|
+
wordCount > wordLimit;
|
|
349
|
+
useEffect(() => {
|
|
350
|
+
if (wordLimit === undefined || !onWordLimitExceeded)
|
|
351
|
+
return;
|
|
352
|
+
const wasOverLimit = previousOverLimitRef.current;
|
|
353
|
+
if (isOverLimit !== wasOverLimit) {
|
|
354
|
+
onWordLimitExceeded({
|
|
355
|
+
wordCount,
|
|
356
|
+
wordLimit,
|
|
357
|
+
exceeded: isOverLimit,
|
|
358
|
+
});
|
|
359
|
+
previousOverLimitRef.current = isOverLimit;
|
|
360
|
+
}
|
|
361
|
+
}, [isOverLimit, charCount, wordLimit, onWordLimitExceeded]);
|
|
362
|
+
const getWordCount = (text) => {
|
|
363
|
+
const trimmed = text.trim();
|
|
364
|
+
if (!trimmed)
|
|
365
|
+
return 0;
|
|
366
|
+
return trimmed.split(/\s+/).length;
|
|
367
|
+
};
|
|
368
|
+
useImperativeHandle(ref, () => ({
|
|
369
|
+
getHTML: () => editor?.getHTML() ?? '',
|
|
370
|
+
setValue: (html) => editor?.commands.setContent(html),
|
|
371
|
+
getJSON: () => editor?.getJSON() ?? { type: 'doc', content: [] },
|
|
372
|
+
setJSON: (json) => editor?.commands.setContent(json),
|
|
373
|
+
getText: () => editor?.getText() ?? '',
|
|
374
|
+
isEmpty: () => editor?.isEmpty ?? true,
|
|
375
|
+
clear: () => editor?.commands.clearContent(true),
|
|
376
|
+
insertHTML: (html) => editor?.chain().focus().insertContent(html).run(),
|
|
377
|
+
focus: () => editor?.commands.focus(),
|
|
378
|
+
blur: () => editor?.commands.blur(),
|
|
379
|
+
undo: () => editor?.chain().focus().undo().run(),
|
|
380
|
+
redo: () => editor?.chain().focus().redo().run(),
|
|
381
|
+
toggleBold: () => editor?.chain().focus().toggleBold().run(),
|
|
382
|
+
toggleItalic: () => editor?.chain().focus().toggleItalic().run(),
|
|
383
|
+
toggleUnderline: () => editor?.chain().focus().toggleUnderline().run(),
|
|
384
|
+
toggleStrike: () => editor?.chain().focus().toggleStrike().run(),
|
|
385
|
+
toggleHighlight: (color = '#fef08a') => editor?.chain().focus().toggleHighlight({ color }).run(),
|
|
386
|
+
setHeading: (level) => {
|
|
387
|
+
if (!editor)
|
|
388
|
+
return;
|
|
389
|
+
level === 0
|
|
390
|
+
? editor.chain().focus().setParagraph().run()
|
|
391
|
+
: editor
|
|
392
|
+
.chain()
|
|
393
|
+
.focus()
|
|
394
|
+
.setHeading({ level: level })
|
|
395
|
+
.run();
|
|
396
|
+
},
|
|
397
|
+
setFontFamily: (font) => editor?.chain().focus().setFontFamily(font).run(),
|
|
398
|
+
setFontSize: (size) => editor?.chain().focus().setMark('textStyle', { fontSize: size }).run(),
|
|
399
|
+
setTextColor: (color) => editor?.chain().focus().setColor(color).run(),
|
|
400
|
+
setTextAlign: (align) => editor?.chain().focus().setTextAlign(align).run(),
|
|
401
|
+
transformText: (transform) => {
|
|
402
|
+
if (!editor)
|
|
403
|
+
return;
|
|
404
|
+
const { from, to } = editor.state.selection;
|
|
405
|
+
editor.commands.command(({ tr, state }) => {
|
|
406
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
407
|
+
if (!node.isText)
|
|
408
|
+
return;
|
|
409
|
+
let text = node.text ?? '';
|
|
410
|
+
if (transform === 'uppercase')
|
|
411
|
+
text = text.toUpperCase();
|
|
412
|
+
else if (transform === 'lowercase')
|
|
413
|
+
text = text.toLowerCase();
|
|
414
|
+
else
|
|
415
|
+
text = text.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
416
|
+
tr.insertText(text, pos, pos + node.nodeSize);
|
|
417
|
+
return false;
|
|
418
|
+
});
|
|
419
|
+
return true;
|
|
420
|
+
});
|
|
421
|
+
},
|
|
422
|
+
isActive: (name, attrs) => editor?.isActive(name, attrs) ?? false,
|
|
423
|
+
hasBlock: (kind) => {
|
|
424
|
+
if (!editor)
|
|
425
|
+
return false;
|
|
426
|
+
let found = false;
|
|
427
|
+
editor.state.doc.forEach((node) => {
|
|
428
|
+
if (node.type.name === 'blockWrapper' && node.attrs['data-block'] === kind) {
|
|
429
|
+
found = true;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
return found;
|
|
433
|
+
},
|
|
434
|
+
upsertBlock: ({ kind, html, position = 'end' }) => {
|
|
435
|
+
if (!editor)
|
|
436
|
+
return;
|
|
437
|
+
const wrappedHTML = `<div data-block="${kind}">${html}</div>`;
|
|
438
|
+
const parsed = parseHTMLToNode(editor, wrappedHTML);
|
|
439
|
+
const newNode = parsed.firstChild;
|
|
440
|
+
if (!newNode)
|
|
441
|
+
return;
|
|
442
|
+
const existing = findBlockPositions(editor, kind);
|
|
443
|
+
editor
|
|
444
|
+
.chain()
|
|
445
|
+
.command(({ tr }) => {
|
|
446
|
+
if (existing.length > 0) {
|
|
447
|
+
const [first, ...rest] = existing;
|
|
448
|
+
tr.replaceWith(first.pos, first.pos + first.size, newNode);
|
|
449
|
+
const delta = newNode.nodeSize - first.size;
|
|
450
|
+
[...rest].reverse().forEach(({ pos, size }) => {
|
|
451
|
+
tr.delete(pos + delta, pos + delta + size);
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
if (kind === 'signature') {
|
|
456
|
+
const isEditorEmpty = tr.doc.childCount === 1 && tr.doc.firstChild?.type.name === 'paragraph' && tr.doc.firstChild?.content.size === 0;
|
|
457
|
+
if (isEditorEmpty) {
|
|
458
|
+
const insertPos = tr?.doc?.firstChild?.nodeSize;
|
|
459
|
+
tr.insert(insertPos, newNode);
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
const lastNode = tr.doc.lastChild;
|
|
463
|
+
const insertPos = lastNode && lastNode.type.name === 'paragraph' && lastNode.content.size === 0 ? tr.doc.content.size - lastNode.nodeSize : tr.doc.content.size;
|
|
464
|
+
tr.insert(insertPos, newNode);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
else if (position === 'end') {
|
|
468
|
+
const lastNode = tr.doc.lastChild;
|
|
469
|
+
const insertPos = lastNode && lastNode.type.name === 'paragraph' && lastNode.content.size === 0 ? tr.doc.content.size - lastNode.nodeSize : tr.doc.content.size;
|
|
470
|
+
tr.insert(insertPos, newNode);
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
tr.insert(0, newNode);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return true;
|
|
477
|
+
})
|
|
478
|
+
.run();
|
|
479
|
+
},
|
|
480
|
+
removeBlock: (kind) => {
|
|
481
|
+
if (!editor)
|
|
482
|
+
return;
|
|
483
|
+
const blocks = findBlockPositions(editor, kind);
|
|
484
|
+
if (blocks.length === 0)
|
|
485
|
+
return;
|
|
486
|
+
editor
|
|
487
|
+
.chain()
|
|
488
|
+
.command(({ tr }) => {
|
|
489
|
+
[...blocks].reverse().forEach(({ pos, size }) => {
|
|
490
|
+
tr.delete(pos, pos + size);
|
|
491
|
+
});
|
|
492
|
+
return true;
|
|
493
|
+
})
|
|
494
|
+
.run();
|
|
495
|
+
},
|
|
496
|
+
}), [editor]);
|
|
497
|
+
if (!editor)
|
|
498
|
+
return null;
|
|
499
|
+
const shouldShowError = isOverLimit;
|
|
500
|
+
return (_jsxs("div", { className: `tiptap-editor${className ? ` ${className}` : ''}`, style: {
|
|
501
|
+
width: width ?? '100%',
|
|
502
|
+
height: height ?? '100%',
|
|
503
|
+
margin: margin ?? '5px auto',
|
|
504
|
+
border: shouldShowError
|
|
505
|
+
? '1px solid #c4272c'
|
|
506
|
+
: undefined,
|
|
507
|
+
backgroundColor: isOverLimit ? '#fafafa' : undefined,
|
|
508
|
+
transition: 'border-color 0.2s, background-color 0.2s',
|
|
509
|
+
display: 'flex',
|
|
510
|
+
flexDirection: 'column',
|
|
511
|
+
}, children: [!readOnly && level !== ContentEditorLevel.None && _jsx(ToolBar, { editor: editor, level: level }), !readOnly && level !== ContentEditorLevel.Basic && level !== ContentEditorLevel.None && _jsx(TableMenu, { editor: editor }), _jsx("div", { className: "tiptap-content-wrapper", style: {
|
|
512
|
+
flex: 1,
|
|
513
|
+
display: 'flex',
|
|
514
|
+
flexDirection: 'column',
|
|
515
|
+
minHeight: 0,
|
|
516
|
+
pointerEvents: 'auto',
|
|
517
|
+
}, children: _jsx(EditorContent, { editor: editor, className: 'tiptap-content', style: { height: contentHeight ?? '100%', flex: 1, display: 'flex', flexDirection: 'column' } }) }), (wordLimit !== undefined || errorMessage) && (_jsxs("div", { style: {
|
|
518
|
+
display: 'flex',
|
|
519
|
+
justifyContent: 'space-between',
|
|
520
|
+
alignItems: 'center',
|
|
521
|
+
padding: '8px 16px',
|
|
522
|
+
borderTop: '1px solid #e4e4e7',
|
|
523
|
+
background: '#fafafa',
|
|
524
|
+
}, children: [_jsx("div", { style: { flex: 1 }, children: shouldShowError && (_jsx("span", { style: {
|
|
525
|
+
color: '#c4272c',
|
|
526
|
+
fontSize: '12px',
|
|
527
|
+
fontWeight: 600,
|
|
528
|
+
}, children: errorMessage || 'Max word limit reached' })) }), wordLimit !== undefined && (_jsxs("span", { style: {
|
|
529
|
+
fontSize: '11px',
|
|
530
|
+
color: isOverLimit ? '#c4272c' : '#888',
|
|
531
|
+
fontWeight: isOverLimit ? 600 : 400,
|
|
532
|
+
transition: 'color 0.2s, font-weight 0.2s',
|
|
533
|
+
}, children: [wordCount, " / ", wordLimit, " words"] }))] }))] }));
|
|
534
|
+
});
|
|
535
|
+
export default memo(TiptapEditor);
|
|
536
|
+
const LinkComponent = memo(({ editor }) => {
|
|
537
|
+
const [linkUrl, setLinkUrl] = useState('');
|
|
538
|
+
const [linkText, setLinkText] = useState('');
|
|
539
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
540
|
+
const handleSetLink = useCallback(() => {
|
|
541
|
+
const url = linkUrl.trim();
|
|
542
|
+
const text = linkText.trim();
|
|
543
|
+
if (!url || !text)
|
|
544
|
+
return;
|
|
545
|
+
editor.chain().focus().insertContent(`<a href="${url}" target="_blank">${text}</a>`).run();
|
|
546
|
+
setIsOpen(false);
|
|
547
|
+
}, [editor, linkUrl, linkText]);
|
|
548
|
+
const handleOpenChange = useCallback((_, data) => {
|
|
549
|
+
setIsOpen(data.open);
|
|
550
|
+
if (data.open) {
|
|
551
|
+
setLinkText(editor.state.doc.textBetween(editor.state.selection.from, editor.state.selection.to));
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
setLinkText('');
|
|
555
|
+
setLinkUrl('');
|
|
556
|
+
}
|
|
557
|
+
}, [editor]);
|
|
558
|
+
return (_jsxs(Popover, { open: isOpen, onOpenChange: handleOpenChange, positioning: 'below', withArrow: true, children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: (triggerProps) => {
|
|
559
|
+
const { ref, ...props } = triggerProps;
|
|
560
|
+
return (_jsx("span", { ref: ref, ...props, className: 'tiptap-link-trigger', children: _jsx(Tooltip, { content: 'Add Link', relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: _jsx(LinkRegular, {}), className: `tiptap-toolbar-btn ${isOpen ? 'is-active' : ''}` }) }) }));
|
|
561
|
+
} }), _jsxs(PopoverSurface, { className: 'tiptap-link-popover', children: [_jsxs("div", { className: 'tiptap-link-row', children: [_jsx("label", { children: "Text" }), _jsx(Field, { children: _jsx(Input, { placeholder: 'text', value: linkText, onChange: (_, d) => setLinkText(d.value) }) })] }), _jsxs("div", { className: 'tiptap-link-row', children: [_jsx("label", { children: "Link" }), _jsx(Field, { children: _jsx(Input, { type: 'url', placeholder: 'Link', value: linkUrl, onChange: (_, d) => setLinkUrl(d.value) }) })] }), _jsxs("div", { className: 'tiptap-link-actions', children: [_jsx(DefaultButton, { onClick: () => setIsOpen(false), children: "Cancel" }), _jsx(PrimaryButton, { onClick: handleSetLink, disabled: !linkUrl.trim() || !linkText.trim(), children: "Add" })] })] })] }));
|
|
562
|
+
});
|
|
563
|
+
const ColorPickerPopover = memo(({ label, icon, tooltip, defaultColor, onApply }) => {
|
|
564
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
565
|
+
const [committed, setCommitted] = useState(defaultColor);
|
|
566
|
+
const [hue, setHue] = useState(0);
|
|
567
|
+
const [sat, setSat] = useState(0);
|
|
568
|
+
const [val, setVal] = useState(0);
|
|
569
|
+
const [hexInput, setHexInput] = useState(defaultColor);
|
|
570
|
+
const hRef = useRef(0);
|
|
571
|
+
const sRef = useRef(0);
|
|
572
|
+
const vRef = useRef(0);
|
|
573
|
+
const currentHex = useMemo(() => cpRgbToHex(...cpHsvToRgb(hue, sat, val)), [hue, sat, val]);
|
|
574
|
+
const hueColor = useMemo(() => cpRgbToHex(...cpHsvToRgb(hue, 1, 1)), [hue]);
|
|
575
|
+
const applyHsv = useCallback((h, s, v) => {
|
|
576
|
+
hRef.current = h;
|
|
577
|
+
sRef.current = s;
|
|
578
|
+
vRef.current = v;
|
|
579
|
+
setHue(h);
|
|
580
|
+
setSat(s);
|
|
581
|
+
setVal(v);
|
|
582
|
+
setHexInput(cpRgbToHex(...cpHsvToRgb(h, s, v)));
|
|
583
|
+
}, []);
|
|
584
|
+
const applyHex = useCallback((hex) => {
|
|
585
|
+
if (!CP_VALID_HEX(hex))
|
|
586
|
+
return;
|
|
587
|
+
const [r, g, b] = cpHexToRgb(hex);
|
|
588
|
+
const [h, s, v] = cpRgbToHsv(r, g, b);
|
|
589
|
+
applyHsv(h, s, v);
|
|
590
|
+
}, [applyHsv]);
|
|
591
|
+
const handleSvPointerDown = useCallback((e) => {
|
|
592
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
593
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
594
|
+
const ns = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
|
|
595
|
+
const nv = Math.max(0, Math.min(1, 1 - (e.clientY - rect.top) / rect.height));
|
|
596
|
+
applyHsv(hRef.current, ns, nv);
|
|
597
|
+
}, [applyHsv]);
|
|
598
|
+
const handleSvPointerMove = useCallback((e) => {
|
|
599
|
+
if (e.buttons === 0)
|
|
600
|
+
return;
|
|
601
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
602
|
+
const ns = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
|
|
603
|
+
const nv = Math.max(0, Math.min(1, 1 - (e.clientY - rect.top) / rect.height));
|
|
604
|
+
applyHsv(hRef.current, ns, nv);
|
|
605
|
+
}, [applyHsv]);
|
|
606
|
+
const handleHuePointerDown = useCallback((e) => {
|
|
607
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
608
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
609
|
+
const nh = Math.max(0, Math.min(360, ((e.clientX - rect.left) / rect.width) * 360));
|
|
610
|
+
applyHsv(nh, sRef.current, vRef.current);
|
|
611
|
+
}, [applyHsv]);
|
|
612
|
+
const handleHuePointerMove = useCallback((e) => {
|
|
613
|
+
if (e.buttons === 0)
|
|
614
|
+
return;
|
|
615
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
616
|
+
const nh = Math.max(0, Math.min(360, ((e.clientX - rect.left) / rect.width) * 360));
|
|
617
|
+
applyHsv(nh, sRef.current, vRef.current);
|
|
618
|
+
}, [applyHsv]);
|
|
619
|
+
const handleHexInputChange = useCallback((_, data) => {
|
|
620
|
+
const v = data.value;
|
|
621
|
+
setHexInput(v);
|
|
622
|
+
if (CP_VALID_HEX(v))
|
|
623
|
+
applyHex(v);
|
|
624
|
+
}, [applyHex]);
|
|
625
|
+
const handleHexInputBlur = useCallback(() => {
|
|
626
|
+
if (!CP_VALID_HEX(hexInput))
|
|
627
|
+
setHexInput(currentHex);
|
|
628
|
+
}, [hexInput, currentHex]);
|
|
629
|
+
const handleOpenChange = useCallback((_, data) => {
|
|
630
|
+
setIsOpen(data.open);
|
|
631
|
+
if (data.open)
|
|
632
|
+
applyHex(committed);
|
|
633
|
+
}, [committed, applyHex]);
|
|
634
|
+
const handleApply = useCallback(() => {
|
|
635
|
+
setCommitted(currentHex);
|
|
636
|
+
onApply(currentHex);
|
|
637
|
+
setIsOpen(false);
|
|
638
|
+
}, [currentHex, onApply]);
|
|
639
|
+
const handleClose = useCallback(() => setIsOpen(false), []);
|
|
640
|
+
return (_jsxs(Popover, { open: isOpen, onOpenChange: handleOpenChange, positioning: 'below', children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: (tp) => {
|
|
641
|
+
const { ref, ...props } = tp;
|
|
642
|
+
return (_jsx("span", { ref: ref, ...props, children: _jsx(Tooltip, { content: tooltip, relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: icon, className: `tiptap-toolbar-btn ${isOpen ? 'is-active' : ''}` }) }) }));
|
|
643
|
+
} }), _jsxs(PopoverSurface, { className: 'tiptap-color-popover cp-surface', children: [_jsxs("div", { className: 'cp-header', children: [_jsx("div", { className: 'cp-preview', style: { background: currentHex } }), _jsx("span", { className: 'cp-label', children: label })] }), _jsxs("div", { className: 'cp-hex-row', children: [_jsx("span", { className: 'cp-hex-label', children: "Hex" }), _jsx("div", { className: 'cp-hex-input-wrap', children: _jsx(Field, { children: _jsx(Input, { className: 'cp-hex-input', value: hexInput, maxLength: 7, onChange: handleHexInputChange, onBlur: handleHexInputBlur, spellCheck: false }) }) })] }), _jsx("div", { className: 'cp-presets', children: CP_PRESETS.map((c) => (_jsx("button", { className: `cp-swatch${currentHex.toLowerCase() === c.toLowerCase() ? ' cp-swatch--active' : ''}`, style: { background: c }, title: c, onClick: () => applyHex(c) }, c))) }), _jsxs("div", { className: 'cp-sv-area', onPointerDown: handleSvPointerDown, onPointerMove: handleSvPointerMove, children: [_jsx("div", { className: 'cp-sv-hue', style: { background: hueColor } }), _jsx("div", { className: 'cp-sv-white' }), _jsx("div", { className: 'cp-sv-black' }), _jsx("div", { className: 'cp-sv-thumb', style: { left: `${sat * 100}%`, top: `${(1 - val) * 100}%` } })] }), _jsx("div", { className: 'cp-hue-area', onPointerDown: handleHuePointerDown, onPointerMove: handleHuePointerMove, children: _jsx("div", { className: 'cp-hue-thumb', style: { left: `${(hue / 360) * 100}%` } }) }), _jsx("div", { className: 'cp-active-preview', style: { background: currentHex } }), _jsxs("div", { className: 'cp-actions', children: [_jsx(PrimaryButton, { onClick: handleApply, children: "Apply" }), _jsx(DefaultButton, { onClick: handleClose, children: "Close" })] })] })] }));
|
|
644
|
+
});
|
|
645
|
+
const TableComponent = memo(({ editor }) => {
|
|
646
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
647
|
+
const [rows, setRows] = useState('');
|
|
648
|
+
const [cols, setCols] = useState('');
|
|
649
|
+
const handleInsertTable = useCallback(() => {
|
|
650
|
+
const r = Number(rows);
|
|
651
|
+
const c = Number(cols);
|
|
652
|
+
if (!r || !c)
|
|
653
|
+
return;
|
|
654
|
+
editor
|
|
655
|
+
.chain()
|
|
656
|
+
.focus()
|
|
657
|
+
.insertTable({
|
|
658
|
+
rows: r,
|
|
659
|
+
cols: c,
|
|
660
|
+
withHeaderRow: true,
|
|
661
|
+
})
|
|
662
|
+
.run();
|
|
663
|
+
setIsOpen(false);
|
|
664
|
+
}, [editor, rows, cols]);
|
|
665
|
+
return (_jsxs(Popover, { open: isOpen, onOpenChange: (_, d) => {
|
|
666
|
+
setIsOpen(d.open);
|
|
667
|
+
if (!d.open) {
|
|
668
|
+
setRows('');
|
|
669
|
+
setCols('');
|
|
670
|
+
}
|
|
671
|
+
}, positioning: 'below', withArrow: true, children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: (tp) => {
|
|
672
|
+
const { ref, ...props } = tp;
|
|
673
|
+
return (_jsx("span", { ref: ref, ...props, children: _jsx(Tooltip, { content: 'Insert Table', relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: _jsx(TableRegular, {}), className: `tiptap-toolbar-btn ${isOpen ? 'is-active' : ''}` }) }) }));
|
|
674
|
+
} }), _jsxs(PopoverSurface, { className: 'tiptap-table-popover', children: [_jsxs("div", { className: 'tiptap-table-row', children: [_jsx("label", { children: "Rows" }), _jsx(Field, { children: _jsx(Input, { placeholder: 'Row', type: 'number', min: 1, value: rows, onChange: (_, d) => setRows(d.value) }) })] }), _jsxs("div", { className: 'tiptap-table-row', children: [_jsx("label", { children: "Columns" }), _jsx(Field, { children: _jsx(Input, { placeholder: 'Column', type: 'number', min: 1, value: cols, onChange: (_, d) => setCols(d.value) }) })] }), _jsxs("div", { className: 'tiptap-table-actions', children: [_jsx(DefaultButton, { onClick: () => {
|
|
675
|
+
setIsOpen(false);
|
|
676
|
+
setRows('');
|
|
677
|
+
setCols('');
|
|
678
|
+
}, children: "Cancel" }), _jsx(PrimaryButton, { onClick: () => {
|
|
679
|
+
handleInsertTable();
|
|
680
|
+
setRows('');
|
|
681
|
+
setCols('');
|
|
682
|
+
}, disabled: !String(rows).trim() || !String(cols).trim(), children: "Add" })] })] })] }));
|
|
683
|
+
});
|
|
684
|
+
const TableMenu = memo(({ editor }) => {
|
|
685
|
+
const [open, setOpen] = useState(false);
|
|
686
|
+
const [visible, setVisible] = useState(false);
|
|
687
|
+
const [position, setPosition] = useState({ top: 0, left: 0 });
|
|
688
|
+
const prevPos = useRef({ top: 0, left: 0 });
|
|
689
|
+
useEffect(() => {
|
|
690
|
+
if (!editor)
|
|
691
|
+
return;
|
|
692
|
+
const update = () => {
|
|
693
|
+
if (!editor.isActive('table')) {
|
|
694
|
+
setVisible(false);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
const { from } = editor.state.selection;
|
|
698
|
+
const domAtPos = editor.view.domAtPos(from);
|
|
699
|
+
let node = domAtPos.node;
|
|
700
|
+
if (node.nodeType === 3)
|
|
701
|
+
node = node.parentElement;
|
|
702
|
+
const cell = node.closest('td, th');
|
|
703
|
+
if (!cell) {
|
|
704
|
+
setVisible(false);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
const cellRect = cell.getBoundingClientRect();
|
|
708
|
+
const editorEl = editor.view.dom.closest('.tiptap-content');
|
|
709
|
+
if (!editorEl)
|
|
710
|
+
return;
|
|
711
|
+
const editorRect = editorEl.getBoundingClientRect();
|
|
712
|
+
const top = cellRect.top - editorRect.top + editorEl.scrollTop + cellRect.height / 2;
|
|
713
|
+
const left = cellRect.right - editorRect.left + editorEl.scrollLeft;
|
|
714
|
+
const cellText = cell.innerText?.trim();
|
|
715
|
+
const isEmpty = !cellText;
|
|
716
|
+
if (prevPos.current.top !== top || prevPos.current.left !== left) {
|
|
717
|
+
prevPos.current = { top, left };
|
|
718
|
+
setPosition({ top, left });
|
|
719
|
+
}
|
|
720
|
+
setVisible(isEmpty);
|
|
721
|
+
};
|
|
722
|
+
update();
|
|
723
|
+
editor.on('selectionUpdate', update);
|
|
724
|
+
editor.on('transaction', update);
|
|
725
|
+
window.addEventListener('resize', update);
|
|
726
|
+
window.addEventListener('scroll', update, true);
|
|
727
|
+
return () => {
|
|
728
|
+
editor.off('selectionUpdate', update);
|
|
729
|
+
editor.off('transaction', update);
|
|
730
|
+
window.removeEventListener('resize', update);
|
|
731
|
+
window.removeEventListener('scroll', update, true);
|
|
732
|
+
};
|
|
733
|
+
}, [editor]);
|
|
734
|
+
if (!visible)
|
|
735
|
+
return null;
|
|
736
|
+
return (_jsx("div", { className: 'table-menu-wrapper', style: position, children: _jsxs(Popover, { open: open, onOpenChange: (_, d) => setOpen(d.open), positioning: 'below-start', withArrow: true, children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: _jsx(Button, { appearance: 'subtle', className: 'table-trigger-btn', children: _jsx(ChevronDownRegular, { fontSize: 12 }) }) }), _jsxs(PopoverSurface, { className: 'table-dropdown-surface', children: [_jsxs("div", { className: 'table-dropdown-section', children: [_jsx("div", { className: 'table-dropdown-title', children: "Insert" }), [
|
|
737
|
+
{ icon: _jsx(ArrowUploadRegular, {}), label: 'Add row above', action: () => editor.chain().focus().addRowBefore().run() },
|
|
738
|
+
{ icon: _jsx(ArrowDownloadRegular, {}), label: 'Add row below', action: () => editor.chain().focus().addRowAfter().run() },
|
|
739
|
+
{ icon: _jsx(ArrowLeftRegular, {}), label: 'Add column left', action: () => editor.chain().focus().addColumnBefore().run() },
|
|
740
|
+
{ icon: _jsx(ArrowRightRegular, {}), label: 'Add column right', action: () => editor.chain().focus().addColumnAfter().run() },
|
|
741
|
+
].map(({ icon, label, action }) => (_jsx(Button, { appearance: 'subtle', icon: icon, className: 'table-dropdown-btn', onClick: () => {
|
|
742
|
+
action();
|
|
743
|
+
setOpen(false);
|
|
744
|
+
}, children: label }, label)))] }), _jsx("div", { className: 'table-dropdown-divider' }), _jsxs("div", { className: 'table-dropdown-section', children: [_jsx("div", { className: 'table-dropdown-title', children: "Delete" }), [
|
|
745
|
+
{
|
|
746
|
+
label: 'Delete row',
|
|
747
|
+
action: () => {
|
|
748
|
+
const { rows } = getTableDimensions(editor);
|
|
749
|
+
rows <= 1 ? editor.chain().focus().deleteTable().run() : editor.chain().focus().deleteRow().run();
|
|
750
|
+
},
|
|
751
|
+
},
|
|
752
|
+
{
|
|
753
|
+
label: 'Delete column',
|
|
754
|
+
action: () => {
|
|
755
|
+
const { cols } = getTableDimensions(editor);
|
|
756
|
+
cols <= 1 ? editor.chain().focus().deleteTable().run() : editor.chain().focus().deleteColumn().run();
|
|
757
|
+
},
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
label: 'Delete table',
|
|
761
|
+
action: () => editor.chain().focus().deleteTable().run(),
|
|
762
|
+
},
|
|
763
|
+
].map(({ label, action }) => (_jsx(Button, { appearance: 'subtle', className: 'table-dropdown-btn danger', icon: _jsx(DeleteRegular, {}), onClick: () => {
|
|
764
|
+
action();
|
|
765
|
+
setOpen(false);
|
|
766
|
+
}, children: label }, label)))] })] })] }) }));
|
|
767
|
+
});
|
|
768
|
+
const ToolBar = memo(({ editor, level = ContentEditorLevel.Basic }) => {
|
|
769
|
+
const isBold = useEditorState({ editor, selector: (c) => c.editor.isActive('bold') });
|
|
770
|
+
const isItalic = useEditorState({ editor, selector: (c) => c.editor.isActive('italic') });
|
|
771
|
+
const isUnderline = useEditorState({ editor, selector: (c) => c.editor.isActive('underline') });
|
|
772
|
+
const isStrike = useEditorState({ editor, selector: (c) => c.editor.isActive('strike') });
|
|
773
|
+
const isSubscript = useEditorState({ editor, selector: (c) => c.editor.isActive('subscript') });
|
|
774
|
+
const isSuperscript = useEditorState({ editor, selector: (c) => c.editor.isActive('superscript') });
|
|
775
|
+
const isHighlight = useEditorState({ editor, selector: (c) => c.editor.isActive('highlight') });
|
|
776
|
+
const isBulletList = useEditorState({ editor, selector: (c) => c.editor.isActive('bulletList') });
|
|
777
|
+
const isOrderedList = useEditorState({ editor, selector: (c) => c.editor.isActive('orderedList') });
|
|
778
|
+
const isBlockquote = useEditorState({ editor, selector: (c) => c.editor.isActive('blockquote') });
|
|
779
|
+
const isLink = useEditorState({ editor, selector: (c) => c.editor.isActive('link') });
|
|
780
|
+
const isAlignLeft = useEditorState({ editor, selector: (c) => c.editor.isActive({ textAlign: 'left' }) });
|
|
781
|
+
const isAlignCenter = useEditorState({ editor, selector: (c) => c.editor.isActive({ textAlign: 'center' }) });
|
|
782
|
+
const isAlignRight = useEditorState({ editor, selector: (c) => c.editor.isActive({ textAlign: 'right' }) });
|
|
783
|
+
const isAlignJustify = useEditorState({ editor, selector: (c) => c.editor.isActive({ textAlign: 'justify' }) });
|
|
784
|
+
const fontFamily = useEditorState({ editor, selector: (c) => c.editor.getAttributes('textStyle')?.fontFamily || 'Lucida Serif' });
|
|
785
|
+
const currentFontSize = useEditorState({ editor, selector: (c) => c.editor.getAttributes('textStyle')?.fontSize || '15px' });
|
|
786
|
+
const textTransform = useEditorState({ editor, selector: (c) => c.editor.getAttributes('textStyle')?.textTransform || '' });
|
|
787
|
+
const isHeading1 = useEditorState({ editor, selector: (c) => c.editor.isActive('heading', { level: 1 }) });
|
|
788
|
+
const isHeading2 = useEditorState({ editor, selector: (c) => c.editor.isActive('heading', { level: 2 }) });
|
|
789
|
+
const isHeading3 = useEditorState({ editor, selector: (c) => c.editor.isActive('heading', { level: 3 }) });
|
|
790
|
+
const isHeading4 = useEditorState({ editor, selector: (c) => c.editor.isActive('heading', { level: 4 }) });
|
|
791
|
+
const isHeading5 = useEditorState({ editor, selector: (c) => c.editor.isActive('heading', { level: 5 }) });
|
|
792
|
+
const isHeading6 = useEditorState({ editor, selector: (c) => c.editor.isActive('heading', { level: 6 }) });
|
|
793
|
+
const selectedHeading = useMemo(() => {
|
|
794
|
+
if (isHeading1)
|
|
795
|
+
return 'heading1';
|
|
796
|
+
if (isHeading2)
|
|
797
|
+
return 'heading2';
|
|
798
|
+
if (isHeading3)
|
|
799
|
+
return 'heading3';
|
|
800
|
+
if (isHeading4)
|
|
801
|
+
return 'heading4';
|
|
802
|
+
if (isHeading5)
|
|
803
|
+
return 'heading5';
|
|
804
|
+
if (isHeading6)
|
|
805
|
+
return 'heading6';
|
|
806
|
+
return 'paragraph';
|
|
807
|
+
}, [isHeading1, isHeading2, isHeading3, isHeading4, isHeading5, isHeading6]);
|
|
808
|
+
const handleHeadingChange = useCallback((v) => {
|
|
809
|
+
if (v === 'paragraph') {
|
|
810
|
+
editor.chain().focus().setParagraph().run();
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
editor
|
|
814
|
+
.chain()
|
|
815
|
+
.focus()
|
|
816
|
+
.setHeading({ level: parseInt(v.replace('heading', '')) })
|
|
817
|
+
.run();
|
|
818
|
+
}, [editor]);
|
|
819
|
+
const handleFontFamilyChange = useCallback((f) => editor.chain().focus().setFontFamily(f).run(), [editor]);
|
|
820
|
+
const handleFontSizeChange = useCallback((s) => editor.chain().focus().setMark('textStyle', { fontSize: s }).run(), [editor]);
|
|
821
|
+
const applyTextTransform = useCallback((t) => {
|
|
822
|
+
const current = editor.getAttributes('textStyle')?.textTransform;
|
|
823
|
+
if (current === t) {
|
|
824
|
+
editor.chain().focus().setMark('textStyle', { textTransform: null }).run();
|
|
825
|
+
}
|
|
826
|
+
else {
|
|
827
|
+
editor.chain().focus().setMark('textStyle', { textTransform: t }).run();
|
|
828
|
+
}
|
|
829
|
+
}, [editor]);
|
|
830
|
+
const applyTextColor = useCallback((c) => editor.chain().focus().setColor(c).run(), [editor]);
|
|
831
|
+
const applyHighlight = useCallback((c) => editor.chain().focus().toggleHighlight({ color: c }).run(), [editor]);
|
|
832
|
+
const btn = (active) => `tiptap-toolbar-btn${active ? ' is-active' : ''}`;
|
|
833
|
+
return (_jsxs(Toolbar, { "aria-label": 'Editor toolbar', className: 'tiptap-toolbar', children: [_jsx(Tooltip, { content: 'Bold', relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: _jsx(TextBoldRegular, {}), className: btn(isBold), onClick: () => editor.chain().focus().toggleBold().run() }) }), _jsx(Tooltip, { content: 'Italic', relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: _jsx(TextItalicRegular, {}), className: btn(isItalic), onClick: () => editor.chain().focus().toggleItalic().run() }) }), _jsx(Tooltip, { content: 'Underline', relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: _jsx(TextUnderlineRegular, {}), className: btn(isUnderline), onClick: () => editor.chain().focus().toggleUnderline().run() }) }), _jsx("div", { className: 'tiptap-divider' }), _jsx(ColorPickerPopover, { label: 'Text color', icon: _jsx(TextColorRegular, {}), tooltip: 'Text Color', defaultColor: '#000000', onApply: applyTextColor }), _jsx(ColorPickerPopover, { label: 'Background color', icon: _jsx(PaintBrushRegular, {}), tooltip: 'Background Color', defaultColor: '#fef08a', onApply: applyHighlight }), _jsx("div", { className: 'tiptap-divider' }), isLink ? (_jsx(Tooltip, { content: 'Remove Link', relationship: 'label', children: _jsx(ToolbarButton, { appearance: 'subtle', icon: _jsx(LinkDismissRegular, {}), className: 'tiptap-toolbar-btn is-active', onClick: () => editor.chain().focus().extendMarkRange('link').unsetLink().run() }) })) : (_jsx(LinkComponent, { editor: editor })), level !== ContentEditorLevel.Basic && _jsx(TableComponent, { editor: editor }), level !== ContentEditorLevel.Basic && _jsx("div", { className: 'tiptap-divider' }), _jsx(HeadingDropdown, { selected: selectedHeading, onChange: handleHeadingChange }), _jsx("div", { className: 'tiptap-divider' }), _jsx(FontFamilyDropdown, { fontFamily: fontFamily, onChange: handleFontFamilyChange }), _jsx("div", { className: 'tiptap-divider' }), _jsx(FontSizeDropdown, { currentSize: currentFontSize, onChange: handleFontSizeChange }), _jsx("div", { className: 'tiptap-divider' }), _jsx(TextToolsDropdown, { editor: editor, textTransform: textTransform, isStrike: isStrike, isSubscript: isSubscript, isSuperscript: isSuperscript, isHighlight: isHighlight, isBulletList: isBulletList, isOrderedList: isOrderedList, isBlockquote: isBlockquote, onTransform: applyTextTransform }), _jsx("div", { className: 'tiptap-divider' }), _jsx(AlignDropdown, { editor: editor, isAlignLeft: isAlignLeft, isAlignCenter: isAlignCenter, isAlignRight: isAlignRight, isAlignJustify: isAlignJustify })] }));
|
|
834
|
+
});
|
|
835
|
+
const HeadingDropdown = memo(({ selected, onChange }) => {
|
|
836
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
837
|
+
return (_jsxs(Popover, { positioning: 'below-start', withArrow: true, open: isOpen, onOpenChange: (_, d) => setIsOpen(d.open), children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: _jsxs("button", { className: `heading-trigger ${isOpen ? 'active' : ''}`, children: [_jsx("span", { children: HEADING_OPTIONS[selected] }), _jsx(ChevronDownRegular, {})] }) }), _jsx(PopoverSurface, { className: 'heading-menu', children: Object.entries(HEADING_OPTIONS).map(([v, label]) => (_jsx("button", { className: `heading-menu-item ${selected === v ? 'active' : ''}`, onClick: () => {
|
|
838
|
+
onChange(v);
|
|
839
|
+
setIsOpen(false);
|
|
840
|
+
}, children: label }, v))) })] }));
|
|
841
|
+
});
|
|
842
|
+
const FontFamilyDropdown = memo(({ fontFamily, onChange }) => {
|
|
843
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
844
|
+
return (_jsxs(Popover, { positioning: 'below-start', withArrow: true, open: isOpen, onOpenChange: (_, d) => setIsOpen(d.open), children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: _jsxs("button", { className: `heading-trigger font-family-trigger ${isOpen ? 'active' : ''}`, children: [_jsx("span", { style: { fontFamily }, children: fontFamily }), _jsx(ChevronDownRegular, {})] }) }), _jsx(PopoverSurface, { className: 'heading-menu font-family-menu', children: FONT_OPTIONS.map((f) => (_jsxs("button", { className: `heading-menu-item ${fontFamily === f ? 'active' : ''}`, style: { fontFamily: f }, onClick: () => {
|
|
845
|
+
onChange(f);
|
|
846
|
+
setIsOpen(false);
|
|
847
|
+
}, children: [_jsx("span", { children: f }), fontFamily === f && _jsx("span", { children: "\u2713" })] }, f))) })] }));
|
|
848
|
+
});
|
|
849
|
+
const FontSizeDropdown = memo(({ currentSize, onChange }) => {
|
|
850
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
851
|
+
return (_jsxs(Popover, { positioning: 'below-start', withArrow: true, open: isOpen, onOpenChange: (_, d) => setIsOpen(d.open), children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: _jsxs("button", { className: `heading-trigger font-size-trigger ${isOpen ? 'active' : ''}`, children: [_jsx("span", { children: currentSize }), _jsx(ChevronDownRegular, {})] }) }), _jsx(PopoverSurface, { className: 'heading-menu font-size-menu', children: FONT_SIZE_OPTIONS.map((s) => (_jsxs("button", { className: `heading-menu-item ${currentSize === s ? 'active' : ''}`, onClick: () => {
|
|
852
|
+
onChange(s);
|
|
853
|
+
setIsOpen(false);
|
|
854
|
+
}, children: [_jsx("span", { children: s }), currentSize === s && _jsx("span", { children: "\u2713" })] }, s))) })] }));
|
|
855
|
+
});
|
|
856
|
+
const TextToolsDropdown = memo((props) => {
|
|
857
|
+
const { editor, textTransform, isStrike, isSubscript, isSuperscript, isHighlight, isBulletList, isOrderedList, isBlockquote, onTransform } = props;
|
|
858
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
859
|
+
const renderItem = (label, icon, isActive, onClick) => (_jsxs("button", { className: `heading-menu-item ${isActive ? 'active' : ''}`, onClick: onClick, children: [_jsx(Checkbox, { checked: isActive, tabIndex: -1, style: { pointerEvents: 'none' } }), icon, _jsx("span", { children: label })] }));
|
|
860
|
+
return (_jsxs(Popover, { positioning: 'below-start', withArrow: true, open: isOpen, onOpenChange: (_, d) => setIsOpen(d.open), children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: _jsxs("button", { className: `heading-trigger text-tools-trigger ${isOpen ? 'active' : ''}`, children: [_jsx("span", { children: "Text" }), _jsx(ChevronDownRegular, {})] }) }), _jsxs(PopoverSurface, { className: 'heading-menu text-tools-menu', children: [renderItem('Uppercase', _jsx(TextCaseUppercaseRegular, {}), textTransform === 'uppercase', () => onTransform('uppercase')), renderItem('Lowercase', _jsx(TextCaseLowercaseRegular, {}), textTransform === 'lowercase', () => onTransform('lowercase')), renderItem('Capitalize', _jsx(TextCaseUppercaseRegular, {}), textTransform === 'capitalize', () => onTransform('capitalize')), _jsx("div", { className: 'text-tools-divider' }), renderItem('Strikethrough', _jsx(TextStrikethroughRegular, {}), isStrike, () => editor.chain().focus().toggleStrike().run()), renderItem('Subscript', _jsx(TextSubscriptRegular, {}), isSubscript, () => editor.chain().focus().toggleSubscript().run()), renderItem('Superscript', _jsx(TextSuperscriptRegular, {}), isSuperscript, () => editor.chain().focus().toggleSuperscript().run()), renderItem('Highlight', _jsx(HighlightRegular, {}), isHighlight, () => editor.chain().focus().toggleHighlight().run()), _jsx("div", { className: 'text-tools-divider' }), renderItem('Bullet list', _jsx(TextBulletListRegular, {}), isBulletList, () => editor.chain().focus().toggleBulletList().run()), renderItem('Number list', _jsx(TextNumberListLtrRegular, {}), isOrderedList, () => editor.chain().focus().toggleOrderedList().run()), renderItem('Page break', _jsx(DocumentPageBreakRegular, {}), false, () => editor.chain().focus().setHardBreak().run()), renderItem('Quote', _jsx(TextQuoteRegular, {}), isBlockquote, () => editor.chain().focus().toggleBlockquote().run())] })] }));
|
|
861
|
+
});
|
|
862
|
+
const AlignDropdown = memo((props) => {
|
|
863
|
+
const { editor, isAlignLeft, isAlignCenter, isAlignRight, isAlignJustify } = props;
|
|
864
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
865
|
+
return (_jsxs(Popover, { positioning: 'below-start', withArrow: true, open: isOpen, onOpenChange: (_, d) => setIsOpen(d.open), children: [_jsx(PopoverTrigger, { disableButtonEnhancement: true, children: _jsxs("button", { className: `heading-trigger text-tools-trigger ${isOpen ? 'active' : ''}`, children: [_jsx("span", { children: "Align" }), _jsx(ChevronDownRegular, {})] }) }), _jsxs(PopoverSurface, { className: 'heading-menu text-tools-menu', children: [_jsxs("button", { className: `heading-menu-item ${isAlignLeft ? 'active' : ''}`, onClick: () => editor.chain().focus().setTextAlign('left').run(), children: [_jsx(TextAlignLeftRegular, {}), _jsx("span", { children: "Left Align" })] }), _jsxs("button", { className: `heading-menu-item ${isAlignCenter ? 'active' : ''}`, onClick: () => editor.chain().focus().setTextAlign('center').run(), children: [_jsx(TextAlignCenterRegular, {}), _jsx("span", { children: "Center Align" })] }), _jsxs("button", { className: `heading-menu-item ${isAlignRight ? 'active' : ''}`, onClick: () => editor.chain().focus().setTextAlign('right').run(), children: [_jsx(TextAlignRightRegular, {}), _jsx("span", { children: "Right Align" })] }), _jsxs("button", { className: `heading-menu-item ${isAlignJustify ? 'active' : ''}`, onClick: () => editor.chain().focus().setTextAlign('justify').run(), children: [_jsx(TextAlignJustifyRegular, {}), _jsx("span", { children: "Justify Align" })] })] })] }));
|
|
866
|
+
});
|