tiptap/packages/extension-bubble-menu/src/bubble-menu-plugin.ts

150 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-03-30 17:36:51 +08:00
import { Editor } from '@tiptap/core'
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
2021-03-30 18:13:55 +08:00
import { coordsAtPos } from './helpers'
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
export interface BubbleMenuPluginOptions {
editor: Editor,
element: HTMLElement,
keepInBounds: boolean,
2021-03-30 17:36:51 +08:00
}
2021-03-30 18:45:56 +08:00
export type BubbleMenuViewOptions = BubbleMenuPluginOptions & {
view: EditorView,
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
export class BubbleMenuView {
public editor: Editor
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public element: HTMLElement
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public keepInBounds = true
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public view: EditorView
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public isActive = false
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public left = 0
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public bottom = 0
2021-03-30 18:13:55 +08:00
2021-03-30 18:45:56 +08:00
public top = 0
2021-03-30 18:13:55 +08:00
2021-03-30 18:45:56 +08:00
public preventHide = false
constructor({
editor,
element,
keepInBounds,
view,
}: BubbleMenuViewOptions) {
this.editor = editor
this.element = element
this.keepInBounds = keepInBounds
this.view = view
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
2021-03-30 18:13:55 +08:00
this.render()
2021-03-30 17:36:51 +08:00
}
2021-03-30 18:45:56 +08:00
mousedownHandler = () => {
this.preventHide = true
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
focusHandler = () => {
this.update(this.editor.view)
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
blurHandler = ({ event }: { event: FocusEvent }) => {
if (this.preventHide) {
this.preventHide = false
return
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
this.hide(event)
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:13:55 +08:00
update(view: EditorView, oldState?: EditorState) {
const { state, composing } = view
const { doc, selection } = state
const docHasChanged = !oldState?.doc.eq(doc)
const selectionHasChanged = !oldState?.selection.eq(selection)
2021-03-30 17:36:51 +08:00
2021-03-30 18:13:55 +08:00
if (composing || (!docHasChanged && !selectionHasChanged)) {
2021-03-30 17:36:51 +08:00
return
}
2021-03-30 18:13:55 +08:00
const { from, to, empty } = selection
2021-03-30 17:36:51 +08:00
2021-03-30 18:13:55 +08:00
if (empty) {
2021-03-30 17:36:51 +08:00
this.hide()
2021-03-30 18:13:55 +08:00
2021-03-30 17:36:51 +08:00
return
}
const start = coordsAtPos(view, from)
const end = coordsAtPos(view, to, true)
2021-03-30 18:45:56 +08:00
const parent = this.element.offsetParent
2021-03-30 17:36:51 +08:00
if (!parent) {
this.hide()
2021-03-30 18:13:55 +08:00
2021-03-30 17:36:51 +08:00
return
}
2021-03-30 18:13:55 +08:00
const parentBox = parent.getBoundingClientRect()
2021-03-30 18:45:56 +08:00
const box = this.element.getBoundingClientRect()
2021-03-30 18:13:55 +08:00
const left = (start.left + end.left) / 2 - parentBox.left
2021-03-30 17:36:51 +08:00
this.left = Math.round(
2021-03-30 18:45:56 +08:00
this.keepInBounds
2021-03-30 18:13:55 +08:00
? Math.min(parentBox.width - box.width / 2, Math.max(left, box.width / 2))
2021-03-30 17:36:51 +08:00
: left,
)
2021-03-30 18:13:55 +08:00
this.bottom = Math.round(parentBox.bottom - start.top)
this.top = Math.round(end.bottom - parentBox.top)
2021-03-30 17:36:51 +08:00
this.isActive = true
2021-03-30 18:13:55 +08:00
this.render()
2021-03-30 17:36:51 +08:00
}
2021-03-30 18:13:55 +08:00
render() {
2021-03-30 18:45:56 +08:00
Object.assign(this.element.style, {
2021-03-30 18:13:55 +08:00
position: 'absolute',
zIndex: 1000,
visibility: this.isActive ? 'visible' : 'hidden',
opacity: this.isActive ? 1 : 0,
left: `${this.left}px`,
// top: `${this.top}px`,
bottom: `${this.bottom}px`,
transform: 'translateX(-50%)',
2021-03-30 17:36:51 +08:00
})
}
hide(event?: FocusEvent) {
if (
2021-03-30 18:13:55 +08:00
event?.relatedTarget
2021-03-30 18:45:56 +08:00
&& this.element.parentNode?.contains(event.relatedTarget as Node)
2021-03-30 17:36:51 +08:00
) {
return
}
this.isActive = false
2021-03-30 18:13:55 +08:00
this.render()
2021-03-30 17:36:51 +08:00
}
destroy() {
2021-03-30 18:45:56 +08:00
this.element.removeEventListener('mousedown', this.mousedownHandler)
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
2021-03-30 17:36:51 +08:00
}
}
2021-03-30 18:45:56 +08:00
export const BubbleMenuPlugin = (options: BubbleMenuPluginOptions) => {
2021-03-30 17:36:51 +08:00
return new Plugin({
2021-03-30 18:45:56 +08:00
key: new PluginKey('menuBubble'),
view: view => new BubbleMenuView({ view, ...options }),
2021-03-30 17:36:51 +08:00
})
}