From 4a68cdaac2345910388847c868ccefc8fd619305 Mon Sep 17 00:00:00 2001 From: streamich Date: Sat, 8 Jun 2024 01:07:55 +0200 Subject: [PATCH] =?UTF-8?q?feat(json-crdt-extensions):=20=F0=9F=8E=B8=20im?= =?UTF-8?q?prove=20how=20blocks=20are=20printed=20to=20console,=20add=20Bl?= =?UTF-8?q?ock.text()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt-extensions/peritext/Peritext.ts | 2 ++ .../peritext/block/Block.ts | 11 +++++++++ .../peritext/block/Inline.ts | 4 ++-- .../peritext/block/LeafBlock.ts | 24 +++++++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/json-crdt-extensions/peritext/Peritext.ts b/src/json-crdt-extensions/peritext/Peritext.ts index 8efe09bc6f..49c26f5c69 100644 --- a/src/json-crdt-extensions/peritext/Peritext.ts +++ b/src/json-crdt-extensions/peritext/Peritext.ts @@ -279,6 +279,8 @@ export class Peritext implements Printable { localSlices.size() ? (tab) => localSlices.toString(tab) : null, nl, (tab) => this.overlay.toString(tab), + nl, + (tab) => this.blocks.toString(tab), ]) ); } diff --git a/src/json-crdt-extensions/peritext/block/Block.ts b/src/json-crdt-extensions/peritext/block/Block.ts index c764f29ad9..46fea383bd 100644 --- a/src/json-crdt-extensions/peritext/block/Block.ts +++ b/src/json-crdt-extensions/peritext/block/Block.ts @@ -107,6 +107,17 @@ export class Block implements IBlock, Printable, Stateful { return new UndefEndIter(this.texts0()); } + public text(): string { + let str = ''; + const iterator = this.texts0(); + let text = iterator(); + while (text) { + str += text.text(); + text = iterator(); + } + return str; + } + // ----------------------------------------------------------------- Stateful public hash: number = 0; diff --git a/src/json-crdt-extensions/peritext/block/Inline.ts b/src/json-crdt-extensions/peritext/block/Inline.ts index 01a8b5a341..77059a8c92 100644 --- a/src/json-crdt-extensions/peritext/block/Inline.ts +++ b/src/json-crdt-extensions/peritext/block/Inline.ts @@ -94,7 +94,7 @@ export class Inline extends Range implements Printable { if (data === undefined) data = 1; attr[type] = data; break; - } + } case SliceBehavior.Erase: { delete attr[type]; break; @@ -120,7 +120,7 @@ export class Inline extends Range implements Printable { return ( header + printTree(tab, [ - !marks + !markKeys.length ? null : (tab) => 'attributes' + diff --git a/src/json-crdt-extensions/peritext/block/LeafBlock.ts b/src/json-crdt-extensions/peritext/block/LeafBlock.ts index 0f59240b7c..57e8e93c0d 100644 --- a/src/json-crdt-extensions/peritext/block/LeafBlock.ts +++ b/src/json-crdt-extensions/peritext/block/LeafBlock.ts @@ -9,13 +9,33 @@ export interface IBlock { } export class LeafBlock extends Block { + // ---------------------------------------------------------------- Printable + protected toStringHeader(): string { - const header = `${super.toStringHeader()}`; + const str = this.text(); + const truncate = str.length > 32; + const text = JSON.stringify(truncate ? str.slice(0, 32) : str) + (truncate ? ' …' : ''); + const header = `${super.toStringHeader()} ${text}`; return header; } public toString(tab: string = ''): string { const header = this.toStringHeader(); - return header + printTree(tab, [this.marker ? (tab) => this.marker!.toString(tab) : null]); + const texts = [...this.texts()]; + const hasSlices = !!texts.length; + return ( + header + + printTree(tab, [ + this.marker ? (tab) => this.marker!.toString(tab) : null, + !hasSlices + ? null + : (tab) => + 'nodes' + + printTree( + tab, + texts.map((inline) => (tab) => inline.toString(tab)), + ), + ]) + ); } }