refactoring

This commit is contained in:
Philipp Kühn
2018-09-29 11:57:49 +02:00
parent 0bcdfb8096
commit 1ad2b6aa78
3 changed files with 17 additions and 7 deletions

View File

@@ -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'

View File

@@ -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)
},

View File

@@ -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