@seanyeh77/ui 0.21.0 → 0.22.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,10 +1,11 @@
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-B2vEJCCf.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-CKLBdBpY.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";
6
6
  import { Dialog as Dialog$1 } from "@base-ui/react/dialog";
7
7
  import { Menu as Menu$1 } from "@base-ui/react/menu";
8
+ import { PreviewCard } from "@base-ui/react/preview-card";
8
9
  import { Popover as Popover$1 } from "@base-ui/react/popover";
9
10
  import { Combobox } from "@base-ui/react/combobox";
10
11
  import { Select as Select$1 } from "@base-ui/react/select";
@@ -2157,7 +2158,7 @@ const AFTER = /([,.([{;])/;
2157
2158
  /** A piece of code with soft break points after commas, dots and opening brackets, and operators kept together. */
2158
2159
  const PATH_IN_CODE = /(?<![\w./-])((?:\.{1,2}\/)?(?:[\w.-]+\/)+[\w.-]+\.[A-Za-z0-9]+(?::\d+(?:-\d+)?)?|[\w.-]+\.[A-Za-z0-9]+:\d+(?:-\d+)?)(?![\w/])/g;
2159
2160
  /** The paths in a comment or string as links (refHref, onRef); the rest stays text. */
2160
- function withPaths(text, refHref, onRef) {
2161
+ function withPaths(text, refHref, onRef, tab) {
2161
2162
  const out = [];
2162
2163
  let at = 0;
2163
2164
  for (const m of text.matchAll(PATH_IN_CODE)) {
@@ -2174,11 +2175,13 @@ function withPaths(text, refHref, onRef) {
2174
2175
  className: "ui-cv-ref",
2175
2176
  href,
2176
2177
  onClick: click,
2178
+ tabIndex: tab,
2177
2179
  children: v
2178
2180
  }, m.index) : /* @__PURE__ */ jsx("button", {
2179
2181
  type: "button",
2180
2182
  className: "ui-cv-ref",
2181
2183
  onClick: click,
2184
+ tabIndex: tab,
2182
2185
  children: v
2183
2186
  }, m.index));
2184
2187
  at = m.index + v.length;
@@ -2207,8 +2210,62 @@ function breakable(text) {
2207
2210
  });
2208
2211
  return out;
2209
2212
  }
2213
+ const NAME_CHAR = /[\p{L}\p{N}_$]/u;
2214
+ /** UTF-16 index of a 1-based column counted in code points; -1 past the end. */
2215
+ function colIndex(s, col) {
2216
+ let i = 0;
2217
+ for (let c = 1; c < col; c++) {
2218
+ if (i >= s.length) return -1;
2219
+ i += (s.codePointAt(i) ?? 0) > 65535 ? 2 : 1;
2220
+ }
2221
+ return i;
2222
+ }
2223
+ /** The first whole `text` on the line: not inside a longer name, and not the tail of a dotted one (x.runtime.deploy). */
2224
+ function findName(s, text) {
2225
+ for (let i = s.indexOf(text); i >= 0; i = s.indexOf(text, i + 1)) {
2226
+ const before = s[i - 1] ?? "";
2227
+ const after = s[i + text.length] ?? "";
2228
+ if (!NAME_CHAR.test(before) && before !== "." && !NAME_CHAR.test(after)) return i;
2229
+ }
2230
+ return -1;
2231
+ }
2232
+ /** Each line's links, sorted, without overlaps; what does not match is left out with a development warning. */
2233
+ function matchLinks(lines, start, links) {
2234
+ const out = /* @__PURE__ */ new Map();
2235
+ links?.forEach((l, i) => {
2236
+ const s = lines[l.line - start];
2237
+ if (s == null) {
2238
+ devWarn(true, "CodeView", `第 ${l.line} 行不在程式碼裡,連結「${l.text}」沒有加上。line 和 target 一樣從 start(${start})算起。`);
2239
+ return;
2240
+ }
2241
+ let from = -1;
2242
+ if (l.col != null) {
2243
+ const at = colIndex(s, l.col);
2244
+ if (at >= 0 && s.startsWith(l.text, at)) from = at;
2245
+ else devWarn(true, "CodeView", `第 ${l.line} 行第 ${l.col} 欄不是「${l.text}」,連結沒有加上。col 從 1 開始、以字元計;位元組或 UTF-16 位置要先換算。`);
2246
+ } else {
2247
+ from = findName(s, l.text);
2248
+ devWarn(from < 0, "CodeView", `第 ${l.line} 行找不到完整的「${l.text}」,連結沒有加上。text 要和程式碼一字不差(整個帶點的名稱),或給 col。`);
2249
+ }
2250
+ if (from < 0) return;
2251
+ const hit = {
2252
+ from,
2253
+ to: from + l.text.length,
2254
+ i
2255
+ };
2256
+ const list = out.get(l.line) ?? [];
2257
+ if (list.some((h) => h.from < hit.to && hit.from < h.to)) {
2258
+ devWarn(true, "CodeView", `第 ${l.line} 行的「${l.text}」和另一個連結重疊,沒有加上。同一行出現兩次的名稱,第二個要給 col。`);
2259
+ return;
2260
+ }
2261
+ list.push(hit);
2262
+ out.set(l.line, list);
2263
+ });
2264
+ for (const list of out.values()) list.sort((a, b) => a.from - b.from);
2265
+ return out;
2266
+ }
2210
2267
  /** Source with line numbers and an optional target line: soft accent background, long lines wrap, light syntax colors. */
2211
- function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile, title, language, languageLabel, highlight = true, tokenizer = tokenize, refHref, onRef, maxHeight, sections, copy, lineNumbers = true, onCopy, onCopyError, className, ref }) {
2268
+ 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, lineNumbers = true, onCopy, onCopyError, className, ref }) {
2212
2269
  const lines = React.useMemo(() => String(code || "").replace(/\n$/, "").split("\n"), [code]);
2213
2270
  const lang = language || languageOf(file);
2214
2271
  const toks = React.useMemo(() => highlight ? tokenizer(lines, lang) : lines.map((l) => [["", l]]), [
@@ -2217,6 +2274,71 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2217
2274
  highlight,
2218
2275
  tokenizer
2219
2276
  ]);
2277
+ const hits = React.useMemo(() => matchLinks(lines, start, links), [
2278
+ lines,
2279
+ start,
2280
+ links
2281
+ ]);
2282
+ const nav = hits.size > 0;
2283
+ const [armed, setArmed] = React.useState(false);
2284
+ const inside = React.useRef(false);
2285
+ React.useEffect(() => {
2286
+ if (!nav) return;
2287
+ const key = (e) => {
2288
+ if (e.key === "Control" || e.key === "Meta") setArmed(e.type === "keydown" && inside.current);
2289
+ };
2290
+ const off = () => setArmed(false);
2291
+ window.addEventListener("keydown", key);
2292
+ window.addEventListener("keyup", key);
2293
+ window.addEventListener("blur", off);
2294
+ return () => {
2295
+ window.removeEventListener("keydown", key);
2296
+ window.removeEventListener("keyup", key);
2297
+ window.removeEventListener("blur", off);
2298
+ };
2299
+ }, [nav]);
2300
+ const [pv, setPv] = React.useState(null);
2301
+ const pvId = React.useId();
2302
+ const showTimer = React.useRef(void 0);
2303
+ const hideTimer = React.useRef(void 0);
2304
+ React.useEffect(() => () => {
2305
+ clearTimeout(showTimer.current);
2306
+ clearTimeout(hideTimer.current);
2307
+ }, []);
2308
+ const pvSoon = (el) => {
2309
+ if (!preview) return;
2310
+ clearTimeout(hideTimer.current);
2311
+ clearTimeout(showTimer.current);
2312
+ if (pv?.el === el) return;
2313
+ showTimer.current = setTimeout(() => setPv({
2314
+ i: Number(el.dataset.link),
2315
+ el
2316
+ }), 500);
2317
+ };
2318
+ const pvLater = () => {
2319
+ clearTimeout(showTimer.current);
2320
+ clearTimeout(hideTimer.current);
2321
+ hideTimer.current = setTimeout(() => setPv(null), 200);
2322
+ };
2323
+ const pvKeep = () => clearTimeout(hideTimer.current);
2324
+ const linkEl = (t) => t instanceof Element ? t.closest(".ui-cv-name") : null;
2325
+ const open = (el, e) => {
2326
+ const link = links?.[Number(el.dataset.link)];
2327
+ if (!link) return;
2328
+ clearTimeout(showTimer.current);
2329
+ setPv(null);
2330
+ if (onLink) onLink(link, e);
2331
+ else if (link.href) window.location.assign(link.href);
2332
+ };
2333
+ const onLinkClick = (e) => {
2334
+ const el = linkEl(e.target);
2335
+ if (!el) return;
2336
+ const href = links?.[Number(el.dataset.link)]?.href;
2337
+ if (e.shiftKey && href) return;
2338
+ e.preventDefault();
2339
+ const type = e.nativeEvent.pointerType;
2340
+ if (e.detail === 0 || type === "touch" || type === "pen" || e.ctrlKey || e.metaKey) open(el, e);
2341
+ };
2220
2342
  const [ta, tb] = target == null ? [null, null] : Array.isArray(target) ? target : [target, target];
2221
2343
  const narrow = useNarrow();
2222
2344
  const [all, setAll] = React.useState(false);
@@ -2245,6 +2367,64 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2245
2367
  if (b && el && b.scrollHeight > b.clientHeight) b.scrollTop += el.getBoundingClientRect().top - b.getBoundingClientRect().top - b.clientHeight / 2 + el.offsetHeight / 2;
2246
2368
  }, [ta, code]);
