refactoring
This commit is contained in:
@@ -20,3 +20,5 @@ export { default as UnderlineMark } from './marks/Underline'
|
||||
|
||||
export { default as HistoryExtension } from './extensions/History'
|
||||
export { default as PlaceholderExtension } from './extensions/Placeholder'
|
||||
|
||||
export { default as SuggestionsPlugin } from './plugins/Suggestions'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Node } from 'tiptap'
|
||||
import { replaceText } from 'tiptap-commands'
|
||||
import { triggerCharacter, suggestionsPlugin } from '../plugins/suggestions'
|
||||
import { suggestionsPlugin } from '../plugins/suggestions'
|
||||
|
||||
export default class MentionNode extends Node {
|
||||
|
||||
@@ -43,10 +43,11 @@ export default class MentionNode extends Node {
|
||||
return [
|
||||
suggestionsPlugin({
|
||||
suggestionClass: 'mention-suggestion',
|
||||
matcher: triggerCharacter('@', {
|
||||
matcher: {
|
||||
char: '@',
|
||||
allowSpaces: false,
|
||||
startOfLine: false,
|
||||
}),
|
||||
},
|
||||
command: ({ position, attrs, schema }) => {
|
||||
return replaceText(position, schema.nodes.mention, attrs)
|
||||
},
|
||||
|
||||
@@ -8,7 +8,11 @@ import { Decoration, DecorationSet } from 'prosemirror-view';
|
||||
* @param {Boolean} allowSpaces
|
||||
* @returns {function(*)}
|
||||
*/
|
||||
export function triggerCharacter(char, { allowSpaces = false, startOfLine = false }) {
|
||||
export function triggerCharacter({
|
||||
char = '@',
|
||||
allowSpaces = false,
|
||||
startOfLine = false,
|
||||
}) {
|
||||
|
||||
/**
|
||||
* @param {ResolvedPos} $position
|
||||
@@ -24,7 +28,6 @@ export function triggerCharacter(char, { allowSpaces = false, startOfLine = fals
|
||||
// Lookup the boundaries of the current node
|
||||
const textFrom = $position.before()
|
||||
const textTo = $position.end()
|
||||
|
||||
const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0')
|
||||
|
||||
let match
|
||||
@@ -65,7 +68,11 @@ export function triggerCharacter(char, { allowSpaces = false, startOfLine = fals
|
||||
* @returns {Plugin}
|
||||
*/
|
||||
export function suggestionsPlugin({
|
||||
matcher = triggerCharacter('#'),
|
||||
matcher = {
|
||||
char: '@',
|
||||
allowSpaces: false,
|
||||
startOfLine: false,
|
||||
},
|
||||
suggestionClass = 'suggestion',
|
||||
command = () => false,
|
||||
items = [],
|
||||
@@ -188,7 +195,7 @@ export function suggestionsPlugin({
|
||||
|
||||
// Try to match against where our cursor currently is
|
||||
const $position = selection.$from
|
||||
const match = matcher($position)
|
||||
const match = triggerCharacter(matcher)($position)
|
||||
const decorationId = (Math.random() + 1).toString(36).substr(2, 5)
|
||||
|
||||
// If we found a match, update the current state to show it
|
||||
Reference in New Issue
Block a user