@flighthq/text 0.5.1-next.973.788d3d7 → 0.6.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.
Files changed (69) hide show
  1. package/README.md +2 -2
  2. package/dist/.tsbuildinfo.contract +1 -0
  3. package/dist/.tsbuildinfo.runtime +1 -0
  4. package/dist/contract.d.ts +12 -10
  5. package/dist/contract.d.ts.map +1 -1
  6. package/dist/contract.js +13 -10
  7. package/dist/contract.js.map +1 -1
  8. package/dist/contract.text-types.d.ts +2 -0
  9. package/dist/contract.text-types.d.ts.map +1 -0
  10. package/dist/contract.text-types.js +2 -0
  11. package/dist/contract.text-types.js.map +1 -0
  12. package/dist/enableTextLabelGuards.d.ts +2 -1
  13. package/dist/enableTextLabelGuards.d.ts.map +1 -1
  14. package/dist/enableTextLabelGuards.js +6 -6
  15. package/dist/enableTextLabelGuards.js.map +1 -1
  16. package/dist/explainTextLabelContent.d.ts +1 -1
  17. package/dist/explainTextLabelContent.d.ts.map +1 -1
  18. package/dist/explainTextLabelContent.js.map +1 -1
  19. package/dist/index.d.ts +2 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +3 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/nativeText.d.ts +10 -5
  24. package/dist/nativeText.d.ts.map +1 -1
  25. package/dist/nativeText.js +14 -14
  26. package/dist/nativeText.js.map +1 -1
  27. package/dist/richText.d.ts +11 -4
  28. package/dist/richText.d.ts.map +1 -1
  29. package/dist/richText.js +97 -30
  30. package/dist/richText.js.map +1 -1
  31. package/dist/richTextContent.d.ts +6 -0
  32. package/dist/richTextContent.d.ts.map +1 -0
  33. package/dist/richTextContent.js +311 -0
  34. package/dist/richTextContent.js.map +1 -0
  35. package/dist/richTextMetrics.d.ts +10 -0
  36. package/dist/richTextMetrics.d.ts.map +1 -0
  37. package/dist/richTextMetrics.js +53 -0
  38. package/dist/richTextMetrics.js.map +1 -0
  39. package/dist/textFormatFont.d.ts +2 -2
  40. package/dist/textFormatFont.d.ts.map +1 -1
  41. package/dist/textFormatFont.js +4 -2
  42. package/dist/textFormatFont.js.map +1 -1
  43. package/dist/textLabel.d.ts +11 -5
  44. package/dist/textLabel.d.ts.map +1 -1
  45. package/dist/textLabel.js +17 -16
  46. package/dist/textLabel.js.map +1 -1
  47. package/dist/textLabelLayout.d.ts +6 -4
  48. package/dist/textLabelLayout.d.ts.map +1 -1
  49. package/dist/textLabelLayout.js +16 -7
  50. package/dist/textLabelLayout.js.map +1 -1
  51. package/dist/textLayoutRuntime.d.ts +5 -0
  52. package/dist/textLayoutRuntime.d.ts.map +1 -0
  53. package/dist/textLayoutRuntime.js +11 -0
  54. package/dist/textLayoutRuntime.js.map +1 -0
  55. package/dist/types.d.ts +141 -0
  56. package/dist/types.d.ts.map +1 -0
  57. package/dist/types.js +4 -0
  58. package/dist/types.js.map +1 -0
  59. package/package.json +12 -10
  60. package/src/enableTextLabelGuards.test.ts +13 -10
  61. package/src/explainTextLabelContent.test.ts +7 -4
  62. package/src/nativeText.test.ts +49 -16
  63. package/src/richText.test.ts +276 -70
  64. package/src/richTextContent.test.ts +154 -0
  65. package/src/richTextMetrics.test.ts +153 -0
  66. package/src/textFormatFont.test.ts +74 -16
  67. package/src/textLabel.test.ts +62 -20
  68. package/src/textLabelLayout.test.ts +31 -5
  69. package/src/textLayoutRuntime.test.ts +20 -0
