@sia.soul/sia-react-ui 0.1.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.
Files changed (46) hide show
  1. package/README.md +37 -0
  2. package/THIRD_PARTY_NOTICES.md +12 -0
  3. package/dist/Select-CJJ5LQap.d.cts +44 -0
  4. package/dist/Select-CJJ5LQap.d.ts +44 -0
  5. package/dist/chart.cjs +7523 -0
  6. package/dist/chart.css +1036 -0
  7. package/dist/chart.d.cts +2061 -0
  8. package/dist/chart.d.ts +2061 -0
  9. package/dist/chart.js +120 -0
  10. package/dist/chunk-34TTF4Y7.js +812 -0
  11. package/dist/chunk-6VXOLMKZ.js +381 -0
  12. package/dist/chunk-DZ45HRVP.js +583 -0
  13. package/dist/chunk-I342FGQY.js +7344 -0
  14. package/dist/chunk-JKZGAZMB.js +318 -0
  15. package/dist/chunk-MBS2HXLZ.js +59 -0
  16. package/dist/chunk-NZAT2RUM.js +60 -0
  17. package/dist/chunk-OMA75YLM.js +298 -0
  18. package/dist/chunk-ULEGTKXR.js +2138 -0
  19. package/dist/chunk-YCVXUMGP.js +945 -0
  20. package/dist/core-BeiAQiDY.d.ts +1076 -0
  21. package/dist/core-DRICIs2n.d.cts +1076 -0
  22. package/dist/core.cjs +3353 -0
  23. package/dist/core.css +8734 -0
  24. package/dist/core.d.cts +3 -0
  25. package/dist/core.d.ts +3 -0
  26. package/dist/core.js +45 -0
  27. package/dist/index.cjs +19658 -0
  28. package/dist/index.css +9762 -0
  29. package/dist/index.d.cts +1222 -0
  30. package/dist/index.d.ts +1222 -0
  31. package/dist/index.js +6843 -0
  32. package/dist/markdown-editor.cjs +417 -0
  33. package/dist/markdown-editor.d.cts +28 -0
  34. package/dist/markdown-editor.d.ts +28 -0
  35. package/dist/markdown-editor.js +6 -0
  36. package/dist/rich-text-editor.cjs +1853 -0
  37. package/dist/rich-text-editor.d.cts +27 -0
  38. package/dist/rich-text-editor.d.ts +27 -0
  39. package/dist/rich-text-editor.js +11 -0
  40. package/dist/select-date-range-BDWdo7qt.d.ts +84 -0
  41. package/dist/select-date-range-DWik3ADr.d.cts +84 -0
  42. package/dist/select-date-range.cjs +2074 -0
  43. package/dist/select-date-range.d.cts +3 -0
  44. package/dist/select-date-range.d.ts +3 -0
  45. package/dist/select-date-range.js +8 -0
  46. package/package.json +129 -0
