Skip to content

Conversation

@caio-pizzol
Copy link
Contributor

Remove manual field ID generation in template-builder to leverage SuperDoc's automatic numeric ID creation for Word compatibility, and update related types.

The template-builder previously generated string IDs (field_${Date.now()}) which bypassed SuperDoc's internal mechanism for generating proper signed 32-bit integer IDs, leading to potential compatibility issues with Microsoft Word. This change delegates ID generation entirely to SuperDoc and updates the TemplateField interface and related functions to accept string | number for IDs. After field insertion, the component now refreshes its state by re-reading fields from the editor to capture the SuperDoc-generated ID.


Linear Issue: SD-548

Open in Cursor Open in Web

Co-authored-by: caiopizzol <caiopizzol@gmail.com>
@cursor
Copy link

cursor bot commented Oct 9, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@linear
Copy link

linear bot commented Oct 9, 2025

SD-548 Use numeric IDs for fields (XML/Word compatibility)

Summary

Remove ID generation from template-builder and let SuperDoc library handle it automatically using its proper generateRandomSigned32BitIntStrId() function

Tasks

  • Remove const fieldId = \field_${Date.now()}`line frominsertFieldInternal`
  • Remove id: fieldId from attrs passed to insertStructuredContentInline
  • Remove id: fieldId from attrs passed to insertStructuredContentBlock
  • Update TemplateField interface - keep id as string | number since SuperDoc returns it
  • Test that fields are created with proper numeric IDs and work in Microsoft Word

Notes

Current problem (src/index.tsx):

const fieldId = `field_${Date.now()}`; // ❌ Template builder generates string ID

editor.commands.insertStructuredContentInline?.({
  attrs: {
    id: fieldId, // ❌ Passes ID to SuperDoc, bypassing auto-generation
    alias: field.alias,

SuperDoc already handles this (structured-content-commands.js):

const attrs = {
  ...options.attrs,
  id: options.attrs?.id || generateRandomSigned32BitIntStrId(), // ✅ Auto-generates if not provided
  tag: 'inline_text_sdt',
  alias: options.attrs?.alias || 'Structured content',
};

The fix - just delete the ID code:

// DELETE: const fieldId = `field_${Date.now()}`;

editor.commands.insertStructuredContentInline?.({
  attrs: {
    // DELETE: id: fieldId,
    alias: field.alias,
    tag: field.category,
  },
  text: field.defaultValue || field.alias,
});

Why this is better:

  • SuperDoc knows the correct ID format (generateRandomSigned32BitIntStrId returns proper signed 32-bit int)
  • Template builder doesn't need to know about ID generation
  • Simpler code - fewer responsibilities
  • IDs are guaranteed to be Word-compatible

After the fix: SuperDoc will automatically generate proper numeric IDs for all new fields

Testing: Create fields → export to .docx → open in Microsoft Word → verify SDT tags work correctly

@caio-pizzol caio-pizzol closed this Oct 9, 2025
@caio-pizzol caio-pizzol deleted the cursor/SD-548-remove-custom-field-id-generation-2767 branch October 9, 2025 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants