@difizen/libro-codemirror 0.0.0-snapshot-20241017072317

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/auto-complete/closebrackets.d.ts +12 -0
  4. package/es/auto-complete/closebrackets.d.ts.map +1 -0
  5. package/es/auto-complete/closebrackets.js +409 -0
  6. package/es/auto-complete/completion.d.ts +57 -0
  7. package/es/auto-complete/completion.d.ts.map +1 -0
  8. package/es/auto-complete/completion.js +267 -0
  9. package/es/auto-complete/config.d.ts +22 -0
  10. package/es/auto-complete/config.d.ts.map +1 -0
  11. package/es/auto-complete/config.js +44 -0
  12. package/es/auto-complete/filter.d.ts +13 -0
  13. package/es/auto-complete/filter.d.ts.map +1 -0
  14. package/es/auto-complete/filter.js +190 -0
  15. package/es/auto-complete/index.d.ts +17 -0
  16. package/es/auto-complete/index.d.ts.map +1 -0
  17. package/es/auto-complete/index.js +107 -0
  18. package/es/auto-complete/snippet.d.ts +14 -0
  19. package/es/auto-complete/snippet.d.ts.map +1 -0
  20. package/es/auto-complete/snippet.js +445 -0
  21. package/es/auto-complete/state.d.ts +63 -0
  22. package/es/auto-complete/state.d.ts.map +1 -0
  23. package/es/auto-complete/state.js +448 -0
  24. package/es/auto-complete/theme.d.ts +6 -0
  25. package/es/auto-complete/theme.d.ts.map +1 -0
  26. package/es/auto-complete/theme.js +150 -0
  27. package/es/auto-complete/tooltip.d.ts +5 -0
  28. package/es/auto-complete/tooltip.d.ts.map +1 -0
  29. package/es/auto-complete/tooltip.js +365 -0
  30. package/es/auto-complete/view.d.ts +38 -0
  31. package/es/auto-complete/view.d.ts.map +1 -0
  32. package/es/auto-complete/view.js +368 -0
  33. package/es/auto-complete/word.d.ts +3 -0
  34. package/es/auto-complete/word.d.ts.map +1 -0
  35. package/es/auto-complete/word.js +119 -0
  36. package/es/completion.d.ts +6 -0
  37. package/es/completion.d.ts.map +1 -0
  38. package/es/completion.js +84 -0
  39. package/es/config.d.ts +189 -0
  40. package/es/config.d.ts.map +1 -0
  41. package/es/config.js +482 -0
  42. package/es/editor-contribution.d.ts +9 -0
  43. package/es/editor-contribution.d.ts.map +1 -0
  44. package/es/editor-contribution.js +35 -0
  45. package/es/editor.d.ts +397 -0
  46. package/es/editor.d.ts.map +1 -0
  47. package/es/editor.js +1338 -0
  48. package/es/factory.d.ts +4 -0
  49. package/es/factory.d.ts.map +1 -0
  50. package/es/factory.js +24 -0
  51. package/es/hyperlink.d.ts +15 -0
  52. package/es/hyperlink.d.ts.map +1 -0
  53. package/es/hyperlink.js +120 -0
  54. package/es/indent.d.ts +8 -0
  55. package/es/indent.d.ts.map +1 -0
  56. package/es/indent.js +58 -0
  57. package/es/indentation-markers/config.d.ts +17 -0
  58. package/es/indentation-markers/config.d.ts.map +1 -0
  59. package/es/indentation-markers/config.js +10 -0
  60. package/es/indentation-markers/index.d.ts +3 -0
  61. package/es/indentation-markers/index.d.ts.map +1 -0
  62. package/es/indentation-markers/index.js +160 -0
  63. package/es/indentation-markers/map.d.ts +77 -0
  64. package/es/indentation-markers/map.d.ts.map +1 -0
  65. package/es/indentation-markers/map.js +265 -0
  66. package/es/indentation-markers/utils.d.ts +27 -0
  67. package/es/indentation-markers/utils.d.ts.map +1 -0
  68. package/es/indentation-markers/utils.js +91 -0
  69. package/es/index.d.ts +13 -0
  70. package/es/index.d.ts.map +1 -0
  71. package/es/index.js +12 -0
  72. package/es/libro-icon.d.ts +3 -0
  73. package/es/libro-icon.d.ts.map +1 -0
  74. package/es/libro-icon.js +2 -0
  75. package/es/lsp/completion.d.ts +5 -0
  76. package/es/lsp/completion.d.ts.map +1 -0
  77. package/es/lsp/completion.js +245 -0
  78. package/es/lsp/format.d.ts +7 -0
  79. package/es/lsp/format.d.ts.map +1 -0
  80. package/es/lsp/format.js +193 -0
  81. package/es/lsp/index.d.ts +7 -0
  82. package/es/lsp/index.d.ts.map +1 -0
  83. package/es/lsp/index.js +6 -0
  84. package/es/lsp/lint.d.ts +3 -0
  85. package/es/lsp/lint.d.ts.map +1 -0
  86. package/es/lsp/lint.js +113 -0
  87. package/es/lsp/protocol.d.ts +7 -0
  88. package/es/lsp/protocol.d.ts.map +1 -0
  89. package/es/lsp/protocol.js +1 -0
  90. package/es/lsp/tooltip.d.ts +3 -0
  91. package/es/lsp/tooltip.d.ts.map +1 -0
  92. package/es/lsp/tooltip.js +113 -0
  93. package/es/lsp/util.d.ts +15 -0
  94. package/es/lsp/util.d.ts.map +1 -0
  95. package/es/lsp/util.js +57 -0
  96. package/es/mimetype.d.ts +22 -0
  97. package/es/mimetype.d.ts.map +1 -0
  98. package/es/mimetype.js +59 -0
  99. package/es/mode.d.ts +86 -0
  100. package/es/mode.d.ts.map +1 -0
  101. package/es/mode.js +280 -0
  102. package/es/module.d.ts +3 -0
  103. package/es/module.d.ts.map +1 -0
  104. package/es/module.js +4 -0
  105. package/es/monitor.d.ts +32 -0
  106. package/es/monitor.d.ts.map +1 -0
  107. package/es/monitor.js +129 -0
  108. package/es/python-lang.d.ts +3 -0
  109. package/es/python-lang.d.ts.map +1 -0
  110. package/es/python-lang.js +7 -0
  111. package/es/style/base.css +129 -0
  112. package/es/style/theme.css +12 -0
  113. package/es/style/variables.css +405 -0
  114. package/es/theme.d.ts +35 -0
  115. package/es/theme.d.ts.map +1 -0
  116. package/es/theme.js +223 -0
  117. package/es/tooltip.d.ts +10 -0
  118. package/es/tooltip.d.ts.map +1 -0
  119. package/es/tooltip.js +168 -0
  120. package/package.json +74 -0
  121. package/src/auto-complete/README.md +71 -0
  122. package/src/auto-complete/closebrackets.ts +423 -0
  123. package/src/auto-complete/completion.ts +345 -0
  124. package/src/auto-complete/config.ts +101 -0
  125. package/src/auto-complete/filter.ts +214 -0
  126. package/src/auto-complete/index.ts +112 -0
  127. package/src/auto-complete/snippet.ts +392 -0
  128. package/src/auto-complete/state.ts +465 -0
  129. package/src/auto-complete/theme.ts +127 -0
  130. package/src/auto-complete/tooltip.ts +386 -0
  131. package/src/auto-complete/view.ts +339 -0
  132. package/src/auto-complete/word.ts +118 -0
  133. package/src/completion.ts +61 -0
  134. package/src/config.ts +701 -0
  135. package/src/editor-contribution.ts +22 -0
  136. package/src/editor.ts +1287 -0
  137. package/src/factory.ts +31 -0
  138. package/src/hyperlink.ts +95 -0
  139. package/src/indent.ts +69 -0
  140. package/src/indentation-markers/config.ts +31 -0
  141. package/src/indentation-markers/index.ts +192 -0
  142. package/src/indentation-markers/map.ts +273 -0
  143. package/src/indentation-markers/utils.ts +84 -0
  144. package/src/index.spec.ts +12 -0
  145. package/src/index.ts +14 -0
  146. package/src/libro-icon.tsx +4 -0
  147. package/src/lsp/completion.ts +175 -0
  148. package/src/lsp/format.ts +144 -0
  149. package/src/lsp/index.ts +6 -0
  150. package/src/lsp/lint.ts +125 -0
  151. package/src/lsp/protocol.ts +8 -0
  152. package/src/lsp/tooltip.ts +76 -0
  153. package/src/lsp/util.ts +69 -0
  154. package/src/mimetype.ts +49 -0
  155. package/src/mode.ts +265 -0
  156. package/src/module.ts +8 -0
  157. package/src/monitor.ts +105 -0
  158. package/src/python-lang.ts +7 -0
  159. package/src/style/base.css +129 -0
  160. package/src/style/theme.css +12 -0
  161. package/src/style/variables.css +405 -0
  162. package/src/theme.ts +231 -0
  163. package/src/tooltip.ts +143 -0
