fix mention rendering

This commit is contained in:
Philipp Kühn
2021-01-19 12:03:38 +01:00
parent c13d65c842
commit 7712325ba9
5 changed files with 17 additions and 15 deletions

View File

@@ -74,7 +74,7 @@ export default {
const item = this.items[index] const item = this.items[index]
if (item) { if (item) {
this.command(item) this.command({ id: item })
} }
}, },
}, },

View File

@@ -70,7 +70,7 @@ export default {
}), }),
], ],
content: ` content: `
<p>text <span data-mention></span></p> <p>text <span data-mention="Philipp"></span></p>
`, `,
}) })
}, },

View File

@@ -1,11 +1,11 @@
import { NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType' import getNodeType from '../helpers/getNodeType'
import { Command, Range } from '../types' import { Command, Range, AnyObject } from '../types'
/** /**
* Replaces text with a node within a range. * Replaces text with a node within a range.
*/ */
export const replace = (range: Range | null = null, typeOrName: string | NodeType, attrs = {}): Command => ({ tr, state, dispatch }) => { export const replace = (range: Range | null = null, typeOrName: string | NodeType, attrs: AnyObject = {}): Command => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const { $from, $to } = state.selection const { $from, $to } = state.selection
const index = $from.index() const index = $from.index()

View File

@@ -22,9 +22,11 @@ export const Mention = Node.create({
return { return {
id: { id: {
default: null, default: null,
}, parseHTML: element => {
label: { return {
default: null, id: element.getAttribute('data-mention'),
}
},
}, },
} }
}, },
@@ -38,7 +40,7 @@ export const Mention = Node.create({
}, },
renderHTML({ node, HTMLAttributes }) { renderHTML({ node, HTMLAttributes }) {
return ['span', HTMLAttributes, `@${node.attrs.label}`] return ['span', HTMLAttributes, `@${node.attrs.id}`]
}, },
addProseMirrorPlugins() { addProseMirrorPlugins() {
@@ -46,11 +48,11 @@ export const Mention = Node.create({
Suggestion({ Suggestion({
editor: this.editor, editor: this.editor,
...this.options, ...this.options,
command: ({ range }) => { command: ({ range, attributes }) => {
this.editor this.editor
.chain() .chain()
.focus() .focus()
.replace(range, 'mention') .replace(range, 'mention', attributes)
.insertText(' ') .insertText(' ')
.run() .run()
}, },

View File

@@ -1,4 +1,4 @@
import { Editor, Range } from '@tiptap/core' import { Editor, Range, AnyObject } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state' import { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view' import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
import { findSuggestionMatch } from './findSuggestionMatch' import { findSuggestionMatch } from './findSuggestionMatch'
@@ -10,7 +10,7 @@ export interface SuggestionOptions {
allowSpaces?: boolean, allowSpaces?: boolean,
startOfLine?: boolean, startOfLine?: boolean,
suggestionClass?: string, suggestionClass?: string,
command?: (props: { range: Range }) => void, command?: (props: { range: Range, attributes: AnyObject }) => void,
items?: (query: string) => any[], items?: (query: string) => any[],
render?: () => { render?: () => {
onStart?: (props: SuggestionProps) => void, onStart?: (props: SuggestionProps) => void,
@@ -26,7 +26,7 @@ export interface SuggestionProps {
query: string, query: string,
text: string, text: string,
items: any[], items: any[],
command: () => void, command: (attributes: AnyObject) => void,
decorationNode: Element | null, decorationNode: Element | null,
virtualNode: VirtualNode | null, virtualNode: VirtualNode | null,
} }
@@ -83,8 +83,8 @@ export function Suggestion({
items: (handleChange || handleStart) items: (handleChange || handleStart)
? await items(state.query) ? await items(state.query)
: [], : [],
command: () => { command: attributes => {
command({ range: state.range }) command({ range: state.range, attributes })
}, },
decorationNode, decorationNode,
// build a virtual node for popper.js or tippy.js // build a virtual node for popper.js or tippy.js