tldraw 5.6.0 → 5.6.1
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 +2 -1
- package/RELEASE_NOTES.md +106 -3
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/shapes/bookmark/BookmarkShapeUtil.js +5 -4
- package/dist-cjs/lib/shapes/bookmark/BookmarkShapeUtil.js.map +2 -2
- package/dist-cjs/lib/shapes/bookmark/bookmarks.js +25 -4
- package/dist-cjs/lib/shapes/bookmark/bookmarks.js.map +2 -2
- package/dist-cjs/lib/ui/components/EditLinkDialog.js +15 -17
- package/dist-cjs/lib/ui/components/EditLinkDialog.js.map +2 -2
- package/dist-cjs/lib/ui/components/Toolbar/LinkEditor.js +46 -19
- package/dist-cjs/lib/ui/components/Toolbar/LinkEditor.js.map +3 -3
- package/dist-cjs/lib/ui/version.js +2 -2
- package/dist-cjs/lib/ui/version.js.map +1 -1
- package/dist-cjs/lib/utils/linkUrl.js +39 -0
- package/dist-cjs/lib/utils/linkUrl.js.map +7 -0
- package/dist-cjs/lib/utils/text/richText.js +4 -1
- package/dist-cjs/lib/utils/text/richText.js.map +2 -2
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/shapes/bookmark/BookmarkShapeUtil.mjs +6 -4
- package/dist-esm/lib/shapes/bookmark/BookmarkShapeUtil.mjs.map +2 -2
- package/dist-esm/lib/shapes/bookmark/bookmarks.mjs +25 -4
- package/dist-esm/lib/shapes/bookmark/bookmarks.mjs.map +2 -2
- package/dist-esm/lib/ui/components/EditLinkDialog.mjs +16 -18
- package/dist-esm/lib/ui/components/EditLinkDialog.mjs.map +2 -2
- package/dist-esm/lib/ui/components/Toolbar/LinkEditor.mjs +36 -19
- package/dist-esm/lib/ui/components/Toolbar/LinkEditor.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/linkUrl.mjs +19 -0
- package/dist-esm/lib/utils/linkUrl.mjs.map +7 -0
- package/dist-esm/lib/utils/text/richText.mjs +5 -1
- package/dist-esm/lib/utils/text/richText.mjs.map +2 -2
- package/package.json +6 -6
- package/src/lib/shapes/bookmark/BookmarkShapeUtil.tsx +6 -6
- package/src/lib/shapes/bookmark/bookmarks.ts +38 -5
- package/src/lib/ui/components/EditLinkDialog.tsx +22 -26
- package/src/lib/ui/components/Toolbar/LinkEditor.tsx +38 -20
- package/src/lib/ui/version.ts +2 -2
- package/src/lib/ui.css +9 -2
- package/src/lib/utils/linkUrl.test.ts +33 -0
- package/src/lib/utils/linkUrl.ts +30 -0
- package/src/lib/utils/text/richText.ts +4 -0
- package/src/test/bookmark-shapes.test.ts +62 -2
- package/src/test/ui/EditLinkDialog.test.tsx +53 -2
- package/src/test/ui/LinkEditor.test.tsx +71 -0
- package/tldraw.css +9 -2
|
@@ -9,7 +9,18 @@ const BOOKMARK_WIDTH = 300;
|
|
|
9
9
|
const BOOKMARK_HEIGHT = 320;
|
|
10
10
|
const BOOKMARK_JUST_URL_HEIGHT = 46;
|
|
11
11
|
const SHORT_BOOKMARK_HEIGHT = 101;
|
|
12
|
-
function
|
|
12
|
+
function canUnfurlUrl(url) {
|
|
13
|
+
try {
|
|
14
|
+
const { protocol } = new URL(url);
|
|
15
|
+
return protocol === "http:" || protocol === "https:";
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isUrlOnlyBookmarkUrl(url) {
|
|
21
|
+
return url !== "" && !canUnfurlUrl(url);
|
|
22
|
+
}
|
|
23
|
+
function getBookmarkHeight(editor, assetId, url) {
|
|
13
24
|
const asset = assetId ? editor.getAsset(assetId) : null;
|
|
14
25
|
if (asset) {
|
|
15
26
|
if (!asset.props.image) {
|
|
@@ -19,18 +30,24 @@ function getBookmarkHeight(editor, assetId) {
|
|
|
19
30
|
return SHORT_BOOKMARK_HEIGHT;
|
|
20
31
|
}
|
|
21
32
|
}
|
|
33
|
+
} else if (isUrlOnlyBookmarkUrl(url)) {
|
|
34
|
+
return SHORT_BOOKMARK_HEIGHT;
|
|
22
35
|
}
|
|
23
36
|
return BOOKMARK_HEIGHT;
|
|
24
37
|
}
|
|
25
38
|
function setBookmarkHeight(editor, shape) {
|
|
26
39
|
return {
|
|
27
40
|
...shape,
|
|
28
|
-
props: {
|
|
41
|
+
props: {
|
|
42
|
+
...shape.props,
|
|
43
|
+
h: getBookmarkHeight(editor, shape.props.assetId, shape.props.url)
|
|
44
|
+
}
|
|
29
45
|
};
|
|
30
46
|
}
|
|
31
47
|
function getHumanReadableAddress(url) {
|
|
32
48
|
try {
|
|
33
49
|
const objUrl = new URL(url);
|
|
50
|
+
if (isUrlOnlyBookmarkUrl(url)) return objUrl.protocol.slice(0, -1);
|
|
34
51
|
return objUrl.hostname.replace(/^www\./, "");
|
|
35
52
|
} catch {
|
|
36
53
|
return url;
|
|
@@ -56,11 +73,12 @@ function getResolvedBookmarkAssetId(editor, assetId, url) {
|
|
|
56
73
|
function getBookmarkShapeHeight(editor, shape) {
|
|
57
74
|
const resolvedAssetId = getResolvedBookmarkAssetId(editor, shape.props.assetId, shape.props.url);
|
|
58
75
|
if (resolvedAssetId === shape.props.assetId) return shape.props.h;
|
|
59
|
-
return getBookmarkHeight(editor, resolvedAssetId);
|
|
76
|
+
return getBookmarkHeight(editor, resolvedAssetId, shape.props.url);
|
|
60
77
|
}
|
|
61
78
|
async function _createBookmarkAssetOnUrlChange(editor, shape) {
|
|
62
79
|
if (editor.isDisposed) return;
|
|
63
80
|
const { url } = shape.props;
|
|
81
|
+
if (!canUnfurlUrl(url)) return;
|
|
64
82
|
const asset = await editor.getAssetForExternalContent({ type: "url", url });
|
|
65
83
|
if (!asset) {
|
|
66
84
|
return;
|
|
@@ -104,7 +122,7 @@ async function createBookmarkFromUrl(editor, {
|
|
|
104
122
|
url,
|
|
105
123
|
assetId: existingAsset?.id ?? null,
|
|
106
124
|
w: BOOKMARK_WIDTH,
|
|
107
|
-
h: getBookmarkHeight(editor, existingAsset?.id)
|
|
125
|
+
h: getBookmarkHeight(editor, existingAsset?.id, url)
|
|
108
126
|
}
|
|
109
127
|
};
|
|
110
128
|
editor.run(() => {
|
|
@@ -124,6 +142,7 @@ async function createBookmarkFromUrl(editor, {
|
|
|
124
142
|
}
|
|
125
143
|
}
|
|
126
144
|
async function hydrateBookmarkShape(editor, shapeId, url) {
|
|
145
|
+
if (!canUnfurlUrl(url)) return;
|
|
127
146
|
let asset;
|
|
128
147
|
try {
|
|
129
148
|
asset = await editor.getAssetForExternalContent({ type: "url", url });
|
|
@@ -157,11 +176,13 @@ export {
|
|
|
157
176
|
BOOKMARK_HEIGHT,
|
|
158
177
|
BOOKMARK_JUST_URL_HEIGHT,
|
|
159
178
|
BOOKMARK_WIDTH,
|
|
179
|
+
SHORT_BOOKMARK_HEIGHT,
|
|
160
180
|
createBookmarkFromUrl,
|
|
161
181
|
getBookmarkHeight,
|
|
162
182
|
getBookmarkShapeHeight,
|
|
163
183
|
getHumanReadableAddress,
|
|
164
184
|
getResolvedBookmarkAssetId,
|
|
185
|
+
isUrlOnlyBookmarkUrl,
|
|
165
186
|
resolveBookmarkAssetForUrlChange,
|
|
166
187
|
setBookmarkHeight
|
|
167
188
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/lib/shapes/bookmark/bookmarks.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n\tAssetRecordType,\n\tEditor,\n\tResult,\n\tTLAssetId,\n\tTLBookmarkAsset,\n\tTLBookmarkShape,\n\tTLShapeId,\n\tTLShapePartial,\n\tcreateShapeId,\n\tdebounce,\n\tgetHashForString,\n} from '@tldraw/editor'\n\nexport const BOOKMARK_WIDTH = 300\nexport const BOOKMARK_HEIGHT = 320\nexport const BOOKMARK_JUST_URL_HEIGHT = 46\
|
|
5
|
-
"mappings": "AAAA;AAAA,EACC;AAAA,EAEA;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEA,MAAM,iBAAiB;AACvB,MAAM,kBAAkB;AACxB,MAAM,2BAA2B;
|
|
4
|
+
"sourcesContent": ["import {\n\tAssetRecordType,\n\tEditor,\n\tResult,\n\tTLAssetId,\n\tTLBookmarkAsset,\n\tTLBookmarkShape,\n\tTLShapeId,\n\tTLShapePartial,\n\tcreateShapeId,\n\tdebounce,\n\tgetHashForString,\n} from '@tldraw/editor'\n\nexport const BOOKMARK_WIDTH = 300\nexport const BOOKMARK_HEIGHT = 320\nexport const BOOKMARK_JUST_URL_HEIGHT = 46\nexport const SHORT_BOOKMARK_HEIGHT = 101\n\n// A bookmark asset's `src` is a `T.srcUrl`, which rejects app links and `mailto:`. Not\n// `T.httpUrl`: it accepts `''` and relative paths, so clearing a bookmark's url would unfurl.\nfunction canUnfurlUrl(url: string) {\n\ttry {\n\t\tconst { protocol } = new URL(url)\n\t\treturn protocol === 'http:' || protocol === 'https:'\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Whether a bookmark without an asset will never get one, rather than still loading. Without\n * this, an app link bookmark shows the loading placeholder forever.\n */\nexport function isUrlOnlyBookmarkUrl(url: string) {\n\treturn url !== '' && !canUnfurlUrl(url)\n}\n\nexport function getBookmarkHeight(\n\teditor: Editor,\n\tassetId: TLAssetId | null | undefined,\n\turl: string\n) {\n\tconst asset = (assetId ? editor.getAsset(assetId) : null) as TLBookmarkAsset | null\n\n\tif (asset) {\n\t\tif (!asset.props.image) {\n\t\t\tif (!asset.props.title) {\n\t\t\t\treturn BOOKMARK_JUST_URL_HEIGHT\n\t\t\t} else {\n\t\t\t\treturn SHORT_BOOKMARK_HEIGHT\n\t\t\t}\n\t\t}\n\t} else if (isUrlOnlyBookmarkUrl(url)) {\n\t\treturn SHORT_BOOKMARK_HEIGHT\n\t}\n\n\treturn BOOKMARK_HEIGHT\n}\n\nexport function setBookmarkHeight(editor: Editor, shape: TLBookmarkShape) {\n\treturn {\n\t\t...shape,\n\t\tprops: {\n\t\t\t...shape.props,\n\t\t\th: getBookmarkHeight(editor, shape.props.assetId, shape.props.url),\n\t\t},\n\t}\n}\n\n/** @internal */\nexport function getHumanReadableAddress(url: string) {\n\ttry {\n\t\tconst objUrl = new URL(url)\n\t\t// An app link's host (`file` in `vscode://file/\u2026`) doesn't say where it goes, but its app does.\n\t\tif (isUrlOnlyBookmarkUrl(url)) return objUrl.protocol.slice(0, -1)\n\t\t// we want the hostname without any www\n\t\treturn objUrl.hostname.replace(/^www\\./, '')\n\t} catch {\n\t\treturn url\n\t}\n}\n\n/**\n * Resolve the asset for a bookmark whose url just changed: the existing asset for the new url\n * if there is one, otherwise `null` while a new one is fetched in the background.\n *\n * Must not write to the store: this runs inside `onBeforeUpdate`, where a nested `updateShapes`\n * is overwritten by the outer put and the old asset would stick.\n */\nexport function resolveBookmarkAssetForUrlChange(\n\teditor: Editor,\n\tshape: TLBookmarkShape\n): TLBookmarkShape {\n\tconst { url } = shape.props\n\n\t// Derive the asset id from the URL\n\tconst assetId: TLAssetId = AssetRecordType.createId(getHashForString(url))\n\n\tif (editor.getAsset(assetId)) {\n\t\tif (shape.props.assetId === assetId) return shape\n\t\treturn { ...shape, props: { ...shape.props, assetId } }\n\t}\n\n\t// No asset for this URL yet: drop the stale reference and fetch one asynchronously\n\tcreateBookmarkAssetOnUrlChange(editor, shape)\n\tif (shape.props.assetId === null) return shape\n\treturn { ...shape, props: { ...shape.props, assetId: null } }\n}\n\n/**\n * Resolve the asset id to render for a bookmark. Bookmark assets are keyed\n * deterministically by URL, so if the shape has no `assetId` but an asset for\n * its URL already exists in the store, use that.\n *\n * This keeps a bookmark from staying stuck on its placeholder after the shape\n * is recreated outside of the hydration flow \u2014 e.g. on redo, where the\n * placeholder is restored with a null `assetId` but its hydrated asset (created\n * with `history: 'ignore'`) still lives in the store.\n */\nexport function getResolvedBookmarkAssetId(\n\teditor: Editor,\n\tassetId: TLAssetId | null,\n\turl: string\n): TLAssetId | null {\n\tif (assetId) return assetId\n\tif (!url) return null\n\n\tconst derivedId: TLAssetId = AssetRecordType.createId(getHashForString(url))\n\treturn editor.getAsset(derivedId) ? derivedId : null\n}\n\n/**\n * The effective height to render and measure a bookmark at.\n *\n * Normally this is the stored `props.h`. But when the shape's `assetId` resolves\n * to a different asset than it stores \u2014 e.g. a placeholder restored on redo with\n * a null `assetId` whose asset already exists in the store \u2014 `props.h` is stale\n * (it still holds the placeholder height). In that case recompute the height\n * from the resolved asset so rendering, the indicator, and the geometry/selection\n * bounds all stay in sync.\n */\nexport function getBookmarkShapeHeight(editor: Editor, shape: TLBookmarkShape): number {\n\tconst resolvedAssetId = getResolvedBookmarkAssetId(editor, shape.props.assetId, shape.props.url)\n\tif (resolvedAssetId === shape.props.assetId) return shape.props.h\n\treturn getBookmarkHeight(editor, resolvedAssetId, shape.props.url)\n}\n\nasync function _createBookmarkAssetOnUrlChange(editor: Editor, shape: TLBookmarkShape) {\n\tif (editor.isDisposed) return\n\n\tconst { url } = shape.props\n\tif (!canUnfurlUrl(url)) return\n\n\t// Create the asset using the external content manager's createAssetFromUrl method.\n\t// This may be overwritten by the user (for example, we overwrite it on tldraw.com)\n\tconst asset = await editor.getAssetForExternalContent({ type: 'url', url })\n\n\tif (!asset) {\n\t\t// No asset? Just leave the bookmark as a null assetId.\n\t\treturn\n\t}\n\n\teditor.run(() => {\n\t\t// Create the new asset\n\t\teditor.createAssets([asset])\n\n\t\t// And update the shape\n\t\teditor.updateShapes([\n\t\t\t{\n\t\t\t\tid: shape.id,\n\t\t\t\ttype: shape.type,\n\t\t\t\tprops: { assetId: asset.id },\n\t\t\t},\n\t\t])\n\t})\n}\n\nconst bookmarkDebouncers = new WeakMap<\n\tEditor,\n\tReturnType<typeof debounce<[Editor, TLBookmarkShape], void>>\n>()\n\nfunction createBookmarkAssetOnUrlChange(editor: Editor, shape: TLBookmarkShape) {\n\tlet fn = bookmarkDebouncers.get(editor)\n\tif (!fn) {\n\t\tfn = debounce(_createBookmarkAssetOnUrlChange, 500)\n\t\tbookmarkDebouncers.set(editor, fn)\n\t}\n\tfn(editor, shape)\n}\n\n/**\n * Creates a bookmark shape from a URL.\n *\n * The shape is created immediately as a placeholder so the user gets visible\n * feedback at the paste location, and the bookmark metadata (title, description,\n * image, favicon) is fetched in the background. Once metadata resolves, the\n * shape is patched with the resulting asset. If the fetch fails, the shape is\n * left as a URL-only bookmark.\n *\n * @returns A Result containing the created bookmark shape or an error\n * @public\n */\nexport async function createBookmarkFromUrl(\n\teditor: Editor,\n\t{\n\t\turl,\n\t\tcenter = editor.getViewportPageBounds().center,\n\t}: {\n\t\turl: string\n\t\tcenter?: { x: number; y: number }\n\t}\n): Promise<Result<TLBookmarkShape, string>> {\n\ttry {\n\t\t// If we already have a bookmark asset for this URL (e.g. another bookmark\n\t\t// shape was created from the same URL earlier), use it immediately rather\n\t\t// than re-fetching.\n\t\tconst expectedAssetId: TLAssetId = AssetRecordType.createId(getHashForString(url))\n\t\tconst existingAsset = editor.getAsset(expectedAssetId) as TLBookmarkAsset | null\n\n\t\tconst shapeId = createShapeId()\n\t\tconst shapePartial: TLShapePartial<TLBookmarkShape> = {\n\t\t\tid: shapeId,\n\t\t\ttype: 'bookmark',\n\t\t\tx: center.x - BOOKMARK_WIDTH / 2,\n\t\t\ty: center.y - BOOKMARK_HEIGHT / 2,\n\t\t\trotation: 0,\n\t\t\topacity: 1,\n\t\t\tprops: {\n\t\t\t\turl,\n\t\t\t\tassetId: existingAsset?.id ?? null,\n\t\t\t\tw: BOOKMARK_WIDTH,\n\t\t\t\th: getBookmarkHeight(editor, existingAsset?.id, url),\n\t\t\t},\n\t\t}\n\n\t\teditor.run(() => {\n\t\t\teditor.createShapes([shapePartial])\n\t\t})\n\n\t\tconst createdShape = editor.getShape<TLBookmarkShape>(shapeId)\n\t\tif (!createdShape) {\n\t\t\treturn Result.err('Failed to create bookmark shape')\n\t\t}\n\n\t\t// If we already had the asset, we're done \u2014 no metadata fetch needed.\n\t\tif (existingAsset) {\n\t\t\treturn Result.ok(createdShape)\n\t\t}\n\n\t\t// Otherwise kick off the metadata fetch in the background. The shape is\n\t\t// already visible as a placeholder; once the asset resolves we'll patch\n\t\t// the shape with its assetId.\n\t\tvoid hydrateBookmarkShape(editor, shapeId, url)\n\n\t\treturn Result.ok(createdShape)\n\t} catch (error) {\n\t\treturn Result.err(error instanceof Error ? error.message : 'Failed to create bookmark')\n\t}\n}\n\nasync function hydrateBookmarkShape(editor: Editor, shapeId: TLShapeId, url: string) {\n\tif (!canUnfurlUrl(url)) return\n\n\tlet asset\n\ttry {\n\t\tasset = await editor.getAssetForExternalContent({ type: 'url', url })\n\t} catch (error) {\n\t\tconsole.error(error)\n\t\treturn\n\t}\n\n\tif (editor.isDisposed) return\n\tif (!asset) return\n\n\t// The shape may have been deleted (e.g. via undo), had its URL changed, or\n\t// had its asset replaced before the fetch resolved. In any of those cases we\n\t// don't want to attach metadata for the old URL.\n\tconst shape = editor.getShape<TLBookmarkShape>(shapeId)\n\tif (!shape) return\n\tif (shape.props.url !== url) return\n\tif (shape.props.assetId) return\n\n\teditor.run(\n\t\t() => {\n\t\t\tif (!editor.getAsset(asset.id)) {\n\t\t\t\teditor.createAssets([asset])\n\t\t\t}\n\t\t\teditor.updateShapes([\n\t\t\t\t{\n\t\t\t\t\tid: shapeId,\n\t\t\t\t\ttype: 'bookmark',\n\t\t\t\t\tprops: { assetId: asset.id },\n\t\t\t\t},\n\t\t\t])\n\t\t},\n\t\t{ history: 'ignore' }\n\t)\n}\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACC;AAAA,EAEA;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEA,MAAM,iBAAiB;AACvB,MAAM,kBAAkB;AACxB,MAAM,2BAA2B;AACjC,MAAM,wBAAwB;AAIrC,SAAS,aAAa,KAAa;AAClC,MAAI;AACH,UAAM,EAAE,SAAS,IAAI,IAAI,IAAI,GAAG;AAChC,WAA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KZ,QAAM,QAAQ,OAAO,SAA0B,OAAO;AACtD,MAAI,CAAC,MAAO;AACZ,MAAI,MAAM,MAAM,QAAQ,IAAK;AAC7B,MAAI,MAAM,MAAM,QAAS;AAEzB,SAAO;AAAA,IACN,MAAM;AACL,UAAI,CAAC,OAAO,SAAS,MAAM,EAAE,GAAG;AAC/B,eAAO,aAAa,CAAC,KAAK,CAAC;AAAA,MAC5B;AACA,aAAO,aAAa;AAAA,QACnB;AAAA,UACC,IAAI;AAAA,UACJ,MAAM;AAAA,UACN,OAAO,EAAE,SAAS,MAAM,GAAG;AAAA,QAC5B;AAAA,MACD,CAAC;AAAA,IACF;AAAA,IACA,EAAE,SAAS,SAAS;AAAA,EACrB;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { defineMessages,
|
|
2
|
+
import { defineMessages, track, useEditor } from "@tldraw/editor";
|
|
3
3
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { normalizeLinkUrl } from "../../utils/linkUrl.mjs";
|
|
4
5
|
import { isShapeWithLink } from "../../utils/shapes/shapes.mjs";
|
|
5
6
|
import { F } from "../context/i18n.mjs";
|
|
6
7
|
import { TldrawUiButton } from "./primitives/Button/TldrawUiButton.mjs";
|
|
@@ -13,19 +14,9 @@ import {
|
|
|
13
14
|
TldrawUiDialogTitle
|
|
14
15
|
} from "./primitives/TldrawUiDialog.mjs";
|
|
15
16
|
import { TldrawUiInput } from "./primitives/TldrawUiInput.mjs";
|
|
16
|
-
function validateUrl(url) {
|
|
17
|
-
if (T.linkUrl.isValid(url)) {
|
|
18
|
-
return { isValid: true, hasProtocol: true };
|
|
19
|
-
}
|
|
20
|
-
if (T.linkUrl.isValid("https://" + url)) {
|
|
21
|
-
return { isValid: true, hasProtocol: false };
|
|
22
|
-
}
|
|
23
|
-
return { isValid: false, hasProtocol: false };
|
|
24
|
-
}
|
|
25
17
|
function getUrlInputState(url) {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
return { actual: url, safe, valid: isValid };
|
|
18
|
+
const normalized = normalizeLinkUrl(url);
|
|
19
|
+
return { actual: url, safe: normalized ?? "", valid: normalized !== null };
|
|
29
20
|
}
|
|
30
21
|
function assertShapeWithLink(shape) {
|
|
31
22
|
if (!isShapeWithLink(shape)) {
|
|
@@ -62,9 +53,10 @@ const EditLinkDialogInner = track(function EditLinkDialogInner2({
|
|
|
62
53
|
const { safe } = getUrlInputState(selectedShape.props.url);
|
|
63
54
|
return { actual: safe, safe, valid: true };
|
|
64
55
|
});
|
|
56
|
+
const [showInvalid, setShowInvalid] = useState(false);
|
|
65
57
|
const handleChange = useCallback((rawValue) => {
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
setUrlInputState(getUrlInputState(rawValue));
|
|
59
|
+
setShowInvalid(false);
|
|
68
60
|
}, []);
|
|
69
61
|
const handleClear = useCallback(() => {
|
|
70
62
|
const onlySelectedShape = editor.getOnlySelectedShape();
|
|
@@ -79,6 +71,11 @@ const EditLinkDialogInner = track(function EditLinkDialogInner2({
|
|
|
79
71
|
const onlySelectedShape = editor.getOnlySelectedShape();
|
|
80
72
|
if (!onlySelectedShape) return;
|
|
81
73
|
assertShapeWithLink(onlySelectedShape);
|
|
74
|
+
if (!urlInputState.valid) {
|
|
75
|
+
setShowInvalid(true);
|
|
76
|
+
rInput.current?.focus();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
82
79
|
if (onlySelectedShape.props.url !== urlInputState.safe) {
|
|
83
80
|
editor.updateShapes([
|
|
84
81
|
{
|
|
@@ -93,7 +90,8 @@ const EditLinkDialogInner = track(function EditLinkDialogInner2({
|
|
|
93
90
|
const handleCancel = useCallback(() => {
|
|
94
91
|
onClose();
|
|
95
92
|
}, [onClose]);
|
|
96
|
-
const
|
|
93
|
+
const isEmpty = urlInputState.valid && urlInputState.safe === "";
|
|
94
|
+
const isRemoving = !!rInitialValue.current && isEmpty;
|
|
97
95
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
98
96
|
/* @__PURE__ */ jsxs(TldrawUiDialogHeader, { children: [
|
|
99
97
|
/* @__PURE__ */ jsx(TldrawUiDialogTitle, { children: /* @__PURE__ */ jsx(F, { defaultMessage: "Edit link", id: "edit-link-dialog.title" }) }),
|
|
@@ -115,7 +113,7 @@ const EditLinkDialogInner = track(function EditLinkDialogInner2({
|
|
|
115
113
|
onCancel: handleCancel
|
|
116
114
|
}
|
|
117
115
|
),
|
|
118
|
-
/* @__PURE__ */ jsx("div", { children:
|
|
116
|
+
/* @__PURE__ */ jsx("div", { children: showInvalid ? /* @__PURE__ */ jsx(F, { defaultMessage: "A link must be a valid URL.", id: "edit-link-dialog.invalid-url" }) : /* @__PURE__ */ jsx(F, { defaultMessage: "Links will open in a new tab.", id: "edit-link-dialog.detail" }) })
|
|
119
117
|
] }) }),
|
|
120
118
|
/* @__PURE__ */ jsxs(TldrawUiDialogFooter, { className: "tlui-dialog__footer__actions", children: [
|
|
121
119
|
/* @__PURE__ */ jsx(TldrawUiButton, { type: "normal", onClick: handleCancel, onTouchEnd: handleCancel, children: /* @__PURE__ */ jsx(TldrawUiButtonLabel, { children: /* @__PURE__ */ jsx(F, { defaultMessage: "Cancel", id: "edit-link-dialog.cancel" }) }) }),
|
|
@@ -123,7 +121,7 @@ const EditLinkDialogInner = track(function EditLinkDialogInner2({
|
|
|
123
121
|
TldrawUiButton,
|
|
124
122
|
{
|
|
125
123
|
type: "primary",
|
|
126
|
-
disabled: !urlInputState.valid,
|
|
124
|
+
disabled: !urlInputState.valid || isEmpty,
|
|
127
125
|
onTouchEnd: handleComplete,
|
|
128
126
|
onClick: handleComplete,
|
|
129
127
|
children: /* @__PURE__ */ jsx(TldrawUiButtonLabel, { children: /* @__PURE__ */ jsx(F, { defaultMessage: "Continue", id: "edit-link-dialog.save" }) })
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/lib/ui/components/EditLinkDialog.tsx"],
|
|
4
|
-
"sourcesContent": ["import { defineMessages,
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import { defineMessages, TLShape, track, useEditor } from '@tldraw/editor'\nimport { useCallback, useEffect, useRef, useState } from 'react'\nimport { normalizeLinkUrl } from '../../utils/linkUrl'\nimport { isShapeWithLink, TLShapeWithLink } from '../../utils/shapes/shapes'\nimport { TLUiDialogProps } from '../context/dialogs'\nimport { F } from '../context/i18n'\nimport { TldrawUiButton } from './primitives/Button/TldrawUiButton'\nimport { TldrawUiButtonLabel } from './primitives/Button/TldrawUiButtonLabel'\nimport {\n\tTldrawUiDialogBody,\n\tTldrawUiDialogCloseButton,\n\tTldrawUiDialogFooter,\n\tTldrawUiDialogHeader,\n\tTldrawUiDialogTitle,\n} from './primitives/TldrawUiDialog'\nimport { TldrawUiInput } from './primitives/TldrawUiInput'\n\nfunction getUrlInputState(url: string) {\n\tconst normalized = normalizeLinkUrl(url)\n\treturn { actual: url, safe: normalized ?? '', valid: normalized !== null }\n}\n\nfunction assertShapeWithLink(shape: TLShape | null | undefined): asserts shape is TLShapeWithLink {\n\tif (!isShapeWithLink(shape)) {\n\t\tthrow new Error('Shape is not a valid TLShapeWithLink')\n\t}\n}\n\nconst messages = defineMessages({\n\t// `TldrawUiInput` takes its label as data, not as an element, so this one can't be an `<F>`.\n\turl: { id: 'edit-link-dialog.url', defaultMessage: 'URL' },\n})\n\nexport const EditLinkDialog = track(function EditLinkDialog({ onClose }: TLUiDialogProps) {\n\tconst editor = useEditor()\n\n\tconst selectedShape = editor.getOnlySelectedShape()\n\tconst hasShapeWithLink = isShapeWithLink(selectedShape)\n\n\t// The shape can be deleted or deselected from under the open dialog (a collaborator, or a\n\t// keypress reaching the canvas). Returning null on its own would leave an empty dialog frame.\n\tuseEffect(() => {\n\t\tif (!hasShapeWithLink) onClose()\n\t}, [hasShapeWithLink, onClose])\n\n\tif (!hasShapeWithLink) {\n\t\treturn null\n\t}\n\n\treturn <EditLinkDialogInner onClose={onClose} selectedShape={selectedShape} />\n})\n\nexport const EditLinkDialogInner = track(function EditLinkDialogInner({\n\tonClose,\n\tselectedShape,\n}: TLUiDialogProps & { selectedShape: TLShapeWithLink }) {\n\tconst editor = useEditor()\n\n\tconst rInput = useRef<HTMLInputElement>(null)\n\n\tuseEffect(() => {\n\t\teditor.timers.requestAnimationFrame(() => rInput.current?.focus())\n\t}, [editor])\n\n\tconst rInitialValue = useRef(selectedShape.props.url)\n\n\tconst [urlInputState, setUrlInputState] = useState(() => {\n\t\tconst { safe } = getUrlInputState(selectedShape.props.url)\n\t\treturn { actual: safe, safe, valid: true }\n\t})\n\n\tconst [showInvalid, setShowInvalid] = useState(false)\n\n\tconst handleChange = useCallback((rawValue: string) => {\n\t\tsetUrlInputState(getUrlInputState(rawValue))\n\t\tsetShowInvalid(false)\n\t}, [])\n\n\tconst handleClear = useCallback(() => {\n\t\tconst onlySelectedShape = editor.getOnlySelectedShape()\n\t\tif (!onlySelectedShape) return\n\t\tassertShapeWithLink(onlySelectedShape)\n\t\teditor.updateShapes([\n\t\t\t{ id: onlySelectedShape.id, type: onlySelectedShape.type, props: { url: '' } },\n\t\t])\n\t\tonClose()\n\t}, [editor, onClose])\n\n\tconst handleComplete = useCallback(() => {\n\t\tconst onlySelectedShape = editor.getOnlySelectedShape()\n\n\t\tif (!onlySelectedShape) return\n\t\tassertShapeWithLink(onlySelectedShape)\n\n\t\t// Enter reaches here even while the save button is disabled.\n\t\tif (!urlInputState.valid) {\n\t\t\tsetShowInvalid(true)\n\t\t\trInput.current?.focus()\n\t\t\treturn\n\t\t}\n\n\t\tif (onlySelectedShape.props.url !== urlInputState.safe) {\n\t\t\teditor.updateShapes([\n\t\t\t\t{\n\t\t\t\t\tid: onlySelectedShape.id,\n\t\t\t\t\ttype: onlySelectedShape.type,\n\t\t\t\t\tprops: { url: urlInputState.safe },\n\t\t\t\t},\n\t\t\t])\n\t\t}\n\t\tonClose()\n\t}, [editor, onClose, urlInputState])\n\n\tconst handleCancel = useCallback(() => {\n\t\tonClose()\n\t}, [onClose])\n\n\tconst isEmpty = urlInputState.valid && urlInputState.safe === ''\n\t// Only an empty field clears a link, so a typo can't delete it.\n\tconst isRemoving = !!rInitialValue.current && isEmpty\n\n\treturn (\n\t\t<>\n\t\t\t<TldrawUiDialogHeader>\n\t\t\t\t<TldrawUiDialogTitle>\n\t\t\t\t\t<F defaultMessage=\"Edit link\" id=\"edit-link-dialog.title\" />\n\t\t\t\t</TldrawUiDialogTitle>\n\t\t\t\t<TldrawUiDialogCloseButton />\n\t\t\t</TldrawUiDialogHeader>\n\t\t\t<TldrawUiDialogBody>\n\t\t\t\t<div className=\"tlui-edit-link-dialog\">\n\t\t\t\t\t<TldrawUiInput\n\t\t\t\t\t\tref={rInput}\n\t\t\t\t\t\tclassName=\"tlui-edit-link-dialog__input\"\n\t\t\t\t\t\tlabel={messages.url.id}\n\t\t\t\t\t\tautoFocus\n\t\t\t\t\t\tautoSelect\n\t\t\t\t\t\tplaceholder=\"https://example.com\"\n\t\t\t\t\t\tvalue={urlInputState.actual}\n\t\t\t\t\t\tonValueChange={handleChange}\n\t\t\t\t\t\tonComplete={handleComplete}\n\t\t\t\t\t\tonCancel={handleCancel}\n\t\t\t\t\t/>\n\t\t\t\t\t<div>\n\t\t\t\t\t\t{showInvalid ? (\n\t\t\t\t\t\t\t<F defaultMessage=\"A link must be a valid URL.\" id=\"edit-link-dialog.invalid-url\" />\n\t\t\t\t\t\t) : (\n\t\t\t\t\t\t\t<F defaultMessage=\"Links will open in a new tab.\" id=\"edit-link-dialog.detail\" />\n\t\t\t\t\t\t)}\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t</TldrawUiDialogBody>\n\t\t\t<TldrawUiDialogFooter className=\"tlui-dialog__footer__actions\">\n\t\t\t\t<TldrawUiButton type=\"normal\" onClick={handleCancel} onTouchEnd={handleCancel}>\n\t\t\t\t\t<TldrawUiButtonLabel>\n\t\t\t\t\t\t<F defaultMessage=\"Cancel\" id=\"edit-link-dialog.cancel\" />\n\t\t\t\t\t</TldrawUiButtonLabel>\n\t\t\t\t</TldrawUiButton>\n\t\t\t\t{isRemoving ? (\n\t\t\t\t\t<TldrawUiButton type=\"danger\" onTouchEnd={handleClear} onClick={handleClear}>\n\t\t\t\t\t\t<TldrawUiButtonLabel>\n\t\t\t\t\t\t\t<F defaultMessage=\"Clear\" id=\"edit-link-dialog.clear\" />\n\t\t\t\t\t\t</TldrawUiButtonLabel>\n\t\t\t\t\t</TldrawUiButton>\n\t\t\t\t) : (\n\t\t\t\t\t<TldrawUiButton\n\t\t\t\t\t\ttype=\"primary\"\n\t\t\t\t\t\tdisabled={!urlInputState.valid || isEmpty}\n\t\t\t\t\t\tonTouchEnd={handleComplete}\n\t\t\t\t\t\tonClick={handleComplete}\n\t\t\t\t\t>\n\t\t\t\t\t\t<TldrawUiButtonLabel>\n\t\t\t\t\t\t\t<F defaultMessage=\"Continue\" id=\"edit-link-dialog.save\" />\n\t\t\t\t\t\t</TldrawUiButtonLabel>\n\t\t\t\t\t</TldrawUiButton>\n\t\t\t\t)}\n\t\t\t</TldrawUiDialogFooter>\n\t\t</>\n\t)\n})\n"],
|
|
5
|
+
"mappings": "AAiDQ,SAyEN,UAzEM,KA0EL,YA1EK;AAjDR,SAAS,gBAAyB,OAAO,iBAAiB;AAC1D,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AACzD,SAAS,wBAAwB;AACjC,SAAS,uBAAwC;AAEjD,SAAS,SAAS;AAClB,SAAS,sBAAsB;AAC/B,SAAS,2BAA2B;AACpC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,qBAAqB;AAE9B,SAAS,iBAAiB,KAAa;AACtC,QAAM,aAAa,iBAAiB,GAAG;AACvC,SAAO,EAAE,QAAQ,KAAK,MAAM,cAAc,I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UACpB,WAAS;AAAA,UACT,YAAU;AAAA,UACV,aAAY;AAAA,UACZ,OAAO,cAAc;AAAA,UACrB,eAAe;AAAA,UACf,YAAY;AAAA,UACZ,UAAU;AAAA;AAAA,MACX;AAAA,MACA,oBAAC,SACC,wBACA,oBAAC,KAAE,gBAAe,+BAA8B,IAAG,gCAA+B,IAElF,oBAAC,KAAE,gBAAe,iCAAgC,IAAG,2BAA0B,GAEjF;AAAA,OACD,GACD;AAAA,IACA,qBAAC,wBAAqB,WAAU,gCAC/B;AAAA,0BAAC,kBAAe,MAAK,UAAS,SAAS,cAAc,YAAY,cAChE,8BAAC,uBACA,8BAAC,KAAE,gBAAe,UAAS,IAAG,2BAA0B,GACzD,GACD;AAAA,MACC,aACA,oBAAC,kBAAe,MAAK,UAAS,YAAY,aAAa,SAAS,aAC/D,8BAAC,uBACA,8BAAC,KAAE,gBAAe,SAAQ,IAAG,0BAAyB,GACvD,GACD,IAEA;AAAA,QAAC;AAAA;AAAA,UACA,MAAK;AAAA,UACL,UAAU,CAAC,cAAc,SAAS;AAAA,UAClC,YAAY;AAAA,UACZ,SAAS;AAAA,UAET,8BAAC,uBACA,8BAAC,KAAE,gBAAe,YAAW,IAAG,yBAAwB,GACzD;AAAA;AAAA,MACD;AAAA,OAEF;AAAA,KACD;AAEF,CAAC;",
|
|
6
6
|
"names": ["EditLinkDialog", "EditLinkDialogInner"]
|
|
7
7
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { defineMessages, openWindow, preventDefault, useEditor } from "@tldraw/editor";
|
|
3
|
+
import classNames from "classnames";
|
|
3
4
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
|
+
import { normalizeLinkUrl } from "../../../utils/linkUrl.mjs";
|
|
4
6
|
import { useUiEvents } from "../../context/events.mjs";
|
|
5
7
|
import { useTranslation } from "../../hooks/useTranslation/useTranslation.mjs";
|
|
6
8
|
import { TldrawUiButton } from "../primitives/Button/TldrawUiButton.mjs";
|
|
@@ -13,18 +15,31 @@ const messages = defineMessages({
|
|
|
13
15
|
function LinkEditor({ textEditor, value: initialValue, onClose }) {
|
|
14
16
|
const editor = useEditor();
|
|
15
17
|
const [value, setValue] = useState(initialValue);
|
|
18
|
+
const [isInvalid, setIsInvalid] = useState(false);
|
|
16
19
|
const msg = useTranslation();
|
|
17
20
|
const ref = useRef(null);
|
|
18
21
|
const trackEvent = useUiEvents();
|
|
19
22
|
const source = "rich-text-menu";
|
|
20
|
-
const
|
|
23
|
+
const normalizedValue = normalizeLinkUrl(value);
|
|
24
|
+
const handleRemoveLink = useCallback(() => {
|
|
25
|
+
trackEvent("rich-text", { operation: "link-remove", source });
|
|
26
|
+
textEditor.chain().unsetLink().focus().run();
|
|
27
|
+
onClose();
|
|
28
|
+
}, [trackEvent, source, textEditor, onClose]);
|
|
21
29
|
const handleLinkComplete = useCallback(
|
|
22
30
|
(link) => {
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
31
|
+
const href = normalizeLinkUrl(link);
|
|
32
|
+
if (href === null) {
|
|
33
|
+
setIsInvalid(true);
|
|
34
|
+
ref.current?.focus();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (href === "") {
|
|
38
|
+
handleRemoveLink();
|
|
39
|
+
return;
|
|
26
40
|
}
|
|
27
|
-
|
|
41
|
+
trackEvent("rich-text", { operation: "link-edit", source });
|
|
42
|
+
textEditor.chain().setLink({ href }).run();
|
|
28
43
|
if (editor.getInstanceState().isCoarsePointer) {
|
|
29
44
|
textEditor.commands.blur();
|
|
30
45
|
} else {
|
|
@@ -32,49 +47,51 @@ function LinkEditor({ textEditor, value: initialValue, onClose }) {
|
|
|
32
47
|
}
|
|
33
48
|
onClose();
|
|
34
49
|
},
|
|
35
|
-
[trackEvent, source, textEditor, editor, onClose]
|
|
50
|
+
[trackEvent, source, textEditor, editor, onClose, handleRemoveLink]
|
|
36
51
|
);
|
|
37
52
|
const handleVisitLink = () => {
|
|
38
53
|
trackEvent("rich-text", { operation: "link-visit", source });
|
|
39
|
-
openWindow(
|
|
54
|
+
if (normalizedValue) openWindow(normalizedValue, "_blank");
|
|
40
55
|
onClose();
|
|
41
56
|
};
|
|
42
|
-
const handleRemoveLink = useCallback(() => {
|
|
43
|
-
trackEvent("rich-text", { operation: "link-remove", source });
|
|
44
|
-
textEditor.chain().unsetLink().focus().run();
|
|
45
|
-
onClose();
|
|
46
|
-
}, [trackEvent, source, textEditor, onClose]);
|
|
47
57
|
useEffect(() => {
|
|
48
58
|
const doc = editor.getContainerDocument();
|
|
49
59
|
const handlePointerDown = (e) => {
|
|
50
60
|
const toolbar = doc.querySelector(".tlui-rich-text__toolbar");
|
|
51
61
|
if (toolbar?.contains(e.target)) return;
|
|
52
|
-
if (
|
|
53
|
-
|
|
62
|
+
if (normalizedValue === null) {
|
|
63
|
+
onClose();
|
|
54
64
|
} else {
|
|
55
|
-
|
|
65
|
+
handleLinkComplete(value);
|
|
56
66
|
}
|
|
57
67
|
};
|
|
58
68
|
doc.addEventListener("pointerdown", handlePointerDown, { capture: true });
|
|
59
69
|
return () => {
|
|
60
70
|
doc.removeEventListener("pointerdown", handlePointerDown, { capture: true });
|
|
61
71
|
};
|
|
62
|
-
}, [editor, handleLinkComplete,
|
|
72
|
+
}, [editor, handleLinkComplete, onClose, normalizedValue, value]);
|
|
63
73
|
useEffect(() => {
|
|
64
74
|
ref.current?.focus();
|
|
65
75
|
}, [value]);
|
|
66
76
|
useEffect(() => {
|
|
67
77
|
setValue(initialValue);
|
|
78
|
+
setIsInvalid(false);
|
|
68
79
|
}, [initialValue]);
|
|
80
|
+
const handleValueChange = useCallback((nextValue) => {
|
|
81
|
+
setValue(nextValue);
|
|
82
|
+
setIsInvalid(false);
|
|
83
|
+
}, []);
|
|
69
84
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
70
85
|
/* @__PURE__ */ jsx(
|
|
71
86
|
TldrawUiInput,
|
|
72
87
|
{
|
|
73
88
|
ref,
|
|
74
89
|
"data-testid": "rich-text.link-input",
|
|
75
|
-
className: "tlui-rich-text__toolbar-link-input",
|
|
90
|
+
className: classNames("tlui-rich-text__toolbar-link-input", {
|
|
91
|
+
"tlui-rich-text__toolbar-link-input--invalid": isInvalid
|
|
92
|
+
}),
|
|
76
93
|
value,
|
|
77
|
-
onValueChange:
|
|
94
|
+
onValueChange: handleValueChange,
|
|
78
95
|
onComplete: handleLinkComplete,
|
|
79
96
|
onCancel: onClose,
|
|
80
97
|
placeholder: "example.com",
|
|
@@ -89,7 +106,7 @@ function LinkEditor({ textEditor, value: initialValue, onClose }) {
|
|
|
89
106
|
type: "icon",
|
|
90
107
|
onPointerDown: preventDefault,
|
|
91
108
|
onClick: handleVisitLink,
|
|
92
|
-
disabled: !
|
|
109
|
+
disabled: !normalizedValue,
|
|
93
110
|
children: /* @__PURE__ */ jsx(TldrawUiButtonIcon, { small: true, icon: "external-link" })
|
|
94
111
|
}
|
|
95
112
|
),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../src/lib/ui/components/Toolbar/LinkEditor.tsx"],
|
|
4
|
-
"sourcesContent": ["import { defineMessages, openWindow, preventDefault, TiptapEditor, useEditor } from '@tldraw/editor'\nimport { useCallback, useEffect, useRef, useState } from 'react'\nimport { useUiEvents } from '../../context/events'\nimport { useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiButton } from '../primitives/Button/TldrawUiButton'\nimport { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'\nimport { TldrawUiInput } from '../primitives/TldrawUiInput'\n\nconst messages = defineMessages({\n\ttoolRichTextLinkRemove: { id: 'tool.rich-text-link-remove', defaultMessage: 'Remove link' },\n\ttoolRichTextLinkVisit: { id: 'tool.rich-text-link-visit', defaultMessage: 'Visit link' },\n})\n\n/** @public */\nexport interface LinkEditorProps {\n\ttextEditor: TiptapEditor\n\tvalue: string\n\tonClose(): void\n}\n\n/** @public @react */\nexport function LinkEditor({ textEditor, value: initialValue, onClose }: LinkEditorProps) {\n\tconst editor = useEditor()\n\tconst [value, setValue] = useState(initialValue)\n\tconst msg = useTranslation()\n\tconst ref = useRef<HTMLInputElement>(null)\n\tconst trackEvent = useUiEvents()\n\tconst source = 'rich-text-menu'\n\tconst
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import { defineMessages, openWindow, preventDefault, TiptapEditor, useEditor } from '@tldraw/editor'\nimport classNames from 'classnames'\nimport { useCallback, useEffect, useRef, useState } from 'react'\nimport { normalizeLinkUrl } from '../../../utils/linkUrl'\nimport { useUiEvents } from '../../context/events'\nimport { useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiButton } from '../primitives/Button/TldrawUiButton'\nimport { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'\nimport { TldrawUiInput } from '../primitives/TldrawUiInput'\n\nconst messages = defineMessages({\n\ttoolRichTextLinkRemove: { id: 'tool.rich-text-link-remove', defaultMessage: 'Remove link' },\n\ttoolRichTextLinkVisit: { id: 'tool.rich-text-link-visit', defaultMessage: 'Visit link' },\n})\n\n/** @public */\nexport interface LinkEditorProps {\n\ttextEditor: TiptapEditor\n\tvalue: string\n\tonClose(): void\n}\n\n/** @public @react */\nexport function LinkEditor({ textEditor, value: initialValue, onClose }: LinkEditorProps) {\n\tconst editor = useEditor()\n\tconst [value, setValue] = useState(initialValue)\n\tconst [isInvalid, setIsInvalid] = useState(false)\n\tconst msg = useTranslation()\n\tconst ref = useRef<HTMLInputElement>(null)\n\tconst trackEvent = useUiEvents()\n\tconst source = 'rich-text-menu'\n\tconst normalizedValue = normalizeLinkUrl(value)\n\n\tconst handleRemoveLink = useCallback(() => {\n\t\ttrackEvent('rich-text', { operation: 'link-remove', source })\n\t\ttextEditor.chain().unsetLink().focus().run()\n\t\tonClose()\n\t}, [trackEvent, source, textEditor, onClose])\n\n\tconst handleLinkComplete = useCallback(\n\t\t(link: string) => {\n\t\t\tconst href = normalizeLinkUrl(link)\n\t\t\tif (href === null) {\n\t\t\t\tsetIsInvalid(true)\n\t\t\t\tref.current?.focus()\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (href === '') {\n\t\t\t\thandleRemoveLink()\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\ttrackEvent('rich-text', { operation: 'link-edit', source })\n\t\t\ttextEditor.chain().setLink({ href }).run()\n\t\t\t// N.B. We shouldn't focus() on mobile because it causes the\n\t\t\t// Return key to replace the link with a newline :facepalm:\n\t\t\tif (editor.getInstanceState().isCoarsePointer) {\n\t\t\t\ttextEditor.commands.blur()\n\t\t\t} else {\n\t\t\t\ttextEditor.commands.focus()\n\t\t\t}\n\t\t\tonClose()\n\t\t},\n\t\t[trackEvent, source, textEditor, editor, onClose, handleRemoveLink]\n\t)\n\n\tconst handleVisitLink = () => {\n\t\ttrackEvent('rich-text', { operation: 'link-visit', source })\n\t\tif (normalizedValue) openWindow(normalizedValue, '_blank')\n\t\tonClose()\n\t}\n\n\tuseEffect(() => {\n\t\tconst doc = editor.getContainerDocument()\n\t\tconst handlePointerDown = (e: PointerEvent) => {\n\t\t\tconst toolbar = doc.querySelector('.tlui-rich-text__toolbar')\n\t\t\tif (toolbar?.contains(e.target as Node)) return\n\t\t\t// If the pointer down is not in the toolbar, complete the link\n\t\t\tif (normalizedValue === null) {\n\t\t\t\tonClose()\n\t\t\t} else {\n\t\t\t\thandleLinkComplete(value)\n\t\t\t}\n\t\t}\n\t\tdoc.addEventListener('pointerdown', handlePointerDown, { capture: true })\n\t\treturn () => {\n\t\t\tdoc.removeEventListener('pointerdown', handlePointerDown, { capture: true })\n\t\t}\n\t}, [editor, handleLinkComplete, onClose, normalizedValue, value])\n\n\tuseEffect(() => {\n\t\tref.current?.focus()\n\t}, [value])\n\n\tuseEffect(() => {\n\t\tsetValue(initialValue)\n\t\tsetIsInvalid(false)\n\t}, [initialValue])\n\n\tconst handleValueChange = useCallback((nextValue: string) => {\n\t\tsetValue(nextValue)\n\t\tsetIsInvalid(false)\n\t}, [])\n\n\treturn (\n\t\t<>\n\t\t\t<TldrawUiInput\n\t\t\t\tref={ref}\n\t\t\t\tdata-testid=\"rich-text.link-input\"\n\t\t\t\tclassName={classNames('tlui-rich-text__toolbar-link-input', {\n\t\t\t\t\t'tlui-rich-text__toolbar-link-input--invalid': isInvalid,\n\t\t\t\t})}\n\t\t\t\tvalue={value}\n\t\t\t\tonValueChange={handleValueChange}\n\t\t\t\tonComplete={handleLinkComplete}\n\t\t\t\tonCancel={onClose}\n\t\t\t\tplaceholder=\"example.com\"\n\t\t\t\taria-label=\"example.com\"\n\t\t\t/>\n\t\t\t<TldrawUiButton\n\t\t\t\tclassName=\"tlui-rich-text__toolbar-link-visit\"\n\t\t\t\ttitle={msg(messages.toolRichTextLinkVisit)}\n\t\t\t\ttype=\"icon\"\n\t\t\t\tonPointerDown={preventDefault}\n\t\t\t\tonClick={handleVisitLink}\n\t\t\t\tdisabled={!normalizedValue}\n\t\t\t>\n\t\t\t\t<TldrawUiButtonIcon small icon=\"external-link\" />\n\t\t\t</TldrawUiButton>\n\t\t\t<TldrawUiButton\n\t\t\t\tclassName=\"tlui-rich-text__toolbar-link-remove\"\n\t\t\t\ttitle={msg(messages.toolRichTextLinkRemove)}\n\t\t\t\tdata-testid=\"rich-text.link-remove\"\n\t\t\t\ttype=\"icon\"\n\t\t\t\tonPointerDown={preventDefault}\n\t\t\t\tonClick={handleRemoveLink}\n\t\t\t>\n\t\t\t\t<TldrawUiButtonIcon small icon=\"trash\" />\n\t\t\t</TldrawUiButton>\n\t\t</>\n\t)\n}\n"],
|
|
5
|
+
"mappings": "AAyGE,mBACC,KADD;AAzGF,SAAS,gBAAgB,YAAY,gBAA8B,iBAAiB;AACpF,OAAO,gBAAgB;AACvB,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AACzD,SAAS,wBAAwB;AACjC,SAAS,mBAAmB;A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AAA,IACA;AAAA,MAAC;AAAA;AAAA,QACA,WAAU;AAAA,QACV,OAAO,IAAI,SAAS,qBAAqB;AAAA,QACzC,MAAK;AAAA,QACL,eAAe;AAAA,QACf,SAAS;AAAA,QACT,UAAU,CAAC;AAAA,QAEX,8BAAC,sBAAmB,OAAK,MAAC,MAAK,iBAAgB;AAAA;AAAA,IAChD;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QACA,WAAU;AAAA,QACV,OAAO,IAAI,SAAS,sBAAsB;AAAA,QAC1C,eAAY;AAAA,QACZ,MAAK;AAAA,QACL,eAAe;AAAA,QACf,SAAS;AAAA,QAET,8BAAC,sBAAmB,OAAK,MAAC,MAAK,SAAQ;AAAA;AAAA,IACxC;AAAA,KACD;AAEF;",
|
|
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.1'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-10-28T14:20:57.238Z',\n\tpatch: '2026-11-16T18:57:15.106Z',\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,19 @@
|
|
|
1
|
+
import { T } from "@tldraw/editor";
|
|
2
|
+
const emailRegex = /^[^\s@/:]+@[^\s@/:]+\.[^\s@/:]+$/;
|
|
3
|
+
function normalizeLinkUrl(input) {
|
|
4
|
+
const url = input.trim().replace(/^https?:\/\/(https?:\/\/)/i, "$1");
|
|
5
|
+
if (T.linkUrl.isValid(url)) return url;
|
|
6
|
+
if (emailRegex.test(url)) return `mailto:${url}`;
|
|
7
|
+
const withProtocol = `https://${url}`;
|
|
8
|
+
if (!T.linkUrl.isValid(withProtocol)) return null;
|
|
9
|
+
const { hostname, username } = new URL(withProtocol);
|
|
10
|
+
if (username) return null;
|
|
11
|
+
if (hostname === "localhost" || hostname.startsWith("[") || /[^.]\.[^.]+$/.test(hostname)) {
|
|
12
|
+
return withProtocol;
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
normalizeLinkUrl
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=linkUrl.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/utils/linkUrl.ts"],
|
|
4
|
+
"sourcesContent": ["import { T } from '@tldraw/editor'\n\nconst emailRegex = /^[^\\s@/:]+@[^\\s@/:]+\\.[^\\s@/:]+$/\n\n/**\n * Turn what a user typed into a link field into the url to store, or null if it isn't a link.\n *\n * - Anything `T.linkUrl` accepts is kept as typed, so `mailto:` and app links (`vscode://\u2026`)\n * are never rewritten.\n * - An email address becomes a `mailto:` link.\n * - A bare host (`example.com`, `localhost:3000`) gets `https://`. A word with no dot, like\n * `hello`, is not treated as a host, or every typo would become a dead `https://hello` link.\n * - The empty string means \"no link\".\n */\nexport function normalizeLinkUrl(input: string): string | null {\n\t// Pasting a url into a field that already holds `https://` doubles the protocol.\n\tconst url = input.trim().replace(/^https?:\\/\\/(https?:\\/\\/)/i, '$1')\n\n\tif (T.linkUrl.isValid(url)) return url\n\tif (emailRegex.test(url)) return `mailto:${url}`\n\n\tconst withProtocol = `https://${url}`\n\tif (!T.linkUrl.isValid(withProtocol)) return null\n\tconst { hostname, username } = new URL(withProtocol)\n\tif (username) return null\n\tif (hostname === 'localhost' || hostname.startsWith('[') || /[^.]\\.[^.]+$/.test(hostname)) {\n\t\treturn withProtocol\n\t}\n\treturn null\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS;AAElB,MAAM,aAAa;AAYZ,SAAS,iBAAiB,OAA8B;AAE9D,QAAM,MAAM,MAAM,KAAK,EAAE,QAAQ,8BAA8B,IAAI;AAEnE,MAAI,EAAE,QAAQ,QAAQ,GAAG,EAAG,QAAO;AACnC,MAAI,WAAW,KAAK,GAAG,EAAG,QAAO,UAAU,GAAG;AAE9C,QAAM,eAAe,WAAW,GAAG;AACnC,MAAI,CAAC,EAAE,QAAQ,QAAQ,YAAY,EAAG,QAAO;AAC7C,QAAM,EAAE,UAAU,SAAS,IAAI,IAAI,IAAI,YAAY;AACnD,MAAI,SAAU,QAAO;AACrB,MAAI,aAAa,eAAe,SAAS,WAAW,GAAG,KAAK,eAAe,KAAK,QAAQ,GAAG;AAC1F,WAAO;AAAA,EACR;AACA,SAAO;AACR;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -11,6 +11,7 @@ import { Typography } from "@tiptap/extension-typography";
|
|
|
11
11
|
import { StarterKit } from "@tiptap/starter-kit";
|
|
12
12
|
import {
|
|
13
13
|
getOwnProperty,
|
|
14
|
+
T,
|
|
14
15
|
WeakCache
|
|
15
16
|
} from "@tldraw/editor";
|
|
16
17
|
import { DefaultFontFaces } from "../../shapes/shared/defaultFonts.mjs";
|
|
@@ -35,7 +36,10 @@ function getTipTapDefaultExtensions(starterKitOptions) {
|
|
|
35
36
|
horizontalRule: false,
|
|
36
37
|
link: {
|
|
37
38
|
openOnClick: false,
|
|
38
|
-
autolink: true
|
|
39
|
+
autolink: true,
|
|
40
|
+
// TipTap strips hrefs outside its own protocol list, which would drop app links
|
|
41
|
+
// (`vscode://…`) that shape links accept.
|
|
42
|
+
isAllowedUri: (url, { defaultValidate }) => defaultValidate(url) || T.linkUrl.isValid(url)
|
|
39
43
|
},
|
|
40
44
|
// List every enabled block type: a missing one gets an empty paragraph appended after it
|
|
41
45
|
// that the user can't delete (#7641 for lists, #11113 for headings).
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/lib/utils/text/richText.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n\tExtension,\n\tExtensions,\n\textensions,\n\tgenerateHTML,\n\tgenerateJSON,\n\tgenerateText,\n\tJSONContent,\n} from '@tiptap/core'\nimport { Code } from '@tiptap/extension-code'\nimport { Highlight } from '@tiptap/extension-highlight'\nimport { Typography } from '@tiptap/extension-typography'\nimport { Node } from '@tiptap/pm/model'\nimport { StarterKit, type StarterKitOptions } from '@tiptap/starter-kit'\nimport {\n\tEditor,\n\tgetOwnProperty,\n\tRichTextFontVisitorState,\n\tTLFontFace,\n\tTLRichText,\n\tWeakCache,\n} from '@tldraw/editor'\nimport { DefaultFontFaces } from '../../shapes/shared/defaultFonts'\nimport { LiteralTypingExtension } from './literalTyping'\nimport { WrapSelectionExtension } from './wrapSelection'\n\n/** @public */\nexport const KeyboardShiftEnterTweakExtension = Extension.create({\n\tname: 'keyboardShiftEnterHandler',\n\taddKeyboardShortcuts() {\n\t\treturn {\n\t\t\t// We don't support soft breaks, so we just use the default enter command.\n\t\t\t'Shift-Enter': ({ editor }) => editor.commands.enter(),\n\t\t}\n\t},\n})\n\n// We change the default Code to override what's in the StarterKit.\n// It allows for other attributes/extensions.\n// @ts-ignore this is fine.\nCode.config.excludes = undefined\n\n// We want the highlighting to take precedence over bolding/italics/links\n// as far as rendering is concerned. Otherwise, the highlighting\n// looks broken up.\nHighlight.config.priority = 1100\n\n/**\n * Build tldraw's default TipTap extension set, optionally overriding the bundled `StarterKit`\n * options. The one lever most consumers want is turning individual nodes off (e.g. comments use a\n * headingless set via `getTipTapDefaultExtensions({ heading: false })`); because `StarterKit` is a\n * single umbrella extension, its sub-extensions can only be disabled through its config, not by\n * filtering the returned array.\n *\n * @public\n */\nexport function getTipTapDefaultExtensions(\n\tstarterKitOptions?: Partial<StarterKitOptions>\n): Extensions {\n\treturn [\n\t\tStarterKit.configure({\n\t\t\tblockquote: false,\n\t\t\tcodeBlock: false,\n\t\t\thorizontalRule: false,\n\t\t\tlink: {\n\t\t\t\topenOnClick: false,\n\t\t\t\tautolink: true,\n\t\t\t},\n\t\t\t// List every enabled block type: a missing one gets an empty paragraph appended after it\n\t\t\t// that the user can't delete (#7641 for lists, #11113 for headings).\n\t\t\ttrailingNode: {\n\t\t\t\tnotAfter: ['paragraph', 'heading', 'bulletList', 'orderedList', 'listItem'],\n\t\t\t},\n\t\t\t...starterKitOptions,\n\t\t}),\n\t\tHighlight,\n\t\tTypography,\n\t\tLiteralTypingExtension,\n\t\tWrapSelectionExtension,\n\t\tKeyboardShiftEnterTweakExtension,\n\n\t\t// N.B. We disable the text direction core extension in RichTextArea,\n\t\t// but we add it back in again here in our own extensions list so that\n\t\t// people can omit/override it if they want to.\n\t\textensions.TextDirection.configure({ direction: 'auto' }),\n\t]\n}\n\n/**\n * Default extensions for the TipTap editor.\n *\n * @public\n */\nexport const tipTapDefaultExtensions: Extensions = getTipTapDefaultExtensions()\n\n// todo: bust this if the editor changes, too\nconst htmlCache = new WeakCache<TLRichText, string>()\n\n/**\n * Renders HTML from a rich text string using an explicit set of TipTap extensions, rather than the\n * ones configured on an editor. Use this when rendering rich text outside of a shape's editor\n * config (e.g. comments, which render through their own headingless extension set).\n *\n * @param richText - The rich text content.\n * @param extensions - The TipTap extensions to render with.\n *\n * @public\n */\nexport function renderHtmlFromRichTextWithExtensions(\n\trichText: TLRichText,\n\textensions: Extensions\n): string {\n\tconst html = generateHTML(richText as JSONContent, extensions)\n\t// We replace empty paragraphs with a single line break to prevent the browser from collapsing\n\t// them. The paragraph's attributes are kept: paragraphs render with a `dir` attribute, usually\n\t// `auto` but `ltr` or `rtl` when the direction was set explicitly or parsed from pasted HTML.\n\treturn html.replace(/<p([^>]*)><\\/p>/g, '<p$1><br /></p>')\n}\n\n/**\n * Renders HTML from a rich text string.\n *\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderHtmlFromRichText(editor: Editor, richText: TLRichText) {\n\treturn htmlCache.get(richText, () => {\n\t\tconst tipTapExtensions =\n\t\t\teditor.getTextOptions().tipTapConfig?.extensions ?? tipTapDefaultExtensions\n\t\treturn renderHtmlFromRichTextWithExtensions(richText, tipTapExtensions)\n\t})\n}\n\n/**\n * Renders HTML from a rich text string for measurement.\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderHtmlFromRichTextForMeasurement(editor: Editor, richText: TLRichText) {\n\tconst html = renderHtmlFromRichText(editor, richText)\n\treturn `<div class=\"tl-rich-text\">${html}</div>`\n}\n\n// A weak cache used to store plaintext that's been extracted from rich text.\nconst plainTextFromRichTextCache = new WeakCache<TLRichText, string>()\n\nexport function isEmptyRichText(richText: TLRichText) {\n\t// An empty document has no text. It can be encoded several equally-valid ways:\n\t// an empty `content` array at the doc level, or a single paragraph whose own\n\t// `content` is missing or an empty array. The interactive editor emits the\n\t// single-paragraph / missing-`content` form; programmatic authoring (snapshot\n\t// loads, and agents/importers emitting tldraw JSON) commonly emits the\n\t// empty-array forms. Treat them all as empty.\n\tif (richText.content.length === 0) return true\n\tif (richText.content.length === 1) {\n\t\tconst node = richText.content[0] as any\n\t\tif (!node.content || node.content.length === 0) return true\n\t}\n\treturn false\n}\n\n/**\n * Whether the editor's active rich text selection is inside a list.\n *\n * `taskList` isn't in the default extension set, but when it is added its list items bind Tab and\n * Shift-Tab themselves. Leaving it out here lets our own Tab handling run alongside TipTap's, so a\n * single keypress both indents the text and nests the item.\n *\n * @internal\n */\nexport function isEditingRichTextList(editor: Editor) {\n\tconst textEditor = editor.getRichTextEditor()\n\treturn !!(\n\t\ttextEditor?.isActive('bulletList') ||\n\t\ttextEditor?.isActive('orderedList') ||\n\t\ttextEditor?.isActive('taskList')\n\t)\n}\n\n/**\n * Renders plaintext from a rich text string.\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderPlaintextFromRichText(editor: Editor, richText: TLRichText) {\n\tif (isEmptyRichText(richText)) return ''\n\n\treturn plainTextFromRichTextCache.get(richText, () => {\n\t\tconst tipTapExtensions =\n\t\t\teditor.getTextOptions().tipTapConfig?.extensions ?? tipTapDefaultExtensions\n\t\treturn generateText(richText as JSONContent, tipTapExtensions, {\n\t\t\tblockSeparator: '\\n',\n\t\t})\n\t})\n}\n\n/**\n * Renders JSONContent from html.\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderRichTextFromHTML(editor: Editor, html: string): TLRichText {\n\tconst tipTapExtensions =\n\t\teditor.getTextOptions().tipTapConfig?.extensions ?? tipTapDefaultExtensions\n\treturn generateJSON(html, tipTapExtensions) as TLRichText\n}\n\n/** @public */\nexport function defaultAddFontsFromNode(\n\tnode: Node,\n\tstate: RichTextFontVisitorState,\n\taddFont: (font: TLFontFace) => void\n) {\n\tfor (const mark of node.marks) {\n\t\tif (mark.type.name === 'bold' && state.weight !== 'bold') {\n\t\t\tstate = { ...state, weight: 'bold' }\n\t\t}\n\t\tif (mark.type.name === 'italic' && state.style !== 'italic') {\n\t\t\tstate = { ...state, style: 'italic' }\n\t\t}\n\t\tif (mark.type.name === 'code' && state.family !== 'tldraw_mono') {\n\t\t\tstate = { ...state, family: 'tldraw_mono' }\n\t\t}\n\t}\n\n\tconst fontsForFamily = getOwnProperty(DefaultFontFaces, state.family)\n\tif (!fontsForFamily) return state\n\n\tconst fontsForStyle = getOwnProperty(fontsForFamily, state.style)\n\tif (!fontsForStyle) return state\n\n\tconst fontsForWeight = getOwnProperty(fontsForStyle, state.weight)\n\tif (!fontsForWeight) return state\n\n\taddFont(fontsForWeight)\n\n\treturn state\n}\n"],
|
|
5
|
-
"mappings": "AAAA;AAAA,EACC;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEM;AACP,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAE3B,SAAS,kBAA0C;AACnD;AAAA,EAEC;AAAA,
|
|
4
|
+
"sourcesContent": ["import {\n\tExtension,\n\tExtensions,\n\textensions,\n\tgenerateHTML,\n\tgenerateJSON,\n\tgenerateText,\n\tJSONContent,\n} from '@tiptap/core'\nimport { Code } from '@tiptap/extension-code'\nimport { Highlight } from '@tiptap/extension-highlight'\nimport { Typography } from '@tiptap/extension-typography'\nimport { Node } from '@tiptap/pm/model'\nimport { StarterKit, type StarterKitOptions } from '@tiptap/starter-kit'\nimport {\n\tEditor,\n\tgetOwnProperty,\n\tRichTextFontVisitorState,\n\tT,\n\tTLFontFace,\n\tTLRichText,\n\tWeakCache,\n} from '@tldraw/editor'\nimport { DefaultFontFaces } from '../../shapes/shared/defaultFonts'\nimport { LiteralTypingExtension } from './literalTyping'\nimport { WrapSelectionExtension } from './wrapSelection'\n\n/** @public */\nexport const KeyboardShiftEnterTweakExtension = Extension.create({\n\tname: 'keyboardShiftEnterHandler',\n\taddKeyboardShortcuts() {\n\t\treturn {\n\t\t\t// We don't support soft breaks, so we just use the default enter command.\n\t\t\t'Shift-Enter': ({ editor }) => editor.commands.enter(),\n\t\t}\n\t},\n})\n\n// We change the default Code to override what's in the StarterKit.\n// It allows for other attributes/extensions.\n// @ts-ignore this is fine.\nCode.config.excludes = undefined\n\n// We want the highlighting to take precedence over bolding/italics/links\n// as far as rendering is concerned. Otherwise, the highlighting\n// looks broken up.\nHighlight.config.priority = 1100\n\n/**\n * Build tldraw's default TipTap extension set, optionally overriding the bundled `StarterKit`\n * options. The one lever most consumers want is turning individual nodes off (e.g. comments use a\n * headingless set via `getTipTapDefaultExtensions({ heading: false })`); because `StarterKit` is a\n * single umbrella extension, its sub-extensions can only be disabled through its config, not by\n * filtering the returned array.\n *\n * @public\n */\nexport function getTipTapDefaultExtensions(\n\tstarterKitOptions?: Partial<StarterKitOptions>\n): Extensions {\n\treturn [\n\t\tStarterKit.configure({\n\t\t\tblockquote: false,\n\t\t\tcodeBlock: false,\n\t\t\thorizontalRule: false,\n\t\t\tlink: {\n\t\t\t\topenOnClick: false,\n\t\t\t\tautolink: true,\n\t\t\t\t// TipTap strips hrefs outside its own protocol list, which would drop app links\n\t\t\t\t// (`vscode://\u2026`) that shape links accept.\n\t\t\t\tisAllowedUri: (url, { defaultValidate }) => defaultValidate(url) || T.linkUrl.isValid(url),\n\t\t\t},\n\t\t\t// List every enabled block type: a missing one gets an empty paragraph appended after it\n\t\t\t// that the user can't delete (#7641 for lists, #11113 for headings).\n\t\t\ttrailingNode: {\n\t\t\t\tnotAfter: ['paragraph', 'heading', 'bulletList', 'orderedList', 'listItem'],\n\t\t\t},\n\t\t\t...starterKitOptions,\n\t\t}),\n\t\tHighlight,\n\t\tTypography,\n\t\tLiteralTypingExtension,\n\t\tWrapSelectionExtension,\n\t\tKeyboardShiftEnterTweakExtension,\n\n\t\t// N.B. We disable the text direction core extension in RichTextArea,\n\t\t// but we add it back in again here in our own extensions list so that\n\t\t// people can omit/override it if they want to.\n\t\textensions.TextDirection.configure({ direction: 'auto' }),\n\t]\n}\n\n/**\n * Default extensions for the TipTap editor.\n *\n * @public\n */\nexport const tipTapDefaultExtensions: Extensions = getTipTapDefaultExtensions()\n\n// todo: bust this if the editor changes, too\nconst htmlCache = new WeakCache<TLRichText, string>()\n\n/**\n * Renders HTML from a rich text string using an explicit set of TipTap extensions, rather than the\n * ones configured on an editor. Use this when rendering rich text outside of a shape's editor\n * config (e.g. comments, which render through their own headingless extension set).\n *\n * @param richText - The rich text content.\n * @param extensions - The TipTap extensions to render with.\n *\n * @public\n */\nexport function renderHtmlFromRichTextWithExtensions(\n\trichText: TLRichText,\n\textensions: Extensions\n): string {\n\tconst html = generateHTML(richText as JSONContent, extensions)\n\t// We replace empty paragraphs with a single line break to prevent the browser from collapsing\n\t// them. The paragraph's attributes are kept: paragraphs render with a `dir` attribute, usually\n\t// `auto` but `ltr` or `rtl` when the direction was set explicitly or parsed from pasted HTML.\n\treturn html.replace(/<p([^>]*)><\\/p>/g, '<p$1><br /></p>')\n}\n\n/**\n * Renders HTML from a rich text string.\n *\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderHtmlFromRichText(editor: Editor, richText: TLRichText) {\n\treturn htmlCache.get(richText, () => {\n\t\tconst tipTapExtensions =\n\t\t\teditor.getTextOptions().tipTapConfig?.extensions ?? tipTapDefaultExtensions\n\t\treturn renderHtmlFromRichTextWithExtensions(richText, tipTapExtensions)\n\t})\n}\n\n/**\n * Renders HTML from a rich text string for measurement.\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderHtmlFromRichTextForMeasurement(editor: Editor, richText: TLRichText) {\n\tconst html = renderHtmlFromRichText(editor, richText)\n\treturn `<div class=\"tl-rich-text\">${html}</div>`\n}\n\n// A weak cache used to store plaintext that's been extracted from rich text.\nconst plainTextFromRichTextCache = new WeakCache<TLRichText, string>()\n\nexport function isEmptyRichText(richText: TLRichText) {\n\t// An empty document has no text. It can be encoded several equally-valid ways:\n\t// an empty `content` array at the doc level, or a single paragraph whose own\n\t// `content` is missing or an empty array. The interactive editor emits the\n\t// single-paragraph / missing-`content` form; programmatic authoring (snapshot\n\t// loads, and agents/importers emitting tldraw JSON) commonly emits the\n\t// empty-array forms. Treat them all as empty.\n\tif (richText.content.length === 0) return true\n\tif (richText.content.length === 1) {\n\t\tconst node = richText.content[0] as any\n\t\tif (!node.content || node.content.length === 0) return true\n\t}\n\treturn false\n}\n\n/**\n * Whether the editor's active rich text selection is inside a list.\n *\n * `taskList` isn't in the default extension set, but when it is added its list items bind Tab and\n * Shift-Tab themselves. Leaving it out here lets our own Tab handling run alongside TipTap's, so a\n * single keypress both indents the text and nests the item.\n *\n * @internal\n */\nexport function isEditingRichTextList(editor: Editor) {\n\tconst textEditor = editor.getRichTextEditor()\n\treturn !!(\n\t\ttextEditor?.isActive('bulletList') ||\n\t\ttextEditor?.isActive('orderedList') ||\n\t\ttextEditor?.isActive('taskList')\n\t)\n}\n\n/**\n * Renders plaintext from a rich text string.\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderPlaintextFromRichText(editor: Editor, richText: TLRichText) {\n\tif (isEmptyRichText(richText)) return ''\n\n\treturn plainTextFromRichTextCache.get(richText, () => {\n\t\tconst tipTapExtensions =\n\t\t\teditor.getTextOptions().tipTapConfig?.extensions ?? tipTapDefaultExtensions\n\t\treturn generateText(richText as JSONContent, tipTapExtensions, {\n\t\t\tblockSeparator: '\\n',\n\t\t})\n\t})\n}\n\n/**\n * Renders JSONContent from html.\n * @param editor - The editor instance.\n * @param richText - The rich text content.\n *\n * @public\n */\nexport function renderRichTextFromHTML(editor: Editor, html: string): TLRichText {\n\tconst tipTapExtensions =\n\t\teditor.getTextOptions().tipTapConfig?.extensions ?? tipTapDefaultExtensions\n\treturn generateJSON(html, tipTapExtensions) as TLRichText\n}\n\n/** @public */\nexport function defaultAddFontsFromNode(\n\tnode: Node,\n\tstate: RichTextFontVisitorState,\n\taddFont: (font: TLFontFace) => void\n) {\n\tfor (const mark of node.marks) {\n\t\tif (mark.type.name === 'bold' && state.weight !== 'bold') {\n\t\t\tstate = { ...state, weight: 'bold' }\n\t\t}\n\t\tif (mark.type.name === 'italic' && state.style !== 'italic') {\n\t\t\tstate = { ...state, style: 'italic' }\n\t\t}\n\t\tif (mark.type.name === 'code' && state.family !== 'tldraw_mono') {\n\t\t\tstate = { ...state, family: 'tldraw_mono' }\n\t\t}\n\t}\n\n\tconst fontsForFamily = getOwnProperty(DefaultFontFaces, state.family)\n\tif (!fontsForFamily) return state\n\n\tconst fontsForStyle = getOwnProperty(fontsForFamily, state.style)\n\tif (!fontsForStyle) return state\n\n\tconst fontsForWeight = getOwnProperty(fontsForStyle, state.weight)\n\tif (!fontsForWeight) return state\n\n\taddFont(fontsForWeight)\n\n\tretur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