add clearNodes command

This commit is contained in:
Philipp Kühn
2020-11-02 12:45:19 +01:00
parent 3dc25640c9
commit 85c1a8ace9
3 changed files with 45 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
import { liftTarget } from 'prosemirror-transform'
import { Command } from '../Editor'
import { createExtension } from '../Extension'
export const ClearNodes = createExtension({
addCommands() {
return {
clearNodes: (): Command => ({ state, tr }) => {
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) {
tr.setNodeMarkup(nodeRange.start, state.schema.nodes.paragraph)
}
if (targetLiftDepth || targetLiftDepth === 0) {
tr.lift(nodeRange, targetLiftDepth)
}
}
}
})
return true
},
}
},
})
declare module '../Editor' {
interface AllExtensions {
ClearNodes: typeof ClearNodes,
}
}

View File

@@ -1,5 +1,6 @@
export { Blur } from './blur' export { Blur } from './blur'
export { ClearContent } from './clearContent' export { ClearContent } from './clearContent'
export { ClearNodes } from './clearNodes'
export { DeleteSelection } from './deleteSelection' export { DeleteSelection } from './deleteSelection'
export { Focus } from './focus' export { Focus } from './focus'
export { InsertHTML } from './insertHTML' export { InsertHTML } from './insertHTML'

View File

@@ -44,7 +44,8 @@ export const ToggleList = createExtension({
// try to convert node to paragraph if needed // try to convert node to paragraph if needed
if (!canWrapInList) { if (!canWrapInList) {
return chain() return chain()
.setBlockType('paragraph') // .setBlockType('paragraph')
.clearNodes()
.wrapInList(listType) .wrapInList(listType)
.run() .run()
} }