@@ -0,0 +1,154 @@
1
+ // Generated from crates/flight-text/tests/rich_text_content_test.rs by flight-compiler.
2
+ // Rust is authoritative; run `npm run generate` after changing it.
3
+
4
+ import { createRichTextData } from '@flighthq/text/contract';
5
+ import type { RichTextData } from '@flighthq/text/contract';
6
+ import { createTextFormatRange } from '@flighthq/textformat/contract';
7
+ import type { TextFormat, TextFormatRangeRaw } from '@flighthq/textformat/contract';
8
+ import { createRichTextContent, createTextLayoutResult } from '@flighthq/textlayout/contract';
9
+ import type { TextLayoutResult } from '@flighthq/textlayout/contract';
10
+ import { describe, expect, it } from 'vitest';
11
+
12
+ import { clearRichTextContent, computeRichTextContent, createRichTextRuntime, getRichTextContent } from './contract.ts';
13
+
14
+ function emptyTextFormat(): TextFormat {
15
+ return {
16
+ align: undefined,
17
+ blockIndent: undefined,
18
+ bold: undefined,
19
+ bullet: undefined,
20
+ color: undefined,
21
+ font: undefined,
22
+ indent: undefined,
23
+ italic: undefined,
24
+ kerning: undefined,
25
+ leading: undefined,
26
+ leftMargin: undefined,
27
+ letterSpacing: undefined,
28
+ listMarker: undefined,
29
+ rightMargin: undefined,
30
+ size: undefined,
31
+ strikethrough: undefined,
32
+ tabStops: undefined,
33
+ target: undefined,
34
+ underline: undefined,
35
+ url: undefined,
36
+ variations: undefined,
37
+ };
38
+ }
39
+
40
+ function formatRef(format: TextFormat): TextFormat {
41
+ return format;
42
+ }
43
+
44
+ function formatRange(format: TextFormat, start: number, end: number): TextFormatRangeRaw {
45
+ return createTextFormatRange(formatRef(format), start, end);
46
+ }
47
+
48
+ function richTextData(): RichTextData {
49
+ return createRichTextData();
50
+ }
51
+
52
+ function textLayoutResult(): TextLayoutResult {
53
+ return createTextLayoutResult();
54
+ }
55
+
56
+ describe('clearRichTextContent', () => {
57
+ it('clears the attached content', () => {
58
+ let runtime = createRichTextRuntime();
59
+ getRichTextContent(runtime);
60
+ clearRichTextContent(runtime);
61
+ expect(runtime.richTextContent === undefined).toBe(true);
62
+ });
63
+ });
64
+
65
+ describe('computeRichTextContent', () => {
66
+ it('applies serialized text format ranges over plain text', () => {
67
+ let content = createRichTextContent();
68
+ let data = richTextData();
69
+ data.text = 'hello';
70
+ let italic = emptyTextFormat();
71
+ italic.italic = true;
72
+ data.textFormatRanges = [formatRange(italic, 1, 4)];
73
+ computeRichTextContent(content, data, undefined);
74
+ const ranges: Array<readonly [number, number, boolean | undefined]> = content.formatRanges.map((range) => [
75
+ range.start,
76
+ range.end,
77
+ range.format!.italic,
78
+ ]);
79
+ expect(ranges).toEqual([
80
+ [0, 1, undefined],
81
+ [1, 4, true],
82
+ [4, 5, undefined],
83
+ ]);
84
+ });
85
+ it('condenses whitespace', () => {
86
+ let content = createRichTextContent();
87
+ let data = richTextData();
88
+ data.condenseWhite = true;
89
+ data.text = ' A \n\t B ';
90
+ computeRichTextContent(content, data, undefined);
91
+ expect(content.text).toEqual('A B ');
92
+ });
93
+ it('decodes entities in plain text', () => {
94
+ let content = createRichTextContent();
95
+ let data = richTextData();
96
+ data.text = 'A&amp;B';
97
+ computeRichTextContent(content, data, undefined);
98
+ expect(content.text).toEqual('A&B');
99
+ });
100
+ it('falls back to the bullet mask when the password character is empty', () => {
101
+ let content = createRichTextContent();
102
+ let data = richTextData();
103
+ data.text = 'ab';
104
+ computeRichTextContent(content, data, '');
105
+ expect(content.text).toEqual('••');
106
+ });
107
+ it('honors max chars', () => {
108
+ let content = createRichTextContent();
109
+ let data = richTextData();
110
+ data.maxChars = 7;
111
+ data.text = 'Hello world';
112
+ computeRichTextContent(content, data, undefined);
113
+ expect(content.text).toEqual('Hello w');
114
+ const lastRangeIndex = content.formatRanges.length - 1;
115
+ expect(content.formatRanges[lastRangeIndex].end).toEqual(7);
116
+ });
117
+ it('masks every character when a password character is supplied', () => {
118
+ let content = createRichTextContent();
119
+ let data = richTextData();
120
+ data.text = 'secret';
121
+ computeRichTextContent(content, data, '*');
122
+ expect(content.text).toEqual('******');
123
+ });
124
+ it('merges serialized text format ranges over the base format', () => {
125
+ let content = createRichTextContent();
126
+ let data = richTextData();
127
+ data.text = 'hello';
128
+ data.textColor = 0x0000_00ff;
129
+ let color = emptyTextFormat();
130
+ color.color = 0xff00_00ff;
131
+ data.textFormatRanges = [formatRange(color, 1, 4)];
132
+ computeRichTextContent(content, data, undefined);
133
+ expect(content.formatRanges[1].format!.color).toEqual(0xff00_00ff);
134
+ });
135
+ it('renders plain text under the base format', () => {
136
+ let content = createRichTextContent();
137
+ let data = richTextData();
138
+ data.text = 'hello';
139
+ data.textColor = 0x3366_99ff;
140
+ computeRichTextContent(content, data, undefined);
141
+ expect(content.text).toEqual('hello');
142
+ expect(content.formatRanges.length).toEqual(1);
143
+ expect(content.formatRanges[0].format!.color).toEqual(0x3366_99ff);
144
+ });
145
+ });
146
+
147
+ describe('getRichTextContent', () => {
148
+ it('attaches reusable content to runtime state', () => {
149
+ let runtime = createRichTextRuntime();
150
+ const content = getRichTextContent(runtime);
151
+ expect(runtime.richTextContent! === content).toBe(true);
152
+ expect(getRichTextContent(runtime) === content).toBe(true);
153
+ });
154
+ });
@@ -0,0 +1,153 @@
1
+ // Generated from crates/flight-text/tests/rich_text_metrics_test.rs by flight-compiler.
2
+ // Rust is authoritative; run `npm run generate` after changing it.
3
+
4
+ import { createRichTextData } from '@flighthq/text/contract';
5
+ import type { RichTextData } from '@flighthq/text/contract';
6
+ import { createTextFormatRange } from '@flighthq/textformat/contract';
7
+ import type { TextFormat, TextFormatRangeRaw } from '@flighthq/textformat/contract';
8
+ import { createTextLayoutResult } from '@flighthq/textlayout/contract';
9
+ import type { TextLayoutResult } from '@flighthq/textlayout/contract';
10
+ import { describe, expect, it } from 'vitest';
11
+
12
+ import {
13
+ computeRichTextBottomScrollV,
14
+ computeRichTextLineCount,
15
+ computeRichTextMaxScrollH,
16
+ computeRichTextMaxScrollV,
17
+ computeRichTextTextHeight,
18
+ computeRichTextTextWidth,
19
+ getRichTextScrollYOffset,
20
+ } from './contract.ts';
21
+
22
+ function emptyTextFormat(): TextFormat {
23
+ return {
24
+ align: undefined,
25
+ blockIndent: undefined,
26
+ bold: undefined,
27
+ bullet: undefined,
28
+ color: undefined,
29
+ font: undefined,
30
+ indent: undefined,
31
+ italic: undefined,
32
+ kerning: undefined,
33
+ leading: undefined,
34
+ leftMargin: undefined,
35
+ letterSpacing: undefined,
36
+ listMarker: undefined,
37
+ rightMargin: undefined,
38
+ size: undefined,
39
+ strikethrough: undefined,
40
+ tabStops: undefined,
41
+ target: undefined,
42
+ underline: undefined,
43
+ url: undefined,
44
+ variations: undefined,
45
+ };
46
+ }
47
+
48
+ function formatRef(format: TextFormat): TextFormat {
49
+ return format;
50
+ }
51
+
52
+ function formatRange(format: TextFormat, start: number, end: number): TextFormatRangeRaw {
53
+ return createTextFormatRange(formatRef(format), start, end);
54
+ }
55
+
56
+ function richTextData(): RichTextData {
57
+ return createRichTextData();
58
+ }
59
+
60
+ function textLayoutResult(): TextLayoutResult {
61
+ return createTextLayoutResult();
62
+ }
63
+
64
+ describe('computeRichTextBottomScrollV', () => {
65
+ it('clamps to the total line count', () => {
66
+ let data = richTextData();
67
+ data.height = 80;
68
+ data.scrollV = 2;
69
+ let layout = textLayoutResult();
70
+ layout.lineHeights = [10, 10, 10];
71
+ layout.numLines = 3;
72
+ expect(computeRichTextBottomScrollV(data, layout)).toEqual(3);
73
+ });
74
+ it('returns the last visible one based line', () => {
75
+ let data = richTextData();
76
+ data.height = 34;
77
+ data.scrollV = 2;
78
+ let layout = textLayoutResult();
79
+ layout.lineHeights = [10, 10, 10, 10];
80
+ layout.numLines = 4;
81
+ expect(computeRichTextBottomScrollV(data, layout)).toEqual(4);
82
+ });
83
+ });
84
+
85
+ describe('computeRichTextLineCount', () => {
86
+ it('returns the layout line count', () => {
87
+ let layout = textLayoutResult();
88
+ layout.numLines = 3;
89
+ expect(computeRichTextLineCount(layout)).toEqual(3);
90
+ });
91
+ });
92
+
93
+ describe('computeRichTextMaxScrollH', () => {
94
+ it('returns horizontal overflow beyond the visible field width', () => {
95
+ let data = richTextData();
96
+ data.width = 54;
97
+ let layout = textLayoutResult();
98
+ layout.textWidth = 80;
99
+ expect(computeRichTextMaxScrollH(data, layout)).toEqual(30);
100
+ });
101
+ it('returns zero when text fits horizontally', () => {
102
+ let data = richTextData();
103
+ data.width = 100;
104
+ let layout = textLayoutResult();
105
+ layout.textWidth = 50;
106
+ expect(computeRichTextMaxScrollH(data, layout)).toEqual(0);
107
+ });
108
+ });
109
+
110
+ describe('computeRichTextMaxScrollV', () => {
111
+ it('returns one for a single line', () => {
112
+ const data = richTextData();
113
+ let layout = textLayoutResult();
114
+ layout.numLines = 1;
115
+ expect(computeRichTextMaxScrollV(data, layout)).toEqual(1);
116
+ });
117
+ it('returns the first scroll v where the final line is visible', () => {
118
+ let data = richTextData();
119
+ data.height = 34;
120
+ let layout = textLayoutResult();
121
+ layout.lineHeights = [10, 10, 10, 10];
122
+ layout.numLines = 4;
123
+ expect(computeRichTextMaxScrollV(data, layout)).toEqual(2);
124
+ });
125
+ });
126
+
127
+ describe('computeRichTextTextHeight', () => {
128
+ it('returns the ceil text height', () => {
129
+ let layout = textLayoutResult();
130
+ layout.textHeight = 12.2;
131
+ expect(computeRichTextTextHeight(layout)).toEqual(13);
132
+ });
133
+ });
134
+
135
+ describe('computeRichTextTextWidth', () => {
136
+ it('returns the ceil text width', () => {
137
+ let layout = textLayoutResult();
138
+ layout.textWidth = 21.1;
139
+ expect(computeRichTextTextWidth(layout)).toEqual(22);
140
+ });
141
+ });
142
+
143
+ describe('getRichTextScrollYOffset', () => {
144
+ it('clamps to the available line count', () => {
145
+ expect(getRichTextScrollYOffset([10, 12], 5)).toEqual(22);
146
+ });
147
+ it('returns the sum of the first n line heights', () => {
148
+ expect(getRichTextScrollYOffset([10, 12, 14], 2)).toEqual(22);
149
+ });
150
+ it('returns zero when first visible line is zero', () => {
151
+ expect(getRichTextScrollYOffset([10, 12, 14], 0)).toEqual(0);
152
+ });
153
+ });
@@ -1,28 +1,86 @@
1
- import { computeTextFormatFontString } from './textFormatFont';
1
+ // Generated from crates/flight-text/tests/text_format_font_test.rs by flight-compiler.
2
+ // Rust is authoritative; run `npm run generate` after changing it.
3
+
4
+ import { createRichTextData } from '@flighthq/text/contract';
5
+ import type { RichTextData } from '@flighthq/text/contract';
6
+ import { createTextFormatRange } from '@flighthq/textformat/contract';
7
+ import type { TextFormat, TextFormatRangeRaw } from '@flighthq/textformat/contract';
8
+ import { createTextLayoutResult } from '@flighthq/textlayout/contract';
9
+ import type { TextLayoutResult } from '@flighthq/textlayout/contract';
10
+ import { describe, expect, it } from 'vitest';
11
+
12
+ import { computeTextFormatFontString } from './contract.ts';
13
+
14
+ function emptyTextFormat(): TextFormat {
15
+ return {
16
+ align: undefined,
17
+ blockIndent: undefined,
18
+ bold: undefined,
19
+ bullet: undefined,
20
+ color: undefined,
21
+ font: undefined,
22
+ indent: undefined,
23
+ italic: undefined,
24
+ kerning: undefined,
25
+ leading: undefined,
26
+ leftMargin: undefined,
27
+ letterSpacing: undefined,
28
+ listMarker: undefined,
29
+ rightMargin: undefined,
30
+ size: undefined,
31
+ strikethrough: undefined,
32
+ tabStops: undefined,
33
+ target: undefined,
34
+ underline: undefined,
35
+ url: undefined,
36
+ variations: undefined,
37
+ };
38
+ }
39
+
40
+ function formatRef(format: TextFormat): TextFormat {
41
+ return format;
42
+ }
43
+
44
+ function formatRange(format: TextFormat, start: number, end: number): TextFormatRangeRaw {
45
+ return createTextFormatRange(formatRef(format), start, end);
46
+ }
47
+
48
+ function richTextData(): RichTextData {
49
+ return createRichTextData();
50
+ }
51
+
52
+ function textLayoutResult(): TextLayoutResult {
53
+ return createTextLayoutResult();
54
+ }
2
55
 
3
56
  describe('computeTextFormatFontString', () => {
4
- it('returns a CSS font string from format properties', () => {
5
- const font = computeTextFormatFontString({ size: 16, font: 'Arial', bold: false, italic: false });
6
- expect(font).toBe('normal normal 16px Arial');
57
+ it('returns a css font string from format properties', () => {
58
+ let format = emptyTextFormat();
59
+ format.size = 16;
60
+ format.font = 'Arial';
61
+ format.bold = false;
62
+ format.italic = false;
63
+ expect(computeTextFormatFontString(format)).toEqual('normal normal 16px Arial');
7
64
  });
8
-
9
65
  it('includes bold and italic when set', () => {
10
- const font = computeTextFormatFontString({ size: 12, font: 'sans-serif', bold: true, italic: true });
11
- expect(font).toBe('italic bold 12px sans-serif');
66
+ let format = emptyTextFormat();
67
+ format.size = 12;
68
+ format.font = 'sans-serif';
69
+ format.bold = true;
70
+ format.italic = true;
71
+ expect(computeTextFormatFontString(format)).toEqual('italic bold 12px sans-serif');
12
72
  });
13
-
14
73
  it('uses defaults when size and font are omitted', () => {
15
- const font = computeTextFormatFontString({});
16
- expect(font).toBe('normal normal 12px sans-serif');
74
+ expect(computeTextFormatFontString(emptyTextFormat())).toEqual('normal normal 12px sans-serif');
17
75
  });
18
-
19
76
  it('uses italic style when italic is true', () => {
20
- const font = computeTextFormatFontString({ italic: true });
21
- expect(font).toMatch(/^italic /);
77
+ let format = emptyTextFormat();
78
+ format.italic = true;
79
+ expect(computeTextFormatFontString(format).startsWith('italic ')).toBe(true);
22
80
  });
23
-
24
81
  it('uses bold weight when bold is true', () => {
25
- const font = computeTextFormatFontString({ bold: true });
26
- expect(font).toContain(' bold ');
82
+ let format = emptyTextFormat();
83
+ format.bold = true;
84
+ expect(computeTextFormatFontString(format).includes(' bold ')).toBe(true);
27
85
  });
28
86
  });
@@ -4,9 +4,8 @@ import {
4
4
  getNodeLocalContentRevision,
5
5
  getNodeLocalTransformRevision,
6
6
  } from '@flighthq/node/contract';
7
+ import type { Node, PartialNode } from '@flighthq/node/contract';
7
8
  import { setTextLayoutMeasureProvider } from '@flighthq/textlayout/contract';
8
- import type { Node, PartialNode, TextLabel } from '@flighthq/types/contract';
9
- import { TextLabelKind } from '@flighthq/types/contract';
10
9
 
11
10
  import {
12
11
  appendTextLabelString,
@@ -24,24 +23,35 @@ import {
24
23
  setTextLabelString,
25
24
  setTextLabelVerticalAlign,
26
25
  setTextLabelWidth,
27
- } from './textLabel';
26
+ } from './textLabel.ts';
27
+ import type { TextLabel } from './types.ts';
28
+ import { TextLabelKind } from './types.ts';
28
29
 
29
30
  describe('appendTextLabelString', () => {
30
31
  it('appends the value to the existing text', () => {
31
- const text = createTextLabel({ data: { text: 'hello' } });
32
+ const text = ((created) => {
33
+ Object.assign(created.data, { text: 'hello' });
34
+ return created;
35
+ })(createTextLabel());
32
36
  appendTextLabelString(text, ' world');
33
37
  expect(text.data.text).toBe('hello world');
34
38
  });
35
39
 
36
40
  it('invalidates local content after append', () => {
37
- const text = createTextLabel({ data: { text: 'hi' } });
41
+ const text = ((created) => {
42
+ Object.assign(created.data, { text: 'hi' });
43
+ return created;
44
+ })(createTextLabel());
38
45
  const content = getNodeLocalContentRevision(text);
39
46
  appendTextLabelString(text, '!');
40
47
  expect(getNodeLocalContentRevision(text)).toBe(content + 1);
41
48
  });
42
49
 
43
50
  it('does not invalidate when value is empty', () => {
44
- const text = createTextLabel({ data: { text: 'hi' } });
51
+ const text = ((created) => {
52
+ Object.assign(created.data, { text: 'hi' });
53
+ return created;
54
+ })(createTextLabel());
45
55
  const content = getNodeLocalContentRevision(text);
46
56
  appendTextLabelString(text, '');
47
57
  expect(getNodeLocalContentRevision(text)).toBe(content);
@@ -54,9 +64,12 @@ describe('computeTextLabelLocalBoundsRectangle', () => {
54
64
  });
55
65
 
56
66
  it('sets out dimensions from data width and height when autoSize is none', () => {
57
- const text = createTextLabel({ data: { width: 200, height: 50 } });
67
+ const text = ((created) => {
68
+ Object.assign(created.data, { width: 200, height: 50 });
69
+ return created;
70
+ })(createTextLabel());
58
71
  const out = createRectangle();
59
- computeTextLabelLocalBoundsRectangle(out, text as unknown as Node);
72
+ computeTextLabelLocalBoundsRectangle(out, text);
60
73
  expect(out.x).toBe(0);
61
74
  expect(out.width).toBe(200);
62
75
  expect(out.height).toBe(50);
@@ -64,20 +77,26 @@ describe('computeTextLabelLocalBoundsRectangle', () => {
64
77
 
65
78
  it('grows to the measured single run under autoSize left, anchored at the origin', () => {
66
79
  setTextLayoutMeasureProvider((value) => value.length * 7);
67
- const text = createTextLabel({ data: { autoSize: 'left', width: 200, height: 50 } });
80
+ const text = ((created) => {
81
+ Object.assign(created.data, { autoSize: 'left', width: 200, height: 50 });
82
+ return created;
83
+ })(createTextLabel());
68
84
  setTextLabelString(text, 'hi');
69
85
  const out = createRectangle();
70
- computeTextLabelLocalBoundsRectangle(out, text as unknown as Node);
86
+ computeTextLabelLocalBoundsRectangle(out, text);
71
87
  expect(out.x).toBe(0);
72
88
  expect(out.width).toBeGreaterThan(0);
73
89
  expect(out.width).toBeLessThan(200);
74
90
  });
75
91
 
76
92
  it('falls back to the fixed box under autoSize when no measure provider exists', () => {
77
- const text = createTextLabel({ data: { autoSize: 'left', width: 200, height: 50 } });
93
+ const text = ((created) => {
94
+ Object.assign(created.data, { autoSize: 'left', width: 200, height: 50 });
95
+ return created;
96
+ })(createTextLabel());
78
97
  setTextLabelString(text, 'hi');
79
98
  const out = createRectangle();
80
- computeTextLabelLocalBoundsRectangle(out, text as unknown as Node);
99
+ computeTextLabelLocalBoundsRectangle(out, text);
81
100
  expect(out.width).toBe(200);
82
101
  expect(out.height).toBe(50);
83
102
  });
@@ -108,7 +127,15 @@ describe('createTextLabel', () => {
108
127
  height: 40,
109
128
  },
110
129
  };
111
- const obj = createTextLabel(base);
130
+ const obj = ((created, fields) => {
131
+ const entityData = created.data;
132
+ Object.assign(created, fields);
133
+ if (fields.data !== undefined) {
134
+ Object.assign(entityData, fields.data);
135
+ Object.assign(created, { data: entityData });
136
+ }
137
+ return created;
138
+ })(createTextLabel(), base);
112
139
  expect(obj.data.autoSize).toStrictEqual(base.data!.autoSize);
113
140
  expect(obj.data.text).toStrictEqual(base.data!.text);
114
141
  expect(obj.data.width).toBe(300);
@@ -117,7 +144,7 @@ describe('createTextLabel', () => {
117
144
 
118
145
  it('returns a new object for better hidden-class performance', () => {
119
146
  const base = {};
120
- const obj = createTextLabel(base);
147
+ const obj = createTextLabel();
121
148
  expect(obj).not.toStrictEqual(base);
122
149
  });
123
150
  });
@@ -133,7 +160,7 @@ describe('createTextLabelData', () => {
133
160
  });
134
161
 
135
162
  it('allows pre-defined values', () => {
136
- const data = createTextLabelData({ autoSize: 'left', text: 'hello', width: 250, height: 30 });
163
+ const data = Object.assign(createTextLabelData(), { autoSize: 'left', text: 'hello', width: 250, height: 30 });
137
164
  expect(data.autoSize).toBe('left');
138
165
  expect(data.text).toBe('hello');
139
166
  expect(data.width).toBe(250);
@@ -161,7 +188,10 @@ describe('createTextLabelRuntime', () => {
161
188
  describe('getTextLabelFormat', () => {
162
189
  it('returns the textFormat field', () => {
163
190
  const format = { size: 24, bold: true };
164
- const text = createTextLabel({ data: { textFormat: format } });
191
+ const text = ((created) => {
192
+ Object.assign(created.data, { textFormat: format });
193
+ return created;
194
+ })(createTextLabel());
165
195
  expect(getTextLabelFormat(text)).toBe(text.data.textFormat);
166
196
  });
167
197
  });
@@ -176,7 +206,10 @@ describe('getTextLabelRuntime', () => {
176
206
 
177
207
  describe('getTextLabelString', () => {
178
208
  it('returns the text field', () => {
179
- const text = createTextLabel({ data: { text: 'hello' } });
209
+ const text = ((created) => {
210
+ Object.assign(created.data, { text: 'hello' });
211
+ return created;
212
+ })(createTextLabel());
180
213
  expect(getTextLabelString(text)).toBe('hello');
181
214
  });
182
215
  });
@@ -250,14 +283,20 @@ describe('setTextLabelString', () => {
250
283
  });
251
284
 
252
285
  it('also invalidates bounds under autoSize (the box is measured from the text)', () => {
253
- const text = createTextLabel({ data: { autoSize: 'left' } });
286
+ const text = ((created) => {
287
+ Object.assign(created.data, { autoSize: 'left' });
288
+ return created;
289
+ })(createTextLabel());
254
290
  const bounds = getNodeLocalBoundsRevision(text);
255
291
  setTextLabelString(text, 'hello');
256
292
  expect(getNodeLocalBoundsRevision(text)).not.toBe(bounds);
257
293
  });
258
294
 
259
295
  it('does not bump content when the value is unchanged', () => {
260
- const text = createTextLabel({ data: { text: 'same' } });
296
+ const text = ((created) => {
297
+ Object.assign(created.data, { text: 'same' });
298
+ return created;
299
+ })(createTextLabel());
261
300
  const content = getNodeLocalContentRevision(text);
262
301
  setTextLabelString(text, 'same');
263
302
  expect(getNodeLocalContentRevision(text)).toBe(content);
@@ -280,7 +319,10 @@ describe('setTextLabelVerticalAlign', () => {
280
319
  });
281
320
 
282
321
  it('does not bump content when the value is unchanged', () => {
283
- const text = createTextLabel({ data: { verticalAlign: 'bottom' } });
322
+ const text = ((created) => {
323
+ Object.assign(created.data, { verticalAlign: 'bottom' });
324
+ return created;
325
+ })(createTextLabel());
284
326
  const content = getNodeLocalContentRevision(text);
285
327
  setTextLabelVerticalAlign(text, 'bottom');
286
328
  expect(getNodeLocalContentRevision(text)).toBe(content);
@@ -1,8 +1,14 @@
1
- import { createTextMetrics, setTextLayoutMeasureProvider } from '@flighthq/textlayout/contract';
2
-
3
- import { createRichText, setRichTextString } from './richText';
4
- import { createTextLabel, setTextLabelString } from './textLabel';
5
- import { ensureTextLayout, getTextLayout, getTextLayoutMetrics, setTextLabelGuard } from './textLabelLayout';
1
+ import { createTextLayoutResult, createTextMetrics, setTextLayoutMeasureProvider } from '@flighthq/textlayout/contract';
2
+
3
+ import { createRichText, setRichTextString } from './richText.ts';
4
+ import { createTextLabel, setTextLabelString } from './textLabel.ts';
5
+ import {
6
+ ensureTextLayout,
7
+ getTextLayout,
8
+ getTextLayoutMetrics,
9
+ setTextLabelGuard,
10
+ setTextLayoutResult,
11
+ } from './textLabelLayout.ts';
6
12
 
7
13
  // A fake fixed-advance measure (7px per char) — exercises the ensure path without a renderer.
8
14
  const measure = (text: string) => text.length * 7;
@@ -131,3 +137,23 @@ describe('setTextLabelGuard', () => {
131
137
  expect(called).toBe(false);
132
138
  });
133
139
  });
140
+
141
+ describe('setTextLayoutResult', () => {
142
+ it('installs a precomputed layout that stays current without a measure provider', () => {
143
+ const richText = createRichText();
144
+ setRichTextString(richText, 'hello');
145
+ const layout = createTextLayoutResult();
146
+ setTextLayoutResult(richText, layout);
147
+ expect(getTextLayout(richText)).toBe(layout);
148
+ });
149
+
150
+ it('is recomputed once the content changes after it was installed', () => {
151
+ setTextLayoutMeasureProvider(measure);
152
+ const richText = createRichText();
153
+ setRichTextString(richText, 'hello');
154
+ setTextLayoutResult(richText, createTextLayoutResult());
155
+ expect(getTextLayout(richText)!.textWidth).toBe(0);
156
+ setRichTextString(richText, 'changed');
157
+ expect(getTextLayout(richText)!.textWidth).toBeGreaterThan(0);
158
+ });
159
+ });
@@ -0,0 +1,20 @@
1
+ import { createTextLabelRuntime } from './contract.ts';
2
+ import { clearTextLayoutResult, getTextLayoutResult } from './textLayoutRuntime.ts';
3
+
4
+ describe('clearTextLayoutResult', () => {
5
+ it('clears the attached layout result', () => {
6
+ const runtime = createTextLabelRuntime();
7
+ getTextLayoutResult(runtime);
8
+ clearTextLayoutResult(runtime);
9
+ expect(runtime.textLayout).toBeNull();
10
+ });
11
+ });
12
+
13
+ describe('getTextLayoutResult', () => {
14
+ it('attaches and reuses a layout result', () => {
15
+ const runtime = createTextLabelRuntime();
16
+ const result = getTextLayoutResult(runtime);
17
+ expect(runtime.textLayout).toBe(result);
18
+ expect(getTextLayoutResult(runtime)).toBe(result);
19
+ });
20
+ });