@moldable-ai/editor 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export { Markdown } from '@moldable/ui';
2
+ //# sourceMappingURL=markdown.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/components/markdown.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA"}
@@ -0,0 +1,2 @@
1
+ // Re-export from @moldable/ui where the Markdown component now lives
2
+ export { Markdown } from '@moldable/ui';
@@ -0,0 +1,28 @@
1
+ import { type DOMConversionMap, type DOMExportOutput, type EditorConfig, type LexicalNode, type NodeKey, type SerializedTextNode, type Spread, TextNode } from 'lexical';
2
+ export type SerializedMentionNode = Spread<{
3
+ username: string;
4
+ }, SerializedTextNode>;
5
+ /**
6
+ * MentionNode - A custom node for @mentions
7
+ * Extends TextNode to inherit text behavior but with custom styling
8
+ */
9
+ export declare class MentionNode extends TextNode {
10
+ __username: string;
11
+ static getType(): string;
12
+ static clone(node: MentionNode): MentionNode;
13
+ constructor(username: string, key?: NodeKey);
14
+ createDOM(config: EditorConfig): HTMLElement;
15
+ updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean;
16
+ static importDOM(): DOMConversionMap | null;
17
+ exportDOM(): DOMExportOutput;
18
+ static importJSON(serializedNode: SerializedMentionNode): MentionNode;
19
+ exportJSON(): SerializedMentionNode;
20
+ getUsername(): string;
21
+ getTextContent(): string;
22
+ canInsertTextBefore(): boolean;
23
+ canInsertTextAfter(): boolean;
24
+ isTextEntity(): true;
25
+ }
26
+ export declare function $createMentionNode(username: string): MentionNode;
27
+ export declare function $isMentionNode(node: LexicalNode | null | undefined): node is MentionNode;
28
+ //# sourceMappingURL=mention-node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mention-node.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/mention-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,gBAAgB,EAErB,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACvB,KAAK,MAAM,EACX,QAAQ,EACT,MAAM,SAAS,CAAA;AAEhB,MAAM,MAAM,qBAAqB,GAAG,MAAM,CACxC;IACE,QAAQ,EAAE,MAAM,CAAA;CACjB,EACD,kBAAkB,CACnB,CAAA;AAiBD;;;GAGG;AACH,qBAAa,WAAY,SAAQ,QAAQ;IACvC,UAAU,EAAE,MAAM,CAAA;IAElB,MAAM,CAAC,OAAO,IAAI,MAAM;IAIxB,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,WAAW,GAAG,WAAW;gBAIhC,QAAQ,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO;IAK3C,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,WAAW;IAQ5C,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO;IAQ1E,MAAM,CAAC,SAAS,IAAI,gBAAgB,GAAG,IAAI;IAc3C,SAAS,IAAI,eAAe;IAQ5B,MAAM,CAAC,UAAU,CAAC,cAAc,EAAE,qBAAqB,GAAG,WAAW;IASrE,UAAU,IAAI,qBAAqB;IAQnC,WAAW,IAAI,MAAM;IAIrB,cAAc,IAAI,MAAM;IAIxB,mBAAmB,IAAI,OAAO;IAI9B,kBAAkB,IAAI,OAAO;IAI7B,YAAY,IAAI,IAAI;CAGrB;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,WAAW,CAIhE;AAED,wBAAgB,cAAc,CAC5B,IAAI,EAAE,WAAW,GAAG,IAAI,GAAG,SAAS,GACnC,IAAI,IAAI,WAAW,CAErB"}
@@ -0,0 +1,102 @@
1
+ import { $applyNodeReplacement, TextNode, } from 'lexical';
2
+ function $convertMentionElement(domNode) {
3
+ const textContent = domNode.textContent;
4
+ if (textContent !== null) {
5
+ // Extract username from @username format
6
+ const username = textContent.startsWith('@')
7
+ ? textContent.slice(1)
8
+ : textContent;
9
+ const node = $createMentionNode(username);
10
+ return { node };
11
+ }
12
+ return null;
13
+ }
14
+ /**
15
+ * MentionNode - A custom node for @mentions
16
+ * Extends TextNode to inherit text behavior but with custom styling
17
+ */
18
+ export class MentionNode extends TextNode {
19
+ __username;
20
+ static getType() {
21
+ return 'mention';
22
+ }
23
+ static clone(node) {
24
+ return new MentionNode(node.__username, node.__key);
25
+ }
26
+ constructor(username, key) {
27
+ super(`@${username}`, key);
28
+ this.__username = username;
29
+ }
30
+ createDOM(config) {
31
+ const dom = super.createDOM(config);
32
+ dom.className = 'mention';
33
+ dom.setAttribute('data-lexical-mention', 'true');
34
+ dom.setAttribute('data-mention-username', this.__username);
35
+ return dom;
36
+ }
37
+ updateDOM(prevNode, dom, config) {
38
+ const isUpdated = super.updateDOM(prevNode, dom, config);
39
+ if (prevNode.__username !== this.__username) {
40
+ dom.setAttribute('data-mention-username', this.__username);
41
+ }
42
+ return isUpdated;
43
+ }
44
+ static importDOM() {
45
+ return {
46
+ span: (domNode) => {
47
+ if (domNode.hasAttribute('data-lexical-mention')) {
48
+ return {
49
+ conversion: $convertMentionElement,
50
+ priority: 1,
51
+ };
52
+ }
53
+ return null;
54
+ },
55
+ };
56
+ }
57
+ exportDOM() {
58
+ const element = document.createElement('span');
59
+ element.setAttribute('data-lexical-mention', 'true');
60
+ element.setAttribute('data-mention-username', this.__username);
61
+ element.textContent = this.getTextContent();
62
+ return { element };
63
+ }
64
+ static importJSON(serializedNode) {
65
+ const node = $createMentionNode(serializedNode.username);
66
+ node.setFormat(serializedNode.format);
67
+ node.setDetail(serializedNode.detail);
68
+ node.setMode(serializedNode.mode);
69
+ node.setStyle(serializedNode.style);
70
+ return node;
71
+ }
72
+ exportJSON() {
73
+ return {
74
+ ...super.exportJSON(),
75
+ type: 'mention',
76
+ username: this.__username,
77
+ };
78
+ }
79
+ getUsername() {
80
+ return this.__username;
81
+ }
82
+ getTextContent() {
83
+ return `@${this.__username}`;
84
+ }
85
+ canInsertTextBefore() {
86
+ return false;
87
+ }
88
+ canInsertTextAfter() {
89
+ return false;
90
+ }
91
+ isTextEntity() {
92
+ return true;
93
+ }
94
+ }
95
+ export function $createMentionNode(username) {
96
+ const mentionNode = new MentionNode(username);
97
+ mentionNode.setMode('segmented').toggleDirectionless();
98
+ return $applyNodeReplacement(mentionNode);
99
+ }
100
+ export function $isMentionNode(node) {
101
+ return node instanceof MentionNode;
102
+ }
@@ -0,0 +1,53 @@
1
+ import { ElementNode, LexicalEditor, LexicalNode } from 'lexical';
2
+ /**
3
+ * A block (paragraph, heading, etc.) ready for translation
4
+ */
5
+ export interface TranslatableBlock {
6
+ /** The block node's key */
7
+ nodeKey: string;
8
+ /** XML string with text wrapped in <x id="nodeKey"> tags */
9
+ xml: string;
10
+ /** Map of text node keys to their format info */
11
+ formatMap: Map<string, number>;
12
+ /** Original node keys in order (for rebuilding if order changes) */
13
+ nodeOrder: string[];
14
+ }
15
+ /**
16
+ * Extract translatable blocks from the editor.
17
+ * Each block's text nodes are wrapped in XML tags with their node keys as IDs.
18
+ */
19
+ export declare function $extractTranslatableBlocks(_editor: LexicalEditor): TranslatableBlock[];
20
+ /**
21
+ * Parse translated XML response, extracting text by node ID.
22
+ * Returns a map of nodeKey -> translated text, plus the new order of nodes.
23
+ */
24
+ export declare function parseTranslatedXml(xml: string): {
25
+ translations: Map<string, string>;
26
+ newOrder: string[];
27
+ };
28
+ /**
29
+ * Apply translations to a block node.
30
+ * Rebuilds the text nodes with translated content, preserving original formatting.
31
+ */
32
+ export declare function $applyBlockTranslation(blockNode: ElementNode, translations: Map<string, string>, formatMap: Map<string, number>, newOrder: string[]): void;
33
+ /**
34
+ * Get a node by its key from the editor
35
+ */
36
+ export declare function $getNodeByKey(key: string): LexicalNode | null;
37
+ export interface TranslatableSegment {
38
+ nodeKey: string;
39
+ text: string;
40
+ type: string;
41
+ }
42
+ /** @deprecated Use $extractTranslatableBlocks instead */
43
+ export declare function $extractTranslatableSegments(): TranslatableSegment[];
44
+ /** @deprecated */
45
+ export declare function $applyTranslations(translations: Map<string, string>): void;
46
+ /** @deprecated */
47
+ export declare function $getTextSegmentsForTranslation(): {
48
+ texts: string[];
49
+ nodeKeys: string[];
50
+ };
51
+ /** @deprecated Use block-level translation */
52
+ export declare function translateEditorState(editor: LexicalEditor, translateFn: (texts: string[]) => Promise<string[]>): Promise<string>;
53
+ //# sourceMappingURL=translation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translation.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/translation.ts"],"names":[],"mappings":"AAIA,OAAO,EAOL,WAAW,EACX,aAAa,EACb,WAAW,EACZ,MAAM,SAAS,CAAA;AAEhB;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2BAA2B;IAC3B,OAAO,EAAE,MAAM,CAAA;IACf,4DAA4D;IAC5D,GAAG,EAAE,MAAM,CAAA;IACX,iDAAiD;IACjD,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,oEAAoE;IACpE,SAAS,EAAE,MAAM,EAAE,CAAA;CACpB;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,aAAa,GACrB,iBAAiB,EAAE,CAkFrB;AAUD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG;IAC/C,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,QAAQ,EAAE,MAAM,EAAE,CAAA;CACnB,CAgBA;AAUD;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,WAAW,EACtB,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EACjC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,QAAQ,EAAE,MAAM,EAAE,GACjB,IAAI,CAeN;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAE7D;AAMD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;CACb;AAED,yDAAyD;AACzD,wBAAgB,4BAA4B,IAAI,mBAAmB,EAAE,CA0BpE;AAED,kBAAkB;AAClB,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAmB1E;AAED,kBAAkB;AAClB,wBAAgB,8BAA8B,IAAI;IAChD,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,QAAQ,EAAE,MAAM,EAAE,CAAA;CACnB,CAqBA;AAED,8CAA8C;AAC9C,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,GAClD,OAAO,CAAC,MAAM,CAAC,CA2EjB"}
@@ -0,0 +1,267 @@
1
+ import { $isCodeNode } from '@lexical/code';
2
+ import { $isLinkNode } from '@lexical/link';
3
+ import { $isListItemNode } from '@lexical/list';
4
+ import { $isHeadingNode, $isQuoteNode } from '@lexical/rich-text';
5
+ import { $createTextNode, $getRoot, $isElementNode, $isParagraphNode, $isTextNode, $getNodeByKey as $lexicalGetNodeByKey, } from 'lexical';
6
+ /**
7
+ * Extract translatable blocks from the editor.
8
+ * Each block's text nodes are wrapped in XML tags with their node keys as IDs.
9
+ */
10
+ export function $extractTranslatableBlocks(_editor) {
11
+ const blocks = [];
12
+ const root = $getRoot();
13
+ function processBlockNode(node) {
14
+ const formatMap = new Map();
15
+ const nodeOrder = [];
16
+ let xml = '';
17
+ function processChildren(parent) {
18
+ if ($isTextNode(parent)) {
19
+ const key = parent.getKey();
20
+ const text = parent.getTextContent();
21
+ const format = parent.getFormat();
22
+ formatMap.set(key, format);
23
+ nodeOrder.push(key);
24
+ xml += `<x id="${key}">${escapeXml(text)}</x>`;
25
+ }
26
+ else if ($isLinkNode(parent)) {
27
+ // For links, wrap the whole link content but track inner text nodes
28
+ const href = parent.getURL();
29
+ const linkKey = parent.getKey();
30
+ xml += `<a href="${escapeXml(href)}" id="${linkKey}">`;
31
+ for (const child of parent.getChildren()) {
32
+ processChildren(child);
33
+ }
34
+ xml += '</a>';
35
+ }
36
+ else if ($isElementNode(parent)) {
37
+ for (const child of parent.getChildren()) {
38
+ processChildren(child);
39
+ }
40
+ }
41
+ }
42
+ processChildren(node);
43
+ // Skip empty blocks
44
+ if (!xml.trim() || nodeOrder.length === 0) {
45
+ return null;
46
+ }
47
+ return {
48
+ nodeKey: node.getKey(),
49
+ xml,
50
+ formatMap,
51
+ nodeOrder,
52
+ };
53
+ }
54
+ function walkTree(node) {
55
+ // Skip code blocks - don't translate code
56
+ if ($isCodeNode(node)) {
57
+ return;
58
+ }
59
+ // These are our translatable block types
60
+ if ($isParagraphNode(node) ||
61
+ $isHeadingNode(node) ||
62
+ $isQuoteNode(node) ||
63
+ $isListItemNode(node)) {
64
+ const block = processBlockNode(node);
65
+ if (block) {
66
+ blocks.push(block);
67
+ }
68
+ return;
69
+ }
70
+ // For lists and other containers, recurse into children
71
+ if ($isElementNode(node)) {
72
+ for (const child of node.getChildren()) {
73
+ walkTree(child);
74
+ }
75
+ }
76
+ }
77
+ for (const child of root.getChildren()) {
78
+ walkTree(child);
79
+ }
80
+ return blocks;
81
+ }
82
+ function escapeXml(text) {
83
+ return text
84
+ .replace(/&/g, '&amp;')
85
+ .replace(/</g, '&lt;')
86
+ .replace(/>/g, '&gt;')
87
+ .replace(/"/g, '&quot;');
88
+ }
89
+ /**
90
+ * Parse translated XML response, extracting text by node ID.
91
+ * Returns a map of nodeKey -> translated text, plus the new order of nodes.
92
+ */
93
+ export function parseTranslatedXml(xml) {
94
+ const translations = new Map();
95
+ const newOrder = [];
96
+ // Simple regex-based parser for our known XML structure
97
+ // Matches <x id="...">content</x> and <a href="..." id="...">...</a>
98
+ const tagRegex = /<x id="([^"]+)">([^<]*)<\/x>/g;
99
+ let match;
100
+ while ((match = tagRegex.exec(xml)) !== null) {
101
+ const [, id, content] = match;
102
+ translations.set(id, unescapeXml(content));
103
+ newOrder.push(id);
104
+ }
105
+ return { translations, newOrder };
106
+ }
107
+ function unescapeXml(text) {
108
+ return text
109
+ .replace(/&quot;/g, '"')
110
+ .replace(/&gt;/g, '>')
111
+ .replace(/&lt;/g, '<')
112
+ .replace(/&amp;/g, '&');
113
+ }
114
+ /**
115
+ * Apply translations to a block node.
116
+ * Rebuilds the text nodes with translated content, preserving original formatting.
117
+ */
118
+ export function $applyBlockTranslation(blockNode, translations, formatMap, newOrder) {
119
+ // Clear existing children
120
+ blockNode.clear();
121
+ // Rebuild in the new order with translated text and original formatting
122
+ for (const nodeKey of newOrder) {
123
+ const translatedText = translations.get(nodeKey);
124
+ const format = formatMap.get(nodeKey) ?? 0;
125
+ if (translatedText !== undefined) {
126
+ const textNode = $createTextNode(translatedText);
127
+ textNode.setFormat(format);
128
+ blockNode.append(textNode);
129
+ }
130
+ }
131
+ }
132
+ /**
133
+ * Get a node by its key from the editor
134
+ */
135
+ export function $getNodeByKey(key) {
136
+ return $lexicalGetNodeByKey(key);
137
+ }
138
+ /** @deprecated Use $extractTranslatableBlocks instead */
139
+ export function $extractTranslatableSegments() {
140
+ const segments = [];
141
+ const root = $getRoot();
142
+ function extractFromNode(node) {
143
+ if ($isTextNode(node)) {
144
+ const text = node.getTextContent();
145
+ if (text.trim()) {
146
+ segments.push({
147
+ nodeKey: node.getKey(),
148
+ text: text,
149
+ type: 'text',
150
+ });
151
+ }
152
+ }
153
+ else if ($isElementNode(node)) {
154
+ for (const child of node.getChildren()) {
155
+ extractFromNode(child);
156
+ }
157
+ }
158
+ }
159
+ for (const child of root.getChildren()) {
160
+ extractFromNode(child);
161
+ }
162
+ return segments;
163
+ }
164
+ /** @deprecated */
165
+ export function $applyTranslations(translations) {
166
+ const root = $getRoot();
167
+ function applyToNode(node) {
168
+ if ($isTextNode(node)) {
169
+ const translation = translations.get(node.getKey());
170
+ if (translation !== undefined) {
171
+ node.setTextContent(translation);
172
+ }
173
+ }
174
+ else if ($isElementNode(node)) {
175
+ for (const child of node.getChildren()) {
176
+ applyToNode(child);
177
+ }
178
+ }
179
+ }
180
+ for (const child of root.getChildren()) {
181
+ applyToNode(child);
182
+ }
183
+ }
184
+ /** @deprecated */
185
+ export function $getTextSegmentsForTranslation() {
186
+ const texts = [];
187
+ const nodeKeys = [];
188
+ const root = $getRoot();
189
+ function processNode(node) {
190
+ if ($isTextNode(node)) {
191
+ texts.push(node.getTextContent());
192
+ nodeKeys.push(node.getKey());
193
+ }
194
+ else if ($isElementNode(node)) {
195
+ for (const child of node.getChildren()) {
196
+ processNode(child);
197
+ }
198
+ }
199
+ }
200
+ for (const child of root.getChildren()) {
201
+ processNode(child);
202
+ }
203
+ return { texts, nodeKeys };
204
+ }
205
+ /** @deprecated Use block-level translation */
206
+ export async function translateEditorState(editor, translateFn) {
207
+ const segments = await new Promise((resolve) => {
208
+ editor.getEditorState().read(() => {
209
+ resolve($extractTranslatableSegments());
210
+ });
211
+ });
212
+ if (segments.length === 0) {
213
+ return '';
214
+ }
215
+ const textsToTranslate = segments.map((s) => s.text);
216
+ const translations = await translateFn(textsToTranslate);
217
+ const translationsByIndex = new Map();
218
+ translations.forEach((translation, index) => {
219
+ translationsByIndex.set(index, translation);
220
+ });
221
+ const { createMoldableHeadlessEditor } = await import('./headless-editor');
222
+ const translatedEditor = createMoldableHeadlessEditor();
223
+ const { $convertToMarkdownString, $convertFromMarkdownString, markdownTransformers, } = await import('./markdown-transformers');
224
+ const originalMarkdown = await new Promise((resolve) => {
225
+ editor.getEditorState().read(() => {
226
+ resolve($convertToMarkdownString({ transformers: markdownTransformers }));
227
+ });
228
+ });
229
+ await new Promise((resolve) => {
230
+ translatedEditor.update(() => {
231
+ $convertFromMarkdownString({
232
+ markdown: originalMarkdown,
233
+ transformers: markdownTransformers,
234
+ });
235
+ resolve();
236
+ });
237
+ });
238
+ await new Promise((resolve) => {
239
+ translatedEditor.update(() => {
240
+ let index = 0;
241
+ const root = $getRoot();
242
+ function applyToNode(node) {
243
+ if ($isTextNode(node)) {
244
+ const translation = translationsByIndex.get(index);
245
+ if (translation !== undefined) {
246
+ node.setTextContent(translation);
247
+ }
248
+ index++;
249
+ }
250
+ else if ($isElementNode(node)) {
251
+ for (const child of node.getChildren()) {
252
+ applyToNode(child);
253
+ }
254
+ }
255
+ }
256
+ for (const child of root.getChildren()) {
257
+ applyToNode(child);
258
+ }
259
+ resolve();
260
+ });
261
+ });
262
+ return new Promise((resolve) => {
263
+ translatedEditor.getEditorState().read(() => {
264
+ resolve($convertToMarkdownString({ transformers: markdownTransformers }));
265
+ });
266
+ });
267
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=translation.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translation.test.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/translation.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,329 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createMoldableHeadlessEditor } from './headless-editor';
3
+ import { $extractTranslatableSegments, $applyTranslations, $getTextSegmentsForTranslation, translateEditorState, } from './translation';
4
+ import { $convertFromMarkdownString, $convertToMarkdownString, markdownTransformers } from './markdown-transformers';
5
+ describe('Translation Utilities', () => {
6
+ describe('$extractTranslatableSegments', () => {
7
+ it('extracts text from a simple paragraph', async () => {
8
+ const editor = createMoldableHeadlessEditor();
9
+ await new Promise((resolve) => {
10
+ editor.update(() => {
11
+ $convertFromMarkdownString({ markdown: 'Hello world' });
12
+ resolve();
13
+ });
14
+ });
15
+ const segments = await new Promise((resolve) => {
16
+ editor.getEditorState().read(() => {
17
+ resolve($extractTranslatableSegments());
18
+ });
19
+ });
20
+ expect(segments).toHaveLength(1);
21
+ expect(segments[0].text).toBe('Hello world');
22
+ });
23
+ it('extracts text from multiple paragraphs', async () => {
24
+ const editor = createMoldableHeadlessEditor();
25
+ await new Promise((resolve) => {
26
+ editor.update(() => {
27
+ $convertFromMarkdownString({ markdown: 'First paragraph\n\nSecond paragraph' });
28
+ resolve();
29
+ });
30
+ });
31
+ const segments = await new Promise((resolve) => {
32
+ editor.getEditorState().read(() => {
33
+ resolve($extractTranslatableSegments());
34
+ });
35
+ });
36
+ expect(segments).toHaveLength(2);
37
+ expect(segments[0].text).toBe('First paragraph');
38
+ expect(segments[1].text).toBe('Second paragraph');
39
+ });
40
+ it('extracts text from headings', async () => {
41
+ const editor = createMoldableHeadlessEditor();
42
+ await new Promise((resolve) => {
43
+ editor.update(() => {
44
+ $convertFromMarkdownString({ markdown: '# Title\n\nContent' });
45
+ resolve();
46
+ });
47
+ });
48
+ const segments = await new Promise((resolve) => {
49
+ editor.getEditorState().read(() => {
50
+ resolve($extractTranslatableSegments());
51
+ });
52
+ });
53
+ expect(segments).toHaveLength(2);
54
+ expect(segments[0].text).toBe('Title');
55
+ expect(segments[1].text).toBe('Content');
56
+ });
57
+ it('extracts text from list items', async () => {
58
+ const editor = createMoldableHeadlessEditor();
59
+ await new Promise((resolve) => {
60
+ editor.update(() => {
61
+ $convertFromMarkdownString({ markdown: '- Item 1\n- Item 2\n- Item 3' });
62
+ resolve();
63
+ });
64
+ });
65
+ const segments = await new Promise((resolve) => {
66
+ editor.getEditorState().read(() => {
67
+ resolve($extractTranslatableSegments());
68
+ });
69
+ });
70
+ expect(segments).toHaveLength(3);
71
+ expect(segments[0].text).toBe('Item 1');
72
+ expect(segments[1].text).toBe('Item 2');
73
+ expect(segments[2].text).toBe('Item 3');
74
+ });
75
+ it('handles bold and italic text as separate segments', async () => {
76
+ const editor = createMoldableHeadlessEditor();
77
+ await new Promise((resolve) => {
78
+ editor.update(() => {
79
+ $convertFromMarkdownString({ markdown: 'This is **bold** and *italic* text' });
80
+ resolve();
81
+ });
82
+ });
83
+ const segments = await new Promise((resolve) => {
84
+ editor.getEditorState().read(() => {
85
+ resolve($extractTranslatableSegments());
86
+ });
87
+ });
88
+ // Bold and italic are separate text nodes
89
+ expect(segments.length).toBeGreaterThanOrEqual(3);
90
+ const allText = segments.map(s => s.text).join('');
91
+ expect(allText).toContain('bold');
92
+ expect(allText).toContain('italic');
93
+ });
94
+ it('extracts text from blockquotes', async () => {
95
+ const editor = createMoldableHeadlessEditor();
96
+ await new Promise((resolve) => {
97
+ editor.update(() => {
98
+ $convertFromMarkdownString({ markdown: '> This is a quote' });
99
+ resolve();
100
+ });
101
+ });
102
+ const segments = await new Promise((resolve) => {
103
+ editor.getEditorState().read(() => {
104
+ resolve($extractTranslatableSegments());
105
+ });
106
+ });
107
+ expect(segments).toHaveLength(1);
108
+ expect(segments[0].text).toBe('This is a quote');
109
+ });
110
+ });
111
+ describe('$applyTranslations', () => {
112
+ it('applies translations to text nodes', async () => {
113
+ const editor = createMoldableHeadlessEditor();
114
+ // Set up initial content
115
+ await new Promise((resolve) => {
116
+ editor.update(() => {
117
+ $convertFromMarkdownString({ markdown: 'Hello world' });
118
+ resolve();
119
+ });
120
+ });
121
+ // Get segments
122
+ const segments = await new Promise((resolve) => {
123
+ editor.getEditorState().read(() => {
124
+ resolve($extractTranslatableSegments());
125
+ });
126
+ });
127
+ // Apply translations
128
+ const translationMap = new Map();
129
+ translationMap.set(segments[0].nodeKey, 'Hej verden');
130
+ await new Promise((resolve) => {
131
+ editor.update(() => {
132
+ $applyTranslations(translationMap);
133
+ resolve();
134
+ });
135
+ });
136
+ // Verify translation was applied
137
+ const result = await new Promise((resolve) => {
138
+ editor.getEditorState().read(() => {
139
+ resolve($convertToMarkdownString({ transformers: markdownTransformers }));
140
+ });
141
+ });
142
+ expect(result).toBe('Hej verden');
143
+ });
144
+ it('preserves structure when applying translations', async () => {
145
+ const editor = createMoldableHeadlessEditor();
146
+ await new Promise((resolve) => {
147
+ editor.update(() => {
148
+ $convertFromMarkdownString({ markdown: '# Title\n\n- Item 1\n- Item 2' });
149
+ resolve();
150
+ });
151
+ });
152
+ const segments = await new Promise((resolve) => {
153
+ editor.getEditorState().read(() => {
154
+ resolve($extractTranslatableSegments());
155
+ });
156
+ });
157
+ // Translate each segment
158
+ const translationMap = new Map();
159
+ segments.forEach((seg) => {
160
+ if (seg.text === 'Title')
161
+ translationMap.set(seg.nodeKey, 'Titel');
162
+ if (seg.text === 'Item 1')
163
+ translationMap.set(seg.nodeKey, 'Element 1');
164
+ if (seg.text === 'Item 2')
165
+ translationMap.set(seg.nodeKey, 'Element 2');
166
+ });
167
+ await new Promise((resolve) => {
168
+ editor.update(() => {
169
+ $applyTranslations(translationMap);
170
+ resolve();
171
+ });
172
+ });
173
+ const result = await new Promise((resolve) => {
174
+ editor.getEditorState().read(() => {
175
+ resolve($convertToMarkdownString({ transformers: markdownTransformers }));
176
+ });
177
+ });
178
+ expect(result).toContain('# Titel');
179
+ expect(result).toContain('- Element 1');
180
+ expect(result).toContain('- Element 2');
181
+ });
182
+ });
183
+ describe('$getTextSegmentsForTranslation', () => {
184
+ it('returns parallel arrays of texts and keys', async () => {
185
+ const editor = createMoldableHeadlessEditor();
186
+ await new Promise((resolve) => {
187
+ editor.update(() => {
188
+ $convertFromMarkdownString({ markdown: 'Hello\n\nWorld' });
189
+ resolve();
190
+ });
191
+ });
192
+ const result = await new Promise((resolve) => {
193
+ editor.getEditorState().read(() => {
194
+ resolve($getTextSegmentsForTranslation());
195
+ });
196
+ });
197
+ expect(result.texts).toHaveLength(2);
198
+ expect(result.nodeKeys).toHaveLength(2);
199
+ expect(result.texts[0]).toBe('Hello');
200
+ expect(result.texts[1]).toBe('World');
201
+ });
202
+ });
203
+ describe('translateEditorState', () => {
204
+ it('translates content and returns markdown', async () => {
205
+ const editor = createMoldableHeadlessEditor();
206
+ await new Promise((resolve) => {
207
+ editor.update(() => {
208
+ $convertFromMarkdownString({ markdown: '# Hello\n\nWorld' });
209
+ resolve();
210
+ });
211
+ });
212
+ // Mock translate function
213
+ const translateFn = async (texts) => {
214
+ return texts.map((t) => {
215
+ if (t === 'Hello')
216
+ return 'Bonjour';
217
+ if (t === 'World')
218
+ return 'Monde';
219
+ return t;
220
+ });
221
+ };
222
+ const result = await translateEditorState(editor, translateFn);
223
+ expect(result).toContain('# Bonjour');
224
+ expect(result).toContain('Monde');
225
+ });
226
+ it('preserves formatting during translation', async () => {
227
+ const editor = createMoldableHeadlessEditor();
228
+ await new Promise((resolve) => {
229
+ editor.update(() => {
230
+ $convertFromMarkdownString({
231
+ markdown: '**Bold text** and *italic text*'
232
+ });
233
+ resolve();
234
+ });
235
+ });
236
+ const translateFn = async (texts) => {
237
+ return texts.map((t) => `[${t}]`); // Just wrap for testing
238
+ };
239
+ const result = await translateEditorState(editor, translateFn);
240
+ // Should preserve bold and italic markers
241
+ expect(result).toContain('**');
242
+ expect(result).toContain('*');
243
+ });
244
+ it('handles complex medical note structure', async () => {
245
+ const editor = createMoldableHeadlessEditor();
246
+ const medicalNote = `# Patient Notes
247
+
248
+ ## Chief Complaint
249
+ Patient presents with chest pain.
250
+
251
+ ## Assessment
252
+ - Stable condition
253
+ - No immediate concerns
254
+
255
+ > Doctor's note: Follow up in 2 weeks`;
256
+ await new Promise((resolve) => {
257
+ editor.update(() => {
258
+ $convertFromMarkdownString({ markdown: medicalNote });
259
+ resolve();
260
+ });
261
+ });
262
+ // Simple translation that uppercases everything
263
+ const translateFn = async (texts) => {
264
+ return texts.map((t) => t.toUpperCase());
265
+ };
266
+ const result = await translateEditorState(editor, translateFn);
267
+ // Should preserve structure
268
+ expect(result).toContain('# PATIENT NOTES');
269
+ expect(result).toContain('## CHIEF COMPLAINT');
270
+ expect(result).toContain('- STABLE CONDITION');
271
+ expect(result).toContain('>');
272
+ });
273
+ it('handles empty content', async () => {
274
+ const editor = createMoldableHeadlessEditor();
275
+ await new Promise((resolve) => {
276
+ editor.update(() => {
277
+ $convertFromMarkdownString({ markdown: '' });
278
+ resolve();
279
+ });
280
+ });
281
+ const translateFn = async (texts) => texts;
282
+ const result = await translateEditorState(editor, translateFn);
283
+ expect(result).toBe('');
284
+ });
285
+ });
286
+ describe('round-trip translation', () => {
287
+ const testCases = [
288
+ { name: 'simple paragraph', markdown: 'Hello world' },
289
+ { name: 'heading', markdown: '# Title' },
290
+ { name: 'multiple headings', markdown: '# H1\n\n## H2\n\n### H3' },
291
+ { name: 'bullet list', markdown: '- Item 1\n- Item 2\n- Item 3' },
292
+ { name: 'numbered list', markdown: '1. First\n2. Second\n3. Third' },
293
+ { name: 'blockquote', markdown: '> This is a quote' },
294
+ { name: 'bold text', markdown: '**bold**' },
295
+ { name: 'italic text', markdown: '*italic*' },
296
+ { name: 'mixed formatting', markdown: 'Normal **bold** and *italic*' },
297
+ { name: 'code inline', markdown: 'Use `code` here' },
298
+ { name: 'horizontal rule', markdown: 'Before\n\n***\n\nAfter' },
299
+ ];
300
+ testCases.forEach(({ name, markdown }) => {
301
+ it(`preserves structure for ${name}`, async () => {
302
+ const editor = createMoldableHeadlessEditor();
303
+ await new Promise((resolve) => {
304
+ editor.update(() => {
305
+ $convertFromMarkdownString({ markdown });
306
+ resolve();
307
+ });
308
+ });
309
+ // Identity translation (no change)
310
+ const translateFn = async (texts) => texts;
311
+ const result = await translateEditorState(editor, translateFn);
312
+ // Re-import and re-export to normalize
313
+ const editor2 = createMoldableHeadlessEditor();
314
+ await new Promise((resolve) => {
315
+ editor2.update(() => {
316
+ $convertFromMarkdownString({ markdown });
317
+ resolve();
318
+ });
319
+ });
320
+ const expected = await new Promise((resolve) => {
321
+ editor2.getEditorState().read(() => {
322
+ resolve($convertToMarkdownString({ transformers: markdownTransformers }));
323
+ });
324
+ });
325
+ expect(result).toBe(expected);
326
+ });
327
+ });
328
+ });
329
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moldable-ai/editor",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Rich text markdown editor for Moldable applications",
5
5
  "author": "Desiderata LLC",
6
6
  "license": "Elastic-2.0",
@@ -32,6 +32,15 @@
32
32
  "src/styles",
33
33
  "LICENSE"
34
34
  ],
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "dev": "tsc --watch",
38
+ "test": "vitest run",
39
+ "test:watch": "vitest",
40
+ "lint": "eslint . --ext .ts,.tsx --max-warnings 0",
41
+ "format": "prettier --write . --ignore-path ../../.prettierignore",
42
+ "check-types": "tsc --noEmit"
43
+ },
35
44
  "dependencies": {
36
45
  "@lexical/clipboard": "^0.39.0",
37
46
  "@lexical/code": "^0.39.0",
@@ -43,6 +52,7 @@
43
52
  "@lexical/react": "^0.39.0",
44
53
  "@lexical/rich-text": "^0.39.0",
45
54
  "@lexical/selection": "^0.39.0",
55
+ "@lexical/headless": "^0.39.0",
46
56
  "@lexical/utils": "^0.39.0",
47
57
  "lexical": "^0.39.0",
48
58
  "lucide-react": "^0.473.0",
@@ -51,31 +61,21 @@
51
61
  "remark-gfm": "^4.0.1"
52
62
  },
