tldraw 4.1.0-canary.62b1976714aa → 4.1.0-canary.63a4004dfd71

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 (50) hide show
  1. package/dist-cjs/index.d.ts +15 -11
  2. package/dist-cjs/index.js +3 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/defaultEmbedDefinitions.js +0 -24
  5. package/dist-cjs/lib/defaultEmbedDefinitions.js.map +2 -2
  6. package/dist-cjs/lib/defaultExternalContentHandlers.js +8 -31
  7. package/dist-cjs/lib/defaultExternalContentHandlers.js.map +2 -2
  8. package/dist-cjs/lib/shapes/bookmark/BookmarkShapeUtil.js +31 -101
  9. package/dist-cjs/lib/shapes/bookmark/BookmarkShapeUtil.js.map +2 -2
  10. package/dist-cjs/lib/shapes/bookmark/bookmarks.js +138 -0
  11. package/dist-cjs/lib/shapes/bookmark/bookmarks.js.map +7 -0
  12. package/dist-cjs/lib/shapes/embed/EmbedShapeUtil.js +25 -3
  13. package/dist-cjs/lib/shapes/embed/EmbedShapeUtil.js.map +2 -2
  14. package/dist-cjs/lib/ui/components/StylePanel/StylePanelButtonPicker.js +1 -1
  15. package/dist-cjs/lib/ui/components/StylePanel/StylePanelButtonPicker.js.map +1 -1
  16. package/dist-cjs/lib/ui/context/actions.js +23 -29
  17. package/dist-cjs/lib/ui/context/actions.js.map +2 -2
  18. package/dist-cjs/lib/ui/version.js +3 -3
  19. package/dist-cjs/lib/ui/version.js.map +1 -1
  20. package/dist-esm/index.d.mts +15 -11
  21. package/dist-esm/index.mjs +3 -1
  22. package/dist-esm/index.mjs.map +2 -2
  23. package/dist-esm/lib/defaultEmbedDefinitions.mjs +0 -24
  24. package/dist-esm/lib/defaultEmbedDefinitions.mjs.map +2 -2
  25. package/dist-esm/lib/defaultExternalContentHandlers.mjs +8 -31
  26. package/dist-esm/lib/defaultExternalContentHandlers.mjs.map +2 -2
  27. package/dist-esm/lib/shapes/bookmark/BookmarkShapeUtil.mjs +34 -101
  28. package/dist-esm/lib/shapes/bookmark/BookmarkShapeUtil.mjs.map +2 -2
  29. package/dist-esm/lib/shapes/bookmark/bookmarks.mjs +124 -0
  30. package/dist-esm/lib/shapes/bookmark/bookmarks.mjs.map +7 -0
  31. package/dist-esm/lib/shapes/embed/EmbedShapeUtil.mjs +26 -3
  32. package/dist-esm/lib/shapes/embed/EmbedShapeUtil.mjs.map +2 -2
  33. package/dist-esm/lib/ui/components/StylePanel/StylePanelButtonPicker.mjs +1 -1
  34. package/dist-esm/lib/ui/components/StylePanel/StylePanelButtonPicker.mjs.map +1 -1
  35. package/dist-esm/lib/ui/context/actions.mjs +23 -29
  36. package/dist-esm/lib/ui/context/actions.mjs.map +2 -2
  37. package/dist-esm/lib/ui/version.mjs +3 -3
  38. package/dist-esm/lib/ui/version.mjs.map +1 -1
  39. package/package.json +3 -3
  40. package/src/index.ts +1 -0
  41. package/src/lib/defaultEmbedDefinitions.ts +0 -24
  42. package/src/lib/defaultExternalContentHandlers.ts +10 -35
  43. package/src/lib/shapes/bookmark/BookmarkShapeUtil.tsx +39 -133
  44. package/src/lib/shapes/bookmark/bookmarks.ts +170 -0
  45. package/src/lib/shapes/embed/EmbedShapeUtil.tsx +28 -2
  46. package/src/lib/ui/components/StylePanel/StylePanelButtonPicker.tsx +1 -1
  47. package/src/lib/ui/context/actions.tsx +27 -31
  48. package/src/lib/ui/version.ts +3 -3
  49. package/src/lib/utils/embeds/embeds.test.ts +0 -34
  50. package/src/test/bookmark-shapes.test.ts +129 -7
@@ -355,23 +355,6 @@ const MATCH_URL_TEST_URLS: (MatchUrlTestNoMatchDef | MatchUrlTestMatchDef)[] = [
355
355
  url: 'https://vimeo.com/foobar',
356
356
  match: false,
357
357
  },
