feat(grid): support selecting readonly cell text

This commit is contained in:
zipg authored and GitHub committed 2026-08-13 19:45:09 +08:00
1 parent a3eaac3413
commit 75df9266cb
4 files changed
+268 -16

No files matched your search

@@ -11,4 +11,17 @@ describe("DataGrid native clipboard regions", () => {
it("keeps transposed field-name text selection out of grid copy shortcuts", () => {
expect(dataGridSource).toMatch(/<div\b(?=[^>]*\bdata-native-clipboard)(?=[^>]*class="sticky left-0)[^>]*>/);
});
it("opens read-only text selection from DOM, canvas, and transposed cells", () => {
expect(dataGridSource.match(/startReadonlyCellTextSelection\(/g)).toHaveLength(4);
expect(dataGridSource).not.toContain("showReadonlyCellDetailsOnDblClick");
expect(dataGridSource).toContain("if (!canEditCellItem(item, actualColIdx)) {");
expect(dataGridSource.match(/<DataGridReadonlyTextSelection/g)).toHaveLength(3);
});
it("uses a text cursor for read-only cells across grid render modes", () => {
expect(dataGridSource).toContain('hitItem && actualColIdx !== undefined ? "text" : "cell"');
expect(dataGridSource).toContain("'cursor-text': !isScrolling && !canEditCellItem(item, col.actualColIdx)");
expect(dataGridSource).toContain("'cursor-text': !isScrolling,");
});
});
@@ -0,0 +1,79 @@
// @vitest-environment happy-dom
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { createApp, nextTick } from "vue";
import { afterEach, describe, expect, it, vi } from "vitest";
import DataGridReadonlyTextSelection from "../DataGridReadonlyTextSelection.vue";
const mountedRoots: HTMLDivElement[] = [];
const dataGridSource = readFileSync(resolve(process.cwd(), "apps/desktop/src/components/grid/DataGrid.vue"), "utf8");
function mountSelection(options: { value?: string; expanded?: boolean } = {}) {
const root = document.createElement("div");
document.body.appendChild(root);
mountedRoots.push(root);
const onClose = vi.fn();
const onEscape = vi.fn();
const app = createApp(DataGridReadonlyTextSelection, {
value: options.value ?? "alpha-copy-test",
expanded: options.expanded ?? false,
onClose,
onEscape,
});
app.mount(root);
return { root, app, onClose, onEscape };
}
afterEach(() => {
for (const root of mountedRoots.splice(0)) root.remove();
});
describe("DataGridReadonlyTextSelection", () => {
it("closes a stale read-only selection when a result is replaced", () => {
const resultWatcher = dataGridSource.match(/watch\(\s*\(\) => props\.result,\s*\(result, previousResult\) => \{[\s\S]*?\n\);/)?.[0] ?? "";
expect(resultWatcher).toContain("if (isDataGridPrefixAppend(previousResult, result)) return;");
expect(resultWatcher).toContain("closeReadonlyCellTextSelection();");
});
it("focuses and selects the complete read-only value on mount", async () => {
const { root } = mountSelection();
await nextTick();
const input = root.querySelector("input");
expect(input).not.toBeNull();
expect(input?.readOnly).toBe(true);
expect(input?.dataset.nativeClipboard).toBe("");
expect(document.activeElement).toBe(input);
expect(input?.selectionStart).toBe(0);
expect(input?.selectionEnd).toBe("alpha-copy-test".length);
});
it("renders multiline or overflowing content in a read-only textarea", async () => {
const value = "first line\nsecond line";
const { root } = mountSelection({ value, expanded: true });
await nextTick();
const textarea = root.querySelector("textarea");
expect(textarea?.value).toBe(value);
expect(textarea?.readOnly).toBe(true);
expect(textarea?.selectionStart).toBe(0);
expect(textarea?.selectionEnd).toBe(value.length);
});
it("prevents paste and emits close signals for Escape and blur", async () => {
const { root, onClose, onEscape } = mountSelection();
await nextTick();
const input = root.querySelector("input")!;
const paste = new Event("paste", { bubbles: true, cancelable: true });
input.dispatchEvent(paste);
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }));
input.dispatchEvent(new FocusEvent("blur"));
expect(paste.defaultPrevented).toBe(true);
expect(input.value).toBe("alpha-copy-test");
expect(onEscape).toHaveBeenCalledOnce();
expect(onClose).toHaveBeenCalledOnce();
});
});