Repository navigation
ENG-2227 Add link support to tldraw text shapes in Roam #1441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
trangdoan982
merged 12 commits into
main
from
eng-2227-add-link-support-to-tldraw-text-shapes-in-roam
Oct 5, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d9cca44
ENG-2227 Add link support to tldraw text shapes in Roam
trangdoan982 9c42b84
Fix sync room schema and move the text link icon out of the glyphs
trangdoan982 c11d417
Remove unnecessary type assertions in sync worker schema test
trangdoan982 fe3c626
Revert props-schema approach for text shape links
trangdoan982 2a81ac7
Merge remote-tracking branch 'origin/main' into eng-2227-add-link-sup…
trangdoan982 96d5284
ENG-2227 Store text shape links in meta and reuse tldraw's link UI
trangdoan982 73744be
Co-locate the text link guard with its only caller
trangdoan982 b8fdd19
Hide the text link icon while its shape is selected
trangdoan982 d248b9d
Merge remote-tracking branch 'origin/main' into eng-2227-add-link-sup…
trangdoan982 0251806
Hide text link icons that overlap the selection handles
trangdoan982 f7aedc8
Keep the text link icon visible while the shape is selected
trangdoan982 c79dfc1
Use Tailwind for the text link overlay's static styles
trangdoan982 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
apps/roam/src/components/canvas/overlays/TextLinkOverlay.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import React from "react"; | ||
| import { | ||
| HyperlinkButton, | ||
| T, | ||
| TLShape, | ||
| TLShapeId, | ||
| useEditor, | ||
| useValue, | ||
| } from "tldraw"; | ||
|
|
||
| type TextLink = { id: TLShapeId; url: string; left: number; top: number }; | ||
|
|
||
| // Matches the hit area of tldraw's .tl-hyperlink-button. | ||
| const BUTTON_SIZE = 44; | ||
|
|
||
| // meta is unvalidated, so this is the only guard before an href. Returning the | ||
| // parsed form keeps the href identical to what passed validation. | ||
| export const getTextShapeLinkUrl = (shape: TLShape): string | undefined => { | ||
| if (shape.type !== "text") return undefined; | ||
| const { url } = shape.meta; | ||
| if (typeof url !== "string" || !T.linkUrl.isValid(url)) return undefined; | ||
| try { | ||
| // linkUrl resolves relative paths against a dummy origin; only absolute URLs render. | ||
| return new URL(url).href; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| // Text bounds hug the glyphs, so the icon sits just outside the right edge | ||
| // rather than in the shape corner where geo shapes draw it. | ||
| export const TextLinkOverlay = (): JSX.Element => { | ||
| const editor = useEditor(); | ||
| const links = useValue<TextLink[]>( | ||
| "textShapeLinks", | ||
| () => { | ||
| const viewport = editor.getViewportPageBounds(); | ||
| return editor.getCurrentPageShapes().flatMap((shape) => { | ||
| const url = getTextShapeLinkUrl(shape); | ||
| if (!url) return []; | ||
| const bounds = editor.getShapePageBounds(shape.id); | ||
| if (!bounds || !viewport.collides(bounds)) return []; | ||
| const anchor = editor.pageToViewport({ | ||
| x: bounds.maxX, | ||
| y: bounds.midY, | ||
| }); | ||
| return [ | ||
| { | ||
| id: shape.id, | ||
| url, | ||
| left: anchor.x, | ||
| top: anchor.y - BUTTON_SIZE / 2, | ||
| }, | ||
| ]; | ||
| }); | ||
| }, | ||
| [editor], | ||
| ); | ||
| const zoomLevel = useValue("zoomLevel", () => editor.getZoomLevel(), [ | ||
| editor, | ||
| ]); | ||
|
|
||
| return ( | ||
| <div className="pointer-events-none absolute inset-0"> | ||
| {links.map(({ id, url, left, top }) => ( | ||
| <div | ||
| key={id} | ||
| className="absolute" | ||
| style={{ | ||
| left: `${left}px`, | ||
| top: `${top}px`, | ||
| width: `${BUTTON_SIZE}px`, | ||
| height: `${BUTTON_SIZE}px`, | ||
| }} | ||
| > | ||
| <HyperlinkButton url={url} zoomLevel={zoomLevel} /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { createTLSchema, TLShape } from "tldraw"; | ||
| import { getTextShapeLinkUrl } from "~/components/canvas/overlays/TextLinkOverlay"; | ||
|
|
||
| const makeShape = ({ | ||
| type = "text", | ||
| meta = {}, | ||
| }: { | ||
| type?: string; | ||
| meta?: Record<string, unknown>; | ||
| } = {}): TLShape => | ||
| ({ | ||
| id: "shape:t1", | ||
| typeName: "shape", | ||
| type, | ||
| x: 0, | ||
| y: 0, | ||
| rotation: 0, | ||
| index: "a1", | ||
| parentId: "page:page", | ||
| isLocked: false, | ||
| opacity: 1, | ||
| meta, | ||
| props: { | ||
| color: "black", | ||
| size: "m", | ||
| font: "draw", | ||
| textAlign: "middle", | ||
| w: 100, | ||
| text: "hello", | ||
| scale: 1, | ||
| autoSize: true, | ||
| }, | ||
| }) as unknown as TLShape; | ||
|
|
||
| const withUrl = (url: unknown): TLShape => makeShape({ meta: { url } }); | ||
|
|
||
| describe("getTextShapeLinkUrl", () => { | ||
| it("returns the parsed href for a valid link", () => { | ||
| expect(getTextShapeLinkUrl(withUrl("https://Example.com"))).toBe( | ||
| "https://example.com/", | ||
| ); | ||
| expect(getTextShapeLinkUrl(withUrl("mailto:a@b.co"))).toBe("mailto:a@b.co"); | ||
| }); | ||
|
|
||
| it("treats a missing or cleared link as no link", () => { | ||
| expect(getTextShapeLinkUrl(makeShape())).toBeUndefined(); | ||
| expect(getTextShapeLinkUrl(withUrl(""))).toBeUndefined(); | ||
| expect(getTextShapeLinkUrl(withUrl(42))).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("ignores links on shapes other than text", () => { | ||
| const geo = makeShape({ | ||
| type: "geo", | ||
| meta: { url: "https://example.com" }, | ||
| }); | ||
| expect(getTextShapeLinkUrl(geo)).toBeUndefined(); | ||
| }); | ||
|
|
||
| // meta has no schema, so a collaborator or API writer can store anything here. | ||
| it("rejects protocols that could run script", () => { | ||
| expect(getTextShapeLinkUrl(withUrl("javascript:alert(1)"))).toBeUndefined(); | ||
| expect( | ||
| getTextShapeLinkUrl(withUrl(" javascript:alert(1)")), | ||
| ).toBeUndefined(); | ||
| expect(getTextShapeLinkUrl(withUrl("data:text/html,x"))).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("rejects relative URLs that linkUrl resolves against a dummy origin", () => { | ||
| expect(getTextShapeLinkUrl(withUrl("/page"))).toBeUndefined(); | ||
| expect(getTextShapeLinkUrl(withUrl("//evil.example/x"))).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("text shape link persistence", () => { | ||
| // The stock schema is what older extension builds and the sync worker use. | ||
| const stockSchema = createTLSchema(); | ||
|
|
||
| it("validates a text shape carrying meta.url against the stock schema", () => { | ||
| expect(() => | ||
| stockSchema.validateRecord( | ||
| {} as never, | ||
| withUrl("https://example.com") as never, | ||
| "initialize", | ||
| null, | ||
| ), | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it("round-trips meta.url through a snapshot load with no migration", () => { | ||
| const shape = withUrl("https://example.com"); | ||
| const migrated = stockSchema.migrateStoreSnapshot({ | ||
| store: { [shape.id]: shape }, | ||
| schema: stockSchema.serialize(), | ||
| }); | ||
| expect(migrated.type).toBe("success"); | ||
| const loaded = (migrated as unknown as { value: Record<string, TLShape> }) | ||
| .value[shape.id]; | ||
| expect(loaded.meta.url).toBe("https://example.com"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.