Skip to content

Commit cb37dbc

Browse files
committed
add comments
1 parent 05a0b0c commit cb37dbc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/client/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ export type RunMutationCtx = {
2222
};
2323

2424
export class ProsemirrorSync<Id extends string = string> {
25+
/**
26+
* Backend API for the ProsemirrorSync component.
27+
* Responsible for exposing the `sync` API to the client, and having
28+
* convenience methods for interacting with the component from the backend.
29+
*
30+
* Typically used like:
31+
*
32+
* ```ts
33+
* const prosemirrorSync = new ProsemirrorSync(components.prosemirrorSync);
34+
* export const {
35+
* ... // see {@link syncApi} docstring for details
36+
* } = prosemirrorSync.syncApi({...});
37+
* ```
38+
*
39+
* @param component - Generally `components.prosemirrorSync` from
40+
* `./_generated/api` once you've configured it in `convex.config.ts`.
41+
*/
2542
constructor(public component: UseApi<Mounts>) {}
2643
/**
2744
* Create a new document with the given ID and content.
@@ -38,6 +55,35 @@ export class ProsemirrorSync<Id extends string = string> {
3855
content: JSON.stringify(content),
3956
});
4057
}
58+
/**
59+
* Expose the sync API to the client for use with the `useTiptapSync` hook.
60+
* If you export these in `convex/prosemirror.ts`, pass `api.prosemirror`
61+
* to the `useTiptapSync` hook.
62+
*
63+
* It allows you to define optional read and write permissions, along with
64+
* a callback when new snapshots are available.
65+
*
66+
* You can pass the optional type argument `<DataModel>` to have the `ctx`
67+
* parameter specific to your tables.
68+
*
69+
* ```ts
70+
* import { DataModel } from "./convex/_generated/dataModel";
71+
* // ...
72+
* export const { ... } = prosemirrorSync.syncApi<DataModel>({...});
73+
* ```
74+
*
75+
* To define just one function to use for both, you can define it like this:
76+
* ```ts
77+
* async function checkPermissions(ctx: QueryCtx, id: string) {
78+
* const user = await getAuthUser(ctx);
79+
* if (!user || !(await canUserAccessDocument(user, id))) {
80+
* throw new Error("Unauthorized");
81+
* }
82+
* }
83+
* ```
84+
* @param opts - Optional callbacks.
85+
* @returns functions to export, so the `useTiptapSync` hook can use them.
86+
*/
4187
syncApi<DataModel extends GenericDataModel>(opts?: {
4288
checkRead?: (
4389
ctx: GenericQueryCtx<DataModel>,

0 commit comments

Comments
 (0)