Skip to content

Commit e7235fd

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 use correct nested block data
1 parent 6b4b675 commit e7235fd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/json-crdt-extensions/peritext/block/Block.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {UndEndIterator, type UndEndNext} from '../../../util/iterator';
55
import {Inline} from './Inline';
66
import {formatType, getTag} from '../slice/util';
77
import {Range} from '../rga/Range';
8+
import {stringify} from '../../../json-text/stringify';
89
import type {Point} from '../rga/Point';
910
import type {OverlayPoint} from '../overlay/OverlayPoint';
1011
import type {Printable} from 'tree-dump';
@@ -49,7 +50,12 @@ export class Block<T = string, Attr = unknown> extends Range<T> implements IBloc
4950
}
5051

5152
public attr(): Attr | undefined {
52-
return this.marker?.data() as Attr | undefined;
53+
const path = this.path;
54+
const length = path.length;
55+
if (length === 0) return;
56+
const tagStep = path[length - 1];
57+
if (!Array.isArray(tagStep)) return;
58+
return tagStep[2] as Attr | undefined;
5359
}
5460

5561
public isLeaf(): boolean {
@@ -197,7 +203,8 @@ export class Block<T = string, Attr = unknown> extends Range<T> implements IBloc
197203
protected toStringHeader(): string {
198204
const hash = `#${this.hash.toString(36).slice(-4)}`;
199205
const tag = this.path.map((step) => formatType(step)).join('.');
200-
const header = `${super.toString('', true)} ${hash} ${tag} `;
206+
const data = stringify(this.attr());
207+
const header = `${super.toString('', true)} ${hash} ${tag} ${data}`;
201208
return header;
202209
}
203210

src/json-crdt-extensions/peritext/transfer/__tests__/export-html.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ const testSuite = (setupKit: () => Kit) => {
5757
peritext.refresh();
5858
expect(toHtml()).toBe('<p>abcd</p><blockquote><p>efgh</p></blockquote><blockquote><p>ijklmnopqrstuvwxyz</p></blockquote>');
5959
});
60+
61+
test('outputs nested block data', () => {
62+
const {peritext, editor, toHtml} = setup();
63+
editor.cursor.setAt(8);
64+
editor.saved.insMarker([[SliceTypeCon.blockquote, 0, {author: 'Mark Twain'}], [SliceTypeCon.p, 0, {indent: 1}]]);
65+
peritext.refresh();
66+
expect(toHtml()).toBe('<p>abcdefgh</p><blockquote data-attr=\'{"author":"Mark Twain"}\'><p data-attr=\'{"indent":1}\'>ijklmnopqrstuvwxyz</p></blockquote>');
67+
});
6068
};
6169

6270
describe('HTML export', () => {

0 commit comments

Comments
 (0)