返回 DeepSeek-Reasonix
resizeDrag.ts
根目录 / desktop / frontend / src / lib / resizeDrag.ts
1 interface StyleTarget {
2 style: {
3 setProperty(name: string, value: string): void;
4 };
5 }
6
7 interface AriaTarget {
8 setAttribute(name: string, value: string): void;
9 }
10
11 interface RafResizeUpdaterOptions {
12 target: StyleTarget;
13 separator?: AriaTarget | null;
14 cssVar: string;
15 onApply?: (value: number) => void;
16 }
17
18 export interface RafResizeUpdater {
19 schedule(value: number): void;
20 flush(): void;
21 cancel(): void;
22 }
23
24 function roundedPixel(value: number): number {
25 return Math.round(value);
26 }
27
28 export function createRafResizeUpdater({ target, separator, cssVar, onApply }: RafResizeUpdaterOptions): RafResizeUpdater {
29 let frame: number | null = null;
30 let latest: number | null = null;
31
32 const apply = () => {
33 frame = null;
34 if (latest === null) return;
35 const rounded = roundedPixel(latest);
36 target.style.setProperty(cssVar, `${rounded}px`);
37 separator?.setAttribute("aria-valuenow", String(rounded));
38 onApply?.(rounded);
39 };
40
41 return {
42 schedule(value: number) {
43 latest = value;
44 if (frame !== null) return;
45 frame = requestAnimationFrame(apply);
46 },
47 flush() {
48 if (frame !== null) {
49 cancelAnimationFrame(frame);
50 frame = null;
51 }
52 apply();
53 },
54 cancel() {
55 if (frame === null) return;
56 cancelAnimationFrame(frame);
57 frame = null;
58 },
59 };
60 }
61
61 lines TYPESCRIPT