@kerebron/extension-yjs 0.6.7 → 0.7.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.
Files changed (103) hide show
  1. package/esm/ExtensionYjs.d.ts +3 -11
  2. package/esm/ExtensionYjs.d.ts.map +1 -1
  3. package/esm/ExtensionYjs.js +71 -45
  4. package/esm/ExtensionYjs.js.map +1 -1
  5. package/esm/WebsocketProvider.d.ts +70 -0
  6. package/esm/WebsocketProvider.d.ts.map +1 -0
  7. package/esm/WebsocketProvider.js +377 -0
  8. package/esm/WebsocketProvider.js.map +1 -0
  9. package/esm/YjsProvider.d.ts +48 -0
  10. package/esm/YjsProvider.d.ts.map +1 -0
  11. package/esm/YjsProvider.js +12 -0
  12. package/esm/YjsProvider.js.map +1 -0
  13. package/esm/_dnt.shims.d.ts +2 -0
  14. package/esm/_dnt.shims.d.ts.map +1 -0
  15. package/esm/_dnt.shims.js +58 -0
  16. package/esm/_dnt.shims.js.map +1 -0
  17. package/esm/binding/BindingMetadata.d.ts +6 -0
  18. package/esm/binding/BindingMetadata.d.ts.map +1 -0
  19. package/esm/binding/BindingMetadata.js +2 -0
  20. package/esm/binding/BindingMetadata.js.map +1 -0
  21. package/esm/binding/DiffViewer.d.ts +17 -0
  22. package/esm/binding/DiffViewer.d.ts.map +1 -0
  23. package/esm/binding/DiffViewer.js +96 -0
  24. package/esm/binding/DiffViewer.js.map +1 -0
  25. package/esm/binding/PmYjsBinding.d.ts +45 -0
  26. package/esm/binding/PmYjsBinding.d.ts.map +1 -0
  27. package/esm/binding/PmYjsBinding.js +230 -0
  28. package/esm/binding/PmYjsBinding.js.map +1 -0
  29. package/esm/binding/convertUtils.d.ts +48 -0
  30. package/esm/binding/convertUtils.d.ts.map +1 -0
  31. package/esm/binding/convertUtils.js +80 -0
  32. package/esm/binding/convertUtils.js.map +1 -0
  33. package/esm/{createNodeFromYElement.d.ts → binding/createNodeFromYElement.d.ts} +2 -2
  34. package/esm/binding/createNodeFromYElement.d.ts.map +1 -0
  35. package/esm/{createNodeFromYElement.js → binding/createNodeFromYElement.js} +2 -2
  36. package/esm/binding/createNodeFromYElement.js.map +1 -0
  37. package/esm/{updateYFragment.d.ts → binding/updateYFragment.d.ts} +3 -3
  38. package/esm/binding/updateYFragment.d.ts.map +1 -0
  39. package/esm/{updateYFragment.js → binding/updateYFragment.js} +10 -7
  40. package/esm/binding/updateYFragment.js.map +1 -0
  41. package/esm/debug.d.ts.map +1 -1
  42. package/esm/debug.js +11 -0
  43. package/esm/debug.js.map +1 -1
  44. package/esm/lib.d.ts +1 -7
  45. package/esm/lib.d.ts.map +1 -1
  46. package/esm/lib.js +1 -200
  47. package/esm/lib.js.map +1 -1
  48. package/esm/position.d.ts +8 -0
  49. package/esm/position.d.ts.map +1 -0
  50. package/esm/position.js +165 -0
  51. package/esm/position.js.map +1 -0
  52. package/esm/ui/selection.d.ts +29 -0
  53. package/esm/ui/selection.d.ts.map +1 -0
  54. package/esm/ui/selection.js +129 -0
  55. package/esm/ui/selection.js.map +1 -0
  56. package/esm/utils.d.ts +1 -1
  57. package/esm/utils.d.ts.map +1 -1
  58. package/esm/utils.js.map +1 -1
  59. package/esm/yPositionPlugin.d.ts +6 -1
  60. package/esm/yPositionPlugin.d.ts.map +1 -1
  61. package/esm/yPositionPlugin.js +91 -50
  62. package/esm/yPositionPlugin.js.map +1 -1
  63. package/esm/ySyncPlugin.d.ts +5 -22
  64. package/esm/ySyncPlugin.d.ts.map +1 -1
  65. package/esm/ySyncPlugin.js +70 -101
  66. package/esm/ySyncPlugin.js.map +1 -1
  67. package/esm/yUndoPlugin.d.ts +11 -10
  68. package/esm/yUndoPlugin.d.ts.map +1 -1
  69. package/esm/yUndoPlugin.js +90 -52
  70. package/esm/yUndoPlugin.js.map +1 -1
  71. package/package.json +9 -6
  72. package/src/ExtensionYjs.ts +98 -67
  73. package/src/WebsocketProvider.ts +528 -0
  74. package/src/YjsProvider.ts +75 -0
  75. package/src/_dnt.shims.ts +60 -0
  76. package/src/binding/BindingMetadata.ts +6 -0
  77. package/src/binding/DiffViewer.ts +138 -0
  78. package/src/binding/PmYjsBinding.ts +360 -0
  79. package/src/binding/convertUtils.ts +124 -0
  80. package/src/{createNodeFromYElement.ts → binding/createNodeFromYElement.ts} +4 -4
  81. package/src/{updateYFragment.ts → binding/updateYFragment.ts} +15 -8
  82. package/src/debug.ts +21 -0
  83. package/src/lib.ts +4 -230
  84. package/src/position.ts +191 -0
  85. package/src/ui/selection.ts +218 -0
  86. package/src/utils.ts +1 -1
  87. package/src/yPositionPlugin.ts +122 -74
  88. package/src/ySyncPlugin.ts +111 -155
  89. package/src/yUndoPlugin.ts +113 -62
  90. package/esm/ProsemirrorBinding.d.ts +0 -60
  91. package/esm/ProsemirrorBinding.d.ts.map +0 -1
  92. package/esm/ProsemirrorBinding.js +0 -405
  93. package/esm/ProsemirrorBinding.js.map +0 -1
  94. package/esm/createNodeFromYElement.d.ts.map +0 -1
  95. package/esm/createNodeFromYElement.js.map +0 -1
  96. package/esm/updateYFragment.d.ts.map +0 -1
  97. package/esm/updateYFragment.js.map +0 -1
  98. package/esm/userColors.d.ts +0 -5
  99. package/esm/userColors.d.ts.map +0 -1
  100. package/esm/userColors.js +0 -11
  101. package/esm/userColors.js.map +0 -1
  102. package/src/ProsemirrorBinding.ts +0 -607
  103. package/src/userColors.ts +0 -10
package/package.json CHANGED
@@ -1,18 +1,21 @@
1
1
  {
2
2
  "name": "@kerebron/extension-yjs",
3
- "version": "0.6.7",
3
+ "version": "0.7.1",
4
4
  "license": "MIT",
5
5
  "module": "./esm/ExtensionYjs.js",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./esm/ExtensionYjs.js"
9
9
  },
10
+ "./YjsProvider": {
11
+ "import": "./esm/YjsProvider.js"
12
+ },
13
+ "./WebsocketProvider": {
14
+ "import": "./esm/WebsocketProvider.js"
15
+ },
10
16
  "./MarkYChange": {
11
17
  "import": "./esm/MarkYChange.js"
12
18
  },
13
- "./userColors": {
14
- "import": "./esm/userColors.js"
15
- },
16
19
  "./debug": {
17
20
  "import": "./esm/debug.js"
18
21
  }
@@ -23,8 +26,8 @@
23
26
  "src"
24
27
  ],
25
28
  "dependencies": {
26
- "@kerebron/editor": "0.6.7",
27
- "@kerebron/extension-basic-editor": "0.6.7",
29
+ "@kerebron/editor": "0.7.1",
30
+ "@kerebron/extension-basic-editor": "0.7.1",
28
31
  "lib0": "0.2.109",
29
32
  "prosemirror-model": "1.25.3",
30
33
  "prosemirror-state": "1.4.3",
@@ -1,40 +1,23 @@
1
- import type { Node, Schema } from 'prosemirror-model';
2
- import type { Plugin } from 'prosemirror-state';
1
+ import type { EditorState, Plugin, Transaction } from 'prosemirror-state';
3
2
 
4
3
  import * as Y from 'yjs';
5
- import * as awarenessProtocol from 'y-protocols/awareness';
6
4
 
7
- import { Converter, CoreEditor, Extension } from '@kerebron/editor';
5
+ import { Extension } from '@kerebron/editor';
8
6
  import type {
9
7
  CommandFactories,
8
+ CommandFactory,
10
9
  CommandShortcuts,
11
10
  } from '@kerebron/editor/commands';
12
11
 
13
- import { ySyncPluginKey } from './keys.js';
14
12
  import { ySyncPlugin } from './ySyncPlugin.js';
15
13
  import { yPositionPlugin } from './yPositionPlugin.js';
16
- import { redo, undo, yUndoPlugin } from './yUndoPlugin.js';
17
-
18
- export interface YjsProvider {
19
- on(eventName: string, callback: (event: any) => void): void;
20
- awareness: awarenessProtocol.Awareness;
21
- }
22
-
23
- function stringToIndex(str: string, arrayLength: number) {
24
- let hash = 0;
25
-
26
- for (let i = 0; i < str.length; i++) {
27
- hash = (hash << 5) - hash + str.charCodeAt(i);
28
- hash |= 0; // force 32-bit integer
29
- }
30
-
31
- return Math.abs(hash) % arrayLength;
32
- }
14
+ import { redoCommand, undoCommand, yUndoPlugin } from './yUndoPlugin.js';
15
+ import { ySyncPluginKey } from './keys.js';
33
16
 
34
- export type CreateWsProvider = (roomId: string) => [YjsProvider, Y.Doc];
17
+ import type { CreateYjsProvider } from './YjsProvider.js';
35
18
 
36
19
  export interface YjsConfig {
37
- createWsProvider: CreateWsProvider;
20
+ createYjsProvider: CreateYjsProvider;
38
21
  }
39
22
 
40
23
  export class ExtensionYjs extends Extension {
@@ -43,11 +26,98 @@ export class ExtensionYjs extends Extension {
43
26
  override conflicts = ['history'];
44
27
  requires = ['remote-selection'];
45
28
 
46
- // declare type Command = (state: EditorState, dispatch?: (tr: Transaction) => void, view?: EditorView) => boolean;
47
29
  override getCommandFactories(): Partial<CommandFactories> {
30
+ const changeRoom: CommandFactory = (roomId: string) => {
31
+ return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
32
+ const tr = state.tr;
33
+ tr.setMeta(ySyncPluginKey, { changeRoom: { roomId } });
34
+
35
+ if (dispatch) {
36
+ dispatch(tr);
37
+ }
38
+
39
+ return true;
40
+ };
41
+ };
42
+
43
+ const leaveRoom: CommandFactory = () => {
44
+ return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
45
+ const tr = state.tr;
46
+ tr.setMeta(ySyncPluginKey, { leaveRoom: true });
47
+
48
+ if (dispatch) {
49
+ dispatch(tr);
50
+ }
51
+
52
+ return true;
53
+ };
54
+ };
55
+
56
+ const getYDoc: CommandFactory = (
57
+ { resolve, reject }: {
58
+ resolve: (doc: Y.Doc) => void;
59
+ reject: (reason: any) => void;
60
+ },
61
+ ) => {
62
+ return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
63
+ const tr = state.tr;
64
+ tr.setMeta(ySyncPluginKey, { getYDoc: { resolve, reject } });
65
+ if (dispatch) {
66
+ dispatch(tr);
67
+ }
68
+ return true;
69
+ };
70
+ };
71
+
72
+ const getYSnapshot: CommandFactory = (
73
+ { resolve, reject }: {
74
+ resolve: (snapshot: Uint8Array) => void;
75
+ reject: (reason: any) => void;
76
+ },
77
+ ) => {
78
+ return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
79
+ const tr = state.tr;
80
+ tr.setMeta(ySyncPluginKey, { getYSnapshot: { resolve, reject } });
81
+ if (dispatch) {
82
+ dispatch(tr);
83
+ }
84
+ return true;
85
+ };
86
+ };
87
+
88
+ const setYSnapshot: CommandFactory = (
89
+ opts: { prevSnapshot: Uint8Array; snapshot: Uint8Array },
90
+ ) => {
91
+ return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
92
+ const tr = state.tr;
93
+ tr.setMeta(ySyncPluginKey, { setYSnapshot: opts });
94
+ if (dispatch) {
95
+ dispatch(tr);
96
+ }
97
+ return true;
98
+ };
99
+ };
100
+
101
+ const resetYSnapshot: CommandFactory = () => {
102
+ return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
103
+ const tr = state.tr;
104
+ tr.setMeta(ySyncPluginKey, { resetYSnapshot: true });
105
+ if (dispatch) {
106
+ dispatch(tr);
107
+ }
108
+ return true;
109
+ };
110
+ };
111
+
48
112
  return {
49
- 'undo': () => undo,
50
- 'redo': () => redo,
113
+ getYDoc,
114
+ changeRoom,
115
+ leaveRoom,
116
+ 'undo': () => undoCommand,
117
+ 'redo': () => redoCommand,
118
+ getYSnapshot,
119
+ setYSnapshot,
120
+ resetYSnapshot,
51
121
  };
