chat-layout 1.1.0-5 → 1.1.1

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/README.md CHANGED
@@ -54,6 +54,7 @@ See [example/chat.ts](./example/chat.ts) for a full chat example.
54
54
  - `Text` and `MultilineText` default to `overflowWrap: "break-word"`, which preserves compatibility-first min-content sizing for shrink layouts.
55
55
  - Use `overflowWrap: "anywhere"` when long unspaced strings should contribute grapheme-level breakpoints to min-content sizing.
56
56
  - `Text` supports `overflow: "ellipsis"` with `ellipsisPosition: "start" | "end" | "middle"` when measured under a finite `maxWidth`.
57
+ - `Text` and `MultilineText` both accept either a plain string or `InlineSpan[]` for mixed inline styles.
57
58
  - `MultilineText` supports `overflow: "ellipsis"` together with `maxLines`; values below `1` are treated as `1`.
58
59
 
59
60
  ## Text ellipsis
@@ -61,7 +62,11 @@ See [example/chat.ts](./example/chat.ts) for a full chat example.
61
62
  Single-line `Text` can ellipsize at the start, end, or middle when a finite width constraint is present:
62
63
 
63
64
  ```ts
64
- const title = new Text("Extremely long thread title that should not blow out the row", {
65
+ const title = new Text([
66
+ { text: "Extremely long " },
67
+ { text: "thread title", font: "700 16px system-ui", color: "#0f766e" },
68
+ { text: " that should not blow out the row" },
69
+ ], {
65
70
  lineHeight: 20,
66
71
  font: "16px system-ui",
67
72
  color: "#111",
package/example/test.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  type Context,
10
10
  type DynValue,
11
11
  type HitTest,
12
+ type InlineSpan,
12
13
  type Node,
13
14
  } from "chat-layout";
14
15
 
@@ -83,6 +84,14 @@ const renderer = new DebugRenderer(ctx, {});
83
84
 
84
85
  let color = "green";
85
86
 
87
+ const singleLineRichText: InlineSpan<C>[] = [
88
+ { text: "单行 " },
89
+ { text: "rich", font: "700 16px monospace", color: "#0f766e" },
90
+ { text: " text 也支持 " },
91
+ { text: "inline span", font: "700 16px monospace", color: "#2563eb" },
92
+ { text: " 省略了" },
93
+ ];
94
+
86
95
  class ClickDetect extends Wrapper<C> {
87
96
  hittest(_ctx: Context<C>, _test: HitTest): boolean {
88
97
  color = "red";
@@ -108,10 +117,12 @@ const node = new RoundedBox(
108
117
  [
109
118
  new ClickDetect(
110
119
  new RoundedBox(
111
- new Text("测试 3".repeat(2), {
120
+ new Text(singleLineRichText, {
112
121
  lineHeight: 20,
113
122
  font: "400 16px monospace",
114
123
  color: () => color,
124
+ overflow: "ellipsis",
125
+ ellipsisPosition: "middle",
115
126
  }),
116
127
  {
117
128
  left: 14,
package/index.d.mts CHANGED
@@ -82,7 +82,7 @@ interface TextStyleOptions<C extends CanvasRenderingContext2D> {
82
82
  overflowWrap?: TextOverflowWrapMode;
83
83
  }
84
84
  /**
85
- * A span-like inline text fragment used by rich multi-line text.
85
+ * A span-like inline text fragment used by rich text nodes.
86
86
  */
87
87
  interface InlineSpan<C extends CanvasRenderingContext2D> {
88
88
  /** Source text contained in this inline fragment. */
@@ -390,13 +390,13 @@ declare class MultilineText<C extends CanvasRenderingContext2D> implements Node<
390
390
  * Draws a single line of text, clipped logically by measurement width.
391
391
  */
392
392
  declare class Text<C extends CanvasRenderingContext2D> implements Node<C> {
393
- readonly text: string;
393
+ readonly text: string | InlineSpan<C>[];
394
394
  readonly options: TextOptions<C>;
395
395
  /**
396
- * @param text Source text to measure and draw.
396
+ * @param text Source text to measure and draw. Pass an `InlineSpan[]` for mixed inline styles.
397
397
  * @param options Text layout and drawing options.
398
398
  */
399
- constructor(text: string, options: TextOptions<C>);
399
+ constructor(text: string | InlineSpan<C>[], options: TextOptions<C>);
400
400
  measure(ctx: Context<C>): Box;
401
401
  measureMinContent(ctx: Context<C>): Box;
402
402
  draw(ctx: Context<C>, x: number, y: number): boolean;