@exsto/ds 0.0.1-rc.13 → 0.0.1-rc.14
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/dist/date-picker.js +1 -344
- package/dist/index.js +75 -4513
- package/dist/rich-text-editor.js +3 -904
- package/dist/source-hash.txt +1 -1
- package/dist/styles.css +5 -19
- package/dist/theme.css +2 -1
- package/dist/ui/src/components/edyn/animated-number/animated-number.js +108 -0
- package/dist/ui/src/components/edyn/animated-number/animated-number.js.map +1 -0
- package/dist/ui/src/components/edyn/audio-player/audio-player.js +86 -0
- package/dist/ui/src/components/edyn/audio-player/audio-player.js.map +1 -0
- package/dist/ui/src/components/edyn/date-picker/date-picker.js +250 -0
- package/dist/ui/src/components/edyn/date-picker/date-picker.js.map +1 -0
- package/dist/ui/src/components/edyn/date-picker/date-text.js +105 -0
- package/dist/ui/src/components/edyn/date-picker/date-text.js.map +1 -0
- package/dist/ui/src/components/edyn/date-range-picker/date-range-picker.js +59 -0
- package/dist/ui/src/components/edyn/date-range-picker/date-range-picker.js.map +1 -0
- package/dist/ui/src/components/edyn/initials-avatar/initials-avatar.js +86 -0
- package/dist/ui/src/components/edyn/initials-avatar/initials-avatar.js.map +1 -0
- package/dist/ui/src/components/edyn/multi-select/multi-select.js +332 -0
- package/dist/ui/src/components/edyn/multi-select/multi-select.js.map +1 -0
- package/dist/ui/src/components/edyn/progress-line/progress-line.js +100 -0
- package/dist/ui/src/components/edyn/progress-line/progress-line.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-content/allow-list.js +37 -0
- package/dist/ui/src/components/edyn/rich-text-content/allow-list.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-content/rich-text-content.js +124 -0
- package/dist/ui/src/components/edyn/rich-text-content/rich-text-content.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/extensions.js +124 -0
- package/dist/ui/src/components/edyn/rich-text-editor/extensions.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/mention-list.js +232 -0
- package/dist/ui/src/components/edyn/rich-text-editor/mention-list.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/rich-text-editor.js +244 -0
- package/dist/ui/src/components/edyn/rich-text-editor/rich-text-editor.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/serialize.js +55 -0
- package/dist/ui/src/components/edyn/rich-text-editor/serialize.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/sync.js +34 -0
- package/dist/ui/src/components/edyn/rich-text-editor/sync.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/toolbar.js +235 -0
- package/dist/ui/src/components/edyn/rich-text-editor/toolbar.js.map +1 -0
- package/dist/ui/src/components/edyn/rich-text-editor/types.js +26 -0
- package/dist/ui/src/components/edyn/rich-text-editor/types.js.map +1 -0
- package/dist/ui/src/components/edyn/video-player/video-player.js +132 -0
- package/dist/ui/src/components/edyn/video-player/video-player.js.map +1 -0
- package/dist/ui/src/components/ui/accordion/accordion.js +83 -0
- package/dist/ui/src/components/ui/accordion/accordion.js.map +1 -0
- package/dist/ui/src/components/ui/alert/alert.js +47 -0
- package/dist/ui/src/components/ui/alert/alert.js.map +1 -0
- package/dist/ui/src/components/ui/alert-dialog/alert-dialog.js +95 -0
- package/dist/ui/src/components/ui/alert-dialog/alert-dialog.js.map +1 -0
- package/dist/ui/src/components/ui/aspect-ratio/aspect-ratio.js +15 -0
- package/dist/ui/src/components/ui/aspect-ratio/aspect-ratio.js.map +1 -0
- package/dist/ui/src/components/ui/attachment/attachment.js +105 -0
- package/dist/ui/src/components/ui/attachment/attachment.js.map +1 -0
- package/dist/ui/src/components/ui/avatar/avatar.js +51 -0
- package/dist/ui/src/components/ui/avatar/avatar.js.map +1 -0
- package/dist/ui/src/components/ui/badge/badge.js +47 -0
- package/dist/ui/src/components/ui/badge/badge.js.map +1 -0
- package/dist/ui/src/components/ui/breadcrumb/breadcrumb.js +73 -0
- package/dist/ui/src/components/ui/breadcrumb/breadcrumb.js.map +1 -0
- package/dist/ui/src/components/ui/bubble/bubble.js +74 -0
- package/dist/ui/src/components/ui/bubble/bubble.js.map +1 -0
- package/dist/{chunks/input-syWxjY2U.js → ui/src/components/ui/button/button.js} +3 -82
- package/dist/ui/src/components/ui/button/button.js.map +1 -0
- package/dist/ui/src/components/ui/button-group/button-group.js +37 -0
- package/dist/ui/src/components/ui/button-group/button-group.js.map +1 -0
- package/dist/{chunks/date-range-picker-CWVcHgy2.js → ui/src/components/ui/calendar/calendar.js} +6 -100
- package/dist/ui/src/components/ui/calendar/calendar.js.map +1 -0
- package/dist/ui/src/components/ui/card/card.js +57 -0
- package/dist/ui/src/components/ui/card/card.js.map +1 -0
- package/dist/ui/src/components/ui/carousel/carousel.js +136 -0
- package/dist/ui/src/components/ui/carousel/carousel.js.map +1 -0
- package/dist/ui/src/components/ui/chart/chart.js +168 -0
- package/dist/ui/src/components/ui/chart/chart.js.map +1 -0
- package/dist/ui/src/components/ui/checkbox/checkbox.js +21 -0
- package/dist/ui/src/components/ui/checkbox/checkbox.js.map +1 -0
- package/dist/ui/src/components/ui/collapsible/collapsible.js +26 -0
- package/dist/ui/src/components/ui/collapsible/collapsible.js.map +1 -0
- package/dist/ui/src/components/ui/combobox/combobox.js +85 -0
- package/dist/ui/src/components/ui/combobox/combobox.js.map +1 -0
- package/dist/ui/src/components/ui/command/command.js +118 -0
- package/dist/ui/src/components/ui/command/command.js.map +1 -0
- package/dist/ui/src/components/ui/context-menu/context-menu.js +134 -0
- package/dist/ui/src/components/ui/context-menu/context-menu.js.map +1 -0
- package/dist/ui/src/components/ui/dialog/dialog.js +92 -0
- package/dist/ui/src/components/ui/dialog/dialog.js.map +1 -0
- package/dist/ui/src/components/ui/direction/direction.js +2 -0
- package/dist/ui/src/components/ui/drawer/drawer.js +86 -0
- package/dist/ui/src/components/ui/drawer/drawer.js.map +1 -0
- package/dist/ui/src/components/ui/dropdown-menu/dropdown-menu.js +160 -0
- package/dist/ui/src/components/ui/dropdown-menu/dropdown-menu.js.map +1 -0
- package/dist/ui/src/components/ui/empty/empty.js +61 -0
- package/dist/ui/src/components/ui/empty/empty.js.map +1 -0
- package/dist/ui/src/components/ui/field/field.js +36 -0
- package/dist/ui/src/components/ui/field/field.js.map +1 -0
- package/dist/ui/src/components/ui/hover-card/hover-card.js +38 -0
- package/dist/ui/src/components/ui/hover-card/hover-card.js.map +1 -0
- package/dist/ui/src/components/ui/indicator/indicator.js +79 -0
- package/dist/ui/src/components/ui/indicator/indicator.js.map +1 -0
- package/dist/ui/src/components/ui/input/input.js +16 -0
- package/dist/ui/src/components/ui/input/input.js.map +1 -0
- package/dist/ui/src/components/ui/input-group/input-group.js +77 -0
- package/dist/ui/src/components/ui/input-group/input-group.js.map +1 -0
- package/dist/ui/src/components/ui/input-otp/input-otp.js +48 -0
- package/dist/ui/src/components/ui/input-otp/input-otp.js.map +1 -0
- package/dist/ui/src/components/ui/item/item.js +122 -0
- package/dist/ui/src/components/ui/item/item.js.map +1 -0
- package/dist/ui/src/components/ui/kbd/kbd.js +21 -0
- package/dist/ui/src/components/ui/kbd/kbd.js.map +1 -0
- package/dist/ui/src/components/ui/label/label.js +14 -0
- package/dist/ui/src/components/ui/label/label.js.map +1 -0
- package/dist/ui/src/components/ui/marker/marker.js +44 -0
- package/dist/ui/src/components/ui/marker/marker.js.map +1 -0
- package/dist/ui/src/components/ui/menubar/menubar.js +137 -0
- package/dist/ui/src/components/ui/menubar/menubar.js.map +1 -0
- package/dist/ui/src/components/ui/message/message.js +50 -0
- package/dist/ui/src/components/ui/message/message.js.map +1 -0
- package/dist/ui/src/components/ui/message-scroller/message-scroller.js +61 -0
- package/dist/ui/src/components/ui/message-scroller/message-scroller.js.map +1 -0
- package/dist/ui/src/components/ui/native-select/native-select.js +39 -0
- package/dist/ui/src/components/ui/native-select/native-select.js.map +1 -0
- package/dist/ui/src/components/ui/navigation-menu/navigation-menu.js +84 -0
- package/dist/ui/src/components/ui/navigation-menu/navigation-menu.js.map +1 -0
- package/dist/ui/src/components/ui/pagination/pagination.js +104 -0
- package/dist/ui/src/components/ui/pagination/pagination.js.map +1 -0
- package/dist/ui/src/components/ui/popover/popover.js +56 -0
- package/dist/ui/src/components/ui/popover/popover.js.map +1 -0
- package/dist/ui/src/components/ui/progress/progress.js +70 -0
- package/dist/ui/src/components/ui/progress/progress.js.map +1 -0
- package/dist/ui/src/components/ui/questionnaire/questionnaire.js +171 -0
- package/dist/ui/src/components/ui/questionnaire/questionnaire.js.map +1 -0
- package/dist/ui/src/components/ui/radio-group/radio-group.js +29 -0
- package/dist/ui/src/components/ui/radio-group/radio-group.js.map +1 -0
- package/dist/ui/src/components/ui/resizable/resizable.js +29 -0
- package/dist/ui/src/components/ui/resizable/resizable.js.map +1 -0
- package/dist/ui/src/components/ui/scroll-area/scroll-area.js +119 -0
- package/dist/ui/src/components/ui/scroll-area/scroll-area.js.map +1 -0
- package/dist/ui/src/components/ui/select/select.js +99 -0
- package/dist/ui/src/components/ui/select/select.js.map +1 -0
- package/dist/ui/src/components/ui/separator/separator.js +30 -0
- package/dist/ui/src/components/ui/separator/separator.js.map +1 -0
- package/dist/ui/src/components/ui/sheet/sheet.js +89 -0
- package/dist/ui/src/components/ui/sheet/sheet.js.map +1 -0
- package/dist/ui/src/components/ui/sidebar/sidebar.js +383 -0
- package/dist/ui/src/components/ui/sidebar/sidebar.js.map +1 -0
- package/dist/ui/src/components/ui/skeleton/skeleton.js +14 -0
- package/dist/ui/src/components/ui/skeleton/skeleton.js.map +1 -0
- package/dist/ui/src/components/ui/slider/slider.js +49 -0
- package/dist/ui/src/components/ui/slider/slider.js.map +1 -0
- package/dist/ui/src/components/ui/spinner/spinner.js +28 -0
- package/dist/ui/src/components/ui/spinner/spinner.js.map +1 -0
- package/dist/ui/src/components/ui/switch/switch.js +20 -0
- package/dist/ui/src/components/ui/switch/switch.js.map +1 -0
- package/dist/ui/src/components/ui/table/table.js +68 -0
- package/dist/ui/src/components/ui/table/table.js.map +1 -0
- package/dist/ui/src/components/ui/tabs/tabs.js +50 -0
- package/dist/ui/src/components/ui/tabs/tabs.js.map +1 -0
- package/dist/ui/src/components/ui/textarea/textarea.js +14 -0
- package/dist/ui/src/components/ui/textarea/textarea.js.map +1 -0
- package/dist/ui/src/components/ui/toast/toast.js +124 -0
- package/dist/ui/src/components/ui/toast/toast.js.map +1 -0
- package/dist/ui/src/components/ui/toggle/toggle.js +37 -0
- package/dist/ui/src/components/ui/toggle/toggle.js.map +1 -0
- package/dist/ui/src/components/ui/toggle-group/toggle-group.js +56 -0
- package/dist/ui/src/components/ui/toggle-group/toggle-group.js.map +1 -0
- package/dist/ui/src/components/ui/tooltip/tooltip.js +42 -0
- package/dist/ui/src/components/ui/tooltip/tooltip.js.map +1 -0
- package/dist/ui/src/hooks/use-media-player.js +140 -0
- package/dist/ui/src/hooks/use-media-player.js.map +1 -0
- package/dist/ui/src/hooks/use-merged-refs.js +18 -0
- package/dist/ui/src/hooks/use-merged-refs.js.map +1 -0
- package/dist/ui/src/hooks/use-mobile.js +20 -0
- package/dist/ui/src/hooks/use-mobile.js.map +1 -0
- package/dist/ui/src/lib/first-weekday.js +34 -0
- package/dist/ui/src/lib/first-weekday.js.map +1 -0
- package/dist/ui/src/lib/popup-list.js +26 -0
- package/dist/ui/src/lib/popup-list.js.map +1 -0
- package/dist/ui/src/lib/utils.js +21 -0
- package/dist/ui/src/lib/utils.js.map +1 -0
- package/package.json +1 -1
- package/dist/chunks/date-range-picker-CWVcHgy2.js.map +0 -1
- package/dist/chunks/input-syWxjY2U.js.map +0 -1
- package/dist/chunks/toggle-Bxg8P-61.js +0 -490
- package/dist/chunks/toggle-Bxg8P-61.js.map +0 -1
- package/dist/date-picker.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/rich-text-editor.js.map +0 -1
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { cn } from "../../../lib/utils.js";
|
|
2
|
+
import { Popover, PopoverContent } from "../../ui/popover/popover.js";
|
|
3
|
+
import { ScrollArea } from "../../ui/scroll-area/scroll-area.js";
|
|
4
|
+
import { popupItemClass } from "../../../lib/popup-list.js";
|
|
5
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { IconLoader2 } from "@tabler/icons-react";
|
|
8
|
+
//#region ../ui/src/components/edyn/rich-text-editor/mention-list.tsx
|
|
9
|
+
var FIRST = {
|
|
10
|
+
status: "loading",
|
|
11
|
+
items: [],
|
|
12
|
+
page: 1,
|
|
13
|
+
hasMore: false,
|
|
14
|
+
more: false,
|
|
15
|
+
active: 0
|
|
16
|
+
};
|
|
17
|
+
var toPage = (result) => Array.isArray(result) ? {
|
|
18
|
+
items: result,
|
|
19
|
+
hasMore: false
|
|
20
|
+
} : result;
|
|
21
|
+
var optionId = (listId, index) => `${listId}-${index}`;
|
|
22
|
+
/** The list that opens under the caret when the writer types @. */
|
|
23
|
+
var MentionList = ({ query, rect, config, labels, listId, onPick, keyHandler, onActiveChange }) => {
|
|
24
|
+
const [state, setState] = useState(FIRST);
|
|
25
|
+
const refSearch = useRef(config.search);
|
|
26
|
+
refSearch.current = config.search;
|
|
27
|
+
const refFirst = useRef(true);
|
|
28
|
+
const refController = useRef(null);
|
|
29
|
+
const refViewport = useRef(null);
|
|
30
|
+
const debounce = config.debounce ?? 250;
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const controller = new AbortController();
|
|
33
|
+
refController.current = controller;
|
|
34
|
+
setState((previous) => ({
|
|
35
|
+
...previous,
|
|
36
|
+
status: "loading",
|
|
37
|
+
page: 1,
|
|
38
|
+
more: false
|
|
39
|
+
}));
|
|
40
|
+
const timer = setTimeout(async () => {
|
|
41
|
+
try {
|
|
42
|
+
const page = toPage(await refSearch.current(query, {
|
|
43
|
+
page: 1,
|
|
44
|
+
signal: controller.signal
|
|
45
|
+
}));
|
|
46
|
+
if (controller.signal.aborted) return;
|
|
47
|
+
setState({
|
|
48
|
+
status: "ready",
|
|
49
|
+
items: page.items,
|
|
50
|
+
page: 1,
|
|
51
|
+
hasMore: page.hasMore === true,
|
|
52
|
+
more: false,
|
|
53
|
+
active: 0
|
|
54
|
+
});
|
|
55
|
+
} catch {
|
|
56
|
+
if (!controller.signal.aborted) setState((previous) => ({
|
|
57
|
+
...previous,
|
|
58
|
+
status: "error",
|
|
59
|
+
items: [],
|
|
60
|
+
hasMore: false
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
}, refFirst.current ? 0 : debounce);
|
|
64
|
+
refFirst.current = false;
|
|
65
|
+
return () => {
|
|
66
|
+
clearTimeout(timer);
|
|
67
|
+
controller.abort();
|
|
68
|
+
};
|
|
69
|
+
}, [query, debounce]);
|
|
70
|
+
const loadMore = () => {
|
|
71
|
+
const controller = refController.current;
|
|
72
|
+
if (!controller || state.status !== "ready" || !state.hasMore || state.more) return;
|
|
73
|
+
const next = state.page + 1;
|
|
74
|
+
setState((previous) => ({
|
|
75
|
+
...previous,
|
|
76
|
+
more: true
|
|
77
|
+
}));
|
|
78
|
+
refSearch.current(query, {
|
|
79
|
+
page: next,
|
|
80
|
+
signal: controller.signal
|
|
81
|
+
}).then((result) => {
|
|
82
|
+
if (controller.signal.aborted) return;
|
|
83
|
+
const page = toPage(result);
|
|
84
|
+
setState((previous) => ({
|
|
85
|
+
...previous,
|
|
86
|
+
items: [...previous.items, ...page.items.filter((item) => !previous.items.some((known) => known.id === item.id))],
|
|
87
|
+
page: next,
|
|
88
|
+
hasMore: page.hasMore === true,
|
|
89
|
+
more: false
|
|
90
|
+
}));
|
|
91
|
+
}).catch(() => {
|
|
92
|
+
if (!controller.signal.aborted) setState((previous) => ({
|
|
93
|
+
...previous,
|
|
94
|
+
more: false,
|
|
95
|
+
hasMore: false
|
|
96
|
+
}));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
const refLoadMore = useRef(loadMore);
|
|
100
|
+
refLoadMore.current = loadMore;
|
|
101
|
+
const activeItem = state.items[state.active];
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
onActiveChange(activeItem ? optionId(listId, state.active) : void 0);
|
|
104
|
+
}, [
|
|
105
|
+
activeItem,
|
|
106
|
+
listId,
|
|
107
|
+
onActiveChange,
|
|
108
|
+
state.active
|
|
109
|
+
]);
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
document.getElementById(optionId(listId, state.active))?.scrollIntoView({ block: "nearest" });
|
|
112
|
+
}, [listId, state.active]);
|
|
113
|
+
const watchSentinel = useCallback((node) => {
|
|
114
|
+
if (!node) return;
|
|
115
|
+
const observer = new IntersectionObserver((entries) => {
|
|
116
|
+
if (entries.some((entry) => entry.isIntersecting)) refLoadMore.current();
|
|
117
|
+
}, { root: refViewport.current });
|
|
118
|
+
observer.observe(node);
|
|
119
|
+
return () => observer.disconnect();
|
|
120
|
+
}, []);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
keyHandler.current = (event) => {
|
|
123
|
+
const count = state.items.length;
|
|
124
|
+
if (event.key === "Escape") {
|
|
125
|
+
event.stopPropagation();
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (count === 0) return false;
|
|
129
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
130
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
131
|
+
const next = (state.active + step + count) % count;
|
|
132
|
+
if (step === 1 && state.active === count - 1) refLoadMore.current();
|
|
133
|
+
setState((previous) => ({
|
|
134
|
+
...previous,
|
|
135
|
+
active: next
|
|
136
|
+
}));
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if ((event.key === "Enter" || event.key === "Tab") && activeItem) {
|
|
140
|
+
onPick(activeItem);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
};
|
|
145
|
+
return () => {
|
|
146
|
+
keyHandler.current = null;
|
|
147
|
+
};
|
|
148
|
+
}, [
|
|
149
|
+
activeItem,
|
|
150
|
+
keyHandler,
|
|
151
|
+
onPick,
|
|
152
|
+
state.active,
|
|
153
|
+
state.items.length
|
|
154
|
+
]);
|
|
155
|
+
const anchor = { getBoundingClientRect: () => rect?.() ?? new DOMRect(0, 0, 0, 0) };
|
|
156
|
+
const announcement = state.status === "loading" ? labels.mentionSearching : state.status === "error" ? labels.mentionError : state.items.length === 0 ? labels.mentionEmpty : labels.mentionResults.replace("{count}", String(state.items.length));
|
|
157
|
+
return /* @__PURE__ */ jsx(Popover, {
|
|
158
|
+
open: true,
|
|
159
|
+
onOpenChange: () => void 0,
|
|
160
|
+
children: /* @__PURE__ */ jsxs(PopoverContent, {
|
|
161
|
+
anchor,
|
|
162
|
+
align: "start",
|
|
163
|
+
side: "bottom",
|
|
164
|
+
sideOffset: 6,
|
|
165
|
+
initialFocus: false,
|
|
166
|
+
finalFocus: false,
|
|
167
|
+
"data-slot": "rich-text-mention-popup",
|
|
168
|
+
className: "w-72 gap-0 p-1",
|
|
169
|
+
children: [/* @__PURE__ */ jsxs(ScrollArea, {
|
|
170
|
+
className: "max-h-60",
|
|
171
|
+
viewportRef: refViewport,
|
|
172
|
+
children: [
|
|
173
|
+
/* @__PURE__ */ jsx("div", {
|
|
174
|
+
role: "listbox",
|
|
175
|
+
id: listId,
|
|
176
|
+
"aria-label": labels.mentionList,
|
|
177
|
+
className: cn("flex flex-col gap-px", state.items.length === 0 && "hidden"),
|
|
178
|
+
children: state.items.map((item, index) => /* @__PURE__ */ jsxs("div", {
|
|
179
|
+
id: optionId(listId, index),
|
|
180
|
+
role: "option",
|
|
181
|
+
"aria-selected": index === state.active,
|
|
182
|
+
"data-slot": "rich-text-mention-option",
|
|
183
|
+
"data-highlighted": index === state.active ? "" : void 0,
|
|
184
|
+
className: cn(popupItemClass("base-ui"), "h-auto min-h-8 flex-col items-start gap-0"),
|
|
185
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
186
|
+
onMouseMove: () => setState((previous) => previous.active === index ? previous : {
|
|
187
|
+
...previous,
|
|
188
|
+
active: index
|
|
189
|
+
}),
|
|
190
|
+
onClick: () => onPick(item),
|
|
191
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
192
|
+
className: "truncate",
|
|
193
|
+
children: item.label
|
|
194
|
+
}), item.secondary ? /* @__PURE__ */ jsx("span", {
|
|
195
|
+
className: "truncate text-xs text-foreground/70",
|
|
196
|
+
children: item.secondary
|
|
197
|
+
}) : null]
|
|
198
|
+
}, item.id))
|
|
199
|
+
}),
|
|
200
|
+
state.status === "loading" && state.items.length === 0 ? /* @__PURE__ */ jsxs("div", {
|
|
201
|
+
className: "flex items-center gap-2 px-2 py-1.5 text-sm text-muted-foreground",
|
|
202
|
+
children: [/* @__PURE__ */ jsx(IconLoader2, {
|
|
203
|
+
className: "size-4 animate-spin motion-reduce:animate-none",
|
|
204
|
+
"aria-hidden": true
|
|
205
|
+
}), labels.mentionSearching]
|
|
206
|
+
}) : null,
|
|
207
|
+
state.status === "ready" && state.items.length === 0 ? /* @__PURE__ */ jsx("div", {
|
|
208
|
+
className: "px-2 py-1.5 text-sm text-muted-foreground",
|
|
209
|
+
children: labels.mentionEmpty
|
|
210
|
+
}) : null,
|
|
211
|
+
state.status === "error" ? /* @__PURE__ */ jsx("div", {
|
|
212
|
+
className: "px-2 py-1.5 text-sm text-destructive",
|
|
213
|
+
children: labels.mentionError
|
|
214
|
+
}) : null,
|
|
215
|
+
state.hasMore ? /* @__PURE__ */ jsx("div", {
|
|
216
|
+
ref: watchSentinel,
|
|
217
|
+
className: "flex items-center gap-2 px-2 py-1.5 text-xs text-muted-foreground",
|
|
218
|
+
children: state.more ? labels.mentionLoadingMore : null
|
|
219
|
+
}, state.items.length) : null
|
|
220
|
+
]
|
|
221
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
222
|
+
role: "status",
|
|
223
|
+
className: "sr-only",
|
|
224
|
+
children: announcement
|
|
225
|
+
})]
|
|
226
|
+
})
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
export { MentionList };
|
|
231
|
+
|
|
232
|
+
//# sourceMappingURL=mention-list.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mention-list.js","names":[],"sources":["../../../../../../../ui/src/components/edyn/rich-text-editor/mention-list.tsx"],"sourcesContent":["import { Popover, PopoverContent } from '@edyn/ui/components/ui/popover';\nimport { ScrollArea } from '@edyn/ui/components/ui/scroll-area';\nimport { popupItemClass } from '@edyn/ui/lib/popup-list';\nimport { cn } from '@edyn/ui/lib/utils';\nimport { IconLoader2 } from '@tabler/icons-react';\nimport { type RefObject, useCallback, useEffect, useRef, useState } from 'react';\nimport type { IRichTextLabels, IRichTextMention, IRichTextMentionItem, IRichTextMentionPage } from './types';\n\ninterface IListState {\n status: 'loading' | 'ready' | 'error';\n items: IRichTextMentionItem[];\n page: number;\n hasMore: boolean;\n more: boolean;\n active: number;\n}\n\nconst FIRST: IListState = { status: 'loading', items: [], page: 1, hasMore: false, more: false, active: 0 };\n\nconst toPage = (result: IRichTextMentionItem[] | IRichTextMentionPage): IRichTextMentionPage =>\n Array.isArray(result) ? { items: result, hasMore: false } : result;\n\nexport interface IMentionListProps {\n query: string;\n /** Where the @ was typed. */\n rect: (() => DOMRect | null) | null;\n config: IRichTextMention;\n labels: IRichTextLabels;\n listId: string;\n onPick: (item: IRichTextMentionItem) => void;\n /** The editor keeps focus, so the list is driven by its keys: the editor calls this one. */\n keyHandler: RefObject<((event: KeyboardEvent) => boolean) | null>;\n /** The id of the option the editor should name as `aria-activedescendant`. */\n onActiveChange: (optionId: string | undefined) => void;\n}\n\nconst optionId = (listId: string, index: number): string => `${listId}-${index}`;\n\n/** The list that opens under the caret when the writer types @. */\nexport const MentionList = ({ query, rect, config, labels, listId, onPick, keyHandler, onActiveChange }: IMentionListProps) => {\n const [state, setState] = useState<IListState>(FIRST);\n const refSearch = useRef(config.search);\n refSearch.current = config.search;\n const refFirst = useRef(true);\n const refController = useRef<AbortController | null>(null);\n const refViewport = useRef<HTMLDivElement | null>(null);\n const debounce = config.debounce ?? 250;\n\n useEffect(() => {\n const controller = new AbortController();\n refController.current = controller;\n setState((previous) => ({ ...previous, status: 'loading', page: 1, more: false }));\n const timer = setTimeout(\n async () => {\n try {\n const page = toPage(await refSearch.current(query, { page: 1, signal: controller.signal }));\n if (controller.signal.aborted) {\n return;\n }\n setState({ status: 'ready', items: page.items, page: 1, hasMore: page.hasMore === true, more: false, active: 0 });\n } catch {\n if (!controller.signal.aborted) {\n setState((previous) => ({ ...previous, status: 'error', items: [], hasMore: false }));\n }\n }\n },\n refFirst.current ? 0 : debounce,\n );\n refFirst.current = false;\n return () => {\n clearTimeout(timer);\n controller.abort();\n };\n }, [query, debounce]);\n\n const loadMore = () => {\n const controller = refController.current;\n if (!controller || state.status !== 'ready' || !state.hasMore || state.more) {\n return;\n }\n const next = state.page + 1;\n setState((previous) => ({ ...previous, more: true }));\n refSearch\n .current(query, { page: next, signal: controller.signal })\n .then((result) => {\n if (controller.signal.aborted) {\n return;\n }\n const page = toPage(result);\n setState((previous) => ({\n ...previous,\n items: [...previous.items, ...page.items.filter((item) => !previous.items.some((known) => known.id === item.id))],\n page: next,\n hasMore: page.hasMore === true,\n more: false,\n }));\n })\n .catch(() => {\n if (!controller.signal.aborted) {\n setState((previous) => ({ ...previous, more: false, hasMore: false }));\n }\n });\n };\n const refLoadMore = useRef(loadMore);\n refLoadMore.current = loadMore;\n\n const activeItem = state.items[state.active];\n\n useEffect(() => {\n onActiveChange(activeItem ? optionId(listId, state.active) : undefined);\n }, [activeItem, listId, onActiveChange, state.active]);\n\n useEffect(() => {\n document.getElementById(optionId(listId, state.active))?.scrollIntoView({ block: 'nearest' });\n }, [listId, state.active]);\n\n const watchSentinel = useCallback((node: HTMLDivElement | null) => {\n if (!node) {\n return;\n }\n const observer = new IntersectionObserver(\n (entries) => {\n if (entries.some((entry) => entry.isIntersecting)) {\n refLoadMore.current();\n }\n },\n { root: refViewport.current },\n );\n observer.observe(node);\n return () => observer.disconnect();\n }, []);\n\n useEffect(() => {\n keyHandler.current = (event) => {\n const count = state.items.length;\n if (event.key === 'Escape') {\n event.stopPropagation();\n return false;\n }\n if (count === 0) {\n return false;\n }\n if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {\n const step = event.key === 'ArrowDown' ? 1 : -1;\n const next = (state.active + step + count) % count;\n if (step === 1 && state.active === count - 1) {\n refLoadMore.current();\n }\n setState((previous) => ({ ...previous, active: next }));\n return true;\n }\n if ((event.key === 'Enter' || event.key === 'Tab') && activeItem) {\n onPick(activeItem);\n return true;\n }\n return false;\n };\n return () => {\n keyHandler.current = null;\n };\n }, [activeItem, keyHandler, onPick, state.active, state.items.length]);\n\n const anchor = { getBoundingClientRect: () => rect?.() ?? new DOMRect(0, 0, 0, 0) };\n const announcement =\n state.status === 'loading'\n ? labels.mentionSearching\n : state.status === 'error'\n ? labels.mentionError\n : state.items.length === 0\n ? labels.mentionEmpty\n : labels.mentionResults.replace('{count}', String(state.items.length));\n\n return (\n <Popover open onOpenChange={() => undefined}>\n <PopoverContent\n anchor={anchor}\n align=\"start\"\n side=\"bottom\"\n sideOffset={6}\n initialFocus={false}\n finalFocus={false}\n data-slot=\"rich-text-mention-popup\"\n className=\"w-72 gap-0 p-1\"\n >\n <ScrollArea className=\"max-h-60\" viewportRef={refViewport}>\n <div\n role=\"listbox\"\n id={listId}\n aria-label={labels.mentionList}\n className={cn('flex flex-col gap-px', state.items.length === 0 && 'hidden')}\n >\n {state.items.map((item, index) => (\n // biome-ignore lint/a11y/useKeyWithClickEvents: the editor keeps focus and drives the list with its own keys\n // biome-ignore lint/a11y/useFocusableInteractive: options are named by aria-activedescendant on the editor, never focused\n <div\n key={item.id}\n id={optionId(listId, index)}\n role=\"option\"\n aria-selected={index === state.active}\n data-slot=\"rich-text-mention-option\"\n data-highlighted={index === state.active ? '' : undefined}\n className={cn(popupItemClass('base-ui'), 'h-auto min-h-8 flex-col items-start gap-0')}\n onMouseDown={(event) => event.preventDefault()}\n onMouseMove={() => setState((previous) => (previous.active === index ? previous : { ...previous, active: index }))}\n onClick={() => onPick(item)}\n >\n <span className=\"truncate\">{item.label}</span>\n {item.secondary ? <span className=\"truncate text-xs text-foreground/70\">{item.secondary}</span> : null}\n </div>\n ))}\n </div>\n {state.status === 'loading' && state.items.length === 0 ? (\n <div className=\"flex items-center gap-2 px-2 py-1.5 text-sm text-muted-foreground\">\n <IconLoader2 className=\"size-4 animate-spin motion-reduce:animate-none\" aria-hidden />\n {labels.mentionSearching}\n </div>\n ) : null}\n {state.status === 'ready' && state.items.length === 0 ? (\n <div className=\"px-2 py-1.5 text-sm text-muted-foreground\">{labels.mentionEmpty}</div>\n ) : null}\n {state.status === 'error' ? <div className=\"px-2 py-1.5 text-sm text-destructive\">{labels.mentionError}</div> : null}\n {state.hasMore ? (\n <div key={state.items.length} ref={watchSentinel} className=\"flex items-center gap-2 px-2 py-1.5 text-xs text-muted-foreground\">\n {state.more ? labels.mentionLoadingMore : null}\n </div>\n ) : null}\n </ScrollArea>\n <div role=\"status\" className=\"sr-only\">\n {announcement}\n </div>\n </PopoverContent>\n </Popover>\n );\n};\n"],"mappings":";;;;;;;;AAiBA,IAAM,QAAoB;CAAE,QAAQ;CAAW,OAAO,CAAC;CAAG,MAAM;CAAG,SAAS;CAAO,MAAM;CAAO,QAAQ;AAAE;AAE1G,IAAM,UAAU,WACd,MAAM,QAAQ,MAAM,IAAI;CAAE,OAAO;CAAQ,SAAS;AAAM,IAAI;AAgB9D,IAAM,YAAY,QAAgB,UAA0B,GAAG,OAAO,GAAG;;AAGzE,IAAa,eAAe,EAAE,OAAO,MAAM,QAAQ,QAAQ,QAAQ,QAAQ,YAAY,qBAAwC;CAC7H,MAAM,CAAC,OAAO,YAAY,SAAqB,KAAK;CACpD,MAAM,YAAY,OAAO,OAAO,MAAM;CACtC,UAAU,UAAU,OAAO;CAC3B,MAAM,WAAW,OAAO,IAAI;CAC5B,MAAM,gBAAgB,OAA+B,IAAI;CACzD,MAAM,cAAc,OAA8B,IA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AM,CAAE;OACjH,eAAe,OAAO,IAAI;OAV5B,UAAA,CAYE,oBAAC,QAAD;QAAM,WAAU;QAAY,UAAA,KAAK;OAAY,CAAA,GAC5C,KAAK,YAAY,oBAAC,QAAD;QAAM,WAAU;QAAuC,UAAA,KAAK;OAAgB,CAAA,IAAI,IAC/F;MAbE,GAAA,KAAK,EAaP,CACN;KACE,CAAA;KACJ,MAAM,WAAW,aAAa,MAAM,MAAM,WAAW,IACpD,qBAAC,OAAD;MAAK,WAAU;MAAf,UAAA,CACE,oBAAC,aAAD;OAAa,WAAU;OAAiD,eAAA;MAAa,CAAA,GACpF,OAAO,gBACL;KACH,CAAA,IAAA;KACH,MAAM,WAAW,WAAW,MAAM,MAAM,WAAW,IAClD,oBAAC,OAAD;MAAK,WAAU;MAA6C,UAAA,OAAO;KAAkB,CAAA,IACnF;KACH,MAAM,WAAW,UAAU,oBAAC,OAAD;MAAK,WAAU;MAAwC,UAAA,OAAO;KAAkB,CAAA,IAAI;KAC/G,MAAM,UACL,oBAAC,OAAD;MAA8B,KAAK;MAAe,WAAU;MACzD,UAAA,MAAM,OAAO,OAAO,qBAAqB;KACvC,GAFK,MAAM,MAAM,MAEjB,IACH;IACM;GACZ,CAAA,GAAA,oBAAC,OAAD;IAAK,MAAK;IAAS,WAAU;IAC1B,UAAA;GACE,CAAA,CACS;;CACT,CAAA;AAEb"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { cn } from "../../../lib/utils.js";
|
|
2
|
+
import { ScrollArea } from "../../ui/scroll-area/scroll-area.js";
|
|
3
|
+
import { RICH_TEXT_BODY_CLASS } from "../rich-text-content/rich-text-content.js";
|
|
4
|
+
import { defaultMentionToText, isDocumentEmpty, serializeDocument } from "./serialize.js";
|
|
5
|
+
import { buildExtensions } from "./extensions.js";
|
|
6
|
+
import { MentionList } from "./mention-list.js";
|
|
7
|
+
import { isLive, syncEditable, syncValue } from "./sync.js";
|
|
8
|
+
import { RichTextToolbar } from "./toolbar.js";
|
|
9
|
+
import { DEFAULT_RICH_TEXT_LABELS } from "./types.js";
|
|
10
|
+
import { useCallback, useEffect, useId, useImperativeHandle, useMemo, useReducer, useRef, useState } from "react";
|
|
11
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
12
|
+
import { EditorContent, useEditor, useEditorState } from "@tiptap/react";
|
|
13
|
+
//#region ../ui/src/components/edyn/rich-text-editor/rich-text-editor.tsx
|
|
14
|
+
var LINE = 1.5;
|
|
15
|
+
/**
|
|
16
|
+
* A small rich-text field: paragraphs, bold, italic, underline, bulleted and numbered lists and links, with optional
|
|
17
|
+
* @mentions the app answers asynchronously. The value is HTML in and out, and only the elements in `RICH_TEXT_ELEMENTS`
|
|
18
|
+
* can come out (no class, no style), whatever is pasted. A mention is a chip while editing and plain text in the HTML.
|
|
19
|
+
* The server should still sanitise: this is a convenience for the writer, not a security boundary.
|
|
20
|
+
*/
|
|
21
|
+
var RichTextEditor = ({ value, defaultValue = "", onChange, placeholder, disabled = false, readOnly = false, invalid = false, minRows = 6, maxRows = 20, maxLength, mention, fields, toolbar = true, labels, id, autoFocus = false, onPasteFiles, ref, className, "aria-invalid": ariaInvalid, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, ...props }) => {
|
|
22
|
+
const isInvalid = invalid || ariaInvalid === true || ariaInvalid === "true";
|
|
23
|
+
const text = useMemo(() => ({
|
|
24
|
+
...DEFAULT_RICH_TEXT_LABELS,
|
|
25
|
+
...labels
|
|
26
|
+
}), [labels]);
|
|
27
|
+
const listId = useId();
|
|
28
|
+
const [suggestion, setSuggestion] = useState(null);
|
|
29
|
+
const [activeOption, setActiveOption] = useState(void 0);
|
|
30
|
+
const [linkOpen, setLinkOpen] = useState(false);
|
|
31
|
+
const refLinkRange = useRef(null);
|
|
32
|
+
const refKeys = useRef(null);
|
|
33
|
+
const refKnown = useRef(value ?? defaultValue);
|
|
34
|
+
const refPending = useRef([]);
|
|
35
|
+
const [, wake] = useReducer((count) => count + 1, 0);
|
|
36
|
+
const editable = !disabled && !readOnly;
|
|
37
|
+
const showToolbar = toolbar && !readOnly;
|
|
38
|
+
const latest = useRef({
|
|
39
|
+
mention,
|
|
40
|
+
maxLength,
|
|
41
|
+
onChange,
|
|
42
|
+
onPasteFiles,
|
|
43
|
+
toolbar: showToolbar
|
|
44
|
+
});
|
|
45
|
+
latest.current = {
|
|
46
|
+
mention,
|
|
47
|
+
maxLength,
|
|
48
|
+
onChange,
|
|
49
|
+
onPasteFiles,
|
|
50
|
+
toolbar: showToolbar
|
|
51
|
+
};
|
|
52
|
+
const host = useMemo(() => ({
|
|
53
|
+
mentionEnabled: () => latest.current.mention !== void 0,
|
|
54
|
+
maxLength: () => latest.current.maxLength,
|
|
55
|
+
toText: () => latest.current.mention?.toText ?? defaultMentionToText,
|
|
56
|
+
onSuggestion: (props) => {
|
|
57
|
+
if (props === null) setActiveOption(void 0);
|
|
58
|
+
setSuggestion(props === null ? null : {
|
|
59
|
+
query: props.query,
|
|
60
|
+
rect: props.clientRect ?? null,
|
|
61
|
+
command: props.command
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
onSuggestionKey: ({ event }) => refKeys.current?.(event) ?? false
|
|
65
|
+
}), []);
|
|
66
|
+
const extensions = useMemo(() => buildExtensions(host), [host]);
|
|
67
|
+
const emit = useCallback((current) => {
|
|
68
|
+
const html = isDocumentEmpty(current.state.doc) ? "" : serializeDocument(current.state.doc, host.toText());
|
|
69
|
+
refKnown.current = html;
|
|
70
|
+
return html;
|
|
71
|
+
}, [host]);
|
|
72
|
+
const attributes = useMemo(() => {
|
|
73
|
+
const entries = {
|
|
74
|
+
role: "textbox",
|
|
75
|
+
"aria-multiline": "true",
|
|
76
|
+
"aria-autocomplete": mention ? "list" : void 0,
|
|
77
|
+
"aria-controls": suggestion ? listId : void 0,
|
|
78
|
+
"aria-activedescendant": suggestion ? activeOption : void 0,
|
|
79
|
+
"aria-label": ariaLabel,
|
|
80
|
+
"aria-labelledby": ariaLabelledBy,
|
|
81
|
+
"aria-describedby": ariaDescribedBy,
|
|
82
|
+
"aria-invalid": isInvalid ? "true" : void 0,
|
|
83
|
+
"aria-readonly": readOnly ? "true" : void 0,
|
|
84
|
+
"aria-disabled": disabled ? "true" : void 0,
|
|
85
|
+
"aria-placeholder": placeholder,
|
|
86
|
+
tabindex: readOnly && !disabled ? "0" : void 0,
|
|
87
|
+
id,
|
|
88
|
+
"data-slot": "rich-text-editor-content",
|
|
89
|
+
class: cn(RICH_TEXT_BODY_CLASS, "min-h-(--rich-text-min) px-2.5 py-2 outline-none")
|
|
90
|
+
};
|
|
91
|
+
return Object.fromEntries(Object.entries(entries).filter((entry) => entry[1] !== void 0));
|
|
92
|
+
}, [
|
|
93
|
+
activeOption,
|
|
94
|
+
ariaDescribedBy,
|
|
95
|
+
ariaLabel,
|
|
96
|
+
ariaLabelledBy,
|
|
97
|
+
disabled,
|
|
98
|
+
id,
|
|
99
|
+
isInvalid,
|
|
100
|
+
listId,
|
|
101
|
+
mention,
|
|
102
|
+
placeholder,
|
|
103
|
+
readOnly,
|
|
104
|
+
suggestion
|
|
105
|
+
]);
|
|
106
|
+
const editor = useEditor({
|
|
107
|
+
extensions,
|
|
108
|
+
content: value ?? defaultValue,
|
|
109
|
+
editable,
|
|
110
|
+
autofocus: autoFocus ? "end" : false,
|
|
111
|
+
editorProps: {
|
|
112
|
+
attributes,
|
|
113
|
+
handlePaste: (_view, event) => {
|
|
114
|
+
const data = event.clipboardData;
|
|
115
|
+
if (!(data !== null && data.files.length > 0 && data.getData("text/plain") === "" && data.getData("text/html") === "") || !latest.current.onPasteFiles) return false;
|
|
116
|
+
latest.current.onPasteFiles(Array.from(data.files));
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
handleKeyDown: (view, event) => {
|
|
120
|
+
if ((event.metaKey || event.ctrlKey) && !event.altKey && !event.shiftKey && event.key.toLowerCase() === "k" && latest.current.toolbar) {
|
|
121
|
+
event.preventDefault();
|
|
122
|
+
refLinkRange.current = {
|
|
123
|
+
from: view.state.selection.from,
|
|
124
|
+
to: view.state.selection.to
|
|
125
|
+
};
|
|
126
|
+
setLinkOpen(true);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
onBlur: () => {
|
|
133
|
+
setSuggestion(null);
|
|
134
|
+
setActiveOption(void 0);
|
|
135
|
+
},
|
|
136
|
+
onUpdate: ({ editor: current }) => {
|
|
137
|
+
latest.current.onChange?.(emit(current));
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
const ready = isLive(editor);
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
editor.on("create", wake);
|
|
143
|
+
wake();
|
|
144
|
+
return () => {
|
|
145
|
+
editor.off("create", wake);
|
|
146
|
+
};
|
|
147
|
+
}, [editor]);
|
|
148
|
+
const whenLive = useCallback((action) => {
|
|
149
|
+
if (isLive(editor)) action(editor);
|
|
150
|
+
else refPending.current.push(action);
|
|
151
|
+
}, [editor]);
|
|
152
|
+
useImperativeHandle(ref, () => ({
|
|
153
|
+
insertText: (text) => {
|
|
154
|
+
whenLive((live) => {
|
|
155
|
+
live.chain().focus().insertContent({
|
|
156
|
+
type: "text",
|
|
157
|
+
text
|
|
158
|
+
}).run();
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
focus: () => {
|
|
162
|
+
whenLive((live) => {
|
|
163
|
+
live.commands.focus();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}), [whenLive]);
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
if (!ready || !isLive(editor)) return;
|
|
169
|
+
for (const action of refPending.current.splice(0)) action(editor);
|
|
170
|
+
}, [editor, ready]);
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
if (ready) syncEditable(editor, editable);
|
|
173
|
+
}, [
|
|
174
|
+
editable,
|
|
175
|
+
editor,
|
|
176
|
+
ready
|
|
177
|
+
]);
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
if (ready) syncValue(editor, value, refKnown);
|
|
180
|
+
}, [
|
|
181
|
+
editor,
|
|
182
|
+
ready,
|
|
183
|
+
value
|
|
184
|
+
]);
|
|
185
|
+
const empty = useEditorState({
|
|
186
|
+
editor,
|
|
187
|
+
selector: ({ editor: current }) => isDocumentEmpty(current.state.doc)
|
|
188
|
+
});
|
|
189
|
+
const pick = useCallback((item) => {
|
|
190
|
+
suggestion?.command(item);
|
|
191
|
+
}, [suggestion]);
|
|
192
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
193
|
+
"data-slot": "rich-text-editor",
|
|
194
|
+
"data-invalid": isInvalid ? "" : void 0,
|
|
195
|
+
"data-disabled": disabled ? "" : void 0,
|
|
196
|
+
"data-readonly": readOnly ? "" : void 0,
|
|
197
|
+
className: cn("flex w-full min-w-0 flex-col rounded-lg border border-input bg-transparent text-base transition-colors focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50 data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-disabled:bg-input/50 data-disabled:opacity-50 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:data-disabled:bg-input/80 dark:data-invalid:ring-destructive/40", className),
|
|
198
|
+
...props,
|
|
199
|
+
children: [
|
|
200
|
+
showToolbar ? /* @__PURE__ */ jsx(RichTextToolbar, {
|
|
201
|
+
editor,
|
|
202
|
+
labels: text,
|
|
203
|
+
fields,
|
|
204
|
+
disabled,
|
|
205
|
+
linkOpen,
|
|
206
|
+
linkRange: refLinkRange,
|
|
207
|
+
onLinkOpenChange: (next) => {
|
|
208
|
+
if (next && !linkOpen) refLinkRange.current = {
|
|
209
|
+
from: editor.state.selection.from,
|
|
210
|
+
to: editor.state.selection.to
|
|
211
|
+
};
|
|
212
|
+
setLinkOpen(next);
|
|
213
|
+
}
|
|
214
|
+
}) : null,
|
|
215
|
+
/* @__PURE__ */ jsxs(ScrollArea, {
|
|
216
|
+
className: "relative",
|
|
217
|
+
style: {
|
|
218
|
+
maxHeight: `${maxRows * LINE + 1}rem`,
|
|
219
|
+
["--rich-text-min"]: `${minRows * LINE + 1}rem`
|
|
220
|
+
},
|
|
221
|
+
focusable: false,
|
|
222
|
+
children: [empty && placeholder ? /* @__PURE__ */ jsx("p", {
|
|
223
|
+
"aria-hidden": true,
|
|
224
|
+
className: "pointer-events-none absolute start-2.5 top-2 text-muted-foreground",
|
|
225
|
+
children: placeholder
|
|
226
|
+
}) : null, /* @__PURE__ */ jsx(EditorContent, { editor })]
|
|
227
|
+
}),
|
|
228
|
+
suggestion && mention ? /* @__PURE__ */ jsx(MentionList, {
|
|
229
|
+
query: suggestion.query,
|
|
230
|
+
rect: suggestion.rect,
|
|
231
|
+
config: mention,
|
|
232
|
+
labels: text,
|
|
233
|
+
listId,
|
|
234
|
+
onPick: pick,
|
|
235
|
+
keyHandler: refKeys,
|
|
236
|
+
onActiveChange: setActiveOption
|
|
237
|
+
}) : null
|
|
238
|
+
]
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
//#endregion
|
|
242
|
+
export { RichTextEditor };
|
|
243
|
+
|
|
244
|
+
//# sourceMappingURL=rich-text-editor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rich-text-editor.js","names":[],"sources":["../../../../../../../ui/src/components/edyn/rich-text-editor/rich-text-editor.tsx"],"sourcesContent":["import { RICH_TEXT_BODY_CLASS } from '@edyn/ui/components/edyn/rich-text-content/rich-text-content';\nimport { ScrollArea } from '@edyn/ui/components/ui/scroll-area';\nimport { cn } from '@edyn/ui/lib/utils';\nimport type { Editor } from '@tiptap/core';\nimport { EditorContent, useEditor, useEditorState } from '@tiptap/react';\nimport type { SuggestionProps } from '@tiptap/suggestion';\nimport {\n type ComponentPropsWithoutRef,\n type Ref,\n useCallback,\n useEffect,\n useId,\n useImperativeHandle,\n useMemo,\n useReducer,\n useRef,\n useState,\n} from 'react';\nimport { buildExtensions, type IEditorHost } from './extensions';\nimport { MentionList } from './mention-list';\nimport { defaultMentionToText, isDocumentEmpty, serializeDocument } from './serialize';\nimport { isLive, syncEditable, syncValue } from './sync';\nimport { type ILinkRange, RichTextToolbar } from './toolbar';\nimport {\n DEFAULT_RICH_TEXT_LABELS,\n type IRichTextEditorHandle,\n type IRichTextField,\n type IRichTextLabels,\n type IRichTextMention,\n type IRichTextMentionItem,\n} from './types';\n\nconst LINE = 1.5;\n\ninterface ISuggestionState {\n query: string;\n rect: (() => DOMRect | null) | null;\n command: (item: IRichTextMentionItem) => void;\n}\n\nexport interface IRichTextEditorProps extends Omit<ComponentPropsWithoutRef<'div'>, 'children' | 'defaultValue' | 'onChange' | 'id'> {\n /** The HTML, when the parent owns it. Pass back what `onChange` gave. An empty editor is `\"\"`. */\n value?: string;\n /** The HTML to start from when the editor owns its own value. */\n defaultValue?: string;\n /** Called with the HTML after every edit. Only the elements in `RICH_TEXT_ELEMENTS` can appear, and `\"\"` when there is no text. */\n onChange?: (html: string) => void;\n placeholder?: string;\n disabled?: boolean;\n /** Shows the content and keeps it selectable and copyable, with no toolbar. */\n readOnly?: boolean;\n /** Draws the error border and sets `aria-invalid`. The message is the form's to draw: point `aria-describedby` at it. */\n invalid?: boolean;\n /**\n * Lines the box is tall when empty.\n * @default 6\n */\n minRows?: number;\n /**\n * Lines it grows to before it scrolls inside.\n * @default 20\n */\n maxRows?: number;\n /** The most characters of text (a mention counts as the text it is written as). An edit past it is refused. Off by default. */\n maxLength?: number;\n /** Turns @ into a search the app answers. Without it, @ is an ordinary character. */\n mention?: IRichTextMention;\n /** Adds an \"Insert a field\" menu that writes `{{id}}` at the caret. */\n fields?: IRichTextField[];\n /**\n * Whether the formatting toolbar shows.\n * @default true\n */\n toolbar?: boolean;\n /** Replaces any of the strings the editor shows (the defaults are English). */\n labels?: Partial<IRichTextLabels>;\n /** On the editable element, so a form can name it. */\n id?: string;\n autoFocus?: boolean;\n /** Called with the files when the writer pastes only files (a screenshot). Without it, such a paste does nothing. */\n onPasteFiles?: (files: File[]) => void;\n /** Gives `insertText` and `focus`, for buttons outside the editor. */\n ref?: Ref<IRichTextEditorHandle>;\n}\n\n/**\n * A small rich-text field: paragraphs, bold, italic, underline, bulleted and numbered lists and links, with optional\n * @mentions the app answers asynchronously. The value is HTML in and out, and only the elements in `RICH_TEXT_ELEMENTS`\n * can come out (no class, no style), whatever is pasted. A mention is a chip while editing and plain text in the HTML.\n * The server should still sanitise: this is a convenience for the writer, not a security boundary.\n */\nexport const RichTextEditor = ({\n value,\n defaultValue = '',\n onChange,\n placeholder,\n disabled = false,\n readOnly = false,\n invalid = false,\n minRows = 6,\n maxRows = 20,\n maxLength,\n mention,\n fields,\n toolbar = true,\n labels,\n id,\n autoFocus = false,\n onPasteFiles,\n ref,\n className,\n 'aria-invalid': ariaInvalid,\n 'aria-label': ariaLabel,\n 'aria-labelledby': ariaLabelledBy,\n 'aria-describedby': ariaDescribedBy,\n ...props\n}: IRichTextEditorProps) => {\n const isInvalid = invalid || ariaInvalid === true || ariaInvalid === 'true';\n const text = useMemo(() => ({ ...DEFAULT_RICH_TEXT_LABELS, ...labels }), [labels]);\n const listId = useId();\n const [suggestion, setSuggestion] = useState<ISuggestionState | null>(null);\n const [activeOption, setActiveOption] = useState<string | undefined>(undefined);\n const [linkOpen, setLinkOpen] = useState(false);\n const refLinkRange = useRef<ILinkRange | null>(null);\n const refKeys = useRef<((event: KeyboardEvent) => boolean) | null>(null);\n // What the editor and the parent agree on. It starts as the value the editor is built from, so a controlled value\n // that has not changed asks for nothing at mount.\n const refKnown = useRef<string | undefined>(value ?? defaultValue);\n const refPending = useRef<Array<(live: Editor) => void>>([]);\n const [, wake] = useReducer((count: number) => count + 1, 0);\n const editable = !disabled && !readOnly;\n const showToolbar = toolbar && !readOnly;\n\n const latest = useRef({ mention, maxLength, onChange, onPasteFiles, toolbar: showToolbar });\n latest.current = { mention, maxLength, onChange, onPasteFiles, toolbar: showToolbar };\n\n const host = useMemo<IEditorHost>(\n () => ({\n mentionEnabled: () => latest.current.mention !== undefined,\n maxLength: () => latest.current.maxLength,\n toText: () => latest.current.mention?.toText ?? defaultMentionToText,\n onSuggestion: (props: SuggestionProps<IRichTextMentionItem, IRichTextMentionItem> | null) => {\n if (props === null) {\n setActiveOption(undefined);\n }\n setSuggestion(props === null ? null : { query: props.query, rect: props.clientRect ?? null, command: props.command });\n },\n onSuggestionKey: ({ event }) => refKeys.current?.(event) ?? false,\n }),\n [],\n );\n const extensions = useMemo(() => buildExtensions(host), [host]);\n\n const emit = useCallback(\n (current: Editor): string => {\n const html = isDocumentEmpty(current.state.doc) ? '' : serializeDocument(current.state.doc, host.toText());\n refKnown.current = html;\n return html;\n },\n [host],\n );\n\n const attributes = useMemo(() => {\n const entries: Record<string, string | undefined> = {\n role: 'textbox',\n 'aria-multiline': 'true',\n 'aria-autocomplete': mention ? 'list' : undefined,\n 'aria-controls': suggestion ? listId : undefined,\n 'aria-activedescendant': suggestion ? activeOption : undefined,\n 'aria-label': ariaLabel,\n 'aria-labelledby': ariaLabelledBy,\n 'aria-describedby': ariaDescribedBy,\n 'aria-invalid': isInvalid ? 'true' : undefined,\n 'aria-readonly': readOnly ? 'true' : undefined,\n 'aria-disabled': disabled ? 'true' : undefined,\n 'aria-placeholder': placeholder,\n tabindex: readOnly && !disabled ? '0' : undefined,\n id,\n 'data-slot': 'rich-text-editor-content',\n class: cn(RICH_TEXT_BODY_CLASS, 'min-h-(--rich-text-min) px-2.5 py-2 outline-none'),\n };\n return Object.fromEntries(Object.entries(entries).filter((entry): entry is [string, string] => entry[1] !== undefined));\n }, [activeOption, ariaDescribedBy, ariaLabel, ariaLabelledBy, disabled, id, isInvalid, listId, mention, placeholder, readOnly, suggestion]);\n\n const editor = useEditor({\n extensions,\n content: value ?? defaultValue,\n editable,\n autofocus: autoFocus ? 'end' : false,\n editorProps: {\n attributes,\n handlePaste: (_view, event) => {\n const data = event.clipboardData;\n const onlyFiles = data !== null && data.files.length > 0 && data.getData('text/plain') === '' && data.getData('text/html') === '';\n if (!onlyFiles || !latest.current.onPasteFiles) {\n return false;\n }\n latest.current.onPasteFiles(Array.from(data.files));\n return true;\n },\n handleKeyDown: (view, event) => {\n if ((event.metaKey || event.ctrlKey) && !event.altKey && !event.shiftKey && event.key.toLowerCase() === 'k' && latest.current.toolbar) {\n event.preventDefault();\n refLinkRange.current = { from: view.state.selection.from, to: view.state.selection.to };\n setLinkOpen(true);\n return true;\n }\n return false;\n },\n },\n onBlur: () => {\n setSuggestion(null);\n setActiveOption(undefined);\n },\n onUpdate: ({ editor: current }) => {\n latest.current.onChange?.(emit(current));\n },\n });\n\n // useEditor can replace the editor (it destroys one that was not mounted in time, and in development a second render\n // makes a second), so readiness is read off the editor in hand: wake up when this one finishes initialising.\n const ready = isLive(editor);\n useEffect(() => {\n editor.on('create', wake);\n wake();\n return () => {\n editor.off('create', wake);\n };\n }, [editor]);\n\n const whenLive = useCallback(\n (action: (live: Editor) => void) => {\n if (isLive(editor)) {\n action(editor);\n } else {\n refPending.current.push(action);\n }\n },\n [editor],\n );\n\n useImperativeHandle(\n ref,\n () => ({\n insertText: (text) => {\n whenLive((live) => {\n live.chain().focus().insertContent({ type: 'text', text }).run();\n });\n },\n focus: () => {\n whenLive((live) => {\n live.commands.focus();\n });\n },\n }),\n [whenLive],\n );\n\n useEffect(() => {\n if (!ready || !isLive(editor)) {\n return;\n }\n for (const action of refPending.current.splice(0)) {\n action(editor);\n }\n }, [editor, ready]);\n\n useEffect(() => {\n if (ready) {\n syncEditable(editor, editable);\n }\n }, [editable, editor, ready]);\n\n useEffect(() => {\n if (ready) {\n syncValue(editor, value, refKnown);\n }\n }, [editor, ready, value]);\n\n const empty = useEditorState({ editor, selector: ({ editor: current }) => isDocumentEmpty(current.state.doc) });\n\n const pick = useCallback(\n (item: IRichTextMentionItem) => {\n suggestion?.command(item);\n },\n [suggestion],\n );\n\n return (\n <div\n data-slot=\"rich-text-editor\"\n data-invalid={isInvalid ? '' : undefined}\n data-disabled={disabled ? '' : undefined}\n data-readonly={readOnly ? '' : undefined}\n className={cn(\n 'flex w-full min-w-0 flex-col rounded-lg border border-input bg-transparent text-base transition-colors focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50 data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-disabled:bg-input/50 data-disabled:opacity-50 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:data-disabled:bg-input/80 dark:data-invalid:ring-destructive/40',\n className,\n )}\n {...props}\n >\n {showToolbar ? (\n <RichTextToolbar\n editor={editor}\n labels={text}\n fields={fields}\n disabled={disabled}\n linkOpen={linkOpen}\n linkRange={refLinkRange}\n onLinkOpenChange={(next) => {\n if (next && !linkOpen) {\n refLinkRange.current = { from: editor.state.selection.from, to: editor.state.selection.to };\n }\n setLinkOpen(next);\n }}\n />\n ) : null}\n <ScrollArea\n className=\"relative\"\n style={{ maxHeight: `${maxRows * LINE + 1}rem`, ['--rich-text-min' as string]: `${minRows * LINE + 1}rem` }}\n focusable={false}\n >\n {empty && placeholder ? (\n <p aria-hidden className=\"pointer-events-none absolute start-2.5 top-2 text-muted-foreground\">\n {placeholder}\n </p>\n ) : null}\n <EditorContent editor={editor} />\n </ScrollArea>\n {suggestion && mention ? (\n <MentionList\n query={suggestion.query}\n rect={suggestion.rect}\n config={mention}\n labels={text}\n listId={listId}\n onPick={pick}\n keyHandler={refKeys}\n onActiveChange={setActiveOption}\n />\n ) : null}\n </div>\n );\n};\n"],"mappings":";;;;;;;;;;;;;AAgCA,IAAM,OAAO;;;;;;;AA2Db,IAAa,kBAAkB,EAC7B,OACA,eAAe,IACf,UACA,aACA,WAAW,OACX,WAAW,OACX,UAAU,OACV,UAAU,GACV,UAAU,IACV,WACA,SACA,QACA,UAAU,MACV,QACA,IACA,YAAY,OACZ,cACA,KACA,WACA,gBAAgB,aAChB,cAAc,WACd,mBAAmB,gBACnB,oBAAoB,iBACpB,GAAG,YACuB;CAC1B,MAAM,Y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AQ,eAAe;EAAE;EAAQ,WAAW,EAAE,QAAQ,cAAc,gBAAgB,QAAQ,MAAM,GAAG;CAAE,CAAC;CAE9G,MAAM,OAAO,aACV,SAA+B;EAC9B,YAAY,QAAQ,IAAI;CAC1B,GACA,CAAC,UAAU,CACb;CAEA,OACE,qBAAC,OAAD;EACE,aAAU;EACV,gBAAc,YAAY,KAAK,KAAA;EAC/B,iBAAe,WAAW,KAAK,KAAA;EAC/B,iBAAe,WAAW,KAAK,KAAA;EAC/B,WAAW,GACT,6dACA,SACF;EACA,GAAI;EATN,UAAA;GAWG,cACC,oBAAC,iBAAD;IACU;IACR,QAAQ;IACA;IACE;IACA;IACV,WAAW;IACX,mBAAmB,SAAS;KAC1B,IAAI,QAAQ,CAAC,UACX,aAAa,UAAU;MAAE,MAAM,OAAO,MAAM,UAAU;MAAM,IAAI,OAAO,MAAM,UAAU;KAAG;KAE5F,YAAY,IAAI;IAClB;GACD,CAAA,IACC;GACJ,qBAAC,YAAD;IACE,WAAU;IACV,OAAO;KAAE,WAAW,GAAG,UAAU,OAAO,EAAE;MAAO,oBAA8B,GAAG,UAAU,OAAO,EAAE;IAAK;IAC1G,WAAW;IAHb,UAAA,CAKG,SAAS,cACR,oBAAC,KAAD;KAAG,eAAA;KAAY,WAAU;KACtB,UAAA;IACA,CAAA,IACD,MACJ,oBAAC,eAAD,EAAuB,OAAS,CAAA,CACtB;;GACX,cAAc,UACb,oBAAC,aAAD;IACE,OAAO,WAAW;IAClB,MAAM,WAAW;IACjB,QAAQ;IACR,QAAQ;IACA;IACR,QAAQ;IACR,YAAY;IACZ,gBAAgB;GACjB,CAAA,IACC;EACD;;AAET"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DOMSerializer } from "@tiptap/pm/model";
|
|
2
|
+
//#region ../ui/src/components/edyn/rich-text-editor/serialize.ts
|
|
3
|
+
var MENTION_MARK = "data-rich-text-mention";
|
|
4
|
+
var defaultMentionToText = (item) => `@${item.id}`;
|
|
5
|
+
/**
|
|
6
|
+
* The document as HTML. Not `editor.getHTML()`: that would write a mention as its chip element, and
|
|
7
|
+
* the HTML must carry the plain text the app chose (`@person@example.test`) and nothing else.
|
|
8
|
+
*/
|
|
9
|
+
var serializeDocument = (doc, toText) => {
|
|
10
|
+
const schema = doc.type.schema;
|
|
11
|
+
const nodes = {
|
|
12
|
+
...DOMSerializer.nodesFromSchema(schema),
|
|
13
|
+
mention: (node) => [
|
|
14
|
+
"span",
|
|
15
|
+
{ [MENTION_MARK]: "" },
|
|
16
|
+
toText({
|
|
17
|
+
id: String(node.attrs.id ?? ""),
|
|
18
|
+
label: String(node.attrs.label ?? "")
|
|
19
|
+
})
|
|
20
|
+
]
|
|
21
|
+
};
|
|
22
|
+
const serializer = new DOMSerializer(nodes, DOMSerializer.marksFromSchema(schema));
|
|
23
|
+
const container = document.createElement("div");
|
|
24
|
+
container.appendChild(serializer.serializeFragment(doc.content));
|
|
25
|
+
for (const element of Array.from(container.querySelectorAll(`[${MENTION_MARK}]`))) element.replaceWith(document.createTextNode(element.textContent ?? ""));
|
|
26
|
+
return container.innerHTML;
|
|
27
|
+
};
|
|
28
|
+
/** No text and no mention. A document of blank paragraphs, spaces and line breaks is empty too. */
|
|
29
|
+
var isDocumentEmpty = (doc) => {
|
|
30
|
+
if (doc.textContent.trim() !== "") return false;
|
|
31
|
+
let hasMention = false;
|
|
32
|
+
doc.descendants((node) => {
|
|
33
|
+
if (node.type.name === "mention") hasMention = true;
|
|
34
|
+
return !hasMention;
|
|
35
|
+
});
|
|
36
|
+
return !hasMention;
|
|
37
|
+
};
|
|
38
|
+
/** Characters the HTML text will have: a mention counts as the text it is written as. */
|
|
39
|
+
var countCharacters = (doc, toText) => {
|
|
40
|
+
let total = 0;
|
|
41
|
+
doc.descendants((node) => {
|
|
42
|
+
if (node.isText) total += node.text?.length ?? 0;
|
|
43
|
+
else if (node.type.name === "mention") total += toText({
|
|
44
|
+
id: String(node.attrs.id ?? ""),
|
|
45
|
+
label: String(node.attrs.label ?? "")
|
|
46
|
+
}).length;
|
|
47
|
+
else if (node.type.name === "hardBreak") total += 1;
|
|
48
|
+
return true;
|
|
49
|
+
});
|
|
50
|
+
return total;
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
export { countCharacters, defaultMentionToText, isDocumentEmpty, serializeDocument };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=serialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.js","names":[],"sources":["../../../../../../../ui/src/components/edyn/rich-text-editor/serialize.ts"],"sourcesContent":["import { type DOMOutputSpec, DOMSerializer, type Node as TProseNode } from '@tiptap/pm/model';\nimport type { IRichTextMention } from './types';\n\nexport type TMentionToText = NonNullable<IRichTextMention['toText']>;\n\n// A serializer can only return elements, so a mention leaves as one and is unwrapped to its text below.\nconst MENTION_MARK = 'data-rich-text-mention';\n\nexport const defaultMentionToText: TMentionToText = (item) => `@${item.id}`;\n\n/**\n * The document as HTML. Not `editor.getHTML()`: that would write a mention as its chip element, and\n * the HTML must carry the plain text the app chose (`@person@example.test`) and nothing else.\n */\nexport const serializeDocument = (doc: TProseNode, toText: TMentionToText): string => {\n const schema = doc.type.schema;\n const nodes = {\n ...DOMSerializer.nodesFromSchema(schema),\n mention: (node: TProseNode): DOMOutputSpec => [\n 'span',\n { [MENTION_MARK]: '' },\n toText({ id: String(node.attrs.id ?? ''), label: String(node.attrs.label ?? '') }),\n ],\n };\n const serializer = new DOMSerializer(nodes, DOMSerializer.marksFromSchema(schema));\n const container = document.createElement('div');\n container.appendChild(serializer.serializeFragment(doc.content));\n for (const element of Array.from(container.querySelectorAll(`[${MENTION_MARK}]`))) {\n element.replaceWith(document.createTextNode(element.textContent ?? ''));\n }\n return container.innerHTML;\n};\n\n/** No text and no mention. A document of blank paragraphs, spaces and line breaks is empty too. */\nexport const isDocumentEmpty = (doc: TProseNode): boolean => {\n if (doc.textContent.trim() !== '') {\n return false;\n }\n let hasMention = false;\n doc.descendants((node) => {\n if (node.type.name === 'mention') {\n hasMention = true;\n }\n return !hasMention;\n });\n return !hasMention;\n};\n\n/** Characters the HTML text will have: a mention counts as the text it is written as. */\nexport const countCharacters = (doc: TProseNode, toText: TMentionToText): number => {\n let total = 0;\n doc.descendants((node) => {\n if (node.isText) {\n total += node.text?.length ?? 0;\n } else if (node.type.name === 'mention') {\n total += toText({ id: String(node.attrs.id ?? ''), label: String(node.attrs.label ?? '') }).length;\n } else if (node.type.name === 'hardBreak') {\n total += 1;\n }\n return true;\n });\n return total;\n};\n"],"mappings":";;AAMA,IAAM,eAAe;AAErB,IAAa,wBAAwC,SAAS,IAAI,KAAK;;;;;AAMvE,IAAa,qBAAqB,KAAiB,WAAmC;CACpF,MAAM,SAAS,IAAI,KAAK;CACxB,MAAM,QAAQ;EACZ,GAAG,cAAc,gBAAgB,MAAM;EACvC,UAAU,SAAoC;GAC5C;GACA,GAAG,eAAe,GAAG;GACrB,OAAO;IAAE,IAAI,OAAO,KAAK,MAAM,MAAM,EAAE;IAAG,OAAO,OAAO,KAAK,MAAM,SAAS,EAAE;GAAE,CAAC;EACnF;CACF;CACA,MAAM,aAAa,IAAI,cAAc,OAAO,cAAc,gBAAgB,MAAM,CAAC;CACjF,MAAM,YAAY,SAAS,cAAc,KAAK;CAC9C,UAAU,YAAY,WAAW,kBAAkB,IAAI,OAAO,CAAC;CAC/D,KAAK,MAAM,WAAW,MAAM,KAAK,UAAU,iBAAiB,IAAI,aAAa,EAAE,CAAC,GAC9E,QAAQ,YAAY,SAAS,eAAe,QAAQ,eAAe,EAAE,CAAC;CAExE,OAAO,UAAU;AACnB;;AAGA,IAAa,mBAAmB,QAA6B;CAC3D,IAAI,IAAI,YAAY,KAAK,MAAM,IAC7B,OAAO;CAET,IAAI,aAAa;CACjB,IAAI,aAAa,SAAS;EACxB,IAAI,KAAK,KAAK,SAAS,WACrB,aAAa;EAEf,OAAO,CAAC;CACV,CAAC;CACD,OAAO,CAAC;AACV;;AAGA,IAAa,mBAAmB,KAAiB,WAAmC;CAClF,IAAI,QAAQ;CACZ,IAAI,aAAa,SAAS;EACxB,IAAI,KAAK,QACP,SAAS,KAAK,MAAM,UAAU;OACzB,IAAI,KAAK,KAAK,SAAS,WAC5B,SAAS,OAAO;GAAE,IAAI,OAAO,KAAK,MAAM,MAAM,EAAE;GAAG,OAAO,OAAO,KAAK,MAAM,SAAS,EAAE;EAAE,CAAC,CAAC,CAAC;OACvF,IAAI,KAAK,KAAK,SAAS,aAC5B,SAAS;EAEX,OAAO;CACT,CAAC;CACD,OAAO;AACT"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { TextSelection } from "@tiptap/pm/state";
|
|
2
|
+
//#region ../ui/src/components/edyn/rich-text-editor/sync.ts
|
|
3
|
+
/**
|
|
4
|
+
* Whether commands may be called on the editor. TipTap builds an editor during the render, mounts its view later, and
|
|
5
|
+
* destroys one that has not been mounted within a moment (useEditor then makes a new one), so an effect can be handed an
|
|
6
|
+
* editor that is not there yet or no longer is. `isDestroyed` is true for both: it reads true until the view exists. A
|
|
7
|
+
* destroyed editor has no command manager, so `editor.commands` throws on it.
|
|
8
|
+
*/
|
|
9
|
+
var isLive = (editor) => editor.isInitialized && !editor.isDestroyed;
|
|
10
|
+
/**
|
|
11
|
+
* Brings the editor to a value the parent passed in, if it is one the editor did not just report itself.
|
|
12
|
+
* `known` is the last HTML the editor and the parent agree on (the starting value, then whatever the editor emitted or was
|
|
13
|
+
* given). The caret stays where it was, kept inside the new content, so a value changed from outside while the writer is
|
|
14
|
+
* typing does not throw the caret to the start.
|
|
15
|
+
*/
|
|
16
|
+
var syncValue = (editor, value, known) => {
|
|
17
|
+
if (!isLive(editor) || value === void 0 || value === known.current) return;
|
|
18
|
+
known.current = value;
|
|
19
|
+
const { from, to } = editor.state.selection;
|
|
20
|
+
editor.commands.setContent(value, { emitUpdate: false });
|
|
21
|
+
editor.commands.command(({ tr }) => {
|
|
22
|
+
const size = tr.doc.content.size;
|
|
23
|
+
tr.setSelection(TextSelection.between(tr.doc.resolve(Math.min(from, size)), tr.doc.resolve(Math.min(to, size))));
|
|
24
|
+
return true;
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
/** Sets whether the editor can be edited without telling the app something changed (TipTap emits an update by default). */
|
|
28
|
+
var syncEditable = (editor, editable) => {
|
|
29
|
+
if (isLive(editor) && editor.isEditable !== editable) editor.setEditable(editable, false);
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { isLive, syncEditable, syncValue };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=sync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.js","names":[],"sources":["../../../../../../../ui/src/components/edyn/rich-text-editor/sync.ts"],"sourcesContent":["import type { Editor } from '@tiptap/core';\nimport { TextSelection } from '@tiptap/pm/state';\n\n/**\n * Whether commands may be called on the editor. TipTap builds an editor during the render, mounts its view later, and\n * destroys one that has not been mounted within a moment (useEditor then makes a new one), so an effect can be handed an\n * editor that is not there yet or no longer is. `isDestroyed` is true for both: it reads true until the view exists. A\n * destroyed editor has no command manager, so `editor.commands` throws on it.\n */\nexport const isLive = (editor: Editor): boolean => editor.isInitialized && !editor.isDestroyed;\n\n/**\n * Brings the editor to a value the parent passed in, if it is one the editor did not just report itself.\n * `known` is the last HTML the editor and the parent agree on (the starting value, then whatever the editor emitted or was\n * given). The caret stays where it was, kept inside the new content, so a value changed from outside while the writer is\n * typing does not throw the caret to the start.\n */\nexport const syncValue = (editor: Editor, value: string | undefined, known: { current: string | undefined }): void => {\n if (!isLive(editor) || value === undefined || value === known.current) {\n return;\n }\n known.current = value;\n const { from, to } = editor.state.selection;\n editor.commands.setContent(value, { emitUpdate: false });\n editor.commands.command(({ tr }) => {\n const size = tr.doc.content.size;\n tr.setSelection(TextSelection.between(tr.doc.resolve(Math.min(from, size)), tr.doc.resolve(Math.min(to, size))));\n return true;\n });\n};\n\n/** Sets whether the editor can be edited without telling the app something changed (TipTap emits an update by default). */\nexport const syncEditable = (editor: Editor, editable: boolean): void => {\n if (isLive(editor) && editor.isEditable !== editable) {\n editor.setEditable(editable, false);\n }\n};\n"],"mappings":";;;;;;;;AASA,IAAa,UAAU,WAA4B,OAAO,iBAAiB,CAAC,OAAO;;;;;;;AAQnF,IAAa,aAAa,QAAgB,OAA2B,UAAiD;CACpH,IAAI,CAAC,OAAO,MAAM,KAAK,UAAU,KAAA,KAAa,UAAU,MAAM,SAC5D;CAEF,MAAM,UAAU;CAChB,MAAM,EAAE,MAAM,OAAO,OAAO,MAAM;CAClC,OAAO,SAAS,WAAW,OAAO,EAAE,YAAY,MAAM,CAAC;CACvD,OAAO,SAAS,SAAS,EAAE,SAAS;EAClC,MAAM,OAAO,GAAG,IAAI,QAAQ;EAC5B,GAAG,aAAa,cAAc,QAAQ,GAAG,IAAI,QAAQ,KAAK,IAAI,MAAM,IAAI,CAAC,GAAG,GAAG,IAAI,QAAQ,KAAK,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC;EAC/G,OAAO;CACT,CAAC;AACH;;AAGA,IAAa,gBAAgB,QAAgB,aAA4B;CACvE,IAAI,OAAO,MAAM,KAAK,OAAO,eAAe,UAC1C,OAAO,YAAY,UAAU,KAAK;AAEtC"}
|