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 { Decoration, DecorationSet } from 'prosemirror-view'
// Create a matcher that matches when a specific character is typed. Useful for @mentions and #tags.
function triggerCharacter({
char = '@',
allowSpaces = false,
startOfLine = false,
}) {
export interface Trigger {
char: string,
allowSpaces: boolean,
startOfLine: boolean,
$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
if ($position.depth <= 0) {
return false
@ -66,14 +71,11 @@ function triggerCharacter({
return position
}
}
export function Suggestion({
matcher = {
char: '@',
allowSpaces: false,
startOfLine: false,
},
char = '@',
allowSpaces = false,
startOfLine = false,
appendText = null,
suggestionClass = 'suggestion',
command = () => false,
@ -171,7 +173,6 @@ export function Suggestion({
},
state: {
// Initialize the plugin's internal state.
init() {
return {
@ -196,9 +197,16 @@ export function Suggestion({
// Try to match against where our cursor currently is
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)
console.log({ match })
// If we found a match, update the current state to show it
if (match) {
next.active = true