358
- // excalidraw
359
- {
360
- url: 'https://excalidraw.com/#room=asdkjashdkjhaskdjh,sadkjhakjshdkjahd',
361
- match: true,
362
- output: {
363
- type: 'excalidraw',
364
- embedUrl: `https://excalidraw.com/#room=asdkjashdkjhaskdjh,sadkjhakjshdkjahd`,
365
- },
366
- },
367
- {
368
- url: 'https://excalidraw.com',
369
- match: false,
370
- },
371
- {
372
- url: 'https://excalidraw.com/help',
373
- match: false,
374
- },
375
358
  //desmos
376
359
  {
377
360
  url: 'https://www.desmos.com/calculator/js9hryvejc',
@@ -687,23 +670,6 @@ const MATCH_EMBED_TEST_URLS: (MatchEmbedTestMatchDef | MatchEmbedTestNoMatchDef)
687
670
  embedUrl: 'https://vimeo.com/foobar',
688
671
  match: false,
689
672
  },
690
- // excalidraw
691
- {
692
- embedUrl: 'https://excalidraw.com/#room=asdkjashdkjhaskdjh,sadkjhakjshdkjahd',
693
- match: true,
694
- output: {
695
- type: 'excalidraw',
696
- url: `https://excalidraw.com/#room=asdkjashdkjhaskdjh,sadkjhakjshdkjahd`,
697
- },
698
- },
699
- {
700
- embedUrl: 'https://excalidraw.com',
701
- match: false,
702
- },
703
- {
704
- embedUrl: 'https://excalidraw.com/help',
705
- match: false,
706
- },
707
673
  // desmos