2247
2369
  const digits = String(start + lines.length - 1).length;
2370
+ const piece = (cls, text, key, inLink) => cls ? /* @__PURE__ */ jsx("span", {
2371
+ className: cls,
2372
+ children: !inLink && (refHref || onRef) && (cls === "is-comment" || cls === "is-string") ? withPaths(text, refHref, onRef, nav ? -1 : void 0) : breakable(text)
2373
+ }, key) : /* @__PURE__ */ jsx(React.Fragment, { children: breakable(text) }, key);
2374
+ const nameLink = (hit, kids) => {
2375
+ const link = links?.[hit.i];
2376
+ if (!link) return null;
2377
+ return /* @__PURE__ */ jsx("a", {
2378
+ className: "ui-cv-name",
2379
+ href: link.href,
2380
+ role: link.href ? void 0 : "link",
2381
+ tabIndex: -1,
2382
+ draggable: false,
2383
+ "data-link": hit.i,
2384
+ "aria-label": link.label,
2385
+ "aria-describedby": pv?.i === hit.i ? pvId : void 0,
2386
+ title: preview ? void 0 : link.label,
2387
+ children: kids
2388
+ }, "n" + hit.i);
2389
+ };
2390
+ const lineContent = (line, lineHits) => {
2391
+ if (!lineHits) return line.map(([cls, text], j) => piece(cls, text, String(j), false));
2392
+ const out = [];
2393
+ let group = [];
2394
+ let open = null;
2395
+ const flush = () => {
2396
+ if (open) out.push(nameLink(open, group));
2397
+ open = null;
2398
+ group = [];
2399
+ };
2400
+ let pos = 0;
2401
+ let h = 0;
2402
+ line.forEach(([cls, text], j) => {
2403
+ let k = 0;
2404
+ while (k < text.length) {
2405
+ const at = pos + k;
2406
+ while (h < lineHits.length && lineHits[h].to <= at) h++;
2407
+ const next = lineHits[h];
2408
+ const hit = next && next.from <= at ? next : null;
2409
+ const end = Math.min(pos + text.length, hit ? hit.to : next ? next.from : Infinity);
2410
+ const key = j + "." + k;
2411
+ if (hit) {
2412
+ if (open !== hit) {
2413
+ flush();
2414
+ open = hit;
2415
+ }
2416
+ group.push(piece(cls, text.slice(k, end - pos), key, true));
2417
+ } else {
2418
+ flush();
2419
+ out.push(piece(cls, text.slice(k, end - pos), key, false));
2420
+ }
2421
+ k = end - pos;
2422
+ }
2423
+ pos += text.length;
2424
+ });
2425
+ flush();
2426
+ return out;
2427
+ };
2248
2428
  const rows = [];
2249
2429
  for (let i = from; i < to; i++) {
2250
2430
  const n = start + i;
@@ -2265,14 +2445,33 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2265
2445
  children: [/* @__PURE__ */ jsx("span", {
2266
2446
  className: "ui-visually-hidden ui-cv-sr",
2267
2447
  children: tr("第 {n} 行", { n }) + indentText(lines[i]) + tr(":")
2268
- }), toks[i].map(([cls, text], j) => cls ? /* @__PURE__ */ jsx("span", {
2269
- className: cls,
2270
- children: (refHref || onRef) && (cls === "is-comment" || cls === "is-string") ? withPaths(text, refHref, onRef) : breakable(text)
2271
- }, j) : /* @__PURE__ */ jsx(React.Fragment, { children: breakable(text) }, j))]
2448
+ }), lineContent(toks[i], hits.get(n))]
2272
2449
  })]
2273
2450
  }, i));
2274
2451
  }
2275
2452
  const keys = [
2453
+ ...nav ? [
2454
+ {
2455
+ k: "ArrowUp ArrowDown",
2456
+ label: "↑↓",
2457
+ what: tr("上一行/下一行")
2458
+ },
2459
+ {
2460
+ k: "ArrowLeft ArrowRight",
2461
+ label: "←→",
2462
+ what: tr("上一個/下一個連結")
2463
+ },
2464
+ {
2465
+ k: "Enter",
2466
+ label: "Enter",
2467
+ what: tr("開啟")
2468
+ },
2469
+ {
2470
+ k: "Escape",
2471
+ label: "Esc",
2472
+ what: tr("回到這一行")
2473
+ }
2474
+ ] : [],
2276
2475
  {
2277
2476
  k: "Home",
2278
2477
  label: "Home",
@@ -2305,11 +2504,55 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2305
2504
  ];
2306
2505
  const onMove = (e) => {
2307
2506
  if (e.altKey || e.ctrlKey || e.metaKey) return;
2308
- const items = [...e.currentTarget.querySelectorAll("[role=\"listitem\"]")];
2507
+ const pre = e.currentTarget;
2508
+ const items = [...pre.querySelectorAll("[role=\"listitem\"]")];
2309
2509
  if (!items.length) return;
2310
2510
  const nOf = (el) => Number(el.dataset.n);
2311
- const at = items.indexOf(document.activeElement instanceof HTMLElement ? document.activeElement : items[0]);
2511
+ const act = document.activeElement instanceof HTMLElement ? document.activeElement : null;
2512
+ const curLine = act && act !== pre ? act.closest("[role=\"listitem\"]") : null;
2513
+ const at = curLine ? items.indexOf(curLine) : -1;
2312
2514
  const now = at < 0 ? -1 : at;
2515
+ const focusLine = (el) => {
2516
+ if (!el) return;
2517
+ el.tabIndex = -1;
2518
+ el.focus();
2519
+ };
2520
+ if (nav) {
2521
+ const stops = [...pre.querySelectorAll(".ui-cv-name, .ui-cv-ref")];
2522
+ const onStop = act ? stops.indexOf(act) : -1;
2523
+ if (e.key === "Escape") {
2524
+ if (pv) setPv(null);
2525
+ else if (onStop >= 0) focusLine(curLine ?? void 0);
2526
+ else if (curLine) pre.focus();
2527
+ else return;
2528
+ e.preventDefault();
2529
+ return;
2530
+ }
2531
+ if (e.key === "Enter" && act && onStop >= 0 && act.classList.contains("ui-cv-name") && !act.hasAttribute("href")) {
2532
+ e.preventDefault();
2533
+ open(act, e);
2534
+ return;
2535
+ }
2536
+ if (e.key === "ArrowUp" || e.key === "ArrowDown") {
2537
+ e.preventDefault();
2538
+ if (now < 0) focusLine(items.find((el) => el.classList.contains("is-target")) ?? items[0]);
2539
+ else focusLine(items[Math.max(0, Math.min(items.length - 1, now + (e.key === "ArrowDown" ? 1 : -1)))]);
2540
+ return;
2541
+ }
2542
+ if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
2543
+ e.preventDefault();
2544
+ const fwd = e.key === "ArrowRight";
2545
+ let to;
2546
+ if (onStop >= 0) to = stops[onStop + (fwd ? 1 : -1)];
2547
+ else {
2548
+ const n = curLine ? nOf(curLine) : ta ?? nOf(items[0]);
2549
+ const lineOf = (el) => Number(el.closest("[role=\"listitem\"]")?.dataset.n);
2550
+ to = fwd ? stops.find((el) => lineOf(el) >= n) : stops.filter((el) => lineOf(el) <= n).pop();
2551
+ }
2552
+ to?.focus();
2553
+ return;
2554
+ }
2555
+ }
2313
2556
  let to = -1;
2314
2557
  let said = "";
2315
2558
  if (e.key === "Home") to = 0;
@@ -2385,17 +2628,62 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2385
2628
  head,
2386
2629
  /* @__PURE__ */ jsx("pre", {
2387
2630
  ref: body,
2388
- className: "ui-cv-body",
2389
2631
  style: maxHeight ? { maxHeight } : void 0,
2390
2632
  tabIndex: 0,
2391
2633
  "aria-label": tr("程式碼"),
2392
2634
  "aria-keyshortcuts": keys.map((k) => k.k).join(" "),
2393
2635
  onKeyDown: onMove,
2636
+ ...nav ? {
2637
+ onClick: onLinkClick,
2638
+ onPointerEnter: () => inside.current = true,
2639
+ onPointerLeave: () => {
2640
+ inside.current = false;
2641
+ setArmed(false);
2642
+ },
2643
+ onPointerMove: (e) => {
2644
+ const held = e.ctrlKey || e.metaKey;
2645
+ if (held !== armed) setArmed(held);
2646
+ },
2647
+ onPointerOver: (e) => {
2648
+ const el = linkEl(e.target);
2649
+ if (el && e.pointerType === "mouse") pvSoon(el);
2650
+ },
2651
+ onPointerOut: (e) => {
2652
+ const el = linkEl(e.target);
2653
+ if (el && !el.contains(e.relatedTarget)) pvLater();
2654
+ },
2655
+ onFocus: (e) => {
2656
+ const el = linkEl(e.target);
2657
+ if (el && el.matches(":focus-visible")) pvSoon(el);
2658
+ },
2659
+ onBlur: (e) => {
2660
+ if (linkEl(e.target)) pvLater();
2661
+ }
2662
+ } : null,
2663
+ className: cx("ui-cv-body", armed && "is-armed"),
2394
2664
  children: /* @__PURE__ */ jsx("code", {
2395
2665
  role: "list",
2396
2666
  children: rows
2397
2667
  })
2398
2668
  }),
2669
+ preview && pv && links?.[pv.i] ? /* @__PURE__ */ jsx(PreviewCard.Root, {
2670
+ open: true,
2671
+ onOpenChange: (o) => o ? void 0 : setPv(null),
2672
+ children: /* @__PURE__ */ jsx(PreviewCard.Portal, { children: /* @__PURE__ */ jsx(PreviewCard.Positioner, {
2673
+ anchor: pv.el,
2674
+ side: "bottom",
2675
+ align: "start",
2676
+ sideOffset: 6,
2677
+ className: "ui-float",
2678
+ children: /* @__PURE__ */ jsx(PreviewCard.Popup, {
2679
+ id: pvId,
2680
+ className: "ui-popover ui-cv-preview",
2681
+ onPointerEnter: pvKeep,
2682
+ onPointerLeave: pvLater,
2683
+ children: preview(links[pv.i])
2684
+ })
2685
+ }) })
2686
+ }) : null,
2399
2687
  /* @__PURE__ */ jsx("div", {
2400
2688
  className: "ui-cv-keys",
2401
2689
  "aria-hidden": "true",
@@ -2859,6 +3147,121 @@ function ScrollHint({ show, done }) {
2859
3147
  }), touch ? tr("左右滑動") : tr("向右捲動")]
2860
3148
  }) : null;
