add updateMark command
This commit is contained in:
@@ -46,4 +46,8 @@ Toggle a mark on and off.
|
||||
|
||||
## .toggleNode()
|
||||
|
||||
Toggle a node with another node.
|
||||
Toggle a node with another node.
|
||||
|
||||
## .updateMark()
|
||||
|
||||
Update a mark with new attributes.
|
||||
@@ -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')
|
||||
|
||||
41
packages/core/src/commands/updateMark.ts
Normal file
41
packages/core/src/commands/updateMark.ts
Normal file
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user