fix(extension/bubble-menu): 🐛 fix bubble menu and floating menu being available when editor not editable (#3195)
This commit is contained in:
@@ -2,7 +2,7 @@ import './styles.scss'
|
|||||||
|
|
||||||
import { BubbleMenu, EditorContent, useEditor } from '@tiptap/react'
|
import { BubbleMenu, EditorContent, useEditor } from '@tiptap/react'
|
||||||
import StarterKit from '@tiptap/starter-kit'
|
import StarterKit from '@tiptap/starter-kit'
|
||||||
import React from 'react'
|
import React, { useEffect } from 'react'
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
@@ -16,8 +16,20 @@ export default () => {
|
|||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const [isEditable, setIsEditable] = React.useState(true)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (editor) {
|
||||||
|
editor.setEditable(isEditable)
|
||||||
|
}
|
||||||
|
}, [isEditable, editor])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<div>
|
||||||
|
<input type="checkbox" checked={isEditable} onChange={() => setIsEditable(!isEditable)} />
|
||||||
|
Editable
|
||||||
|
</div>
|
||||||
{editor && <BubbleMenu editor={editor} tippyOptions={{ duration: 100 }}>
|
{editor && <BubbleMenu editor={editor} tippyOptions={{ duration: 100 }}>
|
||||||
<button
|
<button
|
||||||
onClick={() => editor.chain().focus().toggleBold().run()}
|
onClick={() => editor.chain().focus().toggleBold().run()}
|
||||||
|
|||||||
@@ -3,3 +3,7 @@
|
|||||||
margin-top: 0.75em;
|
margin-top: 0.75em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input[type="checkbox"] {
|
||||||
|
margin-right: 8px;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
|
<div>
|
||||||
|
<input type="checkbox" :checked="isEditable" @change="() => isEditable = !isEditable">
|
||||||
|
Editable
|
||||||
|
</div>
|
||||||
<bubble-menu :editor="editor" :tippy-options="{ duration: 100 }" v-if="editor">
|
<bubble-menu :editor="editor" :tippy-options="{ duration: 100 }" v-if="editor">
|
||||||
<button @click="editor.chain().focus().toggleBold().run()" :class="{ 'is-active': editor.isActive('bold') }">
|
<button @click="editor.chain().focus().toggleBold().run()" :class="{ 'is-active': editor.isActive('bold') }">
|
||||||
bold
|
bold
|
||||||
@@ -28,9 +32,16 @@ export default {
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
editor: null,
|
editor: null,
|
||||||
|
isEditable: true,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
isEditable(value) {
|
||||||
|
this.editor.setEditable(value)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
@@ -57,4 +68,8 @@ export default {
|
|||||||
margin-top: 0.75em;
|
margin-top: 0.75em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input[type="checkbox"] {
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import './styles.scss'
|
|||||||
|
|
||||||
import { EditorContent, FloatingMenu, useEditor } from '@tiptap/react'
|
import { EditorContent, FloatingMenu, useEditor } from '@tiptap/react'
|
||||||
import StarterKit from '@tiptap/starter-kit'
|
import StarterKit from '@tiptap/starter-kit'
|
||||||
import React from 'react'
|
import React, { useEffect } from 'react'
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
@@ -17,8 +17,20 @@ export default () => {
|
|||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const [isEditable, setIsEditable] = React.useState(true)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (editor) {
|
||||||
|
editor.setEditable(isEditable)
|
||||||
|
}
|
||||||
|
}, [isEditable, editor])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<div>
|
||||||
|
<input type="checkbox" checked={isEditable} onChange={() => setIsEditable(!isEditable)} />
|
||||||
|
Editable
|
||||||
|
</div>
|
||||||
{editor && <FloatingMenu editor={editor} tippyOptions={{ duration: 100 }}>
|
{editor && <FloatingMenu editor={editor} tippyOptions={{ duration: 100 }}>
|
||||||
<button
|
<button
|
||||||
onClick={() => editor.chain().focus().toggleHeading({ level: 1 }).run()}
|
onClick={() => editor.chain().focus().toggleHeading({ level: 1 }).run()}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
|
<div>
|
||||||
|
<input type="checkbox" :checked="isEditable" @change="() => isEditable = !isEditable">
|
||||||
|
Editable
|
||||||
|
</div>
|
||||||
<floating-menu :editor="editor" :tippy-options="{ duration: 100 }" v-if="editor">
|
<floating-menu :editor="editor" :tippy-options="{ duration: 100 }" v-if="editor">
|
||||||
<button @click="editor.chain().focus().toggleHeading({ level: 1 }).run()" :class="{ 'is-active': editor.isActive('heading', { level: 1 }) }">
|
<button @click="editor.chain().focus().toggleHeading({ level: 1 }).run()" :class="{ 'is-active': editor.isActive('heading', { level: 1 }) }">
|
||||||
H1
|
H1
|
||||||
@@ -28,9 +32,16 @@ export default {
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
editor: null,
|
editor: null,
|
||||||
|
isEditable: true,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
isEditable(value) {
|
||||||
|
this.editor.setEditable(value)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ export class BubbleMenuView {
|
|||||||
!hasEditorFocus
|
!hasEditorFocus
|
||||||
|| empty
|
|| empty
|
||||||
|| isEmptyTextBlock
|
|| isEmptyTextBlock
|
||||||
|
|| !this.editor.isEditable
|
||||||
) {
|
) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export class FloatingMenuView {
|
|||||||
|| !empty
|
|| !empty
|
||||||
|| !isRootDepth
|
|| !isRootDepth
|
||||||
|| !isEmptyTextBlock
|
|| !isEmptyTextBlock
|
||||||
|
|| !this.editor.isEditable
|
||||||
) {
|
) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user