mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 06:03:22 +08:00
refactoring
This commit is contained in:
parent
eb695878a0
commit
44a026416b
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user