Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()`.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions openspec/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <change> --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.
Expand Down
5 changes: 4 additions & 1 deletion openspec/specs/model/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 37 additions & 1 deletion packages/model/src/EditorJSModel.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
}]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/model/src/entities/EditorDocument/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
Reversean marked this conversation as resolved.
Resolved
} else if (data.previous !== null) {
this.unformat(index.blockIndex!, index.dataKey!, (data.previous as TextUnformattedEventData).tool, index.textRange![0], index.textRange![1]);
}
Expand Down
Loading