@cocorof/graphier 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2049,6 +2049,45 @@ const _right = new THREE__namespace.Vector3();
2049
2049
  const _q = new THREE__namespace.Quaternion();
2050
2050
  const _dir = new THREE__namespace.Vector3();
2051
2051
  const _worldUp = new THREE__namespace.Vector3(0, 1, 0);
2052
+ const PAN_SPEED = 0.02;
2053
+ const PAN_ZOOM_FACTOR = 0.02;
2054
+ function createPanUpdate(camera, controls, keys) {
2055
+ const _panRight = new THREE__namespace.Vector3();
2056
+ const _panUp = new THREE__namespace.Vector3();
2057
+ const _panOffset = new THREE__namespace.Vector3();
2058
+ return function update() {
2059
+ let dx = 0;
2060
+ let dy = 0;
2061
+ if (keys["ArrowLeft"] || keys["a"] || keys["A"]) dx -= 1;
2062
+ if (keys["ArrowRight"] || keys["d"] || keys["D"]) dx += 1;
2063
+ if (keys["ArrowUp"] || keys["w"] || keys["W"]) dy += 1;
2064
+ if (keys["ArrowDown"] || keys["s"] || keys["S"]) dy -= 1;
2065
+ const zoomIn = keys["z"] || keys["Z"] || keys["+"] || keys["="];
2066
+ const zoomOut = keys["x"] || keys["X"] || keys["-"] || keys["_"];
2067
+ if (!dx && !dy && !zoomIn && !zoomOut) return false;
2068
+ const target = controls.target;
2069
+ const dist = camera.position.distanceTo(target);
2070
+ if (dx || dy) {
2071
+ const step = Math.max(dist, 50) * PAN_SPEED;
2072
+ _panRight.setFromMatrixColumn(camera.matrix, 0);
2073
+ _panUp.setFromMatrixColumn(camera.matrix, 1);
2074
+ _panOffset.set(0, 0, 0).addScaledVector(_panRight, dx * step).addScaledVector(_panUp, dy * step);
2075
+ camera.position.add(_panOffset);
2076
+ target.add(_panOffset);
2077
+ }
2078
+ if (zoomIn || zoomOut) {
2079
+ const factor = zoomIn ? -PAN_ZOOM_FACTOR : PAN_ZOOM_FACTOR;
2080
+ _panOffset.copy(camera.position).sub(target);
2081
+ const newLen = Math.max(
2082
+ controls.minDistance,
2083
+ Math.min(controls.maxDistance, _panOffset.length() * (1 + factor))
2084
+ );
2085
+ _panOffset.setLength(newLen);
2086
+ camera.position.copy(target).add(_panOffset);
2087
+ }
2088
+ return true;
2089
+ };
2090
+ }
2052
2091
  function createOrbitUpdate(camera, controls, keys) {
2053
2092
  return function update() {
2054
2093
  let zoom = 0;
@@ -2148,8 +2187,13 @@ function setupKeyboardControls(camera, controls, container, mode = "fly", flySpe
2148
2187
  container.addEventListener("keyup", onKeyUp);
2149
2188
  container.addEventListener("blur", onBlur);
2150
2189
  let enabled = true;
2151
- const innerUpdate = mode === "fly" ? createFlyUpdate(camera, controls, keys, state) : createOrbitUpdate(camera, controls, keys);
2152
- const updateFn = () => enabled ? innerUpdate() : false;
2190
+ let currentMode = mode;
2191
+ const updaters = {
2192
+ fly: createFlyUpdate(camera, controls, keys, state),
2193
+ orbit: createOrbitUpdate(camera, controls, keys),
2194
+ pan: createPanUpdate(camera, controls, keys)
2195
+ };
2196
+ const updateFn = () => enabled ? updaters[currentMode]() : false;
2153
2197
  function cleanup() {
2154
2198
  container.removeEventListener("keydown", onKeyDown2);
2155
2199
  container.removeEventListener("keyup", onKeyUp);
@@ -2162,7 +2206,11 @@ function setupKeyboardControls(camera, controls, container, mode = "fly", flySpe
2162
2206
  enabled = on;
2163
2207
  if (!on) for (const k of Object.keys(keys)) keys[k] = false;
2164
2208
  }
2165
- return { update: updateFn, cleanup, setFlySpeed, setEnabled };
2209
+ function setMode(m2) {
2210
+ currentMode = m2;
2211
+ for (const k of Object.keys(keys)) keys[k] = false;
2212
+ }
2213
+ return { update: updateFn, cleanup, setFlySpeed, setEnabled, setMode };
2166
2214
  }
2167
2215
  function createScene(container, backgroundColor, style, rendererConfig) {
2168
2216
  const scene = new THREE__namespace.Scene();
@@ -2202,7 +2250,10 @@ function createScene(container, backgroundColor, style, rendererConfig) {
2202
2250
  const cameraMode = (rendererConfig == null ? void 0 : rendererConfig.cameraMode) ?? "fly";
2203
2251
  const keyboard = setupKeyboardControls(camera, controls, container, cameraMode, style.flySpeed ?? 1);
2204
2252
  controls.enableZoom = false;
2205
- const flags = { is2D: false };
2253
+ const flags = {
2254
+ is2D: false,
2255
+ keyboardMode3D: cameraMode
2256
+ };
2206
2257
  const _wheelDir = new THREE__namespace.Vector3();
2207
2258
  const onWheel = (e) => {
2208
2259
  if (flags.is2D) return;
@@ -2236,15 +2287,16 @@ function createScene(container, backgroundColor, style, rendererConfig) {
2236
2287
  function applyCameraMode2D(state, is2D) {
2237
2288
  const { controls, camera, keyboard } = state;
2238
2289
  state.flags.is2D = is2D;
2239
- controls.enableRotate = !is2D;
2290
+ controls.enableRotate = true;
2240
2291
  controls.enableZoom = is2D;
2241
2292
  controls.screenSpacePanning = is2D;
2242
- keyboard.setEnabled(!is2D);
2293
+ keyboard.setEnabled(true);
2294
+ keyboard.setMode(is2D ? "pan" : state.flags.keyboardMode3D);
2243
2295
  if (is2D) {
2244
2296
  controls.mouseButtons = {
2245
2297
  LEFT: THREE__namespace.MOUSE.PAN,
2246
2298
  MIDDLE: THREE__namespace.MOUSE.DOLLY,
2247
- RIGHT: THREE__namespace.MOUSE.PAN
2299
+ RIGHT: THREE__namespace.MOUSE.ROTATE
2248
2300
  };
2249
2301
  controls.touches = { ONE: THREE__namespace.TOUCH.PAN, TWO: THREE__namespace.TOUCH.DOLLY_PAN };
2250
2302
  const t = controls.target;
@@ -2622,11 +2674,12 @@ function setupInteraction(canvas, camera, controls, getNodesMesh, getNodes, call
2622
2674
  const dragPlane = new THREE__namespace.Plane();
2623
2675
  const dragIntersect = new THREE__namespace.Vector3();
2624
2676
  function onPointerDown2(e) {
2677
+ var _a;
2625
2678
  mouseDownPos = { x: e.clientX, y: e.clientY };
2626
2679
  if (container && document.activeElement !== container) {
2627
2680
  container.focus({ preventScroll: true });
2628
2681
  }
2629
- if (callbacks.onNodeDrag) {
2682
+ if (callbacks.onNodeDrag && (((_a = options == null ? void 0 : options.getNodeDragEnabled) == null ? void 0 : _a.call(options)) ?? true)) {
2630
2683
  const nodesMesh = getNodesMesh();
2631
2684
  if (nodesMesh) {
2632
2685
  const rect = canvas.getBoundingClientRect();
@@ -2938,6 +2991,7 @@ function NetworkGraph3DInner(props, ref) {
2938
2991
  nodeValueAccessor,
2939
2992
  visibleNodeIds,
2940
2993
  linkVisibility,
2994
+ enableNodeDrag = true,
2941
2995
  clickToFocus = true,
2942
2996
  hoverHighlight = false,
2943
2997
  hoverHighlightHops = 1
@@ -2978,6 +3032,7 @@ function NetworkGraph3DInner(props, ref) {
2978
3032
  const labelFormatterRef = react.useRef(labelFormatter);
2979
3033
  const highlightSetRef = react.useRef(null);
2980
3034
  const clickToFocusRef = react.useRef(clickToFocus);
3035
+ const enableNodeDragRef = react.useRef(enableNodeDrag);
2981
3036
  const hoverHighlightRef = react.useRef(hoverHighlight);
2982
3037
  const hoverHighlightHopsRef = react.useRef(hoverHighlightHops);
2983
3038
  const adjacencyMapRef = react.useRef(/* @__PURE__ */ new Map());
@@ -2999,6 +3054,7 @@ function NetworkGraph3DInner(props, ref) {
2999
3054
  selectedNodeIdRef.current = selectedNodeId;
3000
3055
  labelFormatterRef.current = labelFormatter;
3001
3056
  clickToFocusRef.current = clickToFocus;
3057
+ enableNodeDragRef.current = enableNodeDrag;
3002
3058
  hoverHighlightRef.current = hoverHighlight;
3003
3059
  hoverHighlightHopsRef.current = hoverHighlightHops;
3004
3060
  const is2D = (layoutProp == null ? void 0 : layoutProp.dimensions) === 2;
@@ -3181,7 +3237,8 @@ function NetworkGraph3DInner(props, ref) {
3181
3237
  },
3182
3238
  {
3183
3239
  getClickToFocus: () => clickToFocusRef.current,
3184
- getIs2D: () => is2DRef.current
3240
+ getIs2D: () => is2DRef.current,
3241
+ getNodeDragEnabled: () => enableNodeDragRef.current
3185
3242
  }
3186
3243
  );
3187
3244
  return () => {