This issue has been created
 
 
XWiki Platform / cid:jira-generated-image-avatar-c57471d3-98cf-450a-bc86-edeec00fad13 XWIKI-25814 Open

Block metadata is not updated on undo / redo

 
View issue   ยท   Add comment
 

Issue created

 
cid:jira-generated-image-avatar-39e8b38e-d775-4e15-b3b3-afebac24335c Marius Dumitru Florea created this issue on 20/Nov/26 11:02
 
Summary: Block metadata is not updated on undo / redo
Issue Type: cid:jira-generated-image-avatar-c57471d3-98cf-450a-bc86-edeec00fad13 Bug
Affects Versions: 18.6.0-rc-1
Assignee: Unassigned
Components: BlockNote
Created: 20/Nov/26 11:02
Priority: cid:jira-generated-image-static-major-dd848a1b-8e7f-44c3-abe9-b152075c7895 Major
Reporter: Marius Dumitru Florea
Description:

The information about the edited content that the BlockNote schema can't hold (e.g. the resource reference of a link or of an image, the parameters of a block) is kept outside of the content, in a metadata store, keyed by block id, or by a synthetic id that a link carries in its URL (__xwikiLinkId). Some of this metadata is updated in place, under the same id:

  • the link modal updates the reference (and the generated label) of the edited link
  • the image editor updates the reference of the edited image

Undo / redo revert only the content (the ProseMirror history, or the Yjs undo manager when editing in realtime), not the metadata. After undoing a link or image change, the editor shows the previous target, but the metadata keeps the new reference, which wins on save.

Steps to reproduce:

  1. Edit in place, with BlockNote, a page that has a link to page A.
  2. Change the link target to page B with the link modal.
  3. Undo (Ctrl+Z): the link points to A again.
  4. Save.

Expected: the saved link points to A. Actual: it points to B. The same happens with an image whose source is changed with the image editor, with and without realtime collaboration.

The private metadata (the macro call and its output) isn't affected: the macro output is a cache, used only as long as the macro call didn't change. A related limitation: a copy / pasted block gets a new id from BlockNote, so it loses its metadata (e.g. its parameters).

Investigated solutions

Keep the shared metadata outside of the content

All the variants below keep the shared metadata in a side channel (Y.Map in the realtime session, see XWIKI-25598), so they share these cons: the seeding / joining logic, entries left behind for the blocks and links that no longer exist, and copy / paste losing the block metadata.

Add the shared metadata Y.Map to the scope of the Yjs undo manager

  • Pros: small change, the metadata store keeps its current shape.
  • Cons:
    • BlockNote (0.57) creates the undo manager with yUndoPlugin(), without options: its scope is the content fragment and it tracks only the transactions whose origin is ySyncPluginKey. Adding the map to the scope is not enough: the metadata must also be changed with a tracked origin (checked with a Yjs experiment: with the map in the scope but no tracked origin, undo still reverts only the content).
    • The metadata change and the content change are merged in a single undo step only if they are less than 500ms apart (the capture timeout).
    • It relies on BlockNote internals (yUndoPluginKey state) and must be applied again when BlockNote replaces the sync / undo plugins (e.g. when forking the document).
    • It doesn't help without realtime collaboration: BlockNote uses the ProseMirror history, and the metadata store is a plain Map.

Change the block id when its metadata is updated (the metadata of an id never changes)

  • Pros: the id is part of the content, so undo / redo restore it with and without realtime (checked with BlockNote 0.57). Concurrent edits stay consistent: the id and the other block attributes come from the same user.
  • Cons:
    • editor.updateBlock() ignores the id, so the id is changed with a ProseMirror setNodeMarkup on the blockContainer node, a BlockNote internal.
    • The metadata store needs a copy operation, because the code changing the id knows only its own entries.
    • An entry is left behind on each edit (undo may need it).
    • In realtime, the metadata must be written before the content, and an edit targeting the old id (because of a concurrent change) must be handled.

Replace the block (same block with a new id) when its metadata is updated

  • Pros: public API (replaceBlocks). y-prosemirror syncs it as an update of the attributes of the same Y.XmlElement (checked), so undo / redo and concurrent edits behave as when changing the id.
  • Cons: same as changing the id, except the BlockNote internals; the in place update relies on the y-prosemirror diff heuristic matching the nodes.

