feature(core): add exit handling for marks (#2925)
* feat(core): add exit handling for marks * docs(core): add information about exitable marks
This commit is contained in:
@@ -304,6 +304,11 @@ declare module '@tiptap/core' {
|
||||
parent: ParentConfig<MarkConfig<Options, Storage>>['excludes'],
|
||||
}) => MarkSpec['excludes']),
|
||||
|
||||
/**
|
||||
* Marks this Mark as exitable
|
||||
*/
|
||||
exitable?: boolean | (() => boolean),
|
||||
|
||||
/**
|
||||
* Group
|
||||
*/
|
||||
@@ -486,4 +491,38 @@ export class Mark<Options = any, Storage = any> {
|
||||
|
||||
return extension
|
||||
}
|
||||
|
||||
static handleExit({
|
||||
editor,
|
||||
mark,
|
||||
}: {
|
||||
editor: Editor
|
||||
mark: Mark
|
||||
}) {
|
||||
const { tr } = editor.state
|
||||
const currentPos = editor.state.selection.$from
|
||||
const isAtEnd = currentPos.pos === currentPos.end()
|
||||
|
||||
if (isAtEnd) {
|
||||
const currentMarks = currentPos.marks()
|
||||
const isInMark = !!currentMarks.find(m => m?.type.name === mark.name)
|
||||
|
||||
if (!isInMark) {
|
||||
return false
|
||||
}
|
||||
|
||||
const removeMark = currentMarks.find(m => m?.type.name === mark.name)
|
||||
|
||||
if (removeMark) {
|
||||
tr.removeStoredMark(removeMark)
|
||||
}
|
||||
tr.insertText(' ', currentPos.pos)
|
||||
|
||||
editor.view.dispatch(tr)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user