@krainovsd/graph 0.10.0-rc1 → 0.10.0-rc2

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
@@ -54,7 +54,7 @@ const NODE_SETTINGS = {
54
54
  highlightByHoverNode: true,
55
55
  nodeRadiusFlexible: true,
56
56
  nodeSizeFlexible: true,
57
- textNodeDebug: false,
57
+ textNodeDebug: true,
58
58
  highlightByNodeNodeFading: true,
59
59
  highlightByNodeNodeColor: false,
60
60
  highlightByNodeNodeSizing: true,
@@ -70,7 +70,7 @@ const NODE_SETTINGS = {
70
70
  nodeSizeCoefficient: 5,
71
71
  nodeSizeFactor: 0.1,
72
72
  highlightByNodeNodeSizingAdditional: 0.5,
73
- highlightByLinkNodeSizingAdditionalCoefficient: 0.35,
73
+ highlightByNodeNodeSizingAdditionalCoefficient: 0.35,
74
74
  highlightByNodeNodeColorFadingMin: 0.15,
75
75
  highlightByNodeTextShiftXAdditional: 0,
76
76
  highlightByNodeTextShiftYAdditional: 2,
@@ -80,7 +80,7 @@ const NODE_SETTINGS = {
80
80
  highlightByNodeNodeFadingMin: 0.21,
81
81
  highlightByNodeTextFadingMin: 0.21,
82
82
  highlightByLinkNodeSizingAdditional: 0.5,
83
- highlightByNodeNodeSizingAdditionalCoefficient: 0.35,
83
+ highlightByLinkNodeSizingAdditionalCoefficient: 0.35,
84
84
  highlightByLinkNodeColorFadingMin: 0.15,
85
85
  highlightByLinkTextShiftXAdditional: 0,
86
86
  highlightByLinkTextShiftYAdditional: 2,
@@ -97,8 +97,8 @@ const NODE_OPTIONS = {
97
97
  borderRadius: 0,
98
98
  radius: 4,
99
99
  alpha: 1,
100
- textNodeXPadding: 5,
101
- textNodeYPadding: 3,
100
+ textNodeXPadding: 1,
101
+ textNodeYPadding: 1,
102
102
  borderWidth: 0.1,
103
103
  borderColor: "#000000FF",
104
104
  textAlpha: 1,
@@ -1265,13 +1265,15 @@ function nodeRadiusGetter({ radiusFlexible, radiusInitial, linkCount, radiusCoef
1265
1265
  return ((radiusFlexible && linkCount ? linkCount / radiusCoefficient : 0) * radiusFactor + radiusInitial);
1266
1266
  }
1267
1267
  function nodeSizeGetter({ heightInitial, linkCount, sizeCoefficient, sizeFactor, sizeFlexible, widthInitial, }) {
1268
- let widthCoefficient = 1;
1269
- let heightCoefficient = 1;
1268
+ let additionalSizeCoefficient = 1;
1270
1269
  if (sizeFlexible && linkCount != undefined) {
1271
- widthCoefficient += (linkCount / sizeCoefficient) * sizeFactor;
1272
- heightCoefficient += (linkCount / sizeCoefficient) * sizeFactor;
1270
+ additionalSizeCoefficient += (linkCount / sizeCoefficient) * sizeFactor;
1273
1271
  }
1274
- return { width: widthInitial * widthCoefficient, height: heightInitial * heightCoefficient };
1272
+ return {
1273
+ width: widthInitial * additionalSizeCoefficient,
1274
+ height: heightInitial * additionalSizeCoefficient,
1275
+ additionalSizeCoefficient,
1276
+ };
1275
1277
  }
1276
1278
  function nodeIdGetter(node) {
1277
1279
  return node.id;
@@ -2118,7 +2120,7 @@ function drawText({ context, id, textAlign, textColor, textFont, textStyle, text
2118
2120
  context.fillText(text, x, y);
2119
2121
  return;
2120
2122
  }
2121
- const lines = getTextLines({
2123
+ const { lines } = getTextLines({
2122
2124
  context,
2123
2125
  maxWidth,
2124
2126
  text,
@@ -2142,6 +2144,7 @@ function getTextLines({ context, textAlign, textColor, textFont, textStyle, text
2142
2144
  const lines = [];
2143
2145
  let lineWidth = 0;
2144
2146
  let line = "";
2147
+ let currentMaxSize = 0;
2145
2148
  for (const word of text.split(" ")) {
2146
2149
  const size = context.measureText(word).width;
2147
2150
  lineWidth += size + spaceWidth;
@@ -2150,7 +2153,10 @@ function getTextLines({ context, textAlign, textColor, textFont, textStyle, text
2150
2153
  continue;
2151
2154
  }
2152
2155
  if (lineWidth > maxWidth) {
2153
- lineWidth = 0;
2156
+ const initialSize = lineWidth - size - spaceWidth;
2157
+ if (initialSize > currentMaxSize)
2158
+ currentMaxSize = initialSize;
2159
+ lineWidth = size;
2154
2160
  lines.push(line);
2155
2161
  line = word;
2156
2162
  }
@@ -2161,7 +2167,9 @@ function getTextLines({ context, textAlign, textColor, textFont, textStyle, text
2161
2167
  }
2162
2168
  if (line !== "")
2163
2169
  lines.push(line);
2164
- return lines;
2170
+ if (lineWidth > currentMaxSize)
2171
+ currentMaxSize = lineWidth;
2172
+ return { lines, currentMaxSize };
2165
2173
  }
2166
2174
 
2167
2175
  function getDrawNode(nodeRenders, textRenders, state) {
@@ -2198,6 +2206,7 @@ function getDrawNode(nodeRenders, textRenders, state) {
2198
2206
  let textShiftY = nodeOptions.textShiftY;
2199
2207
  let textWeight = nodeOptions.textWeight;
2200
2208
  let textWidth = nodeOptions.textWidth;
2209
+ let sizeCoefficient = 1;
2201
2210
  /** Node Highlight */
2202
2211
  if (this.highlightedNeighbors && this.highlightedNode) {
2203
2212
  /** Not highlighted */
@@ -2229,12 +2238,11 @@ function getDrawNode(nodeRenders, textRenders, state) {
2229
2238
  if (this.nodeSettings.highlightByNodeNodeSizing && nodeOptions.shape === "circle") {
2230
2239
  radiusInitial = animationByProgress(radiusInitial, this.nodeSettings.highlightByNodeNodeSizingAdditional, this.highlightProgress);
2231
2240
  }
2232
- if (this.nodeSettings.highlightByLinkNodeSizing &&
2241
+ if (this.nodeSettings.highlightByNodeNodeSizing &&
2233
2242
  (nodeOptions.shape === "square" || nodeOptions.shape === "text")) {
2234
- const widthCoefficient = animationByProgress(1, this.nodeSettings.highlightByNodeNodeSizingAdditionalCoefficient, this.highlightProgress);
2235
- const heightCoefficient = animationByProgress(1, this.nodeSettings.highlightByNodeNodeSizingAdditionalCoefficient, this.highlightProgress);
2236
- widthInitial *= widthCoefficient;
2237
- heightInitial *= heightCoefficient;
2243
+ sizeCoefficient = animationByProgress(sizeCoefficient, this.nodeSettings.highlightByNodeNodeSizingAdditionalCoefficient, this.highlightProgress);
2244
+ widthInitial *= sizeCoefficient;
2245
+ heightInitial *= sizeCoefficient;
2238
2246
  }
2239
2247
  if (this.nodeSettings.highlightByNodeTextSizing) {
2240
2248
  textSize = animationByProgress(textSize, this.nodeSettings.highlightByNodeTextSizingAdditional, this.highlightProgress);
@@ -2281,10 +2289,9 @@ function getDrawNode(nodeRenders, textRenders, state) {
2281
2289
  (nodeOptions.shape === "square" ||
2282
2290
  nodeOptions.shape === "text" ||
2283
2291
  nodeOptions.shape === "icon")) {
2284
- const widthCoefficient = animationByProgress(1, this.nodeSettings.highlightByLinkNodeSizingAdditionalCoefficient, this.highlightProgress);
2285
- const heightCoefficient = animationByProgress(1, this.nodeSettings.highlightByLinkNodeSizingAdditionalCoefficient, this.highlightProgress);
2286
- widthInitial *= widthCoefficient;
2287
- heightInitial *= heightCoefficient;
2292
+ sizeCoefficient = animationByProgress(sizeCoefficient, this.nodeSettings.highlightByNodeNodeSizingAdditionalCoefficient, this.highlightProgress);
2293
+ widthInitial *= sizeCoefficient;
2294
+ heightInitial *= sizeCoefficient;
2288
2295
  }
2289
2296
  if (this.nodeSettings.highlightByLinkTextSizing) {
2290
2297
  textSize = animationByProgress(textSize, this.nodeSettings.highlightByLinkTextSizingAdditional, this.highlightProgress);
@@ -2308,7 +2315,7 @@ function getDrawNode(nodeRenders, textRenders, state) {
2308
2315
  /** Flex size */
2309
2316
  let height = heightInitial;
2310
2317
  let width = widthInitial;
2311
- if (nodeOptions.shape === "square" || nodeOptions.shape === "text") {
2318
+ if (nodeOptions.shape === "square") {
2312
2319
  const size = nodeSizeGetter({
2313
2320
  heightInitial,
2314
2321
  widthInitial,
@@ -2320,27 +2327,53 @@ function getDrawNode(nodeRenders, textRenders, state) {
2320
2327
  width = size.width;
2321
2328
  height = size.height;
2322
2329
  }
2330
+ if (nodeOptions.shape === "text") {
2331
+ width = nodeOptions.width;
2332
+ const size = nodeSizeGetter({
2333
+ heightInitial,
2334
+ widthInitial: width,
2335
+ linkCount: node.linkCount,
2336
+ sizeCoefficient: this.nodeSettings.nodeSizeCoefficient,
2337
+ sizeFactor: this.nodeSettings.nodeSizeFactor,
2338
+ sizeFlexible: this.nodeSettings.nodeSizeFlexible,
2339
+ });
2340
+ textSize *= size.additionalSizeCoefficient;
2341
+ }
2342
+ /** Size by text in textNode */
2323
2343
  if (nodeOptions.shape === "text" && nodeOptions.text) {
2324
- const lines = this.cachedNodeText[node.id] ??
2325
- getTextLines({
2344
+ textWidth = width;
2345
+ let lines;
2346
+ let textNodeParameters;
2347
+ const cachedLines = this.cachedNodeText[node.id];
2348
+ const cachedTextNodeParameters = this.cachedTextNodeParameters[node.id];
2349
+ if (cachedLines != undefined && cachedTextNodeParameters != undefined) {
2350
+ lines = cachedLines;
2351
+ textNodeParameters = cachedTextNodeParameters;
2352
+ }
2353
+ else {
2354
+ const textInfo = getTextLines({
2326
2355
  context: this.context,
2327
2356
  text: nodeOptions.text,
2328
2357
  textAlign: nodeOptions.textAlign,
2329
2358
  textColor: nodeOptions.textColor,
2330
2359
  textFont: nodeOptions.textFont,
2331
2360
  textSize,
2332
- maxWidth: width,
2361
+ maxWidth: textWidth,
2333
2362
  textStyle: nodeOptions.textStyle,
2334
2363
  textWeight,
2335
2364
  });
2336
- if (!this.cachedNodeText[node.id]) {
2365
+ textNodeParameters = [textInfo.currentMaxSize, textSize];
2366
+ lines = textInfo.lines;
2337
2367
  this.cachedNodeText[node.id] = lines;
2368
+ this.cachedTextNodeParameters[node.id] = textNodeParameters;
2338
2369
  }
2370
+ const textSizeCoefficient = textSize / textNodeParameters[1];
2371
+ const maxSize = textNodeParameters[0] * textSizeCoefficient;
2339
2372
  height =
2340
2373
  lines.length * textSize +
2341
2374
  (lines.length - 1) * nodeOptions.textGap +
2342
2375
  nodeOptions.textNodeYPadding;
2343
- width += nodeOptions.textNodeXPadding;
2376
+ width = maxSize + nodeOptions.textNodeXPadding;
2344
2377
  }
2345
2378
  /** Node parameters */
2346
2379
  node._radius = radius;
@@ -2397,7 +2430,7 @@ function getDrawNode(nodeRenders, textRenders, state) {
2397
2430
  textSize,
2398
2431
  x: node.x,
2399
2432
  y: node.y + textSize / 4 - (lines.length - 1) * (textSize / 2),
2400
- maxWidth: widthInitial,
2433
+ maxWidth: textWidth,
2401
2434
  textStyle: nodeOptions.textStyle,
2402
2435
  textWeight,
2403
2436
  textGap: nodeOptions.textGap,
@@ -2928,7 +2961,7 @@ function initCollideForce(forceUpdate) {
2928
2961
  maxWidth: width,
2929
2962
  textStyle: nodeOptions.textStyle,
2930
2963
  textWeight: nodeOptions.textWeight,
2931
- });
2964
+ }).lines;
2932
2965
  height =
2933
2966
  lines.length * nodeOptions.textSize +
2934
2967
  (lines.length - 1) * nodeOptions.textGap +
@@ -3013,6 +3046,7 @@ class GraphCanvas {
3013
3046
  draw;
3014
3047
  eventAbortController;
3015
3048
  cachedNodeText = {};
3049
+ cachedTextNodeParameters = {};
3016
3050
  linkOptionsCache = {};
3017
3051
  nodeOptionsCache = {};
3018
3052
  isDragging = false;
@@ -3104,6 +3138,7 @@ class GraphCanvas {
3104
3138
  if (options.nodeSettings) {
3105
3139
  this.nodeSettings = nodeSettingsGetter(options.nodeSettings, this.nodeSettings);
3106
3140
  this.cachedNodeText = {};
3141
+ this.cachedTextNodeParameters = {};
3107
3142
  this.nodeOptionsCache = {};
3108
3143
  initCollideForce.call(this, true);
3109
3144
  }
@@ -3120,6 +3155,7 @@ class GraphCanvas {
3120
3155
  this.nodeOptionsCache = {};
3121
3156
  this.linkOptionsCache = {};
3122
3157
  this.cachedNodeText = {};
3158
+ this.cachedTextNodeParameters = {};
3123
3159
  }
3124
3160
  tick() {
3125
3161
  if (!this.simulationWorking && !this.highlightWorking)
@@ -3151,7 +3187,7 @@ class GraphCanvas {
3151
3187
  }
3152
3188
  this.clearHTMLElements();
3153
3189
  this.clearState();
3154
- this.clearDataDependencies();
3190
+ this.clearCache();
3155
3191
  }
3156
3192
  clearHTMLElements() {
3157
3193
  this.root.replaceChildren();
@@ -3177,13 +3213,8 @@ class GraphCanvas {
3177
3213
  this.highlightWorking = false;
3178
3214
  this.highlightDrawing = false;
3179
3215
  }
3180
- clearDataDependencies() {
3181
- this.cachedNodeText = {};
3182
- this.nodeOptionsCache = {};
3183
- this.linkOptionsCache = {};
3184
- }
3185
3216
  updateData(alpha) {
3186
- this.clearDataDependencies();
3217
+ this.clearCache();
3187
3218
  if (this.simulation) {
3188
3219
  initCollideForce.call(this, false);
3189
3220
  this.simulation