mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
657fdf8cb7
* Tiptap3 migration - WIP * fix collaboration * remove unused code * fix flicker * disable duplicate extensions * update tiptap version * Switch to useEditorState - Set shouldRerenderOnTransaction to false * fix editable state * add tippyoptions for reference * merge main * tiptap 3.6.1 * fix bubble menu * fix converter * fix menus * fix collaboration caret css * fix: Set `isInitialized` to force immediate react node view rendering * feat: Migrate tippy.js menus to Floating UI * feat: Update collaboration connection for HocusPocus v3 * fix: Connect/disconnect websocketProvider * cleanup * cleanup * feat: Improved placeholder and upload handling for images * feat: Improved placeholder and upload handling for videos * refactor: Image node and view clean-up * feat: Improved placeholder and upload handling for attachments * fix: Video view styles * fix: Transaction handling on asset upload * fix: Use imageDimensionsFromStream * feat: Multiple file upload, improved placeholders, local previews * fix: Drag & drop, paste upload * fix: Allow media as attachment * * add skeleton pulse animation * add translation strings * fix attachment view responsiveness * fix collab connection status display * Tiptap v3.17.0 * fix suggestion menu exit bug * fix search shortcut * fix history editor css * tiptap 3.17.1 --------- Co-authored-by: Arek Nawo <areknawo@areknawo.com>
164 lines
4.5 KiB
TypeScript
164 lines
4.5 KiB
TypeScript
import { BubbleMenu as BaseBubbleMenu } from "@tiptap/react/menus";
|
|
import { findParentNode, posToDOMRect, useEditorState } from "@tiptap/react";
|
|
import React, { useCallback } from "react";
|
|
import { Node as PMNode } from "prosemirror-model";
|
|
import {
|
|
EditorMenuProps,
|
|
ShouldShowProps,
|
|
} from "@/features/editor/components/table/types/types.ts";
|
|
import { ActionIcon, Tooltip } from "@mantine/core";
|
|
import {
|
|
IconLayoutAlignCenter,
|
|
IconLayoutAlignLeft,
|
|
IconLayoutAlignRight,
|
|
} from "@tabler/icons-react";
|
|
import { NodeWidthResize } from "@/features/editor/components/common/node-width-resize.tsx";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
export function ImageMenu({ editor }: EditorMenuProps) {
|
|
const { t } = useTranslation();
|
|
|
|
const editorState = useEditorState({
|
|
editor,
|
|
selector: (ctx) => {
|
|
if (!ctx.editor) {
|
|
return null;
|
|
}
|
|
|
|
const imageAttrs = ctx.editor.getAttributes("image");
|
|
|
|
return {
|
|
isImage: ctx.editor.isActive("image"),
|
|
isAlignLeft: ctx.editor.isActive("image", { align: "left" }),
|
|
isAlignCenter: ctx.editor.isActive("image", { align: "center" }),
|
|
isAlignRight: ctx.editor.isActive("image", { align: "right" }),
|
|
width: imageAttrs?.width ? parseInt(imageAttrs.width) : null,
|
|
};
|
|
},
|
|
});
|
|
|
|
const shouldShow = useCallback(
|
|
({ state }: ShouldShowProps) => {
|
|
if (!state) {
|
|
return false;
|
|
}
|
|
|
|
return editor.isActive("image") && editor.getAttributes("image").src;
|
|
},
|
|
[editor],
|
|
);
|
|
|
|
const getReferencedVirtualElement = useCallback(() => {
|
|
if (!editor) return;
|
|
const { selection } = editor.state;
|
|
const predicate = (node: PMNode) => node.type.name === "image";
|
|
const parent = findParentNode(predicate)(selection);
|
|
|
|
if (parent) {
|
|
const dom = editor.view.nodeDOM(parent?.pos) as HTMLElement;
|
|
const domRect = dom.getBoundingClientRect();
|
|
return {
|
|
getBoundingClientRect: () => domRect,
|
|
getClientRects: () => [domRect],
|
|
};
|
|
}
|
|
|
|
const domRect = posToDOMRect(editor.view, selection.from, selection.to);
|
|
return {
|
|
getBoundingClientRect: () => domRect,
|
|
getClientRects: () => [domRect],
|
|
};
|
|
}, [editor]);
|
|
|
|
const alignImageLeft = useCallback(() => {
|
|
editor
|
|
.chain()
|
|
.focus(undefined, { scrollIntoView: false })
|
|
.setImageAlign("left")
|
|
.run();
|
|
}, [editor]);
|
|
|
|
const alignImageCenter = useCallback(() => {
|
|
editor
|
|
.chain()
|
|
.focus(undefined, { scrollIntoView: false })
|
|
.setImageAlign("center")
|
|
.run();
|
|
}, [editor]);
|
|
|
|
const alignImageRight = useCallback(() => {
|
|
editor
|
|
.chain()
|
|
.focus(undefined, { scrollIntoView: false })
|
|
.setImageAlign("right")
|
|
.run();
|
|
}, [editor]);
|
|
|
|
const onWidthChange = useCallback(
|
|
(value: number) => {
|
|
editor
|
|
.chain()
|
|
.focus(undefined, { scrollIntoView: false })
|
|
.setImageWidth(value)
|
|
.run();
|
|
},
|
|
[editor],
|
|
);
|
|
|
|
return (
|
|
<BaseBubbleMenu
|
|
editor={editor}
|
|
pluginKey={`image-menu`}
|
|
updateDelay={0}
|
|
getReferencedVirtualElement={getReferencedVirtualElement}
|
|
options={{
|
|
placement: "top",
|
|
offset: 8,
|
|
flip: false,
|
|
}}
|
|
shouldShow={shouldShow}
|
|
>
|
|
<ActionIcon.Group className="actionIconGroup">
|
|
<Tooltip position="top" label={t("Align left")}>
|
|
<ActionIcon
|
|
onClick={alignImageLeft}
|
|
size="lg"
|
|
aria-label={t("Align left")}
|
|
variant={editorState?.isAlignLeft ? "light" : "default"}
|
|
>
|
|
<IconLayoutAlignLeft size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
|
|
<Tooltip position="top" label={t("Align center")}>
|
|
<ActionIcon
|
|
onClick={alignImageCenter}
|
|
size="lg"
|
|
aria-label={t("Align center")}
|
|
variant={editorState?.isAlignCenter ? "light" : "default"}
|
|
>
|
|
<IconLayoutAlignCenter size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
|
|
<Tooltip position="top" label={t("Align right")}>
|
|
<ActionIcon
|
|
onClick={alignImageRight}
|
|
size="lg"
|
|
aria-label={t("Align right")}
|
|
variant={editorState?.isAlignRight ? "light" : "default"}
|
|
>
|
|
<IconLayoutAlignRight size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
</ActionIcon.Group>
|
|
|
|
{editorState?.width && (
|
|
<NodeWidthResize onChange={onWidthChange} value={editorState.width} />
|
|
)}
|
|
</BaseBubbleMenu>
|
|
);
|
|
}
|
|
|
|
export default ImageMenu;
|