@seanyeh77/ui 0.31.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/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { a as parseDate, c as addMessages, d as unitFor, i as format, l as setLocale, n as fileSize, o as sameDay, r as fmtDate, s as I18N, t as dayAdd, u as tr } from "./format-entry-BohS-vNb.js";
2
+ import { a as parseDate, c as addMessages, d as unitFor, i as format, l as setLocale, n as fileSize, o as sameDay, r as fmtDate, s as I18N, t as dayAdd, u as tr } from "./format-entry-B9oxzJvw.js";
3
3
  import * as React from "react";
4
4
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
5
  import { Tooltip as Tooltip$1 } from "@base-ui/react/tooltip";
@@ -2267,7 +2267,7 @@ function CodeText({ text, refHref, onRef, isRef = isCodeRef, as: Tag = "span", c
2267
2267
  }
2268
2268
  // src/internal/FloatingPanel.tsx
2269
2269
  /** A positioned panel next to its trigger: flips when there is no room, closes on Esc and outside press, returns focus. */
2270
- function FloatingPanel({ open, onOpenChange, trigger, side = "bottom", align = "start", sideOffset = 6, className, openOnHover, anchor, boundary, children, ...aria }) {
2270
+ function FloatingPanel({ open, onOpenChange, trigger, side = "bottom", align = "start", sideOffset = 6, className, openOnHover, anchor, boundary, delay = 0, id, focusOnOpen = true, children, ...aria }) {
2271
2271
  const within = boundary?.current ?? void 0;
2272
2272
  return /* @__PURE__ */ jsxs(Popover$1.Root, {
2273
2273
  open,
@@ -2275,7 +2275,7 @@ function FloatingPanel({ open, onOpenChange, trigger, side = "bottom", align = "
2275
2275
  children: [/* @__PURE__ */ jsx(Popover$1.Trigger, {
2276
2276
  render: trigger,
2277
2277
  openOnHover,
2278
- delay: 0,
2278
+ delay,
2279
2279
  closeDelay: 150
2280
2280
  }), /* @__PURE__ */ jsx(Popover$1.Portal, { children: /* @__PURE__ */ jsx(Popover$1.Positioner, {
2281
2281
  className: "ui-float",
@@ -2290,13 +2290,197 @@ function FloatingPanel({ open, onOpenChange, trigger, side = "bottom", align = "
2290
2290
  align: "shift"
2291
2291
  } : void 0,
2292
2292
  children: /* @__PURE__ */ jsx(Popover$1.Popup, {
2293
+ id,
2293
2294
  className,
2294
2295
  "aria-label": aria["aria-label"],
2296
+ initialFocus: focusOnOpen ? void 0 : false,
2295
2297
  children
2296
2298
  })
2297
2299
  }) })]
2298
2300
  });
2299
2301
  }
2302
+ /** A word that opens its explanation: a floating card from 600 px, a sheet from the bottom on phones. `icon` adds a small
2303
+ * question mark; `inline` marks the word with a dashed underline and opens on hover or keyboard focus. */
2304
+ function Term({ children, title, text, content, code, action, refHref, onRef, isRef, variant = "icon", openOnFocus = true, tabIndex, className }) {
2305
+ const [open, setOpen] = React.useState(false);
2306
+ const narrow = useNarrow();
2307
+ const inlineV = variant === "inline";
2308
+ const cardId = React.useId();
2309
+ const ref = React.useRef(null);
2310
+ const [inline, setInline] = React.useState(false);
2311
+ React.useLayoutEffect(() => {
2312
+ const par = ref.current?.parentNode;
2313
+ const txt = !!par && Array.from(par.childNodes).some((n) => n.nodeType === 3 && !!n.textContent?.trim());
2314
+ setInline(txt);
2315
+ }, [children]);
2316
+ const block = React.useRef(null);
2317
+ const toggle = (next) => {
2318
+ if (next) {
2319
+ const el = ref.current?.closest(".ui-card, .ui-split-a, .ui-split-b, .ui-shell-main, main") ?? null;
2320
+ block.current = el && el.getBoundingClientRect().width >= 240 ? el : null;
2321
+ }
2322
+ setOpen(next);
2323
+ };
2324
+ const heading = title || (typeof children === "string" ? children : "");
2325
+ const onFocus = (e) => {
2326
+ if (openOnFocus && !narrow && e.currentTarget.matches(":focus-visible")) toggle(true);
2327
+ };
2328
+ const onBlur = (e) => {
2329
+ const to = e.relatedTarget;
2330
+ if (to && (ref.current?.contains(to) || document.getElementById(cardId)?.contains(to))) return;
2331
+ if (!narrow) setOpen(false);
2332
+ };
2333
+ const trigger = inlineV ? /* @__PURE__ */ jsxs("button", {
2334
+ type: "button",
2335
+ ref,
2336
+ className: cx("ui-term", "is-word", open && "is-open", className),
2337
+ tabIndex,
2338
+ "aria-describedby": open && !narrow ? cardId : void 0,
2339
+ onFocus,
2340
+ onBlur,
2341
+ children: [children, /* @__PURE__ */ jsx("span", {
2342
+ className: "ui-visually-hidden",
2343
+ children: tr(",有說明")
2344
+ })]
2345
+ }) : /* @__PURE__ */ jsxs("button", {
2346
+ type: "button",
2347
+ ref,
2348
+ className: cx("ui-term", inline && "is-inline", open && "is-open", className),
2349
+ children: [
2350
+ /* @__PURE__ */ jsx("span", {
2351
+ className: "ui-visually-hidden",
2352
+ children: tr("說明") + " "
2353
+ }),
2354
+ children,
2355
+ /* @__PURE__ */ jsx(Icon, {
2356
+ name: "question",
2357
+ size: 14,
2358
+ className: "ui-term-q"
2359
+ })
2360
+ ]
2361
+ });
2362
+ const body = (Title, Close) => /* @__PURE__ */ jsxs(Fragment, { children: [
2363
+ /* @__PURE__ */ jsxs("div", {
2364
+ className: "ui-term-head",
2365
+ children: [/* @__PURE__ */ jsx(Title, {
2366
+ className: "ui-term-title",
2367
+ children: heading
2368
+ }), /* @__PURE__ */ jsx(Close, {
2369
+ className: "ui-dialog-close ui-term-close",
2370
+ "aria-label": tr("關閉說明"),
2371
+ children: /* @__PURE__ */ jsx(Icon, { name: "x" })
2372
+ })]
2373
+ }),
2374
+ text ? /* @__PURE__ */ jsx(CodeText, {
2375
+ as: "div",
2376
+ className: "ui-term-body",
2377
+ text,
2378
+ refHref,
2379
+ onRef,
2380
+ isRef
2381
+ }) : null,
2382
+ content ? /* @__PURE__ */ jsx("div", {
2383
+ className: "ui-term-body",
2384
+ children: content
2385
+ }) : null,
2386
+ code ? /* @__PURE__ */ jsx("pre", {
2387
+ className: "ui-term-code",
2388
+ children: code
2389
+ }) : null,
2390
+ action ? /* @__PURE__ */ jsx("button", {
2391
+ type: "button",
2392
+ className: "ui-link-btn ui-term-action",
2393
+ onClick: () => {
2394
+ setOpen(false);
2395
+ action.onClick?.();
2396
+ },
2397
+ children: action.label
2398
+ }) : null
2399
+ ] });
2400
+ if (narrow) return /* @__PURE__ */ jsxs(Dialog$1.Root, {
2401
+ open,
2402
+ onOpenChange: setOpen,
2403
+ children: [/* @__PURE__ */ jsx(Dialog$1.Trigger, { render: trigger }), /* @__PURE__ */ jsxs(Dialog$1.Portal, { children: [/* @__PURE__ */ jsx(Dialog$1.Backdrop, { className: "ui-term-backdrop" }), /* @__PURE__ */ jsx(Dialog$1.Popup, {
2404
+ className: "ui-term-sheet",
2405
+ children: body(Dialog$1.Title, Dialog$1.Close)
2406
+ })] })]
2407
+ });
2408
+ return /* @__PURE__ */ jsx(FloatingPanel, {
2409
+ open,
2410
+ onOpenChange: toggle,
2411
+ sideOffset: 8,
2412
+ className: "ui-term-pop",
2413
+ trigger,
2414
+ boundary: block,
2415
+ ...inlineV ? {
2416
+ openOnHover: true,
2417
+ delay: 500,
2418
+ id: cardId,
2419
+ focusOnOpen: false
2420
+ } : {},
2421
+ children: inlineV ? /* @__PURE__ */ jsx("div", {
2422
+ onBlur,
2423
+ children: body(Popover$1.Title, Popover$1.Close)
2424
+ }) : body(Popover$1.Title, Popover$1.Close)
2425
+ });
2426
+ }
2427
+ // src/internal/terms.tsx
2428
+ const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2429
+ const WORDY = /^[\p{Script=Latin}\p{N}_]$/u;
2430
+ /** One pattern for all the words, longest first, so 「cookie jar」 wins over 「cookie」. */
2431
+ function termPattern(terms) {
2432
+ const list = (terms ?? []).filter((t) => t.term);
2433
+ if (!list.length) return null;
2434
+ const alts = [...list].sort((a, b) => b.term.length - a.term.length).map((t) => {
2435
+ const w = [...t.term];
2436
+ const pre = WORDY.test(w[0]) ? "(?<![\\p{Script=Latin}\\p{N}_])" : "";
2437
+ const post = WORDY.test(w[w.length - 1]) ? "(?![\\p{Script=Latin}\\p{N}_])" : "";
2438
+ return pre + esc(t.term) + post;
2439
+ });
2440
+ return new RegExp(alts.join("|"), "gu");
2441
+ }
2442
+ /** The words in a piece of text. `seen` holds the words already marked in this block; with `first` they are skipped
2443
+ * and the new ones added. */
2444
+ function findTerms(text, terms, re, mark, seen) {
2445
+ const out = [];
2446
+ for (const m of text.matchAll(re)) {
2447
+ const i = terms.findIndex((t) => t.term === m[0]);
2448
+ if (i < 0) continue;
2449
+ if (mark === "first") {
2450
+ if (seen.has(m[0])) continue;
2451
+ seen.add(m[0]);
2452
+ }
2453
+ out.push({
2454
+ from: m.index,
2455
+ to: m.index + m[0].length,
2456
+ i
2457
+ });
2458
+ }
2459
+ return out;
2460
+ }
2461
+ /** The word as an inline Term. */
2462
+ function termNode(t, key, children, extra) {
2463
+ return /* @__PURE__ */ jsx(Term, {
2464
+ variant: "inline",
2465
+ title: t.title,
2466
+ text: t.text,
2467
+ action: t.action,
2468
+ ...extra,
2469
+ children: children ?? t.term
2470
+ }, key);
2471
+ }
2472
+ /** Text with its words as inline Terms; `rest` renders the text between them. */
2473
+ function withTerms(text, hits, terms, key, rest = (s) => s, extra) {
2474
+ const out = [];
2475
+ let at = 0;
2476
+ for (const h of hits) {
2477
+ if (h.from > at) out.push(/* @__PURE__ */ jsx(React.Fragment, { children: rest(text.slice(at, h.from)) }, key()));
2478
+ out.push(termNode(terms[h.i], key(), text.slice(h.from, h.to), extra));
2479
+ at = h.to;
2480
+ }
2481
+ if (at < text.length) out.push(/* @__PURE__ */ jsx(React.Fragment, { children: rest(text.slice(at)) }, key()));
2482
+ return out;
2483
+ }
2300
2484
  // src/components/CodeView/tokens.ts
2301
2485
  const KEYWORDS = {
2302
2486
  c: new Set("auto break case char const continue default do double else enum extern float for goto if inline int long register restrict return short signed sizeof static struct switch typedef union unsigned void volatile while bool true false NULL nullptr class namespace template typename public private protected virtual new delete".split(" ")),
@@ -2487,6 +2671,69 @@ function matchLinks(lines, start, links) {
2487
2671
  for (const list of out.values()) list.sort((a, b) => a.from - b.from);
2488
2672
  return out;
2489
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
+ }
2701
+ /** Each line's words from `terms`: only in comments and strings, never where a name link or a path is. With 'first',
2702
+ * each word once per file, at its first place. */
2703
+ function matchTerms(toks, start, links, terms, mark) {
2704
+ const out = /* @__PURE__ */ new Map();
2705
+ const re = termPattern(terms);
2706
+ if (!re || !terms) return out;
2707
+ const seen = /* @__PURE__ */ new Set();
2708
+ toks.forEach((line, i) => {
2709
+ const n = start + i;
2710
+ const taken = links.get(n) ?? [];
2711
+ let at = 0;
2712
+ for (const [cls, text] of line) {
2713
+ if (cls === "is-comment" || cls === "is-string") {
2714
+ const paths = [...text.matchAll(PATH_IN_CODE)].map((m) => ({
2715
+ from: at + m.index,
2716
+ to: at + m.index + m[0].length
2717
+ }));
2718
+ const free = (from, to) => ![...taken, ...paths].some((h) => h.from < to && from < h.to);
2719
+ const found = findTerms(text, terms, re, "all", /* @__PURE__ */ new Set()).map((w) => ({
2720
+ ...w,
2721
+ from: w.from + at,
2722
+ to: w.to + at
2723
+ })).filter((w) => free(w.from, w.to)).filter((w) => {
2724
+ if (mark === "all") return true;
2725
+ const t = terms[w.i].term;
2726
+ if (seen.has(t)) return false;
2727
+ seen.add(t);
2728
+ return true;
2729
+ });
2730
+ if (found.length) out.set(n, [...out.get(n) ?? [], ...found]);
2731
+ }
2732
+ at += text.length;
2733
+ }
2734
+ });
2735
+ return out;
2736
+ }
2490
2737
  /** What the keys do: a small panel opened from the keyboard button or with ?; nothing on screen until asked. */
2491
2738
  function KeysPanel(p) {
2492
2739
  return /* @__PURE__ */ jsx(FloatingPanel, {
@@ -2504,7 +2751,7 @@ function KeysPanel(p) {
2504
2751
  });
2505
2752
  }
2506
2753
  /** Source with line numbers and an optional target line: soft accent background, long lines wrap, light syntax colors. */
2507
- function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile, title, language, languageLabel, highlight = true, tokenizer = tokenize, refHref, onRef, links, onLink, preview, 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 }) {
2508
2755
  const lines = React.useMemo(() => String(code || "").replace(/\n$/, "").split("\n"), [code]);
2509
2756
  const lang = language || languageOf(file);
2510
2757
  const toks = React.useMemo(() => highlight ? tokenizer(lines, lang) : lines.map((l) => [["", l]]), [
@@ -2518,7 +2765,14 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2518
2765
  start,
2519
2766
  links
2520
2767
  ]);
2521
- const nav = hits.size > 0;
2768
+ const termHits = React.useMemo(() => matchTerms(toks, start, hits, terms, termsMark), [
2769
+ toks,
2770
+ start,
2771
+ hits,
2772
+ terms,
2773
+ termsMark
2774
+ ]);
2775
+ const nav = hits.size > 0 || termHits.size > 0;
2522
2776
  const [armed, setArmed] = React.useState(false);
2523
2777
  const inside = React.useRef(false);
2524
2778
  React.useEffect(() => {
@@ -2599,17 +2853,39 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2599
2853
  const from = windowed ? Math.max(0, ta - start - context) : 0;
2600
2854
  const to = windowed ? Math.min(lines.length, tb - start + context + 1) : lines.length;
2601
2855
  const body = React.useRef(null);
2602
- React.useImperativeHandle(ref, () => ({ focusTarget: () => body.current?.querySelector(".is-target")?.focus() }));
2856
+ React.useImperativeHandle(ref, () => ({ focusTarget: () => {
2857
+ if (selectedIn(body.current)) return;
2858
+ body.current?.querySelector(".is-target")?.focus();
2859
+ } }));
2603
2860
  React.useEffect(() => {
2604
2861
  const b = body.current;
2605
2862
  const el = b?.querySelector(".is-target");
2606
- 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;
2607
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]);
2608
2870
  const digits = String(start + lines.length - 1).length;
2609
- const piece = (cls, text, key, inLink) => cls ? /* @__PURE__ */ jsx("span", {
2610
- className: cls,
2611
- children: !inLink && (refHref || onRef) && (cls === "is-comment" || cls === "is-string") ? withPaths(text, refHref, onRef, nav ? -1 : void 0) : breakable(text)
2612
- }, key) : /* @__PURE__ */ jsx(React.Fragment, { children: breakable(text) }, key);
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);
2872
+ const piece = (cls, text, key, inLink, at = 0, words) => {
2873
+ const mine = !inLink && terms && words ? words.filter((w) => w.from >= at && w.to <= at + text.length) : [];
2874
+ let k = 0;
2875
+ const kids = mine.length && terms ? withTerms(text, mine.map((w) => ({
2876
+ ...w,
2877
+ from: w.from - at,
2878
+ to: w.to - at
2879
+ })), terms, () => key + "t" + k++, (t) => prose(cls, t, false), {
2880
+ className: "ui-cv-term",
2881
+ tabIndex: -1,
2882
+ openOnFocus: false
2883
+ }) : prose(cls, text, inLink);
2884
+ return cls ? /* @__PURE__ */ jsx("span", {
2885
+ className: cls,
2886
+ children: kids
2887
+ }, key) : /* @__PURE__ */ jsx(React.Fragment, { children: kids }, key);
2888
+ };
2613
2889
  const nameLink = (hit, kids) => {
2614
2890
  const link = links?.[hit.i];
2615
2891
  if (!link) return null;
@@ -2626,8 +2902,15 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2626
2902
  children: kids
2627
2903
  }, "n" + hit.i);
2628
2904
  };
2629
- const lineContent = (line, lineHits) => {
2630
- if (!lineHits) return line.map(([cls, text], j) => piece(cls, text, String(j), false));
2905
+ const lineContent = (line, lineHits, words) => {
2906
+ if (!lineHits) {
2907
+ let at = 0;
2908
+ return line.map(([cls, text], j) => {
2909
+ const el = piece(cls, text, String(j), false, at, words);
2910
+ at += text.length;
2911
+ return el;
2912
+ });
2913
+ }
2631
2914
  const out = [];
2632
2915
  let group = [];
2633
2916
  let open = null;
@@ -2655,7 +2938,7 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2655
2938
  group.push(piece(cls, text.slice(k, end - pos), key, true));
2656
2939
  } else {
2657
2940
  flush();
2658
- out.push(piece(cls, text.slice(k, end - pos), key, false));
2941
+ out.push(piece(cls, text.slice(k, end - pos), key, false, at, words));
2659
2942
  }
2660
2943
  k = end - pos;
2661
2944
  }
@@ -2670,7 +2953,7 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2670
2953
  const on = ta != null && tb != null && n >= ta && n <= tb;
2671
2954
  rows.push(/* @__PURE__ */ jsxs("div", {
2672
2955
  role: "listitem",
2673
- className: cx("ui-cv-line", on && "is-target"),
2956
+ className: cx("ui-cv-line", on && "is-target", peek === n && "is-peek"),
2674
2957
  "data-n": n,
2675
2958
  "aria-current": on && n === ta ? "location" : void 0,
2676
2959
  tabIndex: on && n === ta ? -1 : void 0,
@@ -2684,7 +2967,7 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2684
2967
  children: [/* @__PURE__ */ jsx("span", {
2685
2968
  className: "ui-visually-hidden ui-cv-sr",
2686
2969
  children: tr("第 {n} 行", { n }) + indentText(lines[i]) + tr(":")
2687
- }), lineContent(toks[i], hits.get(n))]
2970
+ }), lineContent(toks[i], hits.get(n), termHits.get(n))]
2688
2971
  })]
2689
2972
  }, i));
