Add application feedback and refine headless canvas design boundaries
This commit is contained in:
@@ -16,7 +16,7 @@ const { window } = dom;
|
||||
const NativeMessageChannel = globalThis.MessageChannel;
|
||||
const channels = [];
|
||||
globalThis.MessageChannel = class extends NativeMessageChannel { constructor() { super(); channels.push(this); } };
|
||||
for (const key of ["window", "document", "HTMLElement", "Element", "Node", "SVGElement", "Event", "MouseEvent", "KeyboardEvent", "MutationObserver", "localStorage", "getComputedStyle"]) Object.defineProperty(globalThis, key, { value: key === "window" ? window : typeof window[key] === "function" && key === "getComputedStyle" ? window[key].bind(window) : window[key], configurable: true });
|
||||
for (const key of ["window", "document", "HTMLElement", "Element", "Node", "SVGElement", "Event", "CustomEvent", "MouseEvent", "KeyboardEvent", "MutationObserver", "localStorage", "getComputedStyle"]) Object.defineProperty(globalThis, key, { value: key === "window" ? window : typeof window[key] === "function" && key === "getComputedStyle" ? window[key].bind(window) : window[key], configurable: true });
|
||||
Object.defineProperty(globalThis, "navigator", { value: window.navigator, configurable: true });
|
||||
globalThis.requestAnimationFrame = window.requestAnimationFrame.bind(window);
|
||||
globalThis.cancelAnimationFrame = window.cancelAnimationFrame.bind(window);
|
||||
@@ -51,7 +51,7 @@ const folder = await mkdtemp(resolve(tmpdir(), "endoleap-canvas-ui-"));
|
||||
try {
|
||||
const outfile = resolve(folder, "fixture.cjs");
|
||||
await build({
|
||||
stdin: { contents: 'export { default as GraphCanvas } from "./src/components/canvas/graph-canvas"; export { BoardDirectory } from "./src/components/workspace/board-directory"; export { useCanvasTree } from "./src/components/canvas/use-canvas-tree"; export { useCanvasAI } from "./src/components/canvas/use-canvas-ai"; export { IconPicker } from "./src/components/canvas/icon-picker"; export { makeCanvasNode, emptyCanvas } from "./src/lib/canvas"; export { createRoot } from "react-dom/client"; export { act, createElement } from "react";', resolveDir: process.cwd(), loader: "tsx" },
|
||||
stdin: { contents: 'export { default as GraphCanvas } from "./src/components/canvas/graph-canvas"; export { AppFeedback } from "./src/components/ui/app-feedback"; export { IconButton } from "./src/components/ui/icon-button"; export { notify, dismissNotification } from "./src/lib/notifications"; export { BoardDirectory } from "./src/components/workspace/board-directory"; export { useCanvasTree } from "./src/components/canvas/use-canvas-tree"; export { useCanvasAI } from "./src/components/canvas/use-canvas-ai"; export { IconPicker } from "./src/components/canvas/icon-picker"; export { makeCanvasNode, emptyCanvas } from "./src/lib/canvas"; export { createRoot } from "react-dom/client"; export { act, createElement } from "react";', resolveDir: process.cwd(), loader: "tsx" },
|
||||
outfile, bundle: true, platform: "node", format: "cjs", jsx: "automatic", logLevel: "silent", nodePaths: [resolve("node_modules")],
|
||||
loader: { ".css": "empty" },
|
||||
plugins: [{ name: "dynamic-component", setup(build) {
|
||||
@@ -61,10 +61,11 @@ try {
|
||||
if (process.env.ROTATION_BASELINE === "1") build.onLoad({ filter: /node-style-toolbar\.tsx$/ }, async ({ path }) => ({ contents: (await readFile(path, "utf8")).replace('onClick={(event) => event.stopPropagation()}', ''), loader: "tsx" }));
|
||||
} }],
|
||||
});
|
||||
const { GraphCanvas, BoardDirectory, useCanvasAI, useCanvasTree, IconPicker, makeCanvasNode, emptyCanvas, act, createElement: h, createRoot } = createRequire(import.meta.url)(outfile);
|
||||
const { GraphCanvas, AppFeedback, IconButton, notify, dismissNotification, BoardDirectory, useCanvasAI, useCanvasTree, IconPicker, makeCanvasNode, emptyCanvas, act, createElement: h, createRoot } = createRequire(import.meta.url)(outfile);
|
||||
const root = createRoot(document.querySelector("#app"));
|
||||
const tick = () => new Promise((resolve) => setTimeout(resolve, 30));
|
||||
const event = async (target, type, init = {}) => { assert.ok(target, `Missing target for ${type}`); await act(async () => { const Type = type.startsWith("pointer") ? PointerEvent : type.startsWith("key") ? window.KeyboardEvent : window.MouseEvent; target.dispatchEvent(new Type(type, { bubbles: true, cancelable: true, view: window, ...init })); await tick(); }); };
|
||||
if (process.env.CANVAS_UI_FEEDBACK_ONLY !== "1") {
|
||||
{
|
||||
const boards = [{ id: "root", title: "主画布", workspace_id: "w", slug: "root-slug" }, { id: "child", title: "未命名分区", parent_board_id: "root", workspace_id: "w", slug: "child-slug" }, { id: "nested", title: "孙画布", parent_board_id: "child", workspace_id: "w" }];
|
||||
const renderDirectory = (active, query = "") => h(BoardDirectory, { boards, workspace: "space", active, query, demo: false });
|
||||
@@ -218,6 +219,9 @@ try {
|
||||
await canvasMemberScenario({ root, act, h, useCanvasTree, GraphCanvas, event, window, makeCanvasNode, emptyCanvas, tick, mock: (handler) => { apiMock = handler; } });
|
||||
const { canvasUploadScenario } = await import("./canvas-upload-scenario.mjs");
|
||||
await canvasUploadScenario({ root, act, h, GraphCanvas, window, emptyCanvas, event, tick, mock: (handler) => { apiMock = handler; } });
|
||||
}
|
||||
const { feedbackScenario } = await import("./feedback-scenario.mjs");
|
||||
await feedbackScenario({ root, act, h, AppFeedback, IconButton, notify, dismissNotification, GraphCanvas, window, makeCanvasNode, emptyCanvas, event, tick });
|
||||
await act(async () => { root.unmount(); await tick(); });
|
||||
} finally {
|
||||
await rm(folder, { recursive: true, force: true }); window.close(); stop();
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
export async function feedbackScenario({ root, act, h, AppFeedback, IconButton, notify, dismissNotification, GraphCanvas, window, makeCanvasNode, emptyCanvas, event, tick }) {
|
||||
await act(async () => { root.render(null); await tick(); });
|
||||
let button, clicks = 0;
|
||||
const icon = () => h("svg", { "aria-hidden": true });
|
||||
const control = () => h(IconButton, { icon, label: "旋转工具", title: "旋转选中元素", ref: (value) => { button = value; }, onClick: () => { clicks++; } });
|
||||
await act(async () => { root.render(control()); await tick(); });
|
||||
assert.equal(button.title, "旋转选中元素", "Standalone controls retain native explanations");
|
||||
await act(async () => { root.render(h(AppFeedback, null, h("div", { id: "controls" }, control()))); await tick(); });
|
||||
assert.ok(document.querySelector("#controls").firstElementChild === button, "Tooltip does not wrap the button or alter toolbar geometry");
|
||||
assert.equal(button.tagName, "BUTTON", "Forwarded ref still points at the actual button");
|
||||
assert.equal(button.title, "", "A provider avoids duplicate native and Radix tooltips");
|
||||
await act(async () => { button.focus(); await tick(); });
|
||||
const tooltip = document.querySelector('[role="tooltip"]');
|
||||
assert.ok(tooltip?.textContent.includes("旋转选中元素"), "Keyboard focus exposes the explanation");
|
||||
assert.ok(button.getAttribute("aria-describedby"), "Explanation is associated with its trigger");
|
||||
await event(document, "keydown", { key: "Escape" });
|
||||
assert.ok(!document.querySelector('[role="tooltip"]'), "Escape dismisses the tooltip");
|
||||
await event(button, "click"); assert.equal(clicks, 1, "Tooltips preserve business clicks");
|
||||
await act(async () => {
|
||||
notify({ id: "feedback-check", title: "正在处理" }); await tick();
|
||||
notify({ id: "feedback-check", title: "已完成", level: "success" }); await tick();
|
||||
});
|
||||
assert.equal(document.querySelectorAll('[data-sonner-toast]').length, 1, "One logical notice updates instead of duplicating");
|
||||
assert.ok(document.querySelector('[data-sonner-toast]').textContent.includes("已完成"));
|
||||
assert.ok(document.querySelector('[aria-label="关闭通知"]'), "Notification close control is labelled");
|
||||
await act(async () => { dismissNotification("feedback-check"); await tick(); });
|
||||
for (let i = 0; i < 20 && document.querySelector('[data-sonner-toast]'); i++) await act(async () => { await tick(); });
|
||||
assert.ok(!document.querySelector('[data-sonner-toast]'), "Dismissed notifications are removed");
|
||||
console.log("PASS feedback: native fallback, forwarded ref, keyboard tooltip/Escape, clicks and stable notification IDs");
|
||||
|
||||
const id = "feedback-board";
|
||||
const node = { ...makeCanvasNode("text", { x: 100, y: 100 }), id: "feedback-text" };
|
||||
const frame = { ...makeCanvasNode("frame", { x: 500, y: 100 }), id: "feedback-frame" };
|
||||
frame.data.title = "提示分区";
|
||||
localStorage.setItem(`endoleap_canvas_v1:w:${id}`, JSON.stringify({ ...emptyCanvas(), nodes: [node, frame] }));
|
||||
await act(async () => { root.render(h(AppFeedback, null, h(GraphCanvas, { board: { id, workspace_id: "w", title: "反馈测试", kind: "board", version: 1 } }))); await tick(); });
|
||||
const textNode = document.querySelector('.react-flow__node[data-id="feedback-text"]');
|
||||
await event(textNode, "mousedown", { clientX: 200, clientY: 200, button: 0, buttons: 1 });
|
||||
await event(window, "mousemove", { clientX: 210, clientY: 200, buttons: 1 });
|
||||
await event(window, "mousemove", { clientX: 700, clientY: 200, buttons: 1 });
|
||||
await event(window, "mouseup", { clientX: 700, clientY: 200, button: 0 });
|
||||
assert.ok(document.querySelector('[data-sonner-toast][data-type="success"]')?.textContent.includes("已加入「提示分区」"));
|
||||
assert.ok(document.querySelector('[title="属于:提示分区"]'), "Notifications do not replace actual membership state");
|
||||
const fileInput = document.querySelector('input[type="file"][accept^="image/"]');
|
||||
Object.defineProperty(fileInput, "files", { configurable: true, value: [new File(["test"], "test.pdf", { type: "application/pdf" })] });
|
||||
await act(async () => { fileInput.dispatchEvent(new window.Event("change", { bubbles: true })); await tick(); });
|
||||
assert.ok(document.querySelector('[data-sonner-toast][data-type="error"]')?.textContent.includes("演示模式请选择"));
|
||||
await event(document.querySelector('[aria-label="添加文本卡片"]'), "click");
|
||||
assert.equal(document.querySelectorAll('.react-flow__node').length, 3, "An error notification never blocks editing");
|
||||
await act(async () => { dismissNotification(`canvas:${id}:error`); root.render(null); await tick(); });
|
||||
console.log("PASS canvas feedback: membership success, input errors and continued local editing through the application provider");
|
||||
}
|
||||
Reference in New Issue
Block a user