|
| 1 | +/** |
| 2 | + * WikiLink TipTap 扩展(双向链接的编辑侧) |
| 3 | + * WikiLink TipTap extension (editing side of bidirectional links) |
| 4 | + * |
| 5 | + * @ai-context: 阶段二双向链接。基于 Mention.extend(name:'wikiLink'),输入 |
| 6 | + * `[[` 触发笔记标题自动补全:suggestion char='[',allow 要求 range 文本以 '[[' |
| 7 | + * 开头(即输入了第二个 '['),items 的 query 去掉前导 '[' 后按标题过滤 |
| 8 | + * useNoteStore.notes。选中后以 wikiLink 节点(attrs id/label)替换 `[[xxx`, |
| 9 | + * 渲染为可识别 chip。弹窗经 ReactRenderer 手动定位(项目未装 tippy.js)。 |
| 10 | + * @ai-context: Mention-based wiki-link; `[[` triggers note-title autocomplete; |
| 11 | + * selected note becomes a wikiLink node (attrs id/label) rendered as a chip. |
| 12 | + */ |
| 13 | +import Mention from '@tiptap/extension-mention'; |
| 14 | +import { mergeAttributes } from '@tiptap/core'; |
| 15 | +import { ReactRenderer } from '@tiptap/react'; |
| 16 | +import type { SuggestionProps, SuggestionKeyDownProps } from '@tiptap/suggestion'; |
| 17 | +import { useNoteStore } from '../../store/useNoteStore'; |
| 18 | +import { WikiLinkSuggestions, type WikiLinkSuggestionsHandle, type WikiLinkItem } from './WikiLinkSuggestions'; |
| 19 | + |
| 20 | +type RectGetter = (() => DOMRect | null) | null | undefined; |
| 21 | + |
| 22 | +/** suggestion 弹窗渲染器(ReactRenderer + 手动 fixed 定位,无 tippy 依赖) */ |
| 23 | +function suggestionRender() { |
| 24 | + let component: ReactRenderer<WikiLinkSuggestionsHandle, { items: WikiLinkItem[]; command: (item: { id: string; label: string }) => void }> | null = null; |
| 25 | + let container: HTMLDivElement | null = null; |
| 26 | + |
| 27 | + const updatePosition = (clientRect: RectGetter) => { |
| 28 | + if (!container || !clientRect) return; |
| 29 | + const rect = clientRect(); |
| 30 | + if (!rect) return; |
| 31 | + container.style.left = `${rect.left}px`; |
| 32 | + container.style.top = `${rect.bottom + 4}px`; |
| 33 | + }; |
| 34 | + |
| 35 | + const cleanup = () => { |
| 36 | + component?.destroy(); |
| 37 | + container?.remove(); |
| 38 | + component = null; |
| 39 | + container = null; |
| 40 | + }; |
| 41 | + |
| 42 | + return { |
| 43 | + onStart: (props: SuggestionProps) => { |
| 44 | + component = new ReactRenderer(WikiLinkSuggestions, { props, editor: props.editor }); |
| 45 | + container = document.createElement('div'); |
| 46 | + container.style.position = 'fixed'; |
| 47 | + container.style.zIndex = '9999'; |
| 48 | + container.appendChild(component.element); |
| 49 | + document.body.appendChild(container); |
| 50 | + updatePosition(props.clientRect); |
| 51 | + }, |
| 52 | + onUpdate: (props: SuggestionProps) => { |
| 53 | + component?.updateProps(props); |
| 54 | + updatePosition(props.clientRect); |
| 55 | + }, |
| 56 | + onKeyDown: (props: SuggestionKeyDownProps) => { |
| 57 | + if (props.event.key === 'Escape') { cleanup(); return true; } |
| 58 | + return component?.ref?.onKeyDown(props) ?? false; |
| 59 | + }, |
| 60 | + onExit: cleanup, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +export const WikiLink = Mention.extend({ |
| 65 | + name: 'wikiLink', |
| 66 | + |
| 67 | + renderHTML({ node, HTMLAttributes }) { |
| 68 | + return [ |
| 69 | + 'span', |
| 70 | + mergeAttributes(HTMLAttributes, { class: 'wiki-link', 'data-id': node.attrs.id }), |
| 71 | + `${node.attrs.label ?? ''}`, |
| 72 | + ]; |
| 73 | + }, |
| 74 | + |
| 75 | + renderText({ node }) { |
| 76 | + return `[[${node.attrs.label ?? ''}]]`; |
| 77 | + }, |
| 78 | +}).configure({ |
| 79 | + HTMLAttributes: { class: 'wiki-link' }, |
| 80 | + suggestion: { |
| 81 | + char: '[', |
| 82 | + // 仅当输入了 `[[`(range 文本以 '[[' 开头)时才弹出 / only after `[[` |
| 83 | + allow: ({ editor, range }) => { |
| 84 | + const text = editor.state.doc.textBetween(range.from, range.to); |
| 85 | + return text.startsWith('[['); |
| 86 | + }, |
| 87 | + // query 含前导 '['(第二个括号),去掉后按标题过滤 / strip leading '[' then filter by title |
| 88 | + items: ({ query }): WikiLinkItem[] => { |
| 89 | + const realQuery = query.startsWith('[') ? query.slice(1) : query; |
| 90 | + const q = realQuery.trim().toLowerCase(); |
| 91 | + return useNoteStore.getState().notes |
| 92 | + .filter((n) => (n.title || '').toLowerCase().includes(q)) |
| 93 | + .slice(0, 8) |
| 94 | + .map((n) => ({ id: n.id, label: n.title })); |
| 95 | + }, |
| 96 | + command: ({ editor, range, props }) => { |
| 97 | + const attrs = props as { id?: string | null; label?: string | null }; |
| 98 | + if (!attrs.id) return; |
| 99 | + editor.chain().focus() |
| 100 | + .deleteRange(range) |
| 101 | + .insertContent({ type: 'wikiLink', attrs: { id: attrs.id, label: attrs.label ?? '' } }) |
| 102 | + .insertContent(' ') |
| 103 | + .run(); |
| 104 | + }, |
| 105 | + render: suggestionRender, |
| 106 | + }, |
| 107 | +}); |
0 commit comments