tldraw 5.6.1 → 5.6.2
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/DOCS.md +20 -9
- package/RELEASE_NOTES.md +60 -5
- package/dist-cjs/index.d.ts +31 -1
- package/dist-cjs/index.js +4 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/shapes/bookmark/BookmarkShapeUtil.js +6 -0
- package/dist-cjs/lib/shapes/bookmark/BookmarkShapeUtil.js.map +2 -2
- package/dist-cjs/lib/shapes/shared/HyperlinkButton.js +4 -0
- package/dist-cjs/lib/shapes/shared/HyperlinkButton.js.map +2 -2
- package/dist-cjs/lib/shapes/shared/RichTextLabel.js +13 -1
- package/dist-cjs/lib/shapes/shared/RichTextLabel.js.map +2 -2
- package/dist-cjs/lib/shapes/shared/useOpenLinkHandler.js +38 -0
- package/dist-cjs/lib/shapes/shared/useOpenLinkHandler.js.map +7 -0
- package/dist-cjs/lib/ui/components/ConfirmOpenLinkDialog.js +57 -0
- package/dist-cjs/lib/ui/components/ConfirmOpenLinkDialog.js.map +7 -0
- package/dist-cjs/lib/ui/components/Toolbar/LinkEditor.js +2 -1
- package/dist-cjs/lib/ui/components/Toolbar/LinkEditor.js.map +2 -2
- package/dist-cjs/lib/ui/context/actions.js +2 -1
- package/dist-cjs/lib/ui/context/actions.js.map +2 -2
- package/dist-cjs/lib/ui/hooks/useEditorEvents.js +12 -1
- package/dist-cjs/lib/ui/hooks/useEditorEvents.js.map +2 -2
- package/dist-cjs/lib/ui/hooks/useTranslation/TLUiTranslationKey.js.map +1 -1
- package/dist-cjs/lib/ui/version.js +2 -2
- package/dist-cjs/lib/ui/version.js.map +1 -1
- package/dist-cjs/lib/utils/openLink.js +68 -0
- package/dist-cjs/lib/utils/openLink.js.map +7 -0
- package/dist-esm/index.d.mts +31 -1
- package/dist-esm/index.mjs +4 -1
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/shapes/bookmark/BookmarkShapeUtil.mjs +6 -0
- package/dist-esm/lib/shapes/bookmark/BookmarkShapeUtil.mjs.map +2 -2
- package/dist-esm/lib/shapes/shared/HyperlinkButton.mjs +4 -0
- package/dist-esm/lib/shapes/shared/HyperlinkButton.mjs.map +2 -2
- package/dist-esm/lib/shapes/shared/RichTextLabel.mjs +13 -2
- package/dist-esm/lib/shapes/shared/RichTextLabel.mjs.map +2 -2
- package/dist-esm/lib/shapes/shared/useOpenLinkHandler.mjs +18 -0
- package/dist-esm/lib/shapes/shared/useOpenLinkHandler.mjs.map +7 -0
- package/dist-esm/lib/ui/components/ConfirmOpenLinkDialog.mjs +43 -0
- package/dist-esm/lib/ui/components/ConfirmOpenLinkDialog.mjs.map +7 -0
- package/dist-esm/lib/ui/components/Toolbar/LinkEditor.mjs +3 -2
- package/dist-esm/lib/ui/components/Toolbar/LinkEditor.mjs.map +2 -2
- package/dist-esm/lib/ui/context/actions.mjs +2 -1
- package/dist-esm/lib/ui/context/actions.mjs.map +2 -2
- package/dist-esm/lib/ui/hooks/useEditorEvents.mjs +13 -2
- package/dist-esm/lib/ui/hooks/useEditorEvents.mjs.map +2 -2
- package/dist-esm/lib/ui/version.mjs +2 -2
- package/dist-esm/lib/ui/version.mjs.map +1 -1
- package/dist-esm/lib/utils/openLink.mjs +48 -0
- package/dist-esm/lib/utils/openLink.mjs.map +7 -0
- package/package.json +6 -6
- package/src/index.ts +1 -0
- package/src/lib/shapes/bookmark/BookmarkShapeUtil.tsx +6 -0
- package/src/lib/shapes/shared/HyperlinkButton.tsx +4 -0
- package/src/lib/shapes/shared/RichTextLabel.tsx +18 -2
- package/src/lib/shapes/shared/useOpenLinkHandler.ts +20 -0
- package/src/lib/ui/components/ConfirmOpenLinkDialog.tsx +53 -0
- package/src/lib/ui/components/Toolbar/LinkEditor.tsx +3 -2
- package/src/lib/ui/context/actions.tsx +2 -1
- package/src/lib/ui/hooks/useEditorEvents.ts +14 -2
- package/src/lib/ui/hooks/useTranslation/TLUiTranslationKey.ts +4 -0
- package/src/lib/ui/version.ts +2 -2
- package/src/lib/ui.css +10 -0
- package/src/lib/utils/openLink.test.ts +83 -0
- package/src/lib/utils/openLink.ts +75 -0
- package/src/test/ui/ConfirmOpenLinkDialog.test.tsx +128 -0
- package/tldraw.css +10 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
|
|
2
|
+
import { createShapeId } from '@tldraw/editor'
|
|
3
|
+
import { Tldraw } from '../../lib/Tldraw'
|
|
4
|
+
import { renderTldrawComponentWithEditor } from '../testutils/renderTldrawComponent'
|
|
5
|
+
|
|
6
|
+
const shapeId = createShapeId('box')
|
|
7
|
+
|
|
8
|
+
let open: ReturnType<typeof vi.spyOn>
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
open = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
open.mockRestore()
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
async function renderWithLink(url: string) {
|
|
19
|
+
const { editor } = await renderTldrawComponentWithEditor(
|
|
20
|
+
(onMount) => <Tldraw onMount={onMount} />,
|
|
21
|
+
{ waitForPatterns: false }
|
|
22
|
+
)
|
|
23
|
+
act(() => {
|
|
24
|
+
editor.createShape({ id: shapeId, type: 'geo', props: { url } })
|
|
25
|
+
})
|
|
26
|
+
return editor
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getHyperlinkButton() {
|
|
30
|
+
return document.querySelector('.tl-hyperlink-button') as HTMLAnchorElement
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getDialogFrame() {
|
|
34
|
+
return document.querySelector('.tlui-dialog__content')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('opening a shape link', () => {
|
|
38
|
+
it('opens a trusted app link without asking', async () => {
|
|
39
|
+
await renderWithLink('vscode://file/src/index.ts')
|
|
40
|
+
fireEvent.click(getHyperlinkButton())
|
|
41
|
+
expect(open).toHaveBeenCalledWith('vscode://file/src/index.ts', '_blank', 'noopener noreferrer')
|
|
42
|
+
expect(getDialogFrame()).toBeNull()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('checks a middle click too, and ignores a right click', async () => {
|
|
46
|
+
await renderWithLink('chatgpt://chat/123')
|
|
47
|
+
fireEvent(getHyperlinkButton(), new MouseEvent('auxclick', { bubbles: true, button: 2 }))
|
|
48
|
+
expect(getDialogFrame()).toBeNull()
|
|
49
|
+
fireEvent(getHyperlinkButton(), new MouseEvent('auxclick', { bubbles: true, button: 1 }))
|
|
50
|
+
await waitFor(() => expect(getDialogFrame()).not.toBeNull())
|
|
51
|
+
expect(open).not.toHaveBeenCalled()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('asks before opening an unknown app link, and opens it on confirm', async () => {
|
|
55
|
+
await renderWithLink('chatgpt://chat/123')
|
|
56
|
+
fireEvent.click(getHyperlinkButton())
|
|
57
|
+
await waitFor(() => expect(getDialogFrame()).not.toBeNull())
|
|
58
|
+
expect(screen.getByText('chatgpt://chat/123')).toBeTruthy()
|
|
59
|
+
expect(open).not.toHaveBeenCalled()
|
|
60
|
+
|
|
61
|
+
fireEvent.click(screen.getByText('Open link'))
|
|
62
|
+
expect(open).toHaveBeenCalledWith('chatgpt://chat/123', '_blank', 'noopener noreferrer')
|
|
63
|
+
await waitFor(() => expect(getDialogFrame()).toBeNull())
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('in rich text', () => {
|
|
67
|
+
async function renderWithRichTextLink(href: string) {
|
|
68
|
+
const editor = await renderWithLink('')
|
|
69
|
+
act(() => {
|
|
70
|
+
editor.updateShape({
|
|
71
|
+
id: shapeId,
|
|
72
|
+
type: 'geo',
|
|
73
|
+
props: {
|
|
74
|
+
url: '',
|
|
75
|
+
richText: {
|
|
76
|
+
type: 'doc',
|
|
77
|
+
content: [
|
|
78
|
+
{
|
|
79
|
+
type: 'paragraph',
|
|
80
|
+
content: [
|
|
81
|
+
{ type: 'text', text: 'link', marks: [{ type: 'link', attrs: { href } }] },
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
const anchor = document.querySelector('.tl-rich-text a') as HTMLAnchorElement
|
|
90
|
+
expect(anchor).not.toBeNull()
|
|
91
|
+
return anchor
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Enter on a focused link fires a click with detail 0.
|
|
95
|
+
it('asks before following a keyboard click on an unknown app link', async () => {
|
|
96
|
+
const anchor = await renderWithRichTextLink('chatgpt://chat/123')
|
|
97
|
+
const click = new MouseEvent('click', { bubbles: true, cancelable: true, detail: 0 })
|
|
98
|
+
fireEvent(anchor, click)
|
|
99
|
+
expect(click.defaultPrevented).toBe(true)
|
|
100
|
+
await waitFor(() => expect(getDialogFrame()).not.toBeNull())
|
|
101
|
+
expect(open).not.toHaveBeenCalled()
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('opens a keyboard click on a trusted link', async () => {
|
|
105
|
+
const anchor = await renderWithRichTextLink('https://tldraw.com')
|
|
106
|
+
fireEvent(anchor, new MouseEvent('click', { bubbles: true, cancelable: true, detail: 0 }))
|
|
107
|
+
expect(open).toHaveBeenCalledWith('https://tldraw.com', '_blank', 'noopener noreferrer')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('does not open a mouse click twice', async () => {
|
|
111
|
+
const anchor = await renderWithRichTextLink('https://tldraw.com')
|
|
112
|
+
const click = new MouseEvent('click', { bubbles: true, cancelable: true, detail: 1 })
|
|
113
|
+
fireEvent(anchor, click)
|
|
114
|
+
expect(click.defaultPrevented).toBe(true)
|
|
115
|
+
expect(open).not.toHaveBeenCalled()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('does not open an unknown app link when the dialog is cancelled', async () => {
|
|
120
|
+
await renderWithLink('chatgpt://chat/123')
|
|
121
|
+
fireEvent.click(getHyperlinkButton())
|
|
122
|
+
await waitFor(() => expect(getDialogFrame()).not.toBeNull())
|
|
123
|
+
|
|
124
|
+
fireEvent.click(screen.getByText('Cancel'))
|
|
125
|
+
await waitFor(() => expect(getDialogFrame()).toBeNull())
|
|
126
|
+
expect(open).not.toHaveBeenCalled()
|
|
127
|
+
})
|
|
128
|
+
})
|
package/tldraw.css
CHANGED
|
@@ -3454,6 +3454,16 @@ tldraw? probably.
|
|
|
3454
3454
|
padding: 0px var(--tl-space-4);
|
|
3455
3455
|
}
|
|
3456
3456
|
|
|
3457
|
+
/* Confirm Open Link Dialog */
|
|
3458
|
+
|
|
3459
|
+
.tlui-confirm-open-link-dialog__url {
|
|
3460
|
+
margin-top: var(--tl-space-4);
|
|
3461
|
+
padding: var(--tl-space-3) var(--tl-space-4);
|
|
3462
|
+
border-radius: var(--tl-radius-2);
|
|
3463
|
+
background-color: var(--tl-color-muted-2);
|
|
3464
|
+
font-family: var(--tl-font-mono);
|
|
3465
|
+
}
|
|
3466
|
+
|
|
3457
3467
|
/* Embed Dialog */
|
|
3458
3468
|
|
|
3459
3469
|
.tlui-embed__spacer {
|