Skip to content

Commit a94846d

Browse files
fix(web): file editor carets and selections follow code font changes (#35144)
1 parent a30cb32 commit a94846d

2 files changed

Lines changed: 122 additions & 7 deletions

File tree

‎patches/@[email protected]‎

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,18 @@ index 984f049ff3694dd353fa6637b0f09fc7dce74992..4d6599905be0fe7b5b5c1a225c0b3ff8
250250
workingDiff,
251251
diff: pendingRenderDiff
252252
diff --git a/dist/editor/editor.js b/dist/editor/editor.js
253-
index a9905d43382de2ca03d6111ba9ab3725d8cb6981..466252fbc025fec5e29fc2958ad54b58f89ed124 100644
253+
index a9905d43382de2ca03d6111ba9ab3725d8cb6981..294f537622b9a62d89111d7ed4a7c07cc24dac30 100644
254254
--- a/dist/editor/editor.js
255255
+++ b/dist/editor/editor.js
256-
@@ -490,7 +490,9 @@ var Editor = class {
256+
@@ -106,6 +106,7 @@ var Editor = class {
257+
#deletionsColumn;
258+
#overlayElement;
259+
#overlayElements;
260+
+ #fontProbeElement;
261+
#caretElements;
262+
#caretHighlightElements;
263+
#primaryCaretElement;
264+
@@ -490,7 +491,9 @@ var Editor = class {
257265
selections: this.#selections
258266
});
259267
const primarySelection = resolvedSelections.at(-1);
@@ -264,6 +272,113 @@ index a9905d43382de2ca03d6111ba9ab3725d8cb6981..466252fbc025fec5e29fc2958ad54b58
264272
this.#scrollToPrimaryCaret();
265273
}
266274
setMarkers(markers) {
275+
@@ -595,6 +598,7 @@ var Editor = class {
276+
this.#contentHasFocus = false;
277+
this.#overlayElement?.remove();
278+
this.#overlayElement = void 0;
279+
+ this.#fontProbeElement = void 0;
280+
this.#resizeObserver?.disconnect();
281+
this.#resizeObserver = void 0;
282+
this.#fontRemeasureScheduled = false;
283+
@@ -1033,6 +1037,19 @@ var Editor = class {
284+
this.#tokenizer?.applyStyle();
285+
this.#spriteElement = createSpriteElement();
286+
this.#overlayElement = h("div", { dataset: "editorOverlay" });
287+
+ // Resizes when the inherited code font changes, so the observer can re-measure.
288+
+ this.#fontProbeElement = h("span", {
289+
+ ariaHidden: "true",
290+
+ style: {
291+
+ position: "absolute",
292+
+ top: "0",
293+
+ left: "0",
294+
+ visibility: "hidden",
295+
+ pointerEvents: "none",
296+
+ whiteSpace: "pre"
297+
+ },
298+
+ textContent: "0"
299+
+ }, this.#overlayElement);
300+
const replacementFocusTargetIsContent = (event) => {
301+
const target = event.composedPath()[0];
302+
return target instanceof Node && this.#contentElement?.contains(target) === true;
303+
@@ -1409,6 +1426,7 @@ var Editor = class {
304+
this.#resizeObserver = new ResizeObserver(this.#handleLayoutResize);
305+
this.#resizeObserver.observe(contentEl);
306+
this.#resizeObserver.observe(contentEl.parentElement);
307+
+ if (this.#fontProbeElement !== void 0) this.#resizeObserver.observe(this.#fontProbeElement);
308+
this.#computeContentOffset(contentEl);
309+
}
310+
#handleCustomPasteEvent = async () => {
311+
@@ -1864,7 +1882,9 @@ var Editor = class {
312+
const change = textDocument.applyEdits(edits, true, selections, nextSelections, true);
313+
if (change !== void 0) this.#applyChange(change, nextSelections);
314+
}
315+
- #handleLayoutResize = () => {
316+
+ #handleLayoutResize = (entries) => {
317+
+ const contentElement = this.#contentElement;
318+
+ const metricsChanged = contentElement !== void 0 && entries.some((entry) => entry.target === this.#fontProbeElement) && this.#metrics.init(contentElement);
319+
const lineAnnotations = this.#lineAnnotations?.length ?? 0;
320+
const prevGutterWidth = this.#gutterWidthCache;
321+
const prevContentWidth = this.#contentWidthCache;
322+
@@ -1872,12 +1892,12 @@ var Editor = class {
323+
this.#contentWidthCache = void 0;
324+
const gutterWidthChanged = this.#getGutterWidth() !== prevGutterWidth;
325+
const contentWidthChanged = this.#getContentWidth() !== prevContentWidth;
326+
- if (!gutterWidthChanged && !contentWidthChanged) return;
327+
+ if (!gutterWidthChanged && !contentWidthChanged && !metricsChanged) return;
328+
this.#lastAccessedCharX = void 0;
329+
this.#metrics.clearTextWidthCache();
330+
- if (contentWidthChanged) {
331+
+ if (contentWidthChanged || metricsChanged) {
332+
this.#wrapLineOffsetsCache.clear();
333+
- if (this.#isWrap || lineAnnotations > 0) this.#lineYCache.clear();
334+
+ if (metricsChanged || this.#isWrap || lineAnnotations > 0) this.#lineYCache.clear();
335+
}
336+
if (this.#selections !== void 0 || this.#matches !== void 0 || this.#markerRenderer !== void 0) {
337+
this.#updateSelections(this.#selections ?? []);
338+
diff --git a/dist/editor/textMeasure.d.ts b/dist/editor/textMeasure.d.ts
339+
index 89e938d89eca77d351aeed65f3c5495383effd97..cde1d6ba89a97bbb1614754a48f8994388c186bd 100644
340+
--- a/dist/editor/textMeasure.d.ts
341+
+++ b/dist/editor/textMeasure.d.ts
342+
@@ -9,8 +9,8 @@ declare class Metrics {
343+
lineHeight: number;
344+
/** Padding top of the root element. */
345+
paddingTop: number;
346+
- /** initialize the metrics */
347+
- init(root: HTMLElement): void;
348+
+ /** (re)read the metrics from root, returning true when they changed */
349+
+ init(root: HTMLElement): boolean;
350+
/**
351+
* Re-measure the '0' character width against the font that is loaded right
352+
* now, returning true when measurement-dependent UI should refresh.
353+
diff --git a/dist/editor/textMeasure.js b/dist/editor/textMeasure.js
354+
index 96d4fb137ea683b66381b1ce6e47e8708a89366c..13f2564897a5bf53a8f311300003b3ea875c73a5 100644
355+
--- a/dist/editor/textMeasure.js
356+
+++ b/dist/editor/textMeasure.js
357+
@@ -16,9 +16,10 @@ var Metrics = class {
358+
lineHeight = 20;
359+
/** Padding top of the root element. */
360+
paddingTop = 0;
361+
- /** initialize the metrics */
362+
+ /** (re)read the metrics from root, returning true when they changed */
363+
init(root) {
364+
- if (this.#root === root && this.#canvasCtx !== void 0 && this.ch !== -1) return;
365+
+ const previousLineHeight = this.lineHeight;
366+
+ let fontChanged = false;
367+
this.#root = root;
368+
this.#canvas ??= h("canvas", { style: { display: "none" } });
369+
this.#canvasCtx ??= this.#canvas.getContext("2d") ?? void 0;
370+
@@ -37,9 +38,11 @@ var Metrics = class {
371+
this.#setCanvasFont(font);
372+
this.ch = this.canvasMeasureTextWidth("0");
373+
this.clearTextWidthCache();
374+
+ fontChanged = true;
375+
}
376+
const nextTabSize = parseInt(tabSize, 10);
377+
if (!Number.isNaN(nextTabSize)) this.tabSize = nextTabSize;
378+
+ return fontChanged || this.lineHeight !== previousLineHeight;
379+
}
380+
/**
381+
* Re-measure the '0' character width against the font that is loaded right
267382
diff --git a/dist/utils/createFileHeaderElement.js b/dist/utils/createFileHeaderElement.js
268383
index 19934176d0787ae1304e771c707230d5c8837e83..298b3386eac5126a7c1ec985674e799aac0ffd42 100644
269384
--- a/dist/utils/createFileHeaderElement.js

‎pnpm-lock.yaml‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)