mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-05 04:19:07 +08:00
add isActive function
This commit is contained in:
parent
e66218bd95
commit
253596fb35
@ -1,7 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="editor">
|
<div class="editor">
|
||||||
<menu-bar class="menubar" :editor="editor">
|
<menu-bar class="menubar" :editor="editor">
|
||||||
<template slot-scope="{ nodes, marks, commands }">
|
<template slot-scope="{ nodes, marks, commands, isActive }">
|
||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': marks.bold.active() }"
|
:class="{ 'is-active': isActive('bold') }"
|
||||||
@click="commands.bold"
|
@click="commands.bold"
|
||||||
>
|
>
|
||||||
<icon name="bold" />
|
<icon name="bold" />
|
||||||
@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': marks.italic.active() }"
|
:class="{ 'is-active': isActive('italic') }"
|
||||||
@click="commands.italic"
|
@click="commands.italic"
|
||||||
>
|
>
|
||||||
<icon name="italic" />
|
<icon name="italic" />
|
||||||
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': marks.strike.active() }"
|
:class="{ 'is-active': isActive('strike') }"
|
||||||
@click="commands.strike"
|
@click="commands.strike"
|
||||||
>
|
>
|
||||||
<icon name="strike" />
|
<icon name="strike" />
|
||||||
@ -43,7 +43,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': marks.underline.active() }"
|
:class="{ 'is-active': isActive('underline') }"
|
||||||
@click="commands.underline"
|
@click="commands.underline"
|
||||||
>
|
>
|
||||||
<icon name="underline" />
|
<icon name="underline" />
|
||||||
@ -51,7 +51,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': marks.code.active() }"
|
:class="{ 'is-active': isActive('code') }"
|
||||||
@click="commands.code"
|
@click="commands.code"
|
||||||
>
|
>
|
||||||
<icon name="code" />
|
<icon name="code" />
|
||||||
@ -59,7 +59,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.paragraph.active() }"
|
:class="{ 'is-active': isActive('paragraph') }"
|
||||||
@click="commands.paragraph"
|
@click="commands.paragraph"
|
||||||
>
|
>
|
||||||
<icon name="paragraph" />
|
<icon name="paragraph" />
|
||||||
@ -67,7 +67,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.heading.active({ level: 1 }) }"
|
:class="{ 'is-active': isActive('heading', { level: 1 }) }"
|
||||||
@click="commands.heading({ level: 1 })"
|
@click="commands.heading({ level: 1 })"
|
||||||
>
|
>
|
||||||
H1
|
H1
|
||||||
@ -75,7 +75,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.heading.active({ level: 2 }) }"
|
:class="{ 'is-active': isActive('heading', { level: 2 }) }"
|
||||||
@click="commands.heading({ level: 2 })"
|
@click="commands.heading({ level: 2 })"
|
||||||
>
|
>
|
||||||
H2
|
H2
|
||||||
@ -83,7 +83,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.heading.active({ level: 3 }) }"
|
:class="{ 'is-active': isActive('heading', { level: 3 }) }"
|
||||||
@click="commands.heading({ level: 3 })"
|
@click="commands.heading({ level: 3 })"
|
||||||
>
|
>
|
||||||
H3
|
H3
|
||||||
@ -91,7 +91,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.bullet_list.active() }"
|
:class="{ 'is-active': isActive('bullet_list') }"
|
||||||
@click="commands.bullet_list"
|
@click="commands.bullet_list"
|
||||||
>
|
>
|
||||||
<icon name="ul" />
|
<icon name="ul" />
|
||||||
@ -99,7 +99,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.ordered_list.active() }"
|
:class="{ 'is-active': isActive('ordered_list') }"
|
||||||
@click="commands.ordered_list"
|
@click="commands.ordered_list"
|
||||||
>
|
>
|
||||||
<icon name="ol" />
|
<icon name="ol" />
|
||||||
@ -107,7 +107,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.blockquote.active() }"
|
:class="{ 'is-active': isActive('blockquote') }"
|
||||||
@click="commands.blockquote"
|
@click="commands.blockquote"
|
||||||
>
|
>
|
||||||
<icon name="quote" />
|
<icon name="quote" />
|
||||||
@ -115,7 +115,7 @@
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
class="menubar__button"
|
class="menubar__button"
|
||||||
:class="{ 'is-active': nodes.code_block.active() }"
|
:class="{ 'is-active': isActive('code_block') }"
|
||||||
@click="commands.code_block"
|
@click="commands.code_block"
|
||||||
>
|
>
|
||||||
<icon name="code" />
|
<icon name="code" />
|
||||||
|
@ -13,6 +13,7 @@ export default {
|
|||||||
focused: this.editor.view.focused,
|
focused: this.editor.view.focused,
|
||||||
focus: this.editor.focus,
|
focus: this.editor.focus,
|
||||||
commands: this.editor.commands,
|
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 { keymap } from 'prosemirror-keymap'
|
||||||
import { baseKeymap } from 'prosemirror-commands'
|
import { baseKeymap } from 'prosemirror-commands'
|
||||||
import { inputRules } from 'prosemirror-inputrules'
|
import { inputRules } from 'prosemirror-inputrules'
|
||||||
|
import { markIsActive, nodeIsActive, getMarkAttrs } from 'tiptap-utils'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
buildMenuActions,
|
|
||||||
ExtensionManager,
|
ExtensionManager,
|
||||||
initNodeViews,
|
initNodeViews,
|
||||||
// menuBubble,
|
// menuBubble,
|
||||||
@ -52,9 +52,7 @@ export default class Editor {
|
|||||||
this.state = this.createState()
|
this.state = this.createState()
|
||||||
this.view = this.createView()
|
this.view = this.createView()
|
||||||
this.commands = this.createCommands()
|
this.commands = this.createCommands()
|
||||||
|
this.getActiveNodesAndMarks()
|
||||||
this.updateMenuActions()
|
|
||||||
|
|
||||||
this.emit('init')
|
this.emit('init')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +176,7 @@ export default class Editor {
|
|||||||
dispatchTransaction(transaction) {
|
dispatchTransaction(transaction) {
|
||||||
this.state = this.state.apply(transaction)
|
this.state = this.state.apply(transaction)
|
||||||
this.view.updateState(this.state)
|
this.view.updateState(this.state)
|
||||||
this.updateMenuActions()
|
this.getActiveNodesAndMarks()
|
||||||
|
|
||||||
if (!transaction.docChanged) {
|
if (!transaction.docChanged) {
|
||||||
return
|
return
|
||||||
@ -233,13 +231,20 @@ export default class Editor {
|
|||||||
}, emitUpdate)
|
}, emitUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateMenuActions() {
|
getActiveNodesAndMarks() {
|
||||||
this.menuActions = buildMenuActions({
|
this.activeMarks = Object
|
||||||
editable: this.options.editable,
|
.entries(this.schema.marks)
|
||||||
schema: this.schema,
|
.reduce((marks, [name, mark]) => ({
|
||||||
state: this.view.state,
|
...marks,
|
||||||
commands: this.commands,
|
[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() {
|
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() {
|
destroy() {
|
||||||
this.emit('destroy')
|
this.emit('destroy')
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { markIsActive, nodeIsActive, getMarkAttrs } from 'tiptap-utils'
|
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)
|
const nodes = Object.entries(schema.nodes)
|
||||||
.map(([name]) => {
|
.map(([name]) => {
|
||||||
@ -10,7 +12,7 @@ export default function ({ schema, state, commands, editable }) {
|
|||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
active,
|
active,
|
||||||
command: editable ? command : () => {}
|
command: editable ? command : () => {},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.reduce((actions, { name, active, command }) => ({
|
.reduce((actions, { name, active, command }) => ({
|
||||||
@ -31,7 +33,7 @@ export default function ({ schema, state, commands, editable }) {
|
|||||||
name,
|
name,
|
||||||
active,
|
active,
|
||||||
attrs,
|
attrs,
|
||||||
command: editable ? command : () => {}
|
command: editable ? command : () => {},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.reduce((actions, {
|
.reduce((actions, {
|
||||||
|
Loading…
Reference in New Issue
Block a user