add floating menu to vue 3 and react
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useEditor, EditorContent, BubbleMenu } from '@tiptap/react'
|
import { useEditor, EditorContent, FloatingMenu } from '@tiptap/react'
|
||||||
import { defaultExtensions } from '@tiptap/starter-kit'
|
import { defaultExtensions } from '@tiptap/starter-kit'
|
||||||
import './styles.scss'
|
import './styles.scss'
|
||||||
|
|
||||||
@@ -10,33 +10,39 @@ export default () => {
|
|||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>
|
<p>
|
||||||
Hey, try to select some text here. There will popup a menu for selecting some inline styles. Remember: you have full control about content and styling of this menu.
|
This is an example of a medium-like editor. Enter a new line and some buttons will appear.
|
||||||
</p>
|
</p>
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ position: 'relative' }}>
|
<div style={{ position: 'relative' }}>
|
||||||
{editor && <BubbleMenu editor={editor}>
|
{editor && <FloatingMenu editor={editor}>
|
||||||
<button
|
<button
|
||||||
onClick={() => editor.chain().focus().toggleBold().run()}
|
onClick={() => editor.chain().focus().toggleHeading({ level: 1 }).run()}
|
||||||
className={editor.isActive('bold') ? 'is-active' : ''}
|
className={editor.isActive('heading', { level: 1 }) ? 'is-active' : ''}
|
||||||
>
|
>
|
||||||
bold
|
h1
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => editor.chain().focus().toggleItalic().run()}
|
onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
|
||||||
className={editor.isActive('italic') ? 'is-active' : ''}
|
className={editor.isActive('heading', { level: 2 }) ? 'is-active' : ''}
|
||||||
>
|
>
|
||||||
italic
|
h2
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => editor.chain().focus().toggleCode().run()}
|
onClick={() => editor.chain().focus().toggleBulletList().run()}
|
||||||
className={editor.isActive('code') ? 'is-active' : ''}
|
className={editor.isActive('bulletList') ? 'is-active' : ''}
|
||||||
>
|
>
|
||||||
code
|
bullet list
|
||||||
</button>
|
</button>
|
||||||
</BubbleMenu>}
|
<button
|
||||||
|
onClick={() => editor.chain().focus().toggleBlockquote().run()}
|
||||||
|
className={editor.isActive('blockquote') ? 'is-active' : ''}
|
||||||
|
>
|
||||||
|
blockquote
|
||||||
|
</button>
|
||||||
|
</FloatingMenu>}
|
||||||
<EditorContent editor={editor} />
|
<EditorContent editor={editor} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tiptap/extension-bubble-menu": "^2.0.0-beta.3",
|
"@tiptap/extension-bubble-menu": "^2.0.0-beta.3",
|
||||||
|
"@tiptap/extension-floating-menu": "^2.0.0-beta.0",
|
||||||
"prosemirror-view": "^1.18.2"
|
"prosemirror-view": "^1.18.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
29
packages/react/src/FloatingMenu.tsx
Normal file
29
packages/react/src/FloatingMenu.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import React, { useEffect, useRef } from 'react'
|
||||||
|
import { FloatingMenuPlugin, FloatingMenuPluginKey, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
|
||||||
|
|
||||||
|
export type FloatingMenuProps = Omit<FloatingMenuPluginProps, 'element'> & {
|
||||||
|
className?: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
|
||||||
|
const element = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const { editor } = props
|
||||||
|
|
||||||
|
editor.registerPlugin(FloatingMenuPlugin({
|
||||||
|
editor,
|
||||||
|
element: element.current as HTMLElement,
|
||||||
|
}))
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
editor.unregisterPlugin(FloatingMenuPluginKey)
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={element} className={props.className}>
|
||||||
|
{props.children}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
export * from '@tiptap/core'
|
export * from '@tiptap/core'
|
||||||
export * from './BubbleMenu'
|
export * from './BubbleMenu'
|
||||||
export { Editor } from './Editor'
|
export { Editor } from './Editor'
|
||||||
|
export * from './FloatingMenu'
|
||||||
export * from './useEditor'
|
export * from './useEditor'
|
||||||
export * from './ReactRenderer'
|
export * from './ReactRenderer'
|
||||||
export * from './ReactNodeViewRenderer'
|
export * from './ReactNodeViewRenderer'
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tiptap/extension-bubble-menu": "^2.0.0-beta.3",
|
"@tiptap/extension-bubble-menu": "^2.0.0-beta.3",
|
||||||
|
"@tiptap/extension-floating-menu": "^2.0.0-beta.0",
|
||||||
"prosemirror-view": "^1.18.2"
|
"prosemirror-view": "^1.18.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tiptap/extension-bubble-menu": "^2.0.0-beta.3",
|
"@tiptap/extension-bubble-menu": "^2.0.0-beta.3",
|
||||||
|
"@tiptap/extension-floating-menu": "^2.0.0-beta.0",
|
||||||
"prosemirror-state": "^1.3.4",
|
"prosemirror-state": "^1.3.4",
|
||||||
"prosemirror-view": "^1.18.2",
|
"prosemirror-view": "^1.18.2",
|
||||||
"vue": "^3.0.0"
|
"vue": "^3.0.0"
|
||||||
|
|||||||
41
packages/vue-3/src/FloatingMenu.ts
Normal file
41
packages/vue-3/src/FloatingMenu.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import {
|
||||||
|
h,
|
||||||
|
ref,
|
||||||
|
PropType,
|
||||||
|
onMounted,
|
||||||
|
onBeforeUnmount,
|
||||||
|
defineComponent,
|
||||||
|
} from 'vue'
|
||||||
|
import {
|
||||||
|
FloatingMenuPlugin,
|
||||||
|
FloatingMenuPluginKey,
|
||||||
|
FloatingMenuPluginProps,
|
||||||
|
} from '@tiptap/extension-floating-menu'
|
||||||
|
|
||||||
|
export const FloatingMenu = defineComponent({
|
||||||
|
name: 'FloatingMenu',
|
||||||
|
|
||||||
|
props: {
|
||||||
|
editor: {
|
||||||
|
type: Object as PropType<FloatingMenuPluginProps['editor']>,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
setup({ editor }, { slots }) {
|
||||||
|
const root = ref<HTMLElement | null>(null)
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
editor.registerPlugin(FloatingMenuPlugin({
|
||||||
|
editor,
|
||||||
|
element: root.value as HTMLElement,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
editor.unregisterPlugin(FloatingMenuPluginKey)
|
||||||
|
})
|
||||||
|
|
||||||
|
return () => h('div', { ref: root }, slots.default?.())
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -2,6 +2,7 @@ export * from '@tiptap/core'
|
|||||||
export * from './BubbleMenu'
|
export * from './BubbleMenu'
|
||||||
export { Editor } from './Editor'
|
export { Editor } from './Editor'
|
||||||
export * from './EditorContent'
|
export * from './EditorContent'
|
||||||
|
export * from './FloatingMenu'
|
||||||
export * from './useEditor'
|
export * from './useEditor'
|
||||||
export * from './VueRenderer'
|
export * from './VueRenderer'
|
||||||
export * from './VueNodeViewRenderer'
|
export * from './VueNodeViewRenderer'
|
||||||
|
|||||||
Reference in New Issue
Block a user