fix more commands for cell selections

This commit is contained in:
Philipp Kühn
2021-03-28 21:30:47 +02:00
parent ffde0a382e
commit 4717d7ff9a
4 changed files with 37 additions and 29 deletions

View File

@@ -14,26 +14,28 @@ declare module '@tiptap/core' {
export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatch }) => { export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatch }) => {
const { selection } = tr const { selection } = tr
const { from, to } = selection const { ranges } = selection
state.doc.nodesBetween(from, to, (node, pos) => { ranges.forEach(range => {
if (!node.type.isText) { state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1)) if (!node.type.isText) {
const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1)) const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1))
const nodeRange = fromPos.blockRange(toPos) const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1))
const nodeRange = fromPos.blockRange(toPos)
if (nodeRange) { if (nodeRange) {
const targetLiftDepth = liftTarget(nodeRange) const targetLiftDepth = liftTarget(nodeRange)
if (node.type.isTextblock && dispatch) { if (node.type.isTextblock && dispatch) {
tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType) tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType)
} }
if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
tr.lift(nodeRange, targetLiftDepth) tr.lift(nodeRange, targetLiftDepth)
}
} }
} }
} })
}) })
return true return true

View File

@@ -17,12 +17,14 @@ declare module '@tiptap/core' {
export const resetNodeAttributes: RawCommands['resetNodeAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => { export const resetNodeAttributes: RawCommands['resetNodeAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const { selection } = tr const { selection } = tr
const { from, to } = selection const { ranges } = selection
state.doc.nodesBetween(from, to, (node, pos) => { ranges.forEach(range => {
if (node.type === type && dispatch) { state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes)) if (node.type === type && dispatch) {
} tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes))
}
})
}) })
return true return true

View File

@@ -13,7 +13,7 @@ declare module '@tiptap/core' {
export const unsetAllMarks: RawCommands['unsetAllMarks'] = () => ({ tr, state, dispatch }) => { export const unsetAllMarks: RawCommands['unsetAllMarks'] = () => ({ tr, state, dispatch }) => {
const { selection } = tr const { selection } = tr
const { from, to, empty } = selection const { empty, ranges } = selection
if (empty) { if (empty) {
return true return true
@@ -23,7 +23,9 @@ export const unsetAllMarks: RawCommands['unsetAllMarks'] = () => ({ tr, state, d
Object Object
.entries(state.schema.marks) .entries(state.schema.marks)
.forEach(([, mark]) => { .forEach(([, mark]) => {
tr.removeMark(from, to, mark as any) ranges.forEach(range => {
tr.removeMark(range.$from.pos, range.$to.pos, mark as any)
})
}) })
} }

View File

@@ -16,15 +16,17 @@ declare module '@tiptap/core' {
export const updateNodeAttributes: RawCommands['updateNodeAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => { export const updateNodeAttributes: RawCommands['updateNodeAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const { selection } = tr const { selection } = tr
const { from, to } = selection const { ranges } = selection
state.doc.nodesBetween(from, to, (node, pos) => { ranges.forEach(range => {
if (node.type === type && dispatch) { state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
tr.setNodeMarkup(pos, undefined, { if (node.type === type && dispatch) {
...node.attrs, tr.setNodeMarkup(pos, undefined, {
...attributes, ...node.attrs,
}) ...attributes,
} })
}
})
}) })
return true return true