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
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { useEditor } from "@tldraw/editor";
|
|
2
|
-
import { useEffect } from "react";
|
|
2
|
+
import { createElement, useEffect } from "react";
|
|
3
|
+
import { ConfirmOpenLinkDialog } from "../components/ConfirmOpenLinkDialog.mjs";
|
|
4
|
+
import { useDialogs } from "../context/dialogs.mjs";
|
|
3
5
|
import { useToasts } from "../context/toasts.mjs";
|
|
4
6
|
function useEditorEvents() {
|
|
5
7
|
const editor = useEditor();
|
|
6
8
|
const { addToast } = useToasts();
|
|
9
|
+
const { addDialog } = useDialogs();
|
|
7
10
|
useEffect(() => {
|
|
8
11
|
function handleMaxShapes({ name, count }) {
|
|
9
12
|
addToast({
|
|
@@ -12,11 +15,19 @@ function useEditorEvents() {
|
|
|
12
15
|
severity: "warning"
|
|
13
16
|
});
|
|
14
17
|
}
|
|
18
|
+
function handleConfirmOpenLink({ url }) {
|
|
19
|
+
addDialog({
|
|
20
|
+
id: "confirm-open-link",
|
|
21
|
+
component: ({ onClose }) => createElement(ConfirmOpenLinkDialog, { url, onClose })
|
|
22
|
+
});
|
|
23
|
+
}
|
|
15
24
|
editor.addListener("max-shapes", handleMaxShapes);
|
|
25
|
+
editor.addListener("confirm-open-link", handleConfirmOpenLink);
|
|
16
26
|
return () => {
|
|
17
27
|
editor.removeListener("max-shapes", handleMaxShapes);
|
|
28
|
+
editor.removeListener("confirm-open-link", handleConfirmOpenLink);
|
|
18
29
|
};
|
|
19
|
-
}, [editor, addToast]);
|
|
30
|
+
}, [editor, addToast, addDialog]);
|
|
20
31
|
}
|
|
21
32
|
export {
|
|
22
33
|
useEditorEvents
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/lib/ui/hooks/useEditorEvents.ts"],
|
|
4
|
-
"sourcesContent": ["import { useEditor } from '@tldraw/editor'\nimport { useEffect } from 'react'\nimport { useToasts } from '../context/toasts'\n\n/** @internal */\nexport function useEditorEvents() {\n\tconst editor = useEditor()\n\tconst { addToast } = useToasts()\n\n\tuseEffect(() => {\n\t\tfunction handleMaxShapes({ name, count }: { name: string; pageId: string; count: number }) {\n\t\t\taddToast({\n\t\t\t\ttitle: 'Maximum shapes reached',\n\t\t\t\tdescription: `You've reached the maximum number of shapes allowed on ${name} (${count}). Please delete some shapes or move to a different page to continue.`,\n\t\t\t\tseverity: 'warning',\n\t\t\t})\n\t\t}\n\n\t\teditor.addListener('max-shapes', handleMaxShapes)\n\t\treturn () => {\n\t\t\teditor.removeListener('max-shapes', handleMaxShapes)\n\t\t}\n\t}, [editor, addToast])\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB;
|
|
4
|
+
"sourcesContent": ["import { useEditor } from '@tldraw/editor'\nimport { createElement, useEffect } from 'react'\nimport { ConfirmOpenLinkDialog } from '../components/ConfirmOpenLinkDialog'\nimport { useDialogs } from '../context/dialogs'\nimport { useToasts } from '../context/toasts'\n\n/** @internal */\nexport function useEditorEvents() {\n\tconst editor = useEditor()\n\tconst { addToast } = useToasts()\n\tconst { addDialog } = useDialogs()\n\n\tuseEffect(() => {\n\t\tfunction handleMaxShapes({ name, count }: { name: string; pageId: string; count: number }) {\n\t\t\taddToast({\n\t\t\t\ttitle: 'Maximum shapes reached',\n\t\t\t\tdescription: `You've reached the maximum number of shapes allowed on ${name} (${count}). Please delete some shapes or move to a different page to continue.`,\n\t\t\t\tseverity: 'warning',\n\t\t\t})\n\t\t}\n\n\t\tfunction handleConfirmOpenLink({ url }: { url: string }) {\n\t\t\taddDialog({\n\t\t\t\tid: 'confirm-open-link',\n\t\t\t\tcomponent: ({ onClose }) => createElement(ConfirmOpenLinkDialog, { url, onClose }),\n\t\t\t})\n\t\t}\n\n\t\teditor.addListener('max-shapes', handleMaxShapes)\n\t\teditor.addListener('confirm-open-link', handleConfirmOpenLink)\n\t\treturn () => {\n\t\t\teditor.removeListener('max-shapes', handleMaxShapes)\n\t\t\teditor.removeListener('confirm-open-link', handleConfirmOpenLink)\n\t\t}\n\t}, [editor, addToast, addDialog])\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,iBAAiB;AAC1B,SAAS,eAAe,iBAAiB;AACzC,SAAS,6BAA6B;AACtC,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB;AAGnB,SAAS,kBAAkB;AACjC,QAAM,SAAS,UAAU;AACzB,QAAM,EAAE,SAAS,IAAI,UAAU;AAC/B,QAAM,EAAE,UAAU,IAAI,WAAW;AAEjC,YAAU,MAAM;AACf,aAAS,gBAAgB,EAAE,MAAM,MAAM,GAAoD;AAC1F,eAAS;AAAA,QACR,OAAO;AAAA,QACP,aAAa,0DAA0D,IAAI,KAAK,KAAK;AAAA,QACrF,UAAU;AAAA,MACX,CAAC;AAAA,IACF;AAEA,aAAS,sBAAsB,EAAE,IAAI,GAAoB;AACxD,gBAAU;AAAA,QACT,IAAI;AAAA,QACJ,WAAW,CAAC,EAAE,QAAQ,MAAM,cAAc,uBAAuB,EAAE,KAAK,QAAQ,CAAC;AAAA,MAClF,CAAC;AAAA,IACF;AAEA,WAAO,YAAY,cAAc,eAAe;AAChD,WAAO,YAAY,qBAAqB,qBAAqB;AAC7D,WAAO,MAAM;AACZ,aAAO,eAAe,cAAc,eAAe;AACnD,aAAO,eAAe,qBAAqB,qBAAqB;AAAA,IACjE;AAAA,EACD,GAAG,CAAC,QAAQ,UAAU,SAAS,CAAC;AACjC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/lib/ui/version.ts"],
|
|
4
|
-
"sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.6.
|
|
4
|
+
"sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.6.2'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-10-28T14:20:57.238Z',\n\tpatch: '2026-11-20T13:16:37.205Z',\n}\n"],
|
|
5
5
|
"mappings": "AAGO,MAAM,UAAU;AAChB,MAAM,eAAe;AAAA,EAC3B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { openWindow, T } from "@tldraw/editor";
|
|
2
|
+
const trustedLinkProtocols = /* @__PURE__ */ new Set([
|
|
3
|
+
"http:",
|
|
4
|
+
"https:",
|
|
5
|
+
"mailto:",
|
|
6
|
+
"claude:",
|
|
7
|
+
"codex:",
|
|
8
|
+
"cursor:",
|
|
9
|
+
"discord:",
|
|
10
|
+
"figma:",
|
|
11
|
+
"jetbrains:",
|
|
12
|
+
"linear:",
|
|
13
|
+
"notion:",
|
|
14
|
+
"obsidian:",
|
|
15
|
+
"slack:",
|
|
16
|
+
"spotify:",
|
|
17
|
+
"vscode:",
|
|
18
|
+
"windsurf:",
|
|
19
|
+
"zed:"
|
|
20
|
+
]);
|
|
21
|
+
function getLinkOpenPolicy(url) {
|
|
22
|
+
if (!url || !T.linkUrl.isValid(url)) return "block";
|
|
23
|
+
if (url.startsWith("/") || url.startsWith("./")) return "open";
|
|
24
|
+
return trustedLinkProtocols.has(new URL(url).protocol) ? "open" : "confirm";
|
|
25
|
+
}
|
|
26
|
+
function openLink(editor, url) {
|
|
27
|
+
switch (getLinkOpenPolicy(url)) {
|
|
28
|
+
case "open":
|
|
29
|
+
openWindow(url, "_blank");
|
|
30
|
+
return;
|
|
31
|
+
case "confirm":
|
|
32
|
+
if (editor.listenerCount("confirm-open-link") > 0) {
|
|
33
|
+
editor.emit("confirm-open-link", { url });
|
|
34
|
+
} else {
|
|
35
|
+
console.warn(
|
|
36
|
+
`Not opening ${url}: links into unknown apps need confirming, and nothing is listening for the editor's confirm-open-link event. Add a listener with editor.on('confirm-open-link', ({ url }) => ...) to ask the user, or render the default tldraw UI.`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
case "block":
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
getLinkOpenPolicy,
|
|
46
|
+
openLink
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=openLink.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/utils/openLink.ts"],
|
|
4
|
+
"sourcesContent": ["import { Editor, openWindow, T } from '@tldraw/editor'\n\n/**\n * How {@link openLink} treats a link. See {@link getLinkOpenPolicy}.\n *\n * @public\n */\nexport type TLLinkOpenPolicy = 'open' | 'confirm' | 'block'\n\n// This list only decides whether we ask before opening, so it can change in any release. Don't move\n// it into `T.linkUrl`: every client and the sync server validate stored urls with that, so changing\n// what it accepts breaks shared boards between versions.\nconst trustedLinkProtocols = new Set([\n\t'http:',\n\t'https:',\n\t'mailto:',\n\t'claude:',\n\t'codex:',\n\t'cursor:',\n\t'discord:',\n\t'figma:',\n\t'jetbrains:',\n\t'linear:',\n\t'notion:',\n\t'obsidian:',\n\t'slack:',\n\t'spotify:',\n\t'vscode:',\n\t'windsurf:',\n\t'zed:',\n])\n\n/**\n * Decide how a link from a shape or rich text should be opened.\n *\n * - `open`: a web link, mailto link, relative link, or a link into a well-known app.\n * - `confirm`: any other link that `T.linkUrl` accepts, such as an app we don't know. Ask the\n * user before opening it.\n * - `block`: anything `T.linkUrl` rejects. Stored records are validated, but a link can also\n * come from rendered rich text or a custom shape, so check again here.\n *\n * @public\n */\nexport function getLinkOpenPolicy(url: string): TLLinkOpenPolicy {\n\tif (!url || !T.linkUrl.isValid(url)) return 'block'\n\tif (url.startsWith('/') || url.startsWith('./')) return 'open'\n\treturn trustedLinkProtocols.has(new URL(url).protocol) ? 'open' : 'confirm'\n}\n\n/**\n * Open a user-provided link in a new window, following {@link getLinkOpenPolicy}. Links that need\n * confirming emit a `confirm-open-link` event, which the default UI answers with a dialog. With no\n * listener for that event (for example with `hideUi`), those links are not opened. To handle them\n * yourself, add a listener that asks the user and calls `openWindow(url, '_blank')` if they agree.\n *\n * @public\n */\nexport function openLink(editor: Editor, url: string) {\n\tswitch (getLinkOpenPolicy(url)) {\n\t\tcase 'open':\n\t\t\topenWindow(url, '_blank')\n\t\t\treturn\n\t\tcase 'confirm':\n\t\t\tif (editor.listenerCount('confirm-open-link') > 0) {\n\t\t\t\teditor.emit('confirm-open-link', { url })\n\t\t\t} else {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`Not opening ${url}: links into unknown apps need confirming, and nothing is listening for the editor's confirm-open-link event. Add a listener with editor.on('confirm-open-link', ({ url }) => ...) to ask the user, or render the default tldraw UI.`\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn\n\t\tcase 'block':\n\t\t\treturn\n\t}\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAiB,YAAY,SAAS;AAYtC,MAAM,uBAAuB,oBAAI,IAAI;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,CAAC;AAaM,SAAS,kBAAkB,KAA+B;AAChE,MAAI,CAAC,OAAO,CAAC,EAAE,QAAQ,QAAQ,GAAG,EAAG,QAAO;AAC5C,MAAI,IAAI,WAAW,GAAG,KAAK,IAAI,WAAW,IAAI,EAAG,QAAO;AACxD,SAAO,qBAAqB,IAAI,IAAI,IAAI,GAAG,EAAE,QAAQ,IAAI,SAAS;AACnE;AAUO,SAAS,SAAS,QAAgB,KAAa;AACrD,UAAQ,kBAAkB,GAAG,GAAG;AAAA,IAC/B,KAAK;AACJ,iBAAW,KAAK,QAAQ;AACxB;AAAA,IACD,KAAK;AACJ,UAAI,OAAO,cAAc,mBAAmB,IAAI,GAAG;AAClD,eAAO,KAAK,qBAAqB,EAAE,IAAI,CAAC;AAAA,MACzC,OAAO;AACN,gBAAQ;AAAA,UACP,eAAe,GAAG;AAAA,QACnB;AAAA,MACD;AACA;AAAA,IACD,KAAK;AACJ;AAAA,EACF;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tldraw",
|
|
3
3
|
"description": "A tiny little drawing editor.",
|
|
4
|
-
"version": "5.6.
|
|
4
|
+
"version": "5.6.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "tldraw Inc.",
|
|
7
7
|
"email": "hello@tldraw.com"
|
|
@@ -56,11 +56,11 @@
|
|
|
56
56
|
"@tiptap/pm": "^3.12.1",
|
|
57
57
|
"@tiptap/react": "^3.12.1",
|
|
58
58
|
"@tiptap/starter-kit": "^3.12.1",
|
|
59
|
-
"@tldraw/driver": "5.6.
|
|
60
|
-
"@tldraw/editor": "5.6.
|
|
61
|
-
"@tldraw/state": "5.6.
|
|
62
|
-
"@tldraw/store": "5.6.
|
|
63
|
-
"@tldraw/tlschema": "5.6.
|
|
59
|
+
"@tldraw/driver": "5.6.2",
|
|
60
|
+
"@tldraw/editor": "5.6.2",
|
|
61
|
+
"@tldraw/state": "5.6.2",
|
|
62
|
+
"@tldraw/store": "5.6.2",
|
|
63
|
+
"@tldraw/tlschema": "5.6.2",
|
|
64
64
|
"classnames": "^2.5.1",
|
|
65
65
|
"idb": "^7.1.1",
|
|
66
66
|
"lz-string": "^1.5.0",
|
package/src/index.ts
CHANGED
|
@@ -844,6 +844,7 @@ export {
|
|
|
844
844
|
setDefaultEditorAssetUrls,
|
|
845
845
|
type TLEditorAssetUrls,
|
|
846
846
|
} from './lib/utils/static-assets/assetUrls'
|
|
847
|
+
export { getLinkOpenPolicy, openLink, type TLLinkOpenPolicy } from './lib/utils/openLink'
|
|
847
848
|
export { sanitizeSvg } from './lib/utils/svg/sanitizeSvg'
|
|
848
849
|
export {
|
|
849
850
|
defaultAddFontsFromNode,
|
|
@@ -24,6 +24,7 @@ import type { ShapeOptionsWithDisplayValues } from '../shared/getDisplayValues'
|
|
|
24
24
|
import { HyperlinkButton } from '../shared/HyperlinkButton'
|
|
25
25
|
import { LINK_ICON } from '../shared/icons-editor'
|
|
26
26
|
import { getRotatedBoxShadow } from '../shared/rotated-box-shadow'
|
|
27
|
+
import { useOpenLinkHandler } from '../shared/useOpenLinkHandler'
|
|
27
28
|
import {
|
|
28
29
|
BOOKMARK_HEIGHT,
|
|
29
30
|
BOOKMARK_WIDTH,
|
|
@@ -181,6 +182,7 @@ export function BookmarkShapeComponent({
|
|
|
181
182
|
const [isFaviconValid, setIsFaviconValid] = useState(true)
|
|
182
183
|
const onFaviconError = () => setIsFaviconValid(false)
|
|
183
184
|
|
|
185
|
+
const handleOpenLink = useOpenLinkHandler(url)
|
|
184
186
|
const markAsHandledOnShiftKey = useCallback<PointerEventHandler>(
|
|
185
187
|
(e) => {
|
|
186
188
|
if (!editor.inputs.getShiftKey()) editor.markEventAsHandled(e)
|
|
@@ -226,6 +228,8 @@ export function BookmarkShapeComponent({
|
|
|
226
228
|
draggable={false}
|
|
227
229
|
onPointerDown={markAsHandledOnShiftKey}
|
|
228
230
|
onPointerUp={markAsHandledOnShiftKey}
|
|
231
|
+
onClick={handleOpenLink}
|
|
232
|
+
onAuxClick={handleOpenLink}
|
|
229
233
|
>
|
|
230
234
|
<h2 className="tl-bookmark__heading">{convertCommonTitleHTMLEntities(title)}</h2>
|
|
231
235
|
</a>
|
|
@@ -241,6 +245,8 @@ export function BookmarkShapeComponent({
|
|
|
241
245
|
draggable={false}
|
|
242
246
|
onPointerDown={markAsHandledOnShiftKey}
|
|
243
247
|
onPointerUp={markAsHandledOnShiftKey}
|
|
248
|
+
onClick={handleOpenLink}
|
|
249
|
+
onAuxClick={handleOpenLink}
|
|
244
250
|
>
|
|
245
251
|
{isFaviconValid && asset?.props.favicon ? (
|
|
246
252
|
<img
|
|
@@ -2,6 +2,7 @@ import { useEditor } from '@tldraw/editor'
|
|
|
2
2
|
import classNames from 'classnames'
|
|
3
3
|
import { PointerEventHandler, useCallback } from 'react'
|
|
4
4
|
import { useEfficientZoomThreshold } from './useEfficientZoomThreshold'
|
|
5
|
+
import { useOpenLinkHandler } from './useOpenLinkHandler'
|
|
5
6
|
|
|
6
7
|
const LINK_ICON =
|
|
7
8
|
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='30' height='30' fill='none'%3E%3Cpath stroke='%23000' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M13 5H7a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6M19 5h6m0 0v6m0-6L13 17'/%3E%3C/svg%3E"
|
|
@@ -9,6 +10,7 @@ const LINK_ICON =
|
|
|
9
10
|
export function HyperlinkButton({ url }: { url: string }) {
|
|
10
11
|
const editor = useEditor()
|
|
11
12
|
const hideButton = useEfficientZoomThreshold()
|
|
13
|
+
const handleOpenLink = useOpenLinkHandler(url)
|
|
12
14
|
const markAsHandledOnShiftKey = useCallback<PointerEventHandler>(
|
|
13
15
|
(e) => {
|
|
14
16
|
if (!editor.inputs.getShiftKey()) editor.markEventAsHandled(e)
|
|
@@ -25,6 +27,8 @@ export function HyperlinkButton({ url }: { url: string }) {
|
|
|
25
27
|
rel="noopener noreferrer"
|
|
26
28
|
onPointerDown={markAsHandledOnShiftKey}
|
|
27
29
|
onPointerUp={markAsHandledOnShiftKey}
|
|
30
|
+
onClick={handleOpenLink}
|
|
31
|
+
onAuxClick={handleOpenLink}
|
|
28
32
|
title={url}
|
|
29
33
|
draggable={false}
|
|
30
34
|
>
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
TLEventInfo,
|
|
5
5
|
TLRichText,
|
|
6
6
|
TLShapeId,
|
|
7
|
-
openWindow,
|
|
8
7
|
preventDefault,
|
|
9
8
|
resolveLineHeightPx,
|
|
10
9
|
useEditor,
|
|
@@ -13,6 +12,7 @@ import {
|
|
|
13
12
|
} from '@tldraw/editor'
|
|
14
13
|
import classNames from 'classnames'
|
|
15
14
|
import React, { useMemo } from 'react'
|
|
15
|
+
import { openLink } from '../../utils/openLink'
|
|
16
16
|
import { renderHtmlFromRichText } from '../../utils/text/richText'
|
|
17
17
|
import { RichTextArea } from '../text/RichTextArea'
|
|
18
18
|
import { isLegacyAlign } from './legacyProps'
|
|
@@ -115,7 +115,7 @@ export const RichTextLabel = React.memo(function RichTextLabel({
|
|
|
115
115
|
if (e.name !== 'pointer_up' || !link) return
|
|
116
116
|
|
|
117
117
|
if (!isDragging.current) {
|
|
118
|
-
|
|
118
|
+
openLink(editor, link)
|
|
119
119
|
}
|
|
120
120
|
editor.off('event', handlePointerUp)
|
|
121
121
|
}
|
|
@@ -123,6 +123,20 @@ export const RichTextLabel = React.memo(function RichTextLabel({
|
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
// Mouse presses on a link are handled in handlePointerDown, since the canvas captures the pointer
|
|
127
|
+
// and the anchor never gets their click. A click that does reach it (Enter on a focused link)
|
|
128
|
+
// would follow the href and skip openLink's check, so route it through openLink here.
|
|
129
|
+
const handleLinkClick = (e: React.MouseEvent<HTMLDivElement>) => {
|
|
130
|
+
if (e.type === 'auxclick' && e.button !== 1) return
|
|
131
|
+
const HTMLElementCtor = editor.getContainerWindow().HTMLElement
|
|
132
|
+
if (!(e.target instanceof HTMLElementCtor)) return
|
|
133
|
+
const anchor = e.target.closest('a')
|
|
134
|
+
if (!anchor) return
|
|
135
|
+
preventDefault(e)
|
|
136
|
+
// A mouse click that gets here was already opened on pointer up; only open keyboard clicks.
|
|
137
|
+
if (e.detail === 0) openLink(editor, anchor.getAttribute('href') ?? '')
|
|
138
|
+
}
|
|
139
|
+
|
|
126
140
|
// Should be guarded higher up so that this doesn't render... but repeated here. This should never be true.
|
|
127
141
|
if (!isEditing && isEmpty) return null
|
|
128
142
|
|
|
@@ -182,6 +196,8 @@ export const RichTextLabel = React.memo(function RichTextLabel({
|
|
|
182
196
|
// todo: see if I can abuse this
|
|
183
197
|
dangerouslySetInnerHTML={{ __html: html || '' }}
|
|
184
198
|
onPointerDown={handlePointerDown}
|
|
199
|
+
onClick={handleLinkClick}
|
|
200
|
+
onAuxClick={handleLinkClick}
|
|
185
201
|
data-is-ready-for-editing={isReadyForEditing}
|
|
186
202
|
/>
|
|
187
203
|
)}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEditor } from '@tldraw/editor'
|
|
2
|
+
import { MouseEventHandler, useCallback } from 'react'
|
|
3
|
+
import { openLink } from '../../utils/openLink'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Click handler for an `<a href>` that holds a user-provided link. Letting the browser follow the
|
|
7
|
+
* href would skip {@link openLink}'s check, so a modified or middle click on an unknown app link
|
|
8
|
+
* would open it without asking. Use it for both `onClick` and `onAuxClick`.
|
|
9
|
+
*/
|
|
10
|
+
export function useOpenLinkHandler(url: string) {
|
|
11
|
+
const editor = useEditor()
|
|
12
|
+
return useCallback<MouseEventHandler>(
|
|
13
|
+
(e) => {
|
|
14
|
+
if (e.type === 'auxclick' && e.button !== 1) return
|
|
15
|
+
e.preventDefault()
|
|
16
|
+
openLink(editor, url)
|
|
17
|
+
},
|
|
18
|
+
[editor, url]
|
|
19
|
+
)
|
|
20
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { openWindow } from '@tldraw/editor'
|
|
2
|
+
import { useCallback } from 'react'
|
|
3
|
+
import { TLUiDialogProps } from '../context/dialogs'
|
|
4
|
+
import { F } from '../context/i18n'
|
|
5
|
+
import { TldrawUiButton } from './primitives/Button/TldrawUiButton'
|
|
6
|
+
import { TldrawUiButtonLabel } from './primitives/Button/TldrawUiButtonLabel'
|
|
7
|
+
import {
|
|
8
|
+
TldrawUiDialogBody,
|
|
9
|
+
TldrawUiDialogCloseButton,
|
|
10
|
+
TldrawUiDialogFooter,
|
|
11
|
+
TldrawUiDialogHeader,
|
|
12
|
+
TldrawUiDialogTitle,
|
|
13
|
+
} from './primitives/TldrawUiDialog'
|
|
14
|
+
|
|
15
|
+
/** @internal */
|
|
16
|
+
export function ConfirmOpenLinkDialog({ url, onClose }: TLUiDialogProps & { url: string }) {
|
|
17
|
+
const handleOpen = useCallback(() => {
|
|
18
|
+
openWindow(url, '_blank')
|
|
19
|
+
onClose()
|
|
20
|
+
}, [url, onClose])
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<>
|
|
24
|
+
<TldrawUiDialogHeader>
|
|
25
|
+
<TldrawUiDialogTitle>
|
|
26
|
+
<F defaultMessage="Open external link?" id="confirm-open-link-dialog.title" />
|
|
27
|
+
</TldrawUiDialogTitle>
|
|
28
|
+
<TldrawUiDialogCloseButton />
|
|
29
|
+
</TldrawUiDialogHeader>
|
|
30
|
+
<TldrawUiDialogBody style={{ maxWidth: 350 }}>
|
|
31
|
+
<p>
|
|
32
|
+
<F
|
|
33
|
+
defaultMessage="This link opens another app on your device. Only continue if you trust where it came from."
|
|
34
|
+
id="confirm-open-link-dialog.detail"
|
|
35
|
+
/>
|
|
36
|
+
</p>
|
|
37
|
+
<p className="tlui-confirm-open-link-dialog__url">{url}</p>
|
|
38
|
+
</TldrawUiDialogBody>
|
|
39
|
+
<TldrawUiDialogFooter className="tlui-dialog__footer__actions">
|
|
40
|
+
<TldrawUiButton type="normal" onClick={onClose}>
|
|
41
|
+
<TldrawUiButtonLabel>
|
|
42
|
+
<F defaultMessage="Cancel" id="confirm-open-link-dialog.cancel" />
|
|
43
|
+
</TldrawUiButtonLabel>
|
|
44
|
+
</TldrawUiButton>
|
|
45
|
+
<TldrawUiButton type="primary" onClick={handleOpen}>
|
|
46
|
+
<TldrawUiButtonLabel>
|
|
47
|
+
<F defaultMessage="Open link" id="confirm-open-link-dialog.open" />
|
|
48
|
+
</TldrawUiButtonLabel>
|
|
49
|
+
</TldrawUiButton>
|
|
50
|
+
</TldrawUiDialogFooter>
|
|
51
|
+
</>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { defineMessages,
|
|
1
|
+
import { defineMessages, preventDefault, TiptapEditor, useEditor } from '@tldraw/editor'
|
|
2
2
|
import classNames from 'classnames'
|
|
3
3
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
4
4
|
import { normalizeLinkUrl } from '../../../utils/linkUrl'
|
|
5
|
+
import { openLink } from '../../../utils/openLink'
|
|
5
6
|
import { useUiEvents } from '../../context/events'
|
|
6
7
|
import { useTranslation } from '../../hooks/useTranslation/useTranslation'
|
|
7
8
|
import { TldrawUiButton } from '../primitives/Button/TldrawUiButton'
|
|
@@ -66,7 +67,7 @@ export function LinkEditor({ textEditor, value: initialValue, onClose }: LinkEdi
|
|
|
66
67
|
|
|
67
68
|
const handleVisitLink = () => {
|
|
68
69
|
trackEvent('rich-text', { operation: 'link-visit', source })
|
|
69
|
-
if (normalizedValue)
|
|
70
|
+
if (normalizedValue) openLink(editor, normalizedValue)
|
|
70
71
|
onClose()
|
|
71
72
|
}
|
|
72
73
|
|
|
@@ -29,6 +29,7 @@ import { defaultHandleExternalTextContent } from '../../defaultExternalContentHa
|
|
|
29
29
|
import { createBookmarkFromUrl } from '../../shapes/bookmark/bookmarks'
|
|
30
30
|
import { downloadFile } from '../../utils/export/exportAs'
|
|
31
31
|
import { fitFrameToContent, getFrameableShapeIds, removeFrame } from '../../utils/frames/frames'
|
|
32
|
+
import { openLink } from '../../utils/openLink'
|
|
32
33
|
import { generateShapeAnnouncementMessage } from '../components/A11y'
|
|
33
34
|
import { EditLinkDialog } from '../components/EditLinkDialog'
|
|
34
35
|
import { EmbedDialog } from '../components/EmbedDialog'
|
|
@@ -683,7 +684,7 @@ export function ActionsProvider({ overrides, children }: ActionsProviderProps) {
|
|
|
683
684
|
return
|
|
684
685
|
}
|
|
685
686
|
|
|
686
|
-
|
|
687
|
+
openLink(editor, shape.props.url)
|
|
687
688
|
},
|
|
688
689
|
},
|
|
689
690
|
{
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { useEditor } from '@tldraw/editor'
|
|
2
|
-
import { useEffect } from 'react'
|
|
2
|
+
import { createElement, useEffect } from 'react'
|
|
3
|
+
import { ConfirmOpenLinkDialog } from '../components/ConfirmOpenLinkDialog'
|
|
4
|
+
import { useDialogs } from '../context/dialogs'
|
|
3
5
|
import { useToasts } from '../context/toasts'
|
|
4
6
|
|
|
5
7
|
/** @internal */
|
|
6
8
|
export function useEditorEvents() {
|
|
7
9
|
const editor = useEditor()
|
|
8
10
|
const { addToast } = useToasts()
|
|
11
|
+
const { addDialog } = useDialogs()
|
|
9
12
|
|
|
10
13
|
useEffect(() => {
|
|
11
14
|
function handleMaxShapes({ name, count }: { name: string; pageId: string; count: number }) {
|
|
@@ -16,9 +19,18 @@ export function useEditorEvents() {
|
|
|
16
19
|
})
|
|
17
20
|
}
|
|
18
21
|
|
|
22
|
+
function handleConfirmOpenLink({ url }: { url: string }) {
|
|
23
|
+
addDialog({
|
|
24
|
+
id: 'confirm-open-link',
|
|
25
|
+
component: ({ onClose }) => createElement(ConfirmOpenLinkDialog, { url, onClose }),
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
19
29
|
editor.addListener('max-shapes', handleMaxShapes)
|
|
30
|
+
editor.addListener('confirm-open-link', handleConfirmOpenLink)
|
|
20
31
|
return () => {
|
|
21
32
|
editor.removeListener('max-shapes', handleMaxShapes)
|
|
33
|
+
editor.removeListener('confirm-open-link', handleConfirmOpenLink)
|
|
22
34
|
}
|
|
23
|
-
}, [editor, addToast])
|
|
35
|
+
}, [editor, addToast, addDialog])
|
|
24
36
|
}
|
|
@@ -231,6 +231,10 @@ export type TLUiTranslationKey =
|
|
|
231
231
|
| 'comments.thread-title'
|
|
232
232
|
| 'comments.title'
|
|
233
233
|
| 'comments.unknown-author'
|
|
234
|
+
| 'confirm-open-link-dialog.cancel'
|
|
235
|
+
| 'confirm-open-link-dialog.detail'
|
|
236
|
+
| 'confirm-open-link-dialog.open'
|
|
237
|
+
| 'confirm-open-link-dialog.title'
|
|
234
238
|
| 'context-menu.arrange'
|
|
235
239
|
| 'context-menu.copy-as'
|
|
236
240
|
| 'context-menu.edit'
|
package/src/lib/ui/version.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// This file is automatically generated by internal/scripts/refresh-assets.ts.
|
|
2
2
|
// Do not edit manually. Or do, I'm a comment, not a cop.
|
|
3
3
|
|
|
4
|
-
export const version = '5.6.
|
|
4
|
+
export const version = '5.6.2'
|
|
5
5
|
export const publishDates = {
|
|
6
6
|
major: '2026-05-06T16:28:18.473Z',
|
|
7
7
|
minor: '2026-10-28T14:20:57.238Z',
|
|
8
|
-
patch: '2026-11-
|
|
8
|
+
patch: '2026-11-20T13:16:37.205Z',
|
|
9
9
|
}
|
package/src/lib/ui.css
CHANGED
|
@@ -1827,6 +1827,16 @@ tldraw? probably.
|
|
|
1827
1827
|
padding: 0px var(--tl-space-4);
|
|
1828
1828
|
}
|
|
1829
1829
|
|
|
1830
|
+
/* Confirm Open Link Dialog */
|
|
1831
|
+
|
|
1832
|
+
.tlui-confirm-open-link-dialog__url {
|
|
1833
|
+
margin-top: var(--tl-space-4);
|
|
1834
|
+
padding: var(--tl-space-3) var(--tl-space-4);
|
|
1835
|
+
border-radius: var(--tl-radius-2);
|
|
1836
|
+
background-color: var(--tl-color-muted-2);
|
|
1837
|
+
font-family: var(--tl-font-mono);
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1830
1840
|
/* Embed Dialog */
|
|
1831
1841
|
|
|
1832
1842
|
.tlui-embed__spacer {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { TestEditor } from '../../test/TestEditor'
|
|
2
|
+
import { getLinkOpenPolicy, openLink } from './openLink'
|
|
3
|
+
|
|
4
|
+
describe('getLinkOpenPolicy', () => {
|
|
5
|
+
it.each([
|
|
6
|
+
'https://tldraw.com',
|
|
7
|
+
'HTTP://tldraw.com',
|
|
8
|
+
'mailto:hello@tldraw.com',
|
|
9
|
+
'/relative/path',
|
|
10
|
+
'./relative/path',
|
|
11
|
+
'vscode://file/src/index.ts',
|
|
12
|
+
'Slack://channel?id=1',
|
|
13
|
+
'figma:design/123',
|
|
14
|
+
])('opens %j', (url) => {
|
|
15
|
+
expect(getLinkOpenPolicy(url)).toBe('open')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it.each(['chatgpt://chat/123', 'ChatGPT://chat/123', 'tel:5551234', 'some-new-app://thing'])(
|
|
19
|
+
'asks before opening an unknown app link: %j',
|
|
20
|
+
(url) => {
|
|
21
|
+
expect(getLinkOpenPolicy(url)).toBe('confirm')
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
it.each([
|
|
26
|
+
'',
|
|
27
|
+
'javascript:alert(1)',
|
|
28
|
+
'JavaScript:alert(1)',
|
|
29
|
+
'data:text/html,hi',
|
|
30
|
+
'file:///etc',
|
|
31
|
+
'ms-msdt:x',
|
|
32
|
+
'localhost:3000',
|
|
33
|
+
'not a url',
|
|
34
|
+
])('blocks %j', (url) => {
|
|
35
|
+
expect(getLinkOpenPolicy(url)).toBe('block')
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('openLink', () => {
|
|
40
|
+
let editor: TestEditor
|
|
41
|
+
let open: ReturnType<typeof vi.spyOn>
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
editor = new TestEditor()
|
|
45
|
+
open = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
open.mockRestore()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('opens a trusted link straight away', () => {
|
|
53
|
+
const onConfirm = vi.fn()
|
|
54
|
+
editor.on('confirm-open-link', onConfirm)
|
|
55
|
+
openLink(editor, 'vscode://file/src/index.ts')
|
|
56
|
+
expect(open).toHaveBeenCalledWith('vscode://file/src/index.ts', '_blank', 'noopener noreferrer')
|
|
57
|
+
expect(onConfirm).not.toHaveBeenCalled()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('asks the listener to confirm an unknown app link instead of opening it', () => {
|
|
61
|
+
const onConfirm = vi.fn()
|
|
62
|
+
editor.on('confirm-open-link', onConfirm)
|
|
63
|
+
openLink(editor, 'chatgpt://chat/123')
|
|
64
|
+
expect(onConfirm).toHaveBeenCalledWith({ url: 'chatgpt://chat/123' })
|
|
65
|
+
expect(open).not.toHaveBeenCalled()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('does not open an unknown app link when nothing can confirm it', () => {
|
|
69
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
70
|
+
openLink(editor, 'chatgpt://chat/123')
|
|
71
|
+
expect(open).not.toHaveBeenCalled()
|
|
72
|
+
expect(warn).toHaveBeenCalled()
|
|
73
|
+
warn.mockRestore()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('never opens a blocked link', () => {
|
|
77
|
+
const onConfirm = vi.fn()
|
|
78
|
+
editor.on('confirm-open-link', onConfirm)
|
|
79
|
+
openLink(editor, 'javascript:alert(1)')
|
|
80
|
+
expect(open).not.toHaveBeenCalled()
|
|
81
|
+
expect(onConfirm).not.toHaveBeenCalled()
|
|
82
|
+
})
|
|
83
|
+
})
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Editor, openWindow, T } from '@tldraw/editor'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How {@link openLink} treats a link. See {@link getLinkOpenPolicy}.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export type TLLinkOpenPolicy = 'open' | 'confirm' | 'block'
|
|
9
|
+
|
|
10
|
+
// This list only decides whether we ask before opening, so it can change in any release. Don't move
|
|
11
|
+
// it into `T.linkUrl`: every client and the sync server validate stored urls with that, so changing
|
|
12
|
+
// what it accepts breaks shared boards between versions.
|
|
13
|
+
const trustedLinkProtocols = new Set([
|
|
14
|
+
'http:',
|
|
15
|
+
'https:',
|
|
16
|
+
'mailto:',
|
|
17
|
+
'claude:',
|
|
18
|
+
'codex:',
|
|
19
|
+
'cursor:',
|
|
20
|
+
'discord:',
|
|
21
|
+
'figma:',
|
|
22
|
+
'jetbrains:',
|
|
23
|
+
'linear:',
|
|
24
|
+
'notion:',
|
|
25
|
+
'obsidian:',
|
|
26
|
+
'slack:',
|
|
27
|
+
'spotify:',
|
|
28
|
+
'vscode:',
|
|
29
|
+
'windsurf:',
|
|
30
|
+
'zed:',
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Decide how a link from a shape or rich text should be opened.
|
|
35
|
+
*
|
|
36
|
+
* - `open`: a web link, mailto link, relative link, or a link into a well-known app.
|
|
37
|
+
* - `confirm`: any other link that `T.linkUrl` accepts, such as an app we don't know. Ask the
|
|
38
|
+
* user before opening it.
|
|
39
|
+
* - `block`: anything `T.linkUrl` rejects. Stored records are validated, but a link can also
|
|
40
|
+
* come from rendered rich text or a custom shape, so check again here.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export function getLinkOpenPolicy(url: string): TLLinkOpenPolicy {
|
|
45
|
+
if (!url || !T.linkUrl.isValid(url)) return 'block'
|
|
46
|
+
if (url.startsWith('/') || url.startsWith('./')) return 'open'
|
|
47
|
+
return trustedLinkProtocols.has(new URL(url).protocol) ? 'open' : 'confirm'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Open a user-provided link in a new window, following {@link getLinkOpenPolicy}. Links that need
|
|
52
|
+
* confirming emit a `confirm-open-link` event, which the default UI answers with a dialog. With no
|
|
53
|
+
* listener for that event (for example with `hideUi`), those links are not opened. To handle them
|
|
54
|
+
* yourself, add a listener that asks the user and calls `openWindow(url, '_blank')` if they agree.
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export function openLink(editor: Editor, url: string) {
|
|
59
|
+
switch (getLinkOpenPolicy(url)) {
|
|
60
|
+
case 'open':
|
|
61
|
+
openWindow(url, '_blank')
|
|
62
|
+
return
|
|
63
|
+
case 'confirm':
|
|
64
|
+
if (editor.listenerCount('confirm-open-link') > 0) {
|
|
65
|
+
editor.emit('confirm-open-link', { url })
|
|
66
|
+
} else {
|
|
67
|
+
console.warn(
|
|
68
|
+
`Not opening ${url}: links into unknown apps need confirming, and nothing is listening for the editor's confirm-open-link event. Add a listener with editor.on('confirm-open-link', ({ url }) => ...) to ask the user, or render the default tldraw UI.`
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
return
|
|
72
|
+
case 'block':
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
}
|