Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 improve how blocks are printed to conso…
Browse files Browse the repository at this point in the history
…le, add Block.text()
  • Loading branch information
streamich committed Jun 7, 2024
1 parent bd1d596 commit 4a68cda
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/json-crdt-extensions/peritext/Peritext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ export class Peritext<T = string> implements Printable {
localSlices.size() ? (tab) => localSlices.toString(tab) : null,
nl,
(tab) => this.overlay.toString(tab),
nl,
(tab) => this.blocks.toString(tab),
])
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/json-crdt-extensions/peritext/block/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ export class Block<Attr = unknown> 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;
Expand Down
4 changes: 2 additions & 2 deletions src/json-crdt-extensions/peritext/block/Inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -120,7 +120,7 @@ export class Inline extends Range implements Printable {
return (
header +
printTree(tab, [
!marks
!markKeys.length
? null
: (tab) =>
'attributes' +
Expand Down
24 changes: 22 additions & 2 deletions src/json-crdt-extensions/peritext/block/LeafBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,33 @@ export interface IBlock<Attr = unknown> {
}

export class LeafBlock<Attr = unknown> extends Block<Attr> {
// ---------------------------------------------------------------- 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)),
),
])
);
}
}

0 comments on commit 4a68cda

Please sign in to comment.