This commit is contained in:
Ivan Rezchikov
2026-07-15 22:35:07 +03:00
parent 687fbaf3a6
commit ac75c72382
4 changed files with 16 additions and 14 deletions
+14 -11
View File
@@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
import { Block, BlockType, Page, PageMeta, PageType } from "@/lib/pages/types";
import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, ChevronDownIcon, ChevronUpIcon, ImageIcon, QuoteIcon, AlertCircleIcon, MinusIcon, ListIcon, TableIcon, TypeIcon, AlignLeftIcon, InfoIcon, DotIcon, ListOrderedIcon, LinkIcon, MoveDiagonalIcon, MoveDiagonal2Icon } from "lucide-react";
import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, ChevronDownIcon, ChevronUpIcon, ImageIcon, QuoteIcon, AlertCircleIcon, MinusIcon, ListIcon, TableIcon, TypeIcon, AlignLeftIcon, InfoIcon, DotIcon, ListOrderedIcon, LinkIcon, MoveDiagonal2Icon } from "lucide-react";
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
@@ -73,8 +73,13 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
const [previewUrls, setPreviewUrls] = useState<Map<string, string>>(new Map());
const previewCacheRef = useRef<Map<string, string>>(new Map());
const onChangeRef = useRef(onChange);
useEffect(() => {
onChange({
onChangeRef.current = onChange;
});
useEffect(() => {
onChangeRef.current({
title,
type,
slug: slug || title.toLowerCase().replace(/\s+/g, "-"),
@@ -82,7 +87,7 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
tags,
blocks,
});
}, [title, slug, location, tags, blocks]);
}, [title, type, slug, location, tags, blocks]);
useEffect(() => {
const imageBlocks = blocks.filter(
@@ -626,15 +631,13 @@ function TableEditor({ content, onChange }: { content: { type: "TABLE"; headers:
function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Block; previewUrl?: string; onChange: (c: Block["content"]) => void; onPreviewReady: (id: string, url: string) => void }) {
const content = block.content as { type: "IMAGE"; url: string; caption?: string; width?: number };
const [isUploading, setIsUploading] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const [supportsDrag, setSupportsDrag] = useState(false);
const [supportsDrag] = useState(
() => typeof window !== "undefined" && window.matchMedia("(hover: hover) and (pointer: fine)").matches
);
const isDraggingRef = useRef(false);
const dragStartRef = useRef<{ x: number; width: number } | null>(null);
const imgRef = useRef<HTMLImageElement>(null);
useEffect(() => {
setSupportsDrag(window.matchMedia("(hover: hover) and (pointer: fine)").matches);
}, []);
const currentWidth = content.width;
function handleDragStart(e: React.PointerEvent) {
@@ -642,7 +645,7 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
e.stopPropagation();
const startWidth = imgRef.current?.offsetWidth ?? 500;
dragStartRef.current = { x: e.clientX, width: startWidth };
setIsDragging(true);
isDraggingRef.current = true;
function handleMove(ev: PointerEvent) {
if (!dragStartRef.current) return;
@@ -654,7 +657,7 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
}
function handleUp() {
setIsDragging(false);
isDraggingRef.current = false;
dragStartRef.current = null;
window.removeEventListener("pointermove", handleMove);
window.removeEventListener("pointerup", handleUp);
-1
View File
@@ -33,7 +33,6 @@ type Props = {
const MAX_SIZE = 5 * 1024 * 1024;
const DEFAULT_ACCEPT = "image/jpeg,image/png,image/webp";
const EDITOR_WRAPPER_CLASS = "flex flex-col gap-2 items-stretch md:items-start";
const EDITOR_IMG_CLASS = "block max-w-full md:max-w-200 h-auto rounded-md border object-contain";
function formatFileSize(bytes: number): string {
return bytes < 1024 * 1024