2861
3149
  }
3150
+ /** The right border of a header cell, dragged to set the column's width. ← → 16 px, Enter or a double-click back
3151
+ * to the widths by content. Hidden on touch screens (CSS), where 「放大表格」 shows the whole table instead. */
3152
+ function ColResize(p) {
3153
+ const [on, setOn] = React.useState(false);
3154
+ const from = React.useRef(null);
3155
+ const min = p.min ?? 48;
3156
+ const set = (w) => p.onWidth(Math.max(min, Math.round(w)));
3157
+ const drag = (v) => {
3158
+ setOn(v);
3159
+ p.onDragging?.(v);
3160
+ };
3161
+ return /* @__PURE__ */ jsx("span", {
3162
+ className: cx("ui-col-resize", on && "is-dragging"),
3163
+ role: "separator",
3164
+ "aria-orientation": "vertical",
3165
+ "aria-label": tr("調整「{name}」欄寬", { name: p.name }),
3166
+ "aria-valuenow": Math.round(p.width),
3167
+ "aria-valuemin": min,
3168
+ tabIndex: 0,
3169
+ onClick: (e) => e.stopPropagation(),
3170
+ onPointerDown: (e) => {
3171
+ if (e.button !== 0) return;
3172
+ e.preventDefault();
3173
+ e.stopPropagation();
3174
+ e.currentTarget.setPointerCapture(e.pointerId);
3175
+ from.current = {
3176
+ x: e.clientX,
3177
+ w: p.start()
3178
+ };
3179
+ drag(true);
3180
+ },
3181
+ onPointerMove: (e) => {
3182
+ if (from.current) set(from.current.w + e.clientX - from.current.x);
3183
+ },
3184
+ onPointerUp: () => {
3185
+ from.current = null;
3186
+ drag(false);
3187
+ },
3188
+ onPointerCancel: () => {
3189
+ from.current = null;
3190
+ drag(false);
3191
+ },
3192
+ onDoubleClick: (e) => {
3193
+ e.stopPropagation();
3194
+ p.onReset();
3195
+ },
3196
+ onKeyDown: (e) => {
3197
+ if (e.altKey || e.metaKey || e.ctrlKey) return;
3198
+ if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
3199
+ e.preventDefault();
3200
+ e.stopPropagation();
3201
+ set(p.start() + (e.key === "ArrowRight" ? 16 : -16));
3202
+ } else if (e.key === "Enter") {
3203
+ e.preventDefault();
3204
+ e.stopPropagation();
3205
+ p.onReset();
3206
+ }
3207
+ }
3208
+ });
3209
+ }
3210
+ /** The 「放大表格」 icon: 32 px, no border, at the table's top right. */
3211
+ function ZoomButton({ onClick }) {
3212
+ return /* @__PURE__ */ jsx(IconButton, {
3213
+ className: "ui-table-zoom",
3214
+ icon: "corners-out",
3215
+ label: tr("放大表格"),
3216
+ onClick
3217
+ });
3218
+ }
3219
+ /** The whole table in a wide dialog (at most 1200 px). A plain click on a link inside (a function, a path) closes the
3220
+ * dialog after the link has done its job; Ctrl, ⌘, Shift or the middle button open a new tab and keep it open. */
3221
+ function TableDialog(p) {
3222
+ if (!p.open) return null;
3223
+ return /* @__PURE__ */ jsx(Dialog, {
3224
+ open: true,
3225
+ onClose: p.onClose,
3226
+ title: p.title || tr("表格"),
3227
+ className: "ui-table-dialog",
3228
+ children: /* @__PURE__ */ jsx("div", {
3229
+ className: "ui-table-dialog-body",
3230
+ onClick: (e) => {
3231
+ if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return;
3232
+ const link = e.target.closest("a[href], [role=\"link\"], .ui-code.is-link, .ui-md-link");
3233
+ if (link && e.currentTarget.contains(link)) p.onClose();
3234
+ },
3235
+ children: p.children
3236
+ })
3237
+ });
3238
+ }
3239
+ /** The 「複製成 Markdown」 icon: copies the text, turns into a check for 2 s and says 「已複製」, as CodeView's copy. */
3240
+ function CopyMarkdownButton(p) {
3241
+ const [done, setDone] = React.useState(false);
3242
+ const timer = React.useRef(void 0);
3243
+ React.useEffect(() => () => clearTimeout(timer.current), []);
3244
+ return /* @__PURE__ */ jsx(IconButton, {
3245
+ className: cx("ui-table-copy", done && "is-copied", p.className),
3246
+ icon: done ? CHECK : "copy",
3247
+ label: done ? tr("已複製") : tr("複製成 Markdown"),
3248
+ onClick: () => copyText(p.text()).then(() => {
3249
+ setDone(true);
3250
+ announce(tr("已複製"));
3251
+ clearTimeout(timer.current);
3252
+ timer.current = setTimeout(() => setDone(false), 2e3);
3253
+ }, () => p.onCopyError?.())
3254
+ });
3255
+ }
3256
+ /** A table as GitHub Markdown: | in a cell is escaped, line breaks become spaces. */
3257
+ function markdownTable(heads, rows) {
3258
+ const cell = (s) => s.replace(/\r?\n/g, " ").replace(/\|/g, "\\|").trim();
3259
+ return [
3260
+ `| ${heads.map(cell).join(" | ")} |`,
3261
+ `|${heads.map(() => "---").join("|")}|`,
3262
+ ...rows.map((r) => `| ${r.map(cell).join(" | ")} |`)
3263
+ ].join("\n");
3264
+ }
2862
3265
  // src/components/DataTable/filters.ts
2863
3266
  const field = (row, key) => row[key];
2864
3267
  /** Cell text: money as NT$ 1,280, dates as 2026/11/04(三), numbers with their unit after them. */
