@remirror/extension-yjs 1.0.20 → 1.0.21

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.
@@ -73,6 +73,9 @@ export declare class YjsExtension extends PlainExtension<YjsOptions> {
73
73
  * The provider that is being used for the editor.
74
74
  */
75
75
  get provider(): YjsRealtimeProvider;
76
+ getBinding(): {
77
+ mapping: Map<any, any>;
78
+ } | undefined;
76
79
  onView(): Dispose | void;
77
80
  /**
78
81
  * Create the yjs plugins.
@@ -110,8 +113,6 @@ export declare class YjsExtension extends PlainExtension<YjsOptions> {
110
113
  * Handle the redo keybinding for the editor.
111
114
  */
112
115
  redoShortcut(props: KeyBindingProps): boolean;
113
- private absolutePositionToRelativePosition;
114
- private relativePositionToAbsolutePosition;
115
116
  }
116
117
  /**
117
118
  * The default destroy provider method.
@@ -3,6 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _applyDecoratedDescriptor = require('@babel/runtime/helpers/applyDecoratedDescriptor');
6
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
7
+ var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
6
8
  var yProsemirror = require('y-prosemirror');
7
9
  var yjs = require('yjs');
8
10
  var core = require('@remirror/core');
@@ -10,6 +12,94 @@ var extensionAnnotation = require('@remirror/extension-annotation');
10
12
  var messages = require('@remirror/messages');
11
13
 
12
14
  var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;
15
+
16
+ var _excluded = ["from", "to"],
17
+ _excluded2 = ["from", "to"];
18
+
19
+ class YjsAnnotationStore {
20
+ constructor(doc, pmName, mapName, getMapping) {
21
+ this.doc = doc;
22
+ this.getMapping = getMapping;
23
+ this.type = doc.getXmlFragment(pmName);
24
+ this.map = doc.getMap(mapName);
25
+ }
26
+
27
+ addAnnotation(_ref) {
28
+ var from = _ref.from,
29
+ to = _ref.to,
30
+ data = _objectWithoutProperties(_ref, _excluded);
31
+
32
+ // XXX: Why is this cast needed?
33
+ var storedData = _objectSpread(_objectSpread({}, data), {}, {
34
+ from: this.absolutePositionToRelativePosition(from),
35
+ to: this.absolutePositionToRelativePosition(to)
36
+ });
37
+
38
+ this.map.set(data.id, storedData);
39
+ }
40
+
41
+ updateAnnotation(id, updateData) {
42
+ var existing = this.map.get(id);
43
+ core.assert(existing);
44
+ this.map.set(id, _objectSpread(_objectSpread({}, updateData), {}, {
45
+ from: existing.from,
46
+ to: existing.to
47
+ }));
48
+ }
49
+
50
+ removeAnnotations(ids) {
51
+ yjs.transact(this.doc, () => {
52
+ ids.forEach(id => this.map.delete(id));
53
+ });
54
+ }
55
+
56
+ setAnnotations(annotations) {
57
+ yjs.transact(this.doc, () => {
58
+ this.map.clear();
59
+ annotations.forEach(annotation => this.addAnnotation(annotation));
60
+ });
61
+ }
62
+
63
+ formatAnnotations() {
64
+ var result = [];
65
+ this.map.forEach(_ref2 => {
66
+ var relFrom = _ref2.from,
67
+ relTo = _ref2.to,
68
+ data = _objectWithoutProperties(_ref2, _excluded2);
69
+
70
+ var from = this.relativePositionToAbsolutePosition(relFrom);
71
+ var to = this.relativePositionToAbsolutePosition(relTo);
72
+
73
+ if (!from || !to) {
74
+ return;
75
+ } // XXX: Why is this cast needed?
76
+
77
+
78
+ result.push(_objectSpread(_objectSpread({}, data), {}, {
79
+ from,
80
+ to
81
+ }));
82
+ });
83
+ return result;
84
+ }
85
+
86
+ absolutePositionToRelativePosition(pos) {
87
+ var mapping = this.getMapping();
88
+ return yProsemirror.absolutePositionToRelativePosition(pos, this.type, mapping);
89
+ }
90
+
91
+ relativePositionToAbsolutePosition(relPos) {
92
+ var mapping = this.getMapping();
93
+ return yProsemirror.relativePositionToAbsolutePosition(this.doc, this.type, relPos, mapping);
94
+ }
95
+
96
+ }
97
+ /**
98
+ * The YJS extension is the recommended extension for creating a collaborative
99
+ * editor.
100
+ */
101
+
102
+
13
103
  var YjsExtension = (_dec = core.extension({
14
104
  defaultOptions: {
15
105
  getProvider: () => {
@@ -29,23 +119,23 @@ var YjsExtension = (_dec = core.extension({
29
119
  defaultPriority: core.ExtensionPriority.High
30
120
  }), _dec2 = core.command({
31
121
  disableChaining: true,
32
- description: _ref => {
33
- var t = _ref.t;
122
+ description: _ref3 => {
123
+ var t = _ref3.t;
34
124
  return t(messages.ExtensionHistoryMessages.UNDO_DESCRIPTION);
35
125
  },
36
- label: _ref2 => {
37
- var t = _ref2.t;
126
+ label: _ref4 => {
127
+ var t = _ref4.t;
38
128
  return t(messages.ExtensionHistoryMessages.UNDO_LABEL);
39
129
  },
40
130
  icon: 'arrowGoBackFill'
41
131
  }), _dec3 = core.command({
42
132
  disableChaining: true,
43
- description: _ref3 => {
44
- var t = _ref3.t;
133
+ description: _ref5 => {
134
+ var t = _ref5.t;
45
135
  return t(messages.ExtensionHistoryMessages.REDO_DESCRIPTION);
46
136
  },
47
- label: _ref4 => {
48
- var t = _ref4.t;
137
+ label: _ref6 => {
138
+ var t = _ref6.t;
49
139
  return t(messages.ExtensionHistoryMessages.REDO_LABEL);
50
140
  },
51
141
  icon: 'arrowGoForwardFill'
@@ -70,12 +160,24 @@ var YjsExtension = (_dec = core.extension({
70
160
  return (_this$_provider = this._provider) !== null && _this$_provider !== void 0 ? _this$_provider : this._provider = getLazyValue(getProvider);
71
161
  }
72
162
 
163
+ getBinding() {
164
+ var state = this.store.getState();
165
+
166
+ var _ySyncPluginKey$getSt = yProsemirror.ySyncPluginKey.getState(state),
167
+ binding = _ySyncPluginKey$getSt.binding;
168
+
169
+ return binding;
170
+ }
171
+
73
172
  onView() {
74
173
  try {
174
+ var annotationStore = new YjsAnnotationStore(this.provider.doc, 'prosemirror', 'annotations', () => {
175
+ var _this$getBinding;
176
+
177
+ return (_this$getBinding = this.getBinding()) === null || _this$getBinding === void 0 ? void 0 : _this$getBinding.mapping;
178
+ });
75
179
  this.store.manager.getExtension(extensionAnnotation.AnnotationExtension).setOptions({
76
- getMap: () => this.provider.doc.getMap('annotations'),
77
- transformPosition: this.absolutePositionToRelativePosition.bind(this),
78
- transformPositionBeforeRender: this.relativePositionToAbsolutePosition.bind(this)
180
+ getStore: () => annotationStore
79
181
  });
80
182
 
81
183
  var handler = (_update, _origin, _doc, yjsTr) => {
@@ -239,26 +341,6 @@ var YjsExtension = (_dec = core.extension({
239
341
  return this.yRedo()(props);
240
342
  }
241
343
 
242
- absolutePositionToRelativePosition(pos) {
243
- var state = this.store.getState();
244
-
245
- var _ySyncPluginKey$getSt = yProsemirror.ySyncPluginKey.getState(state),
246
- type = _ySyncPluginKey$getSt.type,
247
- binding = _ySyncPluginKey$getSt.binding;
248
-
249
- return yProsemirror.absolutePositionToRelativePosition(pos, type, binding.mapping);
250
- }
251
-
252
- relativePositionToAbsolutePosition(relPos) {
253
- var state = this.store.getState();
254
-
255
- var _ySyncPluginKey$getSt2 = yProsemirror.ySyncPluginKey.getState(state),
256
- type = _ySyncPluginKey$getSt2.type,
257
- binding = _ySyncPluginKey$getSt2.binding;
258
-
259
- return yProsemirror.relativePositionToAbsolutePosition(this.provider.doc, type, relPos, binding.mapping);
260
- }
261
-
262
344
  }, (_applyDecoratedDescriptor(_class2.prototype, "yUndo", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "yUndo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "yRedo", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "yRedo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "undoShortcut", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "undoShortcut"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "redoShortcut", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "redoShortcut"), _class2.prototype)), _class2)) || _class);
263
345
  /**
264
346
  * The default destroy provider method.
@@ -1,11 +1,101 @@
1
1
  import _applyDecoratedDescriptor from '@babel/runtime/helpers/esm/applyDecoratedDescriptor';
2
+ import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
3
+ import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
2
4
  import { defaultCursorBuilder, ySyncPluginKey, defaultDeleteFilter, ySyncPlugin, yCursorPlugin, yUndoPlugin, yUndoPluginKey, undo, redo, absolutePositionToRelativePosition, relativePositionToAbsolutePosition } from 'y-prosemirror';
3
- import { UndoManager } from 'yjs';
4
- import { extension, invariant, ErrorConstant, ExtensionPriority, command, keyBinding, NamedShortcut, PlainExtension, isFunction, isEmptyObject, nonChainable, convertCommand } from '@remirror/core';
5
+ import { UndoManager, transact } from 'yjs';
6
+ import { extension, invariant, ErrorConstant, ExtensionPriority, command, keyBinding, NamedShortcut, PlainExtension, isFunction, isEmptyObject, nonChainable, convertCommand, assert } from '@remirror/core';
5
7
  import { AnnotationExtension } from '@remirror/extension-annotation';
6
8
  import { ExtensionHistoryMessages } from '@remirror/messages';
7
9
 
8
10
  var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;
11
+
12
+ var _excluded = ["from", "to"],
13
+ _excluded2 = ["from", "to"];
14
+
15
+ class YjsAnnotationStore {
16
+ constructor(doc, pmName, mapName, getMapping) {
17
+ this.doc = doc;
18
+ this.getMapping = getMapping;
19
+ this.type = doc.getXmlFragment(pmName);
20
+ this.map = doc.getMap(mapName);
21
+ }
22
+
23
+ addAnnotation(_ref) {
24
+ var from = _ref.from,
25
+ to = _ref.to,
26
+ data = _objectWithoutProperties(_ref, _excluded);
27
+
28
+ // XXX: Why is this cast needed?
29
+ var storedData = _objectSpread(_objectSpread({}, data), {}, {
30
+ from: this.absolutePositionToRelativePosition(from),
31
+ to: this.absolutePositionToRelativePosition(to)
32
+ });
33
+
34
+ this.map.set(data.id, storedData);
35
+ }
36
+
37
+ updateAnnotation(id, updateData) {
38
+ var existing = this.map.get(id);
39
+ assert(existing);
40
+ this.map.set(id, _objectSpread(_objectSpread({}, updateData), {}, {
41
+ from: existing.from,
42
+ to: existing.to
43
+ }));
44
+ }
45
+
46
+ removeAnnotations(ids) {
47
+ transact(this.doc, () => {
48
+ ids.forEach(id => this.map.delete(id));
49
+ });
50
+ }
51
+
52
+ setAnnotations(annotations) {
53
+ transact(this.doc, () => {
54
+ this.map.clear();
55
+ annotations.forEach(annotation => this.addAnnotation(annotation));
56
+ });
57
+ }
58
+
59
+ formatAnnotations() {
60
+ var result = [];
61
+ this.map.forEach(_ref2 => {
62
+ var relFrom = _ref2.from,
63
+ relTo = _ref2.to,
64
+ data = _objectWithoutProperties(_ref2, _excluded2);
65
+
66
+ var from = this.relativePositionToAbsolutePosition(relFrom);
67
+ var to = this.relativePositionToAbsolutePosition(relTo);
68
+
69
+ if (!from || !to) {
70
+ return;
71
+ } // XXX: Why is this cast needed?
72
+
73
+
74
+ result.push(_objectSpread(_objectSpread({}, data), {}, {
75
+ from,
76
+ to
77
+ }));
78
+ });
79
+ return result;
80
+ }
81
+
82
+ absolutePositionToRelativePosition(pos) {
83
+ var mapping = this.getMapping();
84
+ return absolutePositionToRelativePosition(pos, this.type, mapping);
85
+ }
86
+
87
+ relativePositionToAbsolutePosition(relPos) {
88
+ var mapping = this.getMapping();
89
+ return relativePositionToAbsolutePosition(this.doc, this.type, relPos, mapping);
90
+ }
91
+
92
+ }
93
+ /**
94
+ * The YJS extension is the recommended extension for creating a collaborative
95
+ * editor.
96
+ */
97
+
98
+
9
99
  var YjsExtension = (_dec = extension({
10
100
  defaultOptions: {
11
101
  getProvider: () => {
@@ -25,23 +115,23 @@ var YjsExtension = (_dec = extension({
25
115
  defaultPriority: ExtensionPriority.High
26
116
  }), _dec2 = command({
27
117
  disableChaining: true,
28
- description: _ref => {
29
- var t = _ref.t;
118
+ description: _ref3 => {
119
+ var t = _ref3.t;
30
120
  return t(ExtensionHistoryMessages.UNDO_DESCRIPTION);
31
121
  },
32
- label: _ref2 => {
33
- var t = _ref2.t;
122
+ label: _ref4 => {
123
+ var t = _ref4.t;
34
124
  return t(ExtensionHistoryMessages.UNDO_LABEL);
35
125
  },
36
126
  icon: 'arrowGoBackFill'
37
127
  }), _dec3 = command({
38
128
  disableChaining: true,
39
- description: _ref3 => {
40
- var t = _ref3.t;
129
+ description: _ref5 => {
130
+ var t = _ref5.t;
41
131
  return t(ExtensionHistoryMessages.REDO_DESCRIPTION);
42
132
  },
43
- label: _ref4 => {
44
- var t = _ref4.t;
133
+ label: _ref6 => {
134
+ var t = _ref6.t;
45
135
  return t(ExtensionHistoryMessages.REDO_LABEL);
46
136
  },
47
137
  icon: 'arrowGoForwardFill'
@@ -66,12 +156,24 @@ var YjsExtension = (_dec = extension({
66
156
  return (_this$_provider = this._provider) !== null && _this$_provider !== void 0 ? _this$_provider : this._provider = getLazyValue(getProvider);
67
157
  }
68
158
 
159
+ getBinding() {
160
+ var state = this.store.getState();
161
+
162
+ var _ySyncPluginKey$getSt = ySyncPluginKey.getState(state),
163
+ binding = _ySyncPluginKey$getSt.binding;
164
+
165
+ return binding;
166
+ }
167
+
69
168
  onView() {
70
169
  try {
170
+ var annotationStore = new YjsAnnotationStore(this.provider.doc, 'prosemirror', 'annotations', () => {
171
+ var _this$getBinding;
172
+
173
+ return (_this$getBinding = this.getBinding()) === null || _this$getBinding === void 0 ? void 0 : _this$getBinding.mapping;
174
+ });
71
175
  this.store.manager.getExtension(AnnotationExtension).setOptions({
72
- getMap: () => this.provider.doc.getMap('annotations'),
73
- transformPosition: this.absolutePositionToRelativePosition.bind(this),
74
- transformPositionBeforeRender: this.relativePositionToAbsolutePosition.bind(this)
176
+ getStore: () => annotationStore
75
177
  });
76
178
 
77
179
  var handler = (_update, _origin, _doc, yjsTr) => {
@@ -235,26 +337,6 @@ var YjsExtension = (_dec = extension({
235
337
  return this.yRedo()(props);
236
338
  }
237
339
 
238
- absolutePositionToRelativePosition(pos) {
239
- var state = this.store.getState();
240
-
241
- var _ySyncPluginKey$getSt = ySyncPluginKey.getState(state),
242
- type = _ySyncPluginKey$getSt.type,
243
- binding = _ySyncPluginKey$getSt.binding;
244
-
245
- return absolutePositionToRelativePosition(pos, type, binding.mapping);
246
- }
247
-
248
- relativePositionToAbsolutePosition(relPos) {
249
- var state = this.store.getState();
250
-
251
- var _ySyncPluginKey$getSt2 = ySyncPluginKey.getState(state),
252
- type = _ySyncPluginKey$getSt2.type,
253
- binding = _ySyncPluginKey$getSt2.binding;
254
-
255
- return relativePositionToAbsolutePosition(this.provider.doc, type, relPos, binding.mapping);
256
- }
257
-
258
340
  }, (_applyDecoratedDescriptor(_class2.prototype, "yUndo", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "yUndo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "yRedo", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "yRedo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "undoShortcut", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "undoShortcut"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "redoShortcut", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "redoShortcut"), _class2.prototype)), _class2)) || _class);
259
341
  /**
260
342
  * The default destroy provider method.
@@ -3,6 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _applyDecoratedDescriptor = require('@babel/runtime/helpers/applyDecoratedDescriptor');
6
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
7
+ var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
6
8
  var yProsemirror = require('y-prosemirror');
7
9
  var yjs = require('yjs');
8
10
  var core = require('@remirror/core');
@@ -10,6 +12,94 @@ var extensionAnnotation = require('@remirror/extension-annotation');
10
12
  var messages = require('@remirror/messages');
11
13
 
12
14
  var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;
15
+
16
+ var _excluded = ["from", "to"],
17
+ _excluded2 = ["from", "to"];
18
+
19
+ class YjsAnnotationStore {
20
+ constructor(doc, pmName, mapName, getMapping) {
21
+ this.doc = doc;
22
+ this.getMapping = getMapping;
23
+ this.type = doc.getXmlFragment(pmName);
24
+ this.map = doc.getMap(mapName);
25
+ }
26
+
27
+ addAnnotation(_ref) {
28
+ var from = _ref.from,
29
+ to = _ref.to,
30
+ data = _objectWithoutProperties(_ref, _excluded);
31
+
32
+ // XXX: Why is this cast needed?
33
+ var storedData = _objectSpread(_objectSpread({}, data), {}, {
34
+ from: this.absolutePositionToRelativePosition(from),
35
+ to: this.absolutePositionToRelativePosition(to)
36
+ });
37
+
38
+ this.map.set(data.id, storedData);
39
+ }
40
+
41
+ updateAnnotation(id, updateData) {
42
+ var existing = this.map.get(id);
43
+ core.assert(existing);
44
+ this.map.set(id, _objectSpread(_objectSpread({}, updateData), {}, {
45
+ from: existing.from,
46
+ to: existing.to
47
+ }));
48
+ }
49
+
50
+ removeAnnotations(ids) {
51
+ yjs.transact(this.doc, () => {
52
+ ids.forEach(id => this.map.delete(id));
53
+ });
54
+ }
55
+
56
+ setAnnotations(annotations) {
57
+ yjs.transact(this.doc, () => {
58
+ this.map.clear();
59
+ annotations.forEach(annotation => this.addAnnotation(annotation));
60
+ });
61
+ }
62
+
63
+ formatAnnotations() {
64
+ var result = [];
65
+ this.map.forEach(_ref2 => {
66
+ var relFrom = _ref2.from,
67
+ relTo = _ref2.to,
68
+ data = _objectWithoutProperties(_ref2, _excluded2);
69
+
70
+ var from = this.relativePositionToAbsolutePosition(relFrom);
71
+ var to = this.relativePositionToAbsolutePosition(relTo);
72
+
73
+ if (!from || !to) {
74
+ return;
75
+ } // XXX: Why is this cast needed?
76
+
77
+
78
+ result.push(_objectSpread(_objectSpread({}, data), {}, {
79
+ from,
80
+ to
81
+ }));
82
+ });
83
+ return result;
84
+ }
85
+
86
+ absolutePositionToRelativePosition(pos) {
87
+ var mapping = this.getMapping();
88
+ return yProsemirror.absolutePositionToRelativePosition(pos, this.type, mapping);
89
+ }
90
+
91
+ relativePositionToAbsolutePosition(relPos) {
92
+ var mapping = this.getMapping();
93
+ return yProsemirror.relativePositionToAbsolutePosition(this.doc, this.type, relPos, mapping);
94
+ }
95
+
96
+ }
97
+ /**
98
+ * The YJS extension is the recommended extension for creating a collaborative
99
+ * editor.
100
+ */
101
+
102
+
13
103
  var YjsExtension = (_dec = core.extension({
14
104
  defaultOptions: {
15
105
  getProvider: () => {
@@ -29,23 +119,23 @@ var YjsExtension = (_dec = core.extension({
29
119
  defaultPriority: core.ExtensionPriority.High
30
120
  }), _dec2 = core.command({
31
121
  disableChaining: true,
32
- description: _ref => {
33
- var t = _ref.t;
122
+ description: _ref3 => {
123
+ var t = _ref3.t;
34
124
  return t(messages.ExtensionHistoryMessages.UNDO_DESCRIPTION);
35
125
  },
36
- label: _ref2 => {
37
- var t = _ref2.t;
126
+ label: _ref4 => {
127
+ var t = _ref4.t;
38
128
  return t(messages.ExtensionHistoryMessages.UNDO_LABEL);
39
129
  },
40
130
  icon: 'arrowGoBackFill'
41
131
  }), _dec3 = core.command({
42
132
  disableChaining: true,
43
- description: _ref3 => {
44
- var t = _ref3.t;
133
+ description: _ref5 => {
134
+ var t = _ref5.t;
45
135
  return t(messages.ExtensionHistoryMessages.REDO_DESCRIPTION);
46
136
  },
47
- label: _ref4 => {
48
- var t = _ref4.t;
137
+ label: _ref6 => {
138
+ var t = _ref6.t;
49
139
  return t(messages.ExtensionHistoryMessages.REDO_LABEL);
50
140
  },
51
141
  icon: 'arrowGoForwardFill'
@@ -70,12 +160,24 @@ var YjsExtension = (_dec = core.extension({
70
160
  return (_this$_provider = this._provider) !== null && _this$_provider !== void 0 ? _this$_provider : this._provider = getLazyValue(getProvider);
71
161
  }
72
162
 
163
+ getBinding() {
164
+ var state = this.store.getState();
165
+
166
+ var _ySyncPluginKey$getSt = yProsemirror.ySyncPluginKey.getState(state),
167
+ binding = _ySyncPluginKey$getSt.binding;
168
+
169
+ return binding;
170
+ }
171
+
73
172
  onView() {
74
173
  try {
174
+ var annotationStore = new YjsAnnotationStore(this.provider.doc, 'prosemirror', 'annotations', () => {
175
+ var _this$getBinding;
176
+
177
+ return (_this$getBinding = this.getBinding()) === null || _this$getBinding === void 0 ? void 0 : _this$getBinding.mapping;
178
+ });
75
179
  this.store.manager.getExtension(extensionAnnotation.AnnotationExtension).setOptions({
76
- getMap: () => this.provider.doc.getMap('annotations'),
77
- transformPosition: this.absolutePositionToRelativePosition.bind(this),
78
- transformPositionBeforeRender: this.relativePositionToAbsolutePosition.bind(this)
180
+ getStore: () => annotationStore
79
181
  });
80
182
 
81
183
  var handler = (_update, _origin, _doc, yjsTr) => {
@@ -239,26 +341,6 @@ var YjsExtension = (_dec = core.extension({
239
341
  return this.yRedo()(props);
240
342
  }
241
343
 
242
- absolutePositionToRelativePosition(pos) {
243
- var state = this.store.getState();
244
-
245
- var _ySyncPluginKey$getSt = yProsemirror.ySyncPluginKey.getState(state),
246
- type = _ySyncPluginKey$getSt.type,
247
- binding = _ySyncPluginKey$getSt.binding;
248
-
249
- return yProsemirror.absolutePositionToRelativePosition(pos, type, binding.mapping);
250
- }
251
-
252
- relativePositionToAbsolutePosition(relPos) {
253
- var state = this.store.getState();
254
-
255
- var _ySyncPluginKey$getSt2 = yProsemirror.ySyncPluginKey.getState(state),
256
- type = _ySyncPluginKey$getSt2.type,
257
- binding = _ySyncPluginKey$getSt2.binding;
258
-
259
- return yProsemirror.relativePositionToAbsolutePosition(this.provider.doc, type, relPos, binding.mapping);
260
- }
261
-
262
344
  }, (_applyDecoratedDescriptor(_class2.prototype, "yUndo", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "yUndo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "yRedo", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "yRedo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "undoShortcut", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "undoShortcut"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "redoShortcut", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "redoShortcut"), _class2.prototype)), _class2)) || _class);
263
345
  /**
264
346
  * The default destroy provider method.
@@ -3,6 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _applyDecoratedDescriptor = require('@babel/runtime/helpers/applyDecoratedDescriptor');
6
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
7
+ var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
6
8
  var yProsemirror = require('y-prosemirror');
7
9
  var yjs = require('yjs');
8
10
  var core = require('@remirror/core');
@@ -10,6 +12,94 @@ var extensionAnnotation = require('@remirror/extension-annotation');
10
12
  var messages = require('@remirror/messages');
11
13
 
12
14
  var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;
15
+
16
+ var _excluded = ["from", "to"],
17
+ _excluded2 = ["from", "to"];
18
+
19
+ class YjsAnnotationStore {
20
+ constructor(doc, pmName, mapName, getMapping) {
21
+ this.doc = doc;
22
+ this.getMapping = getMapping;
23
+ this.type = doc.getXmlFragment(pmName);
24
+ this.map = doc.getMap(mapName);
25
+ }
26
+
27
+ addAnnotation(_ref) {
28
+ var from = _ref.from,
29
+ to = _ref.to,
30
+ data = _objectWithoutProperties(_ref, _excluded);
31
+
32
+ // XXX: Why is this cast needed?
33
+ var storedData = _objectSpread(_objectSpread({}, data), {}, {
34
+ from: this.absolutePositionToRelativePosition(from),
35
+ to: this.absolutePositionToRelativePosition(to)
36
+ });
37
+
38
+ this.map.set(data.id, storedData);
39
+ }
40
+
41
+ updateAnnotation(id, updateData) {
42
+ var existing = this.map.get(id);
43
+ core.assert(existing);
44
+ this.map.set(id, _objectSpread(_objectSpread({}, updateData), {}, {
45
+ from: existing.from,
46
+ to: existing.to
47
+ }));
48
+ }
49
+
50
+ removeAnnotations(ids) {
51
+ yjs.transact(this.doc, () => {
52
+ ids.forEach(id => this.map.delete(id));
53
+ });
54
+ }
55
+
56
+ setAnnotations(annotations) {
57
+ yjs.transact(this.doc, () => {
58
+ this.map.clear();
59
+ annotations.forEach(annotation => this.addAnnotation(annotation));
60
+ });
61
+ }
62
+
63
+ formatAnnotations() {
64
+ var result = [];
65
+ this.map.forEach(_ref2 => {
66
+ var relFrom = _ref2.from,
67
+ relTo = _ref2.to,
68
+ data = _objectWithoutProperties(_ref2, _excluded2);
69
+
70
+ var from = this.relativePositionToAbsolutePosition(relFrom);
71
+ var to = this.relativePositionToAbsolutePosition(relTo);
72
+
73
+ if (!from || !to) {
74
+ return;
75
+ } // XXX: Why is this cast needed?
76
+
77
+
78
+ result.push(_objectSpread(_objectSpread({}, data), {}, {
79
+ from,
80
+ to
81
+ }));
82
+ });
83
+ return result;
84
+ }
85
+
86
+ absolutePositionToRelativePosition(pos) {
87
+ var mapping = this.getMapping();
88
+ return yProsemirror.absolutePositionToRelativePosition(pos, this.type, mapping);
89
+ }
90
+
91
+ relativePositionToAbsolutePosition(relPos) {
92
+ var mapping = this.getMapping();
93
+ return yProsemirror.relativePositionToAbsolutePosition(this.doc, this.type, relPos, mapping);
94
+ }
95
+
96
+ }
97
+ /**
98
+ * The YJS extension is the recommended extension for creating a collaborative
99
+ * editor.
100
+ */
101
+
102
+
13
103
  var YjsExtension = (_dec = core.extension({
14
104
  defaultOptions: {
15
105
  getProvider: () => {
@@ -26,23 +116,23 @@ var YjsExtension = (_dec = core.extension({
26
116
  defaultPriority: core.ExtensionPriority.High
27
117
  }), _dec2 = core.command({
28
118
  disableChaining: true,
29
- description: _ref => {
30
- var t = _ref.t;
119
+ description: _ref3 => {
120
+ var t = _ref3.t;
31
121
  return t(messages.ExtensionHistoryMessages.UNDO_DESCRIPTION);
32
122
  },
33
- label: _ref2 => {
34
- var t = _ref2.t;
123
+ label: _ref4 => {
124
+ var t = _ref4.t;
35
125
  return t(messages.ExtensionHistoryMessages.UNDO_LABEL);
36
126
  },
37
127
  icon: 'arrowGoBackFill'
38
128
  }), _dec3 = core.command({
39
129
  disableChaining: true,
40
- description: _ref3 => {
41
- var t = _ref3.t;
130
+ description: _ref5 => {
131
+ var t = _ref5.t;
42
132
  return t(messages.ExtensionHistoryMessages.REDO_DESCRIPTION);
43
133
  },
44
- label: _ref4 => {
45
- var t = _ref4.t;
134
+ label: _ref6 => {
135
+ var t = _ref6.t;
46
136
  return t(messages.ExtensionHistoryMessages.REDO_LABEL);
47
137
  },
48
138
  icon: 'arrowGoForwardFill'
@@ -67,12 +157,24 @@ var YjsExtension = (_dec = core.extension({
67
157
  return (_this$_provider = this._provider) !== null && _this$_provider !== void 0 ? _this$_provider : this._provider = getLazyValue(getProvider);
68
158
  }
69
159
 
160
+ getBinding() {
161
+ var state = this.store.getState();
162
+
163
+ var _ySyncPluginKey$getSt = yProsemirror.ySyncPluginKey.getState(state),
164
+ binding = _ySyncPluginKey$getSt.binding;
165
+
166
+ return binding;
167
+ }
168
+
70
169
  onView() {
71
170
  try {
171
+ var annotationStore = new YjsAnnotationStore(this.provider.doc, 'prosemirror', 'annotations', () => {
172
+ var _this$getBinding;
173
+
174
+ return (_this$getBinding = this.getBinding()) === null || _this$getBinding === void 0 ? void 0 : _this$getBinding.mapping;
175
+ });
72
176
  this.store.manager.getExtension(extensionAnnotation.AnnotationExtension).setOptions({
73
- getMap: () => this.provider.doc.getMap('annotations'),
74
- transformPosition: this.absolutePositionToRelativePosition.bind(this),
75
- transformPositionBeforeRender: this.relativePositionToAbsolutePosition.bind(this)
177
+ getStore: () => annotationStore
76
178
  });
77
179
 
78
180
  var handler = (_update, _origin, _doc, yjsTr) => {
@@ -236,26 +338,6 @@ var YjsExtension = (_dec = core.extension({
236
338
  return this.yRedo()(props);
237
339
  }
238
340
 
239
- absolutePositionToRelativePosition(pos) {
240
- var state = this.store.getState();
241
-
242
- var _ySyncPluginKey$getSt = yProsemirror.ySyncPluginKey.getState(state),
243
- type = _ySyncPluginKey$getSt.type,
244
- binding = _ySyncPluginKey$getSt.binding;
245
-
246
- return yProsemirror.absolutePositionToRelativePosition(pos, type, binding.mapping);
247
- }
248
-
249
- relativePositionToAbsolutePosition(relPos) {
250
- var state = this.store.getState();
251
-
252
- var _ySyncPluginKey$getSt2 = yProsemirror.ySyncPluginKey.getState(state),
253
- type = _ySyncPluginKey$getSt2.type,
254
- binding = _ySyncPluginKey$getSt2.binding;
255
-
256
- return yProsemirror.relativePositionToAbsolutePosition(this.provider.doc, type, relPos, binding.mapping);
257
- }
258
-
259
341
  }, (_applyDecoratedDescriptor(_class2.prototype, "yUndo", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "yUndo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "yRedo", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "yRedo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "undoShortcut", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "undoShortcut"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "redoShortcut", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "redoShortcut"), _class2.prototype)), _class2)) || _class);
260
342
  /**
261
343
  * The default destroy provider method.
@@ -1,11 +1,101 @@
1
1
  import _applyDecoratedDescriptor from '@babel/runtime/helpers/esm/applyDecoratedDescriptor';
2
+ import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
3
+ import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
2
4
  import { defaultCursorBuilder, ySyncPluginKey, defaultDeleteFilter, ySyncPlugin, yCursorPlugin, yUndoPlugin, yUndoPluginKey, undo, redo, absolutePositionToRelativePosition, relativePositionToAbsolutePosition } from 'y-prosemirror';
3
- import { UndoManager } from 'yjs';
4
- import { extension, invariant, ErrorConstant, ExtensionPriority, command, keyBinding, NamedShortcut, PlainExtension, isFunction, isEmptyObject, nonChainable, convertCommand } from '@remirror/core';
5
+ import { UndoManager, transact } from 'yjs';
6
+ import { extension, invariant, ErrorConstant, ExtensionPriority, command, keyBinding, NamedShortcut, PlainExtension, isFunction, isEmptyObject, nonChainable, convertCommand, assert } from '@remirror/core';
5
7
  import { AnnotationExtension } from '@remirror/extension-annotation';
6
8
  import { ExtensionHistoryMessages } from '@remirror/messages';
7
9
 
8
10
  var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;
11
+
12
+ var _excluded = ["from", "to"],
13
+ _excluded2 = ["from", "to"];
14
+
15
+ class YjsAnnotationStore {
16
+ constructor(doc, pmName, mapName, getMapping) {
17
+ this.doc = doc;
18
+ this.getMapping = getMapping;
19
+ this.type = doc.getXmlFragment(pmName);
20
+ this.map = doc.getMap(mapName);
21
+ }
22
+
23
+ addAnnotation(_ref) {
24
+ var from = _ref.from,
25
+ to = _ref.to,
26
+ data = _objectWithoutProperties(_ref, _excluded);
27
+
28
+ // XXX: Why is this cast needed?
29
+ var storedData = _objectSpread(_objectSpread({}, data), {}, {
30
+ from: this.absolutePositionToRelativePosition(from),
31
+ to: this.absolutePositionToRelativePosition(to)
32
+ });
33
+
34
+ this.map.set(data.id, storedData);
35
+ }
36
+
37
+ updateAnnotation(id, updateData) {
38
+ var existing = this.map.get(id);
39
+ assert(existing);
40
+ this.map.set(id, _objectSpread(_objectSpread({}, updateData), {}, {
41
+ from: existing.from,
42
+ to: existing.to
43
+ }));
44
+ }
45
+
46
+ removeAnnotations(ids) {
47
+ transact(this.doc, () => {
48
+ ids.forEach(id => this.map.delete(id));
49
+ });
50
+ }
51
+
52
+ setAnnotations(annotations) {
53
+ transact(this.doc, () => {
54
+ this.map.clear();
55
+ annotations.forEach(annotation => this.addAnnotation(annotation));
56
+ });
57
+ }
58
+
59
+ formatAnnotations() {
60
+ var result = [];
61
+ this.map.forEach(_ref2 => {
62
+ var relFrom = _ref2.from,
63
+ relTo = _ref2.to,
64
+ data = _objectWithoutProperties(_ref2, _excluded2);
65
+
66
+ var from = this.relativePositionToAbsolutePosition(relFrom);
67
+ var to = this.relativePositionToAbsolutePosition(relTo);
68
+
69
+ if (!from || !to) {
70
+ return;
71
+ } // XXX: Why is this cast needed?
72
+
73
+
74
+ result.push(_objectSpread(_objectSpread({}, data), {}, {
75
+ from,
76
+ to
77
+ }));
78
+ });
79
+ return result;
80
+ }
81
+
82
+ absolutePositionToRelativePosition(pos) {
83
+ var mapping = this.getMapping();
84
+ return absolutePositionToRelativePosition(pos, this.type, mapping);
85
+ }
86
+
87
+ relativePositionToAbsolutePosition(relPos) {
88
+ var mapping = this.getMapping();
89
+ return relativePositionToAbsolutePosition(this.doc, this.type, relPos, mapping);
90
+ }
91
+
92
+ }
93
+ /**
94
+ * The YJS extension is the recommended extension for creating a collaborative
95
+ * editor.
96
+ */
97
+
98
+
9
99
  var YjsExtension = (_dec = extension({
10
100
  defaultOptions: {
11
101
  getProvider: () => {
@@ -25,23 +115,23 @@ var YjsExtension = (_dec = extension({
25
115
  defaultPriority: ExtensionPriority.High
26
116
  }), _dec2 = command({
27
117
  disableChaining: true,
28
- description: _ref => {
29
- var t = _ref.t;
118
+ description: _ref3 => {
119
+ var t = _ref3.t;
30
120
  return t(ExtensionHistoryMessages.UNDO_DESCRIPTION);
31
121
  },
32
- label: _ref2 => {
33
- var t = _ref2.t;
122
+ label: _ref4 => {
123
+ var t = _ref4.t;
34
124
  return t(ExtensionHistoryMessages.UNDO_LABEL);
35
125
  },
36
126
  icon: 'arrowGoBackFill'
37
127
  }), _dec3 = command({
38
128
  disableChaining: true,
39
- description: _ref3 => {
40
- var t = _ref3.t;
129
+ description: _ref5 => {
130
+ var t = _ref5.t;
41
131
  return t(ExtensionHistoryMessages.REDO_DESCRIPTION);
42
132
  },
43
- label: _ref4 => {
44
- var t = _ref4.t;
133
+ label: _ref6 => {
134
+ var t = _ref6.t;
45
135
  return t(ExtensionHistoryMessages.REDO_LABEL);
46
136
  },
47
137
  icon: 'arrowGoForwardFill'
@@ -66,12 +156,24 @@ var YjsExtension = (_dec = extension({
66
156
  return (_this$_provider = this._provider) !== null && _this$_provider !== void 0 ? _this$_provider : this._provider = getLazyValue(getProvider);
67
157
  }
68
158
 
159
+ getBinding() {
160
+ var state = this.store.getState();
161
+
162
+ var _ySyncPluginKey$getSt = ySyncPluginKey.getState(state),
163
+ binding = _ySyncPluginKey$getSt.binding;
164
+
165
+ return binding;
166
+ }
167
+
69
168
  onView() {
70
169
  try {
170
+ var annotationStore = new YjsAnnotationStore(this.provider.doc, 'prosemirror', 'annotations', () => {
171
+ var _this$getBinding;
172
+
173
+ return (_this$getBinding = this.getBinding()) === null || _this$getBinding === void 0 ? void 0 : _this$getBinding.mapping;
174
+ });
71
175
  this.store.manager.getExtension(AnnotationExtension).setOptions({
72
- getMap: () => this.provider.doc.getMap('annotations'),
73
- transformPosition: this.absolutePositionToRelativePosition.bind(this),
74
- transformPositionBeforeRender: this.relativePositionToAbsolutePosition.bind(this)
176
+ getStore: () => annotationStore
75
177
  });
76
178
 
77
179
  var handler = (_update, _origin, _doc, yjsTr) => {
@@ -235,26 +337,6 @@ var YjsExtension = (_dec = extension({
235
337
  return this.yRedo()(props);
236
338
  }
237
339
 
238
- absolutePositionToRelativePosition(pos) {
239
- var state = this.store.getState();
240
-
241
- var _ySyncPluginKey$getSt = ySyncPluginKey.getState(state),
242
- type = _ySyncPluginKey$getSt.type,
243
- binding = _ySyncPluginKey$getSt.binding;
244
-
245
- return absolutePositionToRelativePosition(pos, type, binding.mapping);
246
- }
247
-
248
- relativePositionToAbsolutePosition(relPos) {
249
- var state = this.store.getState();
250
-
251
- var _ySyncPluginKey$getSt2 = ySyncPluginKey.getState(state),
252
- type = _ySyncPluginKey$getSt2.type,
253
- binding = _ySyncPluginKey$getSt2.binding;
254
-
255
- return relativePositionToAbsolutePosition(this.provider.doc, type, relPos, binding.mapping);
256
- }
257
-
258
340
  }, (_applyDecoratedDescriptor(_class2.prototype, "yUndo", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "yUndo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "yRedo", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "yRedo"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "undoShortcut", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "undoShortcut"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "redoShortcut", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "redoShortcut"), _class2.prototype)), _class2)) || _class);
259
341
  /**
260
342
  * The default destroy provider method.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remirror/extension-yjs",
3
- "version": "1.0.20",
3
+ "version": "1.0.21",
4
4
  "description": "Realtime collaboration with yjs",
5
5
  "keywords": [
6
6
  "remirror",
@@ -45,7 +45,7 @@
45
45
  "dependencies": {
46
46
  "@babel/runtime": "^7.13.10",
47
47
  "@remirror/core": "^1.3.3",
48
- "@remirror/extension-annotation": "^1.1.11",
48
+ "@remirror/extension-annotation": "^1.1.12",
49
49
  "@remirror/messages": "^1.0.6",
50
50
  "y-prosemirror": "^1.0.14",
51
51
  "y-protocols": "^1.0.5"