change isActive

This commit is contained in:
Philipp Kühn
2020-03-31 12:53:52 +02:00
parent 13cb03f93b
commit 6f3d3901ce
3 changed files with 27 additions and 29 deletions

View File

@@ -16,6 +16,7 @@ import getAllMethodNames from './utils/getAllMethodNames'
import nodeIsActive from './utils/nodeIsActive' import nodeIsActive from './utils/nodeIsActive'
import markIsActive from './utils/markIsActive' import markIsActive from './utils/markIsActive'
import getMarkAttrs from './utils/getMarkAttrs' import getMarkAttrs from './utils/getMarkAttrs'
import getSchemaTypeByName from './utils/getSchemaTypeByName'
import ExtensionManager from './ExtensionManager' import ExtensionManager from './ExtensionManager'
import Extension from './Extension' import Extension from './Extension'
import Node from './Node' import Node from './Node'
@@ -57,7 +58,6 @@ export class Editor extends EventEmitter {
this.createExtensionManager() this.createExtensionManager()
this.createSchema() this.createSchema()
this.createView() this.createView()
this.setActiveNodesAndMarks()
this.registerCommand('focus', require('./commands/focus').default) this.registerCommand('focus', require('./commands/focus').default)
this.registerCommand('insertText', require('./commands/insertText').default) this.registerCommand('insertText', require('./commands/insertText').default)
this.registerCommand('insertHTML', require('./commands/insertHTML').default) this.registerCommand('insertHTML', require('./commands/insertHTML').default)
@@ -172,7 +172,6 @@ export class Editor extends EventEmitter {
const state = this.state.apply(transaction) const state = this.state.apply(transaction)
this.view.updateState(state) this.view.updateState(state)
this.storeSelection() this.storeSelection()
this.setActiveNodesAndMarks()
this.emit('transaction', { transaction }) this.emit('transaction', { transaction })
if (!transaction.docChanged || transaction.getMeta('preventUpdate')) { if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
@@ -182,34 +181,20 @@ export class Editor extends EventEmitter {
this.emit('update', { transaction }) this.emit('update', { transaction })
} }
setActiveNodesAndMarks() {
this.activeMarks = collect(this.schema.marks)
.mapWithKeys((mark: any) => [mark.name, () => markIsActive(this.state, mark)])
.all()
this.activeMarkAttrs = collect(this.schema.marks)
.mapWithKeys((mark: any) => [mark.name, () => getMarkAttrs(this.state, mark)])
.all()
this.activeNodes = collect(this.schema.nodes)
.mapWithKeys((node: any) => [node.name, (attrs = {}) => nodeIsActive(this.state, node, attrs)])
.all()
}
getMarkAttrs(name: string) { getMarkAttrs(name: string) {
return this.activeMarkAttrs[name] return getMarkAttrs(this.state, this.schema.marks[name])
} }
get isActive() { isActive(name: string, attrs = {}) {
return Object const schemaType = getSchemaTypeByName(name, this.schema)
.entries({
...this.activeMarks, if (schemaType === 'node') {
...this.activeNodes, return nodeIsActive(this.state, this.schema.nodes[name], attrs)
}) } else if (schemaType === 'mark') {
.reduce((types, [name, value]) => ({ return markIsActive(this.state, this.schema.marks[name])
...types, }
[name]: (attrs = {}) => value(attrs),
}), {}) return false
} }
// public setParentComponent(component = null) { // public setParentComponent(component = null) {

View File

@@ -0,0 +1,13 @@
import { Schema } from 'prosemirror-model'
export default function getSchemaTypeByName(name: string, schema: Schema) {
if (schema.nodes[name]) {
return 'node'
}
if (schema.marks[name]) {
return 'mark'
}
return null
}

View File

@@ -7,10 +7,10 @@
<button @click="editor.redo().focus()"> <button @click="editor.redo().focus()">
redo redo
</button> </button>
<button @click="editor.bold().focus()" :class="{ 'is-active': editor.isActive.bold() }"> <button @click="editor.bold().focus()" :class="{ 'is-active': editor.isActive('bold') }">
bold bold
</button> </button>
<button @click="editor.italic().focus()" :class="{ 'is-active': editor.isActive.italic() }"> <button @click="editor.italic().focus()" :class="{ 'is-active': editor.isActive('italic') }">
italic italic
</button> </button>
</div> </div>