add removeMarks command

This commit is contained in:
Philipp Kühn
2020-04-10 22:34:49 +02:00
parent bd4fe7e5e3
commit 42a8f46011
3 changed files with 32 additions and 0 deletions

View File

@@ -66,6 +66,7 @@ export class Editor extends EventEmitter {
this.registerCommand('insertHTML', require('./commands/insertHTML').default) this.registerCommand('insertHTML', require('./commands/insertHTML').default)
this.registerCommand('setContent', require('./commands/setContent').default) this.registerCommand('setContent', require('./commands/setContent').default)
this.registerCommand('clearContent', require('./commands/clearContent').default) this.registerCommand('clearContent', require('./commands/clearContent').default)
this.registerCommand('removeMarks', require('./commands/removeMarks').default)
if (this.options.injectCSS) { if (this.options.injectCSS) {
this.css = injectCSS(require('./style.css')) this.css = injectCSS(require('./style.css'))

View File

@@ -0,0 +1,28 @@
import { Editor } from '../Editor'
declare module '../Editor' {
interface Editor {
removeMarks(): Editor,
}
}
export default function removeMarks(next: Function, editor: Editor): void {
const { state, view, schema } = editor
const { selection, tr } = state
const { from, to, empty } = selection
let transaction = tr
if (empty) {
next()
return
}
Object
.entries(schema.marks)
.forEach(([name, mark]) => {
transaction.removeMark(from, to, mark)
})
view.dispatch(transaction)
next()
}

View File

@@ -1,6 +1,9 @@
<template> <template>
<div> <div>
<div v-if="editor"> <div v-if="editor">
<button @click="editor.focus().removeMarks()">
clear formatting
</button>
<button @click="editor.focus().undo()"> <button @click="editor.focus().undo()">
undo undo
</button> </button>