fix(editor): measure shortcut capture input width to fit CJK
This commit is contained in:
1 parent
b06e952ba0
commit
0437f952e0
4 files changed
+288
-1
No files matched your search
@@ -0,0 +1,129 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { createApp, defineComponent, h, nextTick, ref } from "vue";
|
||||
import type { Ref, WatchSource } from "vue";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { useMeasuredWidth } from "../useMeasuredWidth";
|
||||
|
||||
function createHarness(revalidateSources: WatchSource[] = []) {
|
||||
const targetRef = ref<HTMLElement | null>(null);
|
||||
let widthRef: Ref<number> | undefined;
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
widthRef = useMeasuredWidth(targetRef, 42, revalidateSources);
|
||||
return () => h("div");
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.mount(document.createElement("div"));
|
||||
return { targetRef, widthRef: widthRef!, unmount: () => app.unmount() };
|
||||
}
|
||||
|
||||
/** A bare element whose getBoundingClientRect reports the given width. */
|
||||
function elementOfWidth(width: number): HTMLElement {
|
||||
const element = document.createElement("span");
|
||||
Object.defineProperty(element, "getBoundingClientRect", {
|
||||
configurable: true,
|
||||
value: () => ({ width, height: 0, top: 0, left: 0, bottom: 0, right: 0, x: 0, y: 0, toJSON: () => ({}) }),
|
||||
});
|
||||
return element;
|
||||
}
|
||||
|
||||
async function flush(ticks = 5) {
|
||||
for (let i = 0; i < ticks; i += 1) {
|
||||
await nextTick();
|
||||
}
|
||||
}
|
||||
|
||||
describe("useMeasuredWidth", () => {
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
it("keeps the fallback while no element is attached", async () => {
|
||||
const { targetRef, widthRef } = createHarness();
|
||||
expect(targetRef.value).toBeNull();
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(42);
|
||||
});
|
||||
|
||||
it("measures the element once attached", async () => {
|
||||
const { targetRef, widthRef } = createHarness();
|
||||
|
||||
targetRef.value = elementOfWidth(90);
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(90);
|
||||
});
|
||||
|
||||
it("ignores a zero-width measurement instead of clobbering the previous width", async () => {
|
||||
const flag = ref(0);
|
||||
const { targetRef, widthRef } = createHarness([() => flag.value]);
|
||||
|
||||
targetRef.value = elementOfWidth(90);
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(90);
|
||||
|
||||
// Zero width means "not laid out yet" (or display:none ancestry), never
|
||||
// "the text is zero pixels wide" — must not replace a usable width. Bump
|
||||
// the revalidate source so a re-measure actually runs against the
|
||||
// zero-width stub.
|
||||
Object.defineProperty(targetRef.value!, "getBoundingClientRect", {
|
||||
configurable: true,
|
||||
value: () => ({ width: 0, height: 0, top: 0, left: 0, bottom: 0, right: 0, x: 0, y: 0, toJSON: () => ({}) }),
|
||||
});
|
||||
flag.value += 1;
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(90);
|
||||
});
|
||||
|
||||
it("re-measures when a revalidate source changes (ResizeObserver-unavailable path)", async () => {
|
||||
const flag = ref(0);
|
||||
const { targetRef, widthRef } = createHarness([() => flag.value]);
|
||||
|
||||
targetRef.value = elementOfWidth(70);
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(70);
|
||||
|
||||
// A locale swap changed the text: the element resized without moving, so
|
||||
// only the source watch picks it up.
|
||||
Object.defineProperty(targetRef.value!, "getBoundingClientRect", {
|
||||
configurable: true,
|
||||
value: () => ({ width: 91, height: 0, top: 0, left: 0, bottom: 0, right: 0, x: 0, y: 0, toJSON: () => ({}) }),
|
||||
});
|
||||
flag.value += 1;
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(91);
|
||||
});
|
||||
|
||||
it("re-binds and re-measures when the target element is swapped", async () => {
|
||||
const { targetRef, widthRef } = createHarness();
|
||||
|
||||
targetRef.value = elementOfWidth(90);
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(90);
|
||||
|
||||
targetRef.value = elementOfWidth(120);
|
||||
await flush();
|
||||
expect(widthRef.value).toBe(120);
|
||||
});
|
||||
|
||||
it("stays inert after unmount (observer disconnected, no stray throws)", async () => {
|
||||
const { targetRef, unmount } = createHarness();
|
||||
|
||||
targetRef.value = elementOfWidth(90);
|
||||
await flush();
|
||||
expect(targetRef.value).not.toBeNull();
|
||||
|
||||
unmount();
|
||||
// Mutating the stub and flushing must not throw once the app is gone.
|
||||
Object.defineProperty(targetRef.value!, "getBoundingClientRect", {
|
||||
configurable: true,
|
||||
value: () => ({ width: 200, height: 0, top: 0, left: 0, bottom: 0, right: 0, x: 0, y: 0, toJSON: () => ({}) }),
|
||||
});
|
||||
await flush();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
import { nextTick, onUnmounted, ref, watch, type Ref, type WatchSource } from "vue";
|
||||
|
||||
/**
|
||||
* Measured border-box width (CSS pixels) of a mirror element.
|
||||
*
|
||||
* Measuring primitive for widths that char-count arithmetic cannot predict:
|
||||
* bind the returned ref to a template ref and it tracks the element's real
|
||||
* laid-out width — including font-fallback advances, letter-spacing, and
|
||||
* sub-pixel rounding. Motivated by #9144, where the zh-CN shortcut-capture
|
||||
* placeholder advanced 15px/char on a machine whose CJK fallback font renders
|
||||
* wider than 1em, so a `label.length + 2em` width clipped the last glyph.
|
||||
*
|
||||
* The width starts at `fallback` so callers render a sane size before the
|
||||
* first measurement lands (and in environments without real layout, e.g.
|
||||
* happy-dom). Zero-width measurements are ignored: they mean "not laid out
|
||||
* yet" (or `display: none` ancestry), never "the text is zero pixels wide",
|
||||
* and must not clobber a usable width. `revalidateSources` covers content
|
||||
* changes that resize the element without moving it (e.g. a locale swap)
|
||||
* for environments where ResizeObserver is unavailable.
|
||||
*/
|
||||
export function useMeasuredWidth(targetRef: Ref<HTMLElement | null>, fallback = 0, revalidateSources: WatchSource[] = []): Ref<number> {
|
||||
const width = ref(fallback);
|
||||
let resizeObserver: ResizeObserver | undefined;
|
||||
|
||||
function measure() {
|
||||
const element = targetRef.value;
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
const measured = element.getBoundingClientRect().width;
|
||||
if (measured > 0) {
|
||||
width.value = measured;
|
||||
}
|
||||
}
|
||||
|
||||
if (revalidateSources.length > 0) {
|
||||
watch(revalidateSources, () => {
|
||||
void nextTick(measure);
|
||||
});
|
||||
}
|
||||
|
||||
watch(
|
||||
targetRef,
|
||||
(element) => {
|
||||
resizeObserver?.disconnect();
|
||||
resizeObserver = undefined;
|
||||
if (element && typeof ResizeObserver !== "undefined") {
|
||||
resizeObserver = new ResizeObserver(measure);
|
||||
resizeObserver.observe(element);
|
||||
}
|
||||
void nextTick(measure);
|
||||
},
|
||||
{ flush: "post" },
|
||||
);
|
||||
|
||||
onUnmounted(() => {
|
||||
resizeObserver?.disconnect();
|
||||
resizeObserver = undefined;
|
||||
});
|
||||
|
||||
return width;
|
||||
}
|
||||
Reference in new issue
Block a user