@@ -3302,6 +3705,26 @@ function DataTable(p) {
3302
3705
  const [sel, setSel] = React.useState({});
3303
3706
  const [sumVal, setSumVal] = React.useState("all");
3304
3707
  const [fit, setFit] = React.useState({});
3708
+ const [widths, setWidthsState] = React.useState(() => {
3709
+ if (!p.widthKey) return null;
3710
+ try {
3711
+ const v = JSON.parse(localStorage.getItem("ui-dt-width:" + p.widthKey) || "null");
3712
+ return v && typeof v === "object" ? v : null;
3713
+ } catch {
3714
+ return null;
3715
+ }
3716
+ });
3717
+ const setWidths = (w) => {
3718
+ setWidthsState(w);
3719
+ if (!p.widthKey) return;
3720
+ try {
3721
+ if (w) localStorage.setItem("ui-dt-width:" + p.widthKey, JSON.stringify(w));
3722
+ else localStorage.removeItem("ui-dt-width:" + p.widthKey);
3723
+ } catch {}
3724
+ };
3725
+ const [zoomed, setZoomed] = React.useState(false);
3726
+ const [zoomWidths, setZoomWidths] = React.useState(null);
3727
+ const zoomRef = React.useRef(null);
3305
3728
  const [resetKey, setResetKey] = React.useState(p.resetKey);
3306
3729
  const resetting = resetKey !== p.resetKey;
3307
3730
  if (resetting) {
@@ -3514,16 +3937,31 @@ function DataTable(p) {
3514
3937
  const expander = longCols.length > 0 && !narrow;
3515
3938
  const EXP = 40;
3516
3939
  const minOf = (c) => Math.max(c.width || (c.numeric ? 96 : 120), fit[c.key] || 0);
3517
- const template = [
3518
- expander ? "40px" : null,
3519
- selectable ? "46px" : null,
3520
- ...shownCols.map((c) => `minmax(${minOf(c)}px, ${c.grow || (c.width ? 1 : c.numeric ? .8 : 1)}fr)`),
3521
- hasAct ? actW + "px" : null
3522
- ].filter(Boolean).join(" ");
3523
- const minW = (expander ? EXP : 0) + (selectable ? 46 : 0) + shownCols.reduce((a, c) => a + minOf(c), 0) + (hasAct ? actW : 0);
3524
- const gridStyle = {
3525
- gridTemplateColumns: template,
3526
- minWidth: minW
3940
+ const layout = (w) => {
3941
+ const colW = (c) => w ? `${w[c.key] ?? minOf(c)}px` : `minmax(${minOf(c)}px, ${c.grow || (c.width ? 1 : c.numeric ? .8 : 1)}fr)`;
3942
+ return {
3943
+ gridTemplateColumns: [
3944
+ expander ? "40px" : null,
3945
+ selectable ? "46px" : null,
3946
+ ...shownCols.map(colW),
3947
+ hasAct ? actW + "px" : null
3948
+ ].filter(Boolean).join(" "),
3949
+ minWidth: (expander ? EXP : 0) + (selectable ? 46 : 0) + shownCols.reduce((a, c) => a + (w ? w[c.key] ?? minOf(c) : minOf(c)), 0) + (hasAct ? actW : 0)
3950
+ };
3951
+ };
3952
+ const pageGrid = layout(widths);
3953
+ const zoomGrid = layout(zoomWidths);
3954
+ const drawn = (root) => {
3955
+ const out = {};
3956
+ for (const c of shownCols) {
3957
+ const el = root?.querySelector(`.ui-dt-head [data-col="${CSS.escape(c.key)}"]`);
3958
+ out[c.key] = el ? Math.ceil(el.getBoundingClientRect().width) : minOf(c);
3959
+ }
3960
+ return out;
3961
+ };
3962
+ const headMin = (root, key) => {
3963
+ const el = root?.querySelector(`.ui-dt-head [data-col="${CSS.escape(key)}"] > :first-child`);
3964
+ return Math.max(48, el ? Math.ceil(el.getBoundingClientRect().width) + 24 : 48);
3527
3965
  };
3528
3966
  React.useLayoutEffect(() => {
3529
3967
  const root = rootRef.current;
@@ -3563,84 +4001,101 @@ function DataTable(p) {
3563
4001
  }, [narrow, resetKey]);
3564
4002
  const stickyStyle = (ci) => ci === 0 && sticky ? { left: (expander ? EXP : 0) + (selectable ? 46 : 0) } : void 0;
3565
4003
  const cellClass = (c, ci, ...more) => cx("ui-dt-cell", isNum(c) && "is-num", ci === 0 && sticky && "is-sticky", ...more);
3566
- const head = selectable && selIds.length && p.bulkActions ? /* @__PURE__ */ jsxs("div", {
3567
- className: "ui-dt-row ui-dt-head ui-dt-bulk",
3568
- role: "row",
3569
- style: { minWidth: minW },
3570
- children: [
3571
- /* @__PURE__ */ jsx("div", {
3572
- className: "ui-dt-cell",
3573
- role: "columnheader",
3574
- children: /* @__PURE__ */ jsx(RowCheck, {
3575
- v: allOn ? true : "mixed",
3576
- label: tr("全選"),
3577
- onClick: () => toggle(visIds, !allOn)
3578
- })
3579
- }),
3580
- /* @__PURE__ */ jsx("span", {
3581
- className: "ui-dt-bulk-count",
3582
- children: tr("已選 {n} 筆", { n: selIds.length })
3583
- }),
3584
- /* @__PURE__ */ jsx("span", {
3585
- className: "ui-dt-bulk-actions",
3586
- children: p.bulkActions(selIds, clear)
3587
- }),
3588
- /* @__PURE__ */ jsx("button", {
3589
- type: "button",
3590
- className: "ui-dt-bulk-close",
3591
- "aria-label": tr("取消選取"),
3592
- onClick: clear,
3593
- children: /* @__PURE__ */ jsx(Icon, {
3594
- name: "x",
3595
- size: 14
3596
- })
3597
- })
3598
- ]
3599
- }) : /* @__PURE__ */ jsxs("div", {
3600
- className: "ui-dt-row ui-dt-head",
3601
- role: "row",
3602
- style: gridStyle,
3603
- children: [
3604
- expander ? /* @__PURE__ */ jsx("div", {
3605
- className: "ui-dt-cell ui-dt-exp",
3606
- role: "columnheader",
3607
- "aria-label": tr("詳細")
3608
- }) : null,
3609
- selectable ? /* @__PURE__ */ jsx("div", {
3610
- className: "ui-dt-cell ui-dt-sel",
3611
- role: "columnheader",
3612
- children: /* @__PURE__ */ jsx(RowCheck, {
3613
- v: allOn ? true : mixed ? "mixed" : false,
3614
- label: tr("全選"),
3615
- onClick: () => toggle(visIds, !allOn)
4004
+ const headOf = (inZoom) => {
4005
+ const gridStyle = inZoom ? zoomGrid : pageGrid;
4006
+ const minW = gridStyle.minWidth;
4007
+ const w = inZoom ? zoomWidths : widths;
4008
+ const setW = inZoom ? setZoomWidths : setWidths;
4009
+ const root = () => inZoom ? zoomRef.current : rootRef.current;
4010
+ return selectable && selIds.length && p.bulkActions ? /* @__PURE__ */ jsxs("div", {
4011
+ className: "ui-dt-row ui-dt-head ui-dt-bulk",
4012
+ role: "row",
4013
+ style: { minWidth: minW },
4014
+ children: [
4015
+ /* @__PURE__ */ jsx("div", {
4016
+ className: "ui-dt-cell",
4017
+ role: "columnheader",
4018
+ children: /* @__PURE__ */ jsx(RowCheck, {
4019
+ v: allOn ? true : "mixed",
4020
+ label: tr("全選"),
4021
+ onClick: () => toggle(visIds, !allOn)
4022
+ })
4023
+ }),
4024
+ /* @__PURE__ */ jsx("span", {
4025
+ className: "ui-dt-bulk-count",
4026
+ children: tr("已選 {n} 筆", { n: selIds.length })
4027
+ }),
4028
+ /* @__PURE__ */ jsx("span", {
4029
+ className: "ui-dt-bulk-actions",
4030
+ children: p.bulkActions(selIds, clear)
4031
+ }),
4032
+ /* @__PURE__ */ jsx("button", {
4033
+ type: "button",
4034
+ className: "ui-dt-bulk-close",
4035
+ "aria-label": tr("取消選取"),
4036
+ onClick: clear,
4037
+ children: /* @__PURE__ */ jsx(Icon, {
4038
+ name: "x",
4039
+ size: 14
4040
+ })
3616
4041
  })
3617
- }) : null,
3618
- shownCols.map((c, ci) => {
3619
- const cur = sort.key === c.key;
3620
- return /* @__PURE__ */ jsx("div", {
4042
+ ]
4043
+ }) : /* @__PURE__ */ jsxs("div", {
4044
+ className: "ui-dt-row ui-dt-head",
4045
+ role: "row",
4046
+ style: gridStyle,
4047
+ children: [
4048
+ expander ? /* @__PURE__ */ jsx("div", {
4049
+ className: "ui-dt-cell ui-dt-exp",
3621
4050
  role: "columnheader",
3622
- "aria-sort": cur ? sort.dir === "asc" ? "ascending" : "descending" : "none",
3623
- className: cellClass(c, ci),
3624
- style: stickyStyle(ci),
3625
- children: c.sortable === false ? c.label : /* @__PURE__ */ jsxs("button", {
3626
- type: "button",
3627
- className: cx("ui-dt-sort", cur && "is-on"),
3628
- onClick: () => sortBy(c),
3629
- children: [/* @__PURE__ */ jsx("span", { children: c.label }), /* @__PURE__ */ jsx(Icon, {
3630
- d: ARROWS[cur ? sort.dir : "none"],
3631
- size: 14,
3632
- className: "ui-dt-arrow"
3633
- })]
4051
+ "aria-label": tr("詳細")
4052
+ }) : null,
4053
+ selectable ? /* @__PURE__ */ jsx("div", {
4054
+ className: "ui-dt-cell ui-dt-sel",
4055
+ role: "columnheader",
4056
+ children: /* @__PURE__ */ jsx(RowCheck, {
4057
+ v: allOn ? true : mixed ? "mixed" : false,
4058
+ label: tr("全選"),
4059
+ onClick: () => toggle(visIds, !allOn)
3634
4060
  })
3635
- }, c.key);
3636
- }),
3637
- hasAct ? /* @__PURE__ */ jsx("div", {
3638
- className: "ui-dt-cell",
3639
- role: "columnheader",
3640
- "aria-label": tr("操作")
3641
- }) : null
3642
- ]
3643
- });
4061
+ }) : null,
4062
+ shownCols.map((c, ci) => {
4063
+ const cur = sort.key === c.key;
4064
+ return /* @__PURE__ */ jsxs("div", {
4065
+ role: "columnheader",
4066
+ "aria-sort": cur ? sort.dir === "asc" ? "ascending" : "descending" : "none",
4067
+ className: cellClass(c, ci),
4068
+ style: stickyStyle(ci),
4069
+ "data-col": c.key,
4070
+ children: [c.sortable === false ? c.label : /* @__PURE__ */ jsxs("button", {
4071
+ type: "button",
4072
+ className: cx("ui-dt-sort", cur && "is-on"),
4073
+ onClick: () => sortBy(c),
4074
+ children: [/* @__PURE__ */ jsx("span", { children: c.label }), /* @__PURE__ */ jsx(Icon, {
4075
+ d: ARROWS[cur ? sort.dir : "none"],
4076
+ size: 14,
4077
+ className: "ui-dt-arrow"
4078
+ })]
4079
+ }), narrow ? null : /* @__PURE__ */ jsx(ColResize, {
4080
+ name: c.label,
4081
+ width: w?.[c.key] ?? minOf(c),
4082
+ start: () => (w ?? drawn(root()))[c.key] ?? minOf(c),
4083
+ onWidth: (px) => setW({
4084
+ ...w ?? drawn(root()),
4085
+ [c.key]: Math.max(headMin(root(), c.key), px)
4086
+ }),
4087
+ onReset: () => setW(null)
4088
+ })]
4089
+ }, c.key);
4090
+ }),
4091
+ hasAct ? /* @__PURE__ */ jsx("div", {
4092
+ className: "ui-dt-cell",
4093
+ role: "columnheader",
4094
+ "aria-label": tr("操作")
4095
+ }) : null
4096
+ ]
4097
+ });
4098
+ };
3644
4099
  const mark = searching ? q : "";
3645
4100
  const cellInner = (r, c) => {
3646
4101
  const raw = get(r, c.key);
@@ -3682,6 +4137,259 @@ function DataTable(p) {
3682
4137
  }
3683
4138
  return "";
3684
4139
  };
4140
+ const copyMarkdown = () => {
4141
+ const all = list.map((x) => x.r);
4142
+ if (p.copyText) return p.copyText(all);
4143
+ return markdownTable(shownCols.map((c) => c.label), all.map((r) => shownCols.map((c) => String(c.fullText?.(r) ?? c.searchText?.(r) ?? cellText(c, get(r, c.key)) ?? ""))));
4144
+ };
4145
+ const toolsView = (inZoom) => /* @__PURE__ */ jsxs("div", {
4146
+ className: "ui-dt-tools",
4147
+ children: [
4148
+ search !== false ? /* @__PURE__ */ jsx(SearchField, {
4149
+ value: q,
4150
+ onChange: (v) => {
4151
+ setQ(v);
4152
+ setPage(0);
4153
+ },
4154
+ placeholder: search.placeholder || tr("搜尋"),
4155
+ label: search.placeholder || tr("搜尋"),
4156
+ className: "ui-dt-search"
4157
+ }) : null,
4158
+ filters.length ? /* @__PURE__ */ jsx(FilterButton, {
4159
+ boundary: inZoom ? zoomRef : rootRef,
4160
+ filters,
4161
+ values,
4162
+ activeCount: activeN,
4163
+ count: (f, v) => all.filter((x) => passes(x.r, f.key) && matches(f, x.r, f.type === "list" ? [v] : void 0)).length,
4164
+ onChange: (key, v) => {
4165
+ setValues({
4166
+ ...values,
4167
+ [key]: v
4168
+ });
4169
+ setPage(0);
4170
+ },
4171
+ onClear: () => {
4172
+ setValues({});
4173
+ setPage(0);
4174
+ }
4175
+ }) : null,
4176
+ /* @__PURE__ */ jsxs("span", {
4177
+ className: "ui-dt-result",
4178
+ children: [/* @__PURE__ */ jsx("span", {
4179
+ "aria-hidden": "true",
4180
+ children: seenOnly(tr("{n} / {total} 筆", {
4181
+ n: "",
4182
+ total: ""
4183
+ }), [format.number(list.length), format.number(baseCount)])
4184
+ }), /* @__PURE__ */ jsx("span", {
4185
+ className: "ui-visually-hidden",
4186
+ children: format.fractionLabel(list.length, baseCount, tr("筆"))
4187
+ })]
4188
+ }),
4189
+ p.copy !== false ? /* @__PURE__ */ jsx(CopyMarkdownButton, {
4190
+ text: copyMarkdown,
4191
+ onCopyError: p.onCopyError
4192
+ }) : null,
4193
+ p.columnMenu !== false && cols.length > 2 ? /* @__PURE__ */ jsx(ColumnMenu, {
4194
+ cols,
4195
+ hidden,
4196
+ onToggle: (k) => setToggled({
4197
+ ...toggled,
4198
+ [k]: !hidden[k]
4199
+ })
4200
+ }) : null
4201
+ ]
4202
+ });
4203
+ const tableView = (inZoom) => {
4204
+ const grid = inZoom ? zoomGrid : pageGrid;
4205
+ const pfx = inZoom ? uid + "z" : uid;
4206
+ return /* @__PURE__ */ jsx("div", {
4207
+ className: cx("ui-dt-scroller", scrollMore && !inZoom && "has-more"),
4208
+ children: /* @__PURE__ */ jsx("div", {
4209
+ className: "ui-dt-scroll",
4210
+ ref: inZoom ? void 0 : scrollRef,
4211
+ onScroll: inZoom ? void 0 : onScroll,
4212
+ children: /* @__PURE__ */ jsxs("div", {
4213
+ className: "ui-dt-table",
4214
+ role: "table",
4215
+ "aria-label": p.label || p.caption,
4216
+ children: [
4217
+ headOf(inZoom),
4218
+ /* @__PURE__ */ jsx("div", {
4219
+ ref: inZoom ? void 0 : bodyRef,
4220
+ className: "ui-dt-body",
4221
+ role: "rowgroup",
4222
+ children: vis.length ? vis.map((x, i) => {
4223
+ const id = visIds[i];
4224
+ const on = !!sel[id];
4225
+ const open = expander && !!opened[id];
4226
+ const detailId = `${pfx}-d-${idPart(id)}`;
4227
+ const rowName = shownCols[0] ? String(cellText(shownCols[0], get(x.r, shownCols[0].key)) ?? "") : "";
4228
+ return /* @__PURE__ */ jsxs(React.Fragment, { children: [/* @__PURE__ */ jsxs("div", {
4229
+ "data-rid": id,
4230
+ ref: inZoom ? void 0 : (el) => {
4231
+ rowEls.current[id] = el;
4232
+ },
4233
+ role: "row",
4234
+ "aria-labelledby": shownCols.length ? `${pfx}-n-${start + i}` : void 0,
4235
+ className: cx("ui-dt-row", i % 2 && "is-even", on && "is-selected", selectable && "has-sel", expander && "has-exp"),
4236
+ style: grid,
4237
+ onClick: expander ? (e) => {
4238
+ const t = e.target;
4239
+ if (t.closest(ROW_CONTROLS) && t.closest(ROW_CONTROLS) !== e.currentTarget) return;
4240
+ if (String(window.getSelection() ?? "")) return;
4241
+ setOpened({
4242
+ ...opened,
4243
+ [id]: !open
4244
+ });
4245
+ } : void 0,
4246
+ children: [
4247
+ expander ? /* @__PURE__ */ jsx("div", {
4248
+ className: "ui-dt-cell ui-dt-exp",
4249
+ role: "cell",
4250
+ children: /* @__PURE__ */ jsx("button", {
4251
+ type: "button",
4252
+ className: cx("ui-dt-exp-btn", open && "is-open"),
4253
+ "aria-expanded": open,
4254
+ "aria-controls": open ? detailId : void 0,
4255
+ "aria-label": tr(open ? "收合 {name}" : "展開 {name}", { name: rowName }),
4256
+ onClick: () => setOpened({
4257
+ ...opened,
4258
+ [id]: !open
4259
+ }),
4260
+ children: /* @__PURE__ */ jsx(Icon, {
4261
+ name: "caret-right",
4262
+ size: 16
4263
+ })
4264
+ })
4265
+ }) : null,
4266
+ selectable ? /* @__PURE__ */ jsx("div", {
4267
+ className: "ui-dt-cell ui-dt-sel",
4268
+ role: "cell",
4269
+ children: /* @__PURE__ */ jsx(RowCheck, {
4270
+ v: on,
4271
+ label: tr("選取第 {n} 筆", { n: start + i + 1 }),
4272
+ onClick: () => toggle([id], !on)
4273
+ })
4274
+ }) : null,
4275
+ shownCols.map((c, ci) => /* @__PURE__ */ jsx("div", {
4276
+ role: "cell",
4277
+ id: ci === 0 ? `${pfx}-n-${start + i}` : void 0,
4278
+ "aria-description": c.fullText ? c.fullText(x.r) || void 0 : void 0,
4279
+ "data-label": c.label,
4280
+ "data-col": c.key,
4281
+ className: cellClass(c, ci, c.long && "is-long", mobileClass(c, ci)),
4282
+ style: stickyStyle(ci),
4283
+ children: cell(x.r, c)
4284
+ }, c.key)),
4285
+ hasAct ? /* @__PURE__ */ jsx("div", {
4286
+ className: "ui-dt-cell ui-dt-acts",
4287
+ role: "cell",
4288
+ children: p.rowActions?.(x.r)
4289
+ }) : null
4290
+ ]
4291
+ }), open ? /* @__PURE__ */ jsx("div", {
4292
+ id: detailId,
4293
+ role: "row",
4294
+ className: cx("ui-dt-detail", i % 2 && "is-even"),
4295
+ style: { minWidth: grid.minWidth },
4296
+ children: /* @__PURE__ */ jsx("div", {
4297
+ role: "cell",
4298
+ className: "ui-dt-detail-cell",
4299
+ style: { paddingLeft: EXP + (selectable ? 46 : 0) + 12 },
4300
+ children: longCols.map((c) => /* @__PURE__ */ jsxs("div", {
4301
+ className: "ui-dt-detail-item",
4302
+ children: [/* @__PURE__ */ jsx("div", {
4303
+ className: "ui-dt-detail-label",
4304
+ children: c.label
4305
+ }), /* @__PURE__ */ jsx("div", {
4306
+ className: "ui-dt-detail-text",
4307
+ children: c.fullText ? c.fullText(x.r) : cellInner(x.r, c)
4308
+ })]
4309
+ }, c.key))
4310
+ })
4311
+ }) : null] }, id);
4312
+ }) : /* @__PURE__ */ jsxs("div", {
4313
+ className: "ui-dt-empty",
4314
+ children: [p.emptyText || tr("沒有符合的資料"), q || activeN ? /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Button, {
4315
+ onClick: clearFilters,
4316
+ children: tr("清除篩選")
4317
+ }) }) : null]
4318
+ })
4319
+ }),
4320
+ p.totals && list.length ? /* @__PURE__ */ jsxs("div", {
4321
+ className: "ui-dt-row ui-dt-total",
4322
+ role: "row",
4323
+ style: grid,
4324
+ children: [
4325
+ expander ? /* @__PURE__ */ jsx("div", {
4326
+ className: "ui-dt-cell",
4327
+ role: "cell"
4328
+ }) : null,
4329
+ selectable ? /* @__PURE__ */ jsx("div", {
4330
+ className: "ui-dt-cell",
4331
+ role: "cell"
4332
+ }) : null,
4333
+ shownCols.map((c, ci) => /* @__PURE__ */ jsx("div", {
4334
+ role: "cell",
4335
+ className: cellClass(c, ci),
4336
+ style: stickyStyle(ci),
4337
+ children: total(c, ci)
4338
+ }, c.key)),
4339
+ hasAct ? /* @__PURE__ */ jsx("div", {
4340
+ className: "ui-dt-cell",
4341
+ role: "cell"
4342
+ }) : null
4343
+ ]
4344
+ }) : null
4345
+ ]
4346
+ })
4347
+ })
4348
+ });
4349
+ };
4350
+ const footView = /* @__PURE__ */ jsx(Fragment, { children: pages > 1 ? /* @__PURE__ */ jsxs("div", {
4351
+ className: "ui-dt-foot",
4352
+ children: [/* @__PURE__ */ jsx("span", {
4353
+ className: "ui-dt-range",
4354
+ children: monoNums(tr("第 {a}–{b} 筆,共 {n} 筆", {
4355
+ a: "",
4356
+ b: "",
4357
+ n: ""
4358
+ }), [
4359
+ start + 1,
4360
+ Math.min(list.length, start + pageSize),
4361
+ format.number(list.length)
4362
+ ])
4363
+ }), /* @__PURE__ */ jsxs("nav", {
4364
+ className: "ui-dt-pages",
4365
+ "aria-label": tr("分頁"),
4366
+ children: [
4367
+ /* @__PURE__ */ jsx("button", {
4368
+ type: "button",
4369
+ className: "ui-dt-page",
4370
+ "aria-label": tr("上一頁"),
4371
+ disabled: pageNow === 0,
4372
+ onClick: () => goPage(pageNow - 1),
4373
+ children: "‹"
4374
+ }),
4375
+ Array.from({ length: pages }, (_, i) => /* @__PURE__ */ jsx("button", {
4376
+ type: "button",
4377
+ className: cx("ui-dt-page", i === pageNow && "is-on"),
4378
+ "aria-current": i === pageNow ? "page" : void 0,
4379
+ onClick: () => goPage(i),
4380
+ children: String(i + 1)
4381
+ }, i)),
4382
+ /* @__PURE__ */ jsx("button", {
4383
+ type: "button",
4384
+ className: "ui-dt-page",
4385
+ "aria-label": tr("下一頁"),
4386
+ disabled: pageNow >= pages - 1,
4387
+ onClick: () => goPage(pageNow + 1),
4388
+ children: "›"
4389
+ })
4390
+ ]
4391
+ })]
4392
+ }) : null });
3685
4393
  return /* @__PURE__ */ jsxs("div", {
3686
4394
  ref: rootRef,
3687
4395
  className: cx("ui-dt", selIds.length && "has-sel", p.className),
@@ -3740,250 +4448,31 @@ function DataTable(p) {
3740
4448
  count: sumAll.filter((x) => String(get(x.r, summary.key)) === String(it.value)).length
3741
4449
  }))]
3742
4450
  }) : null,
3743
- /* @__PURE__ */ jsxs("div", {
3744
- className: "ui-dt-tools",
3745
- children: [
3746
- search !== false ? /* @__PURE__ */ jsx(SearchField, {
3747
- value: q,
3748
- onChange: (v) => {
3749
- setQ(v);
3750
- setPage(0);
3751
- },
3752
- placeholder: search.placeholder || tr("搜尋"),
3753
- label: search.placeholder || tr("搜尋"),
3754
- className: "ui-dt-search"
3755
- }) : null,
3756
- filters.length ? /* @__PURE__ */ jsx(FilterButton, {
3757
- boundary: rootRef,
3758
- filters,
3759
- values,
3760
- activeCount: activeN,
3761
- count: (f, v) => all.filter((x) => passes(x.r, f.key) && matches(f, x.r, f.type === "list" ? [v] : void 0)).length,
3762
- onChange: (key, v) => {
3763
- setValues({
3764
- ...values,
3765
- [key]: v
3766
- });
3767
- setPage(0);
3768
- },
3769
- onClear: () => {
3770
- setValues({});
3771
- setPage(0);
3772
- }
3773
- }) : null,
3774
- /* @__PURE__ */ jsxs("span", {
3775
- className: "ui-dt-result",
3776
- children: [/* @__PURE__ */ jsx("span", {
3777
- "aria-hidden": "true",
3778
- children: seenOnly(tr("{n} / {total} 筆", {
3779
- n: "",
3780
- total: ""
3781
- }), [format.number(list.length), format.number(baseCount)])
3782
- }), /* @__PURE__ */ jsx("span", {
3783
- className: "ui-visually-hidden",
3784
- children: format.fractionLabel(list.length, baseCount, tr("筆"))
3785
- })]
3786
- }),
3787
- p.columnMenu !== false && cols.length > 2 ? /* @__PURE__ */ jsx(ColumnMenu, {
3788
- cols,
3789
- hidden,
3790
- onToggle: (k) => setToggled({
3791
- ...toggled,
3792
- [k]: !hidden[k]
3793
- })
3794
- }) : null
3795
- ]
3796
- }),
3797
- /* @__PURE__ */ jsx("div", {
3798
- className: cx("ui-dt-scroller", scrollMore && "has-more"),
3799
- children: /* @__PURE__ */ jsx("div", {
3800
- className: "ui-dt-scroll",
3801
- ref: scrollRef,
3802
- onScroll,
3803
- children: /* @__PURE__ */ jsxs("div", {
3804
- className: "ui-dt-table",
3805
- role: "table",
3806
- "aria-label": p.label || p.caption,
3807
- children: [
3808
- head,
3809
- /* @__PURE__ */ jsx("div", {
3810
- ref: bodyRef,
3811
- className: "ui-dt-body",
3812
- role: "rowgroup",
3813
- children: vis.length ? vis.map((x, i) => {
3814
- const id = visIds[i];
3815
- const on = !!sel[id];
3816
- const open = expander && !!opened[id];
3817
- const detailId = `${uid}-d-${idPart(id)}`;
3818
- const rowName = shownCols[0] ? String(cellText(shownCols[0], get(x.r, shownCols[0].key)) ?? "") : "";
3819
- return /* @__PURE__ */ jsxs(React.Fragment, { children: [/* @__PURE__ */ jsxs("div", {
3820
- "data-rid": id,
3821
- ref: (el) => {
3822
- rowEls.current[id] = el;
3823
- },
3824
- role: "row",
3825
- "aria-labelledby": shownCols.length ? `${uid}-n-${start + i}` : void 0,
3826
- className: cx("ui-dt-row", i % 2 && "is-even", on && "is-selected", selectable && "has-sel", expander && "has-exp"),
3827
- style: gridStyle,
3828
- onClick: expander ? (e) => {
3829
- const t = e.target;
3830
- if (t.closest(ROW_CONTROLS) && t.closest(ROW_CONTROLS) !== e.currentTarget) return;
3831
- if (String(window.getSelection() ?? "")) return;
3832
- setOpened({
3833
- ...opened,
3834
- [id]: !open
3835
- });
3836
- } : void 0,
3837
- children: [
3838
- expander ? /* @__PURE__ */ jsx("div", {
3839
- className: "ui-dt-cell ui-dt-exp",
3840
- role: "cell",
3841
- children: /* @__PURE__ */ jsx("button", {
3842
- type: "button",
3843
- className: cx("ui-dt-exp-btn", open && "is-open"),
3844
- "aria-expanded": open,
3845
- "aria-controls": open ? detailId : void 0,
3846
- "aria-label": tr(open ? "收合 {name}" : "展開 {name}", { name: rowName }),
3847
- onClick: () => setOpened({
3848
- ...opened,
3849
- [id]: !open
3850
- }),
3851
- children: /* @__PURE__ */ jsx(Icon, {
3852
- name: "caret-right",
3853
- size: 16
3854
- })
3855
- })
3856
- }) : null,
3857
- selectable ? /* @__PURE__ */ jsx("div", {
3858
- className: "ui-dt-cell ui-dt-sel",
3859
- role: "cell",
3860
- children: /* @__PURE__ */ jsx(RowCheck, {
3861
- v: on,
3862
- label: tr("選取第 {n} 筆", { n: start + i + 1 }),
3863
- onClick: () => toggle([id], !on)
3864
- })
3865
- }) : null,
3866
- shownCols.map((c, ci) => /* @__PURE__ */ jsx("div", {
3867
- role: "cell",
3868
- id: ci === 0 ? `${uid}-n-${start + i}` : void 0,
3869
- "aria-description": c.fullText ? c.fullText(x.r) || void 0 : void 0,
3870
- "data-label": c.label,
3871
- "data-col": c.key,
3872
- className: cellClass(c, ci, c.long && "is-long", mobileClass(c, ci)),
3873
- style: stickyStyle(ci),
3874
- children: cell(x.r, c)
3875
- }, c.key)),
3876
- hasAct ? /* @__PURE__ */ jsx("div", {
3877
- className: "ui-dt-cell ui-dt-acts",
3878
- role: "cell",
3879
- children: p.rowActions?.(x.r)
3880
- }) : null
3881
- ]
3882
- }), open ? /* @__PURE__ */ jsx("div", {
3883
- id: detailId,
3884
- role: "row",
3885
- className: cx("ui-dt-detail", i % 2 && "is-even"),
3886
- style: { minWidth: minW },
3887
- children: /* @__PURE__ */ jsx("div", {
3888
- role: "cell",
3889
- className: "ui-dt-detail-cell",
3890
- style: { paddingLeft: EXP + (selectable ? 46 : 0) + 12 },
3891
- children: longCols.map((c) => /* @__PURE__ */ jsxs("div", {
3892
- className: "ui-dt-detail-item",
3893
- children: [/* @__PURE__ */ jsx("div", {
3894
- className: "ui-dt-detail-label",
3895
- children: c.label
3896
- }), /* @__PURE__ */ jsx("div", {
3897
- className: "ui-dt-detail-text",
3898
- children: c.fullText ? c.fullText(x.r) : cellInner(x.r, c)
3899
- })]
3900
- }, c.key))
3901
- })
3902
- }) : null] }, id);
3903
- }) : /* @__PURE__ */ jsxs("div", {
3904
- className: "ui-dt-empty",
3905
- children: [p.emptyText || tr("沒有符合的資料"), q || activeN ? /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Button, {
3906
- onClick: clearFilters,
3907
- children: tr("清除篩選")
3908
- }) }) : null]
3909
- })
3910
- }),
3911
- p.totals && list.length ? /* @__PURE__ */ jsxs("div", {
3912
- className: "ui-dt-row ui-dt-total",
3913
- role: "row",
3914
- style: gridStyle,
3915
- children: [
3916
- expander ? /* @__PURE__ */ jsx("div", {
3917
- className: "ui-dt-cell",
3918
- role: "cell"
3919
- }) : null,
3920
- selectable ? /* @__PURE__ */ jsx("div", {
3921
- className: "ui-dt-cell",
3922
- role: "cell"
3923
- }) : null,
3924
- shownCols.map((c, ci) => /* @__PURE__ */ jsx("div", {
3925
- role: "cell",
3926
- className: cellClass(c, ci),
3927
- style: stickyStyle(ci),
3928
- children: total(c, ci)
3929
- }, c.key)),
3930
- hasAct ? /* @__PURE__ */ jsx("div", {
3931
- className: "ui-dt-cell",
3932
- role: "cell"
3933
- }) : null
3934
- ]
3935
- }) : null
3936
- ]
3937
- })
3938
- })
3939
- }),
4451
+ toolsView(false),
4452
+ p.zoom !== false && !narrow ? /* @__PURE__ */ jsx("div", {
4453
+ className: "ui-dt-zoom",
4454
+ children: /* @__PURE__ */ jsx(ZoomButton, { onClick: () => setZoomed(true) })
4455
+ }) : null,
4456
+ tableView(false),
3940
4457
  /* @__PURE__ */ jsx(ScrollHint, {
3941
4458
  show: overflows,
3942
4459
  done: scrolled
3943
4460
  }),
