Skip to content

Conversation

@caio-pizzol
Copy link
Contributor

This commit modifies the TemplateField interface to allow the ID to be either a string or a number. It also updates the SuperDocTemplateBuilder component to handle the new ID type, ensuring that field insertion, deletion, and selection functions correctly accommodate both types. This change enhances flexibility in managing template fields.

This commit modifies the TemplateField interface to allow the ID to be either a string or a number. It also updates the SuperDocTemplateBuilder component to handle the new ID type, ensuring that field insertion, deletion, and selection functions correctly accommodate both types. This change enhances flexibility in managing template fields.
@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 enabled auto-merge (squash) October 9, 2025 20:12
cursor[bot]

This comment was marked as outdated.

This commit simplifies the mapping logic in the getTemplateFieldsFromEditor function by removing unnecessary filtering. The function now directly returns the mapped tags, enhancing readability and maintainability of the code.
@caio-pizzol caio-pizzol disabled auto-merge October 9, 2025 20:26
@caio-pizzol caio-pizzol enabled auto-merge (squash) October 9, 2025 20:26
@caio-pizzol caio-pizzol merged commit e0e6d31 into main Oct 9, 2025
2 checks passed
@caio-pizzol caio-pizzol deleted the cursor/SD-548-remove-custom-field-id-generation-2767 branch October 9, 2025 20:28
@superdoc-bot
Copy link

superdoc-bot bot commented Oct 9, 2025

🎉 This PR is included in version 0.2.0-next.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

superdoc-bot bot pushed a commit that referenced this pull request Nov 13, 2025
# [0.2.0](v0.1.0...v0.2.0) (2025-11-13)

### Bug Fixes

* force pre-release ([f752754](f752754))
* improve cleanup logic ([#11](#11)) ([01f0bf9](01f0bf9))
* update field ID type and improve field handling ([#14](#14)) ([e0e6d31](e0e6d31))
* update field IDs and categories in README and App component ([61a473d](61a473d))

### Features

* add import functionality for .docx files in the template builder ([#15](#15)) ([42faccc](42faccc))
* enhance exportTemplate functionality with configurable options ([#17](#17)) ([7e2a03d](7e2a03d))
* enhance field handling with mode support in template builder ([#16](#16)) ([d46ab5d](d46ab5d))
* implement viewport clamping for menu positioning in SuperDocTemplateBuilder ([#10](#10)) ([09e82ee](09e82ee))
@superdoc-bot
Copy link

superdoc-bot bot commented Nov 13, 2025

🎉 This PR is included in version 0.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants