Backend'ingizdan mention'lar
Mention (eslatma) uchun standart kit'ga mentions sifatida qidiruv funksiyasini bering:
ts
import type { Editor } from '@writeon/core'
import type { KitMention } from '@writeon/kit'
interface Person {
id: string
name: string
title?: string
}
// "@"dan keyin yozilgan matn bilan chaqiriladi. Eskiroq so'rovga kelgan
// javoblar e'tiborsiz qoldiriladi, shuning uchun sekin so'rov hech
// qachon eskirgan ro'yxatni ko'rsatmaydi.
export async function searchPeople(
query: string,
): Promise<KitMention[]> {
const q = encodeURIComponent(query)
const response = await fetch(`/api/people?q=${q}&limit=8`)
if (!response.ok) return []
const people = (await response.json()) as Person[]
return people.map((person) => ({
id: person.id,
label: person.name,
...(person.title ? { description: person.title } : {}),
}))
}
export async function mountWithMentions(
root: HTMLElement,
editor: Editor,
) {
const { mountAdvanced } = await import('@writeon/kit/advanced')
return mountAdvanced(root, editor, { mentions: searchPeople })
}- Menyu so'z boshidagi
@'dan keyin ochiladi va bo'shliq, Escape bosilganda yoki hech narsa mos kelmaganda yopiladi. - Kishini tanlash
idvalabel'li mention node'ini qo'shadi;idsiz saqlab, keyinroq aniqlaydigan qiymatdir. Ikkalasini ham schema tekshiradi. - Natijalar ekran o'quvchilarga e'lon qilinadi ("5 suggestions"), fokus esa hech qachon muharrirdan chiqmaydi.
Mention'lar hujjatda oddiy ma'lumot. Odamlarga xabar yuborish uchun serverda tekshirilgan hujjatdan mention node'larini o'qing:
ts
import type { NodeJSON } from '@writeon/core'
function mentionedIds(node: NodeJSON, out = new Set<string>()): Set<string> {
if (node.type === 'mention' && typeof node.attrs?.id === 'string') out.add(node.attrs.id)
for (const child of node.content ?? []) mentionedIds(child, out)
return out
}Kit'siz @writeon/ui'dagi createSuggestionMenu(editor, { char: '@', items, label })'ni @writeon/advanced'dagi insertMention() bilan birga ishlating.