mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 14:43:06 +08:00
video resize
This commit is contained in:
@@ -9,7 +9,8 @@
|
|||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.wrapper img {
|
.wrapper img,
|
||||||
|
.wrapper video {
|
||||||
height: auto !important;
|
height: auto !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,23 @@
|
|||||||
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 { useCallback } 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,
|
||||||
|
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 classes from "../common/toolbar-menu.module.css";
|
||||||
|
|
||||||
export function VideoMenu({ editor }: EditorMenuProps) {
|
export function VideoMenu({ editor }: EditorMenuProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -32,7 +36,7 @@ export function VideoMenu({ editor }: EditorMenuProps) {
|
|||||||
isAlignLeft: ctx.editor.isActive("video", { align: "left" }),
|
isAlignLeft: ctx.editor.isActive("video", { align: "left" }),
|
||||||
isAlignCenter: ctx.editor.isActive("video", { align: "center" }),
|
isAlignCenter: ctx.editor.isActive("video", { align: "center" }),
|
||||||
isAlignRight: ctx.editor.isActive("video", { align: "right" }),
|
isAlignRight: ctx.editor.isActive("video", { align: "right" }),
|
||||||
width: videoAttrs?.width ? parseInt(videoAttrs.width) : null,
|
src: videoAttrs?.src || null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -70,7 +74,7 @@ export function VideoMenu({ editor }: EditorMenuProps) {
|
|||||||
};
|
};
|
||||||
}, [editor]);
|
}, [editor]);
|
||||||
|
|
||||||
const alignVideoLeft = useCallback(() => {
|
const alignLeft = useCallback(() => {
|
||||||
editor
|
editor
|
||||||
.chain()
|
.chain()
|
||||||
.focus(undefined, { scrollIntoView: false })
|
.focus(undefined, { scrollIntoView: false })
|
||||||
@@ -78,7 +82,7 @@ export function VideoMenu({ editor }: EditorMenuProps) {
|
|||||||
.run();
|
.run();
|
||||||
}, [editor]);
|
}, [editor]);
|
||||||
|
|
||||||
const alignVideoCenter = useCallback(() => {
|
const alignCenter = useCallback(() => {
|
||||||
editor
|
editor
|
||||||
.chain()
|
.chain()
|
||||||
.focus(undefined, { scrollIntoView: false })
|
.focus(undefined, { scrollIntoView: false })
|
||||||
@@ -86,7 +90,7 @@ export function VideoMenu({ editor }: EditorMenuProps) {
|
|||||||
.run();
|
.run();
|
||||||
}, [editor]);
|
}, [editor]);
|
||||||
|
|
||||||
const alignVideoRight = useCallback(() => {
|
const alignRight = useCallback(() => {
|
||||||
editor
|
editor
|
||||||
.chain()
|
.chain()
|
||||||
.focus(undefined, { scrollIntoView: false })
|
.focus(undefined, { scrollIntoView: false })
|
||||||
@@ -94,16 +98,18 @@ export function VideoMenu({ 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;
|
||||||
.setVideoWidth(value)
|
a.download = "";
|
||||||
.run();
|
a.click();
|
||||||
},
|
}, [editorState?.src]);
|
||||||
[editor],
|
|
||||||
);
|
const handleDelete = useCallback(() => {
|
||||||
|
editor.commands.deleteSelection();
|
||||||
|
}, [editor]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BaseBubbleMenu
|
<BaseBubbleMenu
|
||||||
@@ -118,13 +124,15 @@ export function VideoMenu({ 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={alignVideoLeft}
|
onClick={alignLeft}
|
||||||
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>
|
||||||
@@ -132,10 +140,12 @@ export function VideoMenu({ editor }: EditorMenuProps) {
|
|||||||
|
|
||||||
<Tooltip position="top" label={t("Align center")}>
|
<Tooltip position="top" label={t("Align center")}>
|
||||||
<ActionIcon
|
<ActionIcon
|
||||||
onClick={alignVideoCenter}
|
onClick={alignCenter}
|
||||||
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>
|
||||||
@@ -143,19 +153,43 @@ export function VideoMenu({ editor }: EditorMenuProps) {
|
|||||||
|
|
||||||
<Tooltip position="top" label={t("Align right")}>
|
<Tooltip position="top" label={t("Align right")}>
|
||||||
<ActionIcon
|
<ActionIcon
|
||||||
onClick={alignVideoRight}
|
onClick={alignRight}
|
||||||
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("Delete")}>
|
||||||
|
<ActionIcon
|
||||||
|
onClick={handleDelete}
|
||||||
|
size="lg"
|
||||||
|
aria-label={t("Delete")}
|
||||||
|
variant="subtle"
|
||||||
|
c="dark"
|
||||||
|
>
|
||||||
|
<IconTrash size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Tooltip>
|
||||||
|
</div>
|
||||||
</BaseBubbleMenu>
|
</BaseBubbleMenu>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,6 +222,16 @@ export const mainExtensions = [
|
|||||||
}),
|
}),
|
||||||
TiptapVideo.configure({
|
TiptapVideo.configure({
|
||||||
view: VideoView,
|
view: VideoView,
|
||||||
|
resize: {
|
||||||
|
enabled: true,
|
||||||
|
directions: ["left", "right"],
|
||||||
|
minWidth: 80,
|
||||||
|
minHeight: 40,
|
||||||
|
alwaysPreserveAspectRatio: true,
|
||||||
|
//@ts-ignore
|
||||||
|
createCustomHandle: createResizeHandle,
|
||||||
|
className: buildResizeClasses("node-video"),
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
Callout.configure({
|
Callout.configure({
|
||||||
view: CalloutView,
|
view: CalloutView,
|
||||||
|
|||||||
@@ -1,16 +1,35 @@
|
|||||||
import { ReactNodeViewRenderer } from "@tiptap/react";
|
import { ReactNodeViewRenderer } from "@tiptap/react";
|
||||||
import { Range, Node } from "@tiptap/core";
|
import { Range, Node, mergeAttributes, ResizableNodeView } from "@tiptap/core";
|
||||||
|
import type { ResizableNodeViewDirection } from "@tiptap/core";
|
||||||
|
|
||||||
|
export type VideoResizeOptions = {
|
||||||
|
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 VideoOptions {
|
export interface VideoOptions {
|
||||||
view: any;
|
view: any;
|
||||||
HTMLAttributes: Record<string, any>;
|
HTMLAttributes: Record<string, any>;
|
||||||
|
resize: VideoResizeOptions | false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface VideoAttributes {
|
export interface VideoAttributes {
|
||||||
src?: string;
|
src?: 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;
|
||||||
@@ -27,6 +46,7 @@ declare module "@tiptap/core" {
|
|||||||
) => ReturnType;
|
) => ReturnType;
|
||||||
setVideoAlign: (align: "left" | "center" | "right") => ReturnType;
|
setVideoAlign: (align: "left" | "center" | "right") => ReturnType;
|
||||||
setVideoWidth: (width: number) => ReturnType;
|
setVideoWidth: (width: number) => ReturnType;
|
||||||
|
setVideoSize: (width: number, height: number) => ReturnType;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,6 +64,7 @@ export const TiptapVideo = Node.create<VideoOptions>({
|
|||||||
return {
|
return {
|
||||||
view: null,
|
view: null,
|
||||||
HTMLAttributes: {},
|
HTMLAttributes: {},
|
||||||
|
resize: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -64,12 +85,30 @@ export const TiptapVideo = Node.create<VideoOptions>({
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
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: VideoAttributes) => ({
|
renderHTML: (attributes: VideoAttributes) => ({
|
||||||
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: VideoAttributes) => ({
|
||||||
|
height: attributes.height,
|
||||||
|
}),
|
||||||
|
},
|
||||||
size: {
|
size: {
|
||||||
default: null,
|
default: null,
|
||||||
parseHTML: (element) => element.getAttribute("data-size"),
|
parseHTML: (element) => element.getAttribute("data-size"),
|
||||||
@@ -136,13 +175,168 @@ export const TiptapVideo = Node.create<VideoOptions>({
|
|||||||
commands.updateAttributes("video", {
|
commands.updateAttributes("video", {
|
||||||
width: `${Math.max(0, Math.min(100, width))}%`,
|
width: `${Math.max(0, Math.min(100, width))}%`,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
setVideoSize:
|
||||||
|
(width, height) =>
|
||||||
|
({ commands }) =>
|
||||||
|
commands.updateAttributes("video", { 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;
|
|
||||||
|
|
||||||
|
if (!resize || !resize.enabled) {
|
||||||
|
this.editor.isInitialized = true;
|
||||||
return ReactNodeViewRenderer(this.options.view);
|
return ReactNodeViewRenderer(this.options.view);
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
directions,
|
||||||
|
minWidth,
|
||||||
|
minHeight,
|
||||||
|
alwaysPreserveAspectRatio,
|
||||||
|
createCustomHandle,
|
||||||
|
className,
|
||||||
|
} = resize;
|
||||||
|
|
||||||
|
return (props) => {
|
||||||
|
const { node, getPos, HTMLAttributes, editor } = props;
|
||||||
|
|
||||||
|
if (!node.attrs.src) {
|
||||||
|
editor.isInitialized = true;
|
||||||
|
const reactView = ReactNodeViewRenderer(this.options.view);
|
||||||
|
const view = reactView(props);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const el = document.createElement("video");
|
||||||
|
el.src = node.attrs.src;
|
||||||
|
el.controls = true;
|
||||||
|
el.preload = "metadata";
|
||||||
|
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 || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
const w = updatedNode.attrs.width;
|
||||||
|
const h = updatedNode.attrs.height;
|
||||||
|
if (w != null) {
|
||||||
|
el.style.width = `${w}px`;
|
||||||
|
}
|
||||||
|
if (h != null) {
|
||||||
|
el.style.height = `${h}px`;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
applyAlignment(dom, node.attrs.align || "center");
|
||||||
|
|
||||||
|
// Handle percentage width backward compat
|
||||||
|
const widthAttr = node.attrs.width;
|
||||||
|
if (typeof widthAttr === "string" && widthAttr.endsWith("%")) {
|
||||||
|
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 video metadata loads
|
||||||
|
dom.style.visibility = "hidden";
|
||||||
|
dom.style.pointerEvents = "none";
|
||||||
|
el.onloadedmetadata = () => {
|
||||||
|
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