Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/react-components/Anchor.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
width: var(--graph-block-anchor-width, 16px);
height: var(--graph-block-anchor-height, 16px);
cursor: pointer;
will-change: transform;
}

.graph-block-anchor.graph-block-anchor-selected {
Expand Down
14 changes: 8 additions & 6 deletions src/react-components/Block.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
box-sizing: content-box;
width: var(--graph-block-geometry-width);
height: var(--graph-block-geometry-height);

will-change: transform;

contain: layout size;
isolation: isolate;

/* Creates a composite layer */
transform-style: preserve-3d;
transform-style: flat;
pointer-events: all;
transform: translate3d(var(--graph-block-geometry-x, 0px), var(--graph-block-geometry-y, 0px), 0);
}

.graph-block-container-moving {
/*will-change: transform;*/
}

.graph-block-container-non-interactive {
pointer-events: none;
}
Expand All @@ -29,11 +31,11 @@
box-sizing: border-box;

background: var(--graph-block-bg);
border: 1px solid var(--graph-block-border)
border: 1px solid var(--graph-block-border);
}

.graph-block-wrapper.selected {
border-color: var(--graph-block-border-selected)
border-color: var(--graph-block-border-selected);
}

.graph-block-wrapper.selected:hover {
Expand Down
39 changes: 25 additions & 14 deletions src/react-components/Block.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useEffect, useMemo, useRef, useState } from "react";
import React, { ForwardedRef, forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";

import { noop } from "lodash";

import { TBlock } from "../components/canvas/blocks/Block";
import { Graph } from "../graph";
import { ESchedulerPriority } from "../lib/Scheduler";

import { useComputedSignal, useSignalEffect } from "./hooks";
import { useComputedSignal, useSchedulerDebounce, useSignalEffect } from "./hooks";
import { useBlockState } from "./hooks/useBlockState";
import { cn } from "./utils/cn";

Expand Down Expand Up @@ -84,19 +85,22 @@
* ```
*
*/
export const GraphBlock = <T extends TBlock>({
graph,
block,
children,
className,
containerClassName,
autoHideCanvas = true,
canvasVisible,
}: TGraphBlockProps<T>) => {
function GraphBlockInner<T extends TBlock>(
{ graph, block, children, className, containerClassName, autoHideCanvas = true, canvasVisible }: TGraphBlockProps<T>,
ref: ForwardedRef<HTMLDivElement>
) {
const containerRef = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => containerRef.current!);

Check warning on line 93 in src/react-components/Block.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Forbidden non-null assertion
const lastStateRef = useRef({ x: 0, y: 0, width: 0, height: 0, zIndex: 0 });
const state = useBlockState(graph, block);

const stopMoving = useSchedulerDebounce(
(container: HTMLDivElement) => {
container.classList.remove("graph-block-container-moving");
},
{ priority: ESchedulerPriority.LOW, frameTimeout: 150 }
);

const viewState = useComputedSignal(() => state?.$viewComponent.value, [state]);
const [interactive, setInteractive] = useState(viewState?.isInteractive() ?? false);

Expand Down Expand Up @@ -124,7 +128,7 @@
const geometry = state?.$geometry.value;
const container = containerRef.current;
const lastState = lastStateRef.current;
if (!container || !geometry) {
if (!container || !geometry || !viewState) {
return;
}

Expand All @@ -135,6 +139,10 @@
container.style.setProperty("--graph-block-geometry-y", `${geometry.y}px`);
lastState.x = geometry.x;
lastState.y = geometry.y;
if (!container.classList.contains("graph-block-container-moving")) {
container.classList.add("graph-block-container-moving");
}
stopMoving(container);
}

const hasSizeChange = lastState.width !== geometry.width || lastState.height !== geometry.height;
Expand All @@ -146,7 +154,6 @@
}

const { zIndex, order } = viewState.$viewState.value;

const newZIndex = (zIndex || 0) + (order || 0);

if (lastState.zIndex !== newZIndex) {
Expand Down Expand Up @@ -185,4 +192,8 @@
<div className={wrapperClassNames}>{children}</div>
</div>
);
};
}

export const GraphBlock = forwardRef(GraphBlockInner) as <T extends TBlock>(
props: TGraphBlockProps<T> & { ref?: React.Ref<HTMLDivElement> }
) => React.ReactElement | null;
6 changes: 2 additions & 4 deletions src/react-components/hooks/schedulerHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { debounce } from "../../utils/functions";
import { TDebounceOptions, TScheduleOptions, schedule, throttle } from "../../utils/utils/schedule";
import { useFn } from "../utils/hooks/useFn";

type TSchedulerDebounceFn = (...args: unknown[]) => void;
/**
* Hook to create a debounced function that delays execution until both frame and time conditions are met.
*
Expand All @@ -31,7 +30,7 @@ type TSchedulerDebounceFn = (...args: unknown[]) => void;
* debouncedSearch.cancel();
* ```
*/
export function useSchedulerDebounce<T extends TSchedulerDebounceFn>(fn: T, options: TDebounceOptions) {
export function useSchedulerDebounce<T extends (...args: Parameters<T>) => void>(fn: T, options: TDebounceOptions) {
const handle = useFn(fn);

/* Use memo to avoid re-creation of the debounce options on each render */
Expand All @@ -52,7 +51,6 @@ export function useSchedulerDebounce<T extends TSchedulerDebounceFn>(fn: T, opti
return debouncedFn;
}

type TSchedulerThrottleFn = (...args: unknown[]) => void;
/**
* Hook to create a throttled function that limits execution frequency.
*
Expand All @@ -78,7 +76,7 @@ type TSchedulerThrottleFn = (...args: unknown[]) => void;
* );
* ```
*/
export function useSchedulerThrottle<T extends TSchedulerThrottleFn>(fn: T, options: TDebounceOptions) {
export function useSchedulerThrottle<T extends (...args: Parameters<T>) => void>(fn: T, options: TDebounceOptions) {
const handle = useFn(fn);

/* Use memo to avoid re-creation of the throttle options on each render */
Expand Down
4 changes: 4 additions & 0 deletions src/react-components/hooks/useBlockState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function useBlockState<T extends TBlock>(graph: Graph, block: T | T["id"]
}, [graph, block]);
}

export function useSyncBlockState<T extends TBlock>(graph: Graph, block: T | T["id"]) {
return graph.rootStore.blocksList.$blocksMap.value.get(isTBlock(block) ? block.id : block);
}

export function useBlockViewState<T extends TBlock>(graph: Graph, block: T | T["id"]) {
const blockState = useBlockState(graph, block);
return blockState?.getViewComponent();
Expand Down
9 changes: 6 additions & 3 deletions src/services/Layer.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
canvas.layer {
outline: none;
filter: none;
border:none;
border: none;
}

.no-user-select {
Expand All @@ -34,9 +34,12 @@ canvas.layer {
isolation: isolate;
transform-origin: 0 0;
transform-style: preserve-3d;
will-change: transform;
}

.layer-with-camera-moving {
/*will-change: transform;*/
}

.layer-hidden {
visibility: hidden;
}
}
18 changes: 18 additions & 0 deletions src/services/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TGraphColors, TGraphConstants } from "../graphConfig";
import { GraphEventsDefinitions } from "../graphEvents";
import { CoreComponent } from "../lib";
import { Component, TComponentState } from "../lib/Component";
import { ESchedulerPriority } from "../lib/Scheduler";
import { debounce } from "../utils/utils/schedule";

import { ICamera, TCameraState } from "./camera/CameraService";

Expand Down Expand Up @@ -328,9 +330,24 @@ export class Layer<
this.updateSize();
}

private readonly stopCameraMoving = debounce(
() => {
this.html?.classList.remove("layer-with-camera-moving");
this.moving = false;
},
{ priority: ESchedulerPriority.LOW, frameTimeout: 150 }
);

protected moving = false;

protected scheduleCameraChange(camera: TCameraState) {
if (this.html && this.htmlActive) {
if (!this.moving) {
this.html.classList.add("layer-with-camera-moving");
this.moving = true;
}
this.html.style.transform = `matrix(${camera.scale}, 0, 0, ${camera.scale}, ${camera.x}, ${camera.y})`;
this.stopCameraMoving();
}
}

Expand Down Expand Up @@ -413,6 +430,7 @@ export class Layer<
}

protected unmount(): void {
this.stopCameraMoving.cancel();
this.unmountLayer();
super.unmount();
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/utils/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type TDebounceOptions = {
* @param options.frameTimeout - Minimum time in milliseconds to wait before execution (default: 0)
* @returns A debounced version of the function with cancel and flush methods
*/
export const debounce = <T extends (...args: unknown[]) => void>(
export const debounce = <T extends (...args: Parameters<T>) => void>(
fn: T,
{ priority = ESchedulerPriority.MEDIUM, frameInterval = 1, frameTimeout = 0 }: TDebounceOptions = {}
): T & { cancel: () => void; flush: () => void; isScheduled: () => boolean } => {
Expand Down Expand Up @@ -85,7 +85,7 @@ export const debounce = <T extends (...args: unknown[]) => void>(
removeScheduler = null;
const args = latestArgs;
latestArgs = undefined;
fn(...(args ?? []));
fn(...((args ?? []) as Parameters<T>));
if (currentRemoveScheduler) {
currentRemoveScheduler();
}
Expand Down Expand Up @@ -145,7 +145,7 @@ export const debounce = <T extends (...args: unknown[]) => void>(
* @param options.frameTimeout - Minimum time in milliseconds between executions (default: 0)
* @returns A throttled version of the function with cancel and flush methods
*/
export const throttle = <T extends (...args: unknown[]) => void>(
export const throttle = <T extends (...args: Parameters<T>) => void>(
fn: T,
{ priority = ESchedulerPriority.MEDIUM, frameInterval = 1, frameTimeout = 0 }: TDebounceOptions = {}
): T & { cancel: () => void; flush: () => void } => {
Expand Down Expand Up @@ -194,7 +194,7 @@ export const throttle = <T extends (...args: unknown[]) => void>(

const throttledFn = ((...args: Parameters<T>) => {
if (canExecute) {
fn(...(args ?? []));
fn(...args);
canExecute = false;
frameCounter = 0;
startTime = getNow(); // Start timing from this execution
Expand Down
Loading