Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bubble menu and floating menu being available when editor is not editable #3195

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion demos/src/Extensions/BubbleMenu/React/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './styles.scss'

import { BubbleMenu, EditorContent, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import React from 'react'
import React, { useEffect } from 'react'

export default () => {
const editor = useEditor({
Expand All @@ -16,8 +16,20 @@ export default () => {
`,
})

const [isEditable, setIsEditable] = React.useState(true)

useEffect(() => {
if (editor) {
editor.setEditable(isEditable)
}
}, [isEditable, editor])

return (
<>
<div>
<input type="checkbox" checked={isEditable} onChange={() => setIsEditable(!isEditable)} />
Editable
</div>
{editor && <BubbleMenu editor={editor} tippyOptions={{ duration: 100 }}>
<button
onClick={() => editor.chain().focus().toggleBold().run()}
Expand Down
4 changes: 4 additions & 0 deletions demos/src/Extensions/BubbleMenu/React/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
margin-top: 0.75em;
}
}

input[type="checkbox"] {
margin-right: 8px;
}
15 changes: 15 additions & 0 deletions demos/src/Extensions/BubbleMenu/Vue/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<template>
<div>
<div>
<input type="checkbox" :checked="isEditable" @change="() => isEditable = !isEditable">
Editable
</div>
<bubble-menu :editor="editor" :tippy-options="{ duration: 100 }" v-if="editor">
<button @click="editor.chain().focus().toggleBold().run()" :class="{ 'is-active': editor.isActive('bold') }">
bold
Expand Down Expand Up @@ -28,9 +32,16 @@ export default {
data() {
return {
editor: null,
isEditable: true,
}
},

watch: {
isEditable(value) {
this.editor.setEditable(value)
},
},

mounted() {
this.editor = new Editor({
extensions: [
Expand All @@ -57,4 +68,8 @@ export default {
margin-top: 0.75em;
}
}

input[type="checkbox"] {
margin-right: 4px;
}
</style>
14 changes: 13 additions & 1 deletion demos/src/Extensions/FloatingMenu/React/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './styles.scss'

import { EditorContent, FloatingMenu, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import React from 'react'
import React, { useEffect } from 'react'

export default () => {
const editor = useEditor({
Expand All @@ -17,8 +17,20 @@ export default () => {
`,
})

const [isEditable, setIsEditable] = React.useState(true)

useEffect(() => {
if (editor) {
editor.setEditable(isEditable)
}
}, [isEditable, editor])

return (
<>
<div>
<input type="checkbox" checked={isEditable} onChange={() => setIsEditable(!isEditable)} />
Editable
</div>
{editor && <FloatingMenu editor={editor} tippyOptions={{ duration: 100 }}>
<button
onClick={() => editor.chain().focus().toggleHeading({ level: 1 }).run()}
Expand Down
11 changes: 11 additions & 0 deletions demos/src/Extensions/FloatingMenu/Vue/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<template>
<div>
<div>
<input type="checkbox" :checked="isEditable" @change="() => isEditable = !isEditable">
Editable
</div>
<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 }) }">
H1
Expand Down Expand Up @@ -28,9 +32,16 @@ export default {
data() {
return {
editor: null,
isEditable: true,
}
},

watch: {
isEditable(value) {
this.editor.setEditable(value)
},
},

mounted() {
this.editor = new Editor({
extensions: [
Expand Down
1 change: 1 addition & 0 deletions packages/extension-bubble-menu/src/bubble-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class BubbleMenuView {
!hasEditorFocus
|| empty
|| isEmptyTextBlock
|| !this.editor.isEditable
) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class FloatingMenuView {
|| !empty
|| !isRootDepth
|| !isEmptyTextBlock
|| !this.editor.isEditable
) {
return false
}
Expand Down