@seanyeh77/ui 0.7.0 → 0.7.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.js CHANGED
@@ -4141,6 +4141,50 @@ function labelWidth(text) {
4141
4141
  return w + c.measureText(t[0] === "`" ? t.slice(1, -1) : t).width;
4142
4142
  }, 0);
4143
4143
  }
4144
+ const GROW = 24;
4145
+ const GROW_MAX = 144;
4146
+ /** The label on 1 line, then wrapped onto 2, 3 and 4 lines as evenly as it goes. Breaks between Chinese characters
4147
+ * and at spaces; never inside `code`, a word or a number, between a number and the word after it, before a closing
4148
+ * mark or after an opening one. */
4149
+ function wraps(text) {
4150
+ const atoms = [];
4151
+ for (const t of text.match(/`[^`]+`|[A-Za-z0-9_./:#@-]+|\s+|[\s\S]/gu) ?? []) {
4152
+ const prev = atoms[atoms.length - 1];
4153
+ const unit = /\d\s*$/.test(prev ?? "") && !/^[A-Za-z0-9_]/.test(t);
4154
+ if (prev != null && (unit || /^[)」』、,。:;!?)\]]/.test(t) || /[(「『([]$/.test(prev))) atoms[atoms.length - 1] = prev + t;
4155
+ else atoms.push(t);
4156
+ }
4157
+ const fill = (max) => {
4158
+ const lines = [];
4159
+ let cur = "";
4160
+ for (const a of atoms) {
4161
+ if (/^\s+$/.test(a)) {
4162
+ if (cur) cur += a;
4163
+ continue;
4164
+ }
4165
+ if (cur && labelWidth(cur.trimEnd() + a) > max) {
4166
+ lines.push(cur.trimEnd());
4167
+ cur = a;
4168
+ } else cur += a;
4169
+ }
4170
+ if (cur.trim()) lines.push(cur.trimEnd());
4171
+ return lines;
4172
+ };
4173
+ const full = labelWidth(text);
4174
+ const out = [[text]];
4175
+ for (let n = 2; n <= 4; n++) {
4176
+ let lo = Math.max(...atoms.filter((a) => a.trim()).map(labelWidth)) - 1;
4177
+ let hi = full;
4178
+ while (hi - lo > 1) {
4179
+ const mid = (lo + hi) / 2;
4180
+ if (fill(mid).length <= n) hi = mid;
4181
+ else lo = mid;
4182
+ }
4183
+ const lines = fill(hi);
4184
+ if (lines.length > out[out.length - 1].length) out.push(lines);
4185
+ }
4186
+ return out;
4187
+ }
4144
4188
  function labelParts(text) {
4145
4189
  return text.split(SPLIT).filter(Boolean).map((t, i) => t[0] === "`" || /^\d/.test(t) ? /* @__PURE__ */ jsx("span", {
4146
4190
  className: "ui-lg-code",
@@ -4199,7 +4243,8 @@ function LayerGraph(p) {
4199
4243
  const layers = [...byLayer.keys()].sort((a, b) => a - b);
4200
4244
  const cols = Math.max(1, ...layers.map((l) => byLayer.get(l)?.length ?? 0));
4201
4245
  let W = 32 + cols * NW + (cols - 1) * COL;
4202
- const H = 32 + layers.length * NH + Math.max(0, layers.length - 1) * GAP;
4246
+ const gaps = layers.map(() => GAP);
4247
+ let H = 0;
4203
4248
  const pos = {};
4204
4249
  const layerIndex = {};
4205
4250
  layers.forEach((l, k) => {
@@ -4208,11 +4253,16 @@ function LayerGraph(p) {
4208
4253
  row.forEach((n, i) => {
4209
4254
  pos[n.id] = {
4210
4255
  x: off + i * (NW + COL),
4211
- y: PAD + k * 116
4256
+ y: 0
4212
4257
  };
4213
4258
  layerIndex[n.id] = k;
4214
4259
  });
4215
4260
  });
4261
+ const rowTop = (k) => PAD + k * NH + gaps.slice(0, k).reduce((a, b) => a + b, 0);
4262
+ const placeRows = () => {
4263
+ for (const id in pos) pos[id].y = rowTop(layerIndex[id]);
4264
+ H = rowTop(layers.length - 1) + NH + PAD;
4265
+ };
4216
4266
  const maxSize = Math.max(1, ...main.map((n) => sizeOf(n) || 0));
4217
4267
  const maxCount = Math.max(1, ...edges.map((e) => e.count || 0));
4218
4268
  const isUp = (e) => !!pos[e.from] && !!pos[e.to] && layerIndex[e.to] < layerIndex[e.from];
@@ -4296,277 +4346,370 @@ function LayerGraph(p) {
4296
4346
  if (next && sel != null) p.onSelect?.(null);
4297
4347
  pickEdge?.(next);
4298
4348
  };
4299
- const paths = [];
4300
- const labels = [];
4301
- const marks = [];
4302
- const runs = [];
4303
- const segs = [];
4304
- const arrows = [];
4305
- const hitPaths = [];
4306
- edges.forEach((e, idx) => {
4307
- const a = pos[e.from];
4308
- const b = pos[e.to];
4309
- if (!a || !b) return;
4310
- const r = related(e);
4311
- const dim = focus && !r;
4312
- const picked = e === selEdge;
4313
- const hovered = !!pickEdge && hover === edgeId(e) && !r;
4314
- const up = isUp(e);
4315
- let pts;
4316
- let tx;
4317
- let ty;
4318
- let arrow;
4319
- let inn = 1;
4320
- if (up) {
4321
- const lx = laneX(e);
4322
- const L = onLeft(e);
4323
- const si = upOut[e.from].indexOf(e);
4324
- const sn = upOut[e.from].length;
4325
- const ti = upIn[e.to].indexOf(e);
4326
- const tn = upIn[e.to].length;
4327
- const start = [];
4328
- let y0;
4329
- if (L ? leftmost(e.from) : rightmost(e.from)) {
4330
- y0 = Math.round(a.y + NH / 2 + (si - (sn - 1) / 2) * 10);
4331
- start.push([L ? a.x : a.x + NW, y0], [lx, y0]);
4332
- } else {
4333
- const px = L ? a.x + 20 + si * 10 : a.x + NW - 20 - si * 10;
4334
- y0 = a.y - 12 - si * 6;
4335
- start.push([px, a.y], [px, y0], [lx, y0]);
4336
- }
4337
- let y1;
4338
- if (L || rightmost(e.to)) {
4339
- y1 = Math.round(b.y + NH / 2 + (ti - (tn - 1) / 2) * 10);
4340
- tx = L ? b.x : b.x + NW;
4341
- ty = y1;
4342
- const back = L ? tx - 9 : tx + 9;
4343
- pts = [
4344
- ...start,
4345
- [lx, y1],
4346
- [back, y1]
4347
- ];
4348
- arrow = `M${back} ${ty - 5} L${back} ${ty + 5} L${tx} ${ty} Z`;
4349
- arrows.push([
4350
- Math.min(tx, back),
4351
- Math.max(tx, back),
4352
- ty - 5,
4353
- ty + 5
4354
- ]);
4349
+ const draw = () => {
4350
+ const paths = [];
4351
+ const labels = [];
4352
+ const need = /* @__PURE__ */ new Set();
4353
+ const marks = [];
4354
+ const runs = [];
4355
+ const segs = [];
4356
+ const arrows = [];
4357
+ const hitPaths = [];
4358
+ edges.forEach((e, idx) => {
4359
+ const a = pos[e.from];
4360
+ const b = pos[e.to];
4361
+ if (!a || !b) return;
4362
+ const r = related(e);
4363
+ const dim = focus && !r;
4364
+ const picked = e === selEdge;
4365
+ const hovered = !!pickEdge && hover === edgeId(e) && !r;
4366
+ const up = isUp(e);
4367
+ let pts;
4368
+ let tx;
4369
+ let ty;
4370
+ let arrow;
4371
+ let inn = 1;
4372
+ if (up) {
4373
+ const lx = laneX(e);
4374
+ const L = onLeft(e);
4375
+ const si = upOut[e.from].indexOf(e);
4376
+ const sn = upOut[e.from].length;
4377
+ const ti = upIn[e.to].indexOf(e);
4378
+ const tn = upIn[e.to].length;
4379
+ const start = [];
4380
+ let y0;
4381
+ if (L ? leftmost(e.from) : rightmost(e.from)) {
4382
+ y0 = Math.round(a.y + NH / 2 + (si - (sn - 1) / 2) * 10);
4383
+ start.push([L ? a.x : a.x + NW, y0], [lx, y0]);
4384
+ } else {
4385
+ const px = L ? a.x + 20 + si * 10 : a.x + NW - 20 - si * 10;
4386
+ y0 = a.y - 12 - si * 6;
4387
+ start.push([px, a.y], [px, y0], [lx, y0]);
4388
+ }
4389
+ let y1;
4390
+ if (L || rightmost(e.to)) {
4391
+ y1 = Math.round(b.y + NH / 2 + (ti - (tn - 1) / 2) * 10);
4392
+ tx = L ? b.x : b.x + NW;
4393
+ ty = y1;
4394
+ const back = L ? tx - 9 : tx + 9;
4395
+ pts = [
4396
+ ...start,
4397
+ [lx, y1],
4398
+ [back, y1]
4399
+ ];
4400
+ arrow = `M${back} ${ty - 5} L${back} ${ty + 5} L${tx} ${ty} Z`;
4401
+ arrows.push([
4402
+ Math.min(tx, back),
4403
+ Math.max(tx, back),
4404
+ ty - 5,
4405
+ ty + 5
4406
+ ]);
4407
+ runs.push([
4408
+ Math.min(tx, lx),
4409
+ Math.max(tx, lx),
4410
+ y1 - 3,
4411
+ y1 + 3
4412
+ ]);
4413
+ } else {
4414
+ tx = b.x + NW - 20 - ti * 10;
4415
+ y1 = b.y + NH + 12 + ti * 6;
4416
+ ty = b.y + NH;
4417
+ pts = [
4418
+ ...start,
4419
+ [lx, y1],
4420
+ [tx, y1],
4421
+ [tx, ty + 9]
4422
+ ];
4423
+ arrow = `M${tx - 5} ${ty + 9} L${tx + 5} ${ty + 9} L${tx} ${ty} Z`;
4424
+ arrows.push([
4425
+ tx - 5,
4426
+ tx + 5,
4427
+ ty,
4428
+ ty + 9
4429
+ ]);
4430
+ runs.push([
4431
+ tx,
4432
+ lx,
4433
+ y1 - 3,
4434
+ y1 + 3
4435
+ ]);
4436
+ }
4437
+ const sx0 = start[start.length - 2][0];
4355
4438
  runs.push([
4356
- Math.min(tx, lx),
4357
- Math.max(tx, lx),
4358
- y1 - 3,
4359
- y1 + 3
4439
+ Math.min(sx0, lx),
4440
+ Math.max(sx0, lx),
4441
+ y0 - 3,
4442
+ y0 + 3
4360
4443
  ]);
4444
+ if (e.inferred || r) {
4445
+ const text = e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e);
4446
+ marks.push({
4447
+ idx,
4448
+ e,
4449
+ text,
4450
+ tx: L ? upLabelX : upLabelEndL,
4451
+ ty: Math.round((y0 + y1) / 2),
4452
+ left: !L,
4453
+ r,
4454
+ dim,
4455
+ up: true
4456
+ });
4457
+ }
4361
4458
  } else {
4362
- tx = b.x + NW - 20 - ti * 10;
4363
- y1 = b.y + NH + 12 + ti * 6;
4364
- ty = b.y + NH;
4365
- pts = [
4366
- ...start,
4367
- [lx, y1],
4459
+ const oi = outs[e.from].indexOf(e);
4460
+ const on = outs[e.from].length;
4461
+ const ii = ins[e.to].indexOf(e);
4462
+ inn = ins[e.to].length;
4463
+ const sx = Math.round(a.x + NW * (oi + 1) / (on + 1));
4464
+ const sy = a.y + NH;
4465
+ tx = Math.round(b.x + NW * (ii + 1) / (inn + 1));
4466
+ ty = b.y;
4467
+ const off = (oi - (on - 1) / 2) * 6;
4468
+ const y1 = sy + gaps[layerIndex[e.from]] / 2 + off;
4469
+ if (layerIndex[e.to] - layerIndex[e.from] > 1) {
4470
+ const next = (byLayer.get(layers[layerIndex[e.from] + 1]) ?? []).map((n) => pos[n.id]);
4471
+ const mid = (sx + tx) / 2;
4472
+ const chans = [next[0].x - COL / 2, ...next.map((q) => q.x + NW + COL / 2)].map((c) => Math.max(PAD / 2, Math.min(W - PAD / 2, c)));
4473
+ const chx = chans.reduce((best, c) => Math.abs(c - mid) < Math.abs(best - mid) ? c : best, chans[0]) + off;
4474
+ const y2 = ty - gaps[layerIndex[e.to] - 1] / 2 + off;
4475
+ pts = [
4476
+ [sx, sy],
4477
+ [sx, y1],
4478
+ [chx, y1],
4479
+ [chx, y2],
4480
+ [tx, y2],
4481
+ [tx, ty - 8]
4482
+ ];
4483
+ } else pts = [
4484
+ [sx, sy],
4485
+ [sx, y1],
4368
4486
  [tx, y1],
4369
- [tx, ty + 9]
4487
+ [tx, ty - 8]
4370
4488
  ];
4371
- arrow = `M${tx - 5} ${ty + 9} L${tx + 5} ${ty + 9} L${tx} ${ty} Z`;
4489
+ arrow = `M${tx - 5} ${ty - 9} L${tx + 5} ${ty - 9} L${tx} ${ty} Z`;
4372
4490
  arrows.push([
4373
4491
  tx - 5,
4374
4492
  tx + 5,
4375
- ty,
4376
- ty + 9
4377
- ]);
4378
- runs.push([
4379
- tx,
4380
- lx,
4381
- y1 - 3,
4382
- y1 + 3
4493
+ ty - 9,
4494
+ ty
4383
4495
  ]);
4384
4496
  }
4385
- const sx0 = start[start.length - 2][0];
4386
- runs.push([
4387
- Math.min(sx0, lx),
4388
- Math.max(sx0, lx),
4389
- y0 - 3,
4390
- y0 + 3
4391
- ]);
4392
- if (e.inferred || r) {
4497
+ const w = (e.inferred ? 1.5 : 1.5 + 5.5 * ((e.count || 0) / maxCount)) + (picked ? 1.5 : 0) + (hovered ? 2 : 0);
4498
+ const cpts = clean(pts);
4499
+ for (let k = 1; k < cpts.length; k++) {
4500
+ const [ax, ay] = cpts[k - 1];
4501
+ const [bx, by] = cpts[k];
4502
+ segs.push({
4503
+ idx,
4504
+ box: [
4505
+ Math.min(ax, bx) - 3,
4506
+ Math.max(ax, bx) + 3,
4507
+ Math.min(ay, by) - 3,
4508
+ Math.max(ay, by) + 3
4509
+ ]
4510
+ });
4511
+ }
4512
+ const d = route(cpts, 10);
4513
+ paths.push(/* @__PURE__ */ jsx("path", {
4514
+ className: cx("ui-lg-edge", e.inferred && "is-inferred", r && "is-related", dim && "is-dim", hovered && "is-hover"),
4515
+ d,
4516
+ strokeWidth: w
4517
+ }, "p" + idx));
4518
+ if (pickEdge) hitPaths.push(/* @__PURE__ */ jsx("path", {
4519
+ className: "ui-lg-hit",
4520
+ "data-edge": edgeId(e),
4521
+ d,
4522
+ role: "button",
4523
+ tabIndex: 0,
4524
+ "aria-label": `${nameOf(e.from)} → ${nameOf(e.to)},${(e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e)).replace(/`/g, "")}`,
4525
+ "aria-pressed": picked,
4526
+ onClick: (ev) => {
4527
+ ev.stopPropagation();
4528
+ choose(e);
4529
+ },
4530
+ onPointerEnter: () => setHover(edgeId(e)),
4531
+ onPointerLeave: () => setHover(null),
4532
+ onKeyDown: (ev) => {
4533
+ if (ev.key === "Enter" || ev.key === " ") {
4534
+ ev.preventDefault();
4535
+ choose(e);
4536
+ } else if (ev.key === "Escape" && picked) pickEdge(null);
4537
+ }
4538
+ }, "h" + idx));
4539
+ paths.push(/* @__PURE__ */ jsx("path", {
4540
+ className: cx("ui-lg-arrow", r && "is-related", dim && "is-dim"),
4541
+ d: arrow
4542
+ }, "a" + idx));
4543
+ if (!up && (e.inferred || r)) {
4393
4544
  const text = e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e);
4394
4545
  marks.push({
4395
4546
  idx,
4396
4547
  e,
4397
4548
  text,
4398
- tx: L ? upLabelX : upLabelEndL,
4399
- ty: Math.round((y0 + y1) / 2),
4400
- left: !L,
4549
+ tx,
4550
+ ty,
4551
+ left: inn > 1 && tx < b.x + NW / 2,
4401
4552
  r,
4402
- dim,
4403
- up: true
4553
+ dim
4404
4554
  });
4405
4555
  }
4406
- } else {
4407
- const oi = outs[e.from].indexOf(e);
4408
- const on = outs[e.from].length;
4409
- const ii = ins[e.to].indexOf(e);
4410
- inn = ins[e.to].length;
4411
- const sx = Math.round(a.x + NW * (oi + 1) / (on + 1));
4412
- const sy = a.y + NH;
4413
- tx = Math.round(b.x + NW * (ii + 1) / (inn + 1));
4414
- ty = b.y;
4415
- const off = (oi - (on - 1) / 2) * 6;
4416
- const y1 = sy + GAP / 2 + off;
4417
- if (layerIndex[e.to] - layerIndex[e.from] > 1) {
4418
- const next = (byLayer.get(layers[layerIndex[e.from] + 1]) ?? []).map((n) => pos[n.id]);
4419
- const mid = (sx + tx) / 2;
4420
- const chans = [next[0].x - COL / 2, ...next.map((q) => q.x + NW + COL / 2)].map((c) => Math.max(PAD / 2, Math.min(W - PAD / 2, c)));
4421
- const chx = chans.reduce((best, c) => Math.abs(c - mid) < Math.abs(best - mid) ? c : best, chans[0]) + off;
4422
- const y2 = ty - GAP / 2 + off;
4423
- pts = [
4424
- [sx, sy],
4425
- [sx, y1],
4426
- [chx, y1],
4427
- [chx, y2],
4428
- [tx, y2],
4429
- [tx, ty - 8]
4430
- ];
4431
- } else pts = [
4432
- [sx, sy],
4433
- [sx, y1],
4434
- [tx, y1],
4435
- [tx, ty - 8]
4436
- ];
4437
- arrow = `M${tx - 5} ${ty - 9} L${tx + 5} ${ty - 9} L${tx} ${ty} Z`;
4438
- arrows.push([
4439
- tx - 5,
4440
- tx + 5,
4441
- ty - 9,
4442
- ty
4443
- ]);
4444
- }
4445
- const w = (e.inferred ? 1.5 : 1.5 + 5.5 * ((e.count || 0) / maxCount)) + (picked ? 1.5 : 0) + (hovered ? 2 : 0);
4446
- const cpts = clean(pts);
4447
- for (let k = 1; k < cpts.length; k++) {
4448
- const [ax, ay] = cpts[k - 1];
4449
- const [bx, by] = cpts[k];
4450
- segs.push({
4451
- idx,
4452
- box: [
4453
- Math.min(ax, bx) - 3,
4454
- Math.max(ax, bx) + 3,
4455
- Math.min(ay, by) - 3,
4456
- Math.max(ay, by) + 3
4457
- ]
4458
- });
4459
- }
4460
- const d = route(cpts, 10);
4461
- paths.push(/* @__PURE__ */ jsx("path", {
4462
- className: cx("ui-lg-edge", e.inferred && "is-inferred", r && "is-related", dim && "is-dim", hovered && "is-hover"),
4463
- d,
4464
- strokeWidth: w
4465
- }, "p" + idx));
4466
- if (pickEdge) hitPaths.push(/* @__PURE__ */ jsx("path", {
4467
- className: "ui-lg-hit",
4468
- "data-edge": edgeId(e),
4469
- d,
4470
- role: "button",
4471
- tabIndex: 0,
4472
- "aria-label": `${nameOf(e.from)} → ${nameOf(e.to)},${(e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e)).replace(/`/g, "")}`,
4473
- "aria-pressed": picked,
4474
- onClick: (ev) => {
4475
- ev.stopPropagation();
4476
- choose(e);
4556
+ });
4557
+ const placed = [...arrows, ...runs];
4558
+ const boxRects = main.map((n) => [
4559
+ pos[n.id].x,
4560
+ pos[n.id].x + NW,
4561
+ pos[n.id].y,
4562
+ pos[n.id].y + NH
4563
+ ]);
4564
+ const hits = (x0, x1, y0, y1) => placed.some((q) => x0 < q[1] + (q[4] || 0) && q[0] < x1 + (q[4] || 0) && y0 < q[3] && q[2] < y1);
4565
+ const cross = (q, x0, lw, y0, y1) => x0 < q[1] - 1 && q[0] + 1 < x0 + lw && y0 < q[3] - 1 && q[2] + 1 < y1;
4566
+ const labelEl = (m, x0, top, lines = [m.text]) => /* @__PURE__ */ jsx("span", {
4567
+ "aria-hidden": pickEdge ? true : void 0,
4568
+ className: cx("ui-lg-label", pickEdge && "is-clickable", pickEdge && hover === edgeId(m.e) && "is-hover", m.r && "is-related", m.dim && "is-dim"),
4569
+ style: {
4570
+ left: x0,
4571
+ top
4477
4572
  },
4478
- onPointerEnter: () => setHover(edgeId(e)),
4479
- onPointerLeave: () => setHover(null),
4480
- onKeyDown: (ev) => {
4481
- if (ev.key === "Enter" || ev.key === " ") {
4482
- ev.preventDefault();
4483
- choose(e);
4484
- } else if (ev.key === "Escape" && picked) pickEdge(null);
4485
- }
4486
- }, "h" + idx));
4487
- paths.push(/* @__PURE__ */ jsx("path", {
4488
- className: cx("ui-lg-arrow", r && "is-related", dim && "is-dim"),
4489
- d: arrow
4490
- }, "a" + idx));
4491
- if (!up && (e.inferred || r)) {
4492
- const text = e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e);
4493
- marks.push({
4494
- idx,
4495
- e,
4496
- text,
4497
- tx,
4498
- ty,
4499
- left: inn > 1 && tx < b.x + NW / 2,
4500
- r,
4501
- dim
4502
- });
4503
- }
4504
- });
4505
- const placed = [...arrows, ...runs];
4506
- const boxRects = main.map((n) => [
4507
- pos[n.id].x,
4508
- pos[n.id].x + NW,
4509
- pos[n.id].y,
4510
- pos[n.id].y + NH
4511
- ]);
4512
- const hits = (x0, x1, y0, y1) => placed.some((q) => x0 < q[1] + (q[4] || 0) && q[0] < x1 + (q[4] || 0) && y0 < q[3] && q[2] < y1);
4513
- const labelEl = (m, x0, top) => /* @__PURE__ */ jsx("span", {
4514
- "aria-hidden": pickEdge ? true : void 0,
4515
- className: cx("ui-lg-label", pickEdge && "is-clickable", pickEdge && hover === edgeId(m.e) && "is-hover", m.r && "is-related", m.dim && "is-dim"),
4516
- style: {
4517
- left: x0,
4518
- top
4519
- },
4520
- onClick: pickEdge ? (ev) => {
4521
- ev.stopPropagation();
4522
- choose(m.e);
4523
- } : void 0,
4524
- onPointerEnter: pickEdge ? () => setHover(edgeId(m.e)) : void 0,
4525
- onPointerLeave: pickEdge ? () => setHover(null) : void 0,
4526
- children: labelParts(m.text)
4527
- }, "l" + m.idx);
4528
- marks.sort((m, n) => m.ty - n.ty || m.tx - n.tx).forEach((m) => {
4529
- const w = Math.ceil(labelWidth(m.text));
4530
- if (m.up) {
4531
- const x0 = Math.max(PAD / 2, Math.min(W - PAD / 2 - w, m.left ? m.tx - w : m.tx));
4532
- const over = (q, y0, y1) => x0 < q[1] + 4 && q[0] < x0 + w + 4 && y0 < q[3] + 4 && q[2] < y1 + 4;
4533
- const blocked = (y0, y1) => hits(x0, x0 + w, y0, y1) || boxRects.some((q) => over(q, y0, y1));
4534
- const crosses = (y0, y1) => segs.some((g) => g.idx !== m.idx && over(g.box, y0, y1));
4535
- const start = Math.max(10, Math.min(H - 10, m.ty));
4536
- const find = (bad) => {
4537
- for (let k = 0; k <= 16; k++) for (const dir of k ? [-1, 1] : [0]) {
4538
- const t = start + dir * k * 18;
4539
- if (t >= 10 && t <= H - 10 && !bad(t - 8, t + 8)) return t;
4573
+ onClick: pickEdge ? (ev) => {
4574
+ ev.stopPropagation();
4575
+ choose(m.e);
4576
+ } : void 0,
4577
+ onPointerEnter: pickEdge ? () => setHover(edgeId(m.e)) : void 0,
4578
+ onPointerLeave: pickEdge ? () => setHover(null) : void 0,
4579
+ children: lines.map((t, i) => /* @__PURE__ */ jsxs(React.Fragment, { children: [i ? /* @__PURE__ */ jsx("br", {}) : null, labelParts(t)] }, i))
4580
+ }, "l" + m.idx);
4581
+ marks.sort((m, n) => Number(!!m.up) - Number(!!n.up) || m.ty - n.ty || m.tx - n.tx).forEach((m) => {
4582
+ const w = Math.ceil(labelWidth(m.text));
4583
+ if (m.up) {
4584
+ const x0Of = (lw) => Math.max(PAD / 2, Math.min(W - PAD / 2 - lw, m.left ? m.tx - lw : m.tx));
4585
+ const start0 = Math.max(10, Math.min(H - 10, m.ty));
4586
+ const find = (x0, lw, lh, withLines) => {
4587
+ const over = (q, y0, y1) => x0 < q[1] + 4 && q[0] < x0 + lw + 4 && y0 < q[3] + 4 && q[2] < y1 + 4;
4588
+ const bad = (y0, y1) => hits(x0, x0 + lw, y0 - 4, y1 + 4) || boxRects.some((q) => over(q, y0, y1)) || withLines && segs.some((g) => g.idx === m.idx ? cross(g.box, x0, lw, y0, y1) : over(g.box, y0, y1));
4589
+ for (let k = 0; k <= 16; k++) for (const dir of k ? [-1, 1] : [0]) {
4590
+ const t = start0 + dir * k * 18;
4591
+ if (t - lh / 2 >= 2 && t + lh / 2 <= H - 2 && !bad(t - lh / 2, t + lh / 2)) return t;
4592
+ }
4593
+ return null;
4594
+ };
4595
+ let spot = null;
4596
+ for (const lines of wraps(m.text)) {
4597
+ const lw = Math.ceil(Math.max(...lines.map(labelWidth)));
4598
+ const lh = lines.length * 16;
4599
+ const x0 = x0Of(lw);
4600
+ const top = find(x0, lw, lh, true);
4601
+ if (top != null) {
4602
+ spot = {
4603
+ x0,
4604
+ top,
4605
+ lw,
4606
+ lh,
4607
+ lines
4608
+ };
4609
+ break;
4610
+ }
4540
4611
  }
4541
- return null;
4612
+ if (!spot) {
4613
+ const g = Math.max(0, Math.min(layers.length - 2, layers.findIndex((_, k) => k < layers.length - 1 && m.ty < rowTop(k + 1))));
4614
+ need.add(g);
4615
+ const x0 = x0Of(w);
4616
+ spot = {
4617
+ x0,
4618
+ top: find(x0, w, 16, false) ?? start0,
4619
+ lw: w,
4620
+ lh: 16,
4621
+ lines: [m.text]
4622
+ };
4623
+ }
4624
+ placed.push([
4625
+ spot.x0,
4626
+ spot.x0 + spot.lw,
4627
+ spot.top - spot.lh / 2 - 4,
4628
+ spot.top + spot.lh / 2 + 4,
4629
+ 6
4630
+ ]);
4631
+ labels.push(labelEl(m, spot.x0, spot.top, spot.lines));
4632
+ return;
4633
+ }
4634
+ const g = layerIndex[m.e.to] - 1;
4635
+ const sideOf = (lw) => {
4636
+ let left = m.left;
4637
+ if (left && m.tx - 8 - lw < 0) left = false;
4638
+ else if (!left && m.tx + 8 + lw > W) left = m.tx - 8 - lw >= 0;
4639
+ return left;
4640
+ };
4641
+ const at = (left, lw) => left ? m.tx - 8 - lw : Math.min(m.tx + 8, W - lw);
4642
+ const clear = (x0, lw, y0, y1) => {
4643
+ const over = (q) => x0 < q[1] + 2 && q[0] < x0 + lw + 2 && y0 < q[3] + 2 && q[2] < y1 + 2;
4644
+ return !hits(x0, x0 + lw, y0, y1) && !boxRects.some(over) && !segs.some((sg) => sg.idx === m.idx ? cross(sg.box, x0, lw, y0, y1) : over(sg.box));
4542
4645
  };
4543
- const top = find((y0, y1) => blocked(y0, y1) || crosses(y0, y1)) ?? find(blocked) ?? start;
4646
+ let spot = null;
4647
+ for (const lines of wraps(m.text)) {
4648
+ const lw = Math.ceil(Math.max(...lines.map(labelWidth)));
4649
+ const lh = lines.length * 16;
4650
+ const pref = sideOf(lw);
4651
+ for (const left of [pref, !pref]) {
4652
+ const x0 = at(left, lw);
4653
+ if (x0 < 0 || x0 + lw > W) continue;
4654
+ for (let k = 0; k < 8 && !spot; k++) {
4655
+ const top = m.ty - 8 - lh / 2 - k * 18;
4656
+ if (top - lh / 2 < 2) break;
4657
+ if (clear(x0, lw, top - lh / 2, top + lh / 2)) spot = {
4658
+ x0,
4659
+ top,
4660
+ lw,
4661
+ lh,
4662
+ lines
4663
+ };
4664
+ }
4665
+ if (spot) break;
4666
+ }
4667
+ if (spot) break;
4668
+ }
4669
+ if (!spot) {
4670
+ if (g >= 0) need.add(g);
4671
+ const x0 = at(sideOf(w), w);
4672
+ let top = m.ty - 16;
4673
+ for (let k = 0; k < 8 && hits(x0, x0 + w, top - 8, top + 8); k++) top -= 18;
4674
+ spot = {
4675
+ x0,
4676
+ top,
4677
+ lw: w,
4678
+ lh: 16,
4679
+ lines: [m.text]
4680
+ };
4681
+ }
4544
4682
  placed.push([
4545
- x0,
4546
- x0 + w,
4547
- top - 8,
4548
- top + 8,
4683
+ spot.x0,
4684
+ spot.x0 + spot.lw,
4685
+ spot.top - spot.lh / 2,
4686
+ spot.top + spot.lh / 2,
4549
4687
  6
4550
4688
  ]);
4551
- labels.push(labelEl(m, x0, top));
4552
- return;
4553
- }
4554
- let left = m.left;
4555
- if (left && m.tx - 8 - w < 0) left = false;
4556
- else if (!left && m.tx + 8 + w > W) left = m.tx - 8 - w >= 0;
4557
- const x0 = left ? m.tx - 8 - w : Math.min(m.tx + 8, W - w);
4558
- const x1 = x0 + w;
4559
- let top = m.ty - 16;
4560
- for (let k = 0; k < 8 && hits(x0, x1, top - 8, top + 8); k++) top -= 18;
4561
- placed.push([
4562
- x0,
4563
- x1,
4564
- top - 8,
4565
- top + 8,
4566
- 6
4567
- ]);
4568
- labels.push(labelEl(m, x0, top));
4569
- });
4689
+ labels.push(labelEl(m, spot.x0, spot.top, spot.lines));
4690
+ });
4691
+ return {
4692
+ paths,
4693
+ labels,
4694
+ hitPaths,
4695
+ need
4696
+ };
4697
+ };
4698
+ placeRows();
4699
+ let drawn = draw();
4700
+ const grows = () => [...drawn.need].filter((g) => gaps[g] < 200);
4701
+ for (let k = 0; k < GROW_MAX / GROW && grows().length; k++) {
4702
+ for (const g of grows()) gaps[g] = Math.min(200, gaps[g] + GROW);
4703
+ placeRows();
4704
+ drawn = draw();
4705
+ }
4706
+ const stuck = [...drawn.need].filter((g) => gaps[g] >= 200);
4707
+ if (stuck.length) {
4708
+ for (const g of stuck) gaps[g] = GAP;
4709
+ placeRows();
4710
+ drawn = draw();
4711
+ }
4712
+ const { paths, labels, hitPaths } = drawn;
4570
4713
  const sizeText = p.sizeLabel ?? tr("{n} 個項目");
4571
4714
  const useText = p.useLabel ?? tr("使用 {n} 次");
4572
4715
  const nodes = main.map((n) => {