@tldraw/editor 3.14.0-canary.8a1fcbb09c18 → 3.14.0-canary.8c4382413dbd

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 (160) hide show
  1. package/dist-cjs/index.d.ts +68 -63
  2. package/dist-cjs/index.js +8 -10
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/config/TLSessionStateSnapshot.js +1 -12
  5. package/dist-cjs/lib/config/TLSessionStateSnapshot.js.map +3 -3
  6. package/dist-cjs/lib/editor/Editor.js +74 -73
  7. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  8. package/dist-cjs/lib/editor/bindings/BindingUtil.js.map +2 -2
  9. package/dist-cjs/lib/editor/derivations/bindingsIndex.js +22 -22
  10. package/dist-cjs/lib/editor/derivations/bindingsIndex.js.map +2 -2
  11. package/dist-cjs/lib/editor/derivations/parentsToChildren.js +16 -16
  12. package/dist-cjs/lib/editor/derivations/parentsToChildren.js.map +2 -2
  13. package/dist-cjs/lib/editor/managers/{ClickManager.js → ClickManager/ClickManager.js} +1 -1
  14. package/dist-cjs/lib/editor/managers/ClickManager/ClickManager.js.map +7 -0
  15. package/dist-cjs/lib/editor/managers/{EdgeScrollManager.js → EdgeScrollManager/EdgeScrollManager.js} +2 -2
  16. package/dist-cjs/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.js.map +7 -0
  17. package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js.map +7 -0
  18. package/dist-cjs/lib/editor/managers/{FontManager.js → FontManager/FontManager.js} +1 -2
  19. package/dist-cjs/lib/editor/managers/FontManager/FontManager.js.map +7 -0
  20. package/dist-cjs/lib/editor/managers/{HistoryManager.js → HistoryManager/HistoryManager.js} +64 -6
  21. package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +7 -0
  22. package/dist-cjs/lib/editor/managers/{ScribbleManager.js → ScribbleManager/ScribbleManager.js} +1 -1
  23. package/dist-cjs/lib/editor/managers/ScribbleManager/ScribbleManager.js.map +7 -0
  24. package/dist-cjs/lib/editor/managers/{TextManager.js → TextManager/TextManager.js} +73 -42
  25. package/dist-cjs/lib/editor/managers/TextManager/TextManager.js.map +7 -0
  26. package/dist-cjs/lib/editor/managers/{TickManager.js → TickManager/TickManager.js} +1 -1
  27. package/dist-cjs/lib/editor/managers/TickManager/TickManager.js.map +7 -0
  28. package/dist-cjs/lib/editor/managers/{UserPreferencesManager.js → UserPreferencesManager/UserPreferencesManager.js} +1 -1
  29. package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js.map +7 -0
  30. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +1 -1
  31. package/dist-cjs/lib/editor/shapes/group/GroupShapeUtil.js +1 -1
  32. package/dist-cjs/lib/editor/shapes/group/GroupShapeUtil.js.map +1 -1
  33. package/dist-cjs/lib/editor/tools/StateNode.js +3 -3
  34. package/dist-cjs/lib/editor/tools/StateNode.js.map +2 -2
  35. package/dist-cjs/lib/editor/types/external-content.js.map +1 -1
  36. package/dist-cjs/lib/exports/getSvgJsx.js.map +1 -1
  37. package/dist-cjs/lib/hooks/useCanvasEvents.js +1 -2
  38. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  39. package/dist-cjs/lib/primitives/Box.js +0 -6
  40. package/dist-cjs/lib/primitives/Box.js.map +2 -2
  41. package/dist-cjs/lib/utils/areShapesContentEqual.js +1 -1
  42. package/dist-cjs/lib/utils/areShapesContentEqual.js.map +2 -2
  43. package/dist-cjs/lib/utils/reorderShapes.js +11 -10
  44. package/dist-cjs/lib/utils/reorderShapes.js.map +2 -2
  45. package/dist-cjs/lib/utils/richText.js +7 -2
  46. package/dist-cjs/lib/utils/richText.js.map +2 -2
  47. package/dist-cjs/version.js +3 -3
  48. package/dist-cjs/version.js.map +1 -1
  49. package/dist-esm/index.d.mts +68 -63
  50. package/dist-esm/index.mjs +12 -10
  51. package/dist-esm/index.mjs.map +2 -2
  52. package/dist-esm/lib/config/TLSessionStateSnapshot.mjs +1 -1
  53. package/dist-esm/lib/config/TLSessionStateSnapshot.mjs.map +2 -2
  54. package/dist-esm/lib/editor/Editor.mjs +74 -73
  55. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  56. package/dist-esm/lib/editor/bindings/BindingUtil.mjs.map +2 -2
  57. package/dist-esm/lib/editor/derivations/bindingsIndex.mjs +22 -22
  58. package/dist-esm/lib/editor/derivations/bindingsIndex.mjs.map +2 -2
  59. package/dist-esm/lib/editor/derivations/parentsToChildren.mjs +16 -16
  60. package/dist-esm/lib/editor/derivations/parentsToChildren.mjs.map +2 -2
  61. package/dist-esm/lib/editor/managers/{ClickManager.mjs → ClickManager/ClickManager.mjs} +1 -1
  62. package/dist-esm/lib/editor/managers/ClickManager/ClickManager.mjs.map +7 -0
  63. package/dist-esm/lib/editor/managers/{EdgeScrollManager.mjs → EdgeScrollManager/EdgeScrollManager.mjs} +2 -2
  64. package/dist-esm/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.mjs.map +7 -0
  65. package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs.map +7 -0
  66. package/dist-esm/lib/editor/managers/{FontManager.mjs → FontManager/FontManager.mjs} +1 -2
  67. package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs.map +7 -0
  68. package/dist-esm/lib/editor/managers/{HistoryManager.mjs → HistoryManager/HistoryManager.mjs} +60 -2
  69. package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +7 -0
  70. package/dist-esm/lib/editor/managers/{ScribbleManager.mjs → ScribbleManager/ScribbleManager.mjs} +1 -1
  71. package/dist-esm/lib/editor/managers/ScribbleManager/ScribbleManager.mjs.map +7 -0
  72. package/dist-esm/lib/editor/managers/{TextManager.mjs → TextManager/TextManager.mjs} +73 -42
  73. package/dist-esm/lib/editor/managers/TextManager/TextManager.mjs.map +7 -0
  74. package/dist-esm/lib/editor/managers/{TickManager.mjs → TickManager/TickManager.mjs} +1 -1
  75. package/dist-esm/lib/editor/managers/TickManager/TickManager.mjs.map +7 -0
  76. package/dist-esm/lib/editor/managers/{UserPreferencesManager.mjs → UserPreferencesManager/UserPreferencesManager.mjs} +1 -1
  77. package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs.map +7 -0
  78. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +1 -1
  79. package/dist-esm/lib/editor/shapes/group/GroupShapeUtil.mjs +1 -1
  80. package/dist-esm/lib/editor/shapes/group/GroupShapeUtil.mjs.map +1 -1
  81. package/dist-esm/lib/editor/tools/StateNode.mjs +3 -3
  82. package/dist-esm/lib/editor/tools/StateNode.mjs.map +2 -2
  83. package/dist-esm/lib/exports/getSvgJsx.mjs.map +1 -1
  84. package/dist-esm/lib/hooks/useCanvasEvents.mjs +1 -2
  85. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  86. package/dist-esm/lib/primitives/Box.mjs +0 -6
  87. package/dist-esm/lib/primitives/Box.mjs.map +2 -2
  88. package/dist-esm/lib/utils/areShapesContentEqual.mjs +1 -1
  89. package/dist-esm/lib/utils/areShapesContentEqual.mjs.map +2 -2
  90. package/dist-esm/lib/utils/reorderShapes.mjs +11 -10
  91. package/dist-esm/lib/utils/reorderShapes.mjs.map +2 -2
  92. package/dist-esm/lib/utils/richText.mjs +8 -3
  93. package/dist-esm/lib/utils/richText.mjs.map +2 -2
  94. package/dist-esm/version.mjs +3 -3
  95. package/dist-esm/version.mjs.map +1 -1
  96. package/editor.css +433 -482
  97. package/package.json +8 -9
  98. package/src/index.ts +15 -8
  99. package/src/lib/config/TLSessionStateSnapshot.ts +1 -1
  100. package/src/lib/editor/Editor.test.ts +252 -3
  101. package/src/lib/editor/Editor.ts +75 -72
  102. package/src/lib/editor/bindings/BindingUtil.ts +6 -0
  103. package/src/lib/editor/derivations/bindingsIndex.ts +27 -26
  104. package/src/lib/editor/derivations/parentsToChildren.ts +28 -25
  105. package/src/lib/editor/managers/ClickManager/ClickManager.test.ts +442 -0
  106. package/src/lib/editor/managers/{ClickManager.ts → ClickManager/ClickManager.ts} +3 -3
  107. package/src/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.test.ts +374 -0
  108. package/src/lib/editor/managers/{EdgeScrollManager.ts → EdgeScrollManager/EdgeScrollManager.ts} +3 -3
  109. package/src/lib/editor/managers/FocusManager/FocusManager.test.ts +455 -0
  110. package/src/lib/editor/managers/{FocusManager.ts → FocusManager/FocusManager.ts} +1 -1
  111. package/src/lib/editor/managers/FontManager/FontManager.test.ts +263 -0
  112. package/src/lib/editor/managers/{FontManager.ts → FontManager/FontManager.ts} +2 -3
  113. package/src/lib/editor/managers/{HistoryManager.test.ts → HistoryManager/HistoryManager.test.ts} +388 -1
  114. package/src/lib/editor/managers/{HistoryManager.ts → HistoryManager/HistoryManager.ts} +73 -2
  115. package/src/lib/editor/managers/ScribbleManager/ScribbleManager.test.ts +624 -0
  116. package/src/lib/editor/managers/{ScribbleManager.ts → ScribbleManager/ScribbleManager.ts} +2 -2
  117. package/src/lib/editor/managers/SnapManager/SnapManager.test.ts +485 -0
  118. package/src/lib/editor/managers/TextManager/TextManager.test.ts +407 -0
  119. package/src/lib/editor/managers/{TextManager.ts → TextManager/TextManager.ts} +118 -87
  120. package/src/lib/editor/managers/TickManager/TickManager.test.ts +314 -0
  121. package/src/lib/editor/managers/{TickManager.ts → TickManager/TickManager.ts} +2 -2
  122. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.test.ts +591 -0
  123. package/src/lib/editor/managers/{UserPreferencesManager.ts → UserPreferencesManager/UserPreferencesManager.ts} +2 -2
  124. package/src/lib/editor/shapes/ShapeUtil.ts +1 -1
  125. package/src/lib/editor/shapes/group/GroupShapeUtil.tsx +1 -1
  126. package/src/lib/editor/tools/StateNode.ts +3 -3
  127. package/src/lib/editor/types/external-content.ts +11 -2
  128. package/src/lib/exports/getSvgJsx.tsx +1 -1
  129. package/src/lib/hooks/useCanvasEvents.ts +0 -1
  130. package/src/lib/primitives/Box.ts +0 -8
  131. package/src/lib/utils/areShapesContentEqual.ts +1 -2
  132. package/src/lib/utils/reorderShapes.ts +10 -13
  133. package/src/lib/utils/richText.ts +10 -4
  134. package/src/version.ts +3 -3
  135. package/dist-cjs/lib/editor/managers/ClickManager.js.map +0 -7
  136. package/dist-cjs/lib/editor/managers/EdgeScrollManager.js.map +0 -7
  137. package/dist-cjs/lib/editor/managers/FocusManager.js.map +0 -7
  138. package/dist-cjs/lib/editor/managers/FontManager.js.map +0 -7
  139. package/dist-cjs/lib/editor/managers/HistoryManager.js.map +0 -7
  140. package/dist-cjs/lib/editor/managers/ScribbleManager.js.map +0 -7
  141. package/dist-cjs/lib/editor/managers/Stack.js +0 -82
  142. package/dist-cjs/lib/editor/managers/Stack.js.map +0 -7
  143. package/dist-cjs/lib/editor/managers/TextManager.js.map +0 -7
  144. package/dist-cjs/lib/editor/managers/TickManager.js.map +0 -7
  145. package/dist-cjs/lib/editor/managers/UserPreferencesManager.js.map +0 -7
  146. package/dist-esm/lib/editor/managers/ClickManager.mjs.map +0 -7
  147. package/dist-esm/lib/editor/managers/EdgeScrollManager.mjs.map +0 -7
  148. package/dist-esm/lib/editor/managers/FocusManager.mjs.map +0 -7
  149. package/dist-esm/lib/editor/managers/FontManager.mjs.map +0 -7
  150. package/dist-esm/lib/editor/managers/HistoryManager.mjs.map +0 -7
  151. package/dist-esm/lib/editor/managers/ScribbleManager.mjs.map +0 -7
  152. package/dist-esm/lib/editor/managers/Stack.mjs +0 -62
  153. package/dist-esm/lib/editor/managers/Stack.mjs.map +0 -7
  154. package/dist-esm/lib/editor/managers/TextManager.mjs.map +0 -7
  155. package/dist-esm/lib/editor/managers/TickManager.mjs.map +0 -7
  156. package/dist-esm/lib/editor/managers/UserPreferencesManager.mjs.map +0 -7
  157. package/src/lib/editor/managers/ScribbleManager.test.ts +0 -32
  158. package/src/lib/editor/managers/Stack.ts +0 -71
  159. /package/dist-cjs/lib/editor/managers/{FocusManager.js → FocusManager/FocusManager.js} +0 -0
  160. /package/dist-esm/lib/editor/managers/{FocusManager.mjs → FocusManager/FocusManager.mjs} +0 -0
