@tldraw/editor
Version:
tldraw infinite canvas SDK (editor).
8 lines (7 loc) • 5.2 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/lib/utils/deepLinks.ts"],
"sourcesContent": ["import { BoxModel, PageRecordType, TLPageId, TLShapeId, createShapeId } from '@tldraw/tlschema'\nimport { exhaustiveSwitchError } from '@tldraw/utils'\nimport { Editor } from '../editor/Editor'\nimport { Box } from '../primitives/Box'\n\n/** @public */\nexport type TLDeepLink =\n\t| {\n\t\t\ttype: 'shapes'\n\t\t\tshapeIds: TLShapeId[]\n\t }\n\t| { type: 'viewport'; bounds: BoxModel; pageId?: TLPageId }\n\t| { type: 'page'; pageId: TLPageId }\n\n/**\n * Converts a deep link descriptor to a url-safe string\n *\n * @example\n * ```ts\n * const url = `https://example.com?d=${createDeepLinkString({ type: 'shapes', shapeIds: ['shape:1', 'shape:2'] })}`\n * navigator.clipboard.writeText(url)\n * ```\n *\n * @param deepLink - the deep link descriptor\n * @returns a url-safe string\n *\n * @public\n */\nexport function createDeepLinkString(deepLink: TLDeepLink): string {\n\tswitch (deepLink.type) {\n\t\tcase 'shapes': {\n\t\t\tconst ids = deepLink.shapeIds.map((id) => encodeId(id.slice('shape:'.length)))\n\t\t\treturn `s${ids.join('.')}`\n\t\t}\n\t\tcase 'page': {\n\t\t\treturn 'p' + encodeId(PageRecordType.parseId(deepLink.pageId))\n\t\t}\n\t\tcase 'viewport': {\n\t\t\tconst { bounds, pageId } = deepLink\n\t\t\tlet res = `v${Math.round(bounds.x)}.${Math.round(bounds.y)}.${Math.round(bounds.w)}.${Math.round(bounds.h)}`\n\t\t\tif (pageId) {\n\t\t\t\tres += '.' + encodeId(PageRecordType.parseId(pageId))\n\t\t\t}\n\t\t\treturn res\n\t\t}\n\t\tdefault:\n\t\t\texhaustiveSwitchError(deepLink)\n\t}\n}\n\n/**\n * Parses a string created by {@link createDeepLinkString} back into a deep link descriptor.\n *\n * @param deepLinkString - the deep link string\n * @returns a deep link descriptor\n *\n * @public\n */\nexport function parseDeepLinkString(deepLinkString: string): TLDeepLink {\n\tconst type = deepLinkString[0]\n\tswitch (type) {\n\t\tcase 's': {\n\t\t\tconst shapeIds = deepLinkString\n\t\t\t\t.slice(1)\n\t\t\t\t.split('.')\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.map((id) => createShapeId(decodeURIComponent(id)))\n\t\t\treturn { type: 'shapes', shapeIds }\n\t\t}\n\t\tcase 'p': {\n\t\t\tconst pageId = PageRecordType.createId(decodeURIComponent(deepLinkString.slice(1)))\n\t\t\treturn { type: 'page', pageId }\n\t\t}\n\t\tcase 'v': {\n\t\t\tconst [x, y, w, h, pageId] = deepLinkString.slice(1).split('.')\n\t\t\treturn {\n\t\t\t\ttype: 'viewport',\n\t\t\t\tbounds: new Box(Number(x), Number(y), Number(w), Number(h)),\n\t\t\t\tpageId: pageId ? PageRecordType.createId(decodeURIComponent(pageId)) : undefined,\n\t\t\t}\n\t\t}\n\t\tdefault:\n\t\t\tthrow Error('Invalid deep link string')\n\t}\n}\n\nfunction encodeId(str: string): string {\n\t// need to encode dots because they are used as separators\n\treturn encodeURIComponent(str).replace(/\\./g, '%2E')\n}\n\n/** @public */\nexport interface TLDeepLinkOptions {\n\t/**\n\t * The name of the url search param to use for the deep link.\n\t *\n\t * Defaults to `'d'`\n\t */\n\tparam?: string\n\t/**\n\t * The debounce time in ms for updating the url.\n\t */\n\tdebounceMs?: number\n\t/**\n\t * Should return the current url to augment with a deep link query parameter.\n\t * If you supply this function, you must also supply an `onChange` function.\n\t */\n\tgetUrl?(editor: Editor): string | URL\n\t/**\n\t * Should return the current deep link target.\n\t * Defaults to returning the current page and viewport position.\n\t */\n\tgetTarget?(editor: Editor): TLDeepLink\n\t/**\n\t * This is fired when the URL is updated.\n\t *\n\t * If not supplied, the default behavior is to update `window.location`.\n\t *\n\t * @param url - the updated URL\n\t */\n\tonChange?(url: URL, editor: Editor): void\n}\n"],
"mappings": "AAAA,SAAmB,gBAAqC,qBAAqB;AAC7E,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