mirror of
https://github.com/docmost/docmost.git
synced 2025-11-23 03:21:10 +10:00
* update tiptap version * excalidraw init * cleanup * better file handling and other fixes * use different modal to fix excalidraw cursor position issue * see https://github.com/excalidraw/excalidraw/issues/7312 * fix websocket in vite dev mode * WIP * add align attribute * fix table * menu icons * Render image in excalidraw html * add size to custom SVG components * rewrite undefined font urls
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
import {
|
|
BubbleMenu as BaseBubbleMenu,
|
|
findParentNode,
|
|
posToDOMRect,
|
|
} from '@tiptap/react';
|
|
import { useCallback } from 'react';
|
|
import { sticky } from 'tippy.js';
|
|
import { Node as PMNode } from 'prosemirror-model';
|
|
import {
|
|
EditorMenuProps,
|
|
ShouldShowProps,
|
|
} from '@/features/editor/components/table/types/types.ts';
|
|
import { NodeWidthResize } from '@/features/editor/components/common/node-width-resize.tsx';
|
|
|
|
export function ExcalidrawMenu({ editor }: EditorMenuProps) {
|
|
const shouldShow = useCallback(
|
|
({ state }: ShouldShowProps) => {
|
|
if (!state) {
|
|
return false;
|
|
}
|
|
|
|
return editor.isActive('excalidraw') && editor.getAttributes('excalidraw')?.src;
|
|
},
|
|
[editor]
|
|
);
|
|
|
|
const getReferenceClientRect = useCallback(() => {
|
|
const { selection } = editor.state;
|
|
const predicate = (node: PMNode) => node.type.name === 'excalidraw';
|
|
const parent = findParentNode(predicate)(selection);
|
|
|
|
if (parent) {
|
|
const dom = editor.view.nodeDOM(parent?.pos) as HTMLElement;
|
|
return dom.getBoundingClientRect();
|
|
}
|
|
|
|
return posToDOMRect(editor.view, selection.from, selection.to);
|
|
}, [editor]);
|
|
|
|
const onWidthChange = useCallback(
|
|
(value: number) => {
|
|
editor.commands.updateAttributes('excalidraw', { width: `${value}%` });
|
|
},
|
|
[editor]
|
|
);
|
|
|
|
return (
|
|
<BaseBubbleMenu
|
|
editor={editor}
|
|
pluginKey={`excalidraw-menu}`}
|
|
updateDelay={0}
|
|
tippyOptions={{
|
|
getReferenceClientRect,
|
|
offset: [0, 8],
|
|
zIndex: 99,
|
|
popperOptions: {
|
|
modifiers: [{ name: 'flip', enabled: false }],
|
|
},
|
|
plugins: [sticky],
|
|
sticky: 'popper',
|
|
}}
|
|
shouldShow={shouldShow}
|
|
>
|
|
<div
|
|
style={{
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
{editor.getAttributes('excalidraw')?.width && (
|
|
<NodeWidthResize
|
|
onChange={onWidthChange}
|
|
value={parseInt(editor.getAttributes('excalidraw').width)}
|
|
/>
|
|
)}
|
|
</div>
|
|
</BaseBubbleMenu>
|
|
);
|
|
}
|
|
|
|
export default ExcalidrawMenu;
|