3944
- pages > 1 ? /* @__PURE__ */ jsxs("div", {
3945
- className: "ui-dt-foot",
3946
- children: [/* @__PURE__ */ jsx("span", {
3947
- className: "ui-dt-range",
3948
- children: monoNums(tr("第 {a}–{b} 筆,共 {n} 筆", {
3949
- a: "",
3950
- b: "",
3951
- n: ""
3952
- }), [
3953
- start + 1,
3954
- Math.min(list.length, start + pageSize),
3955
- format.number(list.length)
3956
- ])
3957
- }), /* @__PURE__ */ jsxs("nav", {
3958
- className: "ui-dt-pages",
3959
- "aria-label": tr("分頁"),
4461
+ footView,
4462
+ /* @__PURE__ */ jsx(TableDialog, {
4463
+ open: zoomed,
4464
+ onClose: () => setZoomed(false),
4465
+ title: p.caption || p.label,
4466
+ children: /* @__PURE__ */ jsxs("div", {
4467
+ ref: zoomRef,
4468
+ className: "ui-dt ui-dt-zoomed",
3960
4469
  children: [
3961
- /* @__PURE__ */ jsx("button", {
3962
- type: "button",
3963
- className: "ui-dt-page",
3964
- "aria-label": tr("上一頁"),
3965
- disabled: pageNow === 0,
3966
- onClick: () => goPage(pageNow - 1),
3967
- children: "‹"
3968
- }),
3969
- Array.from({ length: pages }, (_, i) => /* @__PURE__ */ jsx("button", {
3970
- type: "button",
3971
- className: cx("ui-dt-page", i === pageNow && "is-on"),
3972
- "aria-current": i === pageNow ? "page" : void 0,
3973
- onClick: () => goPage(i),
3974
- children: String(i + 1)
3975
- }, i)),
3976
- /* @__PURE__ */ jsx("button", {
3977
- type: "button",
3978
- className: "ui-dt-page",
3979
- "aria-label": tr("下一頁"),
3980
- disabled: pageNow >= pages - 1,
3981
- onClick: () => goPage(pageNow + 1),
3982
- children: "›"
3983
- })
4470
+ toolsView(true),
4471
+ tableView(true),
4472
+ footView
3984
4473
  ]
3985
- })]
3986
- }) : null
4474
+ })
4475
+ })
3987
4476
  ]
3988
4477
  });
3989
4478
  }
@@ -5229,34 +5718,28 @@ function MdDetails({ summary, children }) {
5229
5718
  })]
5230
5719
  });
5231
5720
  }
5232
- const MIN_COL = 48;
5233
- /** A Markdown table: column borders in the header drag (TA); the corner button shows the whole table in a wide dialog
5234
- * (TB). Under 480 px every row is a card instead, without either. */
5721
+ /** A Markdown table: column borders in the header drag; the corner button shows the whole table in a wide dialog.
5722
+ * Under 480 px every row is a card instead, without either. The border, the dialog and the copy icon are the same as
5723
+ * DataTable's (internal/tableTools). */
5235
5724
  function MdTable(p) {
5236
5725
  const [widths, setWidths] = React.useState(null);
5237
5726
  const [big, setBig] = React.useState(false);
5238
- const [dragging, setDragging] = React.useState(-1);
5239
- const [measured, setMeasured] = React.useState([]);
5727
+ const [dragging, setDragging] = React.useState(false);
5240
5728
  const table = React.useRef(null);
5241
- const drag = React.useRef(null);
5242
- const measure = React.useCallback(() => [...table.current?.querySelectorAll("thead th") ?? []].map((th) => Math.ceil(th.getBoundingClientRect().width)), []);
5729
+ const measure = () => [...table.current?.querySelectorAll("thead th") ?? []].map((th) => Math.ceil(th.getBoundingClientRect().width));
5730
+ const [measured, setMeasured] = React.useState([]);
5243
5731
  React.useLayoutEffect(() => {
5244
5732
  const el = table.current;
5245
5733
  if (!el) return;
5246
5734
  const update = () => {
5247
- const m = measure();
5735
+ const m = [...el.querySelectorAll("thead th")].map((th) => Math.ceil(th.getBoundingClientRect().width));
5248
5736
  setMeasured((old) => old.length === m.length && old.every((v, i) => v === m[i]) ? old : m);
5249
5737
  };
5250
5738
  update();
5251
5739
  const ro = new ResizeObserver(update);
5252
5740
  ro.observe(el);
5253
5741
  return () => ro.disconnect();
5254
- }, [measure]);
5255
- const widen = (j, base, to) => {
5256
- const next = base.slice();
5257
- next[j] = Math.max(MIN_COL, Math.round(to));
5258
- setWidths(next);
5259
- };
5742
+ }, []);
5260
5743
  const body = (resize) => /* @__PURE__ */ jsxs("table", {
5261
5744
  ref: resize ? table : void 0,
5262
5745
  className: "ui-md-table",
@@ -5269,49 +5752,17 @@ function MdTable(p) {
5269
5752
  resize && widths ? /* @__PURE__ */ jsx("colgroup", { children: widths.map((w, j) => /* @__PURE__ */ jsx("col", { style: { width: w } }, j)) }) : null,
5270
5753
  /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: p.heads.map((h, j) => /* @__PURE__ */ jsxs("th", {
5271
5754
  style: p.align(j),
5272
- children: [h, resize ? /* @__PURE__ */ jsx("span", {
5273
- className: cx("ui-md-col-resize", dragging === j && "is-dragging"),
5274
- role: "separator",
5275
- "aria-orientation": "vertical",
5276
- "aria-label": tr("調整「{name}」欄寬", { name: p.headText[j] }),
5277
- "aria-valuenow": widths?.[j] ?? measured[j] ?? 0,
5278
- "aria-valuemin": MIN_COL,
5279
- tabIndex: 0,
5280
- onPointerDown: (e) => {
5281
- if (e.button !== 0) return;
5282
- e.preventDefault();
5283
- e.currentTarget.setPointerCapture(e.pointerId);
5284
- drag.current = {
5285
- j,
5286
- x: e.clientX,
5287
- base: widths ?? measure()
5288
- };
5289
- setDragging(j);
5755
+ children: [h, resize ? /* @__PURE__ */ jsx(ColResize, {
5756
+ name: p.headText[j],
5757
+ width: widths?.[j] ?? measured[j] ?? 0,
5758
+ start: () => (widths ?? measure())[j],
5759
+ onWidth: (w) => {
5760
+ const next = (widths ?? measure()).slice();
5761
+ next[j] = w;
5762
+ setWidths(next);
5290
5763
  },
5291
- onPointerMove: (e) => {
5292
- const d = drag.current;
5293
- if (d) widen(d.j, d.base, d.base[d.j] + e.clientX - d.x);
5294
- },
5295
- onPointerUp: () => {
5296
- drag.current = null;
5297
- setDragging(-1);
5298
- },
5299
- onPointerCancel: () => {
5300
- drag.current = null;
5301
- setDragging(-1);
5302
- },
5303
- onDoubleClick: () => setWidths(null),
5304
- onKeyDown: (e) => {
5305
- if (e.altKey || e.metaKey || e.ctrlKey) return;
5306
- if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
5307
- e.preventDefault();
5308
- const base = widths ?? measure();
5309
- widen(j, base, base[j] + (e.key === "ArrowRight" ? 16 : -16));
5310
- } else if (e.key === "Enter") {
5311
- e.preventDefault();
5312
- setWidths(null);
5313
- }
5314
- }
5764
+ onReset: () => setWidths(null),
5765
+ onDragging: setDragging
5315
5766
  }) : null]
5316
5767
  }, j)) }) }),
5317
5768
  /* @__PURE__ */ jsx("tbody", { children: p.rows.map((r, ri) => /* @__PURE__ */ jsx("tr", { children: r.map((cell, j) => /* @__PURE__ */ jsx("td", {
@@ -5323,16 +5774,11 @@ function MdTable(p) {
5323
5774
  ]
5324
5775
  });
5325
5776
  return /* @__PURE__ */ jsxs("div", {
5326
- className: cx("ui-md-tablebox", dragging >= 0 && "is-resizing"),
5777
+ className: cx("ui-md-tablebox", dragging && "is-resizing"),
5327
5778
  children: [
5328
5779
  /* @__PURE__ */ jsx("div", {
5329
5780
  className: "ui-md-table-tools",
5330
- children: /* @__PURE__ */ jsx(IconButton, {
5331
- className: "ui-md-table-big",
5332
- icon: "corners-out",
5333
- label: tr("放大表格"),
5334
- onClick: () => setBig(true)
5335
- })
5781
+ children: /* @__PURE__ */ jsx(ZoomButton, { onClick: () => setBig(true) })
5336
5782
  }),
5337
5783
  /* @__PURE__ */ jsx("div", {
5338
5784
  className: "ui-md-table-wrap",
@@ -5341,11 +5787,9 @@ function MdTable(p) {
5341
5787
  "aria-label": p.label,
5342
5788
  children: body(true)
5343
5789
  }),
5344
- big ? /* @__PURE__ */ jsx(Dialog, {
5345
- open: true,
5790
+ /* @__PURE__ */ jsx(TableDialog, {
5791
+ open: big,
5346
5792
  onClose: () => setBig(false),
5347
- title: tr("表格"),
5348
- className: "ui-md-table-dialog",
5349
5793
  children: /* @__PURE__ */ jsx("div", {
5350
5794
  className: "ui-md ui-md-table-wrap",
5351
5795
  tabIndex: 0,
@@ -5353,7 +5797,7 @@ function MdTable(p) {
5353
5797
  "aria-label": p.label,
5354
5798
  children: body(false)
5355
5799
  })
5356
- }) : null
5800
+ })
5357
5801
  ]
5358
5802
  });
5359
5803
  }