mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 19:59:02 +08:00
add clearNodes command
This commit is contained in:
parent
3dc25640c9
commit
85c1a8ace9
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 { ClearContent } from './clearContent'
|
||||
export { ClearNodes } from './clearNodes'
|
||||
export { DeleteSelection } from './deleteSelection'
|
||||
export { Focus } from './focus'
|
||||
export { InsertHTML } from './insertHTML'
|
||||
|
@ -44,7 +44,8 @@ export const ToggleList = createExtension({
|
||||
// try to convert node to paragraph if needed
|
||||
if (!canWrapInList) {
|
||||
return chain()
|
||||
.setBlockType('paragraph')
|
||||
// .setBlockType('paragraph')
|
||||
.clearNodes()
|
||||
.wrapInList(listType)
|
||||
.run()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user