|
| 1 | +# wangEditor mention plugin |
| 2 | + |
| 3 | +[中文文档](./README.md) |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +[wangeditor-next](https://github.com/cycleccc/wangEditor-next) mention plugin, like `@James`. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```shell |
| 14 | +yarn add @wangeditor-next/plugin-mention |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +[Vue demo source code](https://github.com/wangfupeng1988/vue2-wangeditor-demo/blob/master/src/components/MyEditorWithMention.vue) |
| 20 | + |
| 21 | +### Use in editor |
| 22 | + |
| 23 | +```ts |
| 24 | +import { IDomEditor, Boot, IEditorConfig } from '@wangeditor-next/editor' |
| 25 | +import mentionModule, { MentionElement } from '@wangeditor-next/plugin-mention' |
| 26 | + |
| 27 | +// Register |
| 28 | +// You should register this before create editor, and register only once (not repeatedly). |
| 29 | +Boot.registerModule(mentionModule) |
| 30 | + |
| 31 | +// Show your modal |
| 32 | +function showModal(editor: IDomEditor) { |
| 33 | + // Get cursor's position info, to set modal position |
| 34 | + const domSelection = document.getSelection() |
| 35 | + const domRange = domSelection.getRangeAt(0) |
| 36 | + if (domRange == null) return |
| 37 | + const selectionRect = domRange.getBoundingClientRect() |
| 38 | + |
| 39 | + // Get editor container's position info, maybe help to get right modal position |
| 40 | + const containerRect = editor.getEditableContainer().getBoundingClientRect() |
| 41 | + |
| 42 | + // Show your modal, and set position |
| 43 | + // PS: You must implement the modal yourself, use <div> or Vue React component |
| 44 | + |
| 45 | + |
| 46 | + // Insert mention node when emit some event. |
| 47 | + function insertMention() { |
| 48 | + const mentionNode: MentionElement = { |
| 49 | + type: 'mention', // must be 'mention' |
| 50 | + value: 'James', // text |
| 51 | + info: { x: 1, y: 2 }, // extended info |
| 52 | + children: [{ text: '' }], // must have an empty text node in children |
| 53 | + } |
| 54 | + |
| 55 | + editor.restoreSelection() |
| 56 | + editor.deleteBackward('character') // delete '@' |
| 57 | + editor.insertNode(mentionNode) |
| 58 | + editor.move(1) // move curser |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// hide your modal |
| 63 | +function hideModal(editor: IDomEditor) { |
| 64 | + // hide your modal |
| 65 | +} |
| 66 | + |
| 67 | +// editor config |
| 68 | +const editorConfig: Partial<IEditorConfig> = { |
| 69 | + EXTEND_CONF: { |
| 70 | + mentionConfig: { |
| 71 | + showModal, // required |
| 72 | + hideModal, // required |
| 73 | + }, |
| 74 | + }, |
| 75 | + |
| 76 | + // others... |
| 77 | +} |
| 78 | + |
| 79 | +// Then create editor and toolbar, you will use `editorConfig` |
| 80 | +``` |
| 81 | + |
| 82 | +### Render HTML |
| 83 | + |
| 84 | +You will get a mention's HTML format like this. You need to `decodeURIComponent` the value of `data-info`. |
| 85 | + |
| 86 | +```html |
| 87 | +<span data-w-e-type="mention" data-w-e-is-void data-w-e-is-inline data-value="James" data-info="%7B%22x%22%3A10%7D">@James</span> |
| 88 | +``` |
| 89 | + |
| 90 | + |
0 commit comments