The VS Code of video editing, built for agents.
TypeScript
Enhance Text Editing Functionality in JSX Elements
konstantin-paulusae33eabe52b6
diff --git a/apps/desktop/src/edit.test.ts b/apps/desktop/src/edit.test.tsindex daa31d4e..113fd843 100644--- a/apps/desktop/src/edit.test.ts+++ b/apps/desktop/src/edit.test.ts@@ -42,4 +42,36 @@ describe("applyEdits", () => {// The constant is someone else's too, and stays.expect(text).toContain(`const X = 100;`);});++ it("keeps a text's paint when what it says changes", async () => {+ await writeFile(join(dir, FILE), `export default () => <scene id="s"></scene>;\n`);++ // The draw tool's insert: the text, then its paint under it.+ await applyEdits({ dir }, [+ { kind: "insert", source: "pending#1", parent: `${FILE}:s`, tag: "text", props: {}, text: "Text" },+ { kind: "insert", source: "pending#2", parent: "pending#1", tag: "solidPaint", props: { color: "#FFFFFF" } },+ ]);+ const inserted = await readFile(join(dir, FILE), "utf8");+ const id = /<text id="([^"]+)"/.exec(inserted)![1];++ // Then typing into it, once the insert has its name.+ const result = await applyEdits({ dir }, [{ kind: "set", source: `${FILE}:${id}`, props: {}, text: "Hello" }]);++ expect(result.skipped).toEqual([]);+ const text = await readFile(join(dir, FILE), "utf8");+ expect(text).toMatch(/<text id="[^"]+">Hello\s*<solidPaint id="[^"]+" color="#FFFFFF" \/>\s*<\/text>/);+ });++ it("replaces every part of what a text says, and only that", async () => {+ await writeFile(+ join(dir, FILE),+ `export default () => (\n <text id="t">\n Hello {name}!\n <solidPaint color="#FFFFFF" />\n {/* note */}\n </text>\n);\n`,+ );++ const result = await applyEdits({ dir }, [{ kind: "set", source: `${FILE}:t`, props: {}, text: "Bye" }]);++ expect(result.skipped).toEqual([]);+ const text = await readFile(join(dir, FILE), "utf8");+ expect(text).toContain(` <text id="t">\n Bye\n <solidPaint color="#FFFFFF" />\n {/* note */}\n </text>`);+ });});diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.tsindex a9dff52c..3a9c1522 100644--- a/apps/desktop/src/edit.ts+++ b/apps/desktop/src/edit.ts@@ -257,8 +257,10 @@ function jsxText(text: string): string {/*** Replaces what an element says between its tags; a self-closing one is opened- * up around the new text. Everything outside the body keeps its bytes, so an- * element that says something else is otherwise the element it was.+ * up around the new text. Only the text is replaced: the elements it holds+ * besides (its paint, a stroke, a range) stay, and so does everything outside+ * the body, so an element that says something else is otherwise the element+ * it was.*/function setText(sourceFile: SourceFile, tag: JsxTag, text: string): void {const element = elementOf(tag);@@ -270,7 +272,44 @@ function setText(sourceFile: SourceFile, tag: JsxTag, text: string): void {return;}- sourceFile.replaceText([element.getOpeningElement().getEnd(), element.getClosingElement().getStart()], content);+ const ranges = element.getJsxChildren().filter(isSaid).map(saidRange);+ // Nothing said yet: it is said first, the way an insert spells it.+ if (!ranges.length) {+ if (content) sourceFile.insertText(element.getOpeningElement().getEnd(), content);+ return;+ }++ // The first part says all of it and the rest say nothing. Later parts go+ // first, so the positions of the earlier ones still hold.+ for (const range of ranges.slice(1).reverse()) sourceFile.replaceText(range, "");+ sourceFile.replaceText(ranges[0]!, content);+}++/**+ * Whether a child of an element is part of what it says: text JSX keeps, or+ * an expression with no element in it. A comment (an expression with nothing+ * in it) says nothing.+ */+function isSaid(child: Node): boolean {+ if (child.isKind(SyntaxKind.JsxText)) return !child.containsOnlyTriviaWhiteSpaces();+ const expression = child.asKind(SyntaxKind.JsxExpression)?.getExpression();+ if (!expression) return false;+ return !JSX_KINDS.some((kind) => expression.isKind(kind) || expression.getFirstDescendantByKind(kind));+}++const JSX_KINDS = [SyntaxKind.JsxElement, SyntaxKind.JsxSelfClosingElement, SyntaxKind.JsxFragment] as const;++/**+ * Where a part of what an element says is spelled. For text, that is without+ * the whitespace JSX drops around line breaks: the layout of the body is the+ * file's, not the text's.+ */+function saidRange(child: Node): [number, number] {+ if (!child.isKind(SyntaxKind.JsxText)) return [child.getStart(), child.getEnd()];+ const raw = child.getFullText();+ const leading = /^[ \t]*\n\s*/.exec(raw)?.[0].length ?? 0;+ const trailing = /\s*\n[ \t]*$/.exec(raw)?.[0].length ?? 0;+ return [child.getPos() + leading, child.getEnd() - trailing];}// ---------------------------------------------------------------------------