cosveti-sync 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +272 -0
- package/dist/client/index.d.ts +183 -0
- package/dist/client/index.js +238 -0
- package/dist/component/_generated/api.d.ts +33 -0
- package/dist/component/_generated/api.js +30 -0
- package/dist/component/_generated/component.d.ts +77 -0
- package/dist/component/_generated/component.js +10 -0
- package/dist/component/_generated/dataModel.d.ts +45 -0
- package/dist/component/_generated/dataModel.js +10 -0
- package/dist/component/_generated/server.d.ts +120 -0
- package/dist/component/_generated/server.js +77 -0
- package/dist/component/convex.config.d.ts +2 -0
- package/dist/component/convex.config.js +2 -0
- package/dist/component/lib.js +272 -0
- package/dist/component/schema.d.ts +28 -0
- package/dist/component/schema.js +17 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/tiptap/doSync.d.ts +5 -0
- package/dist/tiptap/doSync.js +75 -0
- package/dist/tiptap/getCachedState.d.ts +2 -0
- package/dist/tiptap/getCachedState.js +23 -0
- package/dist/tiptap/index.d.ts +17 -0
- package/dist/tiptap/index.js +134 -0
- package/dist/tiptap/reciveSteps.d.ts +3 -0
- package/dist/tiptap/reciveSteps.js +7 -0
- package/dist/tiptap/syncExtension.d.ts +6 -0
- package/dist/tiptap/syncExtension.js +126 -0
- package/dist/tiptap/types.d.ts +19 -0
- package/dist/tiptap/types.js +1 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2024 Convex, Inc.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# cosveti-sync
|
|
2
|
+
|
|
3
|
+
Real-time collaborative editing for TipTap in Svelte with Convex synchronization
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Real-time synchronization for TipTap editors across multiple clients
|
|
8
|
+
- Convex-powered backend for reliable data synchronization
|
|
9
|
+
- Optimistic updates for smooth user experience
|
|
10
|
+
- Client-side caching with localStorage fallback
|
|
11
|
+
- Configurable sync options and error handling
|
|
12
|
+
- Easy integration with SvelteKit projects
|
|
13
|
+
- Support for pausing/resuming synchronization
|
|
14
|
+
- Prosemirror collab protocol implementation
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Install the package using your preferred package manager:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# npm
|
|
22
|
+
npm install cosveti-sync
|
|
23
|
+
|
|
24
|
+
# yarn
|
|
25
|
+
yarn add cosveti-sync
|
|
26
|
+
|
|
27
|
+
# pnpm
|
|
28
|
+
pnpm add cosveti-sync
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This package has the following peer dependencies that you'll also need to install:
|
|
32
|
+
- `@tiptap/pm` (v3.18.0+)
|
|
33
|
+
- `@tiptap/core` (v3.18.0+)
|
|
34
|
+
- `convex` (v1.10.0+)
|
|
35
|
+
- `svelte` (v5.0.0+)
|
|
36
|
+
|
|
37
|
+
## Basic Setup
|
|
38
|
+
|
|
39
|
+
### 1. Convex Backend Configuration
|
|
40
|
+
|
|
41
|
+
First, set up the backend API in your Convex project. Create a file like `convex/tiptapSync.ts`:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { TiptapSyncSvelte } from 'cosveti-sync';
|
|
45
|
+
import { defineComponent } from './_generated/server';
|
|
46
|
+
import { api } from './_generated/api';
|
|
47
|
+
|
|
48
|
+
// Initialize the component
|
|
49
|
+
const tiptapSyncComponent = defineComponent('./component');
|
|
50
|
+
|
|
51
|
+
// Create the sync instance
|
|
52
|
+
const tiptapSyncSvelte = new TiptapSyncSvelte(tiptapSyncComponent);
|
|
53
|
+
|
|
54
|
+
// Export the sync API with optional permission checks
|
|
55
|
+
export const {
|
|
56
|
+
getSnapshot,
|
|
57
|
+
submitSnapshot,
|
|
58
|
+
latestVersion,
|
|
59
|
+
getSteps,
|
|
60
|
+
submitSteps
|
|
61
|
+
} = tiptapSyncSvelte.syncApi({
|
|
62
|
+
// Optional: Add read/write permission checks
|
|
63
|
+
checkRead: async (ctx, id) => {
|
|
64
|
+
// Add your authorization logic here
|
|
65
|
+
},
|
|
66
|
+
checkWrite: async (ctx, id) => {
|
|
67
|
+
// Add your authorization logic here
|
|
68
|
+
},
|
|
69
|
+
// Optional: Callback when a new snapshot is available
|
|
70
|
+
onSnapshot: async (ctx, id, snapshot, version) => {
|
|
71
|
+
// Do something when a new snapshot is saved
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Export the API for the client to use
|
|
76
|
+
export default {
|
|
77
|
+
tiptapSyncSvelte
|
|
78
|
+
};
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 2. Client-Side Usage
|
|
82
|
+
|
|
83
|
+
In your Svelte component, use the `useTiptapSync` hook:
|
|
84
|
+
|
|
85
|
+
```svelte
|
|
86
|
+
<script lang="ts">
|
|
87
|
+
import { useTiptapSync } from 'cosveti-sync';
|
|
88
|
+
import { api } from '$convex/_generated/api';
|
|
89
|
+
import { useConvexClient } from 'convex-svelte';
|
|
90
|
+
import { Editor } from '@tiptap/core';
|
|
91
|
+
import { StarterKit } from '@tiptap/starter-kit';
|
|
92
|
+
import { onMount } from 'svelte';
|
|
93
|
+
|
|
94
|
+
// Get the Convex client (assuming convex-svelte is set up)
|
|
95
|
+
const convex = useConvexClient();
|
|
96
|
+
|
|
97
|
+
// Initialize the sync functionality
|
|
98
|
+
const { isSyncEnabled, extension, isLoading, initialContent, create } = $derived(
|
|
99
|
+
useTiptapSync(
|
|
100
|
+
convex,
|
|
101
|
+
api.tiptapSyncSvelte,
|
|
102
|
+
'your-document-id', // Unique document ID
|
|
103
|
+
{
|
|
104
|
+
onSyncError: (error) => {
|
|
105
|
+
console.error('Sync error:', error);
|
|
106
|
+
},
|
|
107
|
+
snapshotDebounceMs: 1000, // Debounce snapshot submissions (default: 1000ms)
|
|
108
|
+
debug: false // Enable debug logging (default: false)
|
|
109
|
+
}
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
let editorContainer: HTMLElement;
|
|
114
|
+
let editor: Editor;
|
|
115
|
+
|
|
116
|
+
onMount(() => {
|
|
117
|
+
if (!$extension) return;
|
|
118
|
+
|
|
119
|
+
editor = new Editor({
|
|
120
|
+
element: editorContainer,
|
|
121
|
+
extensions: [
|
|
122
|
+
StarterKit,
|
|
123
|
+
$extension // Add the sync extension from useTiptapSync
|
|
124
|
+
],
|
|
125
|
+
content: $initialContent
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
function handleCreateDocument() {
|
|
130
|
+
create({
|
|
131
|
+
type: 'doc',
|
|
132
|
+
content: [
|
|
133
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'Hello world!' }] }
|
|
134
|
+
]
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
</script>
|
|
138
|
+
|
|
139
|
+
{#if $isLoading}
|
|
140
|
+
<div>Loading editor...</div>
|
|
141
|
+
{:else if !$initialContent}
|
|
142
|
+
<button on:click={handleCreateDocument}>
|
|
143
|
+
Create New Document
|
|
144
|
+
</button>
|
|
145
|
+
{:else if $extension}
|
|
146
|
+
<div>
|
|
147
|
+
<button
|
|
148
|
+
on:click={() => $isSyncEnabled.update(n => !n)}
|
|
149
|
+
>
|
|
150
|
+
{$isSyncEnabled ? 'Pause Sync' : 'Resume Sync'}
|
|
151
|
+
</button>
|
|
152
|
+
<div bind:this={editorContainer}></div>
|
|
153
|
+
</div>
|
|
154
|
+
{/if}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## API Documentation
|
|
158
|
+
|
|
159
|
+
### `useTiptapSync()`
|
|
160
|
+
|
|
161
|
+
The main hook for enabling real-time synchronization.
|
|
162
|
+
|
|
163
|
+
#### Parameters
|
|
164
|
+
|
|
165
|
+
- `convex`: Convex client instance
|
|
166
|
+
- `syncApi`: The sync API object from your Convex backend
|
|
167
|
+
- `id`: Unique identifier for the document
|
|
168
|
+
- `opts` (optional): Options object with the following properties:
|
|
169
|
+
- `onSyncError`: Callback function for handling synchronization errors
|
|
170
|
+
- `snapshotDebounceMs`: Debounce time for snapshot submissions (default: 1000ms)
|
|
171
|
+
- `debug`: Enable debug logging (default: false)
|
|
172
|
+
|
|
173
|
+
#### Returns
|
|
174
|
+
|
|
175
|
+
- `isSyncEnabled`: Svelte store for controlling sync enable/disable state
|
|
176
|
+
- `isLoading`: Svelte store indicating if the initial content is loading
|
|
177
|
+
- `initialContent`: Svelte store with the initial document content
|
|
178
|
+
- `extension`: TipTap extension to add to your editor
|
|
179
|
+
- `create`: Function to create a new document with initial content
|
|
180
|
+
|
|
181
|
+
## Configuration Options
|
|
182
|
+
|
|
183
|
+
### Sync Options
|
|
184
|
+
|
|
185
|
+
| Option | Type | Default | Description |
|
|
186
|
+
|--------|------|---------|-------------|
|
|
187
|
+
| `onSyncError` | `(error: Error) => void` | `undefined` | Callback for handling synchronization errors |
|
|
188
|
+
| `snapshotDebounceMs` | `number` | `1000` | Debounce time in milliseconds for submitting snapshots |
|
|
189
|
+
| `debug` | `boolean` | `false` | Enable debug logging to console |
|
|
190
|
+
|
|
191
|
+
### Backend API Options
|
|
192
|
+
|
|
193
|
+
When creating the sync API in Convex, you can configure:
|
|
194
|
+
|
|
195
|
+
- `checkRead`: Function to check read permissions
|
|
196
|
+
- `checkWrite`: Function to check write permissions
|
|
197
|
+
- `onSnapshot`: Callback when new snapshots are available
|
|
198
|
+
- `pruneSnapshots`: Whether to prune old snapshots (default: true)
|
|
199
|
+
|
|
200
|
+
## Example Usage in Svelte
|
|
201
|
+
|
|
202
|
+
Here's a complete example showing how to integrate the library:
|
|
203
|
+
|
|
204
|
+
```svelte
|
|
205
|
+
<script lang="ts">
|
|
206
|
+
import { useTiptapSync } from 'cosveti-sync';
|
|
207
|
+
import { api } from '$convex/_generated/api';
|
|
208
|
+
import { useConvexClient } from 'convex-svelte';
|
|
209
|
+
import { Editor } from '@tiptap/core';
|
|
210
|
+
import { StarterKit } from '@tiptap/starter-kit';
|
|
211
|
+
import { onMount, onDestroy } from 'svelte';
|
|
212
|
+
|
|
213
|
+
const convex = useConvexClient();
|
|
214
|
+
const documentId = 'my-shared-document';
|
|
215
|
+
|
|
216
|
+
const {
|
|
217
|
+
isSyncEnabled,
|
|
218
|
+
extension,
|
|
219
|
+
isLoading,
|
|
220
|
+
initialContent,
|
|
221
|
+
create
|
|
222
|
+
} = $derived(
|
|
223
|
+
useTiptapSync(convex, api.tiptapSyncSvelte, documentId)
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
let editorContainer: HTMLElement;
|
|
227
|
+
let editor: Editor;
|
|
228
|
+
|
|
229
|
+
$: if ($extension && $initialContent && !editor) {
|
|
230
|
+
editor = new Editor({
|
|
231
|
+
element: editorContainer,
|
|
232
|
+
extensions: [
|
|
233
|
+
StarterKit,
|
|
234
|
+
$extension
|
|
235
|
+
],
|
|
236
|
+
content: $initialContent
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
onDestroy(() => {
|
|
241
|
+
editor?.destroy();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
function handleCreate() {
|
|
245
|
+
create({
|
|
246
|
+
type: 'doc',
|
|
247
|
+
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'New document' }] }]
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
</script>
|
|
251
|
+
|
|
252
|
+
<div class="editor-container">
|
|
253
|
+
{#if $isLoading}
|
|
254
|
+
<p>Loading...</p>
|
|
255
|
+
{:else if !$initialContent}
|
|
256
|
+
<button on:click={handleCreate}>Create Document</button>
|
|
257
|
+
{:else if $extension}
|
|
258
|
+
<div class="controls">
|
|
259
|
+
<button
|
|
260
|
+
on:click={() => $isSyncEnabled.update(enabled => !enabled)}
|
|
261
|
+
>
|
|
262
|
+
{$isSyncEnabled ? 'Pause Sync' : 'Resume Sync'}
|
|
263
|
+
</button>
|
|
264
|
+
</div>
|
|
265
|
+
<div bind:this={editorContainer} class="editor"></div>
|
|
266
|
+
{/if}
|
|
267
|
+
</div>
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
Apache-2.0
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { type ApiFromModules, type GenericDataModel, type GenericMutationCtx, type GenericQueryCtx } from 'convex/server';
|
|
2
|
+
import type { ComponentApi } from '../component/_generated/component.js';
|
|
3
|
+
import { Schema, Node } from '@tiptap/pm/model';
|
|
4
|
+
import { Transform } from '@tiptap/pm/transform';
|
|
5
|
+
export type SyncApi = ApiFromModules<{
|
|
6
|
+
sync: ReturnType<TiptapSyncSvelte['syncApi']>;
|
|
7
|
+
}>['sync'];
|
|
8
|
+
export type RunMutationCtx = {
|
|
9
|
+
runMutation: GenericMutationCtx<GenericDataModel>['runMutation'];
|
|
10
|
+
runQuery: GenericQueryCtx<GenericDataModel>['runQuery'];
|
|
11
|
+
};
|
|
12
|
+
export declare class TiptapSyncSvelte<Id extends string = string> {
|
|
13
|
+
component: ComponentApi;
|
|
14
|
+
/**
|
|
15
|
+
* Backend API for the ProsemirrorSync component.
|
|
16
|
+
* Responsible for exposing the `sync` API to the client, and having
|
|
17
|
+
* convenience methods for interacting with the component from the backend.
|
|
18
|
+
*
|
|
19
|
+
* Typically used like:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const prosemirrorSync = new ProsemirrorSync(components.prosemirrorSync);
|
|
23
|
+
* export const {
|
|
24
|
+
* ... // see {@link syncApi} docstring for details
|
|
25
|
+
* } = prosemirrorSync.syncApi({...});
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param component - Generally `components.prosemirrorSync` from
|
|
29
|
+
* `./_generated/api` once you've configured it in `convex.config.ts`.
|
|
30
|
+
*/
|
|
31
|
+
constructor(component: ComponentApi);
|
|
32
|
+
/**
|
|
33
|
+
* Create a new document with the given ID and content.
|
|
34
|
+
*
|
|
35
|
+
* @param ctx - A Convex mutation context.
|
|
36
|
+
* @param id - The document ID.
|
|
37
|
+
* @param content - The document content. Should be ProseMirror JSON.
|
|
38
|
+
* @returns A promise that resolves when the document is created.
|
|
39
|
+
*/
|
|
40
|
+
create(ctx: RunMutationCtx, id: Id, content: object): Promise<null>;
|
|
41
|
+
/**
|
|
42
|
+
* Get the latest document version and content.
|
|
43
|
+
*
|
|
44
|
+
* @param ctx - A Convex mutation context.
|
|
45
|
+
* @param id - The document ID.
|
|
46
|
+
* @param schema - Your ProseMirror schema.
|
|
47
|
+
* For Tiptap, use `getSchema(extensions)`.
|
|
48
|
+
* For BlockNote, use `editor.pmSchema`.
|
|
49
|
+
* @returns The latest ProseMirror doc (Node) and version.
|
|
50
|
+
*/
|
|
51
|
+
getDoc(ctx: RunMutationCtx, id: Id, schema: Schema): Promise<{
|
|
52
|
+
version: number;
|
|
53
|
+
doc: Node;
|
|
54
|
+
}>;
|
|
55
|
+
/**
|
|
56
|
+
* Transform the document by applying the given function to the document.
|
|
57
|
+
*
|
|
58
|
+
* This will keep applying the function until the document is synced,
|
|
59
|
+
* so ensure that the function is idempotent (can be applied multiple times).
|
|
60
|
+
*
|
|
61
|
+
* e.g.
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { getSchema } from "@tiptap/core";
|
|
64
|
+
* import { Transform } from "@tiptap/pm/transform";
|
|
65
|
+
*
|
|
66
|
+
* const schema = getSchema(extensions);
|
|
67
|
+
* await prosemirrorSync.transform(ctx, id, schema, (doc) => {
|
|
68
|
+
* const tr = new Transform(doc);
|
|
69
|
+
* tr.insert(0, schema.text("Hello world"));
|
|
70
|
+
* return tr;
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @param ctx - A Convex mutation context.
|
|
75
|
+
* @param id - The document ID.
|
|
76
|
+
* @param schema - The document schema.
|
|
77
|
+
* @param fn - A function that takes the document and returns a Transform
|
|
78
|
+
* or null if no changes are needed.
|
|
79
|
+
* @returns A promise that resolves with the transformed document.
|
|
80
|
+
*/
|
|
81
|
+
transform(ctx: RunMutationCtx, id: Id, schema: Schema, fn: (node: Node, version: number) => Transform | null | Promise<Transform | null>, opts?: {
|
|
82
|
+
clientId?: string;
|
|
83
|
+
}): Promise<Node>;
|
|
84
|
+
/**
|
|
85
|
+
* Expose the sync API to the client for use with the `useTiptapSync` hook.
|
|
86
|
+
* If you export these in `convex/prosemirror.ts`, pass `api.prosemirror`
|
|
87
|
+
* to the `useTiptapSync` hook.
|
|
88
|
+
*
|
|
89
|
+
* It allows you to define optional read and write permissions, along with
|
|
90
|
+
* a callback when new snapshots are available.
|
|
91
|
+
*
|
|
92
|
+
* You can pass the optional type argument `<DataModel>` to have the `ctx`
|
|
93
|
+
* parameter specific to your tables.
|
|
94
|
+
*
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { DataModel } from "./convex/_generated/dataModel";
|
|
97
|
+
* // ...
|
|
98
|
+
* export const { ... } = prosemirrorSync.syncApi<DataModel>({...});
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* To define just one function to use for both, you can define it like this:
|
|
102
|
+
* ```ts
|
|
103
|
+
* async function checkPermissions(ctx: QueryCtx, id: string) {
|
|
104
|
+
* const user = await getAuthUser(ctx);
|
|
105
|
+
* if (!user || !(await canUserAccessDocument(user, id))) {
|
|
106
|
+
* throw new Error("Unauthorized");
|
|
107
|
+
* }
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
* @param opts - Optional callbacks.
|
|
111
|
+
* @returns functions to export, so the `useTiptapSync` hook can use them.
|
|
112
|
+
*/
|
|
113
|
+
syncApi<DataModel extends GenericDataModel>(opts?: {
|
|
114
|
+
/**
|
|
115
|
+
* Optional callback to check read permissions.
|
|
116
|
+
* Throw an error if the user is not authorized to read the document.
|
|
117
|
+
* @param ctx - A Convex query context.
|
|
118
|
+
* @param id - The document ID.
|
|
119
|
+
*/
|
|
120
|
+
checkRead?: (ctx: GenericQueryCtx<DataModel>, id: Id) => void | Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* Optional callback to check write permissions.
|
|
123
|
+
* Throw an error if the user is not authorized to write to the document.
|
|
124
|
+
* @param ctx - A Convex mutation context.
|
|
125
|
+
* @param id - The document ID.
|
|
126
|
+
*/
|
|
127
|
+
checkWrite?: (ctx: GenericMutationCtx<DataModel>, id: Id) => void | Promise<void>;
|
|
128
|
+
/**
|
|
129
|
+
* Optional callback to run when a new snapshot is available.
|
|
130
|
+
* Version 1 is the initial content.
|
|
131
|
+
* @param ctx - A Convex mutation context.
|
|
132
|
+
* @param id - The document ID.
|
|
133
|
+
* @param snapshot - The snapshot content, as stringified ProseMirror JSON.
|
|
134
|
+
* @param version - The version this snapshot represents.
|
|
135
|
+
*/
|
|
136
|
+
onSnapshot?: (ctx: GenericMutationCtx<DataModel>, id: Id, snapshot: string, version: number) => void | Promise<void>;
|
|
137
|
+
/**
|
|
138
|
+
* Whether to prune old snapshots.
|
|
139
|
+
* If set to true, only the original and newest snapshots are kept.
|
|
140
|
+
* @default true
|
|
141
|
+
*/
|
|
142
|
+
pruneSnapshots?: boolean;
|
|
143
|
+
}): {
|
|
144
|
+
getSnapshot: import("convex/server").RegisteredQuery<"public", {
|
|
145
|
+
version?: number | undefined;
|
|
146
|
+
id: Id;
|
|
147
|
+
}, Promise<{
|
|
148
|
+
content: null;
|
|
149
|
+
version: null;
|
|
150
|
+
} | {
|
|
151
|
+
content: string;
|
|
152
|
+
version: number;
|
|
153
|
+
}>>;
|
|
154
|
+
submitSnapshot: import("convex/server").RegisteredMutation<"public", {
|
|
155
|
+
id: Id;
|
|
156
|
+
version: number;
|
|
157
|
+
content: string;
|
|
158
|
+
}, Promise<void>>;
|
|
159
|
+
latestVersion: import("convex/server").RegisteredQuery<"public", {
|
|
160
|
+
id: Id;
|
|
161
|
+
}, Promise<number | null>>;
|
|
162
|
+
getSteps: import("convex/server").RegisteredQuery<"public", {
|
|
163
|
+
id: Id;
|
|
164
|
+
version: number;
|
|
165
|
+
}, Promise<{
|
|
166
|
+
clientIds: Array<string | number>;
|
|
167
|
+
steps: Array<string>;
|
|
168
|
+
version: number;
|
|
169
|
+
}>>;
|
|
170
|
+
submitSteps: import("convex/server").RegisteredMutation<"public", {
|
|
171
|
+
id: Id;
|
|
172
|
+
version: number;
|
|
173
|
+
clientId: string | number;
|
|
174
|
+
steps: string[];
|
|
175
|
+
}, Promise<{
|
|
176
|
+
clientIds: Array<string | number>;
|
|
177
|
+
status: "needs-rebase";
|
|
178
|
+
steps: Array<string>;
|
|
179
|
+
} | {
|
|
180
|
+
status: "synced";
|
|
181
|
+
}>>;
|
|
182
|
+
};
|
|
183
|
+
}
|