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