feat: add resetAttributes() command, deprecate resetNodeAttributes()
This commit is contained in:
@@ -168,7 +168,7 @@ Have a look at all of the core commands listed below. They should give you a goo
|
|||||||
| .newlineInCode() | Add a newline character in code. |
|
| .newlineInCode() | Add a newline character in code. |
|
||||||
| .replace() | Replaces text with a node. |
|
| .replace() | Replaces text with a node. |
|
||||||
| .replaceRange() | Replaces text with a node within a range. |
|
| .replaceRange() | Replaces text with a node within a range. |
|
||||||
| .resetNodeAttributes() | Resets all node attributes to the default value. |
|
| .resetAttributes() | Resets some node or mark attributes to the default value. |
|
||||||
| .selectParentNode() | Select the parent node. |
|
| .selectParentNode() | Select the parent node. |
|
||||||
| .setMark() | Add a mark with new attributes. |
|
| .setMark() | Add a mark with new attributes. |
|
||||||
| .setNode() | Replace a given range with a node. |
|
| .setNode() | Replace a given range with a node. |
|
||||||
|
|||||||
61
packages/core/src/commands/resetAttributes.ts
Normal file
61
packages/core/src/commands/resetAttributes.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import { NodeType, MarkType } from 'prosemirror-model'
|
||||||
|
import getNodeType from '../helpers/getNodeType'
|
||||||
|
import getMarkType from '../helpers/getMarkType'
|
||||||
|
import getSchemaTypeNameByName from '../helpers/getSchemaTypeNameByName'
|
||||||
|
import deleteProps from '../utilities/deleteProps'
|
||||||
|
import { Command, RawCommands } from '../types'
|
||||||
|
|
||||||
|
declare module '@tiptap/core' {
|
||||||
|
interface Commands {
|
||||||
|
resetAttributes: {
|
||||||
|
/**
|
||||||
|
* Resets some node attributes to the default value.
|
||||||
|
*/
|
||||||
|
resetAttributes: (typeOrName: string | NodeType | MarkType, attributes: string | string[]) => Command,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const resetAttributes: RawCommands['resetAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
|
||||||
|
let nodeType: NodeType | null = null
|
||||||
|
let markType: MarkType | null = null
|
||||||
|
|
||||||
|
const schemaType = getSchemaTypeNameByName(
|
||||||
|
typeof typeOrName === 'string'
|
||||||
|
? typeOrName
|
||||||
|
: typeOrName.name,
|
||||||
|
state.schema,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!schemaType) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schemaType === 'node') {
|
||||||
|
nodeType = getNodeType(typeOrName as NodeType, state.schema)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schemaType === 'mark') {
|
||||||
|
markType = getMarkType(typeOrName as MarkType, state.schema)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dispatch) {
|
||||||
|
tr.selection.ranges.forEach(range => {
|
||||||
|
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
|
||||||
|
if (nodeType && nodeType === node.type) {
|
||||||
|
tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (markType && node.marks.length) {
|
||||||
|
node.marks.forEach(mark => {
|
||||||
|
if (markType === mark.type) {
|
||||||
|
tr.addMark(pos, pos + node.nodeSize, markType.create(deleteProps(mark.attrs, attributes)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -15,6 +15,8 @@ declare module '@tiptap/core' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const resetNodeAttributes: RawCommands['resetNodeAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
|
export const resetNodeAttributes: RawCommands['resetNodeAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
|
||||||
|
console.warn('[tiptap warn]: resetNodeAttributes() is deprecated. please use resetAttributes() instead.')
|
||||||
|
|
||||||
const type = getNodeType(typeOrName, state.schema)
|
const type = getNodeType(typeOrName, state.schema)
|
||||||
const { selection } = tr
|
const { selection } = tr
|
||||||
const { ranges } = selection
|
const { ranges } = selection
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import * as liftListItem from '../commands/liftListItem'
|
|||||||
import * as newlineInCode from '../commands/newlineInCode'
|
import * as newlineInCode from '../commands/newlineInCode'
|
||||||
import * as replace from '../commands/replace'
|
import * as replace from '../commands/replace'
|
||||||
import * as replaceRange from '../commands/replaceRange'
|
import * as replaceRange from '../commands/replaceRange'
|
||||||
|
import * as resetAttributes from '../commands/resetAttributes'
|
||||||
import * as resetNodeAttributes from '../commands/resetNodeAttributes'
|
import * as resetNodeAttributes from '../commands/resetNodeAttributes'
|
||||||
import * as scrollIntoView from '../commands/scrollIntoView'
|
import * as scrollIntoView from '../commands/scrollIntoView'
|
||||||
import * as selectAll from '../commands/selectAll'
|
import * as selectAll from '../commands/selectAll'
|
||||||
@@ -73,6 +74,7 @@ export { liftListItem }
|
|||||||
export { newlineInCode }
|
export { newlineInCode }
|
||||||
export { replace }
|
export { replace }
|
||||||
export { replaceRange }
|
export { replaceRange }
|
||||||
|
export { resetAttributes }
|
||||||
export { resetNodeAttributes }
|
export { resetNodeAttributes }
|
||||||
export { scrollIntoView }
|
export { scrollIntoView }
|
||||||
export { selectAll }
|
export { selectAll }
|
||||||
@@ -127,6 +129,7 @@ export const Commands = Extension.create({
|
|||||||
...newlineInCode,
|
...newlineInCode,
|
||||||
...replace,
|
...replace,
|
||||||
...replaceRange,
|
...replaceRange,
|
||||||
|
...resetAttributes,
|
||||||
...resetNodeAttributes,
|
...resetNodeAttributes,
|
||||||
...scrollIntoView,
|
...scrollIntoView,
|
||||||
...selectAll,
|
...selectAll,
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export const TextAlign = Extension.create<TextAlignOptions>({
|
|||||||
return this.options.types.every(type => commands.updateAttributes(type, { textAlign: alignment }))
|
return this.options.types.every(type => commands.updateAttributes(type, { textAlign: alignment }))
|
||||||
},
|
},
|
||||||
unsetTextAlign: () => ({ commands }) => {
|
unsetTextAlign: () => ({ commands }) => {
|
||||||
return this.options.types.every(type => commands.resetNodeAttributes(type, 'textAlign'))
|
return this.options.types.every(type => commands.resetAttributes(type, 'textAlign'))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user