2020-11-05 05:38:52 +08:00
|
|
|
import { liftTarget } from 'prosemirror-transform'
|
2021-02-10 16:59:35 +08:00
|
|
|
import { Command, Commands } from '../types'
|
2020-11-05 05:38:52 +08:00
|
|
|
|
2020-11-18 23:43:27 +08:00
|
|
|
/**
|
|
|
|
* Normalize nodes to a simple paragraph.
|
|
|
|
*/
|
2021-02-10 16:59:35 +08:00
|
|
|
export const clearNodes: Commands['clearNodes'] = () => ({ state, tr, dispatch }) => {
|
2020-11-05 05:38:52 +08:00
|
|
|
const { selection } = tr
|
|
|
|
const { from, to } = selection
|
|
|
|
|
|
|
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
|
|
if (!node.type.isText) {
|
|
|
|
const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1))
|
|
|
|
const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1))
|
|
|
|
const nodeRange = fromPos.blockRange(toPos)
|
|
|
|
|
|
|
|
if (nodeRange) {
|
|
|
|
const targetLiftDepth = liftTarget(nodeRange)
|
|
|
|
|
|
|
|
if (node.type.isTextblock && dispatch) {
|
2021-01-29 15:56:52 +08:00
|
|
|
tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType)
|
2020-11-05 05:38:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
|
|
|
|
tr.lift(nodeRange, targetLiftDepth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands {
|
|
|
|
clearNodes: () => Command,
|
|
|
|
}
|
|
|
|
}
|