@krainovsd/graph 0.14.0-beta.4 → 0.14.0-beta.6

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/lib/cjs/index.cjs CHANGED
@@ -315,14 +315,22 @@ function computeGraphBounds(nodes) {
315
315
  }
316
316
 
317
317
  function pointerGetter(mouseEvent, areaRect, areaTransform) {
318
- const clientX = "clientX" in mouseEvent
319
- ? mouseEvent.clientX
320
- : (mouseEvent.touches[0]?.clientX ?? mouseEvent.changedTouches[0]?.clientX);
321
- const clientY = "clientX" in mouseEvent
322
- ? mouseEvent.clientY
323
- : (mouseEvent.touches[0]?.clientY ?? mouseEvent.changedTouches[0]?.clientY);
324
- const px = (clientX - areaRect.left - areaTransform.x) / areaTransform.k;
325
- const py = (clientY - areaRect.top - areaTransform.y) / areaTransform.k;
318
+ let localX;
319
+ let localY;
320
+ if ("offsetX" in mouseEvent) {
321
+ localX = mouseEvent.offsetX;
322
+ localY = mouseEvent.offsetY;
323
+ }
324
+ else {
325
+ if (!areaRect)
326
+ return [0, 0];
327
+ const clientX = mouseEvent.touches[0]?.clientX ?? mouseEvent.changedTouches[0]?.clientX;
328
+ const clientY = mouseEvent.touches[0]?.clientY ?? mouseEvent.changedTouches[0]?.clientY;
329
+ localX = clientX - areaRect.left;
330
+ localY = clientY - areaRect.top;
331
+ }
332
+ const px = (localX - areaTransform.x) / areaTransform.k;
333
+ const py = (localY - areaTransform.y) / areaTransform.k;
326
334
  return [px, py];
327
335
  }
328
336
 
@@ -422,10 +430,7 @@ function linkIterationExtractor(link, i, links, state, option, optionConstantGet
422
430
  return customOptions;
423
431
  }
424
432
 