Use a synthetic id, stored in the edited content, for the metadata that is updated in place (e.g. in the fragment of the image source URL, like the link synthetic id)

  • Pros: the image editor already changes the URL, so the new id is part of the same change: undo / redo and concurrent edits stay consistent (checked), the block id doesn't change, and copy / paste keeps the reference.
  • Cons:
    • The URL fragment must be used (a query parameter is sent to the server, which breaks e.g. pre-signed URLs).
    • The image metadata handler API changes (the URL must be set before the update).
    • The id is visible wherever the URL is.
    • It doesn't apply to the blocks without such a property (e.g. a paragraph whose parameters become editable), which would need to be replaced.

Include the metadata in the content

Why it wasn't done before: the side channel was added by XWIKI-24157 (BlockNote 0.51.4) because adding a property to the propSchema of the standard BlockNote blocks failed with a TypeError in wrapInBlockStructure (the render functions used the original property schema). A generic metadata property was requested upstream (https://github.com/TypeCellOS/BlockNote/issues/1840) and rejected; the maintainers suggested extending the propSchema of the standard blocks instead. With BlockNote 0.57 this works (checked on paragraph, heading, image and bullet list item: rendering, undo / redo with and without realtime, concurrent edits, HTML copy / paste round trip).

  • Pros: undo / redo, realtime sharing and copy / paste come for free; no side channel for the shared metadata.
  • Cons: the metadata is visible in the DOM and in the copied HTML (as a data- attribute), so private metadata must stay out of it, and pasted metadata must be filtered (see below).

Multiple custom properties (one per metadata entry or per editing feature)

  • Pros: concurrent changes of different entries of the same block are merged; booleans can be stored as such.
  • Cons:
    • Each editing feature (including those contributed by extensions) has to extend all the block types, including the custom blocks added by other features.
    • In realtime, the clients must have the same schema: when y-prosemirror syncs a changed node, it removes the attributes unknown to the local schema, so a client without a feature would delete the metadata of that feature.
    • The property values are primitive, so most entries are stored as JSON anyway.

A single generic metadata property, holding a JSON object, used by all the editing features that need to store metadata

  • Pros: the same schema for all the clients; each feature reads, changes and writes back its own entries and keeps the others (even unknown ones); extensions don't change the schema.
  • Cons: concurrent changes of different entries of the same block: the last change wins; custom blocks must declare the property.

Links: the inline content can't be extended: only custom inline content has a propSchema, the default link is converted to a ProseMirror mark with only the href attribute.

  • Keep the synthetic id in the href: requires the shared Y.Map, a new id on each edit and leaves entries behind.
  • Store the metadata directly in the href (encoded JSON in a query parameter, so the metadata becomes the "id"): the metadata is part of the content (undo / redo, realtime, copy / paste) and needs no store. Cons: longer URLs (the metadata is small, usually less than 200 characters encoded), the metadata is visible in the URL and sent to the server if the link is opened from the editor.

Pasting from untrusted sources: when the metadata is part of the content, the pasted HTML can hold crafted metadata. The origin of the pasted content can't be trusted (any page can put BlockNote's clipboard format on the clipboard), so the metadata is filtered on every paste:

  • xwikiParameters can hold any attribute: dropped.
  • xwikiReference can make the saved target differ from the displayed one: references to wiki pages and attachments are kept (the access rights apply); the others (url, mailto, path, unc, interwiki, ...) are kept only if they match the displayed URL. The trusted domains check (front-end URL check policy) doesn't cover this: it's limited to comments by default, and doesn't apply to the URLs without host (e.g. javascript:, file:) or to images.
  • The other entries (generated label, freestanding, verbatim, new lines) are kept.

Chosen approach

  • A generic metadata property (JSON object) on all the blocks, for the shared block metadata.
  • The link metadata stored in the link URL.
  • The metadata filtered on paste.
  • The private metadata (macro output) stays in the local metadata store, which no longer needs to be connected to the realtime session.