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)

View File

@@ -9,35 +9,46 @@ declare module '@tiptap/core' {
/**
* Remove all marks in the current selection.
*/
unsetMark: (typeOrName: string | MarkType) => ReturnType,
unsetMark: (
typeOrName: string | MarkType,
options?: {
/**
* Removes the mark even across the current selection. Defaults to `false`.
*/
extendEmptyMarkRange?: boolean,
},
) => ReturnType,
}
}
}
export const unsetMark: RawCommands['unsetMark'] = typeOrName => ({ tr, state, dispatch }) => {
export const unsetMark: RawCommands['unsetMark'] = (typeOrName, options = {}) => ({ tr, state, dispatch }) => {
const { extendEmptyMarkRange = false } = options
const { selection } = tr
const type = getMarkType(typeOrName, state.schema)
const { $from, empty, ranges } = selection
if (dispatch) {
if (empty) {
let { from, to } = selection
const range = getMarkRange($from, type)
if (!dispatch) {
return true
}
if (range) {
from = range.from
to = range.to
}
if (empty && extendEmptyMarkRange) {
let { from, to } = selection
const range = getMarkRange($from, type)
tr.removeMark(from, to, type)
} else {
ranges.forEach(range => {
tr.removeMark(range.$from.pos, range.$to.pos, type)
})
if (range) {
from = range.from
to = range.to
}
tr.removeStoredMark(type)
tr.removeMark(from, to, type)
} else {
ranges.forEach(range => {
tr.removeMark(range.$from.pos, range.$to.pos, type)
})
}
tr.removeStoredMark(type)
return true
}

View File

@@ -92,10 +92,10 @@ export const Link = Mark.create<LinkOptions>({
return commands.setMark('link', attributes)
},
toggleLink: attributes => ({ commands }) => {
return commands.toggleMark('link', attributes)
return commands.toggleMark('link', attributes, { extendEmptyMarkRange: true })
},
unsetLink: () => ({ commands }) => {
return commands.unsetMark('link')
return commands.unsetMark('link', { extendEmptyMarkRange: true })
},
}
},