Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 add ability to access Peritext slices "…
Browse files Browse the repository at this point in the history
…arr" node
  • Loading branch information
streamich committed May 4, 2024
1 parent 019ab89 commit 2ea4f34
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/json-crdt-extensions/peritext/PeritextApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {NodeApi} from '../../json-crdt/model/api/nodes';
import type {PeritextNode} from './PeritextNode';
import type {ExtensionApi, StrApi} from '../../json-crdt';
import type {ExtensionApi, StrApi, ArrApi, ArrNode} from '../../json-crdt';
import type {SliceNode} from './slice/types';

export class PeritextApi extends NodeApi<PeritextNode> implements ExtensionApi<PeritextNode> {
public text(): StrApi {
return this.api.wrap(this.node.text());
}

public slices(): ArrApi<ArrNode<SliceNode>> {
return this.api.wrap(this.node.slices());
}
}
16 changes: 11 additions & 5 deletions src/json-crdt-extensions/peritext/PeritextNode.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import {MNEMONIC} from './constants';
import {ExtensionNode} from '../../json-crdt/extensions/ExtensionNode';
import {ExtensionId} from '../constants';
import type {StrNode} from '../../json-crdt/nodes';
import type {ArrNode, StrNode} from '../../json-crdt/nodes';
import type {PeritextDataNode} from './types';
import type {SliceNode} from './slice/types';

export class PeritextNode extends ExtensionNode<PeritextDataNode> {
public text(): StrNode<string> {
return this.data.get(0)!;
}

public slices(): ArrNode<SliceNode> {
return this.data.get(1)!;
}

// ------------------------------------------------------------ ExtensionNode
public readonly extId = ExtensionId.peritext;

public name(): string {
return MNEMONIC;
}

public text(): StrNode {
return this.data.get(0)!;
}

public view(): string {
return this.text().view();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {s} from '../../../json-crdt-patch';
import {StrApi, VecApi} from '../../../json-crdt/model';
import {ArrApi, StrApi, VecApi} from '../../../json-crdt/model';
import {ModelWithExt, ext} from '../../ModelWithExt';
import {PeritextApi} from '../PeritextApi';
import {PeritextNode} from '../PeritextNode';
Expand Down Expand Up @@ -27,6 +27,13 @@ test('can access raw text "str" node in type safe way', () => {
expect(model.view().nested.obj.text).toBe('Hello, world!\n');
});

test('can access slices "arr" node in type safe way', () => {
const model = ModelWithExt.create(schema);
const arr = model.s.nested.obj.text.ext().slices();
expect(arr).toBeInstanceOf(ArrApi);
expect(arr.view()).toEqual([]);
});

test('can access PeritextApi using path selector', () => {
const model = ModelWithExt.create(schema);
const api = model.api.vec(['nested', 'obj', 'text']);
Expand Down
2 changes: 2 additions & 0 deletions src/json-crdt/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
/**
* Experimental node retrieval API using proxy objects. Returns a strictly
* typed proxy wrapper around the value of the root node.
*
* @todo consider renaming this to `_`.
*/
public get s() {
return this.api.r.proxy().val;
Expand Down

0 comments on commit 2ea4f34

Please sign in to comment.