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:
Dominik
2022-08-22 15:23:44 +02:00
committed by GitHub
parent f558417a25
commit 5fed0f2fc6
4 changed files with 65 additions and 4 deletions

View File

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