Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 instantiate Peritext contenxt and Edito…
Browse files Browse the repository at this point in the history
…r on PeritextApi
  • Loading branch information
streamich committed May 5, 2024
1 parent 813c69e commit 5a22ffc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/json-crdt-extensions/peritext/PeritextApi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import {NodeApi} from '../../json-crdt/model/api/nodes';
import {Peritext} from './Peritext';
import {printTree} from 'tree-dump/lib/printTree';
import type {Editor} from './editor/Editor';
import type {PeritextNode} from './PeritextNode';
import type {ExtApi, StrApi, ArrApi, ArrNode} from '../../json-crdt';
import type {ExtApi, StrApi, ArrApi, ArrNode, ModelApi} from '../../json-crdt';
import type {SliceNode} from './slice/types';

export class PeritextApi extends NodeApi<PeritextNode> implements ExtApi<PeritextNode> {
public readonly txt: Peritext;
public readonly editor: Editor;

constructor(
public node: PeritextNode,
public readonly api: ModelApi<any>,
) {
super(node, api);
this.txt = new Peritext(api.model, node.text(), node.slices());
this.editor = this.txt.editor;
}

public text(): StrApi {
return this.api.wrap(this.node.text());
}

public slices(): ArrApi<ArrNode<SliceNode>> {
return this.api.wrap(this.node.slices());
}

public toString(tab?: string): string {
return this.constructor.name + printTree(tab, [
(tab) => this.node.toString(tab),
() => '',
(tab) => this.txt.toString(tab),
]);
}
}
24 changes: 21 additions & 3 deletions src/json-crdt-extensions/peritext/__tests__/extension.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {s} from '../../../json-crdt-patch';
import {ArrApi, StrApi, VecApi} from '../../../json-crdt/model';
import {ModelWithExt, ext} from '../../ModelWithExt';
import {Peritext} from '../Peritext';
import {PeritextApi} from '../PeritextApi';
import {PeritextNode} from '../PeritextNode';
import {Editor} from '../editor/Editor';

const schema = s.obj({
nested: s.obj({
Expand Down Expand Up @@ -65,22 +67,22 @@ describe('non-typed access', () => {
describe('typed access', () => {
test('can access PeritextNode in type safe way (using the proxy selector)', () => {
const model = ModelWithExt.create(schema);
let api = model.s.nested.obj.text.ext();
let api = model.s.nested.obj.text.toExt();
expect(api).toBeInstanceOf(PeritextApi);
api = new PeritextApi(api.node, api.api);
});

test('can access raw text "str" node in type safe way', () => {
const model = ModelWithExt.create(schema);
const str = model.s.nested.obj.text.ext().text();
const str = model.s.nested.obj.text.toExt().text();
expect(str).toBeInstanceOf(StrApi);
str.ins(str.length() - 1, '!');
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();
const arr = model.s.nested.obj.text.toExt().slices();
expect(arr).toBeInstanceOf(ArrApi);
expect(arr.view()).toEqual([]);
});
Expand All @@ -96,4 +98,20 @@ describe('typed access', () => {
expect(api2).toBeInstanceOf(PeritextApi);
api2 = new PeritextApi(node, api.api);
});

test('can access Peritext context and Editor', () => {
const model = ModelWithExt.create(schema);
const api = model.s.nested.obj.text.toExt();
expect(api.txt).toBeInstanceOf(Peritext);
expect(api.editor).toBeInstanceOf(Editor);
});

test('can modify Peritext document', () => {
const model = ModelWithExt.create(schema);
const api = model.s.nested.obj.text.toExt();
expect(api.view()).toBe('Hello, world\n');
api.editor.cursor.setAt(12);
api.editor.insert('!');
expect(api.view()).toBe('Hello, world!\n');
});
});

0 comments on commit 5a22ffc

Please sign in to comment.