diff --git a/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/proposal.md b/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/proposal.md new file mode 100644 index 00000000..e5f5a681 --- /dev/null +++ b/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/proposal.md @@ -0,0 +1,7 @@ +## Bug + +`EditorDocument.modifyData()` calls `format()` with the tool but drops `data`. Since #169, re-applying a tool with `data: undefined` replaces the fragment's data, so a redo or a remote Modify op on a link wipes its `href` (and over a partial range splits the link into an href-less head and an href-bearing tail). + +## Fix + +Pass `(data.value as TextFormattedEventData).data` through to `format()`. diff --git a/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/specs/model/spec.md b/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/specs/model/spec.md new file mode 100644 index 00000000..c36696e8 --- /dev/null +++ b/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/specs/model/spec.md @@ -0,0 +1,24 @@ +## MODIFIED Requirements + +### Requirement: EditorDocument block container +The system SHALL provide `EditorDocument` as the root container of `BlockNode`s, supporting add/remove/get by index or id (with an O(1) id lookup map), document-level properties, and generic index-based `insertData`/`removeData`/`modifyData` dispatch, bubbling child events with the block index injected. + +#### Scenario: Adding a block at an out-of-bounds index +- **GIVEN** a document with N blocks +- **WHEN** `addBlock` is called with an index less than 0 or greater than N +- **THEN** it throws an "Index out of bounds" error + +#### Scenario: Adding a block with a duplicate id +- **GIVEN** a block id that already exists in the document +- **WHEN** `addBlock` is called reusing that id +- **THEN** it throws `BlockAlreadyExistsError` + +#### Scenario: Appending a block with no index +- **GIVEN** an existing document +- **WHEN** `addBlock` is called without an index +- **THEN** the block is appended at the end and a `BlockAddedEvent` fires carrying the serialized block data and its index + +#### Scenario: Modifying text with a formatting value +- **GIVEN** a `TextIndex` and a modified value `{ tool, data }` +- **WHEN** `modifyData` is called +- **THEN** the range is formatted with that `tool` and its `data`, so re-applying a data-carrying tool (e.g. redo or a remote op) keeps the fragment's data diff --git a/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/tasks.md b/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/tasks.md new file mode 100644 index 00000000..3c4b1dd9 --- /dev/null +++ b/openspec/changes/archive/2026-11-08-fix-modify-data-inline-data/tasks.md @@ -0,0 +1,5 @@ +## 1. Fix + +- [x] 1.1 Add a failing `EditorDocument.modifyData()` test asserting `format()` receives the modified value's `data` +- [x] 1.2 Pass `data.value.data` through to `format()` in `EditorDocument.modifyData()` +- [x] 1.3 Run model tests and lint; `openspec validate fix-modify-data-inline-data --type change` passes diff --git a/openspec/config.yaml b/openspec/config.yaml index b3cba3b5..b97a8d24 100644 --- a/openspec/config.yaml +++ b/openspec/config.yaml @@ -21,6 +21,7 @@ rules: - Name test cases in should-notation, e.g. it('should return X when Y'). - Check off the final task only after `openspec validate --type change` passes. A fully-checked tasks.md is the signal that the change is ready to archive, and CI (the "OpenSpec archive gate") then requires the archive commit to be present in this PR before it can merge. proposal: + - "Bug fixes get a very short proposal: only a `## Bug` section (what's broken) and a `## Fix` section (the change), a few sentences each. No Why/What Changes/Capabilities/Impact sections and no design.md." - Cross-check against existing docs in docs/ and note if the proposal supersedes any of them. specs: # CI folds deltas by running the `openspec archive` CLI mechanically (no agent), so deltas must fold correctly on their own. diff --git a/openspec/specs/model/spec.md b/openspec/specs/model/spec.md index d9b8bca8..22860e7f 100644 --- a/openspec/specs/model/spec.md +++ b/openspec/specs/model/spec.md @@ -42,7 +42,10 @@ The system SHALL provide `EditorDocument` as the root container of `BlockNode`s, - **WHEN** `addBlock` is called without an index - **THEN** the block is appended at the end and a `BlockAddedEvent` fires carrying the serialized block data and its index -Implemented in `src/entities/EditorDocument/index.ts`, validated by its co-located `.spec.ts`. +#### Scenario: Modifying text with a formatting value +- **GIVEN** a `TextIndex` and a modified value `{ tool, data }` +- **WHEN** `modifyData` is called +- **THEN** the range is formatted with that `tool` and its `data`, so re-applying a data-carrying tool (e.g. redo or a remote op) keeps the fragment's data ### Requirement: BlockNode data tree The system SHALL provide `BlockNode` representing a single block's data tree (nested value/text nodes, arrays, and objects) plus its `BlockTune`s, supporting creation/removal/lookup of data nodes, text insert/remove/format/unformat, tune/value updates, and text content extraction. diff --git a/packages/model/src/EditorJSModel.integration.spec.ts b/packages/model/src/EditorJSModel.integration.spec.ts index 1fc6b8c3..d7f3c475 100644 --- a/packages/model/src/EditorJSModel.integration.spec.ts +++ b/packages/model/src/EditorJSModel.integration.spec.ts @@ -1,4 +1,4 @@ -import { EventType } from '@editorjs/model-types'; +import { EventType, Index, type DataKey, type InlineToolData, type InlineToolName } from '@editorjs/model-types'; import { BlockAddedEvent } from '@editorjs/model-types'; import { EditorJSModel } from './EditorJSModel.js'; import { data } from './mocks/data.js'; @@ -33,4 +33,40 @@ describe('[Integration tests] EditorJSModel', () => { .toHaveBeenCalledWith(expect.any(BlockAddedEvent)); }); }); + + describe('.modifyData()', () => { + const blockIndex = 0; + const dataKey = 'text' as DataKey; + const tool = 'link' as InlineToolName; + const rangeEnd = 6; + const range: [number, number] = [0, rangeEnd]; + const linkData = { href: 'https://editorjs.io' } as unknown as InlineToolData; + let model: EditorJSModel; + + beforeEach(() => { + model = new EditorJSModel('user', { identifier: data.identifier }); + model.initializeDocument(data); + }); + + it('should keep fragment data when re-applying a data-carrying tool', () => { + model.format('user', blockIndex, dataKey, tool, ...range, linkData); + + model.modifyData('user', Index.text([{ blockIndex, + dataKey, + textRange: range }]), { + value: { + tool, + data: linkData, + }, + previous: null, + }); + + expect(model.getFragments(blockIndex, dataKey)) + .toEqual([{ + tool, + range, + data: linkData, + }]); + }); + }); }); diff --git a/packages/model/src/entities/EditorDocument/EditorDocument.spec.ts b/packages/model/src/entities/EditorDocument/EditorDocument.spec.ts index cc535305..a0f21684 100644 --- a/packages/model/src/entities/EditorDocument/EditorDocument.spec.ts +++ b/packages/model/src/entities/EditorDocument/EditorDocument.spec.ts @@ -1480,7 +1480,27 @@ describe('EditorDocument', () => { }); expect(spy) - .toHaveBeenCalledWith(blockIndex, dataKey, 'bold', 0, rangeEnd); + .toHaveBeenCalledWith(blockIndex, dataKey, 'bold', 0, rangeEnd, undefined); + }); + + it('should pass the modified value data to .format() method', () => { + const spy = jest.spyOn(document, 'format'); + const rangeEnd = 5; + const index = Index.text([{ blockIndex, + dataKey, + textRange: [0, rangeEnd] }]); + const data = { href: 'https://example.com' } as unknown as InlineToolData; + + document.modifyData(index, { + value: { + tool: 'link', + data, + }, + previous: null, + }); + + expect(spy) + .toHaveBeenCalledWith(blockIndex, dataKey, 'link', 0, rangeEnd, data); }); it('should call .unformat() method if text index and previous modified value provided', () => { diff --git a/packages/model/src/entities/EditorDocument/index.ts b/packages/model/src/entities/EditorDocument/index.ts index 62140506..0c4cea54 100644 --- a/packages/model/src/entities/EditorDocument/index.ts +++ b/packages/model/src/entities/EditorDocument/index.ts @@ -513,7 +513,9 @@ export class EditorDocument extends EventBus { */ if (index instanceof TextIndex) { if (data.value !== null) { - this.format(index.blockIndex!, index.dataKey!, (data.value as TextFormattedEventData).tool, index.textRange![0], index.textRange![1]); + const { tool, data: toolData } = data.value as TextFormattedEventData; + + this.format(index.blockIndex!, index.dataKey!, tool, index.textRange![0], index.textRange![1], toolData); } else if (data.previous !== null) { this.unformat(index.blockIndex!, index.dataKey!, (data.previous as TextUnformattedEventData).tool, index.textRange![0], index.textRange![1]); }