53
63
  "devDependencies": {
54
- "@lexical/headless": "^0.39.0",
64
+ "@moldable-ai/eslint-config": "workspace:*",
65
+ "@moldable-ai/prettier-config": "workspace:*",
66
+ "@moldable-ai/typescript-config": "workspace:*",
67
+ "@moldable-ai/ui": "workspace:^",
55
68
  "@types/jsdom": "^27.0.0",
56
69
  "@types/node": "^24.0.3",
57
70
  "@types/react": "^19.1.6",
58
71
  "@types/react-dom": "^19.1.6",
59
72
  "jsdom": "^27.4.0",
60
73
  "typescript": "^5.8.3",
61
- "vitest": "^4.0.16",
62
- "@moldable-ai/eslint-config": "0.0.1",
63
- "@moldable-ai/prettier-config": "0.0.1",
64
- "@moldable-ai/typescript-config": "0.0.1",
65
- "@moldable-ai/ui": "^0.1.1"
74
+ "vitest": "^4.0.16"
66
75
  },
67
76
  "peerDependencies": {
77
+ "@moldable-ai/ui": "workspace:^",
68
78
  "react": "^19.0.0",
69
- "react-dom": "^19.0.0",
70
- "@moldable-ai/ui": "^0.1.1"
71
- },
72
- "scripts": {
73
- "build": "tsc",
74
- "dev": "tsc --watch",
75
- "test": "vitest run",
76
- "test:watch": "vitest",
77
- "lint": "eslint . --ext .ts,.tsx --max-warnings 0",
78
- "format": "prettier --write . --ignore-path ../../.prettierignore",
79
- "check-types": "tsc --noEmit"
79
+ "react-dom": "^19.0.0"
80
80
  }
81
- }
81
+ }