wagtail-react-streamfield icon indicating copy to clipboard operation
wagtail-react-streamfield copied to clipboard

Copying an entire StreamField (wagtail-modeltranslation)

Open jordanmt opened this issue 4 years ago • 1 comments

We use wagtail-modeltranslation for internationalizing content on a per-field basis. StreamField content will often contain references that do not need to change between languages, so the workflow for translating a StreamField tends to be:

  1. Create the content in the first language.
  2. Copy the content from the base language's StreamField into a target language's StreamField (Wagtail Modeltranslation adds copy buttons alongside the StreamField label)
  3. Edit text for the new language for CharBlocks and other text fields, while skipping things like references to images, links to internal pages, and so on.

Let's say we have a StreamField named content and two languages, English and French. Modeltranslation means that we end up with the fields content_en and content_fr. At first look, I think it will be possible to emulate the same behaviour that exists in Wagtail Modeltranslation for the old streamfields, adding copy buttons to the DOM that call streamField.init() like this:

window.streamField.init('content_targetlanguage', 'content_sourcelanguage')

Where content_sourcelanguage is the serialized options data used to initialize the field.

I'm poking around to understand the codebase more to work on this, but I'd like to make sure I'm not underestimating the task or totally abusing streamField.init() by using it this way. Hence posting here as opposed to wagtail-modeltranslation.

Some initial questions and findings:

  • The copy function would need to be able to gather the streamfield config and the values as they exists on the client at the time of copying. It looks like it would be workable to take the initial streamfield config (e.g. the blockDefinitions) from one of the 's, but grab the values from the source field's hidden input.
  • Stripping IDs from the source value appears to ensure that new ones are generated
  • Running init() as above does indeed copy the source to the target successfully, but the streamform elements are rendered twice (one full set followed by a + icon followed by a second full set). This doesn't appear to affect the functioning of the streamfield (changes can be made and saved on the first set). Is there a fundamental reason why this might be happening?
  • Are there potentially other side effects from using streamField.init() this way that I'm not accounting for?

I also just wanted to say congrats on all the hard work, we've been following since the campaign :tada:

jordanmt avatar Sep 30 '19 23:09 jordanmt

would be great to have these two libraries working together out of the box

ayushin avatar Oct 26 '20 16:10 ayushin