feat: add extendEmptyMarkRange option to mark commands (#1859)

This commit is contained in:
Philipp Kühn
2021-09-08 09:32:22 +02:00
committed by GitHub
parent b95507797d
commit 14e458ea7d
3 changed files with 42 additions and 21 deletions

View File

@@ -9,17 +9,27 @@ declare module '@tiptap/core' {
/**
* Toggle a mark on and off.
*/
toggleMark: (typeOrName: string | MarkType, attributes?: Record<string, any>) => ReturnType,
toggleMark: (
typeOrName: string | MarkType,
attributes?: Record<string, any>,
options?: {
/**
* Removes the mark even across the current selection. Defaults to `false`.
*/
extendEmptyMarkRange?: boolean,
},
) => ReturnType,
}
}
}
export const toggleMark: RawCommands['toggleMark'] = (typeOrName, attributes = {}) => ({ state, commands }) => {
export const toggleMark: RawCommands['toggleMark'] = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
const { extendEmptyMarkRange = false } = options
const type = getMarkType(typeOrName, state.schema)
const isActive = isMarkActive(state, type, attributes)
if (isActive) {
return commands.unsetMark(type)
return commands.unsetMark(type, { extendEmptyMarkRange })
}
return commands.setMark(type, attributes)