Skip to content

Commit

Permalink
feat(json-crdt): 馃幐 ability to select extension api directly
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 4, 2024
1 parent ebf1eea commit 8af589c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/json-crdt-extensions/peritext/__tests__/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ test('can access PeritextApi using parent proxy selector', () => {
api2 = new PeritextApi(node, api.api);
});

test('can access PeritextApi using inline proxy selector', () => {
const model = ModelWithExt.create(schema);
let api = model.s.nested.obj.text.ext();
expect(api).toBeInstanceOf(PeritextApi);
api = new PeritextApi(api.node, api.api);
});

// test('can access nested nodes using proxy selector', () => {
// const model = ModelWithExt.create(schema);
// const api = model.s.nested.obj.text.toApi();
Expand Down
1 change: 1 addition & 0 deletions src/json-crdt/model/api/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ export class VecApi<N extends VecNode<any> = VecNode<any>> extends NodeApi<N> {
get: (target, prop, receiver) => {
if (prop === 'toApi') return () => this;
if (prop === 'toView') return () => this.view();
if (prop === 'ext') return () => this.ext();
const index = Number(prop);
if (Number.isNaN(index)) throw new Error('INVALID_INDEX');
const child = this.node.get(index);
Expand Down
3 changes: 3 additions & 0 deletions src/json-crdt/model/api/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {JsonNodeApi} from './types';
import type * as nodes from '../../nodes';
import type {PeritextNode} from '../../../json-crdt-extensions';
import type {VecNodeExtensionData} from '../../schema/types';

export interface ProxyNode<N extends nodes.JsonNode = nodes.JsonNode> {
toApi(): JsonNodeApi<N>;
Expand All @@ -13,6 +14,8 @@ export type ProxyNodeVal<N extends nodes.ValNode<any>> = ProxyNode<N> & {
};
export type ProxyNodeVec<N extends nodes.VecNode<any>> = ProxyNode<N> & {
[K in keyof nodes.JsonNodeView<N>]: JsonNodeToProxyNode<nodes.JsonNodeView<N>[K]>;
} & {
ext: () => JsonNodeApi<VecNodeExtensionData<N>>;
};
export type ProxyNodeObj<N extends nodes.ObjNode<any>> = ProxyNode<N> & {
[K in keyof nodes.JsonNodeView<N>]: JsonNodeToProxyNode<(N extends nodes.ObjNode<infer M> ? M : never)[K]>;
Expand Down

0 comments on commit 8af589c

Please sign in to comment.