@seanyeh77/ui 0.32.0 → 0.33.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/dist/bundle.js +17 -16
- package/dist/bundle.js.map +1 -1
- package/dist/index.d.ts +11 -3
- package/dist/index.js +74 -8
- package/dist/index.js.map +1 -1
- package/dist/styles.css +6 -0
- package/docs/CodeView.md +23 -0
- package/docs/TreeList.md +1 -2
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -581,16 +581,23 @@ interface CodeViewProps<L extends CodeLink = CodeLink> {
|
|
|
581
581
|
onCopy?: (text: string) => void;
|
|
582
582
|
/** Copying failed (the browser does not allow the clipboard): say so, for example in a Toast. */
|
|
583
583
|
onCopyError?: () => void;
|
|
584
|
+
/** The reader clicked a line, or moved to one with ↑ ↓ (also Home, End, PageUp, PageDown, [ ]): `how` says which.
|
|
585
|
+
* Dragging to select text is not a move and does not call it. Given this, ↑ ↓ move by line even without links. */
|
|
586
|
+
onLine?: (line: number, how: 'click' | 'key') => void;
|
|
587
|
+
/** Marks one line for a moment, apart from `target` (both can show), e.g. while the pointer rests on an item that
|
|
588
|
+
* points at it. Scrolled into view when needed; focus and selected text stay. null or undefined: none. */
|
|
589
|
+
peek?: number | null;
|
|
584
590
|
className?: string;
|
|
585
591
|
/** focusTarget() moves keyboard and screen-reader focus to the target line. */
|
|
586
592
|
ref?: React.Ref<CodeViewHandle>;
|
|
587
593
|
}
|
|
588
594
|
interface CodeViewHandle {
|
|
589
|
-
/** Focuses the first target line (scrolled into view); nothing happens without a target
|
|
595
|
+
/** Focuses the first target line (scrolled into view); nothing happens without a target, or while the reader has
|
|
596
|
+
* text selected in the code (the selection stays). */
|
|
590
597
|
focusTarget: () => void;
|
|
591
598
|
}
|
|
592
599
|
/** Source with line numbers and an optional target line: soft accent background, long lines wrap, light syntax colors. */
|
|
593
|
-
export declare function CodeView<L extends CodeLink = CodeLink>({ code, start, target, context, file, fileHref, onFile, title, language, languageLabel, highlight, tokenizer, refHref, onRef, links, onLink, preview, terms, termsMark, maxHeight, sections, copy, header, lineNumbers, onCopy, onCopyError, className, ref }: CodeViewProps<L>): React.JSX.Element;
|
|
600
|
+
export declare function CodeView<L extends CodeLink = CodeLink>({ code, start, target, context, file, fileHref, onFile, title, language, languageLabel, highlight, tokenizer, refHref, onRef, links, onLink, preview, terms, termsMark, maxHeight, sections, copy, header, lineNumbers, onCopy, onCopyError, onLine, peek, className, ref }: CodeViewProps<L>): React.JSX.Element;
|
|
594
601
|
interface PaletteItem {
|
|
595
602
|
id?: string | number;
|
|
596
603
|
label: string;
|
|
@@ -1808,7 +1815,8 @@ interface TreeListProps {
|
|
|
1808
1815
|
items: TreeNode[];
|
|
1809
1816
|
/** 'files' (default): folders and files with date and size. 'plain': any other tree, such as a call tree. */
|
|
1810
1817
|
kind?: 'files' | 'plain';
|
|
1811
|
-
/** kind="plain": the selected row.
|
|
1818
|
+
/** kind="plain": the selected row. Clicking it again (or Enter on it) still calls onSelect, but does not open or close
|
|
1819
|
+
* it; its caret does. */
|
|
1812
1820
|
selectedId?: string;
|
|
1813
1821
|
onSelect?: (node: TreeNode) => void;
|
|
1814
1822
|
/** kind="plain": IconButtons at the row's right end. */
|
package/dist/index.js
CHANGED
|
@@ -2671,6 +2671,33 @@ function matchLinks(lines, start, links) {
|
|
|
2671
2671
|
for (const list of out.values()) list.sort((a, b) => a.from - b.from);
|
|
2672
2672
|
return out;
|
|
2673
2673
|
}
|
|
2674
|
+
/** True when the reader has text selected inside el. */
|
|
2675
|
+
function selectedIn(el) {
|
|
2676
|
+
const sel = typeof window === "undefined" ? null : window.getSelection();
|
|
2677
|
+
if (!el || !sel || sel.isCollapsed || !sel.rangeCount) return false;
|
|
2678
|
+
const r = sel.getRangeAt(0);
|
|
2679
|
+
return el.contains(r.startContainer) || el.contains(r.endContainer);
|
|
2680
|
+
}
|
|
2681
|
+
/** The selected code as plain text, one line per line; null when the selection is not in pre. */
|
|
2682
|
+
function selectedCode(pre) {
|
|
2683
|
+
const sel = window.getSelection();
|
|
2684
|
+
if (!sel || sel.isCollapsed || !sel.rangeCount) return null;
|
|
2685
|
+
const r = sel.getRangeAt(0);
|
|
2686
|
+
if (!pre.contains(r.commonAncestorContainer)) return null;
|
|
2687
|
+
const frag = r.cloneContents();
|
|
2688
|
+
frag.querySelectorAll(".ui-cv-num, .ui-visually-hidden").forEach((x) => x.remove());
|
|
2689
|
+
const rows = [...frag.querySelectorAll("[role=\"listitem\"]")];
|
|
2690
|
+
return rows.length ? rows.map((x) => x.textContent ?? "").join("\n") : frag.textContent ?? "";
|
|
2691
|
+
}
|
|
2692
|
+
/** Scrolls el into view inside box (or the page) only when it is not already fully visible. */
|
|
2693
|
+
function inView(el, box) {
|
|
2694
|
+
const r = el.getBoundingClientRect();
|
|
2695
|
+
const b = box && box.scrollHeight > box.clientHeight ? box.getBoundingClientRect() : {
|
|
2696
|
+
top: 0,
|
|
2697
|
+
bottom: window.innerHeight
|
|
2698
|
+
};
|
|
2699
|
+
if (r.top < b.top || r.bottom > b.bottom) el.scrollIntoView({ block: "nearest" });
|
|
2700
|
+
}
|
|
2674
2701
|
/** Each line's words from `terms`: only in comments and strings, never where a name link or a path is. With 'first',
|
|
2675
2702
|
* each word once per file, at its first place. */
|
|
2676
2703
|
function matchTerms(toks, start, links, terms, mark) {
|
|
@@ -2724,7 +2751,7 @@ function KeysPanel(p) {
|
|
|
2724
2751
|
});
|
|
2725
2752
|
}
|
|
2726
2753
|
/** Source with line numbers and an optional target line: soft accent background, long lines wrap, light syntax colors. */
|
|
2727
|
-
function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile, title, language, languageLabel, highlight = true, tokenizer = tokenize, refHref, onRef, links, onLink, preview, terms, termsMark = "first", maxHeight, sections, copy, header = true, lineNumbers = true, onCopy, onCopyError, className, ref }) {
|
|
2754
|
+
function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile, title, language, languageLabel, highlight = true, tokenizer = tokenize, refHref, onRef, links, onLink, preview, terms, termsMark = "first", maxHeight, sections, copy, header = true, lineNumbers = true, onCopy, onCopyError, onLine, peek, className, ref }) {
|
|
2728
2755
|
const lines = React.useMemo(() => String(code || "").replace(/\n$/, "").split("\n"), [code]);
|
|
2729
2756
|
const lang = language || languageOf(file);
|
|
2730
2757
|
const toks = React.useMemo(() => highlight ? tokenizer(lines, lang) : lines.map((l) => [["", l]]), [
|
|
@@ -2826,12 +2853,20 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
|
|
|
2826
2853
|
const from = windowed ? Math.max(0, ta - start - context) : 0;
|
|
2827
2854
|
const to = windowed ? Math.min(lines.length, tb - start + context + 1) : lines.length;
|
|
2828
2855
|
const body = React.useRef(null);
|
|
2829
|
-
React.useImperativeHandle(ref, () => ({ focusTarget: () =>
|
|
2856
|
+
React.useImperativeHandle(ref, () => ({ focusTarget: () => {
|
|
2857
|
+
if (selectedIn(body.current)) return;
|
|
2858
|
+
body.current?.querySelector(".is-target")?.focus();
|
|
2859
|
+
} }));
|
|
2830
2860
|
React.useEffect(() => {
|
|
2831
2861
|
const b = body.current;
|
|
2832
2862
|
const el = b?.querySelector(".is-target");
|
|
2833
|
-
if (b && el && b.scrollHeight > b.clientHeight) b.scrollTop += el.getBoundingClientRect().top - b.getBoundingClientRect().top - b.clientHeight / 2 + el.offsetHeight / 2;
|
|
2863
|
+
if (b && el && b.scrollHeight > b.clientHeight && !selectedIn(b)) b.scrollTop += el.getBoundingClientRect().top - b.getBoundingClientRect().top - b.clientHeight / 2 + el.offsetHeight / 2;
|
|
2834
2864
|
}, [ta, code]);
|
|
2865
|
+
React.useEffect(() => {
|
|
2866
|
+
if (peek == null) return;
|
|
2867
|
+
const el = body.current?.querySelector(`[data-n="${peek}"]`);
|
|
2868
|
+
if (el) inView(el, body.current);
|
|
2869
|
+
}, [peek]);
|
|
2835
2870
|
const digits = String(start + lines.length - 1).length;
|
|
2836
2871
|
const prose = (cls, text, inLink) => !inLink && (refHref || onRef) && (cls === "is-comment" || cls === "is-string") ? withPaths(text, refHref, onRef, nav ? -1 : void 0) : breakable(text);
|
|
2837
2872
|
const piece = (cls, text, key, inLink, at = 0, words) => {
|
|
@@ -2918,7 +2953,7 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
|
|
|
2918
2953
|
const on = ta != null && tb != null && n >= ta && n <= tb;
|
|
2919
2954
|
rows.push(/* @__PURE__ */ jsxs("div", {
|
|
2920
2955
|
role: "listitem",
|
|
2921
|
-
className: cx("ui-cv-line", on && "is-target"),
|
|
2956
|
+
className: cx("ui-cv-line", on && "is-target", peek === n && "is-peek"),
|
|
2922
2957
|
"data-n": n,
|
|
2923
2958
|
"aria-current": on && n === ta ? "location" : void 0,
|
|
2924
2959
|
tabIndex: on && n === ta ? -1 : void 0,
|
|
@@ -3009,7 +3044,14 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
|
|
|
3009
3044
|
if (!el) return;
|
|
3010
3045
|
el.tabIndex = -1;
|
|
3011
3046
|
el.focus();
|
|
3047
|
+
onLine?.(nOf(el), "key");
|
|
3012
3048
|
};
|
|
3049
|
+
if (onLine && !nav && (e.key === "ArrowUp" || e.key === "ArrowDown")) {
|
|
3050
|
+
e.preventDefault();
|
|
3051
|
+
if (now < 0) focusLine(items.find((el) => el.classList.contains("is-target")) ?? items[0]);
|
|
3052
|
+
else focusLine(items[Math.max(0, Math.min(items.length - 1, now + (e.key === "ArrowDown" ? 1 : -1)))]);
|
|
3053
|
+
return;
|
|
3054
|
+
}
|
|
3013
3055
|
if (nav) {
|
|
3014
3056
|
const stops = [...pre.querySelectorAll(".ui-cv-name, .ui-cv-ref, .ui-cv-term")];
|
|
3015
3057
|
const onStop = act ? stops.indexOf(act) : -1;
|
|
@@ -3067,11 +3109,29 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
|
|
|
3067
3109
|
if (!el) return;
|
|
3068
3110
|
el.tabIndex = -1;
|
|
3069
3111
|
el.focus();
|
|
3112
|
+
onLine?.(nOf(el), "key");
|
|
3070
3113
|
if (said) announce(tr("{name},第 {n} 行", {
|
|
3071
3114
|
name: said,
|
|
3072
3115
|
n: nOf(el)
|
|
3073
3116
|
}));
|
|
3074
3117
|
};
|
|
3118
|
+
const onLineClick = (e) => {
|
|
3119
|
+
if (!onLine) return;
|
|
3120
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
3121
|
+
if (!t) return;
|
|
3122
|
+
const type = e.nativeEvent.pointerType;
|
|
3123
|
+
if (t.closest(".ui-cv-name") && (e.detail === 0 || type === "touch" || type === "pen" || e.ctrlKey || e.metaKey)) return;
|
|
3124
|
+
if (t.closest("a:not(.ui-cv-name), button:not(.ui-cv-name)")) return;
|
|
3125
|
+
if (selectedIn(e.currentTarget)) return;
|
|
3126
|
+
const row = t.closest("[role=\"listitem\"]");
|
|
3127
|
+
if (row) onLine(Number(row.dataset.n), "click");
|
|
3128
|
+
};
|
|
3129
|
+
const onCopyText = (e) => {
|
|
3130
|
+
const text = selectedCode(e.currentTarget);
|
|
3131
|
+
if (text == null) return;
|
|
3132
|
+
e.preventDefault();
|
|
3133
|
+
e.clipboardData.setData("text/plain", text);
|
|
3134
|
+
};
|
|
3075
3135
|
const [copied, setCopied] = React.useState(false);
|
|
3076
3136
|
const [keysOpen, setKeysOpen] = React.useState(false);
|
|
3077
3137
|
const byKey = React.useRef(false);
|
|
@@ -3165,8 +3225,13 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
|
|
|
3165
3225
|
"aria-label": tr("程式碼"),
|
|
3166
3226
|
"aria-keyshortcuts": [...keys.map((k) => k.k), "?"].join(" "),
|
|
3167
3227
|
onKeyDown: onMove,
|
|
3228
|
+
onCopy: onCopyText,
|
|
3229
|
+
onClick: nav ? void 0 : onLineClick,
|
|
3168
3230
|
...nav ? {
|
|
3169
|
-
onClick:
|
|
3231
|
+
onClick: (e) => {
|
|
3232
|
+
onLinkClick(e);
|
|
3233
|
+
onLineClick(e);
|
|
3234
|
+
},
|
|
3170
3235
|
onPointerEnter: () => inside.current = true,
|
|
3171
3236
|
onPointerLeave: () => {
|
|
3172
3237
|
inside.current = false;
|
|
@@ -10319,8 +10384,9 @@ function TreeList(p) {
|
|
|
10319
10384
|
}
|
|
10320
10385
|
} else if (k === "Enter") {
|
|
10321
10386
|
if (plain) {
|
|
10387
|
+
const again = p.selectedId != null && p.selectedId === r.n.id;
|
|
10322
10388
|
p.onSelect?.(r.n);
|
|
10323
|
-
if (r.isF) toggle(r.n);
|
|
10389
|
+
if (r.isF && !again) toggle(r.n);
|
|
10324
10390
|
} else if (r.isF) toggle(r.n);
|
|
10325
10391
|
else openFile(r.n);
|
|
10326
10392
|
} else checkToggle(r.n);
|
|
@@ -10355,9 +10421,9 @@ function TreeList(p) {
|
|
|
10355
10421
|
"aria-selected": p.onSelect ? on : void 0,
|
|
10356
10422
|
className: cx("ui-tree-row", "is-plain", on && "is-current", n.muted && "is-muted", r.fresh && "ui-tree-in"),
|
|
10357
10423
|
onClick: () => {
|
|
10358
|
-
setFocusId(n.id);
|
|
10424
|
+
if (focusId !== n.id) setFocusId(n.id);
|
|
10359
10425
|
p.onSelect?.(n);
|
|
10360
|
-
if (isF) toggle(n);
|
|
10426
|
+
if (isF && !on) toggle(n);
|
|
10361
10427
|
},
|
|
10362
10428
|
children: [
|
|
10363
10429
|
/* @__PURE__ */ jsx(Guides, { guides: r.guides }),
|