Repository navigation
fix(muya): start an image resize only from the resize bar's own handles (#5116) - #5382
Merged
Merged
Conversation
…es (#5116) The resize bar listens for mousedown on the whole document body and began a resize for any element inside `.bar`. Document content can carry that class too: inline raw HTML (`<span class="bar">`), an HTML block, or a mermaid node styled with `classDef bar`. Pressing and drag-selecting over such content attached the resize drag listeners, so every mouse move threw "Cannot read properties of null (reading 'querySelector')" when no image had been clicked, or "(reading 'style')" after an image had been clicked and its handles hidden. Because the press carried no handle side, the computed width was NaN, and releasing the mouse rewrote the last clicked image as `<img ... width="NaN" />`, even while its handles were still showing. A right press on such content never gets its mouseup (the context menu takes it), so the listeners stayed attached and every later hover threw. Start a resize only when the pressed `.bar` lies inside this bar's own `.mu-transformer` container, and read the handle side from that element. With several editors on one page, a press on one editor's handle no longer starts a drag in the others, which threw the same way. The container check also covers the bar's shown state: its handles exist only between `_render()` and `hide()`. The unit spec now detaches each bar's document listeners after every test, so a bar left over from one test cannot react to the next test's events. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`_mouseMove` computes a width only for a `left` or `right` handle. For any
other drag the width stayed `''`, `Number.parseInt('')` produced NaN, and
that was written to the image element and, because
`typeof NaN === 'number'`, committed to the markdown as `width="NaN"` on
mouseup. That is how a press on content with class "bar" corrupted the
image in #5116.
The previous commit stops such presses from starting a resize. Return
from any move without a handle side as well, so no other route can store
NaN in the document. With `width` typed as a number, the compiler rejects
reading it on a path that computed none.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Drag-select over inline raw HTML, an HTML block and a mermaid node that carry the resize handles' `bar` class: with no image clicked, after an image was clicked and its handles hidden, and while the handles are shown. None may throw or change the image's markdown, and the bar's own handle must still commit a width. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The image resize bar started a resize on a mousedown on any element with class
baranywhere in the page, not only on its own two handles. Document content can have that class: inline HTML (<span class="bar">), an HTML block, or a mermaid node styled withclassDef bar. On v0.20.0-rc.3 and develop:Cannot read properties of null (reading 'querySelector')at_mouseMove, the same message as [Bug] Unexpected error with message Cannot read properties of null (reading 'querySelector') #5116.<img ... width="NaN" />, and Save writeswidth="NaN"to the file on disk.This PR makes a resize start only from the bar's own handles. It also makes a drag without a left or right handle compute no width.
How this relates to the two reports:
Cannot read properties of null (reading 'getBoundingClientRect')in_mouseMove) comes from a different route: drag listeners left attached after the bar hid. fix(muya): keep the image resize bar from crashing after its image is removed (#5216) #5288 fixed that route in rc.2. The class-barcontent in this PR cannot produce that message, because the pressed element has no handle side.Steps to reproduce
Desktop app, packaged v0.20.0-rc.3 or a develop (8bcb1e0) build:
doc.mdnext to anypic.png:Actual: every mouse move while the button is held raises
Cannot read properties of null (reading 'style')(at_update<_mouseMove), and nothing is selected. After release the image line is<img src="pic.png" title="" alt="" width="NaN" />, and saving writes that line to disk.Expected: a plain text selection, with the document unchanged.
Variants (each measured on rc.3 and on the develop build):
Cannot read properties of null (reading 'querySelector')at_mouseMove([Bug] Unexpected error with message Cannot read properties of null (reading 'querySelector') #5116's message). The markdown is unchanged.width="NaN"is written silently.<div class="bar">block bar</div>, or a mermaid label (A[Start here]:::bar --> B[Finish]withclassDef bar fill:#f96), gives the same result as the main steps.'querySelector'errors.'style'errors, and the eventual mouseup writeswidth="NaN".Start <span class="bar"></span> tail text.). Clicking the image and then pressing and dragging on it writeswidth="NaN"silently.'style'.Cannot destructure property 'path' of 'this.parent' as it is null.fromParagraphContent.updateImageon tab 1's detached block.Root cause
ImageResizeBar._mouseDownlistens ondocument.bodyand checked onlyevent.target.closest('.bar'). A press on content therefore attached the drag's mousemove and mouseup listeners, with_movingAnchornull because content has nodata-position._mouseMovecomputes a width only for aleftorrightanchor. For any other anchor:_referenceis null, so_reference.querySelectorthrows._referenceis stale. The width stays'',Number.parseInt('')gives NaN, NaN is written to the<img>, and_updatethrows because the handles are gone._updatesucceeds, so there is no error.In both image cases,
_mouseUpsawtypeof NaN === 'number'and committedwidth="NaN"throughupdateImage. A right press never delivers its mouseup to the page, so its listeners stayed attached.closest('.bar')here was the only class matcher on a document-wide listener inpackages/muya/src/ui:paragraphQuickInsertMenu'sclosest('.item')listens on its own container and already checkscontainer.contains(...).tooltip'sclosest('[data-tooltip]')is not imported anywhere.The legacy
packages/muyajstransformer has the same pattern with.circle. The desktop app no longer uses it, and it is unchanged.Fix
_mouseDownstarts a resize only when the pressed.baris inside this bar's own.mu-transformercontainer, and it reads the handle side from that element. Handles exist only between_render()andhide(), so this also implies the bar is showing for an image._mouseMovereturns for any anchor other thanleft/right(default: return).widthis now typednumber, so the compiler rejects reading it on a path that computed none.Behaviour changes
baris now an ordinary press. In the steps above, drag-selecting selects "progress bar" again, and the press no longer hides the image toolbar.width="319".Known gaps
Menu.popupwas stubbed and the mouseup withheld through CDP. A real NSMenu was not driven.Test plan
imageResizeBar.spec.ts: 6 new tests..barwith no image clicked, after the bar hides, and while it is shown.'195'for clientX 200).index.ts, 5 fail; the own-handle test passes.default: returnremoved, or with commit 1'sindex.ts, the no-side test fails;tscalso reports TS2454 whendefault: returnis removed.tests/drag/image-resize-bar-foreign-bar-5116.spec.ts(Chromium): 4 of 5 fail with develop'sindex.ts, and 5/5 pass with this PR.tests/dragin Chromium: 21/21 pass.index.ts: 8×getBoundingClientRectwhen the second editor's bar was shown then hidden, and 8×querySelectorwhen it was never shown.pnpm -C packages/muya test: 234 files, 1669 tests pass.pnpm -C packages/muya lint:types; eslint on the changed files (e2e with--no-ignore);pnpm -C packages/muya check-circular.--user-data-dirandMARKTEXT_ERROR_INTERACTION=1, and a positive-control error reached the main-process sink first.build:unpackreproduce the main steps (includingwidth="NaN"on disk) and every variant above.build:unpackgives 0 errors and unchanged markdown in all of them. That includes both right-press variants and the nested-image and two-tab variants. The own-handle resize still commits and saveswidth="319".Fixes #5116
Refs #5352
🤖 Generated with Claude Code