mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 11:49:02 +08:00
refactoring
This commit is contained in:
parent
113133b74d
commit
9e6fbd6c88
@ -16,15 +16,20 @@ export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatc
|
||||
const { selection } = tr
|
||||
const { ranges } = selection
|
||||
|
||||
ranges.forEach(range => {
|
||||
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
|
||||
if (!dispatch) {
|
||||
return true
|
||||
}
|
||||
|
||||
ranges.forEach(({ $from, $to }) => {
|
||||
state.doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
|
||||
if (node.type.isText) {
|
||||
return
|
||||
}
|
||||
|
||||
const $fromPos = tr.doc.resolve(tr.mapping.map(pos))
|
||||
const $toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize))
|
||||
const nodeRange = $fromPos.blockRange($toPos)
|
||||
const { doc, mapping } = tr
|
||||
const $mappedFrom = doc.resolve(mapping.map(pos))
|
||||
const $mappedTo = doc.resolve(mapping.map(pos + node.nodeSize))
|
||||
const nodeRange = $mappedFrom.blockRange($mappedTo)
|
||||
|
||||
if (!nodeRange) {
|
||||
return
|
||||
@ -32,13 +37,13 @@ export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatc
|
||||
|
||||
const targetLiftDepth = liftTarget(nodeRange)
|
||||
|
||||
if (node.type.isTextblock && dispatch) {
|
||||
const { defaultType } = $fromPos.parent.contentMatchAt($fromPos.index())
|
||||
if (node.type.isTextblock) {
|
||||
const { defaultType } = $mappedFrom.parent.contentMatchAt($mappedFrom.index())
|
||||
|
||||
tr.setNodeMarkup(nodeRange.start, defaultType)
|
||||
}
|
||||
|
||||
if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
|
||||
if (targetLiftDepth || targetLiftDepth === 0) {
|
||||
tr.lift(nodeRange, targetLiftDepth)
|
||||
}
|
||||
})
|
||||
|
@ -24,14 +24,17 @@ export const setNode: RawCommands['setNode'] = (typeOrName, attributes = {}) =>
|
||||
return false
|
||||
}
|
||||
|
||||
const canSetBlock = setBlockType(type, attributes)(state)
|
||||
|
||||
if (canSetBlock) {
|
||||
return setBlockType(type, attributes)(state, dispatch)
|
||||
}
|
||||
|
||||
return chain()
|
||||
.clearNodes()
|
||||
// try to convert node to default node if needed
|
||||
.command(({ commands }) => {
|
||||
const canSetBlock = setBlockType(type, attributes)(state)
|
||||
|
||||
if (canSetBlock) {
|
||||
return true
|
||||
}
|
||||
|
||||
return commands.clearNodes()
|
||||
})
|
||||
.command(({ state: updatedState }) => {
|
||||
return setBlockType(type, attributes)(updatedState, dispatch)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user