@@ -0,0 +1,407 @@
1
+ import { Editor } from '../../Editor'
2
+ import { TextManager, TLMeasureTextSpanOpts } from './TextManager'
3
+
4
+ // Create a simple mock DOM environment
5
+ const mockElement = {
6
+ classList: { add: jest.fn() },
7
+ tabIndex: -1,
8
+ cloneNode: jest.fn(),
9
+ innerHTML: '',
10
+ textContent: '',
11
+ setAttribute: jest.fn(),
12
+ style: { setProperty: jest.fn() },
13
+ scrollWidth: 100,
14
+ getBoundingClientRect: jest.fn(() => ({
15
+ width: 100,
16
+ height: 20,
17
+ left: 0,
18
+ top: 0,
19
+ right: 100,
20
+ bottom: 20,
21
+ })),
22
+ remove: jest.fn(),
23
+ insertAdjacentElement: jest.fn(),
24
+ childNodes: [],
25
+ }
26
+
27
+ // Mock document.createElement to return our mock element
28
+ const mockCreateElement = jest.fn(() => {
29
+ const element = { ...mockElement }
30
+ element.cloneNode = jest.fn(() => ({ ...element }))
31
+ return element
32
+ })
33
+
34
+ // Mock editor
35
+ const mockEditor = {
36
+ getContainer: jest.fn(() => ({
37
+ appendChild: jest.fn(),
38
+ })),
39
+ } as unknown as Editor
40
+
41
+ // Setup global mocks
42
+ global.document = {
43
+ createElement: mockCreateElement,
44
+ } as any
45
+
46
+ global.Range = jest.fn(() => ({
47
+ setStart: jest.fn(),
48
+ setEnd: jest.fn(),
49
+ getClientRects: jest.fn(() => [
50
+ {
51
+ width: 10,
52
+ height: 16,
53
+ left: 0,
54
+ top: 0,
55
+ right: 10,
56
+ bottom: 16,
57
+ },
58
+ ]),
59
+ })) as any
60
+
61
+ describe('TextManager', () => {
62
+ let textManager: TextManager
63
+
64
+ beforeEach(() => {
65
+ jest.clearAllMocks()
66
+ textManager = new TextManager(mockEditor)
67
+ })
68
+
69
+ describe('constructor', () => {
70
+ it('should create a TextManager instance', () => {
71
+ expect(textManager).toBeInstanceOf(TextManager)
72
+ expect(textManager.editor).toBe(mockEditor)
73
+ })
74
+ })
75
+
76
+ describe('measureText', () => {
77
+ const defaultOpts = {
78
+ fontStyle: 'normal',
79
+ fontWeight: '400',
80
+ fontFamily: 'Arial',
81
+ fontSize: 16,
82
+ lineHeight: 1.2,
83
+ maxWidth: 200,
84
+ minWidth: null,
85
+ padding: '0px',
86
+ }
87
+
88
+ it('should call measureHtml with normalized text', () => {
89
+ const spy = jest.spyOn(textManager, 'measureHtml')
90
+ textManager.measureText('Hello World', defaultOpts)
91
+ expect(spy).toHaveBeenCalledWith('Hello World', defaultOpts)
92
+ })
93
+
94
+ it('should normalize line breaks', () => {
95
+ const spy = jest.spyOn(textManager, 'measureHtml')
96
+ textManager.measureText('Hello\nWorld\r\nTest', defaultOpts)
97
+ // The text should be normalized to use consistent line breaks
98
+ expect(spy).toHaveBeenCalled()
99
+ })
100
+
101
+ it('should handle empty text', () => {
102
+ const result = textManager.measureText('', { ...defaultOpts, measureScrollWidth: true })
103
+ expect(result).toHaveProperty('x', 0)
104
+ expect(result).toHaveProperty('y', 0)
105
+ expect(result).toHaveProperty('w')
106
+ expect(result).toHaveProperty('h')
107
+ expect(result).toHaveProperty('scrollWidth')
108
+ })
109
+ })
110
+
111
+ describe('measureHtml', () => {
112
+ const defaultOpts = {
113
+ fontStyle: 'normal',
114
+ fontWeight: '400',
115
+ fontFamily: 'Arial',
116
+ fontSize: 16,
117
+ lineHeight: 1.2,
118
+ maxWidth: 200,
119
+ minWidth: null,
120
+ padding: '0px',
121
+ }
122
+
123
+ it('should return measurement object with correct structure', () => {
124
+ const result = textManager.measureHtml('<span>Test</span>', defaultOpts)
125
+
126
+ expect(result).toMatchObject({
127
+ x: 0,
128
+ y: 0,
129
+ w: expect.any(Number),
130
+ h: expect.any(Number),
131
+ })
132
+ })
133
+
134
+ it('should handle null maxWidth', () => {
135
+ const opts = { ...defaultOpts, maxWidth: null }
136
+ const result = textManager.measureHtml('Test', opts)
137
+
138
+ expect(result).toMatchObject({
139
+ x: 0,
140
+ y: 0,
141
+ w: expect.any(Number),
142
+ h: expect.any(Number),
143
+ })
144
+ })
145
+
146
+ it('should handle overflow wrap breaking', () => {
147
+ const opts = { ...defaultOpts, disableOverflowWrapBreaking: true }
148
+ const result = textManager.measureHtml('Test', opts)
149
+
150
+ expect(result).toMatchObject({
151
+ x: 0,
152
+ y: 0,
153
+ w: expect.any(Number),
154
+ h: expect.any(Number),
155
+ })
156
+ })
157
+
158
+ it('should handle other styles', () => {
159
+ const opts = {
160
+ ...defaultOpts,
161
+ otherStyles: {
162
+ 'text-decoration': 'underline',
163
+ color: 'red',
164
+ },
165
+ }
166
+
167
+ const result = textManager.measureHtml('Test', opts)
168
+ expect(result).toMatchObject({
169
+ x: 0,
170
+ y: 0,
171
+ w: expect.any(Number),
172
+ h: expect.any(Number),
173
+ })
174
+ })
175
+ })
176
+
177
+ describe('measureElementTextNodeSpans', () => {
178
+ it('should handle elements with text nodes', () => {
179
+ const mockTextNode = {
180
+ nodeType: 3, // TEXT_NODE
181
+ textContent: 'Hello',
182
+ }
183
+
184
+ const mockElementWithText = {
185
+ childNodes: [mockTextNode],
186
+ getBoundingClientRect: () => ({ left: 0, top: 0 }),
187
+ }
188
+
189
+ const result = textManager.measureElementTextNodeSpans(mockElementWithText as any)
190
+
191
+ expect(result).toHaveProperty('spans')
192
+ expect(result).toHaveProperty('didTruncate')
193
+ expect(Array.isArray(result.spans)).toBe(true)
194
+ expect(typeof result.didTruncate).toBe('boolean')
195
+ })
196
+
197
+ it('should handle empty elements', () => {
198
+ const mockEmptyElement = {
199
+ childNodes: [],
200
+ getBoundingClientRect: () => ({ left: 0, top: 0 }),
201
+ }
202
+
203
+ const result = textManager.measureElementTextNodeSpans(mockEmptyElement as any)
204
+
205
+ expect(result.didTruncate).toBe(false)
206
+ expect(result.spans).toHaveLength(0)
207
+ })
208
+
209
+ it('should handle truncation option', () => {
210
+ const mockTextNode = {
211
+ nodeType: 3, // TEXT_NODE
212
+ textContent: 'Hello World',
213
+ }
214
+
215
+ const mockElementWithText = {
216
+ childNodes: [mockTextNode],
217
+ getBoundingClientRect: () => ({ left: 0, top: 0 }),
218
+ }
219
+
220
+ const result = textManager.measureElementTextNodeSpans(mockElementWithText as any, {
221
+ shouldTruncateToFirstLine: true,
222
+ })
223
+
224
+ expect(result).toHaveProperty('spans')
225
+ expect(result).toHaveProperty('didTruncate')
226
+ })
227
+ })
228
+
229
+ describe('measureTextSpans', () => {
230
+ const defaultOpts: TLMeasureTextSpanOpts = {
231
+ overflow: 'wrap',
232
+ width: 200,
233
+ height: 100,
234
+ padding: 10,
235
+ fontSize: 16,
236
+ fontWeight: '400',
237
+ fontFamily: 'Arial',
238
+ fontStyle: 'normal',
239
+ lineHeight: 1.2,
240
+ textAlign: 'start',
241
+ }
242
+
243
+ it('should return empty array for empty text', () => {
244
+ const result = textManager.measureTextSpans('', defaultOpts)
245
+ expect(result).toEqual([])
246
+ })
247
+
248
+ it('should return array of text spans for non-empty text', () => {
249
+ // Mock measureElementTextNodeSpans to return some spans
250
+ jest.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
251
+ spans: [
252
+ {
253
+ text: 'Hello World',
254
+ box: { x: 0, y: 0, w: 100, h: 16 },
255
+ },
256
+ ],
257
+ didTruncate: false,
258
+ })
259
+
260
+ const result = textManager.measureTextSpans('Hello World', defaultOpts)
261
+
262
+ expect(Array.isArray(result)).toBe(true)
263
+ expect(result.length).toBeGreaterThan(0)
264
+ expect(result[0]).toHaveProperty('text')
265
+ expect(result[0]).toHaveProperty('box')
266
+ })
267
+
268
+ it('should handle wrap overflow', () => {
269
+ jest.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
270
+ spans: [
271
+ {
272
+ text: 'Hello World',
273
+ box: { x: 0, y: 0, w: 100, h: 16 },
274
+ },
275
+ ],
276
+ didTruncate: false,
277
+ })
278
+
279
+ const opts = { ...defaultOpts, overflow: 'wrap' as const }
280
+ const result = textManager.measureTextSpans('Hello World', opts)
281
+
282
+ expect(Array.isArray(result)).toBe(true)
283
+ })
284
+
285
+ it('should handle truncate-ellipsis overflow', () => {
286
+ // Mock the calls for ellipsis handling
287
+ jest
288
+ .spyOn(textManager, 'measureElementTextNodeSpans')
289
+ .mockReturnValueOnce({
290
+ spans: [
291
+ {
292
+ text: 'Hello Wo',
293
+ box: { x: 0, y: 0, w: 80, h: 16 },
294
+ },
295
+ ],
296
+ didTruncate: true,
297
+ })
298
+ .mockReturnValueOnce({
299
+ spans: [
300
+ {
301
+ text: '…',
302
+ box: { x: 0, y: 0, w: 10, h: 16 },
303
+ },
304
+ ],
305
+ didTruncate: false,
306
+ })
307
+ .mockReturnValueOnce({
308
+ spans: [
309
+ {
310
+ text: 'Hello W',
311
+ box: { x: 0, y: 0, w: 70, h: 16 },
312
+ },
313
+ ],
314
+ didTruncate: false,
315
+ })
316
+
317
+ const opts = { ...defaultOpts, overflow: 'truncate-ellipsis' as const }
318
+ const result = textManager.measureTextSpans('Hello World', opts)
319
+
320
+ expect(Array.isArray(result)).toBe(true)
321
+ })
322
+
323
+ it('should handle truncate-clip overflow', () => {
324
+ jest.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
325
+ spans: [
326
+ {
327
+ text: 'Hello Wo',
328
+ box: { x: 0, y: 0, w: 80, h: 16 },
329
+ },
330
+ ],
331
+ didTruncate: true,
332
+ })
333
+
334
+ const opts = { ...defaultOpts, overflow: 'truncate-clip' as const }
335
+ const result = textManager.measureTextSpans('Hello World', opts)
336
+
337
+ expect(Array.isArray(result)).toBe(true)
338
+ })
339
+
340
+ it('should handle different text alignments', () => {
341
+ jest.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
342
+ spans: [
343
+ {
344
+ text: 'Test',
345
+ box: { x: 0, y: 0, w: 40, h: 16 },
346
+ },
347
+ ],
348
+ didTruncate: false,
349
+ })
350
+
351
+ const alignments: Array<TLMeasureTextSpanOpts['textAlign']> = ['start', 'middle', 'end']
352
+
353
+ alignments.forEach((textAlign) => {
354
+ const opts = { ...defaultOpts, textAlign }
355
+ const result = textManager.measureTextSpans('Test', opts)
356
+ expect(Array.isArray(result)).toBe(true)
357
+ })
358
+ })
359
+
360
+ it('should handle custom font properties', () => {
361
+ jest.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
362
+ spans: [
363
+ {
364
+ text: 'Test',
365
+ box: { x: 0, y: 0, w: 40, h: 16 },
366
+ },
367
+ ],
368
+ didTruncate: false,
369
+ })
370
+
371
+ const opts = {
372
+ ...defaultOpts,
373
+ fontSize: 18,
374
+ fontFamily: 'Times',
375
+ fontWeight: 'bold',
376
+ fontStyle: 'italic',
377
+ lineHeight: 1.5,
378
+ }
379
+
380
+ const result = textManager.measureTextSpans('Test', opts)
381
+ expect(Array.isArray(result)).toBe(true)
382
+ })
383
+
384
+ it('should handle other styles', () => {
385
+ jest.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
386
+ spans: [
387
+ {
388
+ text: 'Test',
389
+ box: { x: 0, y: 0, w: 40, h: 16 },
390
+ },
391
+ ],
392
+ didTruncate: false,
393
+ })
394
+
395
+ const opts = {
396
+ ...defaultOpts,
397
+ otherStyles: {
398
+ 'text-shadow': '1px 1px 1px black',
399
+ 'letter-spacing': '1px',
400
+ },
401
+ }
402
+
403
+ const result = textManager.measureTextSpans('Test', opts)
404
+ expect(Array.isArray(result)).toBe(true)
405
+ })
406
+ })
407
+ })
@@ -1,5 +1,5 @@
1
1
  import { BoxModel, TLDefaultHorizontalAlignStyle } from '@tldraw/tlschema'
