Mendefinisikan ekstensi
Gunakan objek Extension saat format membutuhkan atribut, beberapa perintah, atau status aktif tersendiri. Contoh ini menyimpan pengenal numerik pada elemen <cite>.
Pilih @Ada lalu klik Tambah sebutan. Hasil HTML menampilkan <cite data-id="42">@Ada</cite>.
Contoh interaktif
Hasil contoh
import { EditorContent, EditorProvider, useEditor, type Extension } from 'yurich';
export const Mention: Extension = {
name: 'mention',
marks: [{
tag: 'cite',
type: 'mention',
attrs: {
'data-id': value => value && /^\d+$/.test(value) ? value : null,
},
}],
commands: {
setMention(editor, value) {
if (typeof value === 'string' && /^\d+$/.test(value)) {
editor.setMark('cite', { 'data-id': value });
}
},
removeMention(editor) {
editor.removeMark('cite');
},
},
isActive: editor => editor.isMarkActive('cite'),
};
export function MentionEditor() {
const editor = useEditor({
extensions: [Mention],
initialValue: "<p>Minta @Ada mencoba tombol Tebal.</p>",
});
return (
<EditorProvider editor={editor}>
<button
type="button"
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('setMention', '42')}
>
Tambah sebutan
</button>
<EditorContent ariaLabel="Editor" />
</EditorProvider>
);
}
Masukkan Mention ke daftar ekstensi. Pilih teks, jalankan editor.execute('setMention', '42'), lalu periksa hasil dengan editor.getHTML(). Gunakan useIsActive(editor, 'mention') untuk menyinkronkan toolbar.