@vokality/design-system 2.0.0 → 3.0.1
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/CHANGELOG.md +35 -0
- package/README.md +63 -3
- package/bin/vokality-skill.mjs +68 -0
- package/dist/browser.d.ts +13 -0
- package/dist/browser.js +7 -0
- package/dist/charts/chart.d.ts +6 -5
- package/dist/charts/chart.js +44 -25
- package/dist/chat.d.ts +54 -0
- package/dist/chat.js +1 -0
- package/dist/components/Accordion/Accordion.js +1 -1
- package/dist/components/Alert/Alert.js +5 -2
- package/dist/components/AppShell/AppShell.js +10 -14
- package/dist/components/Avatar/Avatar.js +2 -2
- package/dist/components/AvatarGroup/AvatarGroup.js +2 -2
- package/dist/components/BarChart/BarChart.js +17 -15
- package/dist/components/Breadcrumbs/Breadcrumbs.js +6 -3
- package/dist/components/Calendar/Calendar.js +6 -5
- package/dist/components/Card/Card.js +2 -3
- package/dist/components/ChatInbox/ChatInbox.d.ts +11 -0
- package/dist/components/ChatInbox/ChatInbox.js +49 -0
- package/dist/components/ChatPanel/ChatPanel.d.ts +3 -0
- package/dist/components/ChatPanel/ChatPanel.js +13 -0
- package/dist/components/ChatPanel/composer.d.ts +5 -0
- package/dist/components/ChatPanel/composer.js +32 -0
- package/dist/components/ChatPanel/transcript.d.ts +13 -0
- package/dist/components/ChatPanel/transcript.js +39 -0
- package/dist/components/ChatWidget/ChatWidget.d.ts +12 -0
- package/dist/components/ChatWidget/ChatWidget.js +42 -0
- package/dist/components/Checkbox/Checkbox.d.ts +5 -10
- package/dist/components/Checkbox/Checkbox.js +5 -19
- package/dist/components/CodeBlock/CodeBlock.js +19 -5
- package/dist/components/Combobox/Combobox.js +22 -15
- package/dist/components/Combobox/ListPanel.js +20 -9
- package/dist/components/CommandPalette/CommandPalette.js +33 -14
- package/dist/components/ContextMenu/ContextMenu.d.ts +2 -2
- package/dist/components/ContextMenu/ContextMenu.js +5 -50
- package/dist/components/DatePicker/DatePicker.js +35 -29
- package/dist/components/DescriptionList/DescriptionList.js +1 -1
- package/dist/components/Dialog/Dialog.d.ts +24 -7
- package/dist/components/Dialog/Dialog.js +30 -41
- package/dist/components/Drawer/Drawer.d.ts +4 -2
- package/dist/components/EmptyState/EmptyState.js +2 -2
- package/dist/components/ErrorSummary/ErrorSummary.js +5 -3
- package/dist/components/FaqList/FaqList.js +1 -1
- package/dist/components/Field/Field.js +2 -2
- package/dist/components/FileUpload/FileUpload.js +17 -9
- package/dist/components/Grid/Grid.d.ts +5 -5
- package/dist/components/Grid/Grid.js +1 -1
- package/dist/components/Heading/Heading.js +2 -1
- package/dist/components/Icon/Icon.js +4 -2
- package/dist/components/Inline/Inline.d.ts +7 -7
- package/dist/components/LineChart/LineChart.js +17 -15
- package/dist/components/LinkProvider/LinkProvider.d.ts +1 -1
- package/dist/components/LinkProvider/LinkProvider.js +6 -2
- package/dist/components/List/List.js +1 -1
- package/dist/components/MediaCard/MediaCard.js +2 -2
- package/dist/components/Menu/Menu.js +6 -43
- package/dist/components/Menu/entries.d.ts +1 -30
- package/dist/components/Menu/entries.js +16 -100
- package/dist/components/MultiSelect/MultiSelect.js +43 -34
- package/dist/components/NumberInput/NumberInput.js +17 -18
- package/dist/components/OtpInput/OtpInput.d.ts +1 -1
- package/dist/components/OtpInput/OtpInput.js +10 -67
- package/dist/components/PageHeader/PageHeader.js +2 -2
- package/dist/components/Pagination/Pagination.js +3 -1
- package/dist/components/PasswordInput/PasswordInput.d.ts +1 -1
- package/dist/components/PasswordInput/PasswordInput.js +9 -4
- package/dist/components/Popover/Popover.d.ts +6 -7
- package/dist/components/Popover/Popover.js +2 -19
- package/dist/components/Progress/Progress.d.ts +1 -1
- package/dist/components/Progress/Progress.js +7 -11
- package/dist/components/Radio/Radio.d.ts +4 -2
- package/dist/components/RadioGroup/RadioGroup.d.ts +5 -12
- package/dist/components/RadioGroup/RadioGroup.js +6 -33
- package/dist/components/RichTextEditor/RichTextEditor.d.ts +63 -0
- package/dist/components/RichTextEditor/RichTextEditor.js +175 -0
- package/dist/components/RichTextEditor/toolbar.d.ts +15 -0
- package/dist/components/RichTextEditor/toolbar.js +142 -0
- package/dist/components/RichTextEditor/uploads.d.ts +41 -0
- package/dist/components/RichTextEditor/uploads.js +124 -0
- package/dist/components/SearchInput/SearchInput.js +4 -2
- package/dist/components/SectionHeader/SectionHeader.js +1 -3
- package/dist/components/SegmentedControl/SegmentedControl.js +2 -12
- package/dist/components/Select/Select.d.ts +15 -16
- package/dist/components/Select/Select.js +11 -252
- package/dist/components/Slider/Slider.js +11 -21
- package/dist/components/Sparkline/Sparkline.js +8 -3
- package/dist/components/Spinner/Spinner.js +5 -2
- package/dist/components/Stack/Stack.d.ts +5 -5
- package/dist/components/Stat/Stat.js +6 -3
- package/dist/components/Steps/Steps.js +6 -3
- package/dist/components/Switch/Switch.d.ts +7 -7
- package/dist/components/Switch/Switch.js +4 -10
- package/dist/components/Table/Table.js +38 -19
- package/dist/components/Tabs/Tabs.js +2 -22
- package/dist/components/Tag/Tag.js +3 -1
- package/dist/components/Text/Text.d.ts +1 -1
- package/dist/components/TileGrid/TileGrid.js +1 -1
- package/dist/components/TimePicker/TimePicker.js +35 -33
- package/dist/components/Timeline/Timeline.js +5 -4
- package/dist/components/Toaster/Toaster.js +21 -17
- package/dist/components/Tooltip/Tooltip.d.ts +6 -6
- package/dist/components/Tooltip/Tooltip.js +3 -17
- package/dist/components/TreeView/TreeView.js +15 -12
- package/dist/components.css +179 -8
- package/dist/hooks/useTheme.js +6 -6
- package/dist/icons/message-circle.svg +1 -0
- package/dist/internal/CharCount.js +3 -1
- package/dist/internal/choice.d.ts +10 -48
- package/dist/internal/choice.js +8 -96
- package/dist/internal/options.d.ts +10 -5
- package/dist/internal/options.js +25 -11
- package/dist/internal/status.d.ts +3 -2
- package/dist/internal/status.js +10 -2
- package/dist/internal/useScrollActive.js +2 -2
- package/dist/internal/useToday.d.ts +3 -0
- package/dist/internal/useToday.js +42 -0
- package/dist/lib/dates.js +25 -15
- package/dist/lib/format.js +11 -8
- package/dist/lib/icons.d.ts +5 -0
- package/dist/lib/icons.js +2 -0
- package/dist/lib/math.js +17 -5
- package/dist/lib/rich-text.d.ts +13 -0
- package/dist/lib/rich-text.js +62 -0
- package/dist/lib/space.d.ts +1 -2
- package/dist/lib/space.js +4 -1
- package/dist/lib/text.js +1 -1
- package/dist/lib/theme.js +1 -1
- package/dist/localization.d.ts +166 -0
- package/dist/localization.js +142 -0
- package/dist/vokality.css +179 -8
- package/dist/vokality.iife.js +35986 -9685
- package/docs/brand.md +6 -3
- package/docs/components/AppShell/README.md +1 -1
- package/docs/components/BarChart/README.md +1 -0
- package/docs/components/ChatInbox/README.md +41 -0
- package/docs/components/ChatWidget/README.md +47 -0
- package/docs/components/Checkbox/README.md +7 -3
- package/docs/components/ContextMenu/README.md +7 -3
- package/docs/components/Dialog/README.md +9 -4
- package/docs/components/Drawer/README.md +1 -1
- package/docs/components/Field/README.md +1 -1
- package/docs/components/Fieldset/README.md +2 -2
- package/docs/components/LineChart/README.md +1 -0
- package/docs/components/LocaleProvider/README.md +48 -0
- package/docs/components/Menu/README.md +13 -8
- package/docs/components/OtpInput/README.md +9 -5
- package/docs/components/PasswordInput/README.md +11 -2
- package/docs/components/Popover/README.md +6 -3
- package/docs/components/Progress/README.md +3 -1
- package/docs/components/RadioGroup/README.md +5 -2
- package/docs/components/RichTextEditor/README.md +84 -0
- package/docs/components/SegmentedControl/README.md +2 -0
- package/docs/components/Select/README.md +13 -17
- package/docs/components/Skeleton/README.md +1 -1
- package/docs/components/Slider/README.md +4 -2
- package/docs/components/Steps/README.md +1 -1
- package/docs/components/Switch/README.md +5 -2
- package/docs/components/Tabs/README.md +4 -2
- package/docs/components/Toaster/README.md +3 -1
- package/docs/components/Tooltip/README.md +5 -3
- package/package.json +55 -6
- package/skills/vokality-design-system/SKILL.md +55 -0
- package/skills/vokality-design-system/references/examples.tsx +103 -0
|
@@ -1,26 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { useRef, useState } from "react";
|
|
4
3
|
import { Popover as Primitive } from "radix-ui";
|
|
5
4
|
import { Icon } from "../Icon/Icon.js";
|
|
6
|
-
import { focusables } from "../../internal/focusables.js";
|
|
7
5
|
import { cx } from "../../lib/cx.js";
|
|
8
6
|
/** Non-modal panel attached to a button (Radix Popover). */
|
|
9
|
-
export function Popover({ label, title, icon, variant, size, align, width, defaultOpen, children, className }) {
|
|
10
|
-
|
|
11
|
-
const popRef = useRef(null);
|
|
12
|
-
// Focus moves into the panel when it opens, not when it mounts already open.
|
|
13
|
-
const opened = useRef(false);
|
|
14
|
-
const onOpenChange = (next) => {
|
|
15
|
-
if (next)
|
|
16
|
-
opened.current = true;
|
|
17
|
-
setOpen(next);
|
|
18
|
-
};
|
|
19
|
-
return (_jsx("div", { className: cx("vk-menu-root", className), children: _jsxs(Primitive.Root, { open: open, onOpenChange: onOpenChange, children: [_jsxs(Primitive.Trigger, { className: cx("vk-btn", "vk-btn-" + (variant || "outline"), size === "sm" ? "vk-btn-sm" : null), children: [_jsxs("span", { className: "vk-btn-label", children: [icon ? _jsx(Icon, { name: icon, size: 16, className: "vk-menu-trigger-icon" }) : null, label] }), _jsx("span", { className: "vk-btn-glyph", "aria-hidden": "true", children: _jsx(Icon, { name: "chevron-down", size: 14 }) })] }), _jsx(Primitive.Portal, { children: _jsxs(Primitive.Content, { ref: popRef, "aria-label": title || label, className: "vk-popover vk-popover-panel", side: "bottom", align: align === "end" ? "end" : "start", sideOffset: 4, collisionPadding: 8, style: width ? { width } : undefined, onOpenAutoFocus: (e) => {
|
|
20
|
-
e.preventDefault();
|
|
21
|
-
if (!opened.current || !popRef.current)
|
|
22
|
-
return;
|
|
23
|
-
const f = focusables(popRef.current);
|
|
24
|
-
(f[0] || popRef.current).focus();
|
|
25
|
-
}, children: [title ? _jsx("p", { className: "vk-popover-title", children: title }) : null, typeof children === "function" ? children({ close: () => setOpen(false) }) : children] }) })] }) }));
|
|
7
|
+
export function Popover({ label, title, icon, variant, size, align, width, open, defaultOpen, onOpenChange, modal, children, close, className }) {
|
|
8
|
+
return (_jsx("div", { className: cx("vk-menu-root", className), children: _jsxs(Primitive.Root, { open: open, defaultOpen: defaultOpen, onOpenChange: onOpenChange, modal: modal, children: [_jsxs(Primitive.Trigger, { className: cx("vk-btn", "vk-btn-" + (variant || "outline"), size === "sm" ? "vk-btn-sm" : null), children: [_jsxs("span", { className: "vk-btn-label", children: [icon ? _jsx(Icon, { name: icon, size: 16, className: "vk-menu-trigger-icon" }) : null, label] }), _jsx("span", { className: "vk-btn-glyph", "aria-hidden": "true", children: _jsx(Icon, { name: "chevron-down", size: 14 }) })] }), _jsx(Primitive.Portal, { children: _jsxs(Primitive.Content, { "aria-label": title || label, className: "vk-popover vk-popover-panel", side: "bottom", align: align === "end" ? "end" : "start", sideOffset: 4, collisionPadding: 8, style: width ? { width } : undefined, children: [title ? _jsx("p", { className: "vk-popover-title", children: title }) : null, children, close ? _jsx("div", { className: "vk-popover-footer", children: _jsx(Primitive.Close, { asChild: true, children: close }) }) : null] }) })] }) }));
|
|
26
9
|
}
|
|
@@ -1,24 +1,20 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useLocale } from "../../localization.js";
|
|
3
4
|
import { Progress as Primitive } from "radix-ui";
|
|
4
5
|
import { useUid } from "../../internal/useUid.js";
|
|
5
6
|
import { cx } from "../../lib/cx.js";
|
|
6
|
-
function measure(value, maxProp) {
|
|
7
|
-
const max = maxProp && maxProp > 0 ? maxProp : 100;
|
|
8
|
-
// Radix treats a value outside 0..max as indeterminate, so clamp it.
|
|
9
|
-
const now = value == null ? null : Math.max(0, Math.min(max, value));
|
|
10
|
-
const pct = now == null ? 0 : (now / max) * 100;
|
|
11
|
-
return { max, now, pct };
|
|
12
|
-
}
|
|
13
7
|
// The label and the value above the bar; the value is left out while indeterminate.
|
|
14
8
|
function ProgressRow({ label, labelId, value }) {
|
|
15
9
|
return (_jsxs("div", { className: "vk-progress-row", children: [label ? _jsx("span", { id: labelId, className: "vk-progress-label", children: label }) : _jsx("span", {}), value != null ? _jsx("span", { className: "vk-progress-value", children: value }) : null] }));
|
|
16
10
|
}
|
|
17
11
|
/** A bar showing how far a task has got, or that it is still running (Radix Progress). */
|
|
18
12
|
export function Progress({ value, max: maxProp, label, showValue, valueText, tone, "aria-label": ariaLabel, className }) {
|
|
19
|
-
const {
|
|
20
|
-
const
|
|
13
|
+
const { messages } = useLocale();
|
|
14
|
+
const max = maxProp ?? 100;
|
|
15
|
+
const indeterminate = value == null;
|
|
16
|
+
const pct = value == null ? 0 : (value / max) * 100;
|
|
21
17
|
const labelId = useUid("vk-progress");
|
|
22
|
-
const shownValue = showValue && !indeterminate ? valueText || Math.round(pct) + "%" : null;
|
|
23
|
-
return (_jsxs("div", { className: cx("vk-progress", "vk-progress-" + (tone || "primary"), className), children: [label || showValue ? _jsx(ProgressRow, { label: label, labelId: labelId, value: shownValue }) : null, _jsx(Primitive.Root, { value:
|
|
18
|
+
const shownValue = showValue && !indeterminate ? valueText || String(Math.round(pct)) + "%" : null;
|
|
19
|
+
return (_jsxs("div", { className: cx("vk-progress", "vk-progress-" + (tone || "primary"), className), children: [label || showValue ? _jsx(ProgressRow, { label: label, labelId: labelId, value: shownValue }) : null, _jsx(Primitive.Root, { value: value, max: max, "aria-labelledby": label ? labelId : undefined, "aria-label": label ? undefined : ariaLabel || messages.progress, getValueLabel: valueText === undefined ? undefined : () => valueText, className: "vk-progress-track", children: indeterminate ? null : _jsx(Primitive.Indicator, { className: "vk-progress-fill", style: { width: String(pct) + "%" } }) })] }));
|
|
24
20
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { InputHTMLAttributes } from "react";
|
|
2
|
+
import type { ChoiceTextProps } from "../../internal/choice.js";
|
|
3
|
+
export interface RadioProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "type">, ChoiceTextProps {
|
|
4
|
+
}
|
|
3
5
|
/**
|
|
4
6
|
* Square radio (inner square dot) with label and optional description. A native radio, since Radix radios only
|
|
5
7
|
* work inside a group; `RadioGroup` draws the same box with Radix RadioGroup.
|
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { ComponentPropsWithoutRef, ReactNode } from "react";
|
|
2
|
+
import { RadioGroup as Primitive } from "radix-ui";
|
|
3
|
+
export interface RadioGroupProps extends Omit<ComponentPropsWithoutRef<typeof Primitive.Root>, "asChild" | "children" | "onChange"> {
|
|
3
4
|
label: ReactNode;
|
|
4
|
-
name: string;
|
|
5
5
|
options: (string | {
|
|
6
6
|
value: string;
|
|
7
7
|
label: ReactNode;
|
|
8
8
|
description?: ReactNode;
|
|
9
9
|
disabled?: boolean;
|
|
10
10
|
})[];
|
|
11
|
-
value?: string;
|
|
12
|
-
defaultValue?: string;
|
|
13
|
-
/** Receives a change event whose target reads `value`, `name` and `checked` like a native radio. */
|
|
14
|
-
onChange?: ChangeEventHandler<HTMLInputElement>;
|
|
15
11
|
inline?: boolean;
|
|
16
12
|
}
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
* Inside a form each option submits through a hidden radio named `name`.
|
|
20
|
-
*/
|
|
21
|
-
export declare function RadioGroup(props: RadioGroupProps): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
/** Styled Radix RadioGroup with a fieldset legend and direct onValueChange(value) callbacks. */
|
|
14
|
+
export declare function RadioGroup({ label, options, inline, className, ...props }: RadioGroupProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,41 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { useRef, useState } from "react";
|
|
4
3
|
import { RadioGroup as Primitive } from "radix-ui";
|
|
5
|
-
import { ChoiceLabel
|
|
4
|
+
import { ChoiceLabel } from "../../internal/choice.js";
|
|
6
5
|
import { useUid } from "../../internal/useUid.js";
|
|
7
6
|
import { cx } from "../../lib/cx.js";
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
* Inside a form each option submits through a hidden radio named `name`.
|
|
11
|
-
*/
|
|
12
|
-
export function RadioGroup(props) {
|
|
13
|
-
const controlled = props.value !== undefined;
|
|
14
|
-
const [own, setOwn] = useState(props.defaultValue ?? null);
|
|
15
|
-
const value = controlled ? props.value : own;
|
|
16
|
-
const click = useRef(null);
|
|
7
|
+
/** Styled Radix RadioGroup with a fieldset legend and direct onValueChange(value) callbacks. */
|
|
8
|
+
export function RadioGroup({ label, options, inline, className, ...props }) {
|
|
17
9
|
const legendId = useUid("vk-radio-group");
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const c = click.current;
|
|
22
|
-
click.current = null;
|
|
23
|
-
if (!c) {
|
|
24
|
-
// The form was reset; as natively, no change event.
|
|
25
|
-
if (!controlled)
|
|
26
|
-
setOwn(props.defaultValue ?? null);
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
if (!controlled)
|
|
30
|
-
setOwn(v);
|
|
31
|
-
props.onChange?.(choiceChangeEvent(c.event, c.control, { type: "radio", checked: true, value: v, name: props.name }));
|
|
32
|
-
};
|
|
33
|
-
return (_jsx(Primitive.Root, { asChild: true, name: props.name, value: value, onValueChange: onValueChange, ...arrows.group, children: _jsxs("fieldset", { className: cx("vk-choice-group", props.inline && "vk-choice-group-inline"), "aria-labelledby": legendId, children: [_jsx("legend", { id: legendId, className: "vk-field-label", children: props.label }), _jsx("div", { className: "vk-choice-list", children: (props.options || []).map((o) => {
|
|
34
|
-
const opt = typeof o === "string" ? { value: o, label: o } : o;
|
|
35
|
-
return (_jsx(ChoiceLabel, { label: opt.label, description: opt.description, disabled: opt.disabled, children: _jsx(Primitive.Item, { className: "vk-choice-box vk-choice-box-radio", value: opt.value, disabled: opt.disabled, ...arrows.item, onClick: (e) => {
|
|
36
|
-
// Radix checks an option on its click (also the one it sends when an arrow key moves focus).
|
|
37
|
-
if (opt.value !== value)
|
|
38
|
-
click.current = { event: e, control: e.currentTarget };
|
|
39
|
-
}, children: _jsx(Primitive.Indicator, { forceMount: true, className: "vk-choice-dot" }) }) }, opt.value));
|
|
10
|
+
return (_jsx(Primitive.Root, { ...props, asChild: true, children: _jsxs("fieldset", { className: cx("vk-choice-group", inline && "vk-choice-group-inline", className), "aria-labelledby": legendId, children: [_jsx("legend", { id: legendId, className: "vk-field-label", children: label }), _jsx("div", { className: "vk-choice-list", children: options.map((option) => {
|
|
11
|
+
const item = typeof option === "string" ? { value: option, label: option } : option;
|
|
12
|
+
return (_jsx(ChoiceLabel, { label: item.label, description: item.description, disabled: props.disabled || item.disabled, children: _jsx(Primitive.Item, { className: "vk-choice-box vk-choice-box-radio", value: item.value, disabled: item.disabled, children: _jsx(Primitive.Indicator, { forceMount: true, className: "vk-choice-dot" }) }) }, item.value));
|
|
40
13
|
}) })] }) }));
|
|
41
14
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type FocusEventHandler } from "react";
|
|
2
|
+
/** A durable, app-managed attachment; its link is persisted in the Markdown document. */
|
|
3
|
+
export interface RichTextAttachment {
|
|
4
|
+
name: string;
|
|
5
|
+
url: string;
|
|
6
|
+
}
|
|
7
|
+
/** The application owns authentication and storage. Abort on signal when an attempt is canceled. */
|
|
8
|
+
export type RichTextUpload = (file: File, options: {
|
|
9
|
+
signal: AbortSignal;
|
|
10
|
+
}) => Promise<RichTextAttachment>;
|
|
11
|
+
/** Unicode emoji and its localized accessible/search label. */
|
|
12
|
+
export interface RichTextEmoji {
|
|
13
|
+
value: string;
|
|
14
|
+
label: string;
|
|
15
|
+
}
|
|
16
|
+
export interface RichTextEditorProps {
|
|
17
|
+
/** Controlled CommonMark document. */
|
|
18
|
+
value?: string;
|
|
19
|
+
/** Initial CommonMark document for an uncontrolled editor. */
|
|
20
|
+
defaultValue?: string;
|
|
21
|
+
/** Emits canonical CommonMark after document changes; selection changes do not emit. */
|
|
22
|
+
onChange?: (markdown: string) => void;
|
|
23
|
+
/** Native form field name; submissions contain the Markdown string. */
|
|
24
|
+
name?: string;
|
|
25
|
+
/** ID of the editable textbox, including when wired by Field. */
|
|
26
|
+
id?: string;
|
|
27
|
+
/** Prevent all changes, uploads and form submission of this field. */
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
/** Keep the document readable/selectable, without editing or a toolbar. */
|
|
30
|
+
readOnly?: boolean;
|
|
31
|
+
/** Full controls above the field, or a small action row with expandable formatting below. */
|
|
32
|
+
toolbar?: "full" | "minimal";
|
|
33
|
+
/** Native form validation focuses the editor when the Markdown field is empty. */
|
|
34
|
+
required?: boolean;
|
|
35
|
+
/** Hint shown inside an empty editor; supply a visible Field label too. */
|
|
36
|
+
placeholder?: string;
|
|
37
|
+
/** Outer wrapper class. */
|
|
38
|
+
className?: string;
|
|
39
|
+
/** Called when focus leaves the editable textbox. */
|
|
40
|
+
onBlur?: FocusEventHandler<HTMLDivElement>;
|
|
41
|
+
/** Enable attachments with an app-provided upload callback. */
|
|
42
|
+
onUploadAttachment?: RichTextUpload;
|
|
43
|
+
/** File chooser accept filter, for example "image/*,.pdf". Enforce server-side too. */
|
|
44
|
+
accept?: string;
|
|
45
|
+
/** Reject files larger than this byte count before calling the upload callback. */
|
|
46
|
+
maxFileSize?: number;
|
|
47
|
+
/** Override the built-in labeled Unicode emoji choices. */
|
|
48
|
+
emojiOptions?: readonly RichTextEmoji[];
|
|
49
|
+
/** Observe upload and callback-contract errors; the UI shows localized retry feedback. */
|
|
50
|
+
onUploadError?: (error: Error, file: File) => void;
|
|
51
|
+
/** Observe the existing upload state, for example to disable a surrounding Send action. */
|
|
52
|
+
onUploadPendingChange?: (pending: boolean) => void;
|
|
53
|
+
/** Accessible name when a visible Field does not provide one. */
|
|
54
|
+
"aria-label"?: string;
|
|
55
|
+
/** Visible label ID, set by Field. */
|
|
56
|
+
"aria-labelledby"?: string;
|
|
57
|
+
/** Hint/error IDs, set by Field. */
|
|
58
|
+
"aria-describedby"?: string;
|
|
59
|
+
/** Invalid state, set by Field. */
|
|
60
|
+
"aria-invalid"?: boolean | "true" | "false" | "grammar" | "spelling";
|
|
61
|
+
}
|
|
62
|
+
/** A ProseMirror editor with CommonMark persistence, formatting, Unicode emoji and attachment uploads. */
|
|
63
|
+
export declare const RichTextEditor: import("react").ForwardRefExoticComponent<RichTextEditorProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { forwardRef, useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { EditorState, Plugin } from "prosemirror-state";
|
|
5
|
+
import { EditorView } from "prosemirror-view";
|
|
6
|
+
import { baseKeymap, chainCommands, exitCode, setBlockType, toggleMark } from "prosemirror-commands";
|
|
7
|
+
import { history, undo, redo } from "prosemirror-history";
|
|
8
|
+
import { keymap } from "prosemirror-keymap";
|
|
9
|
+
import { schema } from "prosemirror-markdown";
|
|
10
|
+
import { liftListItem, sinkListItem, splitListItem, wrapInList } from "prosemirror-schema-list";
|
|
11
|
+
import { hasSafeRichTextUrls, parseRichTextMarkdown, serializeRichTextMarkdown } from "../../lib/rich-text.js";
|
|
12
|
+
import { cx } from "../../lib/cx.js";
|
|
13
|
+
import { useUid } from "../../internal/useUid.js";
|
|
14
|
+
import { useIsoLayoutEffect } from "../../internal/useIsoLayoutEffect.js";
|
|
15
|
+
import { RichTextToolbar } from "./toolbar.js";
|
|
16
|
+
import { RichTextUploads, useRichTextUploads } from "./uploads.js";
|
|
17
|
+
function editorPlugins() {
|
|
18
|
+
const hardBreak = chainCommands(exitCode, (state, dispatch) => {
|
|
19
|
+
if (dispatch)
|
|
20
|
+
dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create()).scrollIntoView());
|
|
21
|
+
return true;
|
|
22
|
+
});
|
|
23
|
+
return [
|
|
24
|
+
new Plugin({ filterTransaction: (transaction) => !transaction.docChanged || hasSafeRichTextUrls(transaction.doc) }),
|
|
25
|
+
history(),
|
|
26
|
+
keymap({
|
|
27
|
+
"Mod-z": undo, "Mod-y": redo, "Shift-Mod-z": redo,
|
|
28
|
+
"Mod-b": toggleMark(schema.marks.strong), "Mod-i": toggleMark(schema.marks.em), "Mod-`": toggleMark(schema.marks.code),
|
|
29
|
+
"Shift-Ctrl-1": setBlockType(schema.nodes.heading, { level: 1 }),
|
|
30
|
+
"Shift-Ctrl-2": setBlockType(schema.nodes.heading, { level: 2 }),
|
|
31
|
+
"Shift-Ctrl-3": setBlockType(schema.nodes.heading, { level: 3 }),
|
|
32
|
+
"Shift-Ctrl-0": setBlockType(schema.nodes.paragraph),
|
|
33
|
+
"Shift-Ctrl-8": wrapInList(schema.nodes.bullet_list), "Shift-Ctrl-9": wrapInList(schema.nodes.ordered_list),
|
|
34
|
+
Enter: splitListItem(schema.nodes.list_item), "Mod-[": liftListItem(schema.nodes.list_item), "Mod-]": sinkListItem(schema.nodes.list_item),
|
|
35
|
+
"Shift-Enter": hardBreak, "Mod-Enter": hardBreak,
|
|
36
|
+
}),
|
|
37
|
+
keymap(baseKeymap),
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
function useRichTextEditor(props) {
|
|
41
|
+
const surfaceRef = useRef(null);
|
|
42
|
+
const viewRef = useRef(null);
|
|
43
|
+
const settings = useRef(props);
|
|
44
|
+
useIsoLayoutEffect(() => { settings.current = props; }, [props]);
|
|
45
|
+
const [initial] = useState(() => props.value ?? props.defaultValue ?? "");
|
|
46
|
+
const [initialDoc] = useState(() => parseRichTextMarkdown(initial));
|
|
47
|
+
const [uncontrolledMarkdown, setUncontrolledMarkdown] = useState(initial);
|
|
48
|
+
const markdown = props.value ?? uncontrolledMarkdown;
|
|
49
|
+
const [editorView, setEditorView] = useState(null);
|
|
50
|
+
const [editorState, setEditorState] = useState(null);
|
|
51
|
+
const locked = !!(props.disabled || props.readOnly);
|
|
52
|
+
const insertAttachment = (attachment) => {
|
|
53
|
+
const view = viewRef.current;
|
|
54
|
+
if (!view || !view.editable || settings.current.disabled || settings.current.readOnly)
|
|
55
|
+
return;
|
|
56
|
+
const link = schema.marks.link.create({ href: attachment.url });
|
|
57
|
+
const paragraph = schema.nodes.paragraph.create(null, schema.text(attachment.name, [link]));
|
|
58
|
+
// Append a separate paragraph: asynchronous completion never replaces a later selection.
|
|
59
|
+
view.dispatch(view.state.tr.insert(view.state.doc.content.size, paragraph));
|
|
60
|
+
};
|
|
61
|
+
const removeAttachment = (attachment) => {
|
|
62
|
+
const view = viewRef.current;
|
|
63
|
+
if (!view || !view.editable || settings.current.disabled || settings.current.readOnly)
|
|
64
|
+
return;
|
|
65
|
+
let match;
|
|
66
|
+
view.state.doc.descendants((node, position) => {
|
|
67
|
+
if (node.isText && node.marks.some((mark) => mark.type === schema.marks.link && mark.attrs.href === attachment.url)) {
|
|
68
|
+
match = match?.to === position ? { from: match.from, to: position + node.nodeSize } : { from: position, to: position + node.nodeSize };
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
if (match)
|
|
72
|
+
view.dispatch(view.state.tr.delete(match.from, match.to));
|
|
73
|
+
};
|
|
74
|
+
const uploads = useRichTextUploads({ upload: props.onUploadAttachment, locked, maxFileSize: props.maxFileSize, insert: insertAttachment, remove: removeAttachment, onError: props.onUploadError, onPendingChange: props.onUploadPendingChange });
|
|
75
|
+
const resetUploads = uploads.reset;
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!surfaceRef.current)
|
|
78
|
+
return;
|
|
79
|
+
const view = new EditorView({ mount: surfaceRef.current }, {
|
|
80
|
+
state: EditorState.create({ doc: initialDoc, plugins: editorPlugins() }),
|
|
81
|
+
editable: () => !(settings.current.disabled || settings.current.readOnly),
|
|
82
|
+
dispatchTransaction(transaction) {
|
|
83
|
+
if (transaction.docChanged && (settings.current.disabled || settings.current.readOnly))
|
|
84
|
+
return;
|
|
85
|
+
const next = view.state.apply(transaction);
|
|
86
|
+
view.updateState(next);
|
|
87
|
+
setEditorState(next);
|
|
88
|
+
if (transaction.docChanged && !next.doc.eq(transaction.before)) {
|
|
89
|
+
const content = serializeRichTextMarkdown(next.doc);
|
|
90
|
+
if (settings.current.value === undefined)
|
|
91
|
+
setUncontrolledMarkdown(content);
|
|
92
|
+
settings.current.onChange?.(content);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
handleClickOn(_view, _position, _node, _nodePosition, event) {
|
|
96
|
+
// Editing links selects text; it never navigates the hosting application.
|
|
97
|
+
return event.target instanceof Element && !!event.target.closest("a");
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
viewRef.current = view;
|
|
101
|
+
setEditorView(view);
|
|
102
|
+
setEditorState(view.state);
|
|
103
|
+
const form = surfaceRef.current.closest("form");
|
|
104
|
+
// Listen after React's delegated root handler, so a canceled onReset is respected.
|
|
105
|
+
const resetTarget = form?.getRootNode();
|
|
106
|
+
const reset = (event) => {
|
|
107
|
+
if (event.target !== form || event.defaultPrevented || view.isDestroyed)
|
|
108
|
+
return;
|
|
109
|
+
resetUploads();
|
|
110
|
+
if (settings.current.value !== undefined)
|
|
111
|
+
return;
|
|
112
|
+
const value = settings.current.defaultValue ?? "";
|
|
113
|
+
view.updateState(EditorState.create({ doc: parseRichTextMarkdown(value), plugins: editorPlugins() }));
|
|
114
|
+
setEditorState(view.state);
|
|
115
|
+
setUncontrolledMarkdown(value);
|
|
116
|
+
};
|
|
117
|
+
resetTarget?.addEventListener("reset", reset);
|
|
118
|
+
return () => {
|
|
119
|
+
resetTarget?.removeEventListener("reset", reset);
|
|
120
|
+
viewRef.current = null;
|
|
121
|
+
view.destroy();
|
|
122
|
+
};
|
|
123
|
+
}, [initialDoc, resetUploads]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
const view = viewRef.current;
|
|
126
|
+
if (!view)
|
|
127
|
+
return;
|
|
128
|
+
view.setProps({ editable: () => !(props.disabled || props.readOnly) });
|
|
129
|
+
}, [props.disabled, props.readOnly]);
|
|
130
|
+
const currentDoc = editorState?.doc;
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
const view = viewRef.current;
|
|
133
|
+
if (props.value === undefined || !view)
|
|
134
|
+
return;
|
|
135
|
+
const document = parseRichTextMarkdown(props.value);
|
|
136
|
+
if (!view.state.doc.eq(document)) {
|
|
137
|
+
resetUploads();
|
|
138
|
+
view.updateState(EditorState.create({ doc: document, plugins: editorPlugins() }));
|
|
139
|
+
setEditorState(view.state);
|
|
140
|
+
}
|
|
141
|
+
}, [props.value, currentDoc, resetUploads]);
|
|
142
|
+
return { surfaceRef, editorView, editorState, initialDoc, markdown, uploads };
|
|
143
|
+
}
|
|
144
|
+
/** The forwarded ref and Field id identify the same ProseMirror-owned textbox. */
|
|
145
|
+
const RichTextSurface = forwardRef(function RichTextSurface({ props, id, document }, ref) {
|
|
146
|
+
return _jsx("div", { ref: ref, id: id, className: "vk-rich-content vk-prose", role: "textbox", "aria-multiline": "true", "aria-label": props["aria-label"], "aria-labelledby": props["aria-labelledby"], "aria-describedby": props["aria-describedby"], "aria-invalid": props["aria-invalid"], "aria-required": props.required || undefined, "aria-disabled": props.disabled || undefined, "aria-readonly": props.readOnly || undefined, tabIndex: props.disabled ? -1 : 0, "data-placeholder": props.placeholder, "data-empty": document.textContent.length === 0 && document.childCount === 1 && document.firstChild?.type === schema.nodes.paragraph, onBlur: props.onBlur });
|
|
147
|
+
});
|
|
148
|
+
/** Native submission/validation and file selection stay separate from the editable DOM. */
|
|
149
|
+
function RichTextFormFields({ props, markdown, editorView, chooser, uploads }) {
|
|
150
|
+
const locked = !!(props.disabled || props.readOnly);
|
|
151
|
+
return _jsxs(_Fragment, { children: [_jsx("textarea", { className: "vk-sr-only", tabIndex: -1, "aria-hidden": "true", name: props.name, value: markdown, disabled: props.disabled, required: props.required && !props.readOnly, onChange: () => { }, onInvalid: (event) => { event.preventDefault(); editorView?.focus(); } }), props.onUploadAttachment ? _jsx("input", { ref: chooser, type: "file", className: "vk-sr-only", tabIndex: -1, "aria-hidden": "true", multiple: true, accept: props.accept, disabled: locked, onChange: (event) => { uploads.add(Array.from(event.currentTarget.files || [])); event.currentTarget.value = ""; } }) : null] });
|
|
152
|
+
}
|
|
153
|
+
/** Minimal composition follows the field in DOM order; the full editor keeps its toolbar first. */
|
|
154
|
+
function RichTextTools({ props, position, view, state, chooser }) {
|
|
155
|
+
const minimal = props.toolbar === "minimal";
|
|
156
|
+
if (props.readOnly || (position === "after") !== minimal)
|
|
157
|
+
return null;
|
|
158
|
+
return _jsx(RichTextToolbar, { variant: props.toolbar, view: view, state: state, disabled: Boolean(props.disabled), emojiOptions: props.emojiOptions, onAttach: props.onUploadAttachment ? () => chooser.current?.click() : undefined });
|
|
159
|
+
}
|
|
160
|
+
/** A ProseMirror editor with CommonMark persistence, formatting, Unicode emoji and attachment uploads. */
|
|
161
|
+
export const RichTextEditor = forwardRef(function RichTextEditor(props, forwardedRef) {
|
|
162
|
+
const uid = useUid("vk-rich");
|
|
163
|
+
const id = props.id || uid;
|
|
164
|
+
const chooser = useRef(null);
|
|
165
|
+
const { surfaceRef, editorView, editorState, initialDoc, markdown, uploads } = useRichTextEditor(props);
|
|
166
|
+
const locked = !!(props.disabled || props.readOnly);
|
|
167
|
+
const connectSurface = useCallback((node) => {
|
|
168
|
+
surfaceRef.current = node;
|
|
169
|
+
if (typeof forwardedRef === "function")
|
|
170
|
+
forwardedRef(node);
|
|
171
|
+
else if (forwardedRef)
|
|
172
|
+
forwardedRef.current = node;
|
|
173
|
+
}, [forwardedRef, surfaceRef]);
|
|
174
|
+
return _jsxs("div", { className: cx("vk-rich", props.className), "data-disabled": props.disabled || undefined, "data-readonly": props.readOnly || undefined, "data-invalid": props["aria-invalid"] || undefined, children: [_jsx(RichTextTools, { props: props, position: "before", view: editorView, state: editorState, chooser: chooser }), _jsx(RichTextSurface, { ref: connectSurface, props: props, id: id, document: editorState?.doc ?? initialDoc }), _jsx(RichTextTools, { props: props, position: "after", view: editorView, state: editorState, chooser: chooser }), _jsx(RichTextFormFields, { props: props, markdown: markdown, editorView: editorView, chooser: chooser, uploads: uploads }), _jsx(RichTextUploads, { uploads: uploads, locked: locked })] });
|
|
175
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { EditorState } from "prosemirror-state";
|
|
2
|
+
import type { EditorView } from "prosemirror-view";
|
|
3
|
+
import type { RichTextEmoji } from "./RichTextEditor.js";
|
|
4
|
+
interface ToolbarProps {
|
|
5
|
+
view: EditorView | null;
|
|
6
|
+
state: EditorState | null;
|
|
7
|
+
disabled: boolean;
|
|
8
|
+
emojiOptions?: readonly RichTextEmoji[];
|
|
9
|
+
onAttach?: () => void;
|
|
10
|
+
}
|
|
11
|
+
/** Radix owns toolbar navigation and the minimal formatting disclosure. */
|
|
12
|
+
export declare function RichTextToolbar({ variant, ...props }: ToolbarProps & {
|
|
13
|
+
variant?: "full" | "minimal";
|
|
14
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { Collapsible, Popover, Toolbar } from "radix-ui";
|
|
5
|
+
import { lift, setBlockType, toggleMark, wrapIn } from "prosemirror-commands";
|
|
6
|
+
import { undo, redo } from "prosemirror-history";
|
|
7
|
+
import { schema } from "prosemirror-markdown";
|
|
8
|
+
import { liftListItem, wrapInList } from "prosemirror-schema-list";
|
|
9
|
+
import { Input } from "../Input/Input.js";
|
|
10
|
+
import { Button } from "../Button/Button.js";
|
|
11
|
+
import { useLocale } from "../../localization.js";
|
|
12
|
+
import { isRichTextUrl } from "../../lib/rich-text.js";
|
|
13
|
+
function activeNode(state, name, level) {
|
|
14
|
+
if (!state)
|
|
15
|
+
return false;
|
|
16
|
+
for (let depth = state.selection.$from.depth; depth > 0; depth--) {
|
|
17
|
+
const node = state.selection.$from.node(depth);
|
|
18
|
+
if (node.type.name === name && (level === undefined || node.attrs.level === level))
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
function activeMark(state, name) {
|
|
24
|
+
if (!state)
|
|
25
|
+
return false;
|
|
26
|
+
const mark = schema.marks[name];
|
|
27
|
+
const { from, to, empty, $from } = state.selection;
|
|
28
|
+
return empty ? !!mark.isInSet(state.storedMarks || $from.marks()) : state.doc.rangeHasMark(from, to, mark);
|
|
29
|
+
}
|
|
30
|
+
function CommandButton({ label, children, command, view, state, disabled, pressed }) {
|
|
31
|
+
return _jsx(Toolbar.Button, { type: "button", className: "vk-rich-tool", "aria-label": label, title: label, "aria-pressed": pressed, disabled: disabled || !view || !state || !command(state), onMouseDown: (event) => { event.preventDefault(); }, onClick: () => {
|
|
32
|
+
if (!view || !view.editable || disabled)
|
|
33
|
+
return;
|
|
34
|
+
command(view.state, view.dispatch, view);
|
|
35
|
+
view.focus();
|
|
36
|
+
}, children: _jsx("span", { "aria-hidden": "true", children: children }) });
|
|
37
|
+
}
|
|
38
|
+
function LinkTool({ view, disabled, state }) {
|
|
39
|
+
const { messages } = useLocale();
|
|
40
|
+
const copy = messages.richText;
|
|
41
|
+
const [open, setOpen] = useState(false);
|
|
42
|
+
const [url, setUrl] = useState("");
|
|
43
|
+
const [text, setText] = useState("");
|
|
44
|
+
const [invalid, setInvalid] = useState(false);
|
|
45
|
+
const openPanel = (next) => {
|
|
46
|
+
if (next && view) {
|
|
47
|
+
const { from, to, $from } = view.state.selection;
|
|
48
|
+
const link = schema.marks.link.isInSet(view.state.storedMarks || $from.marks());
|
|
49
|
+
setUrl(typeof link?.attrs.href === "string" ? link.attrs.href : "");
|
|
50
|
+
setText(view.state.doc.textBetween(from, to, " "));
|
|
51
|
+
setInvalid(false);
|
|
52
|
+
}
|
|
53
|
+
setOpen(next);
|
|
54
|
+
};
|
|
55
|
+
const apply = () => {
|
|
56
|
+
if (!view || !view.editable || disabled)
|
|
57
|
+
return;
|
|
58
|
+
if (!isRichTextUrl(url)) {
|
|
59
|
+
setInvalid(true);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const { from, to } = view.state.selection;
|
|
63
|
+
const label = text || url;
|
|
64
|
+
const transaction = view.state.tr.insertText(label, from, to).addMark(from, from + label.length, schema.marks.link.create({ href: url }));
|
|
65
|
+
view.dispatch(transaction);
|
|
66
|
+
setOpen(false);
|
|
67
|
+
view.focus();
|
|
68
|
+
};
|
|
69
|
+
const remove = () => {
|
|
70
|
+
if (!view || !view.editable || disabled)
|
|
71
|
+
return;
|
|
72
|
+
const { from, to, empty, $from } = view.state.selection;
|
|
73
|
+
let start = from, end = to;
|
|
74
|
+
if (empty) {
|
|
75
|
+
const link = schema.marks.link.isInSet($from.marks());
|
|
76
|
+
if (link)
|
|
77
|
+
$from.parent.forEach((node, offset) => {
|
|
78
|
+
if (link.isInSet(node.marks)) {
|
|
79
|
+
const nodeStart = $from.start() + offset;
|
|
80
|
+
if (nodeStart <= from && nodeStart + node.nodeSize >= from) {
|
|
81
|
+
start = nodeStart;
|
|
82
|
+
end = nodeStart + node.nodeSize;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
view.dispatch(view.state.tr.removeMark(start, end, schema.marks.link).removeStoredMark(schema.marks.link));
|
|
88
|
+
setOpen(false);
|
|
89
|
+
view.focus();
|
|
90
|
+
};
|
|
91
|
+
return _jsxs(Popover.Root, { open: open && !disabled, onOpenChange: openPanel, children: [_jsx(Toolbar.Button, { asChild: true, disabled: disabled || !view, children: _jsx(Popover.Trigger, { type: "button", className: "vk-rich-tool", "aria-label": copy.addLink, title: copy.addLink, "aria-pressed": activeMark(state, "link"), onMouseDown: (event) => { event.preventDefault(); }, children: _jsx("span", { "aria-hidden": "true", children: "\u2197" }) }) }), _jsx(Popover.Portal, { children: _jsxs(Popover.Content, { className: "vk-popover vk-rich-panel", sideOffset: 4, collisionPadding: 8, "aria-label": copy.addLink, onCloseAutoFocus: (event) => { event.preventDefault(); view?.focus(); }, children: [_jsxs("label", { className: "vk-rich-panel-field", children: [_jsx("span", { children: copy.linkUrl }), _jsx(Input, { type: "url", value: url, onChange: (event) => { setUrl(event.target.value); setInvalid(false); }, "aria-invalid": invalid, onKeyDown: (event) => { if (event.key === "Enter") {
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
apply();
|
|
94
|
+
} } })] }), _jsxs("label", { className: "vk-rich-panel-field", children: [_jsx("span", { children: copy.linkText }), _jsx(Input, { value: text, onChange: (event) => { setText(event.target.value); }, onKeyDown: (event) => { if (event.key === "Enter") {
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
apply();
|
|
97
|
+
} } })] }), invalid ? _jsx("span", { className: "vk-field-error", role: "alert", children: copy.invalidUrl }) : null, _jsxs("div", { className: "vk-rich-panel-actions", children: [_jsx(Button, { type: "button", size: "sm", onClick: apply, children: copy.applyLink }), activeMark(state, "link") ? _jsx(Button, { type: "button", size: "sm", variant: "ghost", onClick: remove, children: copy.removeLink }) : null, _jsx(Popover.Close, { asChild: true, children: _jsx(Button, { type: "button", size: "sm", variant: "ghost", children: copy.cancel }) })] })] }) })] });
|
|
98
|
+
}
|
|
99
|
+
const emojiValues = ["😀", "🙂", "❤️", "👍", "👎", "🎉", "✅", "⚠️", "💡", "🚀", "👀", "🙏"];
|
|
100
|
+
function EmojiTool({ view, disabled, emojiOptions }) {
|
|
101
|
+
const { locale, messages } = useLocale();
|
|
102
|
+
const copy = messages.richText;
|
|
103
|
+
const [open, setOpen] = useState(false);
|
|
104
|
+
const [query, setQuery] = useState("");
|
|
105
|
+
const choices = emojiOptions || emojiValues.map((value) => ({ value, label: copy.emojiName(value) }));
|
|
106
|
+
const filtered = choices.filter((option) => option.value.includes(query) || option.label.toLocaleLowerCase(locale).includes(query.toLocaleLowerCase(locale)));
|
|
107
|
+
return _jsxs(Popover.Root, { open: open && !disabled, onOpenChange: (next) => { setQuery(""); setOpen(next); }, children: [_jsx(Toolbar.Button, { asChild: true, disabled: disabled || !view, children: _jsx(Popover.Trigger, { type: "button", className: "vk-rich-tool", "aria-label": copy.emoji, title: copy.emoji, onMouseDown: (event) => { event.preventDefault(); }, children: _jsx("span", { "aria-hidden": "true", children: "\u263A" }) }) }), _jsx(Popover.Portal, { children: _jsxs(Popover.Content, { className: "vk-popover vk-rich-panel", sideOffset: 4, collisionPadding: 8, "aria-label": copy.emoji, onCloseAutoFocus: (event) => { event.preventDefault(); view?.focus(); }, children: [_jsx(Input, { "aria-label": copy.emojiSearch, placeholder: copy.emojiSearch, value: query, onChange: (event) => { setQuery(event.target.value); } }), _jsx("div", { className: "vk-rich-emoji", role: "group", "aria-label": copy.emoji, children: filtered.map((option) => _jsx("button", { type: "button", className: "vk-rich-tool", "aria-label": option.label, title: option.label, onClick: () => {
|
|
108
|
+
if (!view || !view.editable || disabled)
|
|
109
|
+
return;
|
|
110
|
+
view.dispatch(view.state.tr.insertText(option.value).scrollIntoView());
|
|
111
|
+
setOpen(false);
|
|
112
|
+
view.focus();
|
|
113
|
+
}, children: _jsx("span", { "aria-hidden": "true", children: option.value }) }, option.value + option.label)) }), !filtered.length ? _jsx("p", { role: "status", children: copy.noEmoji }) : null] }) })] });
|
|
114
|
+
}
|
|
115
|
+
function AttachTool({ view, disabled, onAttach }) {
|
|
116
|
+
const { messages } = useLocale();
|
|
117
|
+
const label = messages.richText.attachFiles;
|
|
118
|
+
if (!onAttach)
|
|
119
|
+
return null;
|
|
120
|
+
return _jsx(Toolbar.Button, { type: "button", className: "vk-rich-tool", disabled: disabled || !view, "aria-label": label, title: label, onMouseDown: (event) => { event.preventDefault(); }, onClick: onAttach, children: _jsx("span", { "aria-hidden": "true", children: "\uFF0B" }) });
|
|
121
|
+
}
|
|
122
|
+
function MinimalToolbar({ view, state, disabled, emojiOptions, onAttach }) {
|
|
123
|
+
const { messages } = useLocale();
|
|
124
|
+
const copy = messages.richText;
|
|
125
|
+
const shared = { view, state, disabled };
|
|
126
|
+
const bullet = activeNode(state, "bullet_list");
|
|
127
|
+
const formattingLabel = "Aa: " + copy.formatting;
|
|
128
|
+
return _jsxs(Collapsible.Root, { className: "vk-rich-minimal", disabled: disabled, children: [_jsxs(Toolbar.Root, { className: "vk-rich-toolbar vk-rich-minimal-actions", "aria-label": copy.toolbar, children: [_jsx(Collapsible.Trigger, { asChild: true, children: _jsx(Toolbar.Button, { type: "button", className: "vk-rich-tool", "aria-label": formattingLabel, title: formattingLabel, disabled: disabled || !view, onMouseDown: (event) => { event.preventDefault(); }, children: _jsx("span", { "aria-hidden": "true", children: "Aa" }) }) }), _jsx(EmojiTool, { view: view, disabled: disabled, emojiOptions: emojiOptions }), _jsx(AttachTool, { view: view, disabled: disabled, onAttach: onAttach })] }), _jsx(Collapsible.Content, { children: _jsxs(Toolbar.Root, { className: "vk-rich-toolbar vk-rich-minimal-formatting", "aria-label": copy.formatting, children: [_jsx(CommandButton, { ...shared, label: copy.bold, command: toggleMark(schema.marks.strong), pressed: activeMark(state, "strong"), children: _jsx("strong", { children: "B" }) }), _jsx(CommandButton, { ...shared, label: copy.italic, command: toggleMark(schema.marks.em), pressed: activeMark(state, "em"), children: _jsx("em", { children: "I" }) }), _jsx(LinkTool, { ...shared }), _jsx(CommandButton, { ...shared, label: copy.bulletList, command: bullet ? liftListItem(schema.nodes.list_item) : wrapInList(schema.nodes.bullet_list), pressed: bullet, children: "\u2022\u2261" })] }) })] });
|
|
129
|
+
}
|
|
130
|
+
function FullToolbar({ view, state, disabled, emojiOptions, onAttach }) {
|
|
131
|
+
const { messages } = useLocale();
|
|
132
|
+
const copy = messages.richText;
|
|
133
|
+
const shared = { view, state, disabled };
|
|
134
|
+
const bullet = activeNode(state, "bullet_list");
|
|
135
|
+
const ordered = activeNode(state, "ordered_list");
|
|
136
|
+
const quote = activeNode(state, "blockquote");
|
|
137
|
+
return _jsxs(Toolbar.Root, { className: "vk-rich-toolbar", "aria-label": copy.toolbar, children: [_jsx(CommandButton, { ...shared, label: copy.paragraph, command: setBlockType(schema.nodes.paragraph), pressed: activeNode(state, "paragraph"), children: "\u00B6" }), [1, 2, 3].map((level) => _jsxs(CommandButton, { ...shared, label: "H" + String(level) + ": " + copy.heading(level), command: setBlockType(schema.nodes.heading, { level }), pressed: activeNode(state, "heading", level), children: ["H", level] }, level)), _jsx(Toolbar.Separator, { className: "vk-rich-separator" }), _jsx(CommandButton, { ...shared, label: copy.bold, command: toggleMark(schema.marks.strong), pressed: activeMark(state, "strong"), children: _jsx("strong", { children: "B" }) }), _jsx(CommandButton, { ...shared, label: copy.italic, command: toggleMark(schema.marks.em), pressed: activeMark(state, "em"), children: _jsx("em", { children: "I" }) }), _jsx(CommandButton, { ...shared, label: copy.inlineCode, command: toggleMark(schema.marks.code), pressed: activeMark(state, "code"), children: "\u3008/\u3009" }), _jsx(CommandButton, { ...shared, label: copy.codeBlock, command: setBlockType(schema.nodes.code_block), pressed: activeNode(state, "code_block"), children: "{ }" }), _jsx(Toolbar.Separator, { className: "vk-rich-separator" }), _jsx(CommandButton, { ...shared, label: copy.bulletList, command: bullet ? liftListItem(schema.nodes.list_item) : wrapInList(schema.nodes.bullet_list), pressed: bullet, children: "\u2022\u2261" }), _jsx(CommandButton, { ...shared, label: copy.orderedList, command: ordered ? liftListItem(schema.nodes.list_item) : wrapInList(schema.nodes.ordered_list), pressed: ordered, children: _jsx("svg", { "aria-hidden": "true", width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("path", { d: "M10 6h11M10 12h11M10 18h11M4 2v6M3 3l1-1M3 8h2M3 12h2l-2 4h2M3 20h2M3 19h2v3H3" }) }) }), _jsx(CommandButton, { ...shared, label: copy.quote, command: quote ? lift : wrapIn(schema.nodes.blockquote), pressed: quote, children: "\u275E" }), _jsx(LinkTool, { ...shared }), _jsx(EmojiTool, { view: view, disabled: disabled, emojiOptions: emojiOptions }), _jsx(AttachTool, { view: view, disabled: disabled, onAttach: onAttach }), _jsx(Toolbar.Separator, { className: "vk-rich-separator" }), _jsx(CommandButton, { ...shared, label: copy.undo, command: undo, children: "\u21B6" }), _jsx(CommandButton, { ...shared, label: copy.redo, command: redo, children: "\u21B7" })] });
|
|
138
|
+
}
|
|
139
|
+
/** Radix owns toolbar navigation and the minimal formatting disclosure. */
|
|
140
|
+
export function RichTextToolbar({ variant, ...props }) {
|
|
141
|
+
return variant === "minimal" ? _jsx(MinimalToolbar, { ...props }) : _jsx(FullToolbar, { ...props });
|
|
142
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { RichTextAttachment, RichTextUpload } from "./RichTextEditor.js";
|
|
2
|
+
type UploadIssue = {
|
|
3
|
+
kind: "too-large";
|
|
4
|
+
limit: number;
|
|
5
|
+
} | {
|
|
6
|
+
kind: "failed";
|
|
7
|
+
};
|
|
8
|
+
interface UploadEntry {
|
|
9
|
+
id: number;
|
|
10
|
+
file: File;
|
|
11
|
+
state: "pending" | "error" | "complete";
|
|
12
|
+
issue?: UploadIssue;
|
|
13
|
+
attachment?: RichTextAttachment;
|
|
14
|
+
}
|
|
15
|
+
interface UploadOptions {
|
|
16
|
+
upload?: RichTextUpload;
|
|
17
|
+
locked: boolean;
|
|
18
|
+
maxFileSize?: number;
|
|
19
|
+
insert: (attachment: RichTextAttachment) => void;
|
|
20
|
+
remove: (attachment: RichTextAttachment) => void;
|
|
21
|
+
onError?: (error: Error, file: File) => void;
|
|
22
|
+
onPendingChange?: (pending: boolean) => void;
|
|
23
|
+
}
|
|
24
|
+
/** Even a typed app callback is an external runtime boundary. */
|
|
25
|
+
export declare function validateAttachment(value: unknown): RichTextAttachment;
|
|
26
|
+
/** Tracks upload attempts independently of editor transactions, including canceled/stale promises. */
|
|
27
|
+
export declare function useRichTextUploads(options: UploadOptions): {
|
|
28
|
+
entries: UploadEntry[];
|
|
29
|
+
announcement: string;
|
|
30
|
+
add: (files: File[]) => void;
|
|
31
|
+
retry: (entry: UploadEntry) => void;
|
|
32
|
+
cancel: (id: number) => void;
|
|
33
|
+
remove: (entry: UploadEntry) => void;
|
|
34
|
+
reset: () => void;
|
|
35
|
+
pending: boolean;
|
|
36
|
+
};
|
|
37
|
+
export declare function RichTextUploads({ uploads, locked }: {
|
|
38
|
+
uploads: ReturnType<typeof useRichTextUploads>;
|
|
39
|
+
locked: boolean;
|
|
40
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
41
|
+
export {};
|