add isActive function
This commit is contained in:
@@ -13,6 +13,7 @@ export default {
|
||||
focused: this.editor.view.focused,
|
||||
focus: this.editor.focus,
|
||||
commands: this.editor.commands,
|
||||
isActive: this.editor.isActive.bind(this.editor),
|
||||
}))
|
||||
}
|
||||
},
|
||||
|
||||
@@ -6,9 +6,9 @@ import { gapCursor } from 'prosemirror-gapcursor'
|
||||
import { keymap } from 'prosemirror-keymap'
|
||||
import { baseKeymap } from 'prosemirror-commands'
|
||||
import { inputRules } from 'prosemirror-inputrules'
|
||||
import { markIsActive, nodeIsActive, getMarkAttrs } from 'tiptap-utils'
|
||||
|
||||
import {
|
||||
buildMenuActions,
|
||||
ExtensionManager,
|
||||
initNodeViews,
|
||||
// menuBubble,
|
||||
@@ -52,9 +52,7 @@ export default class Editor {
|
||||
this.state = this.createState()
|
||||
this.view = this.createView()
|
||||
this.commands = this.createCommands()
|
||||
|
||||
this.updateMenuActions()
|
||||
|
||||
this.getActiveNodesAndMarks()
|
||||
this.emit('init')
|
||||
}
|
||||
|
||||
@@ -178,7 +176,7 @@ export default class Editor {
|
||||
dispatchTransaction(transaction) {
|
||||
this.state = this.state.apply(transaction)
|
||||
this.view.updateState(this.state)
|
||||
this.updateMenuActions()
|
||||
this.getActiveNodesAndMarks()
|
||||
|
||||
if (!transaction.docChanged) {
|
||||
return
|
||||
@@ -233,13 +231,20 @@ export default class Editor {
|
||||
}, emitUpdate)
|
||||
}
|
||||
|
||||
updateMenuActions() {
|
||||
this.menuActions = buildMenuActions({
|
||||
editable: this.options.editable,
|
||||
schema: this.schema,
|
||||
state: this.view.state,
|
||||
commands: this.commands,
|
||||
})
|
||||
getActiveNodesAndMarks() {
|
||||
this.activeMarks = Object
|
||||
.entries(this.schema.marks)
|
||||
.reduce((marks, [name, mark]) => ({
|
||||
...marks,
|
||||
[name]: (attrs = {}) => markIsActive(this.state, mark, attrs),
|
||||
}), {})
|
||||
|
||||
this.activeNodes = Object
|
||||
.entries(this.schema.nodes)
|
||||
.reduce((nodes, [name, node]) => ({
|
||||
...nodes,
|
||||
[name]: (attrs = {}) => nodeIsActive(this.state, node, attrs),
|
||||
}), {})
|
||||
}
|
||||
|
||||
focus() {
|
||||
@@ -263,6 +268,19 @@ export default class Editor {
|
||||
}
|
||||
}
|
||||
|
||||
isActive(type = null, attrs = {}) {
|
||||
const types = {
|
||||
...this.activeMarks,
|
||||
...this.activeNodes,
|
||||
}
|
||||
|
||||
if (!types[type]) {
|
||||
return false
|
||||
}
|
||||
|
||||
return types[type](attrs)
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.emit('destroy')
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { markIsActive, nodeIsActive, getMarkAttrs } from 'tiptap-utils'
|
||||
|
||||
export default function ({ schema, state, commands, editable }) {
|
||||
export default function ({
|
||||
schema, state, commands, editable,
|
||||
}) {
|
||||
|
||||
const nodes = Object.entries(schema.nodes)
|
||||
.map(([name]) => {
|
||||
@@ -10,7 +12,7 @@ export default function ({ schema, state, commands, editable }) {
|
||||
return {
|
||||
name,
|
||||
active,
|
||||
command: editable ? command : () => {}
|
||||
command: editable ? command : () => {},
|
||||
}
|
||||
})
|
||||
.reduce((actions, { name, active, command }) => ({
|
||||
@@ -31,7 +33,7 @@ export default function ({ schema, state, commands, editable }) {
|
||||
name,
|
||||
active,
|
||||
attrs,
|
||||
command: editable ? command : () => {}
|
||||
command: editable ? command : () => {},
|
||||
}
|
||||
})
|
||||
.reduce((actions, {
|
||||
|
||||
Reference in New Issue
Block a user