diff --git a/docs/src/data/posts/commands.md b/docs/src/data/posts/commands.md index 118d5ceb..8e6f27da 100644 --- a/docs/src/data/posts/commands.md +++ b/docs/src/data/posts/commands.md @@ -46,4 +46,8 @@ Toggle a mark on and off. ## .toggleNode() -Toggle a node with another node. \ No newline at end of file +Toggle a node with another node. + +## .updateMark() + +Update a mark with new attributes. \ No newline at end of file diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 66b0364a..8b7a0b3f 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -77,6 +77,7 @@ export class Editor extends EventEmitter { this.registerCommand('setContent', require('./commands/setContent').default) this.registerCommand('toggleMark', require('./commands/toggleMark').default) this.registerCommand('toggleNode', require('./commands/toggleNode').default) + this.registerCommand('updateMark', require('./commands/updateMark').default) if (this.options.injectCSS) { require('./style.css') diff --git a/packages/core/src/commands/updateMark.ts b/packages/core/src/commands/updateMark.ts new file mode 100644 index 00000000..e5d81eef --- /dev/null +++ b/packages/core/src/commands/updateMark.ts @@ -0,0 +1,41 @@ +import { Editor } from '../Editor' +import { MarkType } from 'prosemirror-model' +import getMarkType from '../utils/getMarkType' +import getMarkRange from '../utils/getMarkRange' + +type UpdateMark = ( + type: string | MarkType, + attrs: {}, +) => any + +declare module '../Editor' { + interface Editor { + updateMark: UpdateMark, + } +} + +export default (next: Function, editor: Editor): UpdateMark => (typeOrName, attrs) => { + const { view, state, schema } = editor + const { tr, selection, doc } = state + let { from, to, $from, empty } = selection + const type = getMarkType(typeOrName, schema) + + if (empty) { + const range = getMarkRange($from, type) + + if (range) { + from = range.from + to = range.to + } + } + + const hasMark = doc.rangeHasMark(from, to, type) + + if (hasMark) { + tr.removeMark(from, to, type) + } + + tr.addMark(from, to, type.create(attrs)) + view.dispatch(tr) + next() +}