|
| 1 | +import { BlockSchema, InlineContentSchema, StyleSchema } from "@blocknote/core"; |
| 2 | +import { UseFloatingOptions } from "@floating-ui/react"; |
| 3 | +import { FC, CSSProperties, useMemo, useRef, useState, useEffect } from "react"; |
| 4 | +import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js"; |
| 5 | +import { useUIPluginState } from "../../hooks/useUIPluginState.js"; |
| 6 | +import { FormattingToolbar } from "./FormattingToolbar.js"; |
| 7 | +import { FormattingToolbarProps } from "./FormattingToolbarProps.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Experimental formatting toolbar controller for mobile devices. |
| 11 | + * Uses Visual Viewport API to position the toolbar above the virtual keyboard. |
| 12 | + * |
| 13 | + * Currently marked experimental due to the flickering issue with positioning cause by the use of the API (and likely a delay in its updates). |
| 14 | + */ |
| 15 | +export const ExperimentalMobileFormattingToolbarController = (props: { |
| 16 | + formattingToolbar?: FC<FormattingToolbarProps>; |
| 17 | + floatingOptions?: Partial<UseFloatingOptions>; |
| 18 | +}) => { |
| 19 | + const [transform, setTransform] = useState<string>("none"); |
| 20 | + const divRef = useRef<HTMLDivElement>(null); |
| 21 | + const editor = useBlockNoteEditor< |
| 22 | + BlockSchema, |
| 23 | + InlineContentSchema, |
| 24 | + StyleSchema |
| 25 | + >(); |
| 26 | + const state = useUIPluginState( |
| 27 | + editor.formattingToolbar.onUpdate.bind(editor.formattingToolbar) |
| 28 | + ); |
| 29 | + const style = useMemo<CSSProperties>(() => { |
| 30 | + return { |
| 31 | + display: "flex", |
| 32 | + position: "fixed", |
| 33 | + bottom: 0, |
| 34 | + zIndex: 3000, |
| 35 | + transform, |
| 36 | + }; |
| 37 | + }, [transform]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + const viewport = window.visualViewport!; |
| 41 | + function viewportHandler() { |
| 42 | + // Calculate the offset necessary to set the toolbar above the virtual keyboard (using the offset info from the visualViewport) |
| 43 | + const layoutViewport = document.body; |
| 44 | + const offsetLeft = viewport.offsetLeft; |
| 45 | + const offsetTop = |
| 46 | + viewport.height - |
| 47 | + layoutViewport.getBoundingClientRect().height + |
| 48 | + viewport.offsetTop; |
| 49 | + |
| 50 | + setTransform( |
| 51 | + `translate(${offsetLeft}px, ${offsetTop}px) scale(${ |
| 52 | + 1 / viewport.scale |
| 53 | + })` |
| 54 | + ); |
| 55 | + } |
| 56 | + window.visualViewport!.addEventListener("scroll", viewportHandler); |
| 57 | + window.visualViewport!.addEventListener("resize", viewportHandler); |
| 58 | + viewportHandler(); |
| 59 | + |
| 60 | + return () => { |
| 61 | + window.visualViewport!.removeEventListener("scroll", viewportHandler); |
| 62 | + window.visualViewport!.removeEventListener("resize", viewportHandler); |
| 63 | + }; |
| 64 | + }, []); |
| 65 | + |
| 66 | + if (!state) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + if (!state.show && divRef.current) { |
| 71 | + // The component is fading out. Use the previous state to render the toolbar with innerHTML, |
| 72 | + // because otherwise the toolbar will quickly flickr (i.e.: show a different state) while fading out, |
| 73 | + // which looks weird |
| 74 | + return ( |
| 75 | + <div |
| 76 | + ref={divRef} |
| 77 | + style={style} |
| 78 | + dangerouslySetInnerHTML={{ __html: divRef.current.innerHTML }}></div> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const Component = props.formattingToolbar || FormattingToolbar; |
| 83 | + |
| 84 | + return ( |
| 85 | + <div ref={divRef} style={style}> |
| 86 | + <Component /> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
0 commit comments