feat: add extendEmptyMarkRange option to mark commands (#1859)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 })
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user