@difizen/libro-search-code-cell 0.1.2

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.
@@ -0,0 +1,563 @@
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
+ var _dec, _dec2, _dec3, _dec4, _class, _class2, _descriptor, _descriptor2, _descriptor3;
3
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
4
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
5
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
6
+ function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
7
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+ 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); } }
9
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
11
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
12
+ function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
13
+ function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
14
+ import { searchText } from '@difizen/libro-search';
15
+ import { prop } from '@difizen/mana-app';
16
+ import { DisposableCollection, Emitter } from '@difizen/mana-app';
17
+ import { watch } from '@difizen/mana-app';
18
+ import { inject, transient } from '@difizen/mana-app';
19
+ import { CodeEditorSearchHighlighterFactory } from "./code-cell-search-protocol.js";
20
+ /**
21
+ * Search provider for cells.
22
+ */
23
+ export var CodeEditorCellSearchProvider = (_dec = transient(), _dec2 = prop(), _dec3 = prop(), _dec4 = prop(), _dec(_class = (_class2 = /*#__PURE__*/function () {
24
+ /**
25
+ * Constructor
26
+ *
27
+ * @param cell Cell widget
28
+ */
29
+ function CodeEditorCellSearchProvider(highlighterFactory, cell) {
30
+ var _this = this;
31
+ _classCallCheck(this, CodeEditorCellSearchProvider);
32
+ this.toDispose = new DisposableCollection();
33
+ /**
34
+ * CodeMirror search highlighter
35
+ */
36
+ _initializerDefineProperty(this, "editorHighlighter", _descriptor, this);
37
+ /**
38
+ * Current match index
39
+ */
40
+ _initializerDefineProperty(this, "currentIndex", _descriptor2, this);
41
+ /**
42
+ * Current search filters
43
+ */
44
+ _initializerDefineProperty(this, "filters", _descriptor3, this);
45
+ /**
46
+ * Current search query
47
+ */
48
+ this.query = null;
49
+ this._isActive = true;
50
+ this._isDisposed = false;
51
+ this.lastReplacementPosition = null;
52
+ /**
53
+ * Replace all matches in the cell source with the provided text
54
+ *
55
+ * @param newText The replacement text.
56
+ * @returns Whether a replace occurred.
57
+ */
58
+ this.replaceAllMatches = function (newText) {
59
+ if (!_this.isActive) {
60
+ return Promise.resolve(false);
61
+ }
62
+ var occurred = _this.editorHighlighter.matches.length > 0;
63
+ // const src = this.cell.model.value;
64
+ // let lastEnd = 0;
65
+ // const finalSrc = this.cmHandler.matches.reduce((agg, match) => {
66
+ // const start = match.position as number;
67
+ // const end = start + match.text.length;
68
+ // const newStep = `${agg}${src.slice(lastEnd, start)}${newText}`;
69
+ // lastEnd = end;
70
+ // return newStep;
71
+ // }, '');
72
+
73
+ var editor = _this.cell.editor;
74
+ if (occurred && editor) {
75
+ var changes = _this.editorHighlighter.matches.map(function (match) {
76
+ return {
77
+ range: {
78
+ start: editor.getPositionAt(match.position),
79
+ end: editor.getPositionAt(match.position + match.text.length)
80
+ },
81
+ text: newText
82
+ };
83
+ });
84
+ editor === null || editor === void 0 || editor.replaceSelections(changes);
85
+ _this.editorHighlighter.matches = [];
86
+ _this.currentIndex = undefined;
87
+ // this.cell.model.setSource(`${finalSrc}${src.slice(lastEnd)}`);
88
+ }
89
+
90
+ return Promise.resolve(occurred);
91
+ };
92
+ this.updateMatches = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
93
+ var matches, _this$cell$editor$get, _this$cell$editor, cursorOffset, index;
94
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
95
+ while (1) switch (_context.prev = _context.next) {
96
+ case 0:
97
+ if (!(_this.query !== null)) {
98
+ _context.next = 10;
99
+ break;
100
+ }
101
+ if (!_this.isActive) {
102
+ _context.next = 9;
103
+ break;
104
+ }
105
+ _context.next = 4;
106
+ return searchText(_this.query, _this.cell.model.value);
107
+ case 4:
108
+ matches = _context.sent;
109
+ _this.editorHighlighter.matches = matches;
110
+ if (_this.isCellSelected) {
111
+ cursorOffset = _this.cell.editor.getOffsetAt((_this$cell$editor$get = (_this$cell$editor = _this.cell.editor) === null || _this$cell$editor === void 0 ? void 0 : _this$cell$editor.getCursorPosition()) !== null && _this$cell$editor$get !== void 0 ? _this$cell$editor$get : {
112
+ column: 0,
113
+ line: 0
114
+ });
115
+ index = matches.findIndex(function (item) {
116
+ return item.position >= cursorOffset;
117
+ });
118
+ _this.currentIndex = index;
119
+ }
120
+ _context.next = 10;
121
+ break;
122
+ case 9:
123
+ _this.editorHighlighter.matches = [];
124
+ case 10:
125
+ case "end":
126
+ return _context.stop();
127
+ }
128
+ }, _callee);
129
+ }));
130
+ this.cell = cell;
131
+ this.highlighterFactory = highlighterFactory;
132
+ this.currentIndex = undefined;
133
+ this.stateChangedEmitter = new Emitter();
134
+ this.toDispose.push(this.stateChangedEmitter);
135
+ this.editorHighlighter = this.highlighterFactory(this.cell.editor);
136
+ this.toDispose.push(watch(this.cell.model, 'value', this.updateMatches));
137
+ this.toDispose.push(watch(this.cell, 'editor', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
138
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
139
+ while (1) switch (_context2.prev = _context2.next) {
140
+ case 0:
141
+ _context2.next = 2;
142
+ return _this.cell.editorReady;
143
+ case 2:
144
+ if (_this.cell.hasInputHidden === true) {
145
+ _this.endQuery();
146
+ } else {
147
+ _this.startQuery(_this.query, _this.filters);
148
+ }
149
+ case 3:
150
+ case "end":
151
+ return _context2.stop();
152
+ }
153
+ }, _callee2);
154
+ }))));
155
+ }
156
+
157
+ /**
158
+ * Get an initial query value if applicable so that it can be entered
159
+ * into the search box as an initial query
160
+ *
161
+ * @returns Initial value used to populate the search box.
162
+ */
163
+ CodeEditorCellSearchProvider = inject(CodeEditorSearchHighlighterFactory)(CodeEditorCellSearchProvider, undefined, 0) || CodeEditorCellSearchProvider;
164
+ _createClass(CodeEditorCellSearchProvider, [{
165
+ key: "getInitialQuery",
166
+ value: function getInitialQuery() {
167
+ var _this$cell;
168
+ var selection = (_this$cell = this.cell) === null || _this$cell === void 0 || (_this$cell = _this$cell.editor) === null || _this$cell === void 0 ? void 0 : _this$cell.getSelectionValue();
169
+ // if there are newlines, just return empty string
170
+ return (selection === null || selection === void 0 ? void 0 : selection.search(/\r?\n|\r/g)) === -1 ? selection : '';
171
+ }
172
+ }, {
173
+ key: "disposed",
174
+ get: function get() {
175
+ return this._isDisposed;
176
+ }
177
+ }, {
178
+ key: "setEditor",
179
+ value: function () {
180
+ var _setEditor = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
181
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
182
+ while (1) switch (_context3.prev = _context3.next) {
183
+ case 0:
184
+ _context3.next = 2;
185
+ return this.cell.editorReady;
186
+ case 2:
187
+ if (this.cell.editor) {
188
+ this.editorHighlighter.setEditor(this.cell.editor);
189
+ }
190
+ case 3:
191
+ case "end":
192
+ return _context3.stop();
193
+ }
194
+ }, _callee3, this);
195
+ }));
196
+ function setEditor() {
197
+ return _setEditor.apply(this, arguments);
198
+ }
199
+ return setEditor;
200
+ }()
201
+ /**
202
+ * Changed signal to be emitted when search matches change.
203
+ */
204
+ }, {
205
+ key: "stateChanged",
206
+ get: function get() {
207
+ return this.stateChangedEmitter.event;
208
+ }
209
+
210
+ /**
211
+ * Current match index
212
+ */
213
+ }, {
214
+ key: "currentMatchIndex",
215
+ get: function get() {
216
+ return this.isActive ? this.currentIndex : undefined;
217
+ }
218
+
219
+ /**
220
+ * Whether the cell search is active.
221
+ *
222
+ * This is used when applying search only on selected cells.
223
+ */
224
+ }, {
225
+ key: "isActive",
226
+ get: function get() {
227
+ return this._isActive;
228
+ }
229
+
230
+ /**
231
+ * Whether the search provider is disposed or not.
232
+ */
233
+ }, {
234
+ key: "isDisposed",
235
+ get: function get() {
236
+ return this._isDisposed;
237
+ }
238
+
239
+ /**
240
+ * Number of matches in the cell.
241
+ */
242
+ }, {
243
+ key: "matchesCount",
244
+ get: function get() {
245
+ return this.isActive ? this.editorHighlighter.matches.length : 0;
246
+ }
247
+ }, {
248
+ key: "isCellSelected",
249
+ get: function get() {
250
+ return this.cell.parent.isSelected(this.cell);
251
+ }
252
+
253
+ /**
254
+ * Clear currently highlighted match
255
+ */
256
+ }, {
257
+ key: "clearHighlight",
258
+ value: function clearHighlight() {
259
+ this.currentIndex = undefined;
260
+ this.editorHighlighter.clearHighlight();
261
+ return Promise.resolve();
262
+ }
263
+
264
+ /**
265
+ * Dispose the search provider
266
+ */
267
+ }, {
268
+ key: "dispose",
269
+ value: function dispose() {
270
+ if (this.isDisposed) {
271
+ return;
272
+ }
273
+ this.toDispose.dispose();
274
+ this._isDisposed = true;
275
+ if (this.isActive) {
276
+ this.endQuery().catch(function (reason) {
277
+ console.error("Failed to end search query on cells.", reason);
278
+ });
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Set `isActive` status.
284
+ *
285
+ * #### Notes
286
+ * It will start or end the search
287
+ *
288
+ * @param v New value
289
+ */
290
+ }, {
291
+ key: "setIsActive",
292
+ value: function () {
293
+ var _setIsActive = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(v) {
294
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
295
+ while (1) switch (_context4.prev = _context4.next) {
296
+ case 0:
297
+ if (this.isActive !== v) {
298
+ this._isActive = v;
299
+ }
300
+ if (!this.isActive) {
301
+ _context4.next = 7;
302
+ break;
303
+ }
304
+ if (!(this.query !== null)) {
305
+ _context4.next = 5;
306
+ break;
307
+ }
308
+ _context4.next = 5;
309
+ return this.startQuery(this.query, this.filters);
310
+ case 5:
311
+ _context4.next = 9;
312
+ break;
313
+ case 7:
314
+ _context4.next = 9;
315
+ return this.endQuery();
316
+ case 9:
317
+ case "end":
318
+ return _context4.stop();
319
+ }
320
+ }, _callee4, this);
321
+ }));
322
+ function setIsActive(_x) {
323
+ return _setIsActive.apply(this, arguments);
324
+ }
325
+ return setIsActive;
326
+ }()
327
+ /**
328
+ * Initialize the search using the provided options. Should update the UI
329
+ * to highlight all matches and "select" the first match.
330
+ *
331
+ * @param query A RegExp to be use to perform the search
332
+ * @param filters Filter parameters to pass to provider
333
+ */
334
+ }, {
335
+ key: "startQuery",
336
+ value: function () {
337
+ var _startQuery = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(query, filters) {
338
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
339
+ while (1) switch (_context5.prev = _context5.next) {
340
+ case 0:
341
+ this.query = query;
342
+ this.filters = filters;
343
+ if (!this.cell.hasInputHidden) {
344
+ _context5.next = 4;
345
+ break;
346
+ }
347
+ return _context5.abrupt("return");
348
+ case 4:
349
+ _context5.next = 6;
350
+ return this.setEditor();
351
+ case 6:
352
+ _context5.next = 8;
353
+ return this.updateMatches();
354
+ case 8:
355
+ case "end":
356
+ return _context5.stop();
357
+ }
358
+ }, _callee5, this);
359
+ }));
360
+ function startQuery(_x2, _x3) {
361
+ return _startQuery.apply(this, arguments);
362
+ }
363
+ return startQuery;
364
+ }()
365
+ /**
366
+ * Stop the search and clean any UI elements.
367
+ */
368
+ }, {
369
+ key: "endQuery",
370
+ value: function () {
371
+ var _endQuery = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
372
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
373
+ while (1) switch (_context6.prev = _context6.next) {
374
+ case 0:
375
+ this.currentIndex = undefined;
376
+ this.query = null;
377
+ _context6.next = 4;
378
+ return this.editorHighlighter.endQuery();
379
+ case 4:
380
+ case "end":
381
+ return _context6.stop();
382
+ }
383
+ }, _callee6, this);
384
+ }));
385
+ function endQuery() {
386
+ return _endQuery.apply(this, arguments);
387
+ }
388
+ return endQuery;
389
+ }()
390
+ /**
391
+ * Highlight the next match.
392
+ *
393
+ * @returns The next match if there is one.
394
+ */
395
+ }, {
396
+ key: "highlightNext",
397
+ value: function () {
398
+ var _highlightNext = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
399
+ var _this$cell$editor2, match;
400
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
401
+ while (1) switch (_context7.prev = _context7.next) {
402
+ case 0:
403
+ if (!(this.matchesCount === 0 || !this.isActive)) {
404
+ _context7.next = 4;
405
+ break;
406
+ }
407
+ this.currentIndex = undefined;
408
+ _context7.next = 10;
409
+ break;
410
+ case 4:
411
+ if (this.lastReplacementPosition) {
412
+ (_this$cell$editor2 = this.cell.editor) === null || _this$cell$editor2 === void 0 || _this$cell$editor2.setCursorPosition(this.lastReplacementPosition);
413
+ this.lastReplacementPosition = null;
414
+ }
415
+
416
+ // This starts from the cursor position
417
+ _context7.next = 7;
418
+ return this.editorHighlighter.highlightNext();
419
+ case 7:
420
+ match = _context7.sent;
421
+ if (match) {
422
+ this.currentIndex = this.editorHighlighter.currentIndex;
423
+ } else {
424
+ this.currentIndex = undefined;
425
+ }
426
+ return _context7.abrupt("return", match);
427
+ case 10:
428
+ return _context7.abrupt("return", Promise.resolve(this.getCurrentMatch()));
429
+ case 11:
430
+ case "end":
431
+ return _context7.stop();
432
+ }
433
+ }, _callee7, this);
434
+ }));
435
+ function highlightNext() {
436
+ return _highlightNext.apply(this, arguments);
437
+ }
438
+ return highlightNext;
439
+ }()
440
+ /**
441
+ * Highlight the previous match.
442
+ *
443
+ * @returns The previous match if there is one.
444
+ */
445
+ }, {
446
+ key: "highlightPrevious",
447
+ value: function () {
448
+ var _highlightPrevious = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
449
+ var match;
450
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
451
+ while (1) switch (_context8.prev = _context8.next) {
452
+ case 0:
453
+ if (!(this.matchesCount === 0 || !this.isActive)) {
454
+ _context8.next = 4;
455
+ break;
456
+ }
457
+ this.currentIndex = undefined;
458
+ _context8.next = 9;
459
+ break;
460
+ case 4:
461
+ _context8.next = 6;
462
+ return this.editorHighlighter.highlightPrevious();
463
+ case 6:
464
+ match = _context8.sent;
465
+ if (match) {
466
+ this.currentIndex = this.editorHighlighter.currentIndex;
467
+ } else {
468
+ this.currentIndex = undefined;
469
+ }
470
+ return _context8.abrupt("return", match);
471
+ case 9:
472
+ return _context8.abrupt("return", Promise.resolve(this.getCurrentMatch()));
473
+ case 10:
474
+ case "end":
475
+ return _context8.stop();
476
+ }
477
+ }, _callee8, this);
478
+ }));
479
+ function highlightPrevious() {
480
+ return _highlightPrevious.apply(this, arguments);
481
+ }
482
+ return highlightPrevious;
483
+ }()
484
+ /**
485
+ * Replace the currently selected match with the provided text.
486
+ *
487
+ * If no match is selected, it won't do anything.
488
+ *
489
+ * @param newText The replacement text.
490
+ * @returns Whether a replace occurred.
491
+ */
492
+ }, {
493
+ key: "replaceCurrentMatch",
494
+ value: function replaceCurrentMatch(newText) {
495
+ if (!this.isActive) {
496
+ return Promise.resolve(false);
497
+ }
498
+ var occurred = false;
499
+ if (this.currentIndex !== undefined && this.currentIndex < this.editorHighlighter.matches.length) {
500
+ var editor = this.cell.editor;
501
+ var selection = editor === null || editor === void 0 ? void 0 : editor.getSelectionValue();
502
+ var match = this.getCurrentMatch();
503
+ if (!match) {
504
+ return Promise.resolve(occurred);
505
+ }
506
+ // If cursor is not on a selection, highlight the next match
507
+ if (selection !== (match === null || match === void 0 ? void 0 : match.text)) {
508
+ this.currentIndex = undefined;
509
+ // The next will be highlighted as a consequence of this returning false
510
+ } else {
511
+ var _editor$getCursorPosi;
512
+ this.editorHighlighter.matches.splice(this.currentIndex, 1);
513
+ this.currentIndex = undefined;
514
+ // Store the current position to highlight properly the next search hit
515
+ this.lastReplacementPosition = (_editor$getCursorPosi = editor === null || editor === void 0 ? void 0 : editor.getCursorPosition()) !== null && _editor$getCursorPosi !== void 0 ? _editor$getCursorPosi : null;
516
+ editor === null || editor === void 0 || editor.replaceSelection(newText, {
517
+ start: editor.getPositionAt(match.position),
518
+ end: editor.getPositionAt(match.position + match.text.length)
519
+ });
520
+ occurred = true;
521
+ }
522
+ }
523
+ return Promise.resolve(occurred);
524
+ }
525
+ }, {
526
+ key: "getCurrentMatch",
527
+ value:
528
+ /**
529
+ * Get the current match if it exists.
530
+ *
531
+ * @returns The current match
532
+ */
533
+ function getCurrentMatch() {
534
+ if (this.currentIndex === undefined) {
535
+ return undefined;
536
+ } else {
537
+ var match = undefined;
538
+ if (this.currentIndex < this.editorHighlighter.matches.length) {
539
+ match = this.editorHighlighter.matches[this.currentIndex];
540
+ }
541
+ return match;
542
+ }
543
+ }
544
+ }]);
545
+ return CodeEditorCellSearchProvider;
546
+ }(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "editorHighlighter", [_dec2], {
547
+ configurable: true,
548
+ enumerable: true,
549
+ writable: true,
550
+ initializer: null
551
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "currentIndex", [_dec3], {
552
+ configurable: true,
553
+ enumerable: true,
554
+ writable: true,
555
+ initializer: function initializer() {
556
+ return undefined;
557
+ }
558
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "filters", [_dec4], {
559
+ configurable: true,
560
+ enumerable: true,
561
+ writable: true,
562
+ initializer: null
563
+ })), _class2)) || _class);
package/es/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './code-cell-search-protocol.js';
2
+ export * from './code-cell-search-provider.js';
3
+ export * from './code-editor-cell-search-provider.js';
4
+ export * from './module.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uCAAuC,CAAC;AACtD,cAAc,aAAa,CAAC"}
package/es/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./code-cell-search-protocol.js";
2
+ export * from "./code-cell-search-provider.js";
3
+ export * from "./code-editor-cell-search-provider.js";
4
+ export * from "./module.js";
package/es/module.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { ManaModule } from '@difizen/mana-app';
2
+ export declare const SearchCodeCellModule: ManaModule;
3
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAW/C,eAAO,MAAM,oBAAoB,YAgCH,CAAC"}
package/es/module.js ADDED
@@ -0,0 +1,30 @@
1
+ import { LibroSearchModule } from '@difizen/libro-search';
2
+ import { ManaModule } from '@difizen/mana-app';
3
+ import { CodeCellSearchOption, CodeCellSearchProviderFactory, CodeEditorSearchHighlighterFactory } from "./code-cell-search-protocol.js";
4
+ import { CodeCellSearchProviderContribution } from "./code-cell-search-provider-contribution.js";
5
+ import { CodeCellSearchProvider } from "./code-cell-search-provider.js";
6
+ import { GenericSearchHighlighter } from "./search-highlighter.js";
7
+ export var SearchCodeCellModule = ManaModule.create().register(CodeCellSearchProvider, CodeCellSearchProviderContribution, CodeCellSearchProviderFactory, GenericSearchHighlighter, {
8
+ token: CodeEditorSearchHighlighterFactory,
9
+ useFactory: function useFactory(ctx) {
10
+ return function (editor) {
11
+ var child = ctx.container.createChild();
12
+ var highlighter = child.get(GenericSearchHighlighter);
13
+ highlighter.setEditor(editor);
14
+ return highlighter;
15
+ };
16
+ }
17
+ }, {
18
+ token: CodeCellSearchProviderFactory,
19
+ useFactory: function useFactory(ctx) {
20
+ return function (options) {
21
+ var child = ctx.container.createChild();
22
+ child.register({
23
+ token: CodeCellSearchOption,
24
+ useValue: options
25
+ });
26
+ var model = child.get(CodeCellSearchProvider);
27
+ return model;
28
+ };
29
+ }
30
+ }).dependOn(LibroSearchModule);