docx-diff-editor 1.0.43 → 1.0.44

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.
package/README.md CHANGED
@@ -257,13 +257,19 @@ await editorRef.current?.setProperties({
257
257
 
258
258
  ## Parsing HTML to JSON
259
259
 
260
- Convert HTML strings to ProseMirror JSON without visible rendering:
260
+ Convert HTML strings to ProseMirror JSON without visible rendering. **Inline styles are preserved!**
261
261
 
262
262
  ```tsx
263
263
  // Using the ref method (requires editor to be initialized)
264
264
  const json = await editorRef.current?.parseHtml('<h1>Title</h1><p>Content here</p>');
265
265
  console.log(json); // { type: 'doc', content: [...] }
266
266
 
267
+ // Inline styles are converted to marks
268
+ const styledJson = await editorRef.current?.parseHtml(
269
+ '<p><span style="color: red; font-weight: bold;">styled text</span></p>'
270
+ );
271
+ // Result: text with textStyle mark (color) and bold mark
272
+
267
273
  // Use with other methods
268
274
  await editorRef.current?.updateContent(json);
269
275
 
@@ -272,6 +278,19 @@ import { parseHtmlToJson } from 'docx-diff-editor';
272
278
  const json = await parseHtmlToJson(htmlString, SuperDocClass);
273
279
  ```
274
280
 
281
+ ### Supported Inline Styles
282
+
283
+ | CSS Property | ProseMirror Mark |
284
+ |--------------|------------------|
285
+ | `color` | `textStyle.color` |
286
+ | `font-size` | `textStyle.fontSize` |
287
+ | `font-family` | `textStyle.fontFamily` |
288
+ | `font-weight: bold` | `bold` |
289
+ | `font-style: italic` | `italic` |
290
+ | `text-decoration: underline` | `underline` |
291
+ | `text-decoration: line-through` | `strike` |
292
+ | `background-color` | `highlight.color` |
293
+
275
294
  ## Customization
276
295
 
277
296
  ### CSS Variables
package/dist/index.d.mts CHANGED
@@ -424,6 +424,18 @@ declare function detectContentType(content: DocxContent): 'file' | 'html' | 'jso
424
424
  declare function isProseMirrorJSON(content: unknown): boolean;
425
425
  /**
426
426
  * Parse an HTML string into ProseMirror JSON using a hidden SuperDoc instance.
427
+ *
428
+ * IMPORTANT: Uses the "paste" approach instead of the "import" approach.
429
+ * SuperDoc's import path (via `html` option) calls `stripHtmlStyles()` which
430
+ * removes all CSS styles except `text-align`. The paste path (via `view.pasteHTML()`)
431
+ * preserves inline styles like color, font-size, font-family, font-weight, etc.
432
+ *
433
+ * Flow:
434
+ * 1. Create SuperDoc with empty HTML document
435
+ * 2. Wait for editor to be ready
436
+ * 3. Select all content and delete it (start fresh)
437
+ * 4. Use editor.view.pasteHTML(html) - this uses the paste path which preserves styles
438
+ * 5. Return the resulting JSON
427
439
  */
428
440
  declare function parseHtmlToJson(html: string, SuperDoc: SuperDocConstructor): Promise<ProseMirrorJSON>;
429
441
  /**
package/dist/index.d.ts CHANGED
@@ -424,6 +424,18 @@ declare function detectContentType(content: DocxContent): 'file' | 'html' | 'jso
424
424
  declare function isProseMirrorJSON(content: unknown): boolean;
425
425
  /**
426
426
  * Parse an HTML string into ProseMirror JSON using a hidden SuperDoc instance.
427
+ *
428
+ * IMPORTANT: Uses the "paste" approach instead of the "import" approach.
429
+ * SuperDoc's import path (via `html` option) calls `stripHtmlStyles()` which
430
+ * removes all CSS styles except `text-align`. The paste path (via `view.pasteHTML()`)
431
+ * preserves inline styles like color, font-size, font-family, font-weight, etc.
432
+ *
433
+ * Flow:
434
+ * 1. Create SuperDoc with empty HTML document
435
+ * 2. Wait for editor to be ready
436
+ * 3. Select all content and delete it (start fresh)
437
+ * 4. Use editor.view.pasteHTML(html) - this uses the paste path which preserves styles
438
+ * 5. Return the resulting JSON
427
439
  */
428
440
  declare function parseHtmlToJson(html: string, SuperDoc: SuperDocConstructor): Promise<ProseMirrorJSON>;
429
441
  /**
package/dist/index.js CHANGED
@@ -495,8 +495,10 @@ async function parseHtmlToJson(html, SuperDoc) {
495
495
  try {
496
496
  superdoc = new SuperDoc({
497
497
  selector: container,
498
- html,
499
- documentMode: "viewing",
498
+ html: "<p></p>",
499
+ // Minimal empty document
500
+ documentMode: "editing",
501
+ // Need editing mode to use paste
500
502
  rulers: false,
501
503
  user: { name: "Parser", email: "parser@local" },
502
504
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -507,10 +509,26 @@ async function parseHtmlToJson(html, SuperDoc) {
507
509
  if (!editor) {
508
510
  throw new Error("No active editor found");
509
511
  }
510
- const json = editor.getJSON();
511
- resolved = true;
512
- cleanup();
513
- resolve(json);
512
+ const view = editor.view;
513
+ if (!view) {
514
+ throw new Error("No editor view found");
515
+ }
516
+ editor.commands.selectAll();
517
+ editor.commands.deleteSelection();
518
+ view.pasteHTML(html);
519
+ setTimeout(() => {
520
+ if (resolved) return;
521
+ try {
522
+ const json = editor.getJSON();
523
+ resolved = true;
524
+ cleanup();
525
+ resolve(json);
526
+ } catch (err) {
527
+ resolved = true;
528
+ cleanup();
529
+ reject(err);
530
+ }
531
+ }, 50);
514
532
  } catch (err) {
515
533
  resolved = true;
516
534
  cleanup();