feat: use tippy for floating menu
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"prosemirror-state": "^1.3.4",
|
"prosemirror-state": "^1.3.4",
|
||||||
"prosemirror-view": "^1.18.2"
|
"prosemirror-view": "^1.18.2",
|
||||||
|
"tippy.js": "^6.3.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Editor, isNodeEmpty } from '@tiptap/core'
|
import { Editor, isNodeEmpty, posToClientRect } from '@tiptap/core'
|
||||||
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
|
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
|
||||||
import { EditorView } from 'prosemirror-view'
|
import { EditorView } from 'prosemirror-view'
|
||||||
|
import tippy from 'tippy.js'
|
||||||
|
|
||||||
export interface FloatingMenuPluginProps {
|
export interface FloatingMenuPluginProps {
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
@@ -18,27 +19,19 @@ export class FloatingMenuView {
|
|||||||
|
|
||||||
public view: EditorView
|
public view: EditorView
|
||||||
|
|
||||||
public isActive = false
|
|
||||||
|
|
||||||
public top = 0
|
|
||||||
|
|
||||||
public left = 0
|
|
||||||
|
|
||||||
public preventHide = false
|
public preventHide = false
|
||||||
|
|
||||||
constructor({
|
public tippy: any = null
|
||||||
editor,
|
|
||||||
element,
|
constructor({ editor, element, view }: FloatingMenuViewProps) {
|
||||||
view,
|
|
||||||
}: FloatingMenuViewProps) {
|
|
||||||
this.editor = editor
|
this.editor = editor
|
||||||
this.element = element
|
this.element = element
|
||||||
this.view = view
|
this.view = view
|
||||||
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
|
// this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
|
||||||
this.editor.on('focus', this.focusHandler)
|
// this.editor.on('focus', this.focusHandler)
|
||||||
this.editor.on('blur', this.blurHandler)
|
// this.editor.on('blur', this.blurHandler)
|
||||||
this.editor.on('resize', this.resizeHandler)
|
this.createTooltip()
|
||||||
this.render()
|
this.element.style.visibility = 'visible'
|
||||||
}
|
}
|
||||||
|
|
||||||
mousedownHandler = () => {
|
mousedownHandler = () => {
|
||||||
@@ -50,13 +43,8 @@ export class FloatingMenuView {
|
|||||||
setTimeout(() => this.update(this.editor.view))
|
setTimeout(() => this.update(this.editor.view))
|
||||||
}
|
}
|
||||||
|
|
||||||
resizeHandler = () => {
|
|
||||||
if (this.isActive) {
|
|
||||||
this.update(this.editor.view)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
blurHandler = ({ event }: { event: FocusEvent }) => {
|
blurHandler = ({ event }: { event: FocusEvent }) => {
|
||||||
|
console.log('blur')
|
||||||
if (this.preventHide) {
|
if (this.preventHide) {
|
||||||
this.preventHide = false
|
this.preventHide = false
|
||||||
|
|
||||||
@@ -73,6 +61,19 @@ export class FloatingMenuView {
|
|||||||
this.hide()
|
this.hide()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createTooltip() {
|
||||||
|
this.tippy = tippy('body', {
|
||||||
|
duration: 0,
|
||||||
|
getReferenceClientRect: null,
|
||||||
|
appendTo: () => document.body,
|
||||||
|
content: this.element,
|
||||||
|
interactive: true,
|
||||||
|
trigger: 'manual',
|
||||||
|
placement: 'right',
|
||||||
|
// hideOnClick: 'toggle',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
update(view: EditorView, oldState?: EditorState) {
|
update(view: EditorView, oldState?: EditorState) {
|
||||||
const { state, composing } = view
|
const { state, composing } = view
|
||||||
const { doc, selection } = state
|
const { doc, selection } = state
|
||||||
@@ -82,52 +83,43 @@ export class FloatingMenuView {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { $anchor, anchor, empty } = selection
|
const {
|
||||||
const parent = this.element.offsetParent
|
$anchor,
|
||||||
|
empty,
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
} = selection
|
||||||
const isRootDepth = $anchor.depth === 1
|
const isRootDepth = $anchor.depth === 1
|
||||||
const isDefaultNodeType = $anchor.parent.type === state.doc.type.contentMatch.defaultType
|
const isDefaultNodeType = $anchor.parent.type === state.doc.type.contentMatch.defaultType
|
||||||
const isDefaultNodeEmpty = isNodeEmpty(selection.$anchor.parent)
|
const isDefaultNodeEmpty = isNodeEmpty(selection.$anchor.parent)
|
||||||
const isActive = isRootDepth && isDefaultNodeType && isDefaultNodeEmpty
|
const isActive = isRootDepth && isDefaultNodeType && isDefaultNodeEmpty
|
||||||
|
|
||||||
if (!empty || !parent || !isActive) {
|
if (!empty || !isActive) {
|
||||||
this.hide()
|
this.hide()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentBox = parent.getBoundingClientRect()
|
this.tippy[0].setProps({
|
||||||
const cursorCoords = view.coordsAtPos(anchor)
|
getReferenceClientRect: () => posToClientRect(view, from, to),
|
||||||
const top = cursorCoords.top - parentBox.top
|
})
|
||||||
const left = cursorCoords.left - parentBox.left
|
|
||||||
|
|
||||||
this.isActive = true
|
this.show()
|
||||||
this.top = top
|
|
||||||
this.left = left
|
|
||||||
|
|
||||||
this.render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
show() {
|
||||||
Object.assign(this.element.style, {
|
this.tippy[0].show()
|
||||||
position: 'absolute',
|
|
||||||
zIndex: 1,
|
|
||||||
visibility: this.isActive ? 'visible' : 'hidden',
|
|
||||||
opacity: this.isActive ? 1 : 0,
|
|
||||||
left: `${this.left}px`,
|
|
||||||
top: `${this.top}px`,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hide() {
|
hide() {
|
||||||
this.isActive = false
|
this.tippy[0].hide()
|
||||||
this.render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
this.tippy[0].destroy()
|
||||||
this.element.removeEventListener('mousedown', this.mousedownHandler)
|
this.element.removeEventListener('mousedown', this.mousedownHandler)
|
||||||
this.editor.off('focus', this.focusHandler)
|
this.editor.off('focus', this.focusHandler)
|
||||||
this.editor.off('blur', this.blurHandler)
|
this.editor.off('blur', this.blurHandler)
|
||||||
this.editor.off('resize', this.resizeHandler)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user