frontend: let a figure be sized by its height as well as its width

A writer with a tall panel does not want to shrink it from the left edge: what
they know is "this one is too tall". The format had a width and nothing else, so
the only way to make a figure shorter was to guess a percentage and check the
page — and the previous commit's answer, "type 638px", asked them to do the
arithmetic themselves.

So `{height=300px}` joins `{width=…}`, in the same attribute block, and the
editor grows a second box beside the first. Both are typeable as well as
pre-set, both write through the reader's own parse function, and the hint beside
them says what the picture will measure on the page.

Three rules, and the point of all three is that a figure cannot be stretched:

* **a width is exact, a height is a cap.** A width has a reference — the column,
  or a length the writer chose — while nothing measures a height against
  anything, and enlarging a picture past its own pixels only makes it soft. In
  the flow of a sentence a height is always a downscale, which comes to the same
  thing as exact.
* **given one, the other follows from the picture's own shape** — `max-height`
  for a height, `width` for a width — so the height is never written by hand.
* **given both, they are a box to fit inside**, not a box to fill. Two numbers
  that need not have the picture's proportions can only mean "no bigger than",
  and the check asserts the drawn rectangle still matches the file's ratio.

Whichever box is left empty keeps the other's setting, so a size can be set in
two steps, and clearing either goes back to the natural size.

`check-image.mjs`: 26 checks to 36. Two of the new ones found real bugs while
being written. The first: with both a width and a height in one `{…}` block,
neither side parsed — the patterns were anchored to the whole block, so a figure
with two numbers silently printed at its natural size. The attribute block is now
split into declarations first. The second was mine: returning "contain" for a
height-only figure left a 1276×2000 box around a small picture, which the caption
then sat under. A height is a cap, and a cap needs no contain.

The MCP tool takes the same two arguments, so a model can write either number,
and a percentage height is refused with the reason it cannot work: a sentence is
flowed text, so there is nothing for it to be a percentage of and the browser
ignores it. v8 is deployed and DSH has verified both numbers round-tripping.
This commit is contained in:
2026-09-24 13:18:52 +08:00
parent b19f28492e
commit 1531263bd2
10 changed files with 499 additions and 104 deletions
+122 -10
View File
@@ -307,17 +307,36 @@ try {
// 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))
const boxes = await page.evaluate(() => {
const rows = [...document.querySelectorAll('.row')]
const withSize = rows.filter((row) => row.querySelector('.width-select'))
return {
rows: rows.length,
withSize: withSize.length,
// Two boxes per figure — a width and a height, in that order.
values: withSize.map((row) =>
[...row.querySelectorAll('.width-select')].map(
(node) =>
(node.querySelector('.el-select__selected-item')?.textContent ?? '').trim() ||
(node.querySelector('.el-select__placeholder')?.textContent ?? '').trim(),
),
),
}
})
check(
'and each one shows the size it is set to',
controls[0] === '50%' && controls[1] !== '50%',
JSON.stringify(controls),
'a size is offered on the figure rows and nowhere else',
boxes.withSize === 2 && boxes.rows === 3,
JSON.stringify(boxes),
)
check(
'each figure row has a width box and a height box',
boxes.values.every((pair) => pair.length === 2),
JSON.stringify(boxes.values),
)
check(
'and the width box shows the size that row is set to',
boxes.values[0][0] === '50%' && boxes.values[1][0] !== '50%',
JSON.stringify(boxes.values),
)
const hint = async () =>
@@ -384,6 +403,99 @@ try {
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 })
// --- a height, which is what a writer asks for when the width is fine -----
//
// Not every figure needs resizing from the left edge: a tall panel is made
// short by its *height*, and its width has to follow or the picture deforms.
// Written through the API so what is checked is the reader's rule, not the
// editor's; the 480×300 fixture makes the arithmetic checkable by hand.
const shapeOf = async (attributes) => {
await api('PUT', `/papers/${paperId}/paragraphs/${sort}`, {
sentences: [{ content: `![尺寸试验](${image.url})${attributes}`, line_break_before: true }],
})
await page.reload({ waitUntil: 'networkidle' })
await page.waitForSelector('.sentence--figure img')
await page.waitForFunction(
() => document.querySelector('.sentence--figure img')?.complete,
{ timeout: 15000 },
)
return page.evaluate(() => {
const body = document.querySelector('.paragraph-body')
const img = document.querySelector('.sentence--figure img')
const box = img.getBoundingClientRect()
return {
column: Math.round(body.getBoundingClientRect().width),
width: Math.round(box.width),
height: Math.round(box.height),
ratio: box.width / box.height,
natural: img.naturalWidth / img.naturalHeight,
style: img.getAttribute('style') ?? '',
}
})
}
const onlyHeight = await shapeOf('{height=200px}')
check(
'只填高度时,高度就是填的那个数',
Math.abs(onlyHeight.height - 200) <= 1,
JSON.stringify(onlyHeight),
)
check(
'只填高度时,宽度按原比例跟着算(480×300 → 320×200)',
Math.abs(onlyHeight.width - 320) <= 2,
JSON.stringify(onlyHeight),
)
check(
'只填高度时,图还是原来的形状',
Math.abs(onlyHeight.ratio / onlyHeight.natural - 1) < 0.01,
JSON.stringify(onlyHeight),
)
const both = await shapeOf('{width=400px height=100px}')
check(
'宽高都填时,图放进这个框里(400×100 的框 → 160×100)',
Math.abs(both.width - 160) <= 2 && Math.abs(both.height - 100) <= 1,
JSON.stringify(both),
)
check(
'宽高都填时也没有被拉变形',
Math.abs(both.ratio / both.natural - 1) < 0.01,
JSON.stringify(both),
)
const huge = await shapeOf('{height=2000px}')
check(
'高度是个上限:不会把图放大,也不会把它推出栏外',
huge.width === 480 && huge.height === 300,
JSON.stringify(huge),
)
// …and the same thing typed into the editor's 高 box.
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 },
)
check(
'编辑器里宽、高各有一个框',
(await page.locator('.width-select').count()) === 2,
`${await page.locator('.width-select').count()} boxes`,
)
await page.locator('.width-select').nth(1).locator('input').fill('250')
await page.keyboard.press('Enter')
await page.waitForTimeout(400)
check('填高度后立刻换算(250 → 400×250)', /显示 400×250/.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 savedHeight = await api('GET', `/papers/${paperId}/paragraphs/${sort}`)
check(
'存下来的也是高度',
savedHeight.paragraph.sentences[0].content.endsWith('{height=250px}'),
savedHeight.paragraph.sentences[0].content,
)
} finally {
await browser.close()
if (paperId) {