@@ -0,0 +1,298 @@
1
+ import {
2
+ Dropdown,
3
+ Input
4
+ } from "./chunk-DZ45HRVP.js";
5
+ import {
6
+ Button
7
+ } from "./chunk-MBS2HXLZ.js";
8
+ import {
9
+ Icon
10
+ } from "./chunk-YCVXUMGP.js";
11
+
12
+ // src/components/RichTextEditor.tsx
13
+ import { useEffect, useMemo, useRef, useState } from "react";
14
+ import DOMPurify from "dompurify";
15
+ import { Editor as TiptapEditor } from "@tiptap/core";
16
+ import StarterKit from "@tiptap/starter-kit";
17
+ import Placeholder from "@tiptap/extension-placeholder";
18
+ import Image from "@tiptap/extension-image";
19
+ import { Table } from "@tiptap/extension-table";
20
+ import TableRow from "@tiptap/extension-table-row";
21
+ import TableHeader from "@tiptap/extension-table-header";
22
+ import TableCell from "@tiptap/extension-table-cell";
23
+ import TaskList from "@tiptap/extension-task-list";
24
+ import TaskItem from "@tiptap/extension-task-item";
25
+ import { Markdown } from "@tiptap/markdown";
26
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
27
+ function escapeHtml(value) {
28
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#039;");
29
+ }
30
+ function normalizeContent(value) {
31
+ if (!value.trim()) return "";
32
+ if (/<\/?[a-z][\s\S]*>/i.test(value)) return value;
33
+ return escapeHtml(value).replace(/\r?\n/g, "<br>");
34
+ }
35
+ function sanitizeContent(value) {
36
+ return DOMPurify.sanitize(normalizeContent(value), {
37
+ USE_PROFILES: { html: true },
38
+ ADD_ATTR: ["loading", "decoding"]
39
+ }).replace(/<img\b/giu, '<img loading="lazy" decoding="async"');
40
+ }
41
+ function RichTextContent({ content, emptyText = "\u6682\u65E0\u5185\u5BB9", className = "", ...props }) {
42
+ const html = useMemo(() => sanitizeContent(content), [content]);
43
+ if (!html.trim()) return /* @__PURE__ */ jsx("div", { className: `sia-rich-text-content sia-rich-text-content--empty ${className}`.trim(), children: emptyText });
44
+ return /* @__PURE__ */ jsx("article", { className: `sia-rich-text-content ${className}`.trim(), dangerouslySetInnerHTML: { __html: html }, ...props });
45
+ }
46
+ function RichTextEditor({
47
+ value,
48
+ defaultValue = "",
49
+ contentFormat = "html",
50
+ toolbar = "basic",
51
+ placeholder = "\u8BF7\u8F93\u5165\u5185\u5BB9",
52
+ disabled = false,
53
+ minHeight = 150,
54
+ pasteImageHint = "\u53EF\u76F4\u63A5\u7C98\u8D34\u56FE\u7247",
55
+ onChange,
56
+ onPasteImage,
57
+ saveShortcut,
58
+ className = "",
59
+ id,
60
+ style,
61
+ "aria-label": ariaLabel,
62
+ ...props
63
+ }) {
64
+ const initialValueRef = useRef(value ?? defaultValue);
65
+ const lastEmittedValueRef = useRef(value ?? defaultValue);
66
+ const onChangeRef = useRef(onChange);
67
+ const onPasteImageRef = useRef(onPasteImage);
68
+ const saveShortcutRef = useRef(saveShortcut);
69
+ const contentFormatRef = useRef(contentFormat);
70
+ const editorElementRef = useRef(null);
71
+ const editorRef = useRef(null);
72
+ const [editor, setEditor] = useState(null);
73
+ const [linkEditorOpen, setLinkEditorOpen] = useState(false);
74
+ const [linkHref, setLinkHref] = useState("");
75
+ const [, setEditorRevision] = useState(0);
76
+ useEffect(() => {
77
+ onChangeRef.current = onChange;
78
+ }, [onChange]);
79
+ useEffect(() => {
80
+ onPasteImageRef.current = onPasteImage;
81
+ }, [onPasteImage]);
82
+ useEffect(() => {
83
+ saveShortcutRef.current = saveShortcut;
84
+ }, [saveShortcut]);
85
+ useEffect(() => {
86
+ contentFormatRef.current = contentFormat;
87
+ }, [contentFormat]);
88
+ const extensions = useMemo(() => [
89
+ StarterKit.configure({
90
+ heading: { levels: [1, 2, 3, 4, 5, 6] },
91
+ link: { openOnClick: false, autolink: true, linkOnPaste: true },
92
+ underline: { HTMLAttributes: { "data-sia-underline": "" } }
93
+ }),
94
+ Placeholder.configure({ placeholder }),
95
+ Image.configure({ allowBase64: false, inline: false }),
96
+ Table.configure({ resizable: true }),
97
+ TableRow,
98
+ TableHeader,
99
+ TableCell,
100
+ TaskList,
101
+ TaskItem.configure({ nested: true }),
102
+ Markdown.configure({ markedOptions: { gfm: true } })
103
+ ], [placeholder]);
104
+ useEffect(() => {
105
+ const element = editorElementRef.current;
106
+ if (!element) return;
107
+ const nextEditor = new TiptapEditor({
108
+ element,
109
+ extensions,
110
+ content: initialValueRef.current,
111
+ contentType: contentFormatRef.current,
112
+ editable: !disabled,
113
+ editorProps: {
114
+ attributes: {
115
+ role: "textbox",
116
+ "aria-label": ariaLabel ?? "\u5BCC\u6587\u672C\u7F16\u8F91\u5668",
117
+ "aria-multiline": "true"
118
+ },
119
+ handlePaste: (_view, event) => {
120
+ const pasteHandler = onPasteImageRef.current;
121
+ if (!pasteHandler) return false;
122
+ const images = Array.from(event.clipboardData?.items ?? []).filter((item) => item.kind === "file" && item.type.startsWith("image/")).map((item) => item.getAsFile()).filter((file) => Boolean(file));
123
+ if (!images.length) return false;
124
+ event.preventDefault();
125
+ void (async () => {
126
+ for (const file of images) {
127
+ try {
128
+ const src = await pasteHandler(file);
129
+ if (src) nextEditor.chain().focus().setImage({ src, alt: file.name || "\u7C98\u8D34\u7684\u56FE\u7247" }).run();
130
+ } catch {
131
+ }
132
+ }
133
+ })();
134
+ return true;
135
+ },
136
+ handleKeyDown: (_view, event) => {
137
+ if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "s" && saveShortcutRef.current) {
138
+ event.preventDefault();
139
+ saveShortcutRef.current();
140
+ return true;
141
+ }
142
+ return false;
143
+ }
144
+ },
145
+ onSelectionUpdate: () => setEditorRevision((current) => current + 1),
146
+ onUpdate: ({ editor: currentEditor }) => {
147
+ setEditorRevision((current) => current + 1);
148
+ const nextValue = contentFormatRef.current === "markdown" ? currentEditor.getMarkdown() : currentEditor.getHTML();
149
+ lastEmittedValueRef.current = nextValue;
150
+ onChangeRef.current?.(nextValue);
151
+ }
152
+ });
153
+ editorRef.current = nextEditor;
154
+ setEditor(nextEditor);
155
+ return () => {
156
+ editorRef.current = null;
157
+ setEditor(null);
158
+ nextEditor.destroy();
159
+ };
160
+ }, [ariaLabel, extensions]);
161
+ useEffect(() => {
162
+ editor?.setEditable(!disabled);
163
+ }, [disabled, editor]);
164
+ useEffect(() => {
165
+ if (!editor || value === void 0 || value === lastEmittedValueRef.current) return;
166
+ editor.commands.setContent(value, { emitUpdate: false, contentType: contentFormat });
167
+ lastEmittedValueRef.current = value;
168
+ }, [contentFormat, editor, value]);
169
+ function openLinkEditor(open) {
170
+ if (open && editor) setLinkHref(String(editor.getAttributes("link").href ?? ""));
171
+ setLinkEditorOpen(open);
172
+ }
173
+ function submitLink(event) {
174
+ event?.preventDefault();
175
+ if (!editor) return;
176
+ if (!linkHref.trim()) {
177
+ editor.chain().focus().extendMarkRange("link").unsetLink().run();
178
+ } else {
179
+ editor.chain().focus().extendMarkRange("link").setLink({ href: linkHref.trim() }).run();
180
+ }
181
+ setLinkEditorOpen(false);
182
+ }
183
+ function formatButton(label, action, active = false, disabledByState = false, content) {
184
+ return /* @__PURE__ */ jsx(
185
+ Button,
186
+ {
187
+ type: "button",
188
+ variant: "text",
189
+ size: "small",
190
+ className: `sia-rich-text-editor__tool${active ? " is-active" : ""}`,
191
+ disabled: disabled || disabledByState || !editor,
192
+ "aria-label": label,
193
+ "aria-pressed": active || void 0,
194
+ title: label,
195
+ onMouseDown: (event) => event.preventDefault(),
196
+ onClick: action,
197
+ children: content ?? label
198
+ }
199
+ );
200
+ }
201
+ const mergedStyle = {
202
+ ...style,
203
+ "--sia-rich-text-min-height": typeof minHeight === "number" ? `${minHeight}px` : minHeight
204
+ };
205
+ const currentBlockLabel = editor?.isActive("heading", { level: 1 }) ? "H1" : editor?.isActive("heading", { level: 2 }) ? "H2" : editor?.isActive("heading", { level: 3 }) ? "H3" : "\u6B63\u6587";
206
+ const blockTypeMenu = {
207
+ selectable: true,
208
+ selectedKeys: [editor?.isActive("heading", { level: 1 }) ? "h1" : editor?.isActive("heading", { level: 2 }) ? "h2" : editor?.isActive("heading", { level: 3 }) ? "h3" : "paragraph"],
209
+ items: [
210
+ { key: "paragraph", label: "\u6B63\u6587" },
211
+ { key: "h1", label: "\u4E00\u7EA7\u6807\u9898" },
212
+ { key: "h2", label: "\u4E8C\u7EA7\u6807\u9898" },
213
+ { key: "h3", label: "\u4E09\u7EA7\u6807\u9898" }
214
+ ],
215
+ onClick: ({ key }) => {
216
+ if (!editor) return;
217
+ if (key === "paragraph") editor.chain().focus().setParagraph().run();
218
+ else editor.chain().focus().toggleHeading({ level: Number(key.slice(1)) }).run();
219
+ }
220
+ };
221
+ return /* @__PURE__ */ jsxs("div", { ...props, id, style: mergedStyle, className: `sia-rich-text-editor${disabled ? " is-disabled" : ""} sia-rich-text-editor--${toolbar} ${className}`.trim(), children: [
222
+ /* @__PURE__ */ jsxs("div", { className: "sia-rich-text-editor__toolbar", "aria-label": "\u6587\u672C\u683C\u5F0F\u5DE5\u5177\u680F", children: [
223
+ /* @__PURE__ */ jsxs("div", { className: "sia-rich-text-editor__toolbar-main", children: [
224
+ toolbar === "full" ? /* @__PURE__ */ jsxs(Fragment, { children: [
225
+ formatButton("\u64A4\u9500", () => editor?.chain().focus().undo().run(), false, !editor?.can().undo(), /* @__PURE__ */ jsx(Icon, { name: "undo" })),
226
+ formatButton("\u91CD\u505A", () => editor?.chain().focus().redo().run(), false, !editor?.can().redo(), /* @__PURE__ */ jsx(Icon, { name: "redo" })),
227
+ /* @__PURE__ */ jsx("span", { className: "sia-rich-text-editor__divider" }),
228
+ /* @__PURE__ */ jsx(Dropdown, { menu: blockTypeMenu, trigger: ["click"], placement: "bottomLeft", disabled: disabled || !editor, children: /* @__PURE__ */ jsxs(Button, { type: "button", variant: "text", size: "small", className: "sia-rich-text-editor__block-type", disabled: disabled || !editor, "aria-label": "\u6BB5\u843D\u683C\u5F0F", title: "\u6BB5\u843D\u683C\u5F0F", onMouseDown: (event) => event.preventDefault(), children: [
229
+ currentBlockLabel,
230
+ /* @__PURE__ */ jsx(Icon, { name: "chevron-down", size: 13 })
231
+ ] }) }),
232
+ /* @__PURE__ */ jsx("span", { className: "sia-rich-text-editor__divider" })
233
+ ] }) : null,
234
+ formatButton("\u52A0\u7C97", () => editor?.chain().focus().toggleBold().run(), Boolean(editor?.isActive("bold")), false, /* @__PURE__ */ jsx("strong", { children: "B" })),
235
+ formatButton("\u659C\u4F53", () => editor?.chain().focus().toggleItalic().run(), Boolean(editor?.isActive("italic")), false, /* @__PURE__ */ jsx("em", { children: "I" })),
236
+ toolbar === "full" ? /* @__PURE__ */ jsxs(Fragment, { children: [
237
+ formatButton("\u4E0B\u5212\u7EBF", () => editor?.chain().focus().toggleUnderline().run(), Boolean(editor?.isActive("underline")), false, /* @__PURE__ */ jsx("u", { children: "U" })),
238
+ formatButton("\u5220\u9664\u7EBF", () => editor?.chain().focus().toggleStrike().run(), Boolean(editor?.isActive("strike")), false, /* @__PURE__ */ jsx("s", { children: "S" })),
239
+ formatButton("\u884C\u5185\u4EE3\u7801", () => editor?.chain().focus().toggleCode().run(), Boolean(editor?.isActive("code")), false, "</>"),
240
+ /* @__PURE__ */ jsx(
241
+ Dropdown,
242
+ {
243
+ menu: { items: [] },
244
+ trigger: ["click"],
245
+ placement: "bottomLeft",
246
+ open: linkEditorOpen,
247
+ disabled: disabled || !editor,
248
+ onOpenChange: openLinkEditor,
249
+ popupClassName: "sia-rich-text-editor__link-popup",
250
+ popupRender: () => /* @__PURE__ */ jsxs("form", { className: "sia-rich-text-editor__link-form", onSubmit: submitLink, children: [
251
+ /* @__PURE__ */ jsx(Input, { autoFocus: true, "aria-label": "\u94FE\u63A5\u5730\u5740", value: linkHref, onChange: (event) => setLinkHref(event.target.value), placeholder: "https://example.com" }),
252
+ /* @__PURE__ */ jsx(Button, { type: "submit", variant: "primary", size: "small", children: "\u5E94\u7528" }),
253
+ editor?.isActive("link") ? /* @__PURE__ */ jsx(Button, { type: "button", size: "small", onClick: () => {
254
+ setLinkHref("");
255
+ editor.chain().focus().extendMarkRange("link").unsetLink().run();
256
+ setLinkEditorOpen(false);
257
+ }, children: "\u79FB\u9664" }) : null
258
+ ] }),
259
+ children: /* @__PURE__ */ jsx(Button, { type: "button", variant: "text", size: "small", className: `sia-rich-text-editor__tool${editor?.isActive("link") ? " is-active" : ""}`, disabled: disabled || !editor, "aria-label": "\u94FE\u63A5", "aria-pressed": editor?.isActive("link") || void 0, title: "\u94FE\u63A5", onMouseDown: (event) => event.preventDefault(), children: /* @__PURE__ */ jsx(Icon, { name: "link" }) })
260
+ }
261
+ ),
262
+ /* @__PURE__ */ jsx("span", { className: "sia-rich-text-editor__divider" })
263
+ ] }) : null,
264
+ formatButton("\u65E0\u5E8F\u5217\u8868", () => editor?.chain().focus().toggleBulletList().run(), Boolean(editor?.isActive("bulletList")), false, "\u2022 \u5217\u8868"),
265
+ formatButton("\u6709\u5E8F\u5217\u8868", () => editor?.chain().focus().toggleOrderedList().run(), Boolean(editor?.isActive("orderedList")), false, "1. \u5217\u8868"),
266
+ toolbar === "full" ? /* @__PURE__ */ jsxs(Fragment, { children: [
267
+ formatButton("\u4EFB\u52A1\u5217\u8868", () => editor?.chain().focus().toggleTaskList().run(), Boolean(editor?.isActive("taskList")), false, "\u2611 \u4EFB\u52A1"),
268
+ formatButton("\u5F15\u7528", () => editor?.chain().focus().toggleBlockquote().run(), Boolean(editor?.isActive("blockquote")), false, "\u275D \u5F15\u7528"),
269
+ formatButton("\u4EE3\u7801\u5757", () => editor?.chain().focus().toggleCodeBlock().run(), Boolean(editor?.isActive("codeBlock")), false, "\u4EE3\u7801\u5757"),
270
+ formatButton("\u63D2\u5165\u8868\u683C", () => editor?.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(), Boolean(editor?.isActive("table")), false, "\u8868\u683C"),
271
+ formatButton("\u5206\u5272\u7EBF", () => editor?.chain().focus().setHorizontalRule().run(), false, false, /* @__PURE__ */ jsx(Icon, { name: "minus" }))
272
+ ] }) : null
273
+ ] }),
274
+ onPasteImage ? /* @__PURE__ */ jsxs("small", { children: [
275
+ /* @__PURE__ */ jsx(Icon, { name: "image", size: 14 }),
276
+ pasteImageHint
277
+ ] }) : null
278
+ ] }),
279
+ toolbar === "full" && editor?.isActive("table") ? /* @__PURE__ */ jsxs("div", { className: "sia-rich-text-editor__table-toolbar", "aria-label": "\u8868\u683C\u5DE5\u5177\u680F", children: [
280
+ /* @__PURE__ */ jsx("span", { children: "\u8868\u683C" }),
281
+ formatButton("\u524D\u65B9\u63D2\u5165\u5217", () => editor.chain().focus().addColumnBefore().run(), false, !editor.can().addColumnBefore(), "\u524D\u63D2\u5217"),
282
+ formatButton("\u540E\u65B9\u63D2\u5165\u5217", () => editor.chain().focus().addColumnAfter().run(), false, !editor.can().addColumnAfter(), "\u540E\u63D2\u5217"),
283
+ formatButton("\u5220\u9664\u5F53\u524D\u5217", () => editor.chain().focus().deleteColumn().run(), false, !editor.can().deleteColumn(), "\u5220\u5217"),
284
+ /* @__PURE__ */ jsx("span", { className: "sia-rich-text-editor__divider" }),
285
+ formatButton("\u4E0A\u65B9\u63D2\u5165\u884C", () => editor.chain().focus().addRowBefore().run(), false, !editor.can().addRowBefore(), "\u4E0A\u63D2\u884C"),
286
+ formatButton("\u4E0B\u65B9\u63D2\u5165\u884C", () => editor.chain().focus().addRowAfter().run(), false, !editor.can().addRowAfter(), "\u4E0B\u63D2\u884C"),
287
+ formatButton("\u5220\u9664\u5F53\u524D\u884C", () => editor.chain().focus().deleteRow().run(), false, !editor.can().deleteRow(), "\u5220\u884C"),
288
+ /* @__PURE__ */ jsx("span", { className: "sia-rich-text-editor__divider" }),
289
+ formatButton("\u5220\u9664\u8868\u683C", () => editor.chain().focus().deleteTable().run(), false, !editor.can().deleteTable(), "\u5220\u9664\u8868\u683C")
290
+ ] }) : null,
291
+ /* @__PURE__ */ jsx("div", { ref: editorElementRef, className: "sia-rich-text-editor__content" })
292
+ ] });
293
+ }
294
+
295
+ export {
296
+ RichTextContent,
297
+ RichTextEditor
298
+ };