package/es/editor.js ADDED
@@ -0,0 +1,1338 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
3
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
5
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
6
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
7
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
9
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
11
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
12
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
16
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
+ import { insertNewlineAndIndent, redo as _redo, undo as _undo } from '@codemirror/commands';
18
+ import { ensureSyntaxTree } from '@codemirror/language';
19
+ import { EditorSelection, EditorState, Prec, StateEffect, StateField } from '@codemirror/state';
20
+ import { Decoration, EditorView } from '@codemirror/view';
21
+ import { defaultConfig, defaultSelectionStyle } from '@difizen/libro-code-editor';
22
+ import { findFirstArrayIndex, MIME, removeAllWhereFromArray } from '@difizen/libro-common';
23
+ import { Deferred, Disposable, Emitter } from '@difizen/mana-app';
24
+ import { getOrigin, watch } from '@difizen/mana-app';
25
+ import { v4 } from 'uuid';
26
+ import { EditorConfiguration } from "./config.js";
27
+ import { stateFactory } from "./factory.js";
28
+ import { ensure } from "./mode.js";
29
+ import { monitorPlugin } from "./monitor.js";
30
+
31
+ /**
32
+ * The class name added to CodeMirrorWidget instances.
33
+ */
34
+ var EDITOR_CLASS = 'jp-CodeMirrorEditor';
35
+
36
+ /**
37
+ * The class name added to read only cell editor widgets.
38
+ */
39
+ var READ_ONLY_CLASS = 'jp-mod-readOnly';
40
+
41
+ /**
42
+ * The class name for the hover box for collaborator cursors.
43
+ */
44
+ // const COLLABORATOR_CURSOR_CLASS = 'jp-CollaboratorCursor';
45
+
46
+ /**
47
+ * The class name for the hover box for collaborator cursors.
48
+ */
49
+ // const COLLABORATOR_HOVER_CLASS = 'jp-CollaboratorCursor-hover';
50
+
51
+ /**
52
+ * The key code for the up arrow key.
53
+ */
54
+ var UP_ARROW = 38;
55
+
56
+ /**
57
+ * The key code for the down arrow key.
58
+ */
59
+ var DOWN_ARROW = 40;
60
+
61
+ /**
62
+ * The time that a collaborator name hover persists.
63
+ */
64
+ // const HOVER_TIMEOUT = 1000;
65
+ /**
66
+ * The default configuration options for an editor.
67
+ */
68
+
69
+ // interface IYCodeMirrorBinding {
70
+ // text: Y.Text;
71
+ // awareness: Awareness | null;
72
+ // undoManager: Y.UndoManager | null;
73
+ // }
74
+
75
+ export var codeMirrorDefaultConfig = _objectSpread(_objectSpread({}, defaultConfig), {}, {
76
+ mode: 'null',
77
+ mimetype: MIME.python,
78
+ theme: {
79
+ light: 'jupyter',
80
+ dark: 'jupyter',
81
+ hc: 'jupyter'
82
+ },
83
+ smartIndent: true,
84
+ electricChars: true,
85
+ keyMap: 'default',
86
+ extraKeys: null,
87
+ gutters: [],
88
+ fixedGutter: true,
89
+ showCursorWhenSelecting: false,
90
+ coverGutterNextToScrollbar: false,
91
+ dragDrop: true,
92
+ lineSeparator: null,
93
+ scrollbarStyle: 'native',
94
+ lineWiseCopyCut: true,
95
+ scrollPastEnd: false,
96
+ styleActiveLine: false,
97
+ styleSelectedText: true,
98
+ selectionPointer: false,
99
+ handlePaste: true,
100
+ scrollBarHeight: 8,
101
+ //
102
+ highlightActiveLineGutter: false,
103
+ highlightSpecialChars: true,
104
+ history: true,
105
+ drawSelection: true,
106
+ dropCursor: true,
107
+ allowMultipleSelections: true,
108
+ autocompletion: true,
109
+ rectangularSelection: true,
110
+ crosshairCursor: true,
111
+ highlightSelectionMatches: true,
112
+ foldGutter: true,
113
+ syntaxHighlighting: true,
114
+ jupyterKernelCompletion: true,
115
+ indentationMarkers: true,
116
+ hyperLink: true,
117
+ jupyterKernelTooltip: true,
118
+ tabEditorFunction: true,
119
+ lspCompletion: true,
120
+ lspTooltip: true,
121
+ lspLint: true,
122
+ placeholder: ''
123
+ });
124
+ export var CodeMirrorEditor = /*#__PURE__*/function () {
125
+ // highlight
126
+
127
+ /**
128
+ * Construct a CodeMirror editor.
129
+ */
130
+ function CodeMirrorEditor(options) {
131
+ var _this = this,
132
+ _options$state;
133
+ _classCallCheck(this, CodeMirrorEditor);
134
+ this.editorReadyDeferred = new Deferred();
135
+ this.editorReady = this.editorReadyDeferred.promise;
136
+ /**
137
+ * A signal emitted when either the top or bottom edge is requested.
138
+ */
139
+ this.edgeRequestedEmitter = new Emitter();
140
+ this.edgeRequested = this.edgeRequestedEmitter.event;
141
+ this.modalChangeEmitter = new Emitter();
142
+ /**
143
+ * Dispose of the resources held by the widget.
144
+ */
145
+ this.dispose = function () {
146
+ if (_this.disposed) {
147
+ return;
148
+ }
149
+ _this._isDisposed = true;
150
+ _this.host.removeEventListener('focus', _this, true);
151
+ _this.host.removeEventListener('blur', _this, true);
152
+ _this.host.removeEventListener('scroll', _this, true);
153
+ _this._keydownHandlers.length = 0;
154
+ _this.editor.destroy();
155
+ };
156
+ this.handleTooltipChange = function (val) {
157
+ _this.modalChangeEmitter.fire(val);
158
+ };
159
+ this._selectionMarkers = {};
160
+ this._keydownHandlers = new Array();
161
+ this._uuid = '';
162
+ this._isDisposed = false;
163
+ this._lastChange = null;
164
+ this._editorConfig = new EditorConfiguration(options);
165
+ var host = this.host = options.host;
166
+ host.classList.add(EDITOR_CLASS);
167
+ host.classList.add('jp-Editor');
168
+ host.addEventListener('focus', this, true);
169
+ host.addEventListener('blur', this, true);
170
+ host.addEventListener('scroll', this, true);
171
+ this._uuid = options.uuid || v4();
172
+ this.editorState = (_options$state = options.state) !== null && _options$state !== void 0 ? _options$state : stateFactory({
173
+ uuid: options.uuid,
174
+ model: options.model
175
+ });
176
+
177
+ // State and effects for handling the selection marks
178
+ this._addMark = StateEffect.define();
179
+ this._removeMark = StateEffect.define();
180
+ this._markField = StateField.define({
181
+ create: function create() {
182
+ return Decoration.none;
183
+ },
184
+ update: function update(marks, transaction) {
185
+ var _marks = marks.map(transaction.changes);
186
+ var _iterator = _createForOfIteratorHelper(transaction.effects),
187
+ _step;
188
+ try {
189
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
190
+ var ef = _step.value;
191
+ if (ef.is(_this._addMark)) {
192
+ var e = ef;
193
+ var decorations = _this._buildMarkDecoration(e.value.uuid, e.value.selections);
194
+ _marks = _marks.update({
195
+ add: decorations
196
+ });
197
+ _this._selectionMarkers[e.value.uuid] = decorations;
198
+ } else if (ef.is(_this._removeMark)) {
199
+ var _e = ef;
200
+ var _iterator2 = _createForOfIteratorHelper(ef.value.decorations),
201
+ _step2;
202
+ try {
203
+ var _loop = function _loop() {
204
+ var rd = _step2.value;
205
+ _marks = _marks.update({
206
+ filter: function filter(from, to, value) {
207
+ return !(from === rd.from && to === rd.to && value === rd.value);
208
+ }
209
+ });
210
+ };
211
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
212
+ _loop();
213
+ }
214
+ } catch (err) {
215
+ _iterator2.e(err);
216
+ } finally {
217
+ _iterator2.f();
218
+ }
219
+ delete _this._selectionMarkers[_e.value.uuid];
220
+ }
221
+ }
222
+ } catch (err) {
223
+ _iterator.e(err);
224
+ } finally {
225
+ _iterator.f();
226
+ }
227
+ return _marks;
228
+ },
229
+ provide: function provide(f) {
230
+ return EditorView.decorations.from(f);
231
+ }
232
+ });
233
+
234
+ // handle highlight
235
+ this.highlightEffect = StateEffect.define({
236
+ map: function map(value, mapping) {
237
+ return {
238
+ matches: value.matches.map(function (v) {
239
+ return {
240
+ text: v.text,
241
+ position: mapping.mapPos(v.position)
242
+ };
243
+ }),
244
+ currentIndex: value.currentIndex
245
+ };
246
+ }
247
+ });
248
+ this.highlightMark = Decoration.mark({
249
+ class: 'cm-searchMatch'
250
+ });
251
+ this.selectedMatchMark = Decoration.mark({
252
+ class: 'cm-searchMatch cm-searchMatch-selected libro-selectedtext'
253
+ });
254
+ this.highlightField = StateField.define({
255
+ create: function create() {
256
+ return Decoration.none;
257
+ },
258
+ update: function update(highlights, transaction) {
259
+ // eslint-disable-next-line no-param-reassign
260
+ highlights = highlights.map(transaction.changes);
261
+ var _iterator3 = _createForOfIteratorHelper(transaction.effects),
262
+ _step3;
263
+ try {
264
+ var _loop2 = function _loop2() {
265
+ var ef = _step3.value;
266
+ if (ef.is(_this.highlightEffect)) {
267
+ var e = ef;
268
+ if (e.value.matches.length) {
269
+ // eslint-disable-next-line no-param-reassign
270
+ highlights = highlights.update({
271
+ add: e.value.matches.map(function (m, index) {
272
+ if (index === e.value.currentIndex) {
273
+ return _this.selectedMatchMark.range(m.position, m.position + m.text.length);
274
+ }
275
+ return _this.highlightMark.range(m.position, m.position + m.text.length);
276
+ }),
277
+ filter: function filter(from, to) {
278
+ return !e.value.matches.some(function (m) {
279
+ return m.position >= from && m.position + m.text.length <= to;
280
+ }) || from === to;
281
+ }
282
+ });
283
+ } else {
284
+ // eslint-disable-next-line no-param-reassign
285
+ highlights = Decoration.none;
286
+ }
287
+ }
288
+ };
289
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
290
+ _loop2();
291
+ }
292
+ } catch (err) {
293
+ _iterator3.e(err);
294
+ } finally {
295
+ _iterator3.f();
296
+ }
297
+ return highlights;
298
+ },
299
+ provide: function provide(f) {
300
+ return EditorView.decorations.from(f);
301
+ }
302
+ });
303
+
304
+ // Handle selection style.
305
+ var style = options.selectionStyle || {};
306
+ this._selectionStyle = _objectSpread(_objectSpread({}, defaultSelectionStyle), style);
307
+ var model = this._model = options.model;
308
+ var config = options.config || {};
309
+ var fullConfig = this._config = _objectSpread(_objectSpread(_objectSpread({}, codeMirrorDefaultConfig), config), {}, {
310
+ mimetype: options.model.mimeType
311
+ });
312
+
313
+ // this._initializeEditorBinding();
314
+
315
+ // Extension for handling DOM events
316
+ var domEventHandlers = EditorView.domEventHandlers({
317
+ keydown: function keydown(event) {
318
+ var index = findFirstArrayIndex(_this._keydownHandlers, function (handler) {
319
+ if (handler(_this, event) === true) {
320
+ event.preventDefault();
321
+ return true;
322
+ }
323
+ return false;
324
+ });
325
+ if (index === -1) {
326
+ return _this.onKeydown(event);
327
+ }
328
+ return false;
329
+ }
330
+ });
331
+ var updateListener = EditorView.updateListener.of(function (update) {
332
+ _this._onDocChanged(update);
333
+ });
334
+ this._editor = createEditor(host, fullConfig, this.model.value, this._editorConfig, [this._markField, Prec.high(domEventHandlers), updateListener, monitorPlugin({
335
+ onTooltipChange: this.handleTooltipChange
336
+ })]);
337
+ this.editorReadyDeferred.resolve();
338
+
339
+ // every time the model is switched, we need to re-initialize the editor binding
340
+ // this.model.sharedModelSwitched.connect(this._initializeEditorBinding, this);
341
+
342
+ this._onMimeTypeChanged();
343
+ this._onCursorActivity();
344
+ // this._poll = new Poll({
345
+ // factory: async () => {
346
+ // this._checkSync();
347
+ // },
348
+ // frequency: { interval: 3000, backoff: false },
349
+ // standby: () => {
350
+ // // If changed, only stand by when hidden, otherwise always stand by.
351
+ // return this._lastChange ? 'when-hidden' : true;
352
+ // },
353
+ // });
354
+
355
+ watch(model, 'mimeType', this._onMimeTypeChanged);
356
+ }
357
+ _createClass(CodeMirrorEditor, [{
358
+ key: "getState",
359
+ value: function getState() {
360
+ return _objectSpread(_objectSpread({}, this.editorState), {}, {
361
+ cursorPosition: this.getCursorPosition(),
362
+ selections: this.getSelections()
363
+ });
364
+ }
365
+
366
+ /**
367
+ * Initialize the editor binding.
368
+ */
369
+ // protected _initializeEditorBinding(): void {
370
+ // const sharedModel = this.model.sharedModel as models.IYText;
371
+ // this._yeditorBinding = {
372
+ // text: sharedModel.ysource,
373
+ // awareness: sharedModel.awareness,
374
+ // undoManager: sharedModel.undoManager,
375
+ // };
376
+ // }
377
+ }, {
378
+ key: "uuid",
379
+ get:
380
+ /**
381
+ * The DOM node that hosts the editor.
382
+ */
383
+
384
+ /**
385
+ * The uuid of this editor;
386
+ */
387
+ function get() {
388
+ return this._uuid;
389
+ },
390
+ set: function set(value) {
391
+ this._uuid = value;
392
+ }
393
+ }, {
394
+ key: "onModalChange",
395
+ get: function get() {
396
+ return this.modalChangeEmitter.event;
397
+ }
398
+
399
+ /**
400
+ * The selection style of this editor.
401
+ */
402
+ }, {
403
+ key: "selectionStyle",
404
+ get: function get() {
405
+ return this._selectionStyle;
406
+ },
407
+ set: function set(value) {
408
+ this._selectionStyle = value;
409
+ }
410
+
411
+ /**
412
+ * Get the codemirror editor wrapped by the editor.
413
+ */
414
+ }, {
415
+ key: "editor",
416
+ get: function get() {
417
+ return this._editor;
418
+ }
419
+
420
+ /**
421
+ * Get the codemirror doc wrapped by the widget.
422
+ */
423
+ }, {
424
+ key: "doc",
425
+ get: function get() {
426
+ return this._editor.state.doc;
427
+ }
428
+
429
+ /**
430
+ * Get the number of lines in the editor.
431
+ */
432
+ }, {
433
+ key: "lineCount",
434
+ get: function get() {
435
+ return this.doc.lines;
436
+ }
437
+
438
+ /**
439
+ * Returns a model for this editor.
440
+ */
441
+ }, {
442
+ key: "model",
443
+ get: function get() {
444
+ return this._model;
445
+ }
446
+
447
+ /**
448
+ * The height of a line in the editor in pixels.
449
+ */
450
+ }, {
451
+ key: "lineHeight",
452
+ get: function get() {
453
+ return this._editor.defaultLineHeight;
454
+ }
455
+
456
+ /**
457
+ * The widget of a character in the editor in pixels.
458
+ */
459
+ }, {
460
+ key: "charWidth",
461
+ get: function get() {
462
+ return this._editor.defaultCharacterWidth;
463
+ }
464
+
465
+ /**
466
+ * Tests whether the editor is disposed.
467
+ */
468
+ }, {
469
+ key: "disposed",
470
+ get: function get() {
471
+ return this._isDisposed;
472
+ }
473
+ }, {
474
+ key: "getOption",
475
+ value:
476
+ /**
477
+ * Get a config option for the editor.
478
+ */
479
+ function getOption(option) {
480
+ return this._config[option];
481
+ }
482
+
483
+ /**
484
+ * Set a config option for the editor.
485
+ */
486
+ }, {
487
+ key: "setOption",
488
+ value: function setOption(option, value) {
489
+ // Don't bother setting the option if it is already the same.
490
+ if (this._config[option] !== value) {
491
+ this._config[option] = value;
492
+ if (!this.disposed) {
493
+ this._editorConfig.reconfigureExtension(this._editor, option, value);
494
+ }
495
+ }
496
+ if (option === 'readOnly') {
497
+ if (value === true) {
498
+ getOrigin(this._editor).dom.classList.add(READ_ONLY_CLASS);
499
+ } else {
500
+ getOrigin(this._editor).dom.classList.remove(READ_ONLY_CLASS);
501
+ }
502
+ }
503
+ }
504
+ /**
505
+ * Set config options for the editor.
506
+ *
507
+ * This method is preferred when setting several options. The
508
+ * options are set within an operation, which only performs
509
+ * the costly update at the end, and not after every option
510
+ * is set.
511
+ */
512
+ }, {
513
+ key: "setOptions",
514
+ value: function setOptions(options) {
515
+ this._config = _objectSpread(_objectSpread({}, this._config), options);
516
+ this._editorConfig.reconfigureExtensions(this._editor, options);
517
+ }
518
+ }, {
519
+ key: "injectExtension",
520
+ value: function injectExtension(ext) {
521
+ this._editorConfig.injectExtension(this._editor, ext);
522
+ }
523
+
524
+ /**
525
+ * Returns the content for the given line number.
526
+ */
527
+ }, {
528
+ key: "getLine",
529
+ value: function getLine(line) {
530
+ // TODO: CM6 remove +1 when CM6 first line number has propagated
531
+ return line <= this.doc.lines ? this.doc.line(line).text : undefined;
532
+ }
533
+
534
+ /**
535
+ * Find an offset for the given position.
536
+ */
537
+ }, {
538
+ key: "getOffsetAt",
539
+ value: function getOffsetAt(position) {
540
+ // TODO: CM6 remove +1 when CM6 first line number has propagated
541
+ return this.doc.line(position.line).from + position.column - 1;
542
+ }
543
+
544
+ /**
545
+ * Find a position for the given offset.
546
+ */
547
+ }, {
548
+ key: "getPositionAt",
549
+ value: function getPositionAt(offset) {
550
+ // TODO: CM6 remove -1 when CM6 first line number has propagated
551
+ var line = this.doc.lineAt(offset);
552
+ return {
553
+ line: line.number - 1,
554
+ column: offset - line.from
555
+ };
556
+ }
557
+
558
+ /**
559
+ * Undo one edit (if any undo events are stored).
560
+ */
561
+ }, {
562
+ key: "undo",
563
+ value: function undo() {
564
+ _undo({
565
+ state: getOrigin(this.state),
566
+ dispatch: this.editor.dispatch
567
+ });
568
+ }
569
+
570
+ /**
571
+ * Redo one undone edit.
572
+ */
573
+ }, {
574
+ key: "redo",
575
+ value: function redo() {
576
+ _redo({
577
+ state: getOrigin(this.state),
578
+ dispatch: this.editor.dispatch
579
+ });
580
+ }
581
+
582
+ /**
583
+ * Clear the undo history.
584
+ */
585
+ }, {
586
+ key: "clearHistory",
587
+ value: function clearHistory() {
588
+ // this._yeditorBinding?.undoManager?.clear();
589
+ }
590
+
591
+ /**
592
+ * Brings browser focus to this editor text.
593
+ */
594
+ }, {
595
+ key: "focus",
596
+ value: function focus() {
597
+ getOrigin(this._editor).focus();
598
+ }
599
+
600
+ /**
601
+ * Test whether the editor has keyboard focus.
602
+ */
603
+ }, {
604
+ key: "hasFocus",
605
+ value: function hasFocus() {
606
+ return getOrigin(this._editor).hasFocus;
607
+ }
608
+
609
+ /**
610
+ * Explicitly blur the editor.
611
+ */
612
+ }, {
613
+ key: "blur",
614
+ value: function blur() {
615
+ this._editor.contentDOM.blur();
616
+ }
617
+
618
+ /**
619
+ * Refresh the editor if it is focused;
620
+ * otherwise postpone refreshing till focusing.
621
+ */
622
+ }, {
623
+ key: "resizeToFit",
624
+ value: function resizeToFit() {
625
+ this._clearHover();
626
+ }
627
+ }, {
628
+ key: "state",
629
+ get: function get() {
630
+ return this._editor.state;
631
+ }
632
+ }, {
633
+ key: "firstLine",
634
+ value: function firstLine() {
635
+ // TODO: return 1 when CM6 first line number has propagated
636
+ return 0;
637
+ }
638
+ }, {
639
+ key: "lastLine",
640
+ value: function lastLine() {
641
+ return this.doc.lines - 1;
642
+ }
643
+ }, {
644
+ key: "cursorCoords",
645
+ value: function cursorCoords(where
646
+ // mode?: 'window' | 'page' | 'local',
647
+ ) {
648
+ var selection = this.state.selection.main;
649
+ var pos = where ? selection.from : selection.to;
650
+ var rect = this.editor.coordsAtPos(pos);
651
+ return rect;
652
+ }
653
+ }, {
654
+ key: "getRange",
655
+ value: function getRange(from, to
656
+ // separator?: string,
657
+ ) {
658
+ var fromOffset = this.getOffsetAt(this._toPosition(from));
659
+ var toOffset = this.getOffsetAt(this._toPosition(to));
660
+ return this.state.sliceDoc(fromOffset, toOffset);
661
+ }
662
+ }, {
663
+ key: "getSelectionValue",
664
+ value: function getSelectionValue(range) {
665
+ var fromOffset = range ? this.getOffsetAt(range.start) : this.editor.state.selection.main.from;
666
+ var toOffset = range ? this.getOffsetAt(range.end) : this.editor.state.selection.main.to;
667
+ return this.state.sliceDoc(fromOffset, toOffset);
668
+ }
669
+
670
+ /**
671
+ * Add a keydown handler to the editor.
672
+ *
673
+ * @param handler - A keydown handler.
674
+ *
675
+ * @returns A disposable that can be used to remove the handler.
676
+ */
677
+ }, {
678
+ key: "addKeydownHandler",
679
+ value: function addKeydownHandler(handler) {
680
+ var _this2 = this;
681
+ this._keydownHandlers.push(handler);
682
+ return Disposable.create(function () {
683
+ removeAllWhereFromArray(_this2._keydownHandlers, function (val) {
684
+ return val === handler;
685
+ });
686
+ });
687
+ }
688
+
689
+ /**
690
+ * Reveal the given position in the editor.
691
+ */
692
+ }, {
693
+ key: "revealPosition",
694
+ value: function revealPosition(position) {
695
+ var offset = this.getOffsetAt(position);
696
+ this._editor.dispatch({
697
+ effects: EditorView.scrollIntoView(offset)
698
+ });
699
+ }
700
+
701
+ /**
702
+ * Reveal the given selection in the editor.
703
+ */
704
+ }, {
705
+ key: "revealSelection",
706
+ value: function revealSelection(selection) {
707
+ var start = this.getOffsetAt(selection.start);
708
+ var end = this.getOffsetAt(selection.end);
709
+ this._editor.dispatch({
710
+ effects: EditorView.scrollIntoView(EditorSelection.range(start, end))
711
+ });
712
+ }
713
+
714
+ /**
715
+ * Get the window coordinates given a cursor position.
716
+ */
717
+ }, {
718
+ key: "getCoordinateForPosition",
719
+ value: function getCoordinateForPosition(position) {
720
+ var offset = this.getOffsetAt(position);
721
+ var rect = this.editor.coordsAtPos(offset);
722
+ return rect;
723
+ }
724
+ /**
725
+ * Get the cursor position given window coordinates.
726
+ *
727
+ * @param coordinate - The desired coordinate.
728
+ *
729
+ * @returns The position of the coordinates, or null if not
730
+ * contained in the editor.
731
+ */
732
+ }, {
733
+ key: "getPositionForCoordinate",
734
+ value: function getPositionForCoordinate(coordinate) {
735
+ var offset = this.editor.posAtCoords({
736
+ x: coordinate.left,
737
+ y: coordinate.top
738
+ });
739
+ return this.getPositionAt(offset) || null;
740
+ }
741
+
742
+ /**
743
+ * Returns the primary position of the cursor, never `null`.
744
+ */
745
+ }, {
746
+ key: "getCursorPosition",
747
+ value: function getCursorPosition() {
748
+ var offset = this.state.selection.main.head;
749
+ return this.getPositionAt(offset);
750
+ }
751
+
752
+ /**
753
+ * Set the primary position of the cursor.
754
+ *
755
+ * #### Notes
756
+ * This will remove any secondary cursors.
757
+ */
758
+ }, {
759
+ key: "setCursorPosition",
760
+ value: function setCursorPosition(position
761
+ // options?: { bias?: number; origin?: string; scroll?: boolean },
762
+ ) {
763
+ var offset = this.getOffsetAt(position);
764
+ this.editor.dispatch({
765
+ selection: {
766
+ anchor: offset
767
+ },
768
+ scrollIntoView: true
769
+ });
770
+ // If the editor does not have focus, this cursor change
771
+ // will get screened out in _onCursorsChanged(). Make an
772
+ // exception for this method.
773
+ if (!this.editor.hasFocus) {
774
+ this.model.selections = this.getSelections();
775
+ }
776
+ }
777
+
778
+ /**
779
+ * Returns the primary selection, never `null`.
780
+ */
781
+ }, {
782
+ key: "getSelection",
783
+ value: function getSelection() {
784
+ return this.getSelections()[0];
785
+ }
786
+
787
+ /**
788
+ * Set the primary selection. This will remove any secondary cursors.
789
+ */
790
+ }, {
791
+ key: "setSelection",
792
+ value: function setSelection(selection) {
793
+ this.setSelections([selection]);
794
+ }
795
+
796
+ /**
797
+ * Gets the selections for all the cursors, never `null` or empty.
798
+ */
799
+ }, {
800
+ key: "getSelections",
801
+ value: function getSelections() {
802
+ var _this3 = this;
803
+ var selections = this.state.selection.ranges; //= [{anchor: number, head: number}]
804
+ if (selections.length > 0) {
805
+ var sel = selections.map(function (r) {
806
+ return {
807
+ anchor: _this3._toCodeMirrorPosition(_this3.getPositionAt(r.from)),
808
+ head: _this3._toCodeMirrorPosition(_this3.getPositionAt(r.to))
809
+ };
810
+ });
811
+ return sel.map(function (selection) {
812
+ return _this3._toSelection(selection);
813
+ });
814
+ }
815
+ var cursor = this._toCodeMirrorPosition(this.getPositionAt(this.state.selection.main.head));
816
+ var selection = this._toSelection({
817
+ anchor: cursor,
818
+ head: cursor
819
+ });
820
+ return [selection];
821
+ }
822
+
823
+ /**
824
+ * Sets the selections for all the cursors, should not be empty.
825
+ * Cursors will be removed or added, as necessary.
826
+ * Passing an empty array resets a cursor position to the start of a document.
827
+ */
828
+ }, {
829
+ key: "setSelections",
830
+ value: function setSelections(selections) {
831
+ var _this4 = this;
832
+ var sel = selections.length ? selections.map(function (r) {
833
+ return EditorSelection.range(_this4.getOffsetAt(r.start), _this4.getOffsetAt(r.end));
834
+ }) : [EditorSelection.range(0, 0)];
835
+ this.editor.dispatch({
836
+ selection: EditorSelection.create(sel)
837
+ });
838
+ }
839
+
840
+ /**
841
+ * Replaces the current selection with the given text.
842
+ *
843
+ * @param text The text to be inserted.
844
+ */
845
+ }, {
846
+ key: "replaceSelection",
847
+ value: function replaceSelection(text, range) {
848
+ this.editor.dispatch({
849
+ changes: {
850
+ from: this.getOffsetAt(range.start),
851
+ to: this.getOffsetAt(range.end),
852
+ insert: text
853
+ }
854
+ });
855
+ }
856
+ }, {
857
+ key: "replaceSelections",
858
+ value: function replaceSelections(edits) {
859
+ var _this5 = this;
860
+ // const trans = this.state.replaceSelection(text);
861
+ this.editor.dispatch({
862
+ changes: edits.map(function (item) {
863
+ return {
864
+ from: _this5.getOffsetAt(item.range.start),
865
+ to: _this5.getOffsetAt(item.range.end),
866
+ insert: item.text
867
+ };
868
+ })
869
+ });
870
+ }
871
+ }, {
872
+ key: "highlightMatches",
873
+ value: function highlightMatches(matches, currentIndex) {
874
+ var effects = [this.highlightEffect.of({
875
+ matches: matches,
876
+ currentIndex: currentIndex
877
+ })];
878
+ if (!this.state.field(this.highlightField, false)) {
879
+ effects.push(StateEffect.appendConfig.of([this.highlightField]));
880
+ }
881
+ this.editor.dispatch({
882
+ effects: effects
883
+ });
884
+ }
885
+ }, {
886
+ key: "getTokens",
887
+ value:
888
+ /**
889
+ * Get a list of tokens for the current editor text content.
890
+ */
891
+ function getTokens() {
892
+ var _this6 = this;
893
+ var tokens = [];
894
+ var tree = ensureSyntaxTree(this.state, this.doc.length);
895
+ if (tree) {
896
+ tree.iterate({
897
+ enter: function enter(node) {
898
+ tokens.push({
899
+ value: _this6.state.sliceDoc(node.from, node.to),
900
+ offset: node.from,
901
+ type: node.name
902
+ });
903
+ return true;
904
+ }
905
+ });
906
+ }
907
+ return tokens;
908
+ }
909
+
910
+ /**
911
+ * Get the token at a given editor position.
912
+ */
913
+ }, {
914
+ key: "getTokenAt",
915
+ value: function getTokenAt(offset) {
916
+ var tree = ensureSyntaxTree(this.state, offset);
917
+ if (tree) {
918
+ var node = tree.resolveInner(offset);
919
+ return {
920
+ value: this.state.sliceDoc(node.from, node.to),
921
+ offset: node.from,
922
+ type: node.name
923
+ };
924
+ } else {
925
+ return {
926
+ value: '',
927
+ offset: offset
928
+ };
929
+ }
930
+ }
931
+
932
+ /**
933
+ * Get the token a the cursor position.
934
+ */
935
+ }, {
936
+ key: "getTokenAtCursor",
937
+ value: function getTokenAtCursor() {
938
+ return this.getTokenAt(this.state.selection.main.head);
939
+ }
940
+
941
+ /**
942
+ * Insert a new indented line at the current cursor position.
943
+ */
944
+ }, {
945
+ key: "newIndentedLine",
946
+ value: function newIndentedLine() {
947
+ insertNewlineAndIndent({
948
+ state: this.state,
949
+ dispatch: this.editor.dispatch
950
+ });
951
+ }
952
+
953
+ /**
954
+ * Execute a codemirror command on the editor.
955
+ *
956
+ * @param command - The name of the command to execute.
957
+ */
958
+ }, {
959
+ key: "execCommand",
960
+ value: function execCommand(command) {
961
+ command(this.editor);
962
+ }
963
+ }, {
964
+ key: "onKeydown",
965
+ value:
966
+ /**
967
+ * Handle keydown events from the editor.
968
+ */
969
+ function onKeydown(event) {
970
+ var position = this.state.selection.main.head;
971
+ if (position === 0 && event.keyCode === UP_ARROW) {
972
+ if (!event.shiftKey) {
973
+ // this.edgeRequested.emit('top');
974
+ this.edgeRequestedEmitter.fire('top');
975
+ }
976
+ return false;
977
+ }
978
+ var line = this.doc.lineAt(position).number;
979
+ if (line === 1 && event.keyCode === UP_ARROW) {
980
+ if (!event.shiftKey) {
981
+ // this.edgeRequested.emit('topLine');
982
+ this.edgeRequestedEmitter.fire('topLine');
983
+ }
984
+ return false;
985
+ }
986
+ var length = this.doc.length;
987
+ if (position === length && event.keyCode === DOWN_ARROW) {
988
+ if (!event.shiftKey) {
989
+ // this.edgeRequested.emit('bottom');
990
+ this.edgeRequestedEmitter.fire('bottom');
991
+ }
992
+ return false;
993
+ }
994
+ return false;
995
+ }
996
+
997
+ /**
998
+ * Handles a mime type change.
999
+ */
1000
+ }, {
1001
+ key: "_onMimeTypeChanged",
1002
+ value: function _onMimeTypeChanged() {
1003
+ var _this7 = this;
1004
+ var mime = this._model.mimeType;
1005
+ // TODO: should we provide a hook for when the mode is done being set?
1006
+ void ensure(mime).then(function (spec) {
1007
+ if (spec) {
1008
+ _this7._editorConfig.reconfigureExtension(_this7._editor, 'language', spec.support);
1009
+ }
1010
+ return;
1011
+ });
1012
+ }
1013
+
1014
+ /**
1015
+ * Handles a selections change.
1016
+ */
1017
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1018
+ }, {
1019
+ key: "_onSelectionsChanged",
1020
+ value: function _onSelectionsChanged(args) {
1021
+ // const uuid = args.key;
1022
+ // if (uuid !== this.uuid) {
1023
+ // this._cleanSelections(uuid);
1024
+ // if (args.type !== 'remove' && args.newValue) {
1025
+ // this._markSelections(uuid, args.newValue);
1026
+ // }
1027
+ // }
1028
+ }
1029
+
1030
+ /**
1031
+ * Clean selections for the given uuid.
1032
+ */
1033
+ }, {
1034
+ key: "_cleanSelections",
1035
+ value: function _cleanSelections(uuid) {
1036
+ this.editor.dispatch({
1037
+ effects: this._removeMark.of({
1038
+ uuid: uuid,
1039
+ decorations: this._selectionMarkers[uuid]
1040
+ })
1041
+ });
1042
+ }
1043
+ }, {
1044
+ key: "_buildMarkDecoration",
1045
+ value: function _buildMarkDecoration(uuid,
1046
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1047
+ selections) {
1048
+ var decorations = [];
1049
+
1050
+ // If we are marking selections corresponding to an active hover,
1051
+ // remove it.
1052
+ if (uuid === this._hoverId) {
1053
+ this._clearHover();
1054
+ }
1055
+ // If we can id the selection to a specific collaborator,
1056
+ // use that information.
1057
+ // let collaborator: ICollaborator | undefined;
1058
+ // if (this._model.modelDB.collaborators) {
1059
+ // collaborator = this._model.modelDB.collaborators.get(uuid);
1060
+ // }
1061
+
1062
+ // Style each selection for the uuid.
1063
+ // selections.forEach(selection => {
1064
+ // const from = selection.from;
1065
+ // const to = selection.to;
1066
+ // // Only render selections if the start is not equal to the end.
1067
+ // // In that case, we don't need to render the cursor.
1068
+ // if (from !== to) {
1069
+ // const style = collaborator
1070
+ // ? { ...selection.style, color: collaborator.color }
1071
+ // : selection.style;
1072
+ // const decoration = Decoration.mark({
1073
+ // attributes: this._toMarkSpec(style),
1074
+ // });
1075
+ // decorations.push(from > to ? decoration.range(to, from) : decoration.range(to, from));
1076
+ // } else if (collaborator) {
1077
+ // const caret = Decoration.widget({
1078
+ // widget: this._getCaret(collaborator),
1079
+ // });
1080
+ // decorations.push(caret.range(from));
1081
+ // }
1082
+ // });
1083
+
1084
+ return decorations;
1085
+ }
1086
+
1087
+ /**
1088
+ * Converts the selection style to a text marker options.
1089
+ */
1090
+ // protected _toMarkSpec(style: IEditorSelectionStyle) {
1091
+ // const r = parseInt(style.color.slice(1, 3), 16);
1092
+ // const g = parseInt(style.color.slice(3, 5), 16);
1093
+ // const b = parseInt(style.color.slice(5, 7), 16);
1094
+ // const css = `background-color: rgba( ${r}, ${g}, ${b}, 0.15)`;
1095
+ // return {
1096
+ // className: style.className,
1097
+ // title: style.displayName,
1098
+ // css,
1099
+ // };
1100
+ // }
1101
+
1102
+ /**
1103
+ * Construct a caret element representing the position
1104
+ * of a collaborator's cursor.
1105
+ */
1106
+ // protected _getCaret(collaborator: ICollaborator): CaretWidget {
1107
+ // return new CaretWidget(collaborator, {
1108
+ // setHoverId: (sessionId: string) => {
1109
+ // this._clearHover();
1110
+ // this._hoverId = sessionId;
1111
+ // },
1112
+ // setHoverTimeout: () => {
1113
+ // this._hoverTimeout = window.setTimeout(() => {
1114
+ // this._clearHover();
1115
+ // }, HOVER_TIMEOUT);
1116
+ // },
1117
+ // clearHoverTimeout: () => {
1118
+ // window.clearTimeout(this._hoverTimeout);
1119
+ // },
1120
+ // });
1121
+ // }
1122
+
1123
+ /**
1124
+ * Marks selections.
1125
+ */
1126
+ }, {
1127
+ key: "_markSelections",
1128
+ value: function _markSelections(uuid, selections) {
1129
+ var _this8 = this;
1130
+ var sel = selections.map(function (selection) {
1131
+ return {
1132
+ from: _this8.getOffsetAt(selection.start),
1133
+ to: _this8.getOffsetAt(selection.end),
1134
+ style: selection.style
1135
+ };
1136
+ });
1137
+ this.editor.dispatch({
1138
+ effects: this._addMark.of({
1139
+ uuid: uuid,
1140
+ selections: sel
1141
+ })
1142
+ });
1143
+ }
1144
+
1145
+ /**
1146
+ * Handles a cursor activity event.
1147
+ */
1148
+ }, {
1149
+ key: "_onCursorActivity",
1150
+ value: function _onCursorActivity() {
1151
+ // Only add selections if the editor has focus. This avoids unwanted
1152
+ // triggering of cursor activity due to collaborator actions.
1153
+ if (this._editor.hasFocus) {
1154
+ var selections = this.getSelections();
1155
+ this.model.selections = selections;
1156
+ }
1157
+ }
1158
+
1159
+ /**
1160
+ * Converts a code mirror selection to an editor selection.
1161
+ */
1162
+ }, {
1163
+ key: "_toSelection",
1164
+ value: function _toSelection(selection) {
1165
+ return {
1166
+ uuid: this.uuid,
1167
+ start: this._toPosition(selection.anchor),
1168
+ end: this._toPosition(selection.head),
1169
+ style: this.selectionStyle
1170
+ };
1171
+ }
1172
+ /**
1173
+ * Convert a code mirror position to an editor position.
1174
+ */
1175
+ }, {
1176
+ key: "_toPosition",
1177
+ value: function _toPosition(position) {
1178
+ return {
1179
+ line: position.line,
1180
+ column: position.ch
1181
+ };
1182
+ }
1183
+
1184
+ /**
1185
+ * Convert an editor position to a code mirror position.
1186
+ */
1187
+ }, {
1188
+ key: "_toCodeMirrorPosition",
1189
+ value: function _toCodeMirrorPosition(position) {
1190
+ return {
1191
+ line: position.line,
1192
+ ch: position.column
1193
+ };
1194
+ }
1195
+
1196
+ /**
1197
+ * Handles document changes.
1198
+ */
1199
+ }, {
1200
+ key: "_onDocChanged",
1201
+ value: function _onDocChanged(update) {
1202
+ if (update.transactions.length && update.transactions[0].selection) {
1203
+ this._onCursorActivity();
1204
+ }
1205
+ if (update.docChanged) {
1206
+ this._lastChange = update.changes;
1207
+ }
1208
+ this.model.value = update.state.doc.toJSON().join('\n');
1209
+ this.host.style.height = update.view.dom.offsetHeight + 'px';
1210
+ }
1211
+ /**
1212
+ * Handle the DOM events for the editor.
1213
+ *
1214
+ * @param event - The DOM event sent to the editor.
1215
+ *
1216
+ * #### Notes
1217
+ * This method implements the DOM `EventListener` interface and is
1218
+ * called in response to events on the editor's DOM node. It should
1219
+ * not be called directly by user code.
1220
+ */
1221
+ }, {
1222
+ key: "handleEvent",
1223
+ value: function handleEvent(event) {
1224
+ switch (event.type) {
1225
+ case 'focus':
1226
+ this._evtFocus();
1227
+ break;
1228
+ case 'blur':
1229
+ this._evtBlur();
1230
+ break;
1231
+ case 'scroll':
1232
+ this._evtScroll();
1233
+ break;
1234
+ default:
1235
+ break;
1236
+ }
1237
+ }
1238
+
1239
+ /**
1240
+ * Handle `focus` events for the editor.
1241
+ */
1242
+ }, {
1243
+ key: "_evtFocus",
1244
+ value: function _evtFocus() {
1245
+ this.host.classList.add('jp-mod-focused');
1246
+
1247
+ // Update the selections on editor gaining focus because
1248
+ // the onCursorActivity function filters usual cursor events
1249
+ // based on the editor's focus.
1250
+ this._onCursorActivity();
1251
+ }
1252
+
1253
+ /**
1254
+ * Handle `blur` events for the editor.
1255
+ */
1256
+ }, {
1257
+ key: "_evtBlur",
1258
+ value: function _evtBlur() {
1259
+ this.host.classList.remove('jp-mod-focused');
1260
+ }
1261
+
1262
+ /**
1263
+ * Handle `scroll` events for the editor.
1264
+ */
1265
+ }, {
1266
+ key: "_evtScroll",
1267
+ value: function _evtScroll() {
1268
+ // Remove any active hover.
1269
+ this._clearHover();
1270
+ }
1271
+
1272
+ /**
1273
+ * Clear the hover for a caret, due to things like
1274
+ * scrolling, resizing, deactivation, etc, where
1275
+ * the position is no longer valid.
1276
+ */
1277
+ }, {
1278
+ key: "_clearHover",
1279
+ value: function _clearHover() {
1280
+ if (this._caretHover) {
1281
+ window.clearTimeout(this._hoverTimeout);
1282
+ document.body.removeChild(this._caretHover);
1283
+ this._caretHover = null;
1284
+ }
1285
+ }
1286
+ /**
1287
+ * Check for an out of sync editor.
1288
+ */
1289
+ }, {
1290
+ key: "_checkSync",
1291
+ value: function _checkSync() {
1292
+ var change = this._lastChange;
1293
+ if (!change) {
1294
+ return;
1295
+ }
1296
+ this._lastChange = null;
1297
+ var doc = this.doc;
1298
+ if (doc.toString() === this._model.value) {
1299
+ return;
1300
+ }
1301
+
1302
+ // void showDialog({
1303
+ // title: this._trans.__('Code Editor out of Sync'),
1304
+ // body: this._trans.__(
1305
+ // 'Please open your browser JavaScript console for bug report instructions',
1306
+ // ),
1307
+ // });
1308
+ console.warn('If you are able and willing to publicly share the text or code in your editor, you can help us debug the "Code Editor out of Sync" message by pasting the following to the public issue at https://github.com/jupyterlab/jupyterlab/issues/2951. Please note that the data below includes the text/code in your editor.');
1309
+ console.warn(JSON.stringify({
1310
+ model: this._model.value,
1311
+ view: doc.toString(),
1312
+ selections: this.getSelections(),
1313
+ cursor: this.getCursorPosition(),
1314
+ lineSep: this.state.facet(EditorState.lineSeparator),
1315
+ change: change
1316
+ }));
1317
+ }
1318
+
1319
+ // protected _poll: Poll;
1320
+ // protected _yeditorBinding: IYCodeMirrorBinding | null;
1321
+ }]);
1322
+ return CodeMirrorEditor;
1323
+ }();
1324
+ export function createEditor(host, config, value, editorConfig, additionalExtensions) {
1325
+ var extensions = editorConfig.getInitialExtensions(config);
1326
+ extensions.push.apply(extensions, _toConsumableArray(additionalExtensions));
1327
+ var view = new EditorView({
1328
+ state: EditorState.create({
1329
+ doc: value,
1330
+ extensions: extensions
1331
+ }),
1332
+ parent: host
1333
+ });
1334
+ if (config.readOnly) {
1335
+ view.dom.classList.add(READ_ONLY_CLASS);
1336
+ }
1337
+ return view;
1338
+ }