add basic floating menu

This commit is contained in:
Philipp Kühn
2021-04-01 15:19:31 +02:00
parent 0e11fa6cff
commit 007e6f855b
10 changed files with 370 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# @tiptap/extension-floating-menu
[![Version](https://img.shields.io/npm/v/@tiptap/extension-floating-menu.svg?label=version)](https://www.npmjs.com/package/@tiptap/extension-floating-menu)
[![Downloads](https://img.shields.io/npm/dm/@tiptap/extension-floating-menu.svg)](https://npmcharts.com/compare/tiptap?minimal=true)
[![License](https://img.shields.io/npm/l/@tiptap/extension-floating-menu.svg)](https://www.npmjs.com/package/@tiptap/extension-floating-menu)
[![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/ueberdosis)
## Introduction
tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
## Offical Documentation
Documentation can be found on the [tiptap website](https://tiptap.dev).
## License
tiptap is open-sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap-next/blob/main/LICENSE.md).

View File

@@ -0,0 +1,31 @@
{
"name": "@tiptap/extension-floating-menu",
"description": "floating-menu extension for tiptap",
"version": "2.0.0-beta.0",
"homepage": "https://tiptap.dev",
"keywords": [
"tiptap",
"tiptap extension"
],
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/ueberdosis"
},
"main": "dist/tiptap-extension-floating-menu.cjs.js",
"umd": "dist/tiptap-extension-floating-menu.umd.js",
"module": "dist/tiptap-extension-floating-menu.esm.js",
"unpkg": "dist/tiptap-extension-floating-menu.bundle.umd.min.js",
"types": "dist/packages/extension-floating-menu/src/index.d.ts",
"files": [
"src",
"dist"
],
"peerDependencies": {
"@tiptap/core": "^2.0.0-beta.1"
},
"dependencies": {
"prosemirror-state": "^1.3.4",
"prosemirror-view": "^1.18.2"
}
}

View File

@@ -0,0 +1,131 @@
import { Editor } from '@tiptap/core'
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
export interface FloatingMenuPluginProps {
editor: Editor,
element: HTMLElement,
}
export type FloatingMenuViewProps = FloatingMenuPluginProps & {
view: EditorView,
}
export class FloatingMenuView {
public editor: Editor
public element: HTMLElement
public view: EditorView
public isActive = false
public top = 0
public preventHide = false
constructor({
editor,
element,
view,
}: FloatingMenuViewProps) {
this.editor = editor
this.element = element
this.view = view
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.render()
}
mousedownHandler = () => {
this.preventHide = true
}
focusHandler = () => {
// we use `setTimeout` to make sure `selection` is already updated
setTimeout(() => this.update(this.editor.view))
}
blurHandler = ({ event }: { event: FocusEvent }) => {
if (this.preventHide) {
this.preventHide = false
return
}
if (
event?.relatedTarget
&& this.element.parentNode?.contains(event.relatedTarget as Node)
) {
return
}
this.hide()
}
update(view: EditorView, oldState?: EditorState) {
const { state, composing } = view
const { doc, selection } = state
const isSame = oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection)
if (composing || isSame) {
return
}
const { anchor, empty } = selection
const parent = this.element.offsetParent
const currentDom = view.domAtPos(anchor)
const currentElement = currentDom.node as Element
const isActive = currentElement.innerHTML === '<br>'
&& currentElement.tagName === 'P'
&& currentElement.parentNode === view.dom
if (!empty || !parent || !isActive) {
this.hide()
return
}
const parentBox = parent.getBoundingClientRect()
const cursorCoords = view.coordsAtPos(anchor)
const top = cursorCoords.top - parentBox.top
this.isActive = true
this.top = top
this.render()
}
render() {
Object.assign(this.element.style, {
position: 'absolute',
zIndex: 1,
visibility: this.isActive ? 'visible' : 'hidden',
opacity: this.isActive ? 1 : 0,
// left: `${this.left}px`,
top: `${this.top}px`,
// bottom: `${this.bottom}px`,
})
}
hide() {
this.isActive = false
this.render()
}
destroy() {
this.element.removeEventListener('mousedown', this.mousedownHandler)
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
}
}
export const FloatingMenuPluginKey = new PluginKey('menuFloating')
export const FloatingMenuPlugin = (options: FloatingMenuPluginProps) => {
return new Plugin({
key: FloatingMenuPluginKey,
view: view => new FloatingMenuView({ view, ...options }),
})
}

View File

@@ -0,0 +1,27 @@
import { Extension } from '@tiptap/core'
import { FloatingMenuPlugin, FloatingMenuPluginProps } from './floating-menu-plugin'
export type FloatingMenuOptions = Omit<FloatingMenuPluginProps, 'editor' | 'element'> & {
element: HTMLElement | null,
}
export const FloatingMenu = Extension.create<FloatingMenuOptions>({
name: 'bubbleMenu',
defaultOptions: {
element: null,
},
addProseMirrorPlugins() {
if (!this.options.element) {
return []
}
return [
FloatingMenuPlugin({
editor: this.editor,
element: this.options.element,
}),
]
},
})

View File

@@ -0,0 +1,6 @@
import { FloatingMenu } from './floating-menu'
export * from './floating-menu'
export * from './floating-menu-plugin'
export default FloatingMenu