Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(ai-editing): let move place images and other decorator blocks (#8369)
Wolf Mermelstein0c882b20b478
diff --git a/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.ts b/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.tsindex 53bb15b2f6..6a063ee326 100644--- a/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.ts+++ b/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.ts@@ -1,18 +1,8 @@-import { $createHeadingNode } from '@lexical/rich-text';import { $getId } from '@macro-inc/lexical-core/plugins/nodeIdPlugin';-import { $createParagraphNode, $createTextNode } from 'lexical';import { describe, expect, it } from 'vitest';import { serializeWithXml } from '../utils';import { edit, read, setup, topLevelIds } from './_test-helpers';-import {- $appendBlock,- $blockNode,- $mergeBlocks,- $moveBlock,- $prependBlock,- $setBlockType,- $setText,-} from './blocks';+import { $blockNode, $mergeBlocks, $setBlockType, $setText } from './blocks';import { $blockById, $byId } from './locate';describe('block ops', () => {@@ -47,64 +37,6 @@ describe('block ops', () => {expect(xml).toContain(`id="${id}"`);});- it('$appendBlock adds blocks at the end with fresh ids', () => {- const { session, ids } = setup('first');- edit(session, () => {- const h2 = $createHeadingNode('h2');- h2.append($createTextNode('Notes'));- const p = $createParagraphNode();- p.append($createTextNode('Follow up Friday.'));- $appendBlock(h2, p);- });- const after = topLevelIds(session);- expect(after[0]).toBe(ids[0]);- expect(after).toHaveLength(3);- expect(new Set(after).size).toBe(3);- const xml = serializeWithXml(session);- expect(xml).toContain('<h2');- expect(xml).toContain('Notes');- expect(xml).toContain('Follow up Friday.');- });-- it('$prependBlock adds blocks at the top with fresh ids', () => {- const { session, ids } = setup('attendees: wolf, sara');- edit(session, () => {- const h1 = $createHeadingNode('h1');- h1.append($createTextNode('Title'));- $prependBlock(h1);- });- const after = topLevelIds(session);- expect(after).toHaveLength(2);- expect(after[1]).toBe(ids[0]);- expect(after[0]).not.toBe(ids[0]);- const xml = serializeWithXml(session);- expect(xml).toContain('<h1');- expect(xml).toContain('Title');- });-- it('$moveBlock (2-arg) relocates a block after another', () => {- const { session, ids } = setup(- '## Decisions\n\nfiller\n\nneed to finalize pricing'- );- const [decisions, , pricing] = ids;- edit(session, () =>- $moveBlock($byId(session, pricing), { placement: 'after', id: decisions })- );- expect(topLevelIds(session)).toEqual([decisions, pricing, ids[1]]);- const xml = serializeWithXml(session);- expect(xml).toContain('Decisions');- expect(xml).toContain('need to finalize pricing');- });-- it('$moveBlock supports beforeId', () => {- const { session, ids } = setup('a\n\nb\n\nc');- const [idA, idB, idC] = ids;- edit(session, () =>- $moveBlock($byId(session, idC), { placement: 'before', id: idA })- );- expect(topLevelIds(session)).toEqual([idC, idA, idB]);- });-it('$mergeBlocks merges into the first block, KEEPING its id', () => {const { session, ids } = setup("We were behind.\n\nQA hadn't started.");const [idA, idB] = ids;diff --git a/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.ts b/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.tsindex e5ff2af8f0..db5ce75b15 100644--- a/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.ts+++ b/services/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.ts@@ -5,12 +5,9 @@ import {type HeadingTagType,} from '@lexical/rich-text';import { $isCustomCodeNode } from '@macro-inc/lexical-core/nodes/CustomCodeNode';-import { $getId } from '@macro-inc/lexical-core/plugins/nodeIdPlugin';import {$createParagraphNode,$createTextNode,- $getNodeByKey,- $getRoot,$isElementNode,$isTextNode,type ElementNode,@@ -70,72 +67,6 @@ export function $setText(block: ElementNode, text: string): void {}}-/** Append pre-built block node(s) at the end of the document. */-export function $appendBlock(...nodes: ElementNode[]): ElementNode[] {- const root = $getRoot();- for (const node of nodes) {- root.append(node);- }- return nodes;-}--/** Prepend pre-built block node(s) at the top of the document. */-export function $prependBlock(...nodes: ElementNode[]): ElementNode[] {- const root = $getRoot();- const first = root.getFirstChild();- if (!first) {- for (const node of nodes) {- root.append(node);- }- return nodes;- }- for (const node of nodes) {- first.insertBefore(node);- }- return nodes;-}--/** Relocate a block to after/before another (by id). */-export function $moveBlock(- block: LexicalNode,- to: { placement: 'after' | 'before'; id: string },- session?: LexicalSession-): void {- // `to` id is resolved against the active editor state via key mappings if a- // session is provided; otherwise the target is resolved by walking the root.- const target = findTopLevelById(to.id, session);- if (!target) {- throw new Error(`No block with id "${to.id}"`);- }- block.remove();- if (to.placement === 'after') {- target.insertAfter(block);- } else {- target.insertBefore(block);- }-}--function findTopLevelById(- id: string,- session?: LexicalSession-): ElementNode | null {- if (session) {- const key = session.ids.idToNodeKeyMap.get(id);- if (key) {- const node = $getNodeByKey(key);- if (node && $isElementNode(node)) {- return node;- }- }- }- for (const child of $getRoot().getChildren()) {- if ($getId(child) === id && $isElementNode(child)) {- return child;- }- }- return null;-}-/*** Merge blocks into the first one (keeping its id). Inline content of later* blocks is appended, separated by `separator` (default `' '`). The laterdiff --git a/services/ai-editing-worker/src/ai-editing/doc/doc.test.ts b/services/ai-editing-worker/src/ai-editing/doc/doc.test.tsindex 506170c818..976486820c 100644--- a/services/ai-editing-worker/src/ai-editing/doc/doc.test.ts+++ b/services/ai-editing-worker/src/ai-editing/doc/doc.test.ts@@ -116,6 +116,40 @@ describe('Doc — structure & refs', () => {expect(out.indexOf('<hr')).toBeLessThan(out.indexOf('<h2'));});+ it('moves an image, and moves blocks after and before one', () => {+ const { session, ids } = setup('a\n\nb\n\nc');+ const [idA, idB, idC] = ids;+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'img',+ spec: {+ block: 'image',+ srcType: 'url',+ url: 'https://example.com/a.png',+ },+ at: { after: idC! },+ });+ doc.apply({ kind: 'moveNode', node: 'img', at: { after: idA! } });+ expect(+ read(session, () =>+ $getRoot()+ .getChildren()+ .map((n) => $getId(n))+ )+ ).toEqual([idA, 'img', idB, idC]);++ doc.apply({ kind: 'moveNode', node: idC!, at: { after: 'img' } });+ doc.apply({ kind: 'moveNode', node: idA!, at: { before: 'img' } });+ expect(+ read(session, () =>+ $getRoot()+ .getChildren()+ .map((n) => $getId(n))+ )+ ).toEqual([idA, 'img', idC, idB]);+ });+it('appendListItem rejects a non-list target', () => {const { session, ids } = setup('para');expect(() =>diff --git a/services/ai-editing-worker/src/ai-editing/doc/doc.ts b/services/ai-editing-worker/src/ai-editing/doc/doc.tsindex 5565182acb..5dddbe624d 100644--- a/services/ai-editing-worker/src/ai-editing/doc/doc.ts+++ b/services/ai-editing-worker/src/ai-editing/doc/doc.ts@@ -615,24 +615,9 @@ export class Doc implements DocReader, DocWriter {private moveNode(node: NodeRef, at: Position): void {this.tx(() => {- const block = this.block(node);- if ('after' in at)- blocks.$moveBlock(- block,- { placement: 'after', id: at.after },- this.session- );- else if ('before' in at)- blocks.$moveBlock(- block,- { placement: 'before', id: at.before },- this.session- );- else {- block.remove();- if ('appendToRoot' in at) blocks.$appendBlock(block);- else blocks.$prependBlock(block);- }+ const block = this.anchor(node);+ block.remove();+ this.place(block, at);});}