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
+97
View File
@@ -268,6 +268,103 @@ try {
check('the inserted figure is in the paper', saved.count === 2, JSON.stringify(saved))
check('and it is the one that was uploaded', saved.captions.includes('第二张图'), saved.captions.join())
check('both pictures decoded', saved.decoded.every((width) => width === 480), saved.decoded.join())
// --- the figure's size, set from the editor ------------------------------
//
// Figures come out of the plotting tool at whatever size they were made —
// one paper's are 1441, 2400 and 4169 pixels wide — and every one of them can
// be wider than the column, so with no size they all print at the full column
// and a near-square panel fills a page. This control is the answer to that,
// and what has to hold is that what it writes is what the reader prints.
await page.locator('.paragraph-actions button:has-text("编辑")').first().click()
await page.waitForSelector('.editor .rows', { timeout: 15000 })
await page.waitForSelector('.width-select')
// The hint divides the column by the picture's own size, so it needs the
// thumbnail to have decoded first.
await page.waitForFunction(
() => document.querySelector('.size-hint')?.textContent?.includes('原图'),
{ timeout: 15000 },
)
// What each select is showing: Element Plus prints the chosen label in its
// own element, not as the value of the input the writer types into.
const controls = await page.evaluate(() =>
[...document.querySelectorAll('.width-select')].map((node) =>
(node.querySelector('.el-select__selected-item')?.textContent ?? '').trim() ||
(node.querySelector('.el-select__placeholder')?.textContent ?? '').trim(),
),
)
check('only a figure row offers a size', controls.length === 2, JSON.stringify(controls))
check(
'and each one shows the size it is set to',
controls[0] === '50%' && controls[1] !== '50%',
JSON.stringify(controls),
)
const hint = async () =>
(await page.locator('.size-hint').first().innerText()).replace(/\s+/g, ' ')
check(
'the hint says what it will measure on the page',
/显示 638×399/.test(await hint()),
await hint(),
)
// Typing a value that is not one of the presets is the point of the control:
// the size a panel wants is what it measures, not a round number.
await page.locator('.width-select input').first().fill('30%')
await page.keyboard.press('Enter')
await page.waitForTimeout(400)
check('typing 30% converts immediately', /显示 383×239/.test(await hint()), await hint())
await page.getByRole('button', { name: '保存段落' }).click()
await page.waitForSelector('.el-dialog', { state: 'detached', timeout: 15000 })
await page.waitForTimeout(700)
const stored = await api('GET', `/papers/${paperId}/paragraphs/${sort}`)
const sized = stored.paragraph.sentences.find((sentence) =>
sentence.content.includes('试验装置示意图'),
)
check('what was typed is what is stored', sized?.content.endsWith('{width=30%}'), sized?.content)
const shrunk = await page.evaluate(() => {
const body = document.querySelector('.paragraph-body')
const column = Math.round(body.getBoundingClientRect().width)
const figure = [...document.querySelectorAll('.sentence--figure')].find(
(node) => node.querySelector('.figure-caption')?.textContent.trim() === '试验装置示意图',
)
const box = figure.querySelector('img').getBoundingClientRect()
return { column, width: Math.round(box.width), height: Math.round(box.height) }
})
check(
'and the paper prints it at that size',
Math.abs(shrunk.width - shrunk.column * 0.3) <= 2,
`${shrunk.width} vs ${shrunk.column * 0.3}`,
)
// Taking the size off again: 原尺寸, which is what the placeholder promises.
await page.locator('.paragraph-actions button:has-text("编辑")').first().click()
await page.waitForSelector('.width-select', { timeout: 15000 })
await page.waitForFunction(
() => document.querySelector('.size-hint')?.textContent?.includes('原图'),
{ timeout: 15000 },
)
await page.locator('.width-select').first().hover()
await page.locator('.width-select .el-select__clear').first().click({ force: true })
await page.waitForTimeout(300)
check('clearing it goes back to the natural size', /显示 480×300/.test(await hint()), await hint())
// A size the reader cannot read is refused rather than stored: a figure that
// quietly prints at its natural size is the bug this control exists to fix.
await page.locator('.width-select input').first().fill('宽一点')
await page.keyboard.press('Enter')
await page.waitForTimeout(400)
check(
'a size that is not one is refused',
await page.evaluate(() => Boolean(document.querySelector('.el-message--warning'))),
)
check('and the sentence keeps the size it had', /显示 480×300/.test(await hint()), await hint())
await page.getByRole('button', { name: '取消' }).click()
await page.waitForSelector('.el-dialog', { state: 'detached', timeout: 15000 })
} finally {
await browser.close()
if (paperId) {