-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add updateAttributes() command, deprecate updateNodeAttributes(),
fix #254
- Loading branch information
1 parent
e61b2a5
commit aac32b4
Showing
5 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { NodeType, MarkType } from 'prosemirror-model' | ||
import getNodeType from '../helpers/getNodeType' | ||
import getMarkType from '../helpers/getMarkType' | ||
import getSchemaTypeNameByName from '../helpers/getSchemaTypeNameByName' | ||
import { AnyObject, Command, RawCommands } from '../types' | ||
|
||
declare module '@tiptap/core' { | ||
interface Commands { | ||
updateAttributes: { | ||
/** | ||
* Update attributes of a node or mark. | ||
*/ | ||
updateAttributes: (typeOrName: string | NodeType | MarkType, attributes: AnyObject) => Command, | ||
} | ||
} | ||
} | ||
|
||
export const updateAttributes: RawCommands['updateAttributes'] = (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, { | ||
...node.attrs, | ||
...attributes, | ||
}) | ||
} | ||
|
||
if (markType && node.marks.length) { | ||
node.marks.forEach(mark => { | ||
if (markType === mark.type) { | ||
tr.addMark(pos, pos + node.nodeSize, markType.create({ | ||
...mark.attrs, | ||
...attributes, | ||
})) | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters