[Bug Report]: getBytesLength counts every character as uppercase when a word has one capital, and snapToGrid never snaps to 0

Open Beginner friendly
#2,434 0 comments 0 reactions 0 assignees View on GitHub

@qq2514568380 is already working on this.

Since Nov 17, 2026.

  • #2435 by @qq2514568380 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
frontend

Research direction

Two small bugs in packages/core: the getBytesLength loop in src/util/edge.ts (~L809) tests word.match(/[A-Z]/) where it should test the current character c, and snapToGrid in src/util/geometry.ts (~L9) uses || point, which wrongly returns the raw value when the snapped result is 0. Also check the if (nodeX && nodeY) guard in GraphModel.ts getModelAfterSnapToGrid. Start by running the reproduction script from the issue; done means the expected outputs listed there ([0, 0, 10, 10], 7.5 and 17.5 byte lengths) and the existing util tests still pass.

Written by the indexing model from the issue text.

Description

What happened?

Two utility bugs in @logicflow/core affect default text sizing and grid snapping:

  1. getBytesLength checks the whole string instead of the current character. In packages/core/src/util/edge.ts#L809, the loop reads const c = word.charCodeAt(i) but tests if (word.match(/[A-Z]/)), so a single capital letter gives every character in word weight 1.5. getSvgTextSize (edge label background <rect> in LineText) and getSvgTextWidthHeight (TextNodeModel.width) both call it:

    • Lowercase ASCII characters (weight 1) count as 1.5, making 'Approve' 50% wider than 'approve' (75 vs 51 at fontSize: 12).
    • CJK characters (weight 2) also drop to 1.5, so prepending 'A' to '网关审批流程节点' shrinks the computed width from 99 to 87 and the text overflows its box.
  2. snapToGrid skips 0. In packages/core/src/util/geometry.ts#L9, gridSize * Math.round(point / gridSize) || point falls back to point whenever the snapped coordinate is 0 (|point| < gridSize / 2). When snapGrid: true, nodes dragged, dropped, or added near x = 0 or y = 0 keep their raw coordinate instead of snapping to 0 (and getModelAfterSnapToGrid in GraphModel.ts also guards with if (nodeX && nodeY), skipping both axes when either coordinate is 0).

Reproduction (npm i @logicflow/[email protected] && node repro.js, Node v26.13.0):

const {
  snapToGrid,
  getBytesLength,
  getSvgTextSize,
  getSvgTextWidthHeight,
} = require("@logicflow/core");

console.log([4, -4, 6, 14].map((v) => snapToGrid(v, 10, true)));

console.log(getBytesLength("approve"), getBytesLength("Approve"));
console.log(
  getSvgTextSize({ rows: ["approve"], rowsLength: 1, fontSize: 12 }).width,
  getSvgTextSize({ rows: ["Approve"], rowsLength: 1, fontSize: 12 }).width,
);

console.log(getBytesLength("网关审批流程节点"), getBytesLength("A网关审批流程节点"));
console.log(
  getSvgTextWidthHeight({ rows: ["网关审批流程节点"], rowsLength: 1, fontSize: 12 }).width,
  getSvgTextWidthHeight({ rows: ["A网关审批流程节点"], rowsLength: 1, fontSize: 12 }).width,
);

Actual output:

[ 4, -4, 10, 10 ]
7 10.5
51 75
16 13.5
99 87

Expected output:

  • snapToGrid snaps 4 and -4 to 0 ([ 0, 0, 10, 10 ]).
  • getBytesLength tests character i (c >= 0x41 && c <= 0x5a), giving 7.5 for "Approve" (width 51) and 17.5 for "A网关审批流程节点" (width 111).
Core Version

2.2.5 (also verified on master d60a216)

Extension Version

2.3.1 (not required to reproduce)

Engine Version

No response

What browsers are you seeing the problem on?

Chrome

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
11.8k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from didi/LogicFlow

All issues in didi/LogicFlow

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.