mirror of
https://github.com/docmost/docmost.git
synced 2026-05-20 16:44:05 +08:00
feat: new image menu
* switch to resizable side handles * use pixels
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
.toolbar {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 2px;
|
||||||
|
padding: 3px;
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid light-dark(var(--mantine-color-gray-2), var(--mantine-color-dark-4));
|
||||||
|
background-color: light-dark(var(--mantine-color-white), var(--mantine-color-dark-6));
|
||||||
|
box-shadow: 0 2px 12px light-dark(rgba(0, 0, 0, 0.08), rgba(0, 0, 0, 0.35));
|
||||||
|
}
|
||||||
|
|
||||||
|
.divider {
|
||||||
|
width: 1px;
|
||||||
|
height: 16px;
|
||||||
|
align-self: center;
|
||||||
|
margin: 0 2px;
|
||||||
|
background-color: light-dark(var(--mantine-color-gray-3), var(--mantine-color-dark-3));
|
||||||
|
}
|
||||||
|
|
||||||
|
.active {
|
||||||
|
background-color: light-dark(var(--mantine-color-blue-0), var(--mantine-color-dark-5));
|
||||||
|
color: light-dark(var(--mantine-color-blue-7), var(--mantine-color-blue-4));
|
||||||
|
}
|
||||||
@@ -1,22 +1,29 @@
|
|||||||
import { BubbleMenu as BaseBubbleMenu } from "@tiptap/react/menus";
|
import { BubbleMenu as BaseBubbleMenu } from "@tiptap/react/menus";
|
||||||
import { findParentNode, posToDOMRect, useEditorState } from "@tiptap/react";
|
import { findParentNode, posToDOMRect, useEditorState } from "@tiptap/react";
|
||||||
import React, { useCallback } from "react";
|
import React, { useCallback, useRef } from "react";
|
||||||
import { Node as PMNode } from "prosemirror-model";
|
import { Node as PMNode } from "prosemirror-model";
|
||||||
import {
|
import {
|
||||||
EditorMenuProps,
|
EditorMenuProps,
|
||||||
ShouldShowProps,
|
ShouldShowProps,
|
||||||
} from "@/features/editor/components/table/types/types.ts";
|
} from "@/features/editor/components/table/types/types.ts";
|
||||||
import { ActionIcon, Tooltip } from "@mantine/core";
|
import { ActionIcon, Tooltip } from "@mantine/core";
|
||||||
|
import clsx from "clsx";
|
||||||
import {
|
import {
|
||||||
IconLayoutAlignCenter,
|
IconLayoutAlignCenter,
|
||||||
IconLayoutAlignLeft,
|
IconLayoutAlignLeft,
|
||||||
IconLayoutAlignRight,
|
IconLayoutAlignRight,
|
||||||
|
IconDownload,
|
||||||
|
IconRefresh,
|
||||||
|
IconTrash,
|
||||||
} from "@tabler/icons-react";
|
} from "@tabler/icons-react";
|
||||||
import { NodeWidthResize } from "@/features/editor/components/common/node-width-resize.tsx";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { getFileUrl } from "@/lib/config.ts";
|
||||||
|
import { uploadImageAction } from "@/features/editor/components/image/upload-image-action.tsx";
|
||||||
|
import classes from "./image-menu.module.css";
|
||||||
|
|
||||||
export function ImageMenu({ editor }: EditorMenuProps) {
|
export function ImageMenu({ editor }: EditorMenuProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const editorState = useEditorState({
|
const editorState = useEditorState({
|
||||||
editor,
|
editor,
|
||||||
@@ -32,7 +39,7 @@ export function ImageMenu({ editor }: EditorMenuProps) {
|
|||||||
isAlignLeft: ctx.editor.isActive("image", { align: "left" }),
|
isAlignLeft: ctx.editor.isActive("image", { align: "left" }),
|
||||||
isAlignCenter: ctx.editor.isActive("image", { align: "center" }),
|
isAlignCenter: ctx.editor.isActive("image", { align: "center" }),
|
||||||
isAlignRight: ctx.editor.isActive("image", { align: "right" }),
|
isAlignRight: ctx.editor.isActive("image", { align: "right" }),
|
||||||
width: imageAttrs?.width ? parseInt(imageAttrs.width) : null,
|
src: imageAttrs?.src || null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -94,17 +101,39 @@ export function ImageMenu({ editor }: EditorMenuProps) {
|
|||||||
.run();
|
.run();
|
||||||
}, [editor]);
|
}, [editor]);
|
||||||
|
|
||||||
const onWidthChange = useCallback(
|
const handleDownload = useCallback(() => {
|
||||||
(value: number) => {
|
if (!editorState?.src) return;
|
||||||
editor
|
const url = getFileUrl(editorState.src);
|
||||||
.chain()
|
const a = document.createElement("a");
|
||||||
.focus(undefined, { scrollIntoView: false })
|
a.href = url;
|
||||||
.setImageWidth(value)
|
a.download = "";
|
||||||
.run();
|
a.click();
|
||||||
|
}, [editorState?.src]);
|
||||||
|
|
||||||
|
const handleReplace = useCallback(() => {
|
||||||
|
fileInputRef.current?.click();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleFileChange = useCallback(
|
||||||
|
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const file = e.target.files?.[0];
|
||||||
|
if (!file) return;
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const pageId = editor.storage?.pageId;
|
||||||
|
if (pageId) {
|
||||||
|
uploadImageAction(file, editor, pageId);
|
||||||
|
}
|
||||||
|
// Reset so the same file can be selected again
|
||||||
|
e.target.value = "";
|
||||||
},
|
},
|
||||||
[editor],
|
[editor],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const handleDelete = useCallback(() => {
|
||||||
|
editor.commands.deleteSelection();
|
||||||
|
}, [editor]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BaseBubbleMenu
|
<BaseBubbleMenu
|
||||||
editor={editor}
|
editor={editor}
|
||||||
@@ -118,13 +147,15 @@ export function ImageMenu({ editor }: EditorMenuProps) {
|
|||||||
}}
|
}}
|
||||||
shouldShow={shouldShow}
|
shouldShow={shouldShow}
|
||||||
>
|
>
|
||||||
<ActionIcon.Group className="actionIconGroup">
|
<div className={classes.toolbar}>
|
||||||
<Tooltip position="top" label={t("Align left")}>
|
<Tooltip position="top" label={t("Align left")}>
|
||||||
<ActionIcon
|
<ActionIcon
|
||||||
onClick={alignImageLeft}
|
onClick={alignImageLeft}
|
||||||
size="lg"
|
size="lg"
|
||||||
aria-label={t("Align left")}
|
aria-label={t("Align left")}
|
||||||
variant={editorState?.isAlignLeft ? "light" : "default"}
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
className={clsx({ [classes.active]: editorState?.isAlignLeft })}
|
||||||
>
|
>
|
||||||
<IconLayoutAlignLeft size={18} />
|
<IconLayoutAlignLeft size={18} />
|
||||||
</ActionIcon>
|
</ActionIcon>
|
||||||
@@ -135,7 +166,9 @@ export function ImageMenu({ editor }: EditorMenuProps) {
|
|||||||
onClick={alignImageCenter}
|
onClick={alignImageCenter}
|
||||||
size="lg"
|
size="lg"
|
||||||
aria-label={t("Align center")}
|
aria-label={t("Align center")}
|
||||||
variant={editorState?.isAlignCenter ? "light" : "default"}
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
className={clsx({ [classes.active]: editorState?.isAlignCenter })}
|
||||||
>
|
>
|
||||||
<IconLayoutAlignCenter size={18} />
|
<IconLayoutAlignCenter size={18} />
|
||||||
</ActionIcon>
|
</ActionIcon>
|
||||||
@@ -146,16 +179,60 @@ export function ImageMenu({ editor }: EditorMenuProps) {
|
|||||||
onClick={alignImageRight}
|
onClick={alignImageRight}
|
||||||
size="lg"
|
size="lg"
|
||||||
aria-label={t("Align right")}
|
aria-label={t("Align right")}
|
||||||
variant={editorState?.isAlignRight ? "light" : "default"}
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
className={clsx({ [classes.active]: editorState?.isAlignRight })}
|
||||||
>
|
>
|
||||||
<IconLayoutAlignRight size={18} />
|
<IconLayoutAlignRight size={18} />
|
||||||
</ActionIcon>
|
</ActionIcon>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
</ActionIcon.Group>
|
|
||||||
|
|
||||||
{editorState?.width && (
|
<div className={classes.divider} />
|
||||||
<NodeWidthResize onChange={onWidthChange} value={editorState.width} />
|
|
||||||
)}
|
<Tooltip position="top" label={t("Download")}>
|
||||||
|
<ActionIcon
|
||||||
|
onClick={handleDownload}
|
||||||
|
size="lg"
|
||||||
|
aria-label={t("Download")}
|
||||||
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
>
|
||||||
|
<IconDownload size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Tooltip>
|
||||||
|
|
||||||
|
<Tooltip position="top" label={t("Replace image")}>
|
||||||
|
<ActionIcon
|
||||||
|
onClick={handleReplace}
|
||||||
|
size="lg"
|
||||||
|
aria-label={t("Replace image")}
|
||||||
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
>
|
||||||
|
<IconRefresh size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Tooltip>
|
||||||
|
|
||||||
|
<Tooltip position="top" label={t("Delete")}>
|
||||||
|
<ActionIcon
|
||||||
|
onClick={handleDelete}
|
||||||
|
size="lg"
|
||||||
|
aria-label={t("Delete")}
|
||||||
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
>
|
||||||
|
<IconTrash size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Tooltip>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<input
|
||||||
|
ref={fileInputRef}
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
style={{ display: "none" }}
|
||||||
|
onChange={handleFileChange}
|
||||||
|
/>
|
||||||
</BaseBubbleMenu>
|
</BaseBubbleMenu>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import type { ResizableNodeViewDirection } from "@tiptap/core";
|
||||||
|
import classes from "./image-resize.module.css";
|
||||||
|
|
||||||
|
export function createImageHandle(
|
||||||
|
direction: ResizableNodeViewDirection,
|
||||||
|
): HTMLElement {
|
||||||
|
const handle = document.createElement("div");
|
||||||
|
handle.dataset.resizeHandle = direction;
|
||||||
|
handle.style.position = "absolute";
|
||||||
|
handle.className = classes.handle;
|
||||||
|
|
||||||
|
if (direction === "left") {
|
||||||
|
handle.style.left = "-8px";
|
||||||
|
handle.style.top = "0";
|
||||||
|
handle.style.bottom = "0";
|
||||||
|
} else if (direction === "right") {
|
||||||
|
handle.style.right = "-8px";
|
||||||
|
handle.style.top = "0";
|
||||||
|
handle.style.bottom = "0";
|
||||||
|
}
|
||||||
|
|
||||||
|
const bar = document.createElement("div");
|
||||||
|
bar.className = classes.handleBar;
|
||||||
|
handle.appendChild(bar);
|
||||||
|
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const imageResizeClasses = {
|
||||||
|
container: `${classes.container} node-image`,
|
||||||
|
wrapper: classes.wrapper,
|
||||||
|
resizing: classes.resizing,
|
||||||
|
};
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
.container {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wrapper {
|
||||||
|
position: relative;
|
||||||
|
border-radius: 8px;
|
||||||
|
overflow: visible;
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wrapper img {
|
||||||
|
height: auto !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.resizing {
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.handle {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
width: 16px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
cursor: ew-resize;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.2s ease;
|
||||||
|
z-index: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.handle[data-resize-handle="left"] {
|
||||||
|
left: -8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.handle[data-resize-handle="right"] {
|
||||||
|
right: -8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wrapper:hover .handle {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.resizing .handle {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.handleBar {
|
||||||
|
width: 4px;
|
||||||
|
height: 48px;
|
||||||
|
border-radius: 4px;
|
||||||
|
transition: background-color 0.15s ease;
|
||||||
|
background-color: light-dark(var(--mantine-color-blue-4), var(--mantine-color-blue-5));
|
||||||
|
}
|
||||||
|
|
||||||
|
.handle:hover .handleBar {
|
||||||
|
background-color: light-dark(var(--mantine-color-blue-6), var(--mantine-color-blue-4));
|
||||||
|
}
|
||||||
|
|
||||||
|
.resizing .handleBar {
|
||||||
|
background-color: light-dark(var(--mantine-color-blue-6), var(--mantine-color-blue-4));
|
||||||
|
}
|
||||||
@@ -52,6 +52,10 @@ import { IUser } from "@/features/user/types/user.types.ts";
|
|||||||
import MathInlineView from "@/features/editor/components/math/math-inline.tsx";
|
import MathInlineView from "@/features/editor/components/math/math-inline.tsx";
|
||||||
import MathBlockView from "@/features/editor/components/math/math-block.tsx";
|
import MathBlockView from "@/features/editor/components/math/math-block.tsx";
|
||||||
import ImageView from "@/features/editor/components/image/image-view.tsx";
|
import ImageView from "@/features/editor/components/image/image-view.tsx";
|
||||||
|
import {
|
||||||
|
createImageHandle,
|
||||||
|
imageResizeClasses,
|
||||||
|
} from "@/features/editor/components/image/image-resize-handles.ts";
|
||||||
import CalloutView from "@/features/editor/components/callout/callout-view.tsx";
|
import CalloutView from "@/features/editor/components/callout/callout-view.tsx";
|
||||||
import VideoView from "@/features/editor/components/video/video-view.tsx";
|
import VideoView from "@/features/editor/components/video/video-view.tsx";
|
||||||
import AttachmentView from "@/features/editor/components/attachment/attachment-view.tsx";
|
import AttachmentView from "@/features/editor/components/attachment/attachment-view.tsx";
|
||||||
@@ -91,6 +95,7 @@ lowlight.register("fortran", fortran);
|
|||||||
lowlight.register("haskell", haskell);
|
lowlight.register("haskell", haskell);
|
||||||
lowlight.register("scala", scala);
|
lowlight.register("scala", scala);
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
export const mainExtensions = [
|
export const mainExtensions = [
|
||||||
StarterKit.configure({
|
StarterKit.configure({
|
||||||
heading: false,
|
heading: false,
|
||||||
@@ -200,6 +205,16 @@ export const mainExtensions = [
|
|||||||
TiptapImage.configure({
|
TiptapImage.configure({
|
||||||
view: ImageView,
|
view: ImageView,
|
||||||
allowBase64: false,
|
allowBase64: false,
|
||||||
|
resize: {
|
||||||
|
enabled: true,
|
||||||
|
directions: ["left", "right"],
|
||||||
|
minWidth: 80,
|
||||||
|
minHeight: 40,
|
||||||
|
alwaysPreserveAspectRatio: true,
|
||||||
|
//@ts-ignore
|
||||||
|
createCustomHandle: createImageHandle,
|
||||||
|
className: imageResizeClasses,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
TiptapVideo.configure({
|
TiptapVideo.configure({
|
||||||
view: VideoView,
|
view: VideoView,
|
||||||
|
|||||||
@@ -42,9 +42,9 @@ if (isCloud() && isPostHogEnabled) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(
|
|
||||||
document.getElementById("root") as HTMLElement,
|
const container = document.getElementById("root") as HTMLElement;
|
||||||
);
|
const root = (container as any).__reactRoot ??= ReactDOM.createRoot(container);
|
||||||
|
|
||||||
root.render(
|
root.render(
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
|
|||||||
@@ -1,18 +1,41 @@
|
|||||||
import Image from "@tiptap/extension-image";
|
import Image from "@tiptap/extension-image";
|
||||||
import { ImageOptions as DefaultImageOptions } from "@tiptap/extension-image";
|
import { ImageOptions as DefaultImageOptions } from "@tiptap/extension-image";
|
||||||
import { ReactNodeViewRenderer } from "@tiptap/react";
|
import { ReactNodeViewRenderer } from "@tiptap/react";
|
||||||
import { mergeAttributes, Range } from "@tiptap/core";
|
import {
|
||||||
|
mergeAttributes,
|
||||||
|
Range,
|
||||||
|
ResizableNodeView,
|
||||||
|
} from "@tiptap/core";
|
||||||
|
import type { ResizableNodeViewDirection } from "@tiptap/core";
|
||||||
|
|
||||||
|
export type ImageResizeOptions = {
|
||||||
|
enabled: boolean;
|
||||||
|
directions?: ResizableNodeViewDirection[];
|
||||||
|
minWidth?: number;
|
||||||
|
minHeight?: number;
|
||||||
|
alwaysPreserveAspectRatio?: boolean;
|
||||||
|
createCustomHandle?: (direction: ResizableNodeViewDirection) => HTMLElement;
|
||||||
|
className?: {
|
||||||
|
container?: string;
|
||||||
|
wrapper?: string;
|
||||||
|
handle?: string;
|
||||||
|
resizing?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export interface ImageOptions extends DefaultImageOptions {
|
export interface ImageOptions extends DefaultImageOptions {
|
||||||
view: any;
|
view: any;
|
||||||
|
resize: ImageResizeOptions | false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ImageAttributes {
|
export interface ImageAttributes {
|
||||||
src?: string;
|
src?: string;
|
||||||
alt?: string;
|
alt?: string;
|
||||||
align?: string;
|
align?: string;
|
||||||
attachmentId?: string;
|
attachmentId?: string;
|
||||||
size?: number;
|
size?: number;
|
||||||
width?: number;
|
width?: number | string;
|
||||||
|
height?: number;
|
||||||
aspectRatio?: number;
|
aspectRatio?: number;
|
||||||
placeholder?: {
|
placeholder?: {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -25,10 +48,11 @@ declare module "@tiptap/core" {
|
|||||||
imageBlock: {
|
imageBlock: {
|
||||||
setImage: (attributes: ImageAttributes) => ReturnType;
|
setImage: (attributes: ImageAttributes) => ReturnType;
|
||||||
setImageAt: (
|
setImageAt: (
|
||||||
attributes: ImageAttributes & { pos: number | Range }
|
attributes: ImageAttributes & { pos: number | Range },
|
||||||
) => ReturnType;
|
) => ReturnType;
|
||||||
setImageAlign: (align: "left" | "center" | "right") => ReturnType;
|
setImageAlign: (align: "left" | "center" | "right") => ReturnType;
|
||||||
setImageWidth: (width: number) => ReturnType;
|
setImageWidth: (width: number) => ReturnType;
|
||||||
|
setImageSize: (width: number, height: number) => ReturnType;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,6 +70,7 @@ export const TiptapImage = Image.extend<ImageOptions>({
|
|||||||
return {
|
return {
|
||||||
...this.parent?.(),
|
...this.parent?.(),
|
||||||
view: null,
|
view: null,
|
||||||
|
resize: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -59,12 +84,30 @@ export const TiptapImage = Image.extend<ImageOptions>({
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
width: {
|
width: {
|
||||||
default: "100%",
|
default: null,
|
||||||
parseHTML: (element) => element.getAttribute("width"),
|
parseHTML: (element) => {
|
||||||
|
const raw = element.getAttribute("width");
|
||||||
|
if (!raw) return null;
|
||||||
|
if (raw.endsWith("%")) return raw;
|
||||||
|
const num = parseFloat(raw);
|
||||||
|
return isNaN(num) ? null : num;
|
||||||
|
},
|
||||||
renderHTML: (attributes: ImageAttributes) => ({
|
renderHTML: (attributes: ImageAttributes) => ({
|
||||||
width: attributes.width,
|
width: attributes.width,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
height: {
|
||||||
|
default: null,
|
||||||
|
parseHTML: (element) => {
|
||||||
|
const raw = element.getAttribute("height");
|
||||||
|
if (!raw) return null;
|
||||||
|
const num = parseFloat(raw);
|
||||||
|
return isNaN(num) ? null : num;
|
||||||
|
},
|
||||||
|
renderHTML: (attributes: ImageAttributes) => ({
|
||||||
|
height: attributes.height,
|
||||||
|
}),
|
||||||
|
},
|
||||||
align: {
|
align: {
|
||||||
default: "center",
|
default: "center",
|
||||||
parseHTML: (element) => element.getAttribute("data-align"),
|
parseHTML: (element) => element.getAttribute("data-align"),
|
||||||
@@ -142,16 +185,189 @@ export const TiptapImage = Image.extend<ImageOptions>({
|
|||||||
setImageWidth:
|
setImageWidth:
|
||||||
(width) =>
|
(width) =>
|
||||||
({ commands }) =>
|
({ commands }) =>
|
||||||
commands.updateAttributes("image", {
|
commands.updateAttributes("image", { width }),
|
||||||
width: `${Math.max(0, Math.min(100, width))}%`,
|
|
||||||
}),
|
setImageSize:
|
||||||
|
(width, height) =>
|
||||||
|
({ commands }) =>
|
||||||
|
commands.updateAttributes("image", { width, height }),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
addNodeView() {
|
addNodeView() {
|
||||||
// Force the react node view to render immediately using flush sync (https://github.com/ueberdosis/tiptap/blob/b4db352f839e1d82f9add6ee7fb45561336286d8/packages/react/src/ReactRenderer.tsx#L183-L191)
|
const resize = this.options.resize;
|
||||||
this.editor.isInitialized = true;
|
|
||||||
|
|
||||||
return ReactNodeViewRenderer(this.options.view);
|
if (!resize || !resize.enabled) {
|
||||||
|
// Fallback to React node view (existing behavior)
|
||||||
|
this.editor.isInitialized = true;
|
||||||
|
return ReactNodeViewRenderer(this.options.view);
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
directions,
|
||||||
|
minWidth,
|
||||||
|
minHeight,
|
||||||
|
alwaysPreserveAspectRatio,
|
||||||
|
createCustomHandle,
|
||||||
|
className,
|
||||||
|
} = resize;
|
||||||
|
|
||||||
|
return ({ node, getPos, HTMLAttributes, editor }) => {
|
||||||
|
// If no src yet (placeholder/uploading), use React view for loading UI
|
||||||
|
if (!HTMLAttributes.src) {
|
||||||
|
editor.isInitialized = true;
|
||||||
|
const reactView = ReactNodeViewRenderer(this.options.view);
|
||||||
|
const view = reactView({
|
||||||
|
node,
|
||||||
|
getPos,
|
||||||
|
HTMLAttributes,
|
||||||
|
editor,
|
||||||
|
extension: this,
|
||||||
|
decorations: [] as any,
|
||||||
|
innerDecorations: {} as any,
|
||||||
|
});
|
||||||
|
|
||||||
|
// When the node gets a src, return false from update to force rebuild
|
||||||
|
const originalUpdate = view.update?.bind(view);
|
||||||
|
view.update = (updatedNode, decorations, innerDecorations) => {
|
||||||
|
if (updatedNode.attrs.src && !node.attrs.src) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (originalUpdate) {
|
||||||
|
return originalUpdate(updatedNode, decorations, innerDecorations);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has src — use ResizableNodeView
|
||||||
|
const el = document.createElement("img");
|
||||||
|
|
||||||
|
Object.entries(HTMLAttributes).forEach(([key, value]) => {
|
||||||
|
if (value != null) {
|
||||||
|
switch (key) {
|
||||||
|
case "width":
|
||||||
|
case "height":
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
el.setAttribute(key, String(value));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
el.src = HTMLAttributes.src;
|
||||||
|
el.style.display = "block";
|
||||||
|
el.style.maxWidth = "100%";
|
||||||
|
el.style.borderRadius = "8px";
|
||||||
|
|
||||||
|
let currentNode = node;
|
||||||
|
|
||||||
|
const nodeView = new ResizableNodeView({
|
||||||
|
element: el,
|
||||||
|
editor,
|
||||||
|
node,
|
||||||
|
getPos,
|
||||||
|
onResize: (w, h) => {
|
||||||
|
el.style.width = `${w}px`;
|
||||||
|
el.style.height = `${h}px`;
|
||||||
|
},
|
||||||
|
onCommit: () => {
|
||||||
|
const pos = getPos();
|
||||||
|
if (pos === undefined) return;
|
||||||
|
|
||||||
|
this.editor
|
||||||
|
.chain()
|
||||||
|
.setNodeSelection(pos)
|
||||||
|
.updateAttributes(this.name, {
|
||||||
|
width: Math.round(el.offsetWidth),
|
||||||
|
height: Math.round(el.offsetHeight),
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
},
|
||||||
|
onUpdate: (updatedNode, _decorations, _innerDecorations) => {
|
||||||
|
if (updatedNode.type !== currentNode.type) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updatedNode.attrs.src !== currentNode.attrs.src) {
|
||||||
|
el.src = updatedNode.attrs.src || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updatedNode.attrs.alt !== currentNode.attrs.alt) {
|
||||||
|
el.alt = updatedNode.attrs.alt || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update alignment on container
|
||||||
|
const align = updatedNode.attrs.align || "center";
|
||||||
|
const container = nodeView.dom as HTMLElement;
|
||||||
|
applyAlignment(container, align);
|
||||||
|
|
||||||
|
currentNode = updatedNode;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
directions,
|
||||||
|
min: {
|
||||||
|
width: minWidth,
|
||||||
|
height: minHeight,
|
||||||
|
},
|
||||||
|
preserveAspectRatio: alwaysPreserveAspectRatio === true,
|
||||||
|
createCustomHandle,
|
||||||
|
className,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const dom = nodeView.dom as HTMLElement;
|
||||||
|
|
||||||
|
// Apply initial alignment
|
||||||
|
applyAlignment(dom, node.attrs.align || "center");
|
||||||
|
|
||||||
|
// Handle percentage width backward compat
|
||||||
|
const widthAttr = node.attrs.width;
|
||||||
|
if (typeof widthAttr === "string" && widthAttr.endsWith("%")) {
|
||||||
|
// Defer conversion until we can measure the container
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
const parentEl = dom.parentElement;
|
||||||
|
if (parentEl) {
|
||||||
|
const containerWidth = parentEl.clientWidth;
|
||||||
|
const pctValue = parseInt(widthAttr, 10);
|
||||||
|
if (!isNaN(pctValue) && containerWidth > 0) {
|
||||||
|
const pxWidth = Math.round(
|
||||||
|
containerWidth * (pctValue / 100),
|
||||||
|
);
|
||||||
|
el.style.width = `${pxWidth}px`;
|
||||||
|
if (node.attrs.aspectRatio) {
|
||||||
|
el.style.height = `${Math.round(pxWidth / node.attrs.aspectRatio)}px`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dom.style.visibility = "";
|
||||||
|
dom.style.pointerEvents = "";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hide until image loads (official TipTap pattern)
|
||||||
|
dom.style.visibility = "hidden";
|
||||||
|
dom.style.pointerEvents = "none";
|
||||||
|
el.onload = () => {
|
||||||
|
dom.style.visibility = "";
|
||||||
|
dom.style.pointerEvents = "";
|
||||||
|
};
|
||||||
|
|
||||||
|
return nodeView;
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function applyAlignment(container: HTMLElement, align: string) {
|
||||||
|
if (align === "left") {
|
||||||
|
container.style.justifyContent = "flex-start";
|
||||||
|
} else if (align === "right") {
|
||||||
|
container.style.justifyContent = "flex-end";
|
||||||
|
} else {
|
||||||
|
container.style.justifyContent = "center";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user