2020-11-18 04:38:15 +08:00
|
|
|
import { MarkType } from 'prosemirror-model'
|
|
|
|
import { Command } from '../types'
|
|
|
|
import getMarkType from '../utils/getMarkType'
|
2020-11-18 18:05:19 +08:00
|
|
|
import getMarkAttributes from '../utils/getMarkAttributes'
|
2020-11-18 04:38:15 +08:00
|
|
|
|
2020-11-18 23:43:27 +08:00
|
|
|
/**
|
|
|
|
* Add a mark with new attributes.
|
|
|
|
*/
|
2020-11-19 00:36:00 +08:00
|
|
|
export const setMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => {
|
2020-11-18 04:38:15 +08:00
|
|
|
const { selection } = tr
|
2020-11-18 18:05:19 +08:00
|
|
|
const { from, to, empty } = selection
|
2020-11-18 04:38:15 +08:00
|
|
|
const type = getMarkType(typeOrName, state.schema)
|
2020-11-18 18:05:19 +08:00
|
|
|
const oldAttributes = getMarkAttributes(state, type)
|
|
|
|
const newAttributes = {
|
|
|
|
...oldAttributes,
|
|
|
|
...attributes,
|
|
|
|
}
|
2020-11-18 04:38:15 +08:00
|
|
|
|
|
|
|
if (dispatch) {
|
2020-11-18 18:05:19 +08:00
|
|
|
if (empty) {
|
|
|
|
tr.addStoredMark(type.create(newAttributes))
|
|
|
|
} else {
|
|
|
|
tr.addMark(from, to, type.create(newAttributes))
|
|
|
|
}
|
2020-11-18 04:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|