@dxos/react-ui-editor 0.3.11-main.00a28cb → 0.3.11-main.0999ea0
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/dist/lib/browser/index.mjs +1304 -1092
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +12 -17
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +13 -5
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +9 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +14 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +8 -10
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -6
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/awareness.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +23 -15
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +3 -3
- package/dist/types/src/extensions/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -7
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts +11 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
- package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts +8 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/tasklist.d.ts +3 -0
- package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -1
- package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +25 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +3 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +2 -27
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts +2 -2
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +45 -36
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +64 -75
- package/src/components/TextEditor/TextEditor.tsx +103 -71
- package/src/components/TextEditor/comments.stories.tsx +46 -44
- package/src/components/Toolbar/Toolbar.stories.tsx +86 -0
- package/src/components/Toolbar/Toolbar.tsx +115 -0
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +50 -39
- package/src/extensions/automerge/cursor.ts +3 -3
- package/src/extensions/automerge/defs.ts +11 -36
- package/src/extensions/automerge/semaphore.ts +31 -28
- package/src/extensions/automerge/update-automerge.ts +14 -8
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +17 -17
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/comments.ts +247 -179
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/index.ts +0 -7
- package/src/extensions/markdown/bundle.ts +20 -30
- package/src/extensions/markdown/code.ts +142 -0
- package/src/extensions/markdown/formatting.ts +159 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +9 -15
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +5 -2
- package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -34
- package/src/extensions/mention.ts +9 -2
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +3 -2
- package/src/hooks/useActionHandler.ts +45 -0
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +49 -55
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +1 -3
- package/src/util.ts +26 -3
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/code.d.ts.map +0 -1
- package/dist/types/src/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/extensions/image.d.ts.map +0 -1
- package/dist/types/src/extensions/link.d.ts.map +0 -1
- package/dist/types/src/extensions/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts +0 -7
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -104
- package/src/extensions/hr.ts +0 -72
- package/src/extensions/yjs/cursor.ts +0 -22
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -156
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
- /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
- /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
- /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
- /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
- /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
package/src/hooks/yjs.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as decoding from 'lib0/decoding';
|
|
6
|
-
import * as encoding from 'lib0/encoding';
|
|
7
|
-
import { Observable } from 'lib0/observable';
|
|
8
|
-
import * as YP from 'y-protocols/awareness';
|
|
9
|
-
import { type Doc } from 'yjs';
|
|
10
|
-
|
|
11
|
-
import { invariant } from '@dxos/invariant';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
import { type Space } from '@dxos/react-client/echo';
|
|
14
|
-
import { type GossipMessage } from '@dxos/react-client/mesh';
|
|
15
|
-
import type { YText } from '@dxos/text-model';
|
|
16
|
-
|
|
17
|
-
import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
|
|
18
|
-
import { yjs } from '../extensions';
|
|
19
|
-
|
|
20
|
-
export const createYjsModel = ({ identity, space, text }: UseTextModelProps): EditorModel => {
|
|
21
|
-
invariant(text?.doc && text?.content);
|
|
22
|
-
const provider = space
|
|
23
|
-
? new YAwarenessProvider({ space, doc: text.doc, channel: `yjs.awareness.${text.id}` })
|
|
24
|
-
: undefined;
|
|
25
|
-
|
|
26
|
-
const model: EditorModel = {
|
|
27
|
-
id: text.doc.guid,
|
|
28
|
-
content: text.content,
|
|
29
|
-
text: () => text.content!.toString(),
|
|
30
|
-
extension: [modelState.init(() => model), yjs(text.content as YText, provider?.awareness)],
|
|
31
|
-
awareness: provider?.awareness,
|
|
32
|
-
peer: identity
|
|
33
|
-
? {
|
|
34
|
-
id: identity.identityKey.toHex(),
|
|
35
|
-
name: identity.profile?.displayName,
|
|
36
|
-
}
|
|
37
|
-
: undefined,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return model;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Based on https://github.com/yjs/y-webrtc/blob/88baab2/src/y-webrtc.js.
|
|
44
|
-
|
|
45
|
-
const messageAwareness = 1;
|
|
46
|
-
const messageQueryAwareness = 3;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Yjs awareness provider on top of a DXOS space.
|
|
50
|
-
*/
|
|
51
|
-
export class YAwarenessProvider extends Observable<any> {
|
|
52
|
-
private readonly _space: Space;
|
|
53
|
-
private readonly _awareness: YP.Awareness;
|
|
54
|
-
private readonly _clientId: number;
|
|
55
|
-
private readonly _channel: string;
|
|
56
|
-
|
|
57
|
-
constructor({
|
|
58
|
-
space,
|
|
59
|
-
doc,
|
|
60
|
-
channel,
|
|
61
|
-
awareness,
|
|
62
|
-
}: {
|
|
63
|
-
space: Space;
|
|
64
|
-
doc: Doc;
|
|
65
|
-
channel: string;
|
|
66
|
-
awareness?: YP.Awareness;
|
|
67
|
-
}) {
|
|
68
|
-
super();
|
|
69
|
-
this._space = space;
|
|
70
|
-
this._awareness = awareness ?? new YP.Awareness(doc);
|
|
71
|
-
this._channel = channel;
|
|
72
|
-
this._clientId = doc.clientID;
|
|
73
|
-
|
|
74
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
75
|
-
this._space.listen(this._channel, this._handleSpaceMessage.bind(this));
|
|
76
|
-
|
|
77
|
-
if (typeof window !== 'undefined') {
|
|
78
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
79
|
-
} else if (typeof process !== 'undefined') {
|
|
80
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Post queryAwareness.
|
|
84
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
85
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
86
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessQuery));
|
|
87
|
-
|
|
88
|
-
// Post local awareness state.
|
|
89
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
90
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
91
|
-
encoding.writeVarUint8Array(encoderAwarenessState, YP.encodeAwarenessUpdate(this.awareness, [this._clientId]));
|
|
92
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessState));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
get awareness(): YP.Awareness {
|
|
96
|
-
return this._awareness;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
100
|
-
log('awareness update', { added, updated, removed, origin });
|
|
101
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
102
|
-
const encoderAwareness = encoding.createEncoder();
|
|
103
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
104
|
-
encoding.writeVarUint8Array(encoderAwareness, YP.encodeAwarenessUpdate(this._awareness, changedClients));
|
|
105
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwareness));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private _handleSpaceMessage({ payload }: GossipMessage) {
|
|
109
|
-
log('space message', payload);
|
|
110
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
111
|
-
const encoder = this._readMessage(data);
|
|
112
|
-
if (encoder) {
|
|
113
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoder));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private _readMessage(message: Uint8Array) {
|
|
118
|
-
const decoder = decoding.createDecoder(message);
|
|
119
|
-
const encoder = encoding.createEncoder();
|
|
120
|
-
const messageType = decoding.readVarUint(decoder);
|
|
121
|
-
|
|
122
|
-
let sendReply = false;
|
|
123
|
-
switch (messageType) {
|
|
124
|
-
case messageAwareness: {
|
|
125
|
-
YP.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
case messageQueryAwareness: {
|
|
130
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
131
|
-
encoding.writeVarUint8Array(
|
|
132
|
-
encoder,
|
|
133
|
-
YP.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
134
|
-
);
|
|
135
|
-
sendReply = true;
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
default: {
|
|
140
|
-
console.error('Invalid message:', messageType);
|
|
141
|
-
return encoder;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!sendReply) {
|
|
146
|
-
// Nothing has been written, no answer created.
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return encoder;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private _handleBeforeUnload() {
|
|
154
|
-
YP.removeAwarenessStates(this._awareness, [this._clientId], 'window unload');
|
|
155
|
-
}
|
|
156
|
-
}
|
package/src/testing/index.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @generated @dxos/echo-typegen src/testing/proto/schema.proto
|
|
3
|
-
**/
|
|
4
|
-
|
|
5
|
-
import * as dxos_echo_schema from "@dxos/echo-schema";
|
|
6
|
-
|
|
7
|
-
export const types = new dxos_echo_schema.TypeCollection();
|
|
8
|
-
|
|
9
|
-
export type EditorDocumentProps = {
|
|
10
|
-
content: dxos_echo_schema.Text;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export class EditorDocument extends dxos_echo_schema.TypedObject<EditorDocumentProps> {
|
|
14
|
-
declare static readonly schema: dxos_echo_schema.Schema;
|
|
15
|
-
|
|
16
|
-
static filter(
|
|
17
|
-
filter?:
|
|
18
|
-
| Partial<EditorDocumentProps>
|
|
19
|
-
| dxos_echo_schema.OperatorFilter<EditorDocument>
|
|
20
|
-
): dxos_echo_schema.Filter<EditorDocument> {
|
|
21
|
-
return dxos_echo_schema.Filter.typename(
|
|
22
|
-
"dxos.testing.editor.EditorDocument",
|
|
23
|
-
filter
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
initValues?: Partial<EditorDocumentProps>,
|
|
29
|
-
opts?: dxos_echo_schema.TypedObjectOptions
|
|
30
|
-
) {
|
|
31
|
-
super({ ...initValues }, { schema: EditorDocument.schema, ...opts });
|
|
32
|
-
}
|
|
33
|
-
declare content: dxos_echo_schema.Text;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
types.registerPrototype(EditorDocument, {
|
|
37
|
-
typename: "dxos.testing.editor.EditorDocument",
|
|
38
|
-
props: [
|
|
39
|
-
{
|
|
40
|
-
id: "content",
|
|
41
|
-
type: 5,
|
|
42
|
-
repeated: false,
|
|
43
|
-
refName: "dxos.schema.Text",
|
|
44
|
-
refModelType: "dxos.org/model/text",
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
types.link();
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// TODO(wittjosiah): Remove. Use DXOS protocols to reduce duplicate functionality weighing down apps.
|
|
6
|
-
import * as decoding from 'lib0/decoding';
|
|
7
|
-
import * as encoding from 'lib0/encoding';
|
|
8
|
-
import { Observable } from 'lib0/observable';
|
|
9
|
-
import { useMemo } from 'react';
|
|
10
|
-
import * as YP from 'y-protocols/awareness';
|
|
11
|
-
import * as syncProtocol from 'y-protocols/sync';
|
|
12
|
-
import { Doc } from 'yjs';
|
|
13
|
-
|
|
14
|
-
import { Event } from '@dxos/async';
|
|
15
|
-
import { log } from '@dxos/log';
|
|
16
|
-
import { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
|
|
17
|
-
|
|
18
|
-
import { type EditorModel } from '../hooks';
|
|
19
|
-
import { cursorColor } from '../styles';
|
|
20
|
-
|
|
21
|
-
type Awareness = YP.Awareness;
|
|
22
|
-
|
|
23
|
-
const messageSync = 0;
|
|
24
|
-
const messageAwareness = 1;
|
|
25
|
-
const messageQueryAwareness = 3;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Yjs awareness provider in-memory.
|
|
29
|
-
*/
|
|
30
|
-
export class AwarenessProvider extends Observable<any> {
|
|
31
|
-
private readonly _remoteUpdate: Event<Uint8Array>;
|
|
32
|
-
private readonly _awareness: Awareness;
|
|
33
|
-
private readonly _doc: Doc;
|
|
34
|
-
|
|
35
|
-
constructor({ update, doc, awareness }: { update: Event<Uint8Array>; doc: Doc; awareness?: Awareness }) {
|
|
36
|
-
super();
|
|
37
|
-
this._remoteUpdate = update;
|
|
38
|
-
this._doc = doc;
|
|
39
|
-
this._awareness = awareness ?? new YP.Awareness(doc);
|
|
40
|
-
|
|
41
|
-
this._doc.on('update', this._handleDocUpdate.bind(this));
|
|
42
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
43
|
-
this._remoteUpdate.on(this._handleRemoteUpdate.bind(this));
|
|
44
|
-
|
|
45
|
-
if (typeof window !== 'undefined') {
|
|
46
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
47
|
-
} else if (typeof process !== 'undefined') {
|
|
48
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Post queryAwareness.
|
|
52
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
53
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
54
|
-
void this._remoteUpdate.emit(encoding.toUint8Array(encoderAwarenessQuery));
|
|
55
|
-
|
|
56
|
-
// Post local awareness state.
|
|
57
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
58
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
59
|
-
encoding.writeVarUint8Array(encoderAwarenessState, YP.encodeAwarenessUpdate(this.awareness, [this._doc.clientID]));
|
|
60
|
-
void this._remoteUpdate.emit(encoding.toUint8Array(encoderAwarenessState));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get awareness(): Awareness {
|
|
64
|
-
return this._awareness;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private _handleDocUpdate(update: Uint8Array, origin: any) {
|
|
68
|
-
log('doc update', { update, origin });
|
|
69
|
-
const encoder = encoding.createEncoder();
|
|
70
|
-
encoding.writeVarUint(encoder, messageSync);
|
|
71
|
-
syncProtocol.writeUpdate(encoder, update);
|
|
72
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoder));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
76
|
-
log('awareness update', { added, updated, removed, origin });
|
|
77
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
78
|
-
const encoderAwareness = encoding.createEncoder();
|
|
79
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
80
|
-
encoding.writeVarUint8Array(encoderAwareness, YP.encodeAwarenessUpdate(this._awareness, changedClients));
|
|
81
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoderAwareness));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private _handleRemoteUpdate(payload: Uint8Array) {
|
|
85
|
-
log('remote update', { payload });
|
|
86
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
87
|
-
const encoder = this._readMessage(data);
|
|
88
|
-
if (encoder) {
|
|
89
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoder));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private _readMessage(message: Uint8Array) {
|
|
94
|
-
const decoder = decoding.createDecoder(message);
|
|
95
|
-
const encoder = encoding.createEncoder();
|
|
96
|
-
const messageType = decoding.readVarUint(decoder);
|
|
97
|
-
|
|
98
|
-
let sendReply = false;
|
|
99
|
-
switch (messageType) {
|
|
100
|
-
case messageSync: {
|
|
101
|
-
encoding.writeVarUint(encoder, messageSync);
|
|
102
|
-
syncProtocol.readSyncMessage(decoder, encoder, this._doc, this);
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
case messageAwareness: {
|
|
107
|
-
YP.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
case messageQueryAwareness: {
|
|
112
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
113
|
-
encoding.writeVarUint8Array(
|
|
114
|
-
encoder,
|
|
115
|
-
YP.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
116
|
-
);
|
|
117
|
-
sendReply = true;
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
default: {
|
|
122
|
-
console.error('Invalid message:', messageType);
|
|
123
|
-
return encoder;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!sendReply) {
|
|
128
|
-
// Nothing has been written, no answer created.
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return encoder;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private _handleBeforeUnload() {
|
|
136
|
-
YP.removeAwarenessStates(this._awareness, [this._doc.clientID], 'window unload');
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* This mocks the replication across the swarm.
|
|
142
|
-
*/
|
|
143
|
-
export class Replicator {
|
|
144
|
-
private readonly _update = new Event<Uint8Array>();
|
|
145
|
-
private _peers: EditorModel[] = [];
|
|
146
|
-
|
|
147
|
-
constructor(private readonly _kind: TextKind) {}
|
|
148
|
-
|
|
149
|
-
setPeers(peers: EditorModel[]) {
|
|
150
|
-
this._peers = peers;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
createPeer = (id: string, doc: Doc = new Doc()): EditorModel => {
|
|
154
|
-
const provider = new AwarenessProvider({ update: this._update, doc });
|
|
155
|
-
provider.awareness.setLocalStateField('user', {
|
|
156
|
-
name: 'Anonymous ' + Math.floor(Math.random() * 100),
|
|
157
|
-
color: cursorColor.color,
|
|
158
|
-
colorLight: cursorColor.light,
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const field = 'content';
|
|
162
|
-
const content = this._kind === TextKind.PLAIN ? doc.getText(field) : doc.getXmlFragment(field);
|
|
163
|
-
const model: EditorModel = {
|
|
164
|
-
id: doc.guid,
|
|
165
|
-
text: () => content.toString(),
|
|
166
|
-
content,
|
|
167
|
-
awareness: provider.awareness,
|
|
168
|
-
peer: { id },
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
this._peers.push(model);
|
|
172
|
-
return model;
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export type UseYjsModelOptions = {
|
|
177
|
-
replicator: Replicator;
|
|
178
|
-
id: string;
|
|
179
|
-
doc?: Doc;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
export const useYjsModel = ({ replicator, id, doc }: UseYjsModelOptions): EditorModel => {
|
|
183
|
-
return useMemo(() => replicator.createPeer(id, doc), [doc]);
|
|
184
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|