codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.76k stars 368 forks source link

Line wrapping + line numbers not resizing to print media #1354

Closed fonsp closed 6 months ago

fonsp commented 6 months ago

Describe the issue

When line wrapping is enabled, line numbers are sized to match line heights, and this is updated when the container resizes:

Scherm­afbeelding 2024-03-13 om 11 40 11

When printing, these recalculations don't happen. For example, printing this reproduction example with A5:

Scherm­afbeelding 2024-03-13 om 11 40 02

The long line went from wrapping 2 lines to 3 lines, but the line number element is still sized to 2 lines.

If you print on a page size that roughly matches the screen size, you don't see this issue.

Browser and platform

Chrome 122 and Firefox 123

Reproduction link

https://codemirror.net/try/?c=aW1wb3J0IHtiYXNpY1NldHVwLCBFZGl0b3JWaWV3fSBmcm9tICJjb2RlbWlycm9yIgppbXBvcnQge2phdmFzY3JpcHR9IGZyb20gIkBjb2RlbWlycm9yL2xhbmctamF2YXNjcmlwdCIKCmNvbnN0IGRvYyA9IGAKaGVsbG8Kc29tZSB0ZXh0CnRoaXMgaXMgYSBwcmV0dHkgbG9uZyBsaW5lLi4uLi4uLi4uLi4uLgpidXQKdGhlIG5leHQgb25lCmlzIHN1cGVyIGR1cGVyIGxvbmcgYW5kIHdpbGwgYWx3YXlzIHdyYXAgc2xrZGZqIGFsc2RrZmogYWxza2RmaiBhbHNrZGpmIGxha3NqZGYgbGthanMgZGZsa2phIHNkZmxraiBhbHNkamZrIGFzZGYKbGluZSA4CgoKCmxpbmUgMTIKYAoKbmV3IEVkaXRvclZpZXcoewogIGRvYywKICBleHRlbnNpb25zOiBbYmFzaWNTZXR1cCwgamF2YXNjcmlwdCgpLCBFZGl0b3JWaWV3LmxpbmVXcmFwcGluZ10sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5Cn0pCg==

fonsp commented 6 months ago

I noticed that when changing paper sizes (A4 to A5), the window resize event is not fired. But on Chrome, the window.matchMedia("print").addEventListener event is fired whenever the page size changes. (On Firefox I can't find an event that matches a paper size change.)

fonsp commented 6 months ago

I added this to the MWE to make cm aware of the matchMedia event:

window.matchMedia("print").addEventListener("change", (x) => {
  window.dispatchEvent(new CustomEvent("beforeprint"))
})

and it fixes the issue! (on Chrome) So maybe we could bind the onPrint handler to matchMedia("print")?

marijnh commented 6 months ago

Attached patch uses media queries in a way that should help with this.