425
- function nodeByPointerGetter({ areaRect, areaTransform, mouseEvent, nodes, }) {
426
- if (!areaRect)
427
- return undefined;
428
- const [pointerX, pointerY] = pointerGetter(mouseEvent, areaRect, areaTransform);
433
+ function nodeByPointerGetter({ pointerX, pointerY, nodes, }) {
429
434
  return d3Array.greatest(nodes, (node) => {
430
435
  if (isOverlapsNode(node, pointerX, pointerY, undefined))
431
436
  return node.index;
@@ -825,10 +830,7 @@ function approximateQuadraticBezierLength(x0, y0, x1, y1, x2, y2) {
825
830
  return (a + b + c) / 2;
826
831
  }
827
832
 
828
- function linkByPointerGetter({ areaRect, areaTransform, mouseEvent, links, linkHoverExtraZone, curve, }) {
829
- if (!areaRect)
830
- return undefined;
831
- const [pointerX, pointerY] = pointerGetter(mouseEvent, areaRect, areaTransform);
833
+ function linkByPointerGetter({ pointerX, pointerY, links, linkHoverExtraZone, curve, }) {
832
834
  return d3Array.greatest(links, (link) => {
833
835
  if (!jsHelpers.isObject(link.source) ||
834
836
  !jsHelpers.isObject(link.target) ||
@@ -1316,7 +1318,6 @@ function initArea() {
1316
1318
  if (!this.area)
1317
1319
  throw new Error("couldn't create canvas");
1318
1320
  this.container.appendChild(this.area);
1319
- this.areaRect = this.area.getBoundingClientRect();
1320
1321
  this.context = this.area.getContext("2d");
1321
1322
  if (!this.context)
1322
1323
  throw new Error("couldn't create canvas context");
@@ -1332,10 +1333,8 @@ function initDnd() {
1332
1333
  if (this.listeners.onDragSubject) {
1333
1334
  return this.listeners.onDragSubject.call(this, event);
1334
1335
  }
1335
- if (!this.areaRect)
1336
- return;
1337
1336
  const mouseEvent = event.sourceEvent;
1338
- const [pointerX, pointerY] = pointerGetter(mouseEvent, this.areaRect, this.areaTransform);
1337
+ const [pointerX, pointerY] = this.getPointerAreaPosition(mouseEvent);
1339
1338
  return d3Array.greatest(this.nodes, (node) => {
1340
1339
  if (!node.x || !node.y || (jsHelpers.isBoolean(node.drag) && !node.drag))
1341
1340
  return undefined;
@@ -1351,10 +1350,8 @@ function initDnd() {
1351
1350
  if (this.simulation)
1352
1351
  this.simulation.alphaTarget(0.3).restart();
1353
1352
  }
1354
- if (!this.areaRect)
1355
- return;
1356
1353
  const mouseEvent = event.sourceEvent;
1357
- const [pointerX, pointerY] = pointerGetter(mouseEvent, this.areaRect, this.areaTransform);
1354
+ const [pointerX, pointerY] = this.getPointerAreaPosition(mouseEvent);
1358
1355
  if (this._translateExtent) {
1359
1356
  event.subject.fx = Math.max(this._translateExtent[0][0], Math.min(this._translateExtent[1][0], pointerX));
1360
1357
  event.subject.fy = Math.max(this._translateExtent[0][1], Math.min(this._translateExtent[1][1], pointerY));
@@ -1370,8 +1367,8 @@ function initDnd() {
1370
1367
  if (!event.active && this.simulation)
1371
1368
  this.simulation.alphaTarget(0);
1372
1369
  const sourceEvent = event.sourceEvent;
1373
- if (sourceEvent.altKey && this.areaRect) {
1374
- const [pointerX, pointerY] = pointerGetter(sourceEvent, this.areaRect, this.areaTransform);
1370
+ if (sourceEvent.altKey) {
1371
+ const [pointerX, pointerY] = this.getPointerAreaPosition(sourceEvent);
1375
1372
  if (this._translateExtent) {
1376
1373
  event.subject.fx = Math.max(this._translateExtent[0][0], Math.min(this._translateExtent[1][0], pointerX));
1377
1374
  event.subject.fy = Math.max(this._translateExtent[0][1], Math.min(this._translateExtent[1][1], pointerY));
@@ -2132,10 +2129,10 @@ function initPointer() {
2132
2129
  let highlightNode = true;
2133
2130
  let highlightLink = true;
2134
2131
  if (checkHighlightNode) {
2132
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2135
2133
  currentNode = nodeByPointerGetter({
2136
- areaRect: this.areaRect,
2137
- areaTransform: this.areaTransform,
2138
- mouseEvent: event,
2134
+ pointerX,
2135
+ pointerY,
2139
2136
  nodes: this.nodes,
2140
2137
  });
2141
2138
  if (currentNode?.highlight != undefined)
@@ -2145,11 +2142,11 @@ function initPointer() {
2145
2142
  this.area.style.cursor = "pointer";
2146
2143
  }
2147
2144
  else if (checkHighlightLink) {
2145
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2148
2146
  currentLink = linkByPointerGetter({
2147
+ pointerX,
2148
+ pointerY,
2149
2149
  linkHoverExtraZone: this.highlightSettings.linkHoverExtraZone,
2150
- areaRect: this.areaRect,
2151
- areaTransform: this.areaTransform,
2152
- mouseEvent: event,
2153
2150
  links: this.links,
2154
2151
  curve: this.linkSettings.curve,
2155
2152
  });
@@ -2168,19 +2165,20 @@ function initPointer() {
2168
2165
  this.animateHighlight(highlightNode ? currentNode : undefined, highlightLink ? currentLink : undefined);
2169
2166
  if (!this.listeners.onMove)
2170
2167
  return;
2171
- if (!currentNode && !checkHighlightNode)
2168
+ if (!currentNode && !checkHighlightNode) {
2169
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2172
2170
  currentNode = nodeByPointerGetter({
2173
- areaRect: this.areaRect,
2174
- areaTransform: this.areaTransform,
2175
- mouseEvent: event,
2171
+ pointerX,
2172
+ pointerY,
2176
2173
  nodes: this.nodes,
2177
2174
  });
2175
+ }
2178
2176
  if (!currentNode && (!checkHighlightNode || (!checkHighlightLink && !currentLink))) {
2177
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2179
2178
  currentLink = linkByPointerGetter({
2179
+ pointerX,
2180
+ pointerY,
2180
2181
  linkHoverExtraZone: this.highlightSettings.linkHoverExtraZone,
2181
- areaRect: this.areaRect,
2182
- areaTransform: this.areaTransform,
2183
- mouseEvent: event,
2184
2182
  links: this.links,
2185
2183
  curve: this.linkSettings.curve,
2186
2184
  });
@@ -2194,18 +2192,18 @@ function initPointer() {
2194
2192
  !("button" in event) ||
2195
2193
  event.button !== 1)
2196
2194
  return;
2195
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2197
2196
  const currentNode = nodeByPointerGetter({
2198
- areaRect: this.areaRect,
2199
- areaTransform: this.areaTransform,
2200
- mouseEvent: event,
2197
+ pointerX,
2198
+ pointerY,
2201
2199
  nodes: this.nodes,
2202
2200
  });
2203
2201
  if (!currentNode) {
2202
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2204
2203
  const currentLink = linkByPointerGetter({
2204
+ pointerX,
2205
+ pointerY,
2205
2206
  linkHoverExtraZone: this.highlightSettings.linkHoverExtraZone,
2206
- areaRect: this.areaRect,
2207
- areaTransform: this.areaTransform,
2208
- mouseEvent: event,
2209
2207
  links: this.links,
2210
2208
  curve: this.linkSettings.curve,
2211
2209
  });
@@ -2216,18 +2214,18 @@ function initPointer() {
2216
2214
  function onRightClick(event) {
2217
2215
  if (!this.listeners.onContextMenu)
2218
2216
  return;
2217
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2219
2218
  const currentNode = nodeByPointerGetter({
2220
- areaRect: this.areaRect,
2221
- areaTransform: this.areaTransform,
2222
- mouseEvent: event,
2219
+ pointerX,
2220
+ pointerY,
2223
2221
  nodes: this.nodes,
2224
2222
  });
2225
2223
  if (!currentNode) {
2224
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2226
2225
  const currentLink = linkByPointerGetter({
2226
+ pointerX,
2227
+ pointerY,
2227
2228
  linkHoverExtraZone: this.highlightSettings.linkHoverExtraZone,
2228
- areaRect: this.areaRect,
2229
- areaTransform: this.areaTransform,
2230
- mouseEvent: event,
2231
2229
  links: this.links,
2232
2230
  curve: this.linkSettings.curve,
2233
2231
  });
@@ -2238,18 +2236,18 @@ function initPointer() {
2238
2236
  function onDoubleClick(event) {
2239
2237
  if (!this.listeners.onDoubleClick)
2240
2238
  return;
2239
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2241
2240
  const currentNode = nodeByPointerGetter({
2242
- areaRect: this.areaRect,
2243
- areaTransform: this.areaTransform,
2244
- mouseEvent: event,
2241
+ pointerX,
2242
+ pointerY,
2245
2243
  nodes: this.nodes,
2246
2244
  });
2247
2245
  if (!currentNode) {
2246
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2248
2247
  const currentLink = linkByPointerGetter({
2248
+ pointerX,
2249
+ pointerY,
2249
2250
  linkHoverExtraZone: this.highlightSettings.linkHoverExtraZone,
2250
- areaRect: this.areaRect,
2251
- areaTransform: this.areaTransform,
2252
- mouseEvent: event,
2253
2251
  links: this.links,
2254
2252
  curve: this.linkSettings.curve,
2255
2253
  });
@@ -2260,18 +2258,18 @@ function initPointer() {
2260
2258
  function onClick(event) {
2261
2259
  if (this.isDragging || !this.listeners.onClick || ("button" in event && event.button !== 0))
2262
2260
  return;
2261
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2263
2262
  const currentNode = nodeByPointerGetter({
2264
- areaRect: this.areaRect,
2265
- areaTransform: this.areaTransform,
2266
- mouseEvent: event,
2263
+ pointerX,
2264
+ pointerY,
2267
2265
  nodes: this.nodes,
2268
2266
  });
2269
2267
  if (!currentNode) {
2268
+ const [pointerX, pointerY] = this.getPointerAreaPosition(event);
2270
2269
  const currentLink = linkByPointerGetter({
2270
+ pointerX,
2271
+ pointerY,
2271
2272
  linkHoverExtraZone: this.highlightSettings.linkHoverExtraZone,
2272
- areaRect: this.areaRect,
2273
- areaTransform: this.areaTransform,
2274
- mouseEvent: event,
2275
2273
  links: this.links,
2276
2274
  curve: this.linkSettings.curve,
2277
2275
  });
@@ -2330,13 +2328,13 @@ function initResize() {
2330
2328
  });
2331
2329
  document.addEventListener("visibilitychange", () => {
2332
2330
  if (!document.hidden)
2333
- this.updateRect();
2331
+ this.updateSize();
2334
2332
  }, { signal: abortController.signal });
2335
2333
  observer.observe(this.area);
2336
2334
  }
2337
2335
 
2338
2336
  function initSelection() {
2339
- if (!this.area || !this.areaRect)
2337
+ if (!this.area)
2340
2338
  throw new Error("bad init data");
2341
2339
  this.isSelecting = false;
2342
2340
  this.selectionRect = null;
@@ -2346,11 +2344,11 @@ function initSelection() {
2346
2344
  function onMouseDown(event) {
2347
2345
  if (event.button !== 0 || !event.shiftKey)
2348
2346
  return;
2349
- if (!this.areaRect || !this.area)
2347
+ if (!this.area)
2350
2348
  return;
2351
2349
  event.stopPropagation();
2352
2350
  event.preventDefault();
2353
- const [startX, startY] = pointerGetter(event, this.areaRect, this.areaTransform);
2351
+ const [startX, startY] = this.getPointerAreaPosition(event);
2354
2352
  this.isSelecting = true;
2355
2353
  localSelection = true;
2356
2354
  this.selectionRect = { x1: startX, y1: startY, x2: startX, y2: startY };
@@ -2372,10 +2370,10 @@ function initSelection() {
2372
2370
  }, { signal: controller.signal });
2373
2371
  }
2374
2372
  function onMouseMove(event) {
2375
- if (!this.isSelecting || !this.selectionRect || !this.areaRect)
2373
+ if (!this.isSelecting || !this.selectionRect)
2376
2374
  return;
2377
2375
  event.stopPropagation();
2378
- const [x, y] = pointerGetter(event, this.areaRect, this.areaTransform);
2376
+ const [x, y] = this.getPointerAreaPosition(event);
2379
2377
  this.selectionRect.x2 = x;
2380
2378
  this.selectionRect.y2 = y;
2381
2379
  const rect = normalizeRect(this.selectionRect);
@@ -2964,7 +2962,6 @@ class GraphCanvas {
2964
2962
  simulation;
2965
2963
  areaTransform = d3Zoom.zoomIdentity;
2966
2964
  _translateExtent;
2967
- areaRect;
2968
2965
  draw;
2969
2966
  eventAbortController;
2970
2967
  cachedNodeText = [];
@@ -2984,6 +2981,10 @@ class GraphCanvas {
2984
2981
  _zoomAnimating = false;
2985
2982
  isSelecting = false;
2986
2983
  selectionRect = null;
2984
+ areaRect;
2985
+ // protected get areaRect() {
2986
+ // return this.area?.getBoundingClientRect();
2987
+ // }
2987
2988
  get simulationWorking() {
2988
2989
  const simulationAlpha = this.simulation?.alpha?.() ?? 0;
2989
2990
  const simulationAlphaMin = this.simulation?.alphaMin?.() ?? 0;
@@ -3141,7 +3142,7 @@ class GraphCanvas {
3141
3142
  this.height = height;
3142
3143
  this.area.width = this.dpi * this.width;
3143
3144
  this.area.height = this.dpi * this.height;
3144
- this.areaRect = this.area.getBoundingClientRect();
3145
+ this.updateRect();
3145
3146
  this.context = this.area.getContext("2d");
3146
3147
  if (!this.context)
3147
3148
  throw new Error("couldn't create canvas context");
@@ -3245,6 +3246,31 @@ class GraphCanvas {
3245
3246
  this.clearState();
3246
3247
  this.clearCache(true);
3247
3248
  };
3249
+ getPointerAreaPosition = (event) => {
3250
+ let localX;
3251
+ let localY;
3252
+ if ("offsetX" in event) {
3253
+ // localX = event.offsetX;
3254
+ // localY = event.offsetY;
3255
+ const rect = this.areaRect;
3256
+ if (!rect)
3257
+ return [0, 0];
3258
+ localX = event.clientX - rect.left;
3259
+ localY = event.clientY - rect.top;
3260
+ }
3261
+ else {
3262
+ const rect = this.areaRect;
3263
+ if (!rect)
3264
+ return [0, 0];
3265
+ const clientX = event.touches[0]?.clientX ?? event.changedTouches[0]?.clientX;
3266
+ const clientY = event.touches[0]?.clientY ?? event.changedTouches[0]?.clientY;
3267
+ localX = clientX - rect.left;
3268
+ localY = clientY - rect.top;
3269
+ }
3270
+ const px = (localX - this.areaTransform.x) / this.areaTransform.k;
3271
+ const py = (localY - this.areaTransform.y) / this.areaTransform.k;
3272
+ return [px, py];
3273
+ };
3248
3274
  clearHTMLElements = () => {
3249
3275
  this.root.replaceChildren();
3250
3276
  this.area = undefined;