2690
2973
  }
@@ -2698,7 +2981,7 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2698
2981
  {
2699
2982
  k: "ArrowLeft ArrowRight",
2700
2983
  label: "←→",
2701
- what: tr("上一個/下一個連結")
2984
+ what: tr(!termHits.size ? "上一個/下一個連結" : hits.size ? "上一個/下一個連結或名詞" : "上一個/下一個名詞")
2702
2985
  },
2703
2986
  {
2704
2987
  k: "Enter",
@@ -2761,11 +3044,19 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2761
3044
  if (!el) return;
2762
3045
  el.tabIndex = -1;
2763
3046
  el.focus();
3047
+ onLine?.(nOf(el), "key");
2764
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
+ }
2765
3055
  if (nav) {
2766
- const stops = [...pre.querySelectorAll(".ui-cv-name, .ui-cv-ref")];
3056
+ const stops = [...pre.querySelectorAll(".ui-cv-name, .ui-cv-ref, .ui-cv-term")];
2767
3057
  const onStop = act ? stops.indexOf(act) : -1;
2768
3058
  if (e.key === "Escape") {
3059
+ if (act?.classList.contains("ui-cv-term") && act.getAttribute("aria-expanded") === "true") return;
2769
3060
  if (pv) setPv(null);
2770
3061
  else if (onStop >= 0) focusLine(curLine ?? void 0);
2771
3062
  else if (curLine) pre.focus();
@@ -2818,11 +3109,29 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2818
3109
  if (!el) return;
2819
3110
  el.tabIndex = -1;
2820
3111
  el.focus();
3112
+ onLine?.(nOf(el), "key");
2821
3113
  if (said) announce(tr("{name},第 {n} 行", {
2822
3114
  name: said,
2823
3115
  n: nOf(el)
2824
3116
  }));
2825
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
+ };
2826
3135
  const [copied, setCopied] = React.useState(false);
2827
3136
  const [keysOpen, setKeysOpen] = React.useState(false);
2828
3137
  const byKey = React.useRef(false);
@@ -2916,8 +3225,13 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2916
3225
  "aria-label": tr("程式碼"),
2917
3226
  "aria-keyshortcuts": [...keys.map((k) => k.k), "?"].join(" "),
2918
3227
  onKeyDown: onMove,
3228
+ onCopy: onCopyText,
3229
+ onClick: nav ? void 0 : onLineClick,
2919
3230
  ...nav ? {
2920
- onClick: onLinkClick,
3231
+ onClick: (e) => {
3232
+ onLinkClick(e);
3233
+ onLineClick(e);
3234
+ },
2921
3235
  onPointerEnter: () => inside.current = true,
2922
3236
  onPointerLeave: () => {
2923
3237
  inside.current = false;
@@ -5562,100 +5876,6 @@ function FormGrid({ children, className }) {
5562
5876
  children
5563
5877
  });
5564
5878
  }
5565
- // src/components/Term/Term.tsx
5566
- /** A word followed by a small question mark that opens its explanation: a floating card from 600 px, a sheet from the bottom on phones. */
5567
- function Term({ children, title, text, content, code, action, refHref, onRef, isRef, className }) {
5568
- const [open, setOpen] = React.useState(false);
5569
- const narrow = useNarrow();
5570
- const ref = React.useRef(null);
5571
- const [inline, setInline] = React.useState(false);
5572
- React.useLayoutEffect(() => {
5573
- const par = ref.current?.parentNode;
5574
- const txt = !!par && Array.from(par.childNodes).some((n) => n.nodeType === 3 && !!n.textContent?.trim());
5575
- setInline(txt);
5576
- }, [children]);
5577
- const block = React.useRef(null);
5578
- const toggle = (next) => {
5579
- if (next) {
5580
- const el = ref.current?.closest(".ui-card, .ui-split-a, .ui-split-b, .ui-shell-main, main") ?? null;
5581
- block.current = el && el.getBoundingClientRect().width >= 240 ? el : null;
5582
- }
5583
- setOpen(next);
5584
- };
5585
- const heading = title || (typeof children === "string" ? children : "");
5586
- const trigger = /* @__PURE__ */ jsxs("button", {
5587
- type: "button",
5588
- ref,
5589
- className: cx("ui-term", inline && "is-inline", open && "is-open", className),
5590
- children: [
5591
- /* @__PURE__ */ jsx("span", {
5592
- className: "ui-visually-hidden",
5593
- children: tr("說明") + " "
5594
- }),
5595
- children,
5596
- /* @__PURE__ */ jsx(Icon, {
5597
- name: "question",
5598
- size: 14,
5599
- className: "ui-term-q"
5600
- })
5601
- ]
5602
- });
5603
- const body = (Title, Close) => /* @__PURE__ */ jsxs(Fragment, { children: [
5604
- /* @__PURE__ */ jsxs("div", {
5605
- className: "ui-term-head",
5606
- children: [/* @__PURE__ */ jsx(Title, {
5607
- className: "ui-term-title",
5608
- children: heading
5609
- }), /* @__PURE__ */ jsx(Close, {
5610
- className: "ui-dialog-close ui-term-close",
5611
- "aria-label": tr("關閉說明"),
5612
- children: /* @__PURE__ */ jsx(Icon, { name: "x" })
5613
- })]
5614
- }),
5615
- text ? /* @__PURE__ */ jsx(CodeText, {
5616
- as: "div",
5617
- className: "ui-term-body",
5618
- text,
5619
- refHref,
5620
- onRef,
5621
- isRef
5622
- }) : null,
5623
- content ? /* @__PURE__ */ jsx("div", {
5624
- className: "ui-term-body",
5625
- children: content
5626
- }) : null,
5627
- code ? /* @__PURE__ */ jsx("pre", {
5628
- className: "ui-term-code",
5629
- children: code
5630
- }) : null,
5631
- action ? /* @__PURE__ */ jsx("button", {
5632
- type: "button",
5633
- className: "ui-link-btn ui-term-action",
5634
- onClick: () => {
5635
- setOpen(false);
5636
- action.onClick?.();
5637
- },
5638
- children: action.label
5639
- }) : null
5640
- ] });
5641
- if (narrow) return /* @__PURE__ */ jsxs(Dialog$1.Root, {
5642
- open,
5643
- onOpenChange: setOpen,
5644
- children: [/* @__PURE__ */ jsx(Dialog$1.Trigger, { render: trigger }), /* @__PURE__ */ jsxs(Dialog$1.Portal, { children: [/* @__PURE__ */ jsx(Dialog$1.Backdrop, { className: "ui-term-backdrop" }), /* @__PURE__ */ jsx(Dialog$1.Popup, {
5645
- className: "ui-term-sheet",
5646
- children: body(Dialog$1.Title, Dialog$1.Close)
5647
- })] })]
5648
- });
5649
- return /* @__PURE__ */ jsx(FloatingPanel, {
5650
- open,
5651
- onOpenChange: toggle,
5652
- sideOffset: 8,
5653
- className: "ui-term-pop",
5654
- trigger,
5655
- boundary: block,
5656
- children: body(Popover$1.Title, Popover$1.Close)
5657
- });
5658
- }
5659
5879
  // src/components/Graph/Graph.tsx
5660
5880
  /** <defs> with the arrowheads #ui-arrow and #ui-arrow-accent; put it first inside your diagram's <svg>. */
5661
5881
  function GraphDefs() {
@@ -5970,13 +6190,24 @@ const BLOCKED = [
5970
6190
  "data",
5971
6191
  "vbscript"
5972
6192
  ];
6193
+ /** A new block: its own count of marked words. */
6194
+ const block = (c) => ({
6195
+ ...c,
6196
+ seen: /* @__PURE__ */ new Set()
6197
+ });
6198
+ /** No words marked inside (headings, link text, a summary button). */
6199
+ const noTerms = (c) => ({
6200
+ ...c,
6201
+ seen: void 0
6202
+ });
5973
6203
  const CJK = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\u3000-\u303f\uff00-\uffef]/u;
5974
6204
  /** Inline Markdown: code spans, links, autolinks, bold, italic, hard breaks; HTML tags dropped, their text kept. */
5975
6205
  function inline(src, c) {
5976
6206
  const out = [];
5977
6207
  let text = "";
5978
6208
  const flush = () => {
5979
- if (text) out.push(text);
6209
+ if (text && c.termRe && c.terms && c.seen) out.push(...withTerms(text, findTerms(text, c.terms, c.termRe, c.termsMark, c.seen), c.terms, c.k));
6210
+ else if (text) out.push(text);
5980
6211
  text = "";
5981
6212
  };
5982
6213
  let i = 0;
@@ -6054,8 +6285,21 @@ function inline(src, c) {
6054
6285
  continue;
6055
6286
  }
6056
6287
  flush();
6288
+ const termId = /^term:(.*)$/i.exec(m.href.trim())?.[1];
6289
+ if (termId !== void 0) {
6290
+ let id = termId;
6291
+ try {
6292
+ id = decodeURIComponent(termId);
6293
+ } catch {}
6294
+ const t = c.terms?.find((x) => x.term === id);
6295
+ devWarn(!t, "Markdown", `[${m.text}](term:${termId}) 找不到 terms 裡 term 為「${id}」的項目,只顯示文字。把它加進 terms,或改 term: 後面的字。`);
6296
+ c.seen?.add(id);
6297
+ out.push(t ? termNode(t, c.k(), inline(m.text, noTerms(c))) : /* @__PURE__ */ jsx(React.Fragment, { children: inline(m.text, noTerms(c)) }, c.k()));
6298
+ i = m.end;
6299
+ continue;
6300
+ }
6057
6301
  const name = /^\s*`([^`]+)`\s*$/.exec(m.text)?.[1];
6058
- out.push(link(m.href, inline(m.text, c), c, name));
6302
+ out.push(link(m.href, inline(m.text, noTerms(c)), c, name));
6059
6303
  i = m.end;
6060
6304
  continue;
6061
6305
  }
@@ -6211,19 +6455,24 @@ const NUM = /^[-+]?(?:NT\$\s?)?[\d,]+(?:\.\d+)?\s?(?:%|[A-Za-z]{1,3}|\p{Script=H
6211
6455
  * tags are dropped and their text kept; links only to http, https, mailto and relative addresses. */
6212
6456
  function Markdown(p) {
6213
6457
  let n = 0;
6458
+ const termsMark = p.termsMark ?? "first";
6459
+ const termRe = React.useMemo(() => termPattern(p.terms), [p.terms]);
6214
6460
  const c = {
6215
6461
  k: () => "m" + n++,
6216
6462
  schemes: p.schemes,
6217
6463
  linkHref: p.linkHref,
6218
6464
  onLink: p.onLink,
6219
6465
  refHref: p.refHref,
6220
- onRef: p.onRef
6466
+ onRef: p.onRef,
6467
+ terms: p.terms,
6468
+ termsMark,
6469
+ termRe
6221
6470
  };
6222
6471
  const src = String(p.children ?? "").replace(/\r\n?/g, "\n");
6223
6472
  const cls = cx("ui-md", p.size === "small" && "is-small", p.className);
6224
6473
  if (p.inline) return /* @__PURE__ */ jsx("span", {
6225
6474
  className: cx(cls, "is-inline"),
6226
- children: inline(src.trim().replace(/\n\s*\n/g, "\n"), c)
6475
+ children: inline(src.trim().replace(/\n\s*\n/g, "\n"), block(c))
6227
6476
  });
6228
6477
  const top = p.topLevel ?? 4;
6229
6478
  const render = (blocks) => blocks.map((b) => {
@@ -6232,13 +6481,13 @@ function Markdown(p) {
6232
6481
  const H = `h${Math.min(6, top + b.level - 1)}`;
6233
6482
  return /* @__PURE__ */ jsx(H, {
6234
6483
  className: "ui-md-h",
6235
- children: inline(b.text, c)
6484
+ children: inline(b.text, noTerms(c))
6236
6485
  }, c.k());
6237
6486
  }
6238
- case "p": return /* @__PURE__ */ jsx("p", { children: inline(b.text, c) }, c.k());
6487
+ case "p": return /* @__PURE__ */ jsx("p", { children: inline(b.text, block(c)) }, c.k());
6239
6488
  case "hr": return /* @__PURE__ */ jsx("hr", {}, c.k());
6240
6489
  case "details": return /* @__PURE__ */ jsx(MdDetails, {
6241
- summary: inline(b.summary, c),
6490
+ summary: inline(b.summary, noTerms(c)),
6242
6491
  children: render(b.blocks)
6243
6492
  }, c.k());
6244
6493
  case "alert": return /* @__PURE__ */ jsx(Alert, {
@@ -6248,7 +6497,7 @@ function Markdown(p) {
6248
6497
  }, c.k());
6249
6498
  case "quote": return /* @__PURE__ */ jsx("blockquote", { children: render(b.blocks) }, c.k());
6250
6499
  case "list": {
6251
- const items = b.items.map((it) => /* @__PURE__ */ jsx("li", { children: it.length === 1 && it[0].t === "p" ? inline(it[0].text, c) : render(it) }, c.k()));
6500
+ const items = b.items.map((it) => /* @__PURE__ */ jsx("li", { children: it.length === 1 && it[0].t === "p" ? inline(it[0].text, block(c)) : render(it) }, c.k()));
6252
6501
  return b.ordered ? /* @__PURE__ */ jsx("ol", {
6253
6502
  start: b.start === 1 ? void 0 : b.start,
6254
6503
  children: items
@@ -6262,7 +6511,9 @@ function Markdown(p) {
6262
6511
  copy: true,
6263
6512
  lineNumbers: false,
6264
6513
  refHref: p.refHref,
6265
- onRef: p.onRef
6514
+ onRef: p.onRef,
6515
+ terms: p.terms,
6516
+ termsMark
6266
6517
  }, c.k());
6267
6518
  case "table": {
6268
6519
  const n = b.head.length;
@@ -6273,9 +6524,9 @@ function Markdown(p) {
6273
6524
  };
6274
6525
  return /* @__PURE__ */ jsx(MdTable, {
6275
6526
  label: b.head.map(plain).join("、"),
6276
- heads: b.head.map((h) => inline(h, c)),
6527
+ heads: b.head.map((h) => inline(h, noTerms(c))),
6277
6528
  headText: b.head.map(plain),
6278
- rows: b.rows.map((r) => Array.from({ length: n }, (_, j) => inline(r[j] ?? "", c))),
6529
+ rows: b.rows.map((r) => Array.from({ length: n }, (_, j) => inline(r[j] ?? "", block(c)))),
6279
6530
  numeric,
6280
6531
  align
6281
6532
  }, c.k());
@@ -10133,8 +10384,9 @@ function TreeList(p) {
10133
10384
  }
10134
10385
  } else if (k === "Enter") {
10135
10386
  if (plain) {
10387
+ const again = p.selectedId != null && p.selectedId === r.n.id;
10136
10388
  p.onSelect?.(r.n);
10137
- if (r.isF) toggle(r.n);
10389
+ if (r.isF && !again) toggle(r.n);
10138
10390
  } else if (r.isF) toggle(r.n);
10139
10391
  else openFile(r.n);
10140
10392
  } else checkToggle(r.n);
@@ -10169,9 +10421,9 @@ function TreeList(p) {
10169
10421
  "aria-selected": p.onSelect ? on : void 0,
10170
10422
  className: cx("ui-tree-row", "is-plain", on && "is-current", n.muted && "is-muted", r.fresh && "ui-tree-in"),
10171
10423
  onClick: () => {
10172
- setFocusId(n.id);
10424
+ if (focusId !== n.id) setFocusId(n.id);
10173
10425
  p.onSelect?.(n);
10174
- if (isF) toggle(n);
10426
+ if (isF && !on) toggle(n);
10175
10427
  },
10176
10428
  children: [
10177
10429
  /* @__PURE__ */ jsx(Guides, { guides: r.guides }),