frontend: let the writer set a figure's size in the editor

Figures come out of a plotting tool at whatever size they were made. On the
paper this was written for, the sixteen of them are 1441, 1857, 2409 and 4169
pixels wide — every one wider than the 1276px column — so with no size they all
printed at the full width, and the near-square ones (1857×1986, 1974×2068) took
1365px of height, a page each. Measured in the browser, all sixteen came out at
exactly 1276px: not one of them was the size its author wanted.

`{width=…}` has been in the syntax all along, so this is the control that was
missing, not the feature. A row that is a figure now carries 宽度 in its header:
25/50/70/100%, or any value typed in — the size a panel wants is what it
measures, not a round number — with 原尺寸 as the empty value. Beside it, what
the picture will measure on the page (`原图 1857×1986 · 显示 893×955`), worked
out against the column actually rendered behind the dialog, which is the number
the writer is deciding with.

Two rules keep it honest. The value goes through `parseImageWidth` — the same
function that parses the attribute when the paper is printed — so what the
control shows is what will be printed, and a value it cannot read is refused
with the forms that work instead of stored as a size that quietly does nothing.
And the attribute is written into the sentence content by `withImageWidth`,
which edits only the first picture: the control is offered for a figure, and a
sentence with words around its picture is not one. Reading the size back out of
the content rather than keeping a copy of it in the draft means there is one
place it can be wrong instead of two.

`check-image.mjs` grows from 15 checks to 24, driving the real control: that
only figure rows offer one, that it shows what each is set to, that typing 30%
converts to 383×239 immediately, that what was typed is what gets stored, that
the paper then prints it at 30% of the column, that clearing it goes back to the
natural 480×300, and that "宽一点" is refused while the sentence keeps its size.

The MCP side of the same rule was wrong and is fixed with it: `image_upload(width=
"60")` wrote `{width=60px}`, and the reader takes a bare number as a share of the
column — `parseImageWidth` only reads pixels when the value says `px`. A model
asking for 60% got a 60-pixel figure. v7 is deployed with the corrected reading,
and smoke_mcp.py checks it.
This commit is contained in:
2026-09-24 11:25:18 +08:00
parent 9e6a1f8c3e
commit 48f0c12fd2
9 changed files with 357 additions and 12 deletions
+16 -3
View File
@@ -63,7 +63,8 @@ def register(server: MCPServer) -> None:
str | None,
Field(
description=(
"可选宽度:70%(占栏宽)或 400px。不传则按原尺寸,最大不超过栏宽"
"可选宽度:70%(占栏宽)或 400px;只写数字按百分比算。"
"不传则按原尺寸,最大不超过栏宽"
)
),
] = None,
@@ -169,6 +170,12 @@ def _width_attribute(width: str | None) -> str:
Tolerant about how it is written — ``70%``, ``70 %``, ``{width=70%}`` and
``w=400px`` all mean the same thing, which is what a model repeats back from
a sentence it read — and strict about what it means.
A bare number is a **percentage**, because that is what the reader makes of
one: ``parseImageWidth`` only takes a value as pixels when it is written
``px``, and treats everything else as a share of the column. ``400`` is
therefore out of range and refused with the two forms that work, rather than
written as ``400px`` and printed at a third of the page.
"""
if width is None or not str(width).strip():
return ""
@@ -181,10 +188,16 @@ def _width_attribute(width: str | None) -> str:
"不写则按原尺寸显示(最多不超过栏宽)"
)
digits, unit = int(match.group(1)), (match.group(2) or "px").lower()
digits = int(match.group(1))
unit = (match.group(2) or "%").lower()
limit = _MAX_PERCENT if unit == "%" else _MAX_PIXELS
if not 1 <= digits <= limit:
s.fail(f"width 的 {unit} 值要在 1 到 {limit} 之间,收到的是“{width}”")
# The pointer to pixels only helps when the caller wrote a bare number:
# "150%" is already written as a percentage and does not want advice.
hint = f"像素要写成 “{digits}px”" if match.group(2) is None else ""
s.fail(
f"width 的 {unit} 值要在 1 到 {limit} 之间,收到的是“{width}”。{hint}"
)
return f"{{width={digits}{unit}}}"