add clearNodes command
This commit is contained in:
42
packages/core/src/extensions/clearNodes.ts
Normal file
42
packages/core/src/extensions/clearNodes.ts
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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'
|
||||||
|
|||||||
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user