@open-artifact/editor-kit 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/announce.d.ts +5 -2
- package/src/announce.js +13 -3
- package/src/chrome.d.ts +13 -4
- package/src/chrome.js +37 -7
- package/src/file-drop.d.ts +33 -0
- package/src/file-drop.js +36 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/editor-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "What every artifact editor shares: collaboration (comments, presence, history), roles and host session, Shell chrome reports, find, recovery stashes, line merge, announcements, chrome primitives and design-system loading.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@base-ui/react": "^1.8.0",
|
|
25
|
-
"@open-artifact/design-system-core": "0.
|
|
26
|
-
"@open-artifact/runtime": "0.
|
|
27
|
-
"@open-artifact/ui": "0.
|
|
25
|
+
"@open-artifact/design-system-core": "0.12.0",
|
|
26
|
+
"@open-artifact/runtime": "0.12.0",
|
|
27
|
+
"@open-artifact/ui": "0.12.0",
|
|
28
28
|
"clsx": "^2.1.1",
|
|
29
29
|
"lucide-react": "^1.65.0",
|
|
30
30
|
"zustand": "^5.0.10",
|
package/src/announce.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* component renders `useAnnouncer` in a `role="status"` region (see
|
|
5
5
|
* `Announcer.tsx`). Repeating the same message re-announces it.
|
|
6
6
|
*/
|
|
7
|
-
/** Longest message read out (longer ones are cut). */
|
|
7
|
+
/** Longest message read out (longer ones are cut, at a word where they can be). */
|
|
8
8
|
export declare const ANNOUNCE_LIMIT = 200;
|
|
9
9
|
export interface AnnouncerState {
|
|
10
10
|
message: string;
|
|
@@ -12,7 +12,10 @@ export interface AnnouncerState {
|
|
|
12
12
|
serial: number;
|
|
13
13
|
}
|
|
14
14
|
export declare const useAnnouncer: import("zustand").UseBoundStore<import("zustand").StoreApi<AnnouncerState>>;
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* The text read out for a message, whitespace collapsed; empty: nothing. Past {@link ANNOUNCE_LIMIT} it is cut at the
|
|
17
|
+
* last word break within that many characters, or mid-word when that break comes early, and ends with an ellipsis.
|
|
18
|
+
*/
|
|
16
19
|
export declare function announcementText(message: string): string;
|
|
17
20
|
/** Reads a message out to screen readers. */
|
|
18
21
|
export declare function announce(message: string): void;
|
package/src/announce.js
CHANGED
|
@@ -5,13 +5,23 @@
|
|
|
5
5
|
* `Announcer.tsx`). Repeating the same message re-announces it.
|
|
6
6
|
*/
|
|
7
7
|
import { create } from 'zustand';
|
|
8
|
-
/** Longest message read out (longer ones are cut). */
|
|
8
|
+
/** Longest message read out (longer ones are cut, at a word where they can be). */
|
|
9
9
|
export const ANNOUNCE_LIMIT = 200;
|
|
10
|
+
/** A cut at a word keeps at least this many characters; one that would keep fewer cuts at the limit instead. */
|
|
11
|
+
const SHORTEST_WORD_CUT = 119;
|
|
10
12
|
export const useAnnouncer = create()(() => ({ message: '', serial: 0 }));
|
|
11
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* The text read out for a message, whitespace collapsed; empty: nothing. Past {@link ANNOUNCE_LIMIT} it is cut at the
|
|
15
|
+
* last word break within that many characters, or mid-word when that break comes early, and ends with an ellipsis.
|
|
16
|
+
*/
|
|
12
17
|
export function announcementText(message) {
|
|
13
18
|
const text = message.replace(/\s+/g, ' ').trim();
|
|
14
|
-
|
|
19
|
+
if (text.length <= ANNOUNCE_LIMIT)
|
|
20
|
+
return text;
|
|
21
|
+
const wordBreak = text.slice(0, ANNOUNCE_LIMIT).search(/\s+\S*$/);
|
|
22
|
+
// A cut mid-word leaves room for the ellipsis and no half of a surrogate pair.
|
|
23
|
+
const kept = wordBreak >= SHORTEST_WORD_CUT ? text.slice(0, wordBreak) : text.slice(0, ANNOUNCE_LIMIT - 1).replace(/[\uD800-\uDBFF]$/, '');
|
|
24
|
+
return `${kept}…`;
|
|
15
25
|
}
|
|
16
26
|
/** Reads a message out to screen readers. */
|
|
17
27
|
export function announce(message) {
|
package/src/chrome.d.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* `shadow-chrome`, `bg-tip`…); the focus ring is `outline-chrome-focus`, so
|
|
16
16
|
* each editor picks its own focus color.
|
|
17
17
|
*/
|
|
18
|
+
import { Popover as BasePopover } from '@base-ui/react/popover';
|
|
18
19
|
import { type ButtonHTMLAttributes, type CSSProperties, type HTMLAttributes, type ReactElement, type ReactNode } from 'react';
|
|
19
20
|
import type { ChromeDraw, ShellConnection } from '@open-artifact/runtime';
|
|
20
21
|
/** A floating chrome card. */
|
|
@@ -110,7 +111,7 @@ export interface ChromeMenuItemProps {
|
|
|
110
111
|
icon?: ReactNode;
|
|
111
112
|
title?: string;
|
|
112
113
|
}
|
|
113
|
-
/** One menu row. */
|
|
114
|
+
/** One menu row. A row that closes the menu keeps the rest of a double click from reaching what the menu uncovers. */
|
|
114
115
|
export declare function ChromeMenuItem({ children, onSelect, shortcut, checked, disabled, danger, icon, title, description, closeOnClick }: ChromeMenuItemProps): ReactNode;
|
|
115
116
|
/** A submenu row and its popup. */
|
|
116
117
|
export declare function ChromeSubmenu({ label, icon, children, disabled, className, mark, popupBase, }: {
|
|
@@ -138,7 +139,7 @@ export interface ChromePopoverProps {
|
|
|
138
139
|
side?: ChromeSide;
|
|
139
140
|
align?: 'start' | 'center' | 'end';
|
|
140
141
|
open?: boolean;
|
|
141
|
-
onOpenChange?: (open: boolean) => void;
|
|
142
|
+
onOpenChange?: (open: boolean, details: ChromePopoverChange) => void;
|
|
142
143
|
className?: string;
|
|
143
144
|
/** Accessible name of the popup. */
|
|
144
145
|
label?: string;
|
|
@@ -153,9 +154,17 @@ export interface ChromePopoverProps {
|
|
|
153
154
|
/** Popup classes in place of {@link POPUP}. */
|
|
154
155
|
popupBase?: string;
|
|
155
156
|
style?: CSSProperties;
|
|
157
|
+
/**
|
|
158
|
+
* Once something has been typed into a field of the popup (other than a
|
|
159
|
+
* search field), a press outside no longer closes it: Escape, or the
|
|
160
|
+
* popup's own buttons, do.
|
|
161
|
+
*/
|
|
162
|
+
holdWhileTyped?: boolean;
|
|
156
163
|
}
|
|
157
|
-
/**
|
|
158
|
-
export
|
|
164
|
+
/** Why a {@link ChromePopover} opens or closes; `cancel()` keeps it as it is. */
|
|
165
|
+
export type ChromePopoverChange = BasePopover.Root.ChangeEventDetails;
|
|
166
|
+
/** A floating chrome panel anchored to a trigger (or to `anchor`). A press outside that closes it does not double-click what is under it. */
|
|
167
|
+
export declare function ChromePopover({ trigger, children, side, align, open, onOpenChange, className, label, anchor, gap, initialFocus, mark, popupBase, style, holdWhileTyped, }: ChromePopoverProps): ReactNode;
|
|
159
168
|
/** One color choice of a {@link SwatchRow}. */
|
|
160
169
|
export interface Swatch {
|
|
161
170
|
/** The value stored (a palette name or `#rrggbb`). */
|
package/src/chrome.js
CHANGED
|
@@ -20,8 +20,8 @@ import { Menu as BaseMenu } from '@base-ui/react/menu';
|
|
|
20
20
|
import { Popover as BasePopover } from '@base-ui/react/popover';
|
|
21
21
|
import { Tooltip as BaseTooltip } from '@base-ui/react/tooltip';
|
|
22
22
|
import { Check, ChevronRight } from 'lucide-react';
|
|
23
|
-
import { forwardRef, useEffect, useState } from 'react';
|
|
24
|
-
import { cn } from '@open-artifact/ui';
|
|
23
|
+
import { forwardRef, useEffect, useRef, useState } from 'react';
|
|
24
|
+
import { cn, swallowFollowUpClicks } from '@open-artifact/ui';
|
|
25
25
|
import { menuFit } from "./menu-fit.js";
|
|
26
26
|
/** A floating chrome card. */
|
|
27
27
|
export const Card = forwardRef(function Card({ className, ...rest }, ref) {
|
|
@@ -66,9 +66,13 @@ export function ChromeMenu({ trigger, children, side = 'bottom', align = 'start'
|
|
|
66
66
|
const triggerElement = _jsx(BaseMenu.Trigger, { render: trigger });
|
|
67
67
|
return (_jsxs(BaseMenu.Root, { open: open, onOpenChange: onOpenChange ? (next) => onOpenChange(next) : undefined, children: [tip ? (_jsx(Tip, { label: tip.label, shortcut: tip.shortcut, side: tip.side ?? (side === 'top' ? 'top' : 'bottom'), children: triggerElement })) : (triggerElement), _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { side: side, align: align, sideOffset: sideOffset, className: "z-[1000]", children: _jsx(BaseMenu.Popup, { ...marked(mark), className: cn(popupBase, className), style: menuFit(className), children: children }) }) })] }));
|
|
68
68
|
}
|
|
69
|
-
/** One menu row. */
|
|
69
|
+
/** One menu row. A row that closes the menu keeps the rest of a double click from reaching what the menu uncovers. */
|
|
70
70
|
export function ChromeMenuItem({ children, onSelect, shortcut, checked, disabled, danger, icon, title, description, closeOnClick }) {
|
|
71
|
-
return (_jsxs(BaseMenu.Item, { className: cn(ITEM, description !== undefined && 'h-auto min-h-7 items-start py-1.5', danger && 'text-danger data-[highlighted]:bg-danger data-[highlighted]:text-white'), disabled: disabled, onClick:
|
|
71
|
+
return (_jsxs(BaseMenu.Item, { className: cn(ITEM, description !== undefined && 'h-auto min-h-7 items-start py-1.5', danger && 'text-danger data-[highlighted]:bg-danger data-[highlighted]:text-white'), disabled: disabled, onClick: (event) => {
|
|
72
|
+
if (closeOnClick !== false)
|
|
73
|
+
swallowFollowUpClicks(event.nativeEvent);
|
|
74
|
+
onSelect?.();
|
|
75
|
+
}, closeOnClick: closeOnClick, title: title, children: [checked !== undefined ? (_jsx("span", { className: "flex size-4 shrink-0 items-center justify-center", children: checked ? _jsx(Check, { className: "size-4 text-chrome-accent", strokeWidth: 2.25 }) : null })) : null, icon ? _jsx("span", { "data-chrome-icon": "", className: "flex size-4 shrink-0 items-center justify-center text-chrome-muted", children: icon }) : null, description !== undefined ? (_jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [_jsx("span", { className: "truncate", children: children }), _jsx("span", { className: "truncate text-[12px] leading-4 text-chrome-muted", children: description })] })) : (_jsx("span", { className: "min-w-0 flex-1 truncate", children: children })), shortcut ? _jsx("span", { className: "ml-6 max-w-40 shrink-0 truncate text-[13px] text-chrome-muted", children: shortcut }) : null] }));
|
|
72
76
|
}
|
|
73
77
|
/** A submenu row and its popup. */
|
|
74
78
|
export function ChromeSubmenu({ label, icon, children, disabled, className, mark, popupBase = POPUP, }) {
|
|
@@ -82,9 +86,35 @@ export function ChromeMenuSeparator() {
|
|
|
82
86
|
export function ChromeMenuLabel({ children }) {
|
|
83
87
|
return _jsx("div", { className: "px-2.5 py-1 text-[13px] font-medium text-chrome-muted", children: children });
|
|
84
88
|
}
|
|
85
|
-
/**
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
/** True for a field whose typing is a search (a filter, not an edit worth keeping). */
|
|
90
|
+
function isSearchField(target) {
|
|
91
|
+
if (!(target instanceof HTMLElement))
|
|
92
|
+
return true;
|
|
93
|
+
if (!target.matches('input, textarea, [contenteditable="true"]'))
|
|
94
|
+
return true;
|
|
95
|
+
return target.matches('[type="search"], [role="searchbox"], [role="combobox"]');
|
|
96
|
+
}
|
|
97
|
+
/** A floating chrome panel anchored to a trigger (or to `anchor`). A press outside that closes it does not double-click what is under it. */
|
|
98
|
+
export function ChromePopover({ trigger, children, side = 'top', align = 'center', open, onOpenChange, className, label, anchor, gap = 8, initialFocus = true, mark, popupBase = POPUP, style, holdWhileTyped = false, }) {
|
|
99
|
+
const typed = useRef(false);
|
|
100
|
+
const change = (next, details) => {
|
|
101
|
+
if (!next && details.reason === 'outside-press') {
|
|
102
|
+
if (holdWhileTyped && typed.current) {
|
|
103
|
+
details.cancel();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (details.event instanceof MouseEvent)
|
|
107
|
+
swallowFollowUpClicks(details.event);
|
|
108
|
+
}
|
|
109
|
+
onOpenChange?.(next, details);
|
|
110
|
+
};
|
|
111
|
+
return (_jsxs(BasePopover.Root, { open: open, onOpenChange: change, onOpenChangeComplete: (done) => {
|
|
112
|
+
if (!done)
|
|
113
|
+
typed.current = false;
|
|
114
|
+
}, children: [_jsx(BasePopover.Trigger, { render: trigger }), _jsx(BasePopover.Portal, { children: _jsx(BasePopover.Positioner, { side: side, align: align, sideOffset: gap, anchor: anchor ?? undefined, className: "z-[1000]", children: _jsx(BasePopover.Popup, { ...marked(mark), "aria-label": label, initialFocus: initialFocus ? undefined : false, className: cn(popupBase, className), style: style, onInput: (event) => {
|
|
115
|
+
if (!isSearchField(event.target))
|
|
116
|
+
typed.current = true;
|
|
117
|
+
}, children: children }) }) })] }));
|
|
88
118
|
}
|
|
89
119
|
/** A row of round color swatches (radio semantics); the current one is ringed in the chrome accent. */
|
|
90
120
|
export function SwatchRow({ label, swatches, current, onPick, size = 20, className, }) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A file dropped where nothing takes it (a read-only canvas, panels, toolbars)
|
|
3
|
+
* would make the browser open the file in place of the editor. This guard
|
|
4
|
+
* swallows such drops; a handler that takes the drop has already prevented its
|
|
5
|
+
* default, and an enabled file input keeps its own drop.
|
|
6
|
+
*/
|
|
7
|
+
/** Minimal shape of a drag event target, so the guard runs without a DOM. */
|
|
8
|
+
export interface FileDropTarget {
|
|
9
|
+
readonly tagName?: string;
|
|
10
|
+
readonly type?: string;
|
|
11
|
+
readonly disabled?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** The parts of a drag event the guard reads. */
|
|
14
|
+
export interface FileDragEvent {
|
|
15
|
+
readonly type: string;
|
|
16
|
+
readonly defaultPrevented: boolean;
|
|
17
|
+
readonly target?: EventTarget | FileDropTarget | null;
|
|
18
|
+
readonly dataTransfer: {
|
|
19
|
+
types: ArrayLike<string> | Iterable<string>;
|
|
20
|
+
dropEffect: string;
|
|
21
|
+
} | null;
|
|
22
|
+
composedPath?(): readonly unknown[];
|
|
23
|
+
preventDefault(): void;
|
|
24
|
+
}
|
|
25
|
+
/** The parts of a window the guard listens on. */
|
|
26
|
+
export interface FileDropWindow {
|
|
27
|
+
addEventListener(type: 'dragover' | 'drop', listener: (event: FileDragEvent) => void): void;
|
|
28
|
+
removeEventListener(type: 'dragover' | 'drop', listener: (event: FileDragEvent) => void): void;
|
|
29
|
+
}
|
|
30
|
+
/** True when the drag carries files from outside the page. */
|
|
31
|
+
export declare function carriesFiles(event: FileDragEvent): boolean;
|
|
32
|
+
/** Installs the guard; returns a function that removes it. */
|
|
33
|
+
export declare function guardFileDrops(win: FileDropWindow): () => void;
|
package/src/file-drop.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A file dropped where nothing takes it (a read-only canvas, panels, toolbars)
|
|
3
|
+
* would make the browser open the file in place of the editor. This guard
|
|
4
|
+
* swallows such drops; a handler that takes the drop has already prevented its
|
|
5
|
+
* default, and an enabled file input keeps its own drop.
|
|
6
|
+
*/
|
|
7
|
+
/** True when the drag carries files from outside the page. */
|
|
8
|
+
export function carriesFiles(event) {
|
|
9
|
+
return Array.from(event.dataTransfer?.types ?? []).includes('Files');
|
|
10
|
+
}
|
|
11
|
+
function isEnabledFileInput(node) {
|
|
12
|
+
if (typeof node !== 'object' || node === null)
|
|
13
|
+
return false;
|
|
14
|
+
const target = node;
|
|
15
|
+
return typeof target.tagName === 'string' && target.tagName.toLowerCase() === 'input' && target.type === 'file' && target.disabled !== true;
|
|
16
|
+
}
|
|
17
|
+
function landsOnFileInput(event) {
|
|
18
|
+
const first = event.composedPath?.()[0] ?? event.target ?? null;
|
|
19
|
+
return isEnabledFileInput(first);
|
|
20
|
+
}
|
|
21
|
+
/** Installs the guard; returns a function that removes it. */
|
|
22
|
+
export function guardFileDrops(win) {
|
|
23
|
+
const guard = (event) => {
|
|
24
|
+
if (event.defaultPrevented || !carriesFiles(event) || landsOnFileInput(event))
|
|
25
|
+
return;
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
if (event.type === 'dragover' && event.dataTransfer)
|
|
28
|
+
event.dataTransfer.dropEffect = 'none';
|
|
29
|
+
};
|
|
30
|
+
win.addEventListener('dragover', guard);
|
|
31
|
+
win.addEventListener('drop', guard);
|
|
32
|
+
return () => {
|
|
33
|
+
win.removeEventListener('dragover', guard);
|
|
34
|
+
win.removeEventListener('drop', guard);
|
|
35
|
+
};
|
|
36
|
+
}
|
package/src/index.d.ts
CHANGED