Render at mention with Linkify component
- mentions are now Next <Link> rather than <a> - fix bug where editor.getText() returns [object Object] for mentions - fix mention rendering for posted markets
This commit is contained in:
parent
340fbcff49
commit
5aeedb0a1c
|
@ -22,6 +22,7 @@ import { FileUploadButton } from './file-upload-button'
|
||||||
import { linkClass } from './site-link'
|
import { linkClass } from './site-link'
|
||||||
import { useUsers } from 'web/hooks/use-users'
|
import { useUsers } from 'web/hooks/use-users'
|
||||||
import { mentionSuggestion } from './editor/mention-suggestion'
|
import { mentionSuggestion } from './editor/mention-suggestion'
|
||||||
|
import { DisplayMention } from './editor/mention'
|
||||||
|
|
||||||
const proseClass = clsx(
|
const proseClass = clsx(
|
||||||
'prose prose-p:my-0 prose-li:my-0 prose-blockquote:not-italic max-w-none prose-quoteless leading-relaxed',
|
'prose prose-p:my-0 prose-li:my-0 prose-blockquote:not-italic max-w-none prose-quoteless leading-relaxed',
|
||||||
|
@ -62,16 +63,7 @@ export function useTextEditor(props: {
|
||||||
class: clsx('no-underline !text-indigo-700', linkClass),
|
class: clsx('no-underline !text-indigo-700', linkClass),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
Mention.configure({
|
DisplayMention.configure({
|
||||||
HTMLAttributes: {
|
|
||||||
class: clsx('not-prose text-indigo-700', linkClass),
|
|
||||||
},
|
|
||||||
renderLabel: ({ options, node }) =>
|
|
||||||
[
|
|
||||||
'a',
|
|
||||||
{ href: node.attrs.label },
|
|
||||||
`${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`,
|
|
||||||
] as any,
|
|
||||||
suggestion: mentionSuggestion(users),
|
suggestion: mentionSuggestion(users),
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
@ -163,7 +155,11 @@ function RichContent(props: { content: JSONContent }) {
|
||||||
const { content } = props
|
const { content } = props
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
editorProps: { attributes: { class: proseClass } },
|
editorProps: { attributes: { class: proseClass } },
|
||||||
extensions: exhibitExts,
|
extensions: [
|
||||||
|
// replace tiptap's Mention with ours, to add style and link
|
||||||
|
...exhibitExts.filter((ex) => ex.name !== Mention.name),
|
||||||
|
DisplayMention,
|
||||||
|
],
|
||||||
content,
|
content,
|
||||||
editable: false,
|
editable: false,
|
||||||
})
|
})
|
||||||
|
|
29
web/components/editor/mention.tsx
Normal file
29
web/components/editor/mention.tsx
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import Mention from '@tiptap/extension-mention'
|
||||||
|
import {
|
||||||
|
mergeAttributes,
|
||||||
|
NodeViewWrapper,
|
||||||
|
ReactNodeViewRenderer,
|
||||||
|
} from '@tiptap/react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { Linkify } from '../linkify'
|
||||||
|
|
||||||
|
const name = 'mention-component'
|
||||||
|
|
||||||
|
const MentionComponent = (props: any) => {
|
||||||
|
return (
|
||||||
|
<NodeViewWrapper className={clsx(name, 'not-prose inline text-indigo-700')}>
|
||||||
|
<Linkify text={'@' + props.node.attrs.label} />
|
||||||
|
</NodeViewWrapper>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mention extension that renders React. See:
|
||||||
|
* https://tiptap.dev/guide/custom-extensions#extend-existing-extensions
|
||||||
|
* https://tiptap.dev/guide/node-views/react#render-a-react-component
|
||||||
|
*/
|
||||||
|
export const DisplayMention = Mention.extend({
|
||||||
|
parseHTML: () => [{ tag: name }],
|
||||||
|
renderHTML: ({ HTMLAttributes }) => [name, mergeAttributes(HTMLAttributes)],
|
||||||
|
addNodeView: () => ReactNodeViewRenderer(MentionComponent),
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user