From 7712325ba92bfb3bbbffccba6a5f437314cbdf1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Tue, 19 Jan 2021 12:03:38 +0100 Subject: [PATCH] fix mention rendering --- docs/src/demos/Nodes/Mention/MentionList.vue | 2 +- docs/src/demos/Nodes/Mention/index.vue | 2 +- packages/core/src/commands/replace.ts | 4 ++-- packages/extension-mention/src/mention.ts | 14 ++++++++------ packages/suggestion/src/suggestion.ts | 10 +++++----- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/src/demos/Nodes/Mention/MentionList.vue b/docs/src/demos/Nodes/Mention/MentionList.vue index 5a6257514..bf9b95520 100644 --- a/docs/src/demos/Nodes/Mention/MentionList.vue +++ b/docs/src/demos/Nodes/Mention/MentionList.vue @@ -74,7 +74,7 @@ export default { const item = this.items[index] if (item) { - this.command(item) + this.command({ id: item }) } }, }, diff --git a/docs/src/demos/Nodes/Mention/index.vue b/docs/src/demos/Nodes/Mention/index.vue index 3c163d173..6cd716f02 100644 --- a/docs/src/demos/Nodes/Mention/index.vue +++ b/docs/src/demos/Nodes/Mention/index.vue @@ -70,7 +70,7 @@ export default { }), ], content: ` -

text

+

text

`, }) }, diff --git a/packages/core/src/commands/replace.ts b/packages/core/src/commands/replace.ts index 92c693a86..ae29981ae 100644 --- a/packages/core/src/commands/replace.ts +++ b/packages/core/src/commands/replace.ts @@ -1,11 +1,11 @@ import { NodeType } from 'prosemirror-model' import getNodeType from '../helpers/getNodeType' -import { Command, Range } from '../types' +import { Command, Range, AnyObject } from '../types' /** * 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 { $from, $to } = state.selection const index = $from.index() diff --git a/packages/extension-mention/src/mention.ts b/packages/extension-mention/src/mention.ts index d4b7f6692..cae92f99c 100644 --- a/packages/extension-mention/src/mention.ts +++ b/packages/extension-mention/src/mention.ts @@ -22,9 +22,11 @@ export const Mention = Node.create({ return { id: { default: null, - }, - label: { - default: null, + parseHTML: element => { + return { + id: element.getAttribute('data-mention'), + } + }, }, } }, @@ -38,7 +40,7 @@ export const Mention = Node.create({ }, renderHTML({ node, HTMLAttributes }) { - return ['span', HTMLAttributes, `@${node.attrs.label}`] + return ['span', HTMLAttributes, `@${node.attrs.id}`] }, addProseMirrorPlugins() { @@ -46,11 +48,11 @@ export const Mention = Node.create({ Suggestion({ editor: this.editor, ...this.options, - command: ({ range }) => { + command: ({ range, attributes }) => { this.editor .chain() .focus() - .replace(range, 'mention') + .replace(range, 'mention', attributes) .insertText(' ') .run() }, diff --git a/packages/suggestion/src/suggestion.ts b/packages/suggestion/src/suggestion.ts index 37fbf43e7..2d73511bc 100644 --- a/packages/suggestion/src/suggestion.ts +++ b/packages/suggestion/src/suggestion.ts @@ -1,4 +1,4 @@ -import { Editor, Range } from '@tiptap/core' +import { Editor, Range, AnyObject } from '@tiptap/core' import { Plugin, PluginKey } from 'prosemirror-state' import { Decoration, DecorationSet, EditorView } from 'prosemirror-view' import { findSuggestionMatch } from './findSuggestionMatch' @@ -10,7 +10,7 @@ export interface SuggestionOptions { allowSpaces?: boolean, startOfLine?: boolean, suggestionClass?: string, - command?: (props: { range: Range }) => void, + command?: (props: { range: Range, attributes: AnyObject }) => void, items?: (query: string) => any[], render?: () => { onStart?: (props: SuggestionProps) => void, @@ -26,7 +26,7 @@ export interface SuggestionProps { query: string, text: string, items: any[], - command: () => void, + command: (attributes: AnyObject) => void, decorationNode: Element | null, virtualNode: VirtualNode | null, } @@ -83,8 +83,8 @@ export function Suggestion({ items: (handleChange || handleStart) ? await items(state.query) : [], - command: () => { - command({ range: state.range }) + command: attributes => { + command({ range: state.range, attributes }) }, decorationNode, // build a virtual node for popper.js or tippy.js