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
|
|
|
|
|
|
|
interface BubbleMenuPluginOptions {
|
|
|
|
editor: Editor;
|
|
|
|
element: HTMLElement;
|
|
|
|
keepInBounds: boolean;
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:13:55 +08:00
|
|
|
class BubbleMenuView {
|
2021-03-30 17:36:51 +08:00
|
|
|
public options: BubbleMenuPluginOptions;
|
|
|
|
|
|
|
|
public editorView: EditorView;
|
|
|
|
|
|
|
|
public isActive = false;
|
|
|
|
|
|
|
|
public left = 0;
|
|
|
|
|
|
|
|
public bottom = 0;
|
|
|
|
|
|
|
|
public top = 0;
|
|
|
|
|
|
|
|
public preventHide = false;
|
|
|
|
|
|
|
|
constructor({
|
|
|
|
options,
|
|
|
|
editorView,
|
|
|
|
}: {
|
|
|
|
options: BubbleMenuPluginOptions;
|
|
|
|
editorView: EditorView;
|
|
|
|
}) {
|
|
|
|
this.options = {
|
|
|
|
...{
|
|
|
|
element: null,
|
|
|
|
keepInBounds: true,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
}
|
2021-03-30 18:13:55 +08:00
|
|
|
|
2021-03-30 17:36:51 +08:00
|
|
|
this.editorView = editorView
|
2021-03-30 18:13:55 +08:00
|
|
|
|
|
|
|
this.render()
|
|
|
|
// this.options.element.addEventListener('mousedown', this.mousedownHandler, {
|
|
|
|
// capture: true,
|
|
|
|
// })
|
|
|
|
// this.options.editor.on('focus', this.focusHandler)
|
|
|
|
// this.options.editor.on('blur', this.blurHandler)
|
2021-03-30 17:36:51 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:13:55 +08:00
|
|
|
// mousedownHandler = () => {
|
|
|
|
// this.preventHide = true
|
|
|
|
// };
|
2021-03-30 17:36:51 +08:00
|
|
|
|
2021-03-30 18:13:55 +08:00
|
|
|
// focusHandler = () => {
|
|
|
|
// this.update(this.options.editor.view)
|
|
|
|
// };
|
2021-03-30 17:36:51 +08:00
|
|
|
|
2021-03-30 18:13:55 +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:13:55 +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)
|
|
|
|
const parent = this.options.element.offsetParent
|
|
|
|
|
|
|
|
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()
|
|
|
|
const box = this.options.element.getBoundingClientRect()
|
|
|
|
const left = (start.left + end.left) / 2 - parentBox.left
|
2021-03-30 17:36:51 +08:00
|
|
|
|
|
|
|
this.left = Math.round(
|
|
|
|
this.options.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() {
|
|
|
|
Object.assign(this.options.element.style, {
|
|
|
|
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 17:36:51 +08:00
|
|
|
&& this.options.element.parentNode
|
|
|
|
&& this.options.element.parentNode.contains(event.relatedTarget as Node)
|
|
|
|
) {
|
|
|
|
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:13:55 +08:00
|
|
|
// this.options.element.removeEventListener(
|
|
|
|
// 'mousedown',
|
|
|
|
// this.mousedownHandler,
|
|
|
|
// )
|
|
|
|
// this.options.editor.off('focus', this.focusHandler)
|
|
|
|
// this.options.editor.off('blur', this.blurHandler)
|
2021-03-30 17:36:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const BubbleMenuPlugin = (options: BubbleMenuPluginOptions) => {
|
|
|
|
return new Plugin({
|
|
|
|
key: new PluginKey('menu_bubble'),
|
|
|
|
view(editorView) {
|
2021-03-30 18:13:55 +08:00
|
|
|
return new BubbleMenuView({ editorView, options })
|
2021-03-30 17:36:51 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export { BubbleMenuPlugin }
|