दस्तावेज़

एक्सटेंशन ऑब्जेक्ट बनाएँ

ऐट्रिब्यूट, एक से ज़्यादा कमांड या अपनी सक्रिय स्थिति के लिए Extension ऑब्जेक्ट इस्तेमाल करें। इस उदाहरण में <cite> टैग पर अंकों वाला पहचानकर्ता सेव होता है।

@Ada चुनें और मेंशन जोड़ें दबाएँ। HTML आउटपुट में <cite data-id="42">@Ada</cite> दिखेगा।

लाइव उदाहरण
उदाहरण का परिणाम
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>@Ada से बोल्ड बटन आज़माने को कहें।</p>",
  });

  return (
    <EditorProvider editor={editor}>
      <button
        type="button"
        onMouseDown={event => event.preventDefault()}
        onClick={() => editor.execute('setMention', '42')}
      >
        मेंशन जोड़ें
      </button>
      <EditorContent ariaLabel="एडिटर" />
    </EditorProvider>
  );
}

Mention को एक्सटेंशन सूची में जोड़ें। टेक्स्ट चुनकर editor.execute('setMention', '42') चलाएँ और editor.getHTML() से नतीजा देखें। टूलबार की स्थिति के लिए useIsActive(editor, 'mention') इस्तेमाल करें।

एक्सटेंशन की जाँच करें