remove updateMarkAttributes

This commit is contained in:
Philipp Kühn
2020-11-18 11:05:19 +01:00
parent 91377be21e
commit 9d99e9c9d0
7 changed files with 17 additions and 37 deletions

View File

@@ -106,6 +106,7 @@ Have a look at all of the core commands listed below. They should give you a goo
### Nodes & Marks
| Command | Description |
| ----------------------- | --------------------------------------------------------- |
| .addMark() | Add a mark with new attributes. |
| .clearNodes() | Normalize nodes to a simple paragraph. |
| .extendMarkRange() | Extends the text selection to the current mark. |
| .removeMark() | Remove a mark in the current selection. |
@@ -118,7 +119,6 @@ Have a look at all of the core commands listed below. They should give you a goo
| .toggleBlockType() | Toggle a node with another node. |
| .toggleMark() | Toggle a mark on and off. |
| .toggleWrap() | Wraps nodes in another node, or removes an existing wrap. |
| .updateMarkAttributes() | Update a mark with new attributes. |
| .updateNodeAttributes() | Update attributes of a node. |
### Lists

View File

@@ -1,14 +1,24 @@
import { MarkType } from 'prosemirror-model'
import { Command } from '../types'
import getMarkType from '../utils/getMarkType'
import getMarkAttributes from '../utils/getMarkAttributes'
export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => {
const { selection } = tr
const { from, to, empty } = selection
const type = getMarkType(typeOrName, state.schema)
const { from, to } = selection
const oldAttributes = getMarkAttributes(state, type)
const newAttributes = {
...oldAttributes,
...attributes,
}
if (dispatch) {
tr.addMark(from, to, type.create(attributes))
if (empty) {
tr.addStoredMark(type.create(newAttributes))
} else {
tr.addMark(from, to, type.create(newAttributes))
}
}
return true

View File

@@ -12,7 +12,7 @@ export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ s
&& !markIsActive(state, type, attributes)
if (attributes && hasMarkWithDifferentAttributes) {
return commands.updateMarkAttributes(type, attributes)
return commands.addMark(type, attributes)
}
return toggleMark(type, attributes)(state, dispatch)

View File

@@ -1,25 +0,0 @@
import { MarkType } from 'prosemirror-model'
import { Command } from '../types'
import getMarkType from '../utils/getMarkType'
import getMarkAttributes from '../utils/getMarkAttributes'
export default (typeOrName: string | MarkType, attributes: {}): Command => ({ tr, state, dispatch }) => {
const { selection } = tr
const { from, to, empty } = selection
const type = getMarkType(typeOrName, state.schema)
const oldAttributes = getMarkAttributes(state, type)
const newAttributes = {
...oldAttributes,
...attributes,
}
if (dispatch) {
if (empty) {
tr.addStoredMark(type.create(newAttributes))
} else {
tr.addMark(from, to, type.create(newAttributes))
}
}
return true
}

View File

@@ -26,7 +26,6 @@ import toggleList from '../commands/toggleList'
import toggleMark from '../commands/toggleMark'
import toggleWrap from '../commands/toggleWrap'
import tryCommand from '../commands/try'
import updateMarkAttributes from '../commands/updateMarkAttributes'
import updateNodeAttributes from '../commands/updateNodeAttributes'
import wrapInList from '../commands/wrapInList'
@@ -34,7 +33,7 @@ export const Commands = Extension.create({
addCommands() {
return {
/**
* Add a mark.
* Add a mark with new attributes.
*/
addMark,
/**
@@ -141,10 +140,6 @@ export const Commands = Extension.create({
* Runs one command after the other and stops at the first which returns true.
*/
try: tryCommand,
/**
* Update a mark with new attributes.
*/
updateMarkAttributes,
/**
* Update attributes of a node.
*/

View File

@@ -42,7 +42,7 @@ const FontFamily = Extension.create({
*/
fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => {
return chain()
.updateMarkAttributes('textStyle', { fontFamily })
.addMark('textStyle', { fontFamily })
.removeEmptyTextStyle()
.run()
},

View File

@@ -54,7 +54,7 @@ const Link = Mark.create({
return commands.removeMark('link')
}
return commands.updateMarkAttributes('link', options)
return commands.addMark('link', options)
},
}
},