@bendyline/squisq-react 2.12.8 → 2.12.10

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.
@@ -523,6 +523,9 @@ var SOCIAL_CAPTION_LAYOUT = {
523
523
  chunkPadXEm: 0.55,
524
524
  letterSpacingEm: 0.01
525
525
  };
526
+ function socialCaptionLayout(textCase = "upper") {
527
+ return textCase === "upper" ? SOCIAL_CAPTION_LAYOUT : { ...SOCIAL_CAPTION_LAYOUT, columnGapEm: 0.16 };
528
+ }
526
529
  var CHAR_EM = {
527
530
  A: 0.76,
528
531
  B: 0.76,
@@ -582,28 +585,116 @@ var CHAR_EM = {
582
585
  "\xB0": 0.42,
583
586
  $: 0.64
584
587
  };
588
+ var AS_WRITTEN_CHAR_EM = {
589
+ A: 0.76,
590
+ B: 0.76,
591
+ C: 0.74,
592
+ D: 0.83,
593
+ E: 0.72,
594
+ F: 0.67,
595
+ G: 0.81,
596
+ H: 0.91,
597
+ I: 0.45,
598
+ J: 0.6,
599
+ K: 0.82,
600
+ L: 0.69,
601
+ M: 1.02,
602
+ N: 0.84,
603
+ O: 0.82,
604
+ P: 0.7,
605
+ Q: 0.82,
606
+ R: 0.8,
607
+ S: 0.67,
608
+ T: 0.68,
609
+ U: 0.83,
610
+ V: 0.76,
611
+ W: 1.13,
612
+ X: 0.81,
613
+ Y: 0.73,
614
+ Z: 0.69,
615
+ a: 0.6,
616
+ b: 0.65,
617
+ c: 0.57,
618
+ d: 0.66,
619
+ e: 0.57,
620
+ f: 0.39,
621
+ g: 0.61,
622
+ h: 0.68,
623
+ i: 0.35,
624
+ j: 0.35,
625
+ k: 0.63,
626
+ l: 0.34,
627
+ m: 1.02,
628
+ n: 0.69,
629
+ o: 0.64,
630
+ p: 0.66,
631
+ q: 0.65,
632
+ r: 0.52,
633
+ s: 0.56,
634
+ t: 0.4,
635
+ u: 0.68,
636
+ v: 0.57,
637
+ w: 0.86,
638
+ x: 0.59,
639
+ y: 0.56,
640
+ z: 0.53,
641
+ "0": 0.7,
642
+ "1": 0.57,
643
+ "2": 0.63,
644
+ "3": 0.62,
645
+ "4": 0.65,
646
+ "5": 0.6,
647
+ "6": 0.65,
648
+ "7": 0.57,
649
+ "8": 0.68,
650
+ "9": 0.65,
651
+ "'": 0.28,
652
+ "\u2019": 0.31,
653
+ ".": 0.33,
654
+ ",": 0.33,
655
+ ";": 0.37,
656
+ ":": 0.37,
657
+ "!": 0.38,
658
+ "?": 0.61,
659
+ "-": 0.41,
660
+ "\u2013": 0.7,
661
+ "\u2014": 1,
662
+ '"': 0.51,
663
+ "\u201C": 0.52,
664
+ "\u201D": 0.52,
665
+ "(": 0.45,
666
+ ")": 0.45,
667
+ "%": 1,
668
+ "&": 0.9,
669
+ "/": 0.47,
670
+ "\xB0": 0.44,
671
+ $: 0.64
672
+ };
585
673
  var DEFAULT_CHAR_EM = 0.9;
586
674
  var WIDTH_SAFETY = 1.06;
587
- function estimateSocialWordEm(text) {
675
+ function estimateSocialWordEm(text, textCase = "upper") {
676
+ const upper = textCase === "upper";
677
+ const table = upper ? CHAR_EM : AS_WRITTEN_CHAR_EM;
588
678
  let em = 0;
589
679
  let count = 0;
590
- for (const ch of text.toUpperCase().normalize("NFD").replace(/\p{M}/gu, "")) {
591
- em += CHAR_EM[ch] ?? DEFAULT_CHAR_EM;
680
+ for (const ch of (upper ? text.toUpperCase() : text).normalize("NFD").replace(/\p{M}/gu, "")) {
681
+ em += table[ch] ?? DEFAULT_CHAR_EM;
592
682
  count++;
593
683
  }
594
- const { letterSpacingEm: letterSpacingEm2, wordPadEm: wordPadEm2 } = SOCIAL_CAPTION_LAYOUT;
595
- return (em + count * letterSpacingEm2) * WIDTH_SAFETY + 2 * wordPadEm2;
684
+ const { letterSpacingEm, wordPadEm } = socialCaptionLayout(textCase);
685
+ return (em + count * letterSpacingEm) * WIDTH_SAFETY + 2 * wordPadEm;
596
686
  }
597
- function estimateSocialRows(words, rowEm) {
687
+ function estimateSocialRows(words, rowEm, textCase = "upper") {
688
+ const { columnGapEm } = socialCaptionLayout(textCase);
598
689
  let rows = 0;
599
690
  let x = 0;
600
691
  for (const word of words) {
601
- const width = estimateSocialWordEm(word);
692
+ const width = estimateSocialWordEm(word, textCase);
602
693
  if (rows === 0) {
603
694
  rows = 1;
604
695
  x = width;
605
- } else if (x + SOCIAL_CAPTION_LAYOUT.columnGapEm + width <= rowEm) {
606
- x += SOCIAL_CAPTION_LAYOUT.columnGapEm + width;
696
+ } else if (x + columnGapEm + width <= rowEm) {
697
+ x += columnGapEm + width;
607
698
  } else {
608
699
  rows++;
609
700
  x = width;
@@ -611,12 +702,16 @@ function estimateSocialRows(words, rowEm) {
611
702
  }
612
703
  return rows;
613
704
  }
705
+ function endsSentence(text) {
706
+ return /[.!?…]["'”’»)\]]*$/.test(text);
707
+ }
614
708
  function resolvePhraseTiming(phrase) {
615
709
  if (phrase.words && phrase.words.length > 0) {
616
710
  return phrase.words.map((w) => ({
617
711
  text: w.text,
618
712
  startTime: w.startTime,
619
- endTime: w.endTime
713
+ endTime: w.endTime,
714
+ ...w.paragraphEnd ? { paragraphEnd: true } : {}
620
715
  }));
621
716
  }
622
717
  const rawWords = phrase.text.split(/\s+/).filter((w) => w.length > 0);
@@ -629,7 +724,7 @@ function resolvePhraseTiming(phrase) {
629
724
  endTime: phrase.startTime + (i + 1) * wordDuration
630
725
  }));
631
726
  }
632
- function buildWordStream(captions, rowEm = Number.POSITIVE_INFINITY) {
727
+ function buildWordStream(captions, rowEm = Number.POSITIVE_INFINITY, textCase = "upper") {
633
728
  const allWords = [];
634
729
  for (const phrase of captions.phrases) {
635
730
  for (const word of resolvePhraseTiming(phrase))
@@ -656,7 +751,7 @@ function buildWordStream(captions, rowEm = Number.POSITIVE_INFINITY) {
656
751
  current = [];
657
752
  };
658
753
  for (const word of allWords) {
659
- if (current.length > 0 && (current.length >= chunkSize || word.segment !== current[0].segment || estimateSocialRows([...current.map((w) => w.text), word.text], rowEm) > SOCIAL_CAPTION_MAX_ROWS)) {
754
+ if (current.length > 0 && (current.length >= chunkSize || word.segment !== current[0].segment || current[current.length - 1].paragraphEnd || current.length >= MIN_CHUNK_SIZE && endsSentence(current[current.length - 1].text) || estimateSocialRows([...current.map((w) => w.text), word.text], rowEm, textCase) > SOCIAL_CAPTION_MAX_ROWS)) {
660
755
  close();
661
756
  }
662
757
  current.push(word);
@@ -667,7 +762,6 @@ function buildWordStream(captions, rowEm = Number.POSITIVE_INFINITY) {
667
762
 
668
763
  // src/SocialCaptionOverlay.tsx
669
764
  import { jsx as jsx2 } from "react/jsx-runtime";
670
- var { wordPadEm, columnGapEm, chunkPadXEm, letterSpacingEm } = SOCIAL_CAPTION_LAYOUT;
671
765
  function positionStyle(position, portrait) {
672
766
  switch (position) {
673
767
  case "top":
@@ -703,10 +797,12 @@ function SocialCaptionOverlay({
703
797
  const baseFontSize = Math.min(viewportHeight * 0.055, viewportWidth * 0.068);
704
798
  const fontSize = Math.max(22, Math.min(96, Math.round(baseFontSize)));
705
799
  const sidePadding = portrait ? 0.07 : 0.1;
800
+ const textCase = theme?.typography?.captionCase ?? "upper";
801
+ const { wordPadEm, columnGapEm, chunkPadXEm, letterSpacingEm } = socialCaptionLayout(textCase);
706
802
  const rowEm = viewportWidth * (1 - 2 * sidePadding) / fontSize - 2 * chunkPadXEm;
707
803
  const { chunks } = useMemo3(
708
- () => captions ? buildWordStream(captions, rowEm) : { words: [], chunks: [] },
709
- [captions, rowEm]
804
+ () => captions ? buildWordStream(captions, rowEm, textCase) : { words: [], chunks: [] },
805
+ [captions, rowEm, textCase]
710
806
  );
711
807
  if (!enabled || chunks.length === 0) {
712
808
  return /* @__PURE__ */ jsx2(
@@ -765,8 +861,11 @@ function SocialCaptionOverlay({
765
861
  if (activeWordIndex === -1) activeWordIndex = 0;
766
862
  }
767
863
  const primaryColor = theme?.colors?.primary ?? "#5b9bd5";
768
- const fontFamily = theme?.typography?.titleFont ? resolveFontFamily(theme.typography.titleFont, '"PT Serif", Georgia, serif') : '"PT Serif", Georgia, serif';
769
- const widestEm = Math.max(...activeChunk.words.map((w) => estimateSocialWordEm(w.text)));
864
+ const captionFont = theme?.typography?.captionFont ?? theme?.typography?.titleFont;
865
+ const fontFamily = captionFont ? resolveFontFamily(captionFont, '"PT Serif", Georgia, serif') : '"PT Serif", Georgia, serif';
866
+ const widestEm = Math.max(
867
+ ...activeChunk.words.map((w) => estimateSocialWordEm(w.text, textCase))
868
+ );
770
869
  const chunkFontSize = widestEm > rowEm ? Math.max(16, Math.floor(fontSize * rowEm / widestEm)) : fontSize;
771
870
  return /* @__PURE__ */ jsx2(
772
871
  "div",
@@ -806,7 +905,7 @@ function SocialCaptionOverlay({
806
905
  fontFamily,
807
906
  fontSize: `${chunkFontSize}px`,
808
907
  lineHeight: 1.18,
809
- textTransform: "uppercase",
908
+ textTransform: textCase === "upper" ? "uppercase" : "none",
810
909
  letterSpacing: `${letterSpacingEm}em`,
811
910
  whiteSpace: "normal"
812
911
  },
@@ -0,0 +1,223 @@
1
+ import * as react from 'react';
2
+ import { CSSProperties } from 'react';
3
+ import { LayoutResult, GraphLayoutEngine, LayoutGraph, LayoutOptions } from '@bendyline/squisq-graph';
4
+
5
+ /**
6
+ * A named color role. Hosts map each to their palette through
7
+ * `--squisq-graph-tone-<tone>`; `neutral` is the plain node.
8
+ */
9
+ type GraphTone = 'neutral' | 'accent' | 'positive' | 'notice' | 'warning' | 'danger' | 'muted';
10
+ /** How one laid-out node is drawn. Matched to `LayoutResult.nodes` by id. */
11
+ interface GraphViewNode {
12
+ id: string;
13
+ label: string;
14
+ /** A smaller second line (a folder, a count). Size the node with `graphNodeSize` to fit it. */
15
+ detail?: string;
16
+ /** A short chip at the right edge, such as a count of findings. */
17
+ badge?: string;
18
+ tone?: GraphTone;
19
+ /** An explicit fill, for color lenses; wins over the tone's. */
20
+ fill?: string;
21
+ /** Show a toggle: a collapsed group that can open, or an open container that can close. */
22
+ expandable?: boolean;
23
+ expanded?: boolean;
24
+ /** Tooltip; defaults to the label. */
25
+ title?: string;
26
+ className?: string;
27
+ }
28
+ /** How one routed edge is drawn. Matched to `LayoutResult.edges` by id. */
29
+ interface GraphViewEdge {
30
+ id: string;
31
+ /** Line weight; width grows with its logarithm. Default 1. */
32
+ weight?: number;
33
+ dashed?: boolean;
34
+ /** Arrowheads at both ends. */
35
+ bidirectional?: boolean;
36
+ tone?: GraphTone;
37
+ /** Tooltip. */
38
+ title?: string;
39
+ className?: string;
40
+ }
41
+ interface DiagramViewerProps {
42
+ /** Positions and routes from `@bendyline/squisq-graph`. */
43
+ layout: LayoutResult;
44
+ /** Presentation per node id; a node without one shows its id. */
45
+ nodes?: readonly GraphViewNode[];
46
+ edges?: readonly GraphViewEdge[];
47
+ /** Controlled selection (node ids). */
48
+ selectedIds?: readonly string[];
49
+ onSelectionChange?: (ids: string[]) => void;
50
+ /** Double-click or Enter on a node. */
51
+ onNodeActivate?: (id: string) => void;
52
+ /** A toggle was clicked, or Space pressed on an expandable node; `expanded` is the state asked for. */
53
+ onContainerToggle?: (id: string, expanded: boolean) => void;
54
+ onEdgeClick?: (id: string) => void;
55
+ onHoverChange?: (id: string | null) => void;
56
+ /** Keep these nodes (and the edges between them) bright and dim the rest, e.g. search hits. */
57
+ highlightIds?: readonly string[];
58
+ /** Hovering a node dims everything but it, its neighbors and its edges. Default true. */
59
+ hoverNeighbors?: boolean;
60
+ /**
61
+ * What the mouse wheel does: zoom (default), or scroll the page unless
62
+ * Ctrl/⌘ is held — for a diagram embedded in a scrolling document.
63
+ */
64
+ wheel?: 'zoom' | 'scroll-unless-modifier';
65
+ /** Corner radius of edge bends. Default 6. */
66
+ cornerRadius?: number;
67
+ /** Screen padding when fitting, and the largest scale a fit may reach. Defaults 24 and 1.5. */
68
+ fitPadding?: number;
69
+ maxFitScale?: number;
70
+ /** Show the zoom and fit buttons. Default true. */
71
+ controls?: boolean;
72
+ /** Name of the diagram for assistive technology. */
73
+ ariaLabel: string;
74
+ /** What to announce when a node is selected; defaults to its label. */
75
+ describeNode?: (id: string) => string;
76
+ /** Size to assume before the element is measured (tests, server rendering). */
77
+ initialSize?: {
78
+ width: number;
79
+ height: number;
80
+ };
81
+ className?: string;
82
+ style?: CSSProperties;
83
+ }
84
+ interface DiagramViewerHandle {
85
+ /** Fit the whole drawing. */
86
+ fit(): void;
87
+ /** Fit these nodes. */
88
+ focus(ids: readonly string[]): void;
89
+ /** Pan just enough to show this node, keeping the zoom. */
90
+ reveal(id: string): void;
91
+ /**
92
+ * Hold this node at its screen position through the next layout, when one
93
+ * arrives within a few seconds. The viewer does this itself when a toggle
94
+ * opens or closes a node; call it before changing the graph some other way.
95
+ */
96
+ keepInPlace(id: string): void;
97
+ zoomBy(factor: number): void;
98
+ }
99
+
100
+ declare const DiagramViewer: react.ForwardRefExoticComponent<DiagramViewerProps & react.RefAttributes<DiagramViewerHandle>>;
101
+
102
+ /** Above this many nodes, `DiagramViewer` draws only what is near the screen. */
103
+ declare const GRAPH_VIEW_CULL_AT = 600;
104
+
105
+ /**
106
+ * useGraphLayout — lay a graph out for `DiagramViewer`, off the render path.
107
+ *
108
+ * Results are cached by `graphSignature`, so returning to a graph seen
109
+ * recently is instant. A layout superseded before it finishes is aborted;
110
+ * with a worker engine that ends the worker's work too. While a new layout
111
+ * is computing the previous one stays in place, and `slow` turns on only
112
+ * when the wait is long enough to be worth showing.
113
+ */
114
+
115
+ /** Forget every cached layout (tests, or after the engine version changes). */
116
+ declare function clearGraphLayoutCache(): void;
117
+ interface UseGraphLayoutOptions {
118
+ /** Where layouts run; default the calling thread. Pass `createWorkerLayoutEngine()` for large graphs. */
119
+ engine?: GraphLayoutEngine;
120
+ /** How long a layout may take before `slow` turns on. Default 150 ms. */
121
+ slowAfterMs?: number;
122
+ }
123
+ interface GraphLayoutState {
124
+ /** The layout for the current graph, or the previous one while it computes; null before the first. */
125
+ layout: LayoutResult | null;
126
+ /** True while the current graph's layout is still computing. */
127
+ pending: boolean;
128
+ /** True once a pending layout has taken longer than `slowAfterMs`. */
129
+ slow: boolean;
130
+ error: Error | null;
131
+ signature: string | null;
132
+ }
133
+ declare function useGraphLayout(graph: LayoutGraph | null, options?: LayoutOptions, config?: UseGraphLayoutOptions): GraphLayoutState;
134
+
135
+ /**
136
+ * Text measurement without the DOM. A layout is computed before anything is
137
+ * drawn, often in a worker, so node sizes come from a character-width table
138
+ * for a typical UI sans-serif. The viewer truncates labels with the same
139
+ * table, which is what keeps a label inside the box it was sized for.
140
+ */
141
+ /** Approximate rendered width of `text` at `fontSize`, in the same units. */
142
+ declare function estimateTextWidth(text: string, fontSize?: number): number;
143
+ /** `text` cut with an ellipsis so it fits `maxWidth`; unchanged when it already fits. */
144
+ declare function truncateToWidth(text: string, maxWidth: number, fontSize?: number): string;
145
+ /** The type sizes and insets the viewer draws nodes with. */
146
+ declare const GRAPH_NODE_METRICS: {
147
+ readonly labelSize: 12;
148
+ readonly detailSize: 10;
149
+ readonly badgeSize: 10;
150
+ readonly paddingX: 10;
151
+ /** Height of a node with a label only, and with a label plus a detail line. */
152
+ readonly height: 28;
153
+ readonly heightWithDetail: 40;
154
+ /** Width of a badge per character beyond its padding, and the room for a toggle chevron. */
155
+ readonly badgePadding: 10;
156
+ readonly toggleWidth: 16;
157
+ };
158
+ interface GraphNodeSizeInput {
159
+ label: string;
160
+ detail?: string;
161
+ badge?: string;
162
+ expandable?: boolean;
163
+ }
164
+ interface GraphNodeSizeOptions {
165
+ minWidth?: number;
166
+ maxWidth?: number;
167
+ }
168
+ /**
169
+ * The box a node needs for its label, detail, badge and toggle, clamped to
170
+ * `minWidth`–`maxWidth` (default 64–280). Feed the result to the layout as
171
+ * the node's size; the viewer truncates whatever the clamp cut off.
172
+ */
173
+ declare function graphNodeSize(node: GraphNodeSizeInput, options?: GraphNodeSizeOptions): {
174
+ width: number;
175
+ height: number;
176
+ };
177
+
178
+ /**
179
+ * Pan and zoom as plain math: a transform `{ tx, ty, scale }` maps drawing
180
+ * units to screen pixels (`screen = drawing * scale + t`). Pure, so the
181
+ * viewer and its tests share one definition of fitting and zooming.
182
+ */
183
+ interface ViewTransform {
184
+ tx: number;
185
+ ty: number;
186
+ scale: number;
187
+ }
188
+ interface Size {
189
+ width: number;
190
+ height: number;
191
+ }
192
+ /** Zoom by `factor`, keeping the drawing point under screen point (`fx`, `fy`) still. */
193
+ declare function zoomAt(t: ViewTransform, factor: number, fx: number, fy: number): ViewTransform;
194
+ /** The transform that centers `box` in `size`, as large as fits within `maxScale`. */
195
+ declare function fitTransform(box: {
196
+ x: number;
197
+ y: number;
198
+ width: number;
199
+ height: number;
200
+ }, size: Size, padding?: number, maxScale?: number): ViewTransform;
201
+ /** The part of the drawing the screen shows. */
202
+ declare function visibleRect(t: ViewTransform, size: Size): {
203
+ x0: number;
204
+ y0: number;
205
+ x1: number;
206
+ y1: number;
207
+ };
208
+ /** Pan the least distance that brings `box` fully into view (with `margin`), keeping the scale. */
209
+ declare function revealTransform(t: ViewTransform, box: {
210
+ x: number;
211
+ y: number;
212
+ width: number;
213
+ height: number;
214
+ }, size: Size, margin?: number): ViewTransform;
215
+
216
+ /**
217
+ * The viewer's stylesheet, rendered with each viewer so it needs no CSS
218
+ * import. Every color is a custom property with a light fallback; a host
219
+ * themes the viewer by setting them on an ancestor.
220
+ */
221
+ declare const GRAPH_TONE_NAMES: readonly ["neutral", ...string[]];
222
+
223
+ export { DiagramViewer, type DiagramViewerHandle, type DiagramViewerProps, GRAPH_NODE_METRICS, GRAPH_TONE_NAMES, GRAPH_VIEW_CULL_AT, type GraphLayoutState, type GraphNodeSizeInput, type GraphNodeSizeOptions, type GraphTone, type GraphViewEdge, type GraphViewNode, type UseGraphLayoutOptions, type ViewTransform, clearGraphLayoutCache, estimateTextWidth, fitTransform, graphNodeSize, revealTransform, truncateToWidth, useGraphLayout, visibleRect, zoomAt };