52
122
  }
53
123
 
@@ -62,48 +132,9 @@ export class ExtensionYjs extends Extension {
62
132
  super();
63
133
  }
64
134
 
65
- // changeUser(userName: string) {
66
- // const idx = stringToIndex(userName, userColors.length);
67
- // const userColor = userColors[idx];
68
- // this.wsProvider.awareness.setLocalStateField('user', {
69
- // name: userName,
70
- // color: userColor.color,
71
- // colorLight: userColor.light,
72
- // });
73
- // }
74
- // //
75
-
76
- override getConverters(
77
- editor: CoreEditor,
78
- schema: Schema,
79
- ): Record<string, Converter> {
80
- return {
81
- 'yjs': {
82
- fromDoc: async (document: Node): Promise<Uint8Array> => {
83
- throw new Error('Not implemented');
84
- },
85
- toDoc: async (buffer: Uint8Array): Promise<Node> => {
86
- const roomId = new TextDecoder().decode(buffer);
87
-
88
- const tr = editor.state.tr.setMeta(ySyncPluginKey, {
89
- roomId: '',
90
- });
91
- editor.view.dispatch(tr);
92
-
93
- setTimeout(() => {
94
- const tr = editor.state.tr.setMeta(ySyncPluginKey, { roomId });
95
- editor.view.dispatch(tr);
96
- }, 100);
97
-
98
- return schema.topNodeType.createAndFill()!;
99
- },
100
- },
101
- };
102
- }
103
-
104
135
  override getProseMirrorPlugins(): Plugin[] {
105
136
  return [
106
- ySyncPlugin(this.editor.schema, this.config.createWsProvider),
137
+ ySyncPlugin(this.editor, this.config.createYjsProvider),
107
138
  yPositionPlugin(this.editor),
108
139
  yUndoPlugin(),
109
140
  ];