708
674
  {
709
675
  embedUrl: 'https://www.desmos.com/calculator/js9hryvejc?embed',
@@ -1,5 +1,6 @@
1
1
  import { TLBookmarkShape, createShapeId } from '@tldraw/editor'
2
- import { getHumanReadableAddress } from '../lib/shapes/bookmark/BookmarkShapeUtil'
2
+ import { vi } from 'vitest'
3
+ import { createBookmarkFromUrl, getHumanReadableAddress } from '../lib/shapes/bookmark/bookmarks'
3
4
  import { TestEditor } from './TestEditor'
4
5
 
5
6
  let editor: TestEditor
@@ -74,12 +75,12 @@ describe('The URL formatter', () => {
74
75
  const e = editor.getShape<TLBookmarkShape>(ids.e)!
75
76
  const f = editor.getShape<TLBookmarkShape>(ids.f)!
76
77
 
77
- expect(getHumanReadableAddress(a)).toBe('github.com')
78
- expect(getHumanReadableAddress(b)).toBe('github.com')
79
- expect(getHumanReadableAddress(c)).toBe('github.com')
80
- expect(getHumanReadableAddress(d)).toBe('github.com')
81
- expect(getHumanReadableAddress(e)).toBe('github.com')
82
- expect(getHumanReadableAddress(f)).toBe('github.com')
78
+ expect(getHumanReadableAddress(a.props.url)).toBe('github.com')
79
+ expect(getHumanReadableAddress(b.props.url)).toBe('github.com')
80
+ expect(getHumanReadableAddress(c.props.url)).toBe('github.com')
81
+ expect(getHumanReadableAddress(d.props.url)).toBe('github.com')
82
+ expect(getHumanReadableAddress(e.props.url)).toBe('github.com')
83
+ expect(getHumanReadableAddress(f.props.url)).toBe('github.com')
83
84
  })
84
85
 
85
86
  it("Doesn't resize bookmarks", () => {
@@ -132,3 +133,124 @@ describe('The URL formatter', () => {
132
133
  expect(newBookmark.props.h).toBe(320)
133
134
  })
134
135
  })
136
+
137
+ describe('createBookmarkFromUrl', () => {
138
+ it('creates a bookmark shape with unfurled metadata', async () => {
139
+ const url = 'https://example.com'
140
+ const center = { x: 100, y: 200 }
141
+
142
+ // Mock the asset creation to return a test asset
143
+ const mockAsset = {
144
+ id: 'asset:test-asset-id' as any,
145
+ typeName: 'asset' as const,
146
+ type: 'bookmark' as const,
147
+ props: {
148
+ src: url,
149
+ title: 'Example Site',
150
+ description: 'An example website',
151
+ image: 'https://example.com/image.jpg',
152
+ favicon: 'https://example.com/favicon.ico',
153
+ },
154
+ meta: {},
155
+ }
156
+
157
+ // Mock the getAssetForExternalContent method
158
+ vi.spyOn(editor, 'getAssetForExternalContent').mockResolvedValue(mockAsset)
159
+
160
+ const result = await createBookmarkFromUrl(editor, { url, center })
161
+
162
+ assert(result.ok, 'Failed to create bookmark')
163
+ const shape = result.value
164
+ expect(shape.type).toBe('bookmark')
165
+ expect(shape.props.url).toBe(url)
166
+ expect(shape.props.assetId).toBe('asset:test-asset-id')
167
+ expect(shape.props.w).toBe(300)
168
+ expect(shape.props.h).toBe(320)
169
+ expect(shape.x).toBe(center.x - 150) // BOOKMARK_WIDTH / 2
170
+ expect(shape.y).toBe(center.y - 160) // BOOKMARK_HEIGHT / 2
171
+
172
+ // Verify the shape was created in the editor
173
+ const createdShape = editor.getShape(result.value.id)
174
+ expect(createdShape).toBeDefined()
175
+ expect(createdShape?.type).toBe('bookmark')
176
+
177
+ // Verify the asset was created
178
+ const createdAsset = editor.getAsset('asset:test-asset-id' as any)
179
+ expect(createdAsset).toBeDefined()
180
+ expect(createdAsset?.type).toBe('bookmark')
181
+ })
182
+
183
+ it('creates a bookmark shape with default center when no center provided', async () => {
184
+ const url = 'https://example.com'
185
+ const viewportCenter = { x: 500, y: 300 }
186
+
187
+ // Mock getViewportPageBounds to return a known center
188
+ vi.spyOn(editor, 'getViewportPageBounds').mockReturnValue({
189
+ x: 0,
190
+ y: 0,
191
+ w: 1000,
192
+ h: 600,
193
+ center: viewportCenter,
194
+ } as any)
195
+
196
+ const mockAsset = {
197
+ id: 'asset:test-asset-id' as any,
198
+ typeName: 'asset' as const,
199
+ type: 'bookmark' as const,
200
+ props: {
201
+ src: url,
202
+ title: 'Example Site',
203
+ description: 'An example website',
204
+ image: '',
205
+ favicon: '',
206
+ },
207
+ meta: {},
208
+ }
209
+
210
+ vi.spyOn(editor, 'getAssetForExternalContent').mockResolvedValue(mockAsset)
211
+
212
+ const result = await createBookmarkFromUrl(editor, { url })
213
+
214
+ assert(result.ok, 'Failed to create bookmark')
215
+ const shape = result.value
216
+ expect(shape.x).toBe(viewportCenter.x - 150)
217
+ expect(shape.y).toBe(viewportCenter.y - 160)
218
+ })
219
+
220
+ it('handles asset creation failure gracefully', async () => {
221
+ const url = 'https://invalid-url.com'
222
+ const center = { x: 100, y: 200 }
223
+
224
+ // Mock the asset creation to fail
225
+ vi.spyOn(editor, 'getAssetForExternalContent').mockRejectedValue(new Error('Failed to fetch'))
226
+
227
+ const result = await createBookmarkFromUrl(editor, { url, center })
228
+
229
+ assert(!result.ok, 'Failed to create bookmark')
230
+ expect(result.error).toBe('Failed to fetch')
231
+
232
+ // Verify no shape was created
233
+ const shapes = editor.getCurrentPageShapes()
234
+ expect(shapes).toHaveLength(0)
235
+ })
236
+
237
+ it('creates bookmark shape even when asset creation returns null', async () => {
238
+ const url = 'https://example.com'
239
+ const center = { x: 100, y: 200 }
240
+
241
+ // Mock the asset creation to return null
242
+ vi.spyOn(editor, 'getAssetForExternalContent').mockResolvedValue(null as any)
243
+
244
+ const result = await createBookmarkFromUrl(editor, { url, center })
245
+
246
+ assert(result.ok, 'Failed to create bookmark')
247
+ const shape = result.value
248
+ expect(shape.type).toBe('bookmark')
249
+ expect(shape.props.url).toBe(url)
250
+ expect(shape.props.assetId).toBe(null)
251
+
252
+ // Verify the shape was created
253
+ const createdShape = editor.getShape(result.value.id)
254
+ expect(createdShape).toBeDefined()
255
+ })
256
+ })