2
- import { Editor } from '../Editor'
2
+ import { Editor } from '../../Editor'
3
3
 
4
4
  const fixNewLines = /\r?\n|\r/g
5
5
 
@@ -20,6 +20,27 @@ const textAlignmentsForLtr = {
20
20
  'end-legacy': 'right',
21
21
  }
22
22
 
23
+ /** @public */
24
+ export interface TLMeasureTextOpts {
25
+ fontStyle: string
26
+ fontWeight: string
27
+ fontFamily: string
28
+ fontSize: number
29
+ lineHeight: number
30
+ /**
31
+ * When maxWidth is a number, the text will be wrapped to that maxWidth. When maxWidth
32
+ * is null, the text will be measured without wrapping, but explicit line breaks and
33
+ * space are preserved.
34
+ */
35
+ maxWidth: null | number
36
+ minWidth?: null | number
37
+ // todo: make this a number so that it is consistent with other TLMeasureTextSpanOpts
38
+ padding: string
39
+ otherStyles?: Record<string, string>
40
+ disableOverflowWrapBreaking?: boolean
41
+ measureScrollWidth?: boolean
42
+ }
43
+
23
44
  /** @public */
24
45
  export interface TLMeasureTextSpanOpts {
25
46
  overflow: 'wrap' | 'truncate-ellipsis' | 'truncate-clip'
@@ -33,96 +54,99 @@ export interface TLMeasureTextSpanOpts {
33
54
  lineHeight: number
34
55
  textAlign: TLDefaultHorizontalAlignStyle
35
56
  otherStyles?: Record<string, string>
57
+ measureScrollWidth?: boolean
36
58
  }
37
59
 
38
60
  const spaceCharacterRegex = /\s/
39
61
 
40
62
  /** @public */
41
63
  export class TextManager {
42
- private baseElem: HTMLDivElement
64
+ private elm: HTMLDivElement
65
+ private defaultStyles: Record<string, string | null>
43
66
 
44
67
  constructor(public editor: Editor) {
45
- this.baseElem = document.createElement('div')
46
- this.baseElem.classList.add('tl-text')
47
- this.baseElem.classList.add('tl-text-measure')
48
- this.baseElem.tabIndex = -1
68
+ const elm = document.createElement('div')
69
+ elm.classList.add('tl-text')
70
+ elm.classList.add('tl-text-measure')
71
+ elm.setAttribute('dir', 'auto')
72
+ elm.tabIndex = -1
73
+ this.editor.getContainer().appendChild(elm)
74
+
75
+ // we need to save the default styles so that we can restore them when we're done
76
+ // these must be the css names, not the js names for the styles
77
+ this.defaultStyles = {
78
+ 'overflow-wrap': 'break-word',
79
+ 'word-break': 'auto',
80
+ width: null,
81
+ height: null,
82
+ 'max-width': null,
83
+ 'min-width': null,
84
+ }
85
+
86
+ this.elm = elm
49
87
  }
50
88
 
51
- measureText(
52
- textToMeasure: string,
53
- opts: {
54
- fontStyle: string
55
- fontWeight: string
56
- fontFamily: string
57
- fontSize: number
58
- lineHeight: number
59
- /**
60
- * When maxWidth is a number, the text will be wrapped to that maxWidth. When maxWidth
61
- * is null, the text will be measured without wrapping, but explicit line breaks and
62
- * space are preserved.
63
- */
64
- maxWidth: null | number
65
- minWidth?: null | number
66
- padding: string
67
- disableOverflowWrapBreaking?: boolean
89
+ dispose() {
90
+ return this.elm.remove()
91
+ }
92
+
93
+ private resetElmStyles() {
94
+ const { elm, defaultStyles } = this
95
+ for (const key in defaultStyles) {
96
+ elm.style.setProperty(key, defaultStyles[key])
68
97
  }
69
- ): BoxModel & { scrollWidth: number } {
98
+ }
99
+
100
+ measureText(textToMeasure: string, opts: TLMeasureTextOpts): BoxModel & { scrollWidth: number } {
70
101
  const div = document.createElement('div')
71
102
  div.textContent = normalizeTextForDom(textToMeasure)
72
103
  return this.measureHtml(div.innerHTML, opts)
73
104
  }
74
105
 
75
- measureHtml(
76
- html: string,
77
- opts: {
78
- fontStyle: string
79
- fontWeight: string
80
- fontFamily: string
81
- fontSize: number
82
- lineHeight: number
83
- /**
84
- * When maxWidth is a number, the text will be wrapped to that maxWidth. When maxWidth
85
- * is null, the text will be measured without wrapping, but explicit line breaks and
86
- * space are preserved.
87
- */
88
- maxWidth: null | number
89
- minWidth?: null | number
90
- otherStyles?: Record<string, string>
91
- padding: string
92
- disableOverflowWrapBreaking?: boolean
106
+ measureHtml(html: string, opts: TLMeasureTextOpts): BoxModel & { scrollWidth: number } {
107
+ const { elm } = this
108
+
109
+ if (opts.otherStyles) {
110
+ for (const key in opts.otherStyles) {
111
+ if (!this.defaultStyles[key]) {
112
+ // we need to save the original style so that we can restore it when we're done
113
+ this.defaultStyles[key] = elm.style.getPropertyValue(key)
114
+ }
115
+ }
93
116
  }
94
- ): BoxModel & { scrollWidth: number } {
95
- // Duplicate our base element; we don't need to clone deep
96
- const wrapperElm = this.baseElem.cloneNode() as HTMLDivElement
97
- this.editor.getContainer().appendChild(wrapperElm)
98
- wrapperElm.innerHTML = html
99
- this.baseElem.insertAdjacentElement('afterend', wrapperElm)
100
-
101
- wrapperElm.setAttribute('dir', 'auto')
102
- // N.B. This property, while discouraged ("intended for Document Type Definition (DTD) designers")
103
- // is necessary for ensuring correct mixed RTL/LTR behavior when exporting SVGs.
104
- wrapperElm.style.setProperty('unicode-bidi', 'plaintext')
105
- wrapperElm.style.setProperty('font-family', opts.fontFamily)
106
- wrapperElm.style.setProperty('font-style', opts.fontStyle)
107
- wrapperElm.style.setProperty('font-weight', opts.fontWeight)
108
- wrapperElm.style.setProperty('font-size', opts.fontSize + 'px')
109
- wrapperElm.style.setProperty('line-height', opts.lineHeight * opts.fontSize + 'px')
110
- wrapperElm.style.setProperty('max-width', opts.maxWidth === null ? null : opts.maxWidth + 'px')
111
- wrapperElm.style.setProperty('min-width', opts.minWidth === null ? null : opts.minWidth + 'px')
112
- wrapperElm.style.setProperty('padding', opts.padding)
113
- wrapperElm.style.setProperty(
114
- 'overflow-wrap',
115
- opts.disableOverflowWrapBreaking ? 'normal' : 'break-word'
116
- )
117
+
118
+ elm.innerHTML = html
119
+
120
+ // Apply the default styles to the element (for all styles here or that were ever seen in opts.otherStyles)
121
+ this.resetElmStyles()
122
+
123
+ elm.style.setProperty('font-family', opts.fontFamily)
124
+ elm.style.setProperty('font-style', opts.fontStyle)
125
+ elm.style.setProperty('font-weight', opts.fontWeight)
126
+ elm.style.setProperty('font-size', opts.fontSize + 'px')
127
+ elm.style.setProperty('line-height', opts.lineHeight * opts.fontSize + 'px')
128
+ elm.style.setProperty('padding', opts.padding)
129
+
130
+ if (opts.maxWidth) {
131
+ elm.style.setProperty('max-width', opts.maxWidth + 'px')
132
+ }
133
+
134
+ if (opts.minWidth) {
135
+ elm.style.setProperty('min-width', opts.minWidth + 'px')
136
+ }
137
+
138
+ if (opts.disableOverflowWrapBreaking) {
139
+ elm.style.setProperty('overflow-wrap', 'normal')
140
+ }
141
+
117
142
  if (opts.otherStyles) {
118
143
  for (const [key, value] of Object.entries(opts.otherStyles)) {
119
- wrapperElm.style.setProperty(key, value)
144
+ elm.style.setProperty(key, value)
120
145
  }
121
146
  }
122
147
 
123
- const scrollWidth = wrapperElm.scrollWidth
124
- const rect = wrapperElm.getBoundingClientRect()
125
- wrapperElm.remove()
148
+ const scrollWidth = opts.measureScrollWidth ? elm.scrollWidth : 0
149
+ const rect = elm.getBoundingClientRect()
126
150
 
127
151
  return {
128
152
  x: 0,
@@ -247,27 +271,29 @@ export class TextManager {
247
271
  ): { text: string; box: BoxModel }[] {
248
272
  if (textToMeasure === '') return []
249
273
 
250
- const elm = this.baseElem.cloneNode() as HTMLDivElement
251
- this.editor.getContainer().appendChild(elm)
274
+ const { elm } = this
275
+
276
+ if (opts.otherStyles) {
277
+ for (const key in opts.otherStyles) {
278
+ if (!this.defaultStyles[key]) {
279
+ // we need to save the original style so that we can restore it when we're done
280
+ this.defaultStyles[key] = elm.style.getPropertyValue(key)
281
+ }
282
+ }
283
+ }
284
+
285
+ this.resetElmStyles()
286
+
287
+ elm.style.setProperty('font-family', opts.fontFamily)
288
+ elm.style.setProperty('font-style', opts.fontStyle)
289
+ elm.style.setProperty('font-weight', opts.fontWeight)
290
+ elm.style.setProperty('font-size', opts.fontSize + 'px')
291
+ elm.style.setProperty('line-height', opts.lineHeight * opts.fontSize + 'px')
252
292
 
253
293
  const elementWidth = Math.ceil(opts.width - opts.padding * 2)
254
- elm.setAttribute('dir', 'auto')
255
- // N.B. This property, while discouraged ("intended for Document Type Definition (DTD) designers")
256
- // is necessary for ensuring correct mixed RTL/LTR behavior when exporting SVGs.
257
- elm.style.setProperty('unicode-bidi', 'plaintext')
258
294
  elm.style.setProperty('width', `${elementWidth}px`)
259
295
  elm.style.setProperty('height', 'min-content')
260
- elm.style.setProperty('font-size', `${opts.fontSize}px`)
261
- elm.style.setProperty('font-family', opts.fontFamily)
262
- elm.style.setProperty('font-weight', opts.fontWeight)
263
- elm.style.setProperty('line-height', `${opts.lineHeight * opts.fontSize}px`)
264
296
  elm.style.setProperty('text-align', textAlignmentsForLtr[opts.textAlign])
265
- elm.style.setProperty('font-style', opts.fontStyle)
266
- if (opts.otherStyles) {
267
- for (const [key, value] of Object.entries(opts.otherStyles)) {
268
- elm.style.setProperty(key, value)
269
- }
270
- }
271
297
 
272
298
  const shouldTruncateToFirstLine =
273
299
  opts.overflow === 'truncate-ellipsis' || opts.overflow === 'truncate-clip'
@@ -277,6 +303,12 @@ export class TextManager {
277
303
  elm.style.setProperty('word-break', 'break-all')
278
304
  }
279
305
 
306
+ if (opts.otherStyles) {
307
+ for (const [key, value] of Object.entries(opts.otherStyles)) {
308
+ elm.style.setProperty(key, value)
309
+ }
310
+ }
311
+
280
312
  const normalizedText = normalizeTextForDom(textToMeasure)
281
313
 
282
314
  // Render the text into the measurement element:
@@ -313,11 +345,10 @@ export class TextManager {
313
345
  h: lastSpan.box.h,
314
346
  },
315
347
  })
348
+
316
349
  return truncatedSpans
317
350
  }
318
351
 
319
- elm.remove()
320
-
321
352
  return spans
322
353
  }
323
354
  }