refactoring

This commit is contained in:
Philipp Kühn
2021-01-15 09:25:50 +01:00
parent 83b3de8711
commit a5d28a0184

View File

@@ -2,14 +2,19 @@ import { Plugin, PluginKey } from 'prosemirror-state'
import { ResolvedPos } from 'prosemirror-model' import { ResolvedPos } from 'prosemirror-model'
import { Decoration, DecorationSet } from 'prosemirror-view' import { Decoration, DecorationSet } from 'prosemirror-view'
// Create a matcher that matches when a specific character is typed. Useful for @mentions and #tags. export interface Trigger {
function triggerCharacter({ char: string,
char = '@', allowSpaces: boolean,
allowSpaces = false, startOfLine: boolean,
startOfLine = false, $position: ResolvedPos,
}) { }
// Create a matcher that matches when a specific character is typed. Useful for @mentions and #tags.
function triggerCharacter(config: Trigger) {
const {
char, allowSpaces, startOfLine, $position,
} = config
return ($position: ResolvedPos) => {
// cancel if top level node // cancel if top level node
if ($position.depth <= 0) { if ($position.depth <= 0) {
return false return false
@@ -65,15 +70,12 @@ function triggerCharacter({
} }
return position return position
}
} }
export function Suggestion({ export function Suggestion({
matcher = { char = '@',
char: '@', allowSpaces = false,
allowSpaces: false, startOfLine = false,
startOfLine: false,
},
appendText = null, appendText = null,
suggestionClass = 'suggestion', suggestionClass = 'suggestion',
command = () => false, command = () => false,
@@ -171,7 +173,6 @@ export function Suggestion({
}, },
state: { state: {
// Initialize the plugin's internal state. // Initialize the plugin's internal state.
init() { init() {
return { return {
@@ -196,9 +197,16 @@ export function Suggestion({
// 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 = triggerCharacter(matcher)($position) const match = triggerCharacter({
char,
allowSpaces,
startOfLine,
$position,
})
const decorationId = (Math.random() + 1).toString(36).substr(2, 5) const decorationId = (Math.random() + 1).toString(36).substr(2, 5)
console.log({ match })
// If we found a match, update the current state to show it // If we found a match, update the current state to show it
if (match) { if (match) {
next.active = true next.active = true