Skip to content

Conversation

@wiiiimm
Copy link
Contributor

@wiiiimm wiiiimm commented Sep 15, 2025

Description

This PR fixes an issue where pasting content into empty internationalized Portable Text fields causes an error: "Attempt to apply insert patch to non-array value".

The Problem

The bug only affects the document-level "Add translation" button when buttonLocations includes 'document'. Field-level buttons and unstable_fieldAction buttons work correctly.

When using the document-level button to add a new language entry for array-based fields (like Portable Text), the value field was initialized as undefined. This causes Sanity's patch operations to fail when trying to insert content into what it expects to be an array.

The Solution

The fix modifies two components:

1. DocumentAddButtons.tsx

  • Added getInitialValueForType function that uses the useSchema hook to detect array-based fields
  • Initializes array fields with [] instead of undefined
  • Detects array fields by:
    • Checking if the field's jsonType is 'array'
    • Matching against known Portable Text field names ('body', 'htmlContent', 'blockContent', 'portableText')
    • Examining the schema type definition for array characteristics

2. InternationalizedInput.tsx

  • Added wrappedOnChange handler to intercept problematic insert patches
  • Ensures the value field exists as an empty array before applying insert operations
  • Fixes the path structure for paste operations into uninitialized fields

Testing

Tested locally with:

  • Document-level "Add translation" button with internationalizedArrayBody fields
  • Pasting content into newly created empty fields (now works without errors)
  • Field-level and unstable_fieldAction buttons (confirmed still working)
  • Various Portable Text field types

Related Issue

Fixes #104

Changes Made

  • Modified src/components/DocumentAddButtons.tsx to detect array fields and initialize them properly
  • Modified src/components/InternationalizedInput.tsx to handle paste operations into empty fields

@wiiiimm wiiiimm marked this pull request as draft September 16, 2025 05:22
- Fix document-level add locale button for Portable Text fields
- Detect array-type fields and initialize with [] instead of undefined
- Add paste operation support for empty internationalized fields
- Field-level and unstable_fieldAction buttons remain unchanged

Resolves 'Attempt to apply insert patch to non-array value' error
that occurred only with document-level locale addition buttons.
…edInput

- Ensure wrapped onChange handler correctly processes both single and multiple patch operations.
- Add checks for empty or uninitialized Portable Text fields to prevent errors during paste operations.
- Improve patch transformation logic to handle nested structures more robustly.

This update addresses issues related to applying insert patches to non-existent structures, ensuring a smoother user experience when interacting with internationalized fields.
@wiiiimm
Copy link
Contributor Author

wiiiimm commented Sep 19, 2025

@SimeonGriggs @thebiggianthead can you take a look at the issue and PR? thanks.

Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, fix those linter errors and I'll merge and release 👍

* Initial plan

* Initial progress report - linting errors identified

Co-authored-by: wiiiimm <[email protected]>

* fix: resolve all ESLint errors

Co-authored-by: wiiiimm <[email protected]>

* fix: add explicit return types and resolve all linter warnings

Co-authored-by: wiiiimm <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: wiiiimm <[email protected]>
@wiiiimm wiiiimm requested a review from stipsan October 2, 2025 19:03
@wiiiimm
Copy link
Contributor Author

wiiiimm commented Oct 2, 2025

@stipsan just fixed all lint errors

Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 💖

@stipsan stipsan merged commit 9072757 into sanity-io:main Oct 13, 2025
9 checks passed
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.

Portable Text fields throw "Attempt to apply insert patch to non-array value" when pasting content

2 participants