@tldraw/mentions 0.0.0-bootstrap
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/README.md +16 -0
- package/dist-cjs/avatar.js +51 -0
- package/dist-cjs/avatar.js.map +7 -0
- package/dist-cjs/comment-author.js +17 -0
- package/dist-cjs/comment-author.js.map +7 -0
- package/dist-cjs/index.d.ts +131 -0
- package/dist-cjs/index.js +41 -0
- package/dist-cjs/index.js.map +7 -0
- package/dist-cjs/mention-extension.js +42 -0
- package/dist-cjs/mention-extension.js.map +7 -0
- package/dist-cjs/mention-list.js +70 -0
- package/dist-cjs/mention-list.js.map +7 -0
- package/dist-cjs/mention-suggestion.js +206 -0
- package/dist-cjs/mention-suggestion.js.map +7 -0
- package/dist-cjs/mention.js +31 -0
- package/dist-cjs/mention.js.map +7 -0
- package/dist-esm/avatar.mjs +31 -0
- package/dist-esm/avatar.mjs.map +7 -0
- package/dist-esm/comment-author.mjs +1 -0
- package/dist-esm/comment-author.mjs.map +7 -0
- package/dist-esm/index.d.mts +131 -0
- package/dist-esm/index.mjs +25 -0
- package/dist-esm/index.mjs.map +7 -0
- package/dist-esm/mention-extension.mjs +22 -0
- package/dist-esm/mention-extension.mjs.map +7 -0
- package/dist-esm/mention-list.mjs +50 -0
- package/dist-esm/mention-list.mjs.map +7 -0
- package/dist-esm/mention-suggestion.mjs +186 -0
- package/dist-esm/mention-suggestion.mjs.map +7 -0
- package/dist-esm/mention.mjs +11 -0
- package/dist-esm/mention.mjs.map +7 -0
- package/mentions.css +108 -0
- package/package.json +68 -0
- package/src/avatar.tsx +35 -0
- package/src/comment-author.ts +15 -0
- package/src/index.ts +25 -0
- package/src/mention-extension.test.ts +41 -0
- package/src/mention-extension.ts +51 -0
- package/src/mention-list.tsx +103 -0
- package/src/mention-suggestion.test.ts +18 -0
- package/src/mention-suggestion.tsx +286 -0
- package/src/mention.tsx +9 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/mention-suggestion.tsx"],
|
|
4
|
+
"sourcesContent": ["import type { MentionNodeAttrs } from '@tiptap/extension-mention'\nimport { ReactRenderer } from '@tiptap/react'\nimport type { SuggestionKeyDownProps, SuggestionOptions } from '@tiptap/suggestion'\nimport { type ReactNode, forwardRef, useImperativeHandle, useState } from 'react'\nimport { type Editor as TldrawEditor, atom, react } from 'tldraw'\nimport { MentionList, MentionMember } from './mention-list'\n\n/** The handle the suggestion plugin drives \u2014 it forwards navigation keys into the popup. */\nexport interface MentionPopupHandle {\n\tonKeyDown(props: SuggestionKeyDownProps): boolean\n}\n\ninterface MentionPopupProps {\n\titems: MentionMember[]\n\tcommand(attrs: MentionNodeAttrs): void\n\trenderMember?(member: MentionMember): ReactNode\n}\n\n/** The live @-picker popup: owns the highlighted index and keyboard, renders the presentational list. */\nconst MentionPopup = forwardRef<MentionPopupHandle, MentionPopupProps>(function MentionPopup(\n\t{ items, command, renderMember },\n\tref\n) {\n\tconst [activeIndex, setActiveIndex] = useState(0)\n\t// A new query yields new items; reset the highlight to the top during render \u2014 not in an effect,\n\t// which would leave a frame where `activeIndex` still points past a shrunk list and Enter selects\n\t// its (now out-of-range, undefined) item, swallowing the key without inserting a mention.\n\tconst [prevItems, setPrevItems] = useState(items)\n\tif (items !== prevItems) {\n\t\tsetPrevItems(items)\n\t\tsetActiveIndex(0)\n\t}\n\n\tconst select = (member: MentionMember | undefined) => {\n\t\tif (member) command({ id: member.id, label: member.name })\n\t}\n\n\tuseImperativeHandle(ref, () => ({\n\t\tonKeyDown: ({ event }) => {\n\t\t\tif (items.length === 0) return false\n\t\t\tif (event.key === 'ArrowUp') {\n\t\t\t\tsetActiveIndex((i) => (i + items.length - 1) % items.length)\n\t\t\t\treturn true\n\t\t\t}\n\t\t\tif (event.key === 'ArrowDown') {\n\t\t\t\tsetActiveIndex((i) => (i + 1) % items.length)\n\t\t\t\treturn true\n\t\t\t}\n\t\t\t// Enter and Tab both complete the highlighted member (falling back to the top match so a\n\t\t\t// stale index never selects `undefined`). The empty-roster case is handled a level up, in\n\t\t\t// the suggestion's onKeyDown, which cancels the picker.\n\t\t\tif (event.key === 'Enter' || event.key === 'Tab') {\n\t\t\t\tselect(items[activeIndex] ?? items[0])\n\t\t\t\treturn true\n\t\t\t}\n\t\t\treturn false\n\t\t},\n\t}))\n\n\treturn (\n\t\t<MentionList\n\t\t\tmembers={items}\n\t\t\tactiveIndex={activeIndex}\n\t\t\tonSelect={select}\n\t\t\trenderMember={renderMember}\n\t\t/>\n\t)\n})\n\nconst MAX_SUGGESTIONS = 8\n\n/** Members whose name contains the query (case-insensitive), capped to the popup's length.\n * @public */\nexport function filterMentionMembers(members: MentionMember[], query: string): MentionMember[] {\n\tconst q = query.toLowerCase()\n\treturn members.filter((m) => m.name.toLowerCase().includes(q)).slice(0, MAX_SUGGESTIONS)\n}\n\n// A reactive flag for whether the @-picker is showing. Deliberately NOT tldraw's open-menu registry:\n// registering there mounts MenuClickCapture, which covers the canvas with a click-capture overlay to\n// make it inert while a menu is open. But the picker is an inline autocomplete, not a modal \u2014 the\n// canvas must stay pannable/zoomable beneath it \u2014 so we track \"open\" ourselves instead.\nconst mentionPickerOpen = atom('isMentionPickerOpen', false)\n\n/**\n * Whether the \\@-mention picker is currently showing. Host dismissal (Escape, outside-click) checks\n * this so it can defer to the picker instead of tearing down the composer or thread beneath it.\n * @public\n */\nexport function isMentionPickerOpen(): boolean {\n\treturn mentionPickerOpen.get()\n}\n\n/** @public */\nexport interface MentionSuggestionOptions {\n\t/** Override a member row's content in the picker. Defaults to avatar + name (+ secondary). */\n\trenderMember?(member: MentionMember): ReactNode\n\t/**\n\t * The tldraw editor the composer lives in. When provided, the popup re-anchors reactively as the\n\t * canvas camera moves (the composer rides it) instead of polling every frame. Omit off-canvas.\n\t */\n\teditor?: TldrawEditor | null\n}\n\n/**\n * Build the TipTap `suggestion` config for the \\@-picker. `getSuggestions(query)` is the host's\n * resolver \u2014 it returns the members matching the query (sync or async); the SDK owns neither the\n * roster nor the filtering. The plugin runs outside React, so `render` mounts `MentionPopup` via a\n * `ReactRenderer`, forwards navigation keys through the popup's imperative handle, and lets it call\n * `command` to insert.\n * @public\n */\nexport function createMentionSuggestion(\n\tgetSuggestions: (query: string) => MentionMember[] | Promise<MentionMember[]>,\n\toptions: MentionSuggestionOptions = {}\n): Omit<SuggestionOptions<MentionMember, MentionNodeAttrs>, 'editor'> {\n\treturn {\n\t\tchar: '@',\n\t\titems: ({ query }) => getSuggestions(query),\n\t\trender: () => {\n\t\t\tlet renderer: ReactRenderer<MentionPopupHandle, MentionPopupProps> | null = null\n\t\t\tlet container: HTMLElement | null = null\n\t\t\tlet editorEl: HTMLElement | null = null\n\t\t\tlet canvasEl: Element | null = null\n\t\t\tlet stopCameraReaction: (() => void) | null = null\n\t\t\t// The composer field's top-left in page space, plus the popup's screen width. Captured on a\n\t\t\t// fresh read so camera moves can re-derive the popup's screen position from the page anchor\n\t\t\t// (see reposition) rather than the field's DOM rect.\n\t\t\tlet anchorPage: { x: number; y: number } | null = null\n\t\t\tlet popupWidth = 0\n\n\t\t\tconst applyScreen = (left: number, top: number, width: number) => {\n\t\t\t\tif (!container) return\n\t\t\t\tcontainer.style.left = `${left}px`\n\t\t\t\tcontainer.style.top = `${top + 4}px`\n\t\t\t\tcontainer.style.width = `${width}px`\n\t\t\t}\n\n\t\t\t// Fresh placement: read the field's real screen rect, position the popup flush under it (not\n\t\t\t// the caret, matching its width), and remember the field's page-space anchor for reposition.\n\t\t\tconst place = () => {\n\t\t\t\tif (!container || !editorEl || container.style.display === 'none') return\n\t\t\t\tconst field = editorEl.closest('.tlui-cmt-composer__field') ?? editorEl\n\t\t\t\tconst rect = field.getBoundingClientRect()\n\t\t\t\tpopupWidth = rect.width\n\t\t\t\tanchorPage = options.editor?.screenToPage({ x: rect.left, y: rect.bottom }) ?? null\n\t\t\t\tapplyScreen(rect.left, rect.bottom, rect.width)\n\t\t\t}\n\n\t\t\t// Re-derive the popup's screen position from the remembered page anchor \u2014 pure camera math,\n\t\t\t// always current. Reading the field's DOM rect here instead would lag by a frame: the canvas\n\t\t\t// composer re-positions itself on a React commit (a `useValue(pageToViewport(...))`), which\n\t\t\t// lands after a camera reaction runs, so the rect is still last frame's during a pan.\n\t\t\tconst reposition = () => {\n\t\t\t\tif (!anchorPage || !options.editor) return\n\t\t\t\tconst s = options.editor.pageToScreen(anchorPage)\n\t\t\t\tapplyScreen(s.x, s.y, popupWidth)\n\t\t\t}\n\n\t\t\t// The popup is `position: fixed`, but its anchor \u2014 a canvas composer \u2014 rides the camera:\n\t\t\t// panning or zooming moves the composer with no scroll/resize event to hook. Re-anchor when\n\t\t\t// the camera actually changes (via a tldraw reaction) rather than polling every frame, plus\n\t\t\t// on window scroll/resize for the off-canvas case (which re-read the field directly).\n\t\t\tconst startFollowing = () => {\n\t\t\t\twindow.addEventListener('scroll', place, true)\n\t\t\t\twindow.addEventListener('resize', place)\n\t\t\t\tif (options.editor) {\n\t\t\t\t\tstopCameraReaction = react('anchor mention popup to camera', () => {\n\t\t\t\t\t\toptions.editor!.getCamera() // track the camera so this re-runs as it moves\n\t\t\t\t\t\treposition()\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst stopFollowing = () => {\n\t\t\t\twindow.removeEventListener('scroll', place, true)\n\t\t\t\twindow.removeEventListener('resize', place)\n\t\t\t\tstopCameraReaction?.()\n\t\t\t\tstopCameraReaction = null\n\t\t\t\tanchorPage = null\n\t\t\t}\n\n\t\t\t// Dismiss the roster \u2014 on Escape, or when the composer loses focus \u2014 by hiding it and\n\t\t\t// clearing the open flag, so isMentionPickerOpen() stays accurate and the thread's own\n\t\t\t// Escape/outside-click dismissal can take over. The TipTap suggestion stays active, so typing\n\t\t\t// re-shows the roster via onUpdate.\n\t\t\tconst hide = () => {\n\t\t\t\tif (container) container.style.display = 'none'\n\t\t\t\tmentionPickerOpen.set(false)\n\t\t\t}\n\n\t\t\t// Wheel/panning over the popup drives the canvas beneath it, so scrolling to pan or zoom\n\t\t\t// isn't swallowed by the roster \u2014 the same passthrough the rest of the comments UI gets from\n\t\t\t// `usePassThroughWheelEvents`. The list still scrolls itself when the roster overflows (we\n\t\t\t// only redispatch when it can't). Done imperatively because the popup lives outside React.\n\t\t\tconst onWheel = (e: WheelEvent) => {\n\t\t\t\tif ((e as any).isSpecialRedispatchedEvent || !canvasEl) return\n\t\t\t\tconst list = container?.querySelector('.tlui-cmt-mention-list')\n\t\t\t\tif (list && list.scrollHeight > list.clientHeight) return\n\t\t\t\te.preventDefault()\n\t\t\t\tconst redispatched = new WheelEvent('wheel', e)\n\t\t\t\t;(redispatched as any).isSpecialRedispatchedEvent = true\n\t\t\t\tcanvasEl.dispatchEvent(redispatched)\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tonStart: (props) => {\n\t\t\t\t\trenderer = new ReactRenderer(MentionPopup, {\n\t\t\t\t\t\tprops: {\n\t\t\t\t\t\t\titems: props.items,\n\t\t\t\t\t\t\tcommand: props.command,\n\t\t\t\t\t\t\trenderMember: options.renderMember,\n\t\t\t\t\t\t},\n\t\t\t\t\t\teditor: props.editor,\n\t\t\t\t\t})\n\t\t\t\t\teditorEl = props.editor.view.dom as HTMLElement\n\t\t\t\t\t// The TipTap suggestion has no blur handling, so without this the picker would stay\n\t\t\t\t\t// \"open\" (and the thread would defer its Escape to it) after focus moved away from the\n\t\t\t\t\t// composer \u2014 leaving Escape a no-op and the roster stuck on screen.\n\t\t\t\t\teditorEl.addEventListener('blur', hide)\n\t\t\t\t\tcontainer = document.createElement('div')\n\t\t\t\t\tcontainer.className = 'tlui-cmt-mention-popup'\n\t\t\t\t\tcontainer.appendChild(renderer.element)\n\t\t\t\t\t// Mount inside the tldraw container so the popup inherits the theme variables\n\t\t\t\t\t// (--tl-color-*); portaling to document.body would strip them and lose the panel.\n\t\t\t\t\tconst themed = editorEl.closest('.tl-container')\n\t\t\t\t\t;(themed ?? document.body).appendChild(container)\n\t\t\t\t\tcanvasEl = themed?.querySelector('.tl-canvas') ?? null\n\t\t\t\t\tcontainer.addEventListener('wheel', onWheel, { passive: false })\n\t\t\t\t\tplace()\n\t\t\t\t\tstartFollowing()\n\t\t\t\t\tmentionPickerOpen.set(true)\n\t\t\t\t},\n\t\t\t\tonUpdate: (props) => {\n\t\t\t\t\tif (renderer)\n\t\t\t\t\t\trenderer.updateProps({\n\t\t\t\t\t\t\titems: props.items,\n\t\t\t\t\t\t\tcommand: props.command,\n\t\t\t\t\t\t\trenderMember: options.renderMember,\n\t\t\t\t\t\t})\n\t\t\t\t\t// Typing after an Escape re-shows the roster.\n\t\t\t\t\tif (container) container.style.display = ''\n\t\t\t\t\tmentionPickerOpen.set(true)\n\t\t\t\t\tplace()\n\t\t\t\t},\n\t\t\t\tonKeyDown: (props) => {\n\t\t\t\t\t// Once the roster is hidden (a prior Escape), the TipTap suggestion is still active but\n\t\t\t\t\t// this handler goes inert \u2014 every key passes through to the composer/thread (so a second\n\t\t\t\t\t// Escape closes them, and Arrow/Enter don't select from an invisible list). Typing\n\t\t\t\t\t// re-shows the roster via onUpdate.\n\t\t\t\t\tif (!isMentionPickerOpen()) return false\n\t\t\t\t\tif (props.event.key === 'Escape') {\n\t\t\t\t\t\t// Dismiss only the roster: hide it and stop the key so the composer/thread beneath\n\t\t\t\t\t\t// doesn't also treat Escape as \"abandon\".\n\t\t\t\t\t\thide()\n\t\t\t\t\t\tprops.event.stopPropagation()\n\t\t\t\t\t\treturn true\n\t\t\t\t\t}\n\t\t\t\t\tif (props.event.key === 'Enter' || props.event.key === 'Tab') {\n\t\t\t\t\t\t// Complete the highlighted member if there is one to complete; if the roster is empty\n\t\t\t\t\t\t// there's nothing to pick, so cancel the picker and swallow the key \u2014 the composer\n\t\t\t\t\t\t// beneath neither submits (Enter) nor moves focus / indents (Tab).\n\t\t\t\t\t\tconst completed = renderer?.ref?.onKeyDown(props) ?? false\n\t\t\t\t\t\tif (!completed) {\n\t\t\t\t\t\t\thide()\n\t\t\t\t\t\t\tprops.event.stopPropagation()\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn true\n\t\t\t\t\t}\n\t\t\t\t\tif (renderer && renderer.ref) return renderer.ref.onKeyDown(props)\n\t\t\t\t\treturn false\n\t\t\t\t},\n\t\t\t\tonExit: () => {\n\t\t\t\t\tstopFollowing()\n\t\t\t\t\tif (editorEl) editorEl.removeEventListener('blur', hide)\n\t\t\t\t\tif (container) container.removeEventListener('wheel', onWheel)\n\t\t\t\t\tif (container) container.remove()\n\t\t\t\t\tif (renderer) renderer.destroy()\n\t\t\t\t\trenderer = null\n\t\t\t\t\tcontainer = null\n\t\t\t\t\tcanvasEl = null\n\t\t\t\t\tmentionPickerOpen.set(false)\n\t\t\t\t},\n\t\t\t}\n\t\t},\n\t}\n}\n"],
|
|
5
|
+
"mappings": "AA4DE;AA3DF,SAAS,qBAAqB;AAE9B,SAAyB,YAAY,qBAAqB,gBAAgB;AAC1E,SAAsC,MAAM,aAAa;AACzD,SAAS,mBAAkC;AAc3C,MAAM,eAAe,WAAkD,SAASA,cAC/E,EAAE,OAAO,SAAS,aAAa,GAC/B,KACC;AACD,QAAM,CAAC,aAAa,cAAc,IAAI,SAAS,CAAC;AAIhD,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAChD,MAAI,UAAU,WAAW;AACxB,iBAAa,KAAK;AAClB,mBAAe,CAAC;AAAA,EACjB;AAEA,QAAM,SAAS,CAAC,WAAsC;AACrD,QAAI,OAAQ,SAAQ,EAAE,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,CAAC;AAAA,EAC1D;AAEA,sBAAoB,KAAK,OAAO;AAAA,IAC/B,WAAW,CAAC,EAAE,MAAM,MAAM;AACzB,UAAI,MAAM,WAAW,EAAG,QAAO;AAC/B,UAAI,MAAM,QAAQ,WAAW;AAC5B,uBAAe,CAAC,OAAO,IAAI,MAAM,SAAS,KAAK,MAAM,MAAM;AAC3D,eAAO;AAAA,MACR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AK,QAAO,SAAS,IAAI,UAAU,KAAK;AACjE,iBAAO;AAAA,QACR;AAAA,QACA,QAAQ,MAAM;AACb,wBAAc;AACd,cAAI,SAAU,UAAS,oBAAoB,QAAQ,IAAI;AACvD,cAAI,UAAW,WAAU,oBAAoB,SAAS,OAAO;AAC7D,cAAI,UAAW,WAAU,OAAO;AAChC,cAAI,SAAU,UAAS,QAAQ;AAC/B,qBAAW;AACX,sBAAY;AACZ,qBAAW;AACX,4BAAkB,IAAI,KAAK;AAAA,QAC5B;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;",
|
|
6
|
+
"names": ["MentionPopup"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/mention.tsx"],
|
|
4
|
+
"sourcesContent": ["/** @public */\nexport interface MentionProps {\n\tname: string\n}\n\n/** An \\@-mention pill for referencing a person. @public @react */\nexport function Mention({ name }: MentionProps) {\n\treturn <span className=\"tlui-cmt-mention\">@{name}</span>\n}\n"],
|
|
5
|
+
"mappings": "AAOQ;AADD,SAAS,QAAQ,EAAE,KAAK,GAAiB;AAC/C,SAAO,qBAAC,UAAK,WAAU,oBAAmB;AAAA;AAAA,IAAE;AAAA,KAAK;AAClD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/mentions.css
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* avatar — a person's image, or a single-initial coloured circle. Shared by the mention picker and,
|
|
2
|
+
via @tldraw/commenting, the comment UI. */
|
|
3
|
+
.tlui-cmt-avatar {
|
|
4
|
+
width: 28px;
|
|
5
|
+
height: 28px;
|
|
6
|
+
border-radius: 50%;
|
|
7
|
+
background: var(--tl-color-selected);
|
|
8
|
+
color: #fff;
|
|
9
|
+
display: flex;
|
|
10
|
+
align-items: center;
|
|
11
|
+
justify-content: center;
|
|
12
|
+
font-size: 11px;
|
|
13
|
+
font-weight: 600;
|
|
14
|
+
flex-shrink: 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.tlui-cmt-avatar--image {
|
|
18
|
+
object-fit: cover;
|
|
19
|
+
background: none;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* mention pill */
|
|
23
|
+
.tlui-cmt-mention {
|
|
24
|
+
color: var(--tl-color-selected);
|
|
25
|
+
font-weight: 600;
|
|
26
|
+
background: color-mix(in srgb, var(--tl-color-selected) 12%, transparent);
|
|
27
|
+
padding: 0 4px;
|
|
28
|
+
border-radius: 4px;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* mention picker */
|
|
32
|
+
.tlui-cmt-mention-list {
|
|
33
|
+
display: flex;
|
|
34
|
+
flex-direction: column;
|
|
35
|
+
gap: 1px;
|
|
36
|
+
padding: 4px;
|
|
37
|
+
width: 100%;
|
|
38
|
+
box-sizing: border-box;
|
|
39
|
+
max-height: 260px;
|
|
40
|
+
overflow-y: auto;
|
|
41
|
+
color: var(--tl-color-text-1, #1d1d1d);
|
|
42
|
+
background: var(--tl-color-panel, #fff);
|
|
43
|
+
border: 1px solid var(--tl-color-divider, rgba(0, 0, 0, 0.08));
|
|
44
|
+
border-radius: 10px;
|
|
45
|
+
box-shadow: var(--tl-shadow-2, 0 2px 12px rgba(0, 0, 0, 0.14));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.tlui-cmt-mention-list__item {
|
|
49
|
+
display: flex;
|
|
50
|
+
align-items: center;
|
|
51
|
+
gap: 8px;
|
|
52
|
+
padding: 6px 8px;
|
|
53
|
+
border: none;
|
|
54
|
+
background: transparent;
|
|
55
|
+
border-radius: 6px;
|
|
56
|
+
cursor: pointer;
|
|
57
|
+
text-align: left;
|
|
58
|
+
color: var(--tl-color-text-1);
|
|
59
|
+
font: inherit;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.tlui-cmt-mention-list__item--active,
|
|
63
|
+
.tlui-cmt-mention-list__item:hover {
|
|
64
|
+
background: var(--tl-color-muted-2, rgba(0, 0, 0, 0.06));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.tlui-cmt-mention-list__text {
|
|
68
|
+
display: flex;
|
|
69
|
+
flex-direction: column;
|
|
70
|
+
min-width: 0;
|
|
71
|
+
line-height: 1.3;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.tlui-cmt-mention-list__name {
|
|
75
|
+
font-size: 13px;
|
|
76
|
+
font-weight: 500;
|
|
77
|
+
white-space: nowrap;
|
|
78
|
+
overflow: hidden;
|
|
79
|
+
text-overflow: ellipsis;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.tlui-cmt-mention-list__you {
|
|
83
|
+
margin-left: 4px;
|
|
84
|
+
font-weight: 400;
|
|
85
|
+
color: var(--tl-color-text-3);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.tlui-cmt-mention-list__secondary {
|
|
89
|
+
font-size: 12px;
|
|
90
|
+
color: var(--tl-color-text-3);
|
|
91
|
+
white-space: nowrap;
|
|
92
|
+
overflow: hidden;
|
|
93
|
+
text-overflow: ellipsis;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.tlui-cmt-mention-list--empty {
|
|
97
|
+
padding: 8px 10px;
|
|
98
|
+
width: 100%;
|
|
99
|
+
box-sizing: border-box;
|
|
100
|
+
color: var(--tl-color-text-3);
|
|
101
|
+
font-size: 13px;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* the @-picker popup — anchored under the editor field, positioned by the suggestion plugin */
|
|
105
|
+
.tlui-cmt-mention-popup {
|
|
106
|
+
position: fixed;
|
|
107
|
+
z-index: var(--tl-layer-menus);
|
|
108
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tldraw/mentions",
|
|
3
|
+
"description": "tldraw @-mention picker and TipTap node, shared by commenting and shape rich text.",
|
|
4
|
+
"version": "0.0.0-bootstrap",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "tldraw Inc.",
|
|
7
|
+
"email": "hello@tldraw.com"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://tldraw.dev",
|
|
10
|
+
"license": "SEE LICENSE IN LICENSE.md",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/tldraw/tldraw"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/tldraw/tldraw/issues"
|
|
17
|
+
},
|
|
18
|
+
"main": "dist-cjs/index.js",
|
|
19
|
+
"style": "./mentions.css",
|
|
20
|
+
"files": [
|
|
21
|
+
"mentions.css",
|
|
22
|
+
"dist-esm",
|
|
23
|
+
"dist-cjs",
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test-ci": "yarn run -T vitest run --passWithNoTests",
|
|
28
|
+
"test": "yarn run -T vitest --passWithNoTests",
|
|
29
|
+
"test-coverage": "yarn run -T vitest run --coverage --passWithNoTests",
|
|
30
|
+
"build": "yarn run -T tsx ../../internal/scripts/build-package.ts",
|
|
31
|
+
"build-api": "yarn run -T tsx ../../internal/scripts/build-api.ts",
|
|
32
|
+
"prepack": "yarn run -T tsx ../../internal/scripts/prepack.ts",
|
|
33
|
+
"postpack": "../../internal/scripts/postpack.sh",
|
|
34
|
+
"pack-tarball": "yarn pack",
|
|
35
|
+
"lint": "yarn run -T tsx ../../internal/scripts/lint.ts"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/react": "^19.2.7",
|
|
39
|
+
"@types/react-dom": "^19.2.3",
|
|
40
|
+
"react": "^19.2.1",
|
|
41
|
+
"react-dom": "^19.2.1"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": "^18.2.0 || ^19.2.1",
|
|
45
|
+
"react-dom": "^18.2.0 || ^19.2.1"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.12.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@tiptap/core": "^3.12.1",
|
|
52
|
+
"@tiptap/extension-mention": "3.13.0",
|
|
53
|
+
"@tiptap/pm": "^3.12.1",
|
|
54
|
+
"@tiptap/react": "^3.12.1",
|
|
55
|
+
"@tiptap/suggestion": "3.13.0",
|
|
56
|
+
"@tldraw/utils": "5.2.5",
|
|
57
|
+
"tldraw": "5.2.5"
|
|
58
|
+
},
|
|
59
|
+
"module": "dist-esm/index.mjs",
|
|
60
|
+
"source": "src/index.ts",
|
|
61
|
+
"exports": {
|
|
62
|
+
".": {
|
|
63
|
+
"import": "./dist-esm/index.mjs",
|
|
64
|
+
"require": "./dist-cjs/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./mentions.css": "./mentions.css"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/avatar.tsx
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { getFirstCharacter } from 'tldraw'
|
|
2
|
+
import { CommentAuthor } from './comment-author'
|
|
3
|
+
|
|
4
|
+
/** @public */
|
|
5
|
+
export interface AvatarProps {
|
|
6
|
+
author: CommentAuthor
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function initial(name: string) {
|
|
10
|
+
return (getFirstCharacter(name.trim()) || '?').toUpperCase()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A person's avatar — their image if provided, otherwise a single-initial coloured circle.
|
|
14
|
+
* @public @react */
|
|
15
|
+
export function Avatar({ author }: AvatarProps) {
|
|
16
|
+
if (author.image) {
|
|
17
|
+
return (
|
|
18
|
+
<img
|
|
19
|
+
className="tlui-cmt-avatar tlui-cmt-avatar--image"
|
|
20
|
+
src={author.image}
|
|
21
|
+
alt=""
|
|
22
|
+
aria-hidden="true"
|
|
23
|
+
/>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
return (
|
|
27
|
+
<div
|
|
28
|
+
className="tlui-cmt-avatar"
|
|
29
|
+
aria-hidden="true"
|
|
30
|
+
style={author.color ? { backgroundColor: author.color } : undefined}
|
|
31
|
+
>
|
|
32
|
+
{initial(author.name)}
|
|
33
|
+
</div>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { JsonObject } from 'tldraw'
|
|
2
|
+
|
|
3
|
+
/** A comment author's display identity, resolved from an author id.
|
|
4
|
+
* @public */
|
|
5
|
+
export interface CommentAuthor {
|
|
6
|
+
name: string
|
|
7
|
+
/** The author's color (any CSS color) — colors their avatar and pins.
|
|
8
|
+
* Omit for the default tint. */
|
|
9
|
+
color?: string
|
|
10
|
+
/** Avatar image URL. When set, avatars show the image instead of the colored initial;
|
|
11
|
+
* the canvas pin keeps the initial-on-color. */
|
|
12
|
+
image?: string
|
|
13
|
+
/** User-defined metadata for the author, never read by the toolkit (like a shape's `meta`). */
|
|
14
|
+
meta?: JsonObject
|
|
15
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { registerTldrawLibraryVersion } from '@tldraw/utils'
|
|
2
|
+
|
|
3
|
+
// Presentational mention components. These are tldraw-independent and can be used to build custom
|
|
4
|
+
// mention UI.
|
|
5
|
+
export { Avatar, type AvatarProps } from './avatar'
|
|
6
|
+
export { type CommentAuthor } from './comment-author'
|
|
7
|
+
export { Mention, type MentionProps } from './mention'
|
|
8
|
+
export { MentionList, type MentionListProps, type MentionMember } from './mention-list'
|
|
9
|
+
|
|
10
|
+
// The TipTap mention node + `@`-picker suggestion. Add the node to a rich-text editor's extensions
|
|
11
|
+
// (comments composer, shape rich text) to render mention pills and — when a suggestion is provided —
|
|
12
|
+
// drive the picker.
|
|
13
|
+
export { createMentionExtension, type MentionExtensionOptions } from './mention-extension'
|
|
14
|
+
export {
|
|
15
|
+
createMentionSuggestion,
|
|
16
|
+
filterMentionMembers,
|
|
17
|
+
isMentionPickerOpen,
|
|
18
|
+
type MentionSuggestionOptions,
|
|
19
|
+
} from './mention-suggestion'
|
|
20
|
+
|
|
21
|
+
registerTldrawLibraryVersion(
|
|
22
|
+
(globalThis as any).TLDRAW_LIBRARY_NAME,
|
|
23
|
+
(globalThis as any).TLDRAW_LIBRARY_VERSION,
|
|
24
|
+
(globalThis as any).TLDRAW_LIBRARY_MODULES
|
|
25
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { generateHTML } from '@tiptap/core'
|
|
2
|
+
import { getTipTapDefaultExtensions } from 'tldraw'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import { createMentionExtension } from './mention-extension'
|
|
5
|
+
|
|
6
|
+
// The exact render path shape text uses: tldraw's default rich-text extensions plus the mention
|
|
7
|
+
// node, run through TipTap's `generateHTML` (what `renderHtmlFromRichText` does under the hood).
|
|
8
|
+
const render = (body: any, resolveName?: (id: string) => string | undefined) =>
|
|
9
|
+
generateHTML(body, [...getTipTapDefaultExtensions(), createMentionExtension({ resolveName })])
|
|
10
|
+
|
|
11
|
+
const doc = (...content: any[]) => ({ type: 'doc', content }) as any
|
|
12
|
+
const para = (...content: any[]) => ({ type: 'paragraph', content })
|
|
13
|
+
const text = (value: string) => ({ type: 'text', text: value })
|
|
14
|
+
const mention = (id: string, label?: string) => ({ type: 'mention', attrs: { id, label } })
|
|
15
|
+
|
|
16
|
+
describe('createMentionExtension', () => {
|
|
17
|
+
it('renders a mention node as a pill carrying its member id', () => {
|
|
18
|
+
const html = render(doc(para(text('hi '), mention('ada', 'Ada'))))
|
|
19
|
+
expect(html).toContain('tlui-cmt-mention')
|
|
20
|
+
expect(html).toContain('data-mention-id="ada"')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('resolves the id to the current name, not the label frozen at insert time', () => {
|
|
24
|
+
const html = render(doc(para(mention('ada', 'Ada'))), (id) =>
|
|
25
|
+
id === 'ada' ? 'Ada Lovelace' : undefined
|
|
26
|
+
)
|
|
27
|
+
expect(html).toContain('@Ada Lovelace')
|
|
28
|
+
expect(html).not.toContain('@Ada<')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('falls back to the stored label when the id can not be resolved', () => {
|
|
32
|
+
const html = render(doc(para(mention('ghost', 'Ada'))), () => undefined)
|
|
33
|
+
expect(html).toContain('@Ada')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('escapes a resolved name so it can never inject markup', () => {
|
|
37
|
+
const html = render(doc(para(mention('ada'))), () => '<img src=x onerror=alert(1)>')
|
|
38
|
+
expect(html).not.toContain('<img')
|
|
39
|
+
expect(html).toContain('<img')
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { mergeAttributes } from '@tiptap/core'
|
|
2
|
+
import { Mention, type MentionNodeAttrs, type MentionOptions } from '@tiptap/extension-mention'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A mention node's display name: the member's current name, resolved live from its id when a
|
|
6
|
+
* resolver is given (so a rename shows through), falling back to the label captured at insert time
|
|
7
|
+
* when no source can name the id — e.g. a deleted account. The stored id is always the source of
|
|
8
|
+
* truth; live resolution only freshens the displayed name over that captured label.
|
|
9
|
+
*/
|
|
10
|
+
function mentionName(
|
|
11
|
+
attrs: MentionNodeAttrs,
|
|
12
|
+
resolveName?: (id: string) => string | undefined
|
|
13
|
+
): string {
|
|
14
|
+
const resolved = attrs.id && resolveName ? resolveName(attrs.id) : undefined
|
|
15
|
+
return resolved ?? attrs.label ?? attrs.id ?? ''
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @public */
|
|
19
|
+
export interface MentionExtensionOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Resolve a member id to its current display name — for the read-only render paths, and for the
|
|
22
|
+
* editor while a mention is displayed. Returns `undefined` when the id can't be resolved, so the
|
|
23
|
+
* render falls back to the mention's stored label.
|
|
24
|
+
*/
|
|
25
|
+
resolveName?(id: string): string | undefined
|
|
26
|
+
/** The `@`-picker suggestion config — for editing (omit on read-only render paths). */
|
|
27
|
+
suggestion?: MentionOptions['suggestion']
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The \@-mention node — TipTap's `Mention` configured to render as a `.tlui-cmt-mention` pill.
|
|
32
|
+
*
|
|
33
|
+
* A factory rather than a shared constant because it's configured differently per context: the
|
|
34
|
+
* read-only render paths pass `resolveName` (so the stored `{ id }` node always shows the member's
|
|
35
|
+
* current name, not a copy frozen at insert time), while an editor passes a `suggestion` (the `@`
|
|
36
|
+
* picker). Shape rich text passes both — the same extension both edits and renders. The node schema
|
|
37
|
+
* is identical either way — only these two levers differ.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export function createMentionExtension({ resolveName, suggestion }: MentionExtensionOptions = {}) {
|
|
41
|
+
return Mention.configure({
|
|
42
|
+
HTMLAttributes: { class: 'tlui-cmt-mention' },
|
|
43
|
+
renderText: ({ node }) => `@${mentionName(node.attrs as MentionNodeAttrs, resolveName)}`,
|
|
44
|
+
renderHTML: ({ node, options }) => [
|
|
45
|
+
'span',
|
|
46
|
+
mergeAttributes(options.HTMLAttributes, { 'data-mention-id': node.attrs.id }),
|
|
47
|
+
`@${mentionName(node.attrs as MentionNodeAttrs, resolveName)}`,
|
|
48
|
+
],
|
|
49
|
+
...(suggestion ? { suggestion } : {}),
|
|
50
|
+
})
|
|
51
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ReactNode } from 'react'
|
|
2
|
+
import { useTranslation } from 'tldraw'
|
|
3
|
+
import { Avatar } from './avatar'
|
|
4
|
+
import { CommentAuthor } from './comment-author'
|
|
5
|
+
|
|
6
|
+
/** A person the composer's \@-mention picker can offer: a {@link CommentAuthor} plus its id and
|
|
7
|
+
* picker-only display fields. @public */
|
|
8
|
+
export interface MentionMember extends CommentAuthor {
|
|
9
|
+
id: string
|
|
10
|
+
/** A secondary line under the name — e.g. an email or handle. Omit for a single-line row. */
|
|
11
|
+
secondary?: string
|
|
12
|
+
/** Marks the current user; shown as a "(You)" suffix after the name. */
|
|
13
|
+
you?: boolean
|
|
14
|
+
/** Hosts may carry extra fields; a custom `renderMember` can read them. */
|
|
15
|
+
[key: string]: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @public */
|
|
19
|
+
export interface MentionListProps {
|
|
20
|
+
/** Members to choose from — already resolved for the current query by the host. */
|
|
21
|
+
members: MentionMember[]
|
|
22
|
+
/** Index of the highlighted row, driven by the composer's keyboard navigation. */
|
|
23
|
+
activeIndex?: number
|
|
24
|
+
/** Called when a member is chosen (click, or Enter on the active row). */
|
|
25
|
+
onSelect?(member: MentionMember): void
|
|
26
|
+
/** Shown when no member matches the query. Defaults to a translated "No matches". */
|
|
27
|
+
emptyLabel?: string
|
|
28
|
+
/** Override a row's content (inside the selectable button). Defaults to avatar + name (+ secondary). */
|
|
29
|
+
renderMember?(member: MentionMember): ReactNode
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The default row content: avatar, name (with a "(You)" marker), and an optional secondary line. */
|
|
33
|
+
function DefaultMemberRow({ member }: { member: MentionMember }) {
|
|
34
|
+
const msg = useTranslation()
|
|
35
|
+
return (
|
|
36
|
+
<>
|
|
37
|
+
<Avatar author={member} />
|
|
38
|
+
<span className="tlui-cmt-mention-list__text">
|
|
39
|
+
<span className="tlui-cmt-mention-list__name">
|
|
40
|
+
{member.name}
|
|
41
|
+
{member.you && (
|
|
42
|
+
<span className="tlui-cmt-mention-list__you">{`(${msg('comments.mention-you')})`}</span>
|
|
43
|
+
)}
|
|
44
|
+
</span>
|
|
45
|
+
{member.secondary && (
|
|
46
|
+
<span className="tlui-cmt-mention-list__secondary">{member.secondary}</span>
|
|
47
|
+
)}
|
|
48
|
+
</span>
|
|
49
|
+
</>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The \@-mention member picker: a popover list of people to mention, shown as the user types after
|
|
55
|
+
* `@`. Presentational — the composer's suggestion plugin owns resolution, keyboard navigation, and
|
|
56
|
+
* placement, and drives this with the host-resolved `members` and the highlighted `activeIndex`.
|
|
57
|
+
* @public @react
|
|
58
|
+
*/
|
|
59
|
+
export function MentionList({
|
|
60
|
+
members,
|
|
61
|
+
activeIndex = 0,
|
|
62
|
+
onSelect,
|
|
63
|
+
emptyLabel,
|
|
64
|
+
renderMember,
|
|
65
|
+
}: MentionListProps) {
|
|
66
|
+
const msg = useTranslation()
|
|
67
|
+
if (members.length === 0) {
|
|
68
|
+
return (
|
|
69
|
+
<div className="tlui-cmt-mention-list tlui-cmt-mention-list--empty">
|
|
70
|
+
{emptyLabel ?? msg('comments.mention-no-matches')}
|
|
71
|
+
</div>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
return (
|
|
75
|
+
<div className="tlui-cmt-mention-list" role="listbox">
|
|
76
|
+
{members.map((member, i) => {
|
|
77
|
+
const active = i === activeIndex
|
|
78
|
+
return (
|
|
79
|
+
<button
|
|
80
|
+
key={member.id}
|
|
81
|
+
type="button"
|
|
82
|
+
role="option"
|
|
83
|
+
aria-selected={active}
|
|
84
|
+
className={
|
|
85
|
+
active
|
|
86
|
+
? 'tlui-cmt-mention-list__item tlui-cmt-mention-list__item--active'
|
|
87
|
+
: 'tlui-cmt-mention-list__item'
|
|
88
|
+
}
|
|
89
|
+
// Keep the composer focused on a row press: it's portaled outside the editor, so a
|
|
90
|
+
// bare mousedown would blur the composer — which now dismisses the picker — before
|
|
91
|
+
// the click could select. Selection runs on click; focus stays put.
|
|
92
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
93
|
+
onClick={() => {
|
|
94
|
+
if (onSelect) onSelect(member)
|
|
95
|
+
}}
|
|
96
|
+
>
|
|
97
|
+
{renderMember ? renderMember(member) : <DefaultMemberRow member={member} />}
|
|
98
|
+
</button>
|
|
99
|
+
)
|
|
100
|
+
})}
|
|
101
|
+
</div>
|
|
102
|
+
)
|
|
103
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { filterMentionMembers } from './mention-suggestion'
|
|
3
|
+
|
|
4
|
+
const members = [
|
|
5
|
+
{ id: 'u1', name: 'Ada Lovelace' },
|
|
6
|
+
{ id: 'u2', name: 'Alan Turing' },
|
|
7
|
+
{ id: 'u3', name: 'Grace Hopper' },
|
|
8
|
+
]
|
|
9
|
+
const ids = (q: string) => filterMentionMembers(members, q).map((m) => m.id)
|
|
10
|
+
|
|
11
|
+
describe('filterMentionMembers', () => {
|
|
12
|
+
it('matches a case-insensitive substring; empty query keeps all', () => {
|
|
13
|
+
expect(ids('')).toEqual(['u1', 'u2', 'u3'])
|
|
14
|
+
expect(ids('grace')).toEqual(['u3'])
|
|
15
|
+
expect(ids('AL')).toEqual(['u2']) // "Alan", case-insensitive; "Ada Lovelace" has no "al"
|
|
16
|
+
expect(ids('zzz')).toEqual([])
|
|
17
|
+
})
|
|
18
|
+
})
|