From 7326e6811055859bf3ef92e6abd596719f35d864 Mon Sep 17 00:00:00 2001 From: djj <421774554@qq.com> Date: Mon, 16 Nov 2026 09:48:24 +0800 Subject: [PATCH 1/5] [feat] canvas: resize Markdown text nodes and edit them fullscreen ## summary - Add a selected-only text resize grip with independent dimensions, a usable minimum, lock checks and one undo step per gesture. Reuse resize-node and canvas.update, persisting after the drag ends. - Render Markdown cards and add source/preview modes to the existing fullscreen window. Share text, selection, caret and scroll state; retain uncontrolled inline input and protect IME composition. - Keep editing live: Done, closing and Escape finish the surface without a separate Cancel, and undo reverses the completed edit. - Add canvas text create/show/update/resize commands using the existing asset and canvas services, lock checks and revision conflicts. Read complete UTF-8 source up to 1 MiB without widening agent read limits. - Add bilingual labels and guidance, command and lock tests, and a browser gate for Markdown, editing handoff and resize persistence. ## validation - npm run check:i18n: 3018 keys, zero problems. - npx tsc --noEmit: passed without diagnostics. - npm test: 820 passed, zero failed or skipped. - PYTHONPATH=packages/core/src:apps/api/src .venv/bin/python -m pytest \ tests/test_canvas_text_cli.py tests/test_cli.py \ tests/test_agent_artifacts.py -q: 47 passed. - PYTHONPATH=packages/core/src:apps/api/src .venv/bin/python \ -m pytest -q -m ready: 90 passed, 2014 deselected. - npm run check:browser-manifest: 76 entries, passed. - npm run check:browser-locale: 71 pages, zero problems. - node tests/browser/canvas_text_markdown.mjs: passed standalone against isolated API/Vite with Chrome, including both locales. - npm run test:browser: 21/21 workflows passed with isolated services. - /opt/anaconda3/bin/python -m pre_commit run: all applicable hooks passed; git diff --staged --check also passed. - Browser model calls used a fixture double. Physical IME and multi-runtime evidence remain unverified. Refs: #336 --- docs/guides/cli.md | 33 +++ packages/core/src/opendirector/cli.py | 143 ++++++++++++ scripts/run-browser-suite.mjs | 1 + src/canvas/CanvasActionsContext.ts | 2 + src/canvas/CanvasFlowSurface.tsx | 19 +- src/canvas/CanvasNodeView.tsx | 43 +++- src/canvas/TextMarkdown.tsx | 32 +++ src/canvas/TextNodeEditor.tsx | 60 +++++ src/canvas/TextNodeWindow.tsx | 86 +++++-- src/canvas/model.ts | 14 +- src/canvas/nodes.css | 11 +- src/canvas/text-markdown.css | 90 +++++++ src/canvas/text-node-window.css | 41 +++- src/canvas/themes.css | 2 +- src/canvas/workspace/WorkspaceView.tsx | 8 +- src/canvas/workspace/useWorkspaceDocument.ts | 7 +- .../workspace/useWorkspaceInteraction.tsx | 8 +- src/i18n/messages/en-US/canvasNodes.ts | 2 + src/i18n/messages/en-US/canvasShell.ts | 3 + src/i18n/messages/zh-CN/canvasNodes.ts | 2 + src/i18n/messages/zh-CN/canvasShell.ts | 3 + tests/browser/canvas_text_markdown.mjs | 219 ++++++++++++++++++ tests/browser/manifest.json | 5 + tests/canvasText.test.ts | 78 +++++++ tests/test_agent_artifacts.py | 18 ++ tests/test_canvas_text_cli.py | 154 ++++++++++++ 26 files changed, 1027 insertions(+), 57 deletions(-) create mode 100644 src/canvas/TextMarkdown.tsx create mode 100644 src/canvas/TextNodeEditor.tsx create mode 100644 src/canvas/text-markdown.css create mode 100644 tests/browser/canvas_text_markdown.mjs create mode 100644 tests/canvasText.test.ts create mode 100644 tests/test_canvas_text_cli.py diff --git a/docs/guides/cli.md b/docs/guides/cli.md index cc85732e2..743279638 100644 --- a/docs/guides/cli.md +++ b/docs/guides/cli.md @@ -128,3 +128,36 @@ opendirector voice create recording.wav --model MODEL_CONFIG_UUID --connection C Use the returned `voiceId` as the speech parameter `voice`, and preserve the returned selection fields as `voiceSelection` when updating the audio generation specification. Use `opendirector generation-spec update --help-field payload.spec@audio.parameters.voiceSelection` to inspect the current typed fields. Do not copy voice identities across connections. `opendirector voice delete VOICE_ID --model MODEL_CONFIG_UUID --connection CONNECTION_ID` immediately deletes the uploaded voice if the provider supports deletion. Obtain authorization for that specific voice before invoking the CLI or Agent tool. The composer asks for confirmation in the UI. These commands use the same services as the UI and Agent tools. 音色库命令同样适用于中文工作流:先用 `voice list` 读取当前模型的连接身份及要求,再用 `voice create` 上传已获授权的录音;Drama3 等适配器必须填写 `--ref-text`。保存生成配置时同时保留服务端 `voiceId` 和返回的音色选择信息。不同服务之间不能直接复用音色 ID。`voice delete` 仅删除服务支持的上传音色,CLI 和 Agent 会立即执行,请先取得针对该音色的明确授权。在界面中删除时会弹出确认框。 + +## Canvas text: Markdown and sizing / 画布文本:Markdown 与尺寸 + +Double-click a text card (or press Enter with it selected) to edit Markdown source. Outside editing, the card renders headings, lists, quotes, emphasis, links, fenced code and tables. Select the card, then drag its bottom-right handle to resize width and height independently; the UI minimum is 200 × 120 canvas units. Long content scrolls within the card. One resize gesture is one undo step and does not change the source. Locked nodes, including nodes inside a locked group, remain read-only. + +Use **Expand editor** for a viewport-filling source/preview window. Both views edit the same node; source selection, caret and scroll position survive switching views and returning to the canvas. **Done**, closing and Escape finish the current surface without discarding the draft, just like inline editing; undo reverses the completed edit. Failed saves preserve the local draft for correction/retry. Links use the same confirmation card as assistant replies. + +双击文本卡片(或选中后按 Enter)编辑 Markdown 源码;非编辑状态显示标题、列表、引用、强调、链接、围栏代码及表格。选中卡片后,拖动右下角手柄可独立调整宽高,界面最小尺寸为 200 × 120 画布单位,长内容在卡片内部滚动。一次拖动对应一次撤销,正文不变。节点自身或父分组锁定时只读。 + +通过「展开编辑」打开铺满应用视口的源码/预览窗口。两个界面编辑同一节点,切换视图和返回画布时保留源码选区、光标及滚动位置。「完成」、关闭和 Escape 与行内编辑一致,结束当前编辑界面但不丢弃草稿;撤销可还原完成的编辑。保存失败时保留本地草稿以便修正、重试。链接使用与助手回复相同的确认卡片。 + +```bash +opendirector --project PROJECT_UUID canvas text --help +opendirector --project PROJECT_UUID --operation-id OPERATION_UUID canvas text create \ + --file notes.md --title Notes --item-id ITEM_UUID --x 20 --y 40 --width 480 --height 320 +opendirector --project PROJECT_UUID canvas text show --id ITEM_UUID +opendirector --project PROJECT_UUID --operation-id UPDATE_UUID canvas text update \ + --id ITEM_UUID --file - --expected-version canvas_item:ITEM_UUID:1 < revised.md +opendirector --project PROJECT_UUID --operation-id RESIZE_UUID canvas text resize \ + --id ITEM_UUID --width 640 --height 400 --expected-version canvas_item:ITEM_UUID:2 +``` + +`--file` is UTF-8 Markdown, not a JSON command document; `-` reads stdin without shell escaping. The asset limit is 1 MiB (1,048,576 UTF-8 bytes); the `manualText` field also has a 1,048,576-character schema bound. `show` returns JSON with the **complete** `text`, stable `itemId`, `revision`, dimensions and `expectedVersion`, without opening an edit session. Width and height are positive canvas coordinate units independent of zoom; the shared backend permits smaller values than the UI handle minimum. Creation defaults to 352 × 198; `--x` and `--y` are required. + +Updates and resizes require the `canvas_item` revision returned by `show`. Receipts include `status`, `itemId` and `canvasItems` with committed revisions and dimensions. Locks and stale revisions are rejected with exit 1 and an issue code/message; usage errors exit 2. Keep your source file, read the latest text and merge before a new attempt. Agent Shell and ordinary HTTP CLI use the same business services. Text registration and canvas publication are separate commits: if publication fails, the registered asset may remain unbound. Use the same global operation ID and identical file/options to retry without registering a duplicate; do not reuse it for a changed edit. A geometry read failure is reported as `geometryReadError` alongside the committed receipt. + +`--file` 接收 UTF-8 Markdown 而非 JSON 命令文档,`-` 从标准输入读取,无须对引号或代码围栏进行 Shell 转义。文本资产限制为 1 MiB(1,048,576 个 UTF-8 字节);`manualText` 字段另有 1,048,576 字符的契约限制。`show` 不申请编辑会话,返回包含完整 `text`、稳定 `itemId`、`revision`、宽高及 `expectedVersion` 的 JSON。宽高为与缩放无关的正数画布单位,共用后端允许小于界面手柄最小尺寸的数值。创建默认 352 × 198,必须提供 `--x`、`--y`。 + +更新和缩放必须携带 `show` 返回的 `canvas_item` 版本。回执包含 `status`、`itemId` 及带已提交版本和尺寸的 `canvasItems`。锁定或旧版本冲突返回退出码 1 及错误码、说明,参数用法错误返回 2。保留源码文件,重新读取最新正文并合并后再尝试。Agent Shell 与普通 HTTP CLI 复用相同业务服务。文本登记和画布采用是两个提交:采用失败时,已登记资产可能尚未绑定。使用相同全局操作 ID 和完全相同的文件、选项重试可复用资产,修改输入后应使用新的 ID。几何回读失败通过已提交回执中的 `geometryReadError` 单独报告。 + +`canvas text show` reads `/api/creation/projects/{projectId}/assets/{assetId}/content` through a bounded HTTP stream on both CLI transports, retaining UTF-8 BOM and CRLF. Its cap is `MAX_CREATION_TEXT_BYTES` (1,048,576 bytes); the shared agent `read` tool still uses its original 64 KiB budget. The API address comes from `--url` or `OPENDIRECTOR_API_URL`, which the application launcher supplies to Agent Shell. + +`canvas text show` 在两种 CLI 传输方式下都通过有界 HTTP 流读取 `/api/creation/projects/{projectId}/assets/{assetId}/content`,保留 UTF-8 BOM 和 CRLF。上限为 `MAX_CREATION_TEXT_BYTES`(1,048,576 字节);共用 Agent `read` 工具仍保留原有的 64 KiB 预算。API 地址来自 `--url` 或 `OPENDIRECTOR_API_URL`,应用启动器会将该环境变量传给 Agent Shell。 diff --git a/packages/core/src/opendirector/cli.py b/packages/core/src/opendirector/cli.py index 2b66ec8ad..a34526631 100644 --- a/packages/core/src/opendirector/cli.py +++ b/packages/core/src/opendirector/cli.py @@ -430,6 +430,149 @@ def add_material(ctx: typer.Context, asset: Annotated[UUID, typer.Option(help="R }})) +text_app = typer.Typer(no_args_is_help=True, + help="Create, read, edit and resize UTF-8 Markdown text nodes. Dimensions use canvas coordinate units, independent of zoom.", + epilog="Text assets are limited to 1 MiB of UTF-8 bytes (manualText also has a 1,048,576-character bound). " + "Output is JSON; show includes complete source in text. Writes return status, itemId and canvasItems " + "with committed revisions and dimensions. Rejected locks/conflicts exit 1; usage errors exit 2. " + "Use show to read the latest revision before updating; keep your file on a conflict. " + "Text registration and canvas publication have separate receipts: a failed publication can leave an " + "unbound asset. Retry identical input with the same global --operation-id to reuse it.") +group("canvas").add_typer(text_app, name="text") + +MarkdownFile = Annotated[Path, typer.Option(help="UTF-8 Markdown file, not JSON; - reads standard input. Maximum 1 MiB.")] +TextWidth = Annotated[float, typer.Option(help="Width in canvas coordinate units, independent of viewport zoom; must be positive.")] +TextHeight = Annotated[float, typer.Option(help="Height in canvas coordinate units, independent of viewport zoom; must be positive.")] +TextVersion = Annotated[str, typer.Option(help="Required canvas_item:UUID:revision from text show. Stale revisions are rejected; read again and merge your file.")] + + +def markdown_file(file): + from opendirector.ports.files import MAX_CREATION_TEXT_BYTES + if str(file) == "-": + content = sys.stdin.buffer.read(MAX_CREATION_TEXT_BYTES + 1) + else: + with file.open("rb") as stream: + content = stream.read(MAX_CREATION_TEXT_BYTES + 1) + if len(content) > MAX_CREATION_TEXT_BYTES: + raise ValueError("Markdown must not exceed 1 MiB of UTF-8 bytes.") + return content.decode("utf-8") + + +def text_content(client, asset_id): + from opendirector.ports.files import MAX_CREATION_TEXT_BYTES + content = bytearray() + with client.http.stream("GET", client.path(f"/assets/{asset_id}/content")) as response: + if not response.is_success: + response.read() + raise client.response_error(response) + for chunk in response.iter_bytes(chunk_size=65536): + if len(content) + len(chunk) > MAX_CREATION_TEXT_BYTES: + raise ValueError("Markdown must not exceed 1 MiB of UTF-8 bytes.") + content.extend(chunk) + return content.decode("utf-8") + + +def text_item(client, item_id, revision=None): + item = client.query("object.get", objectId=str(item_id), **({"revision": revision} if revision else {})) + if item.get("kind") != "text" or "binding" not in item: + raise ValueError("--id must identify a text canvas item.") + return item + + +def text_version(item_id, expected_version): + try: + kind, identity, revision = expected_version.split(":") + if kind != "canvas_item" or UUID(identity) != item_id or int(revision) < 1: + raise ValueError() + except (ValueError, AttributeError): + raise ValueError("--expected-version must be canvas_item::.") from None + return dict(objectType=kind, id=str(item_id), revision=int(revision)) + + +def text_asset(client, item): + binding = item["binding"] + if binding["role"] == "material": + return binding["objectRef"] + if binding["role"] == "generation": + if binding.get("materialOverride"): + return binding["materialOverride"] + spec = client.query("object.get", objectId=binding["objectRef"]["id"]) + if spec.get("currentResultId"): + return client.query("object.get", objectId=spec["currentResultId"])["asset"] + return None + + +def write_markdown(client, text, operation, *, previous=None, versions=()): + receipt = client.execute("asset.writeText", {"name": "text.md", "text": text, + "previousAsset": previous}, [*versions, *([dict(objectType="asset", id=previous["assetId"], + revision=previous["revision"])] if previous else [])], identity=uuid5(operation, "text-asset")) + asset_version = next(item for item in receipt["changed"] if item["objectType"] == "asset") + asset = client.query("object.get", objectId=asset_version["id"], revision=asset_version["revision"]) + return {"assetId": asset["id"], **{key: asset[key] for key in ("kind", "fingerprint", "revision")}} + + +@text_app.command("create", help="Create an editable text node from a Markdown file or stdin.", + epilog="Example: opendirector --project PROJECT_UUID canvas text create --file notes.md --x 20 --y 40 --width 480 --height 320") +def text_create(ctx: typer.Context, file: MarkdownFile, + x: Annotated[float, typer.Option(help="Canvas X coordinate, independent of viewport zoom.")], + y: Annotated[float, typer.Option(help="Canvas Y coordinate, independent of viewport zoom.")], + width: TextWidth = 352, height: TextHeight = 198, + title: Annotated[str | None, typer.Option(help="Optional text node title.")] = None, + item_id: Annotated[UUID | None, typer.Option(help="Optional stable canvas item UUID.")] = None): + from opendirector.domain.creation.canvas import TextCanvasItem + client = ctx.obj + client.command = ctx.command_path + operation = client.operation_id or uuid4() + item = TextCanvasItem.model_validate(dict(id=str(item_id or uuid5(operation, "text-item")), + projectId=client.project, kind="text", title=title or "", position=dict(x=x, y=y), + width=width, height=height, binding=dict(role="empty"))).model_dump(mode="json") + asset = write_markdown(client, markdown_file(file), operation) + item["binding"] = dict(role="material", objectRef=asset) + emit(client.execute("canvas.show", {"item": item}, identity=operation)) + + +@text_app.command("show", help="Read complete Markdown source, dimensions and canvas_item revision as JSON; no editing lease.", + epilog="Example: opendirector --project PROJECT_UUID canvas text show --id ITEM_UUID") +def text_show(ctx: typer.Context, id: Annotated[UUID, typer.Option(help="Text canvas item ID.")]): + client = ctx.obj + client.command = ctx.command_path + item = text_item(client, id) + asset = text_asset(client, item) + text = item["binding"].get("manualText") or "" + if asset: + text = text_content(client, asset["assetId"]) + emit(dict(status="ok", itemId=item["id"], revision=item["revision"], + expectedVersion=f"canvas_item:{item['id']}:{item['revision']}", + width=item["width"], height=item["height"], coordinateSpace="canvas", text=text)) + + +@text_app.command("update", help="Replace Markdown source via asset.writeText and canvas.materialize, preserving dimensions.", + epilog="Example: opendirector --project PROJECT_UUID canvas text update --id ITEM_UUID --file - --expected-version canvas_item:ITEM_UUID:1") +def text_update(ctx: typer.Context, id: Annotated[UUID, typer.Option(help="Text canvas item ID.")], + file: MarkdownFile, expected_version: TextVersion): + client = ctx.obj + client.command = ctx.command_path + version = text_version(id, expected_version) + item = text_item(client, id, version["revision"]) + operation = client.operation_id or uuid4() + previous = text_asset(client, item) + # shortcut: asset registration is a separate commit, use a shared atomic command if rollback across publication becomes required. + asset = write_markdown(client, markdown_file(file), operation, previous=previous, versions=[version]) + emit(client.execute("canvas.materialize", dict(itemId=str(id), asset=asset, + discardGenerationDraft=True), [version], identity=operation)) + + +@text_app.command("resize", help="Resize a text node through canvas.update without changing Markdown.", + epilog="Example: opendirector --project PROJECT_UUID canvas text resize --id ITEM_UUID --width 640 --height 400 --expected-version canvas_item:ITEM_UUID:2") +def text_resize(ctx: typer.Context, id: Annotated[UUID, typer.Option(help="Text canvas item ID.")], + width: TextWidth, height: TextHeight, expected_version: TextVersion): + client = ctx.obj + client.command = ctx.command_path + version = text_version(id, expected_version) + text_item(client, id, version["revision"]) + emit(client.execute("canvas.update", dict(itemId=str(id), width=width, height=height), [version])) + + @group("video").command("create", help="Create a visible, editable video generation node with its input references.") def video_create(ctx: typer.Context, model: Annotated[str, typer.Option(help="Video model configuration UUID or unique exact model name from opendirector model list.")], diff --git a/scripts/run-browser-suite.mjs b/scripts/run-browser-suite.mjs index b64bb9576..5254c9dd7 100644 --- a/scripts/run-browser-suite.mjs +++ b/scripts/run-browser-suite.mjs @@ -37,6 +37,7 @@ const workflows = [ { name: "mp4 export", script: "tests/browser/mp4_export.mjs" }, { name: "editor save", script: "tests/browser/editor_save.mjs" }, { name: "canvas crop", script: "tests/browser/canvas_crop.mjs" }, + { name: "canvas text Markdown", script: "tests/browser/canvas_text_markdown.mjs" }, { name: "canvas layers", script: "tests/browser/canvas_layers.mjs", diff --git a/src/canvas/CanvasActionsContext.ts b/src/canvas/CanvasActionsContext.ts index b3a170002..b255f3d23 100644 --- a/src/canvas/CanvasActionsContext.ts +++ b/src/canvas/CanvasActionsContext.ts @@ -1,6 +1,7 @@ import { createContext, type MouseEvent } from "react"; import type { CanvasNode, CanvasNodeData } from "./model"; import type { NodeGenerationActivity } from "./generationActivity"; +import type { TextSelections } from "./TextNodeEditor"; import type { SubjectMediaSlot } from "./subjectMedia.ts"; export interface CanvasActions { @@ -20,6 +21,7 @@ export interface CanvasActions { contextMenu: (id: string, event: MouseEvent) => void; beginEdit?: (id: string) => void; endEdit?: (id: string) => void; + textSelections: TextSelections; isTextEditing: (id: string) => boolean; finishTextEdit: () => void; edit: (id: string) => void; diff --git a/src/canvas/CanvasFlowSurface.tsx b/src/canvas/CanvasFlowSurface.tsx index 8f1daac34..5eb6178e7 100644 --- a/src/canvas/CanvasFlowSurface.tsx +++ b/src/canvas/CanvasFlowSurface.tsx @@ -99,6 +99,7 @@ export function CanvasFlowSurface({ }), [store], ); + const [resizing, setResizing] = useState<{ id: string; width: number; height: number } | null>(null); const [preview, setPreview] = useState(empty); const frame = useRef(null); const session = useRef<{ @@ -117,6 +118,7 @@ export function CanvasFlowSurface({ marquee.current = null; stopFrame(); session.current = null; + setResizing(null); setPreview(empty()); const state = store.getState(); if (state.userSelectionRect || state.userSelectionActive) { @@ -219,6 +221,18 @@ export function CanvasFlowSurface({ ); function changes(changes: NodeChange[]) { const current = latest.current; + const resize = changes.find((change) => change.type === "dimensions" && change.resizing !== undefined); + if (resize?.type === "dimensions" && resize.dimensions && + store.getState().nodeLookup.get(resize.id)?.type === "text") { + if (current.gesture.current.cancelled) return; + if (resize.resizing) { + setResizing({ id: resize.id, ...resize.dimensions }); + } else { + setResizing(null); + current.props.onNodesChange?.([{ ...resize, setAttributes: true }]); + } + return; + } const ordinary: NodeChange[] = []; let moved = false; for (const change of changes) { @@ -273,7 +287,10 @@ export function CanvasFlowSurface({ return ( {...props} - nodes={projected.nodes} + nodes={resizing ? projected.nodes.map((node) => node.id === resizing.id ? { + ...node, width: resizing.width, height: resizing.height, + style: { ...node.style, width: resizing.width, height: resizing.height }, + } : node) : projected.nodes} edges={projected.edges} onNodesChange={changes} onEdgesChange={(changes) => { diff --git a/src/canvas/CanvasNodeView.tsx b/src/canvas/CanvasNodeView.tsx index 5a22474f1..def8aafc5 100644 --- a/src/canvas/CanvasNodeView.tsx +++ b/src/canvas/CanvasNodeView.tsx @@ -1,5 +1,8 @@ import { memo, useContext, type MouseEvent } from "react"; -import type { Node, NodeProps } from "@xyflow/react"; +import { NodeResizeControl, type Node, type NodeProps } from "@xyflow/react"; +import { TEXT_NODE_MIN_SIZE } from "./model"; +import { TextNodeEditor } from "./TextNodeEditor"; +import { TextMarkdown } from "./TextMarkdown"; import { NodeFrame } from "./NodeFrame"; import type { CanvasNodeData, CanvasNodeKind, CanvasResource } from "./model"; import { ResourcePreview } from "./EditorModal"; @@ -77,19 +80,17 @@ function CanvasNodeContents({ id, data, selected }: NodeProps) { ) : kind === "text" ? ( actions?.isTextEditing(id) ? ( -