@difizen/libro-search-code-cell 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.
@@ -0,0 +1,578 @@
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
11
+ 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); }
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 { Disposable } from '@difizen/mana-app';
16
+ import { prop } from '@difizen/mana-app';
17
+ import { DisposableCollection, Emitter } from '@difizen/mana-app';
18
+ import { watch } from '@difizen/mana-app';
19
+ import { inject, transient } from '@difizen/mana-app';
20
+ import { CodeEditorSearchHighlighterFactory } from "./code-cell-search-protocol.js";
21
+ /**
22
+ * Search provider for cells.
23
+ */
24
+ export var CodeEditorCellSearchProvider = (_dec = transient(), _dec2 = prop(), _dec3 = prop(), _dec4 = prop(), _dec(_class = (_class2 = /*#__PURE__*/function () {
25
+ /**
26
+ * Constructor
27
+ *
28
+ * @param cell Cell widget
29
+ */
30
+ function CodeEditorCellSearchProvider(highlighterFactory, cell) {
31
+ var _this = this,
32
+ _this$cell$editorView,
33
+ _this$cell$editorView2;
34
+ _classCallCheck(this, CodeEditorCellSearchProvider);
35
+ this.toDispose = new DisposableCollection();
36
+ /**
37
+ * code editor search highlighter
38
+ */
39
+ _initializerDefineProperty(this, "editorHighlighter", _descriptor, this);
40
+ /**
41
+ * Current match index
42
+ */
43
+ _initializerDefineProperty(this, "currentIndex", _descriptor2, this);
44
+ /**
45
+ * Current search filters
46
+ */
47
+ _initializerDefineProperty(this, "filters", _descriptor3, this);
48
+ /**
49
+ * Current search query
50
+ */
51
+ this.query = null;
52
+ this._isActive = true;
53
+ this._isDisposed = false;
54
+ this.lastReplacementPosition = null;
55
+ /**
56
+ * Replace all matches in the cell source with the provided text
57
+ *
58
+ * @param newText The replacement text.
59
+ * @returns Whether a replace occurred.
60
+ */
61
+ this.replaceAllMatches = function (newText) {
62
+ if (!_this.isActive) {
63
+ return Promise.resolve(false);
64
+ }
65
+ var occurred = _this.editorHighlighter.matches.length > 0;
66
+ // const src = this.cell.model.value;
67
+ // let lastEnd = 0;
68
+ // const finalSrc = this.cmHandler.matches.reduce((agg, match) => {
69
+ // const start = match.position as number;
70
+ // const end = start + match.text.length;
71
+ // const newStep = `${agg}${src.slice(lastEnd, start)}${newText}`;
72
+ // lastEnd = end;
73
+ // return newStep;
74
+ // }, '');
75
+
76
+ var editor = _this.cell.editor;
77
+ if (occurred && editor) {
78
+ var changes = _this.editorHighlighter.matches.map(function (match) {
79
+ return {
80
+ range: {
81
+ start: editor.getPositionAt(match.position),
82
+ end: editor.getPositionAt(match.position + match.text.length)
83
+ },
84
+ text: newText
85
+ };
86
+ });
87
+ editor === null || editor === void 0 || editor.replaceSelections(changes);
88
+ _this.editorHighlighter.matches = [];
89
+ _this.currentIndex = undefined;
90
+ // this.cell.model.setSource(`${finalSrc}${src.slice(lastEnd)}`);
91
+ }
92
+ return Promise.resolve(occurred);
93
+ };
94
+ this.updateMatches = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
95
+ var matches, _this$cell$editor, _this$cell$editor$get, _this$cell$editor2, cursorOffset, index;
96
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
97
+ while (1) switch (_context.prev = _context.next) {
98
+ case 0:
99
+ if (!(_this.query !== null)) {
100
+ _context.next = 15;
101
+ break;
102
+ }
103
+ if (!_this.isActive) {
104
+ _context.next = 14;
105
+ break;
106
+ }
107
+ _context.next = 4;
108
+ return searchText(_this.query, _this.cell.model.value);
109
+ case 4:
110
+ matches = _context.sent;
111
+ _this.editorHighlighter.matches = matches;
112
+ if (!_this.isCellSelected) {
113
+ _context.next = 12;
114
+ break;
115
+ }
116
+ cursorOffset = (_this$cell$editor = _this.cell.editor) === null || _this$cell$editor === void 0 ? void 0 : _this$cell$editor.getOffsetAt((_this$cell$editor$get = (_this$cell$editor2 = _this.cell.editor) === null || _this$cell$editor2 === void 0 ? void 0 : _this$cell$editor2.getCursorPosition()) !== null && _this$cell$editor$get !== void 0 ? _this$cell$editor$get : {
117
+ column: 0,
118
+ line: 0
119
+ });
120
+ if (!(cursorOffset === undefined)) {
121
+ _context.next = 10;
122
+ break;
123
+ }
124
+ return _context.abrupt("return");
125
+ case 10:
126
+ index = matches.findIndex(function (item) {
127
+ return item.position + item.text.length >= cursorOffset;
128
+ });
129
+ if (index >= 0) {
130
+ _this.currentIndex = index;
131
+ _this.editorHighlighter.currentIndex = index;
132
+ }
133
+ case 12:
134
+ _context.next = 15;
135
+ break;
136
+ case 14:
137
+ _this.editorHighlighter.matches = [];
138
+ case 15:
139
+ case "end":
140
+ return _context.stop();
141
+ }
142
+ }, _callee);
143
+ }));
144
+ this.cell = cell;
145
+ this.highlighterFactory = highlighterFactory;
146
+ this.currentIndex = undefined;
147
+ this.stateChangedEmitter = new Emitter();
148
+ this.toDispose.push(this.stateChangedEmitter);
149
+ this.editorHighlighter = this.highlighterFactory(this.cell.editor);
150
+ this.toDispose.push(watch(this.cell.model, 'value', this.updateMatches));
151
+ this.toDispose.push((_this$cell$editorView = (_this$cell$editorView2 = this.cell.editorView) === null || _this$cell$editorView2 === void 0 ? void 0 : _this$cell$editorView2.onEditorStatusChange(function (e) {
152
+ if (e.status === 'ready') {
153
+ var _this$cell$editorView3;
154
+ var editor = (_this$cell$editorView3 = _this.cell.editorView) === null || _this$cell$editorView3 === void 0 ? void 0 : _this$cell$editorView3.editor;
155
+ if (editor) {
156
+ _this.editorHighlighter.setEditor(editor);
157
+ }
158
+ if (e.prevState === 'init') {
159
+ if (_this.cell.hasInputHidden === true) {
160
+ _this.endQuery();
161
+ } else {
162
+ _this.startQuery(_this.query, _this.filters);
163
+ }
164
+ }
165
+ }
166
+ })) !== null && _this$cell$editorView !== void 0 ? _this$cell$editorView : Disposable.NONE);
167
+ }
168
+
169
+ /**
170
+ * Get an initial query value if applicable so that it can be entered
171
+ * into the search box as an initial query
172
+ *
173
+ * @returns Initial value used to populate the search box.
174
+ */
175
+ CodeEditorCellSearchProvider = inject(CodeEditorSearchHighlighterFactory)(CodeEditorCellSearchProvider, undefined, 0) || CodeEditorCellSearchProvider;
176
+ _createClass(CodeEditorCellSearchProvider, [{
177
+ key: "getInitialQuery",
178
+ value: function getInitialQuery() {
179
+ var _this$cell;
180
+ 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();
181
+ // if there are newlines, just return empty string
182
+ return (selection === null || selection === void 0 ? void 0 : selection.search(/\r?\n|\r/g)) === -1 ? selection : '';
183
+ }
184
+ }, {
185
+ key: "disposed",
186
+ get: function get() {
187
+ return this._isDisposed;
188
+ }
189
+ }, {
190
+ key: "setEditor",
191
+ value: function () {
192
+ var _setEditor = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
193
+ var _this$cell$editorView4;
194
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
195
+ while (1) switch (_context2.prev = _context2.next) {
196
+ case 0:
197
+ if (this.cell.editor && ((_this$cell$editorView4 = this.cell.editorView) === null || _this$cell$editorView4 === void 0 ? void 0 : _this$cell$editorView4.editorStatus) === 'ready') {
198
+ this.editorHighlighter.setEditor(this.cell.editor);
199
+ }
200
+ case 1:
201
+ case "end":
202
+ return _context2.stop();
203
+ }
204
+ }, _callee2, this);
205
+ }));
206
+ function setEditor() {
207
+ return _setEditor.apply(this, arguments);
208
+ }
209
+ return setEditor;
210
+ }()
211
+ /**
212
+ * Changed signal to be emitted when search matches change.
213
+ */
214
+ }, {
215
+ key: "stateChanged",
216
+ get: function get() {
217
+ return this.stateChangedEmitter.event;
218
+ }
219
+
220
+ /**
221
+ * Current match index
222
+ */
223
+ }, {
224
+ key: "currentMatchIndex",
225
+ get: function get() {
226
+ return this.isActive ? this.currentIndex : undefined;
227
+ }
228
+
229
+ /**
230
+ * Whether the cell search is active.
231
+ *
232
+ * This is used when applying search only on selected cells.
233
+ */
234
+ }, {
235
+ key: "isActive",
236
+ get: function get() {
237
+ return this._isActive;
238
+ }
239
+
240
+ /**
241
+ * Whether the search provider is disposed or not.
242
+ */
243
+ }, {
244
+ key: "isDisposed",
245
+ get: function get() {
246
+ return this._isDisposed;
247
+ }
248
+
249
+ /**
250
+ * Number of matches in the cell.
251
+ */
252
+ }, {
253
+ key: "matchesCount",
254
+ get: function get() {
255
+ return this.isActive ? this.editorHighlighter.matches.length : 0;
256
+ }
257
+ }, {
258
+ key: "isCellSelected",
259
+ get: function get() {
260
+ return this.cell.parent.isSelected(this.cell);
261
+ }
262
+
263
+ /**
264
+ * Clear currently highlighted match
265
+ */
266
+ }, {
267
+ key: "clearHighlight",
268
+ value: function clearHighlight() {
269
+ this.currentIndex = undefined;
270
+ this.editorHighlighter.clearHighlight();
271
+ return Promise.resolve();
272
+ }
273
+
274
+ /**
275
+ * Dispose the search provider
276
+ */
277
+ }, {
278
+ key: "dispose",
279
+ value: function dispose() {
280
+ if (this.isDisposed) {
281
+ return;
282
+ }
283
+ this.toDispose.dispose();
284
+ this._isDisposed = true;
285
+ if (this.isActive) {
286
+ this.endQuery().catch(function (reason) {
287
+ console.error("Failed to end search query on cells.", reason);
288
+ });
289
+ }
290
+ }
291
+
292
+ /**
293
+ * Set `isActive` status.
294
+ *
295
+ * #### Notes
296
+ * It will start or end the search
297
+ *
298
+ * @param v New value
299
+ */
300
+ }, {
301
+ key: "setIsActive",
302
+ value: (function () {
303
+ var _setIsActive = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(v) {
304
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
305
+ while (1) switch (_context3.prev = _context3.next) {
306
+ case 0:
307
+ if (this.isActive !== v) {
308
+ this._isActive = v;
309
+ }
310
+ if (!this.isActive) {
311
+ _context3.next = 7;
312
+ break;
313
+ }
314
+ if (!(this.query !== null)) {
315
+ _context3.next = 5;
316
+ break;
317
+ }
318
+ _context3.next = 5;
319
+ return this.startQuery(this.query, this.filters);
320
+ case 5:
321
+ _context3.next = 9;
322
+ break;
323
+ case 7:
324
+ _context3.next = 9;
325
+ return this.endQuery();
326
+ case 9:
327
+ case "end":
328
+ return _context3.stop();
329
+ }
330
+ }, _callee3, this);
331
+ }));
332
+ function setIsActive(_x) {
333
+ return _setIsActive.apply(this, arguments);
334
+ }
335
+ return setIsActive;
336
+ }()
337
+ /**
338
+ * Initialize the search using the provided options. Should update the UI
339
+ * to highlight all matches and "select" the first match.
340
+ *
341
+ * @param query A RegExp to be use to perform the search
342
+ * @param filters Filter parameters to pass to provider
343
+ */
344
+ )
345
+ }, {
346
+ key: "startQuery",
347
+ value: (function () {
348
+ var _startQuery = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(query, filters) {
349
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
350
+ while (1) switch (_context4.prev = _context4.next) {
351
+ case 0:
352
+ this.query = query;
353
+ this.filters = filters;
354
+ if (!this.cell.hasInputHidden) {
355
+ _context4.next = 4;
356
+ break;
357
+ }
358
+ return _context4.abrupt("return");
359
+ case 4:
360
+ _context4.next = 6;
361
+ return this.setEditor();
362
+ case 6:
363
+ _context4.next = 8;
364
+ return this.updateMatches();
365
+ case 8:
366
+ case "end":
367
+ return _context4.stop();
368
+ }
369
+ }, _callee4, this);
370
+ }));
371
+ function startQuery(_x2, _x3) {
372
+ return _startQuery.apply(this, arguments);
373
+ }
374
+ return startQuery;
375
+ }()
376
+ /**
377
+ * Stop the search and clean any UI elements.
378
+ */
379
+ )
380
+ }, {
381
+ key: "endQuery",
382
+ value: (function () {
383
+ var _endQuery = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
384
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
385
+ while (1) switch (_context5.prev = _context5.next) {
386
+ case 0:
387
+ this.currentIndex = undefined;
388
+ this.query = null;
389
+ _context5.next = 4;
390
+ return this.editorHighlighter.endQuery();
391
+ case 4:
392
+ case "end":
393
+ return _context5.stop();
394
+ }
395
+ }, _callee5, this);
396
+ }));
397
+ function endQuery() {
398
+ return _endQuery.apply(this, arguments);
399
+ }
400
+ return endQuery;
401
+ }()
402
+ /**
403
+ * Highlight the next match.
404
+ *
405
+ * @returns The next match if there is one.
406
+ */
407
+ )
408
+ }, {
409
+ key: "highlightNext",
410
+ value: (function () {
411
+ var _highlightNext = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
412
+ var _this$cell$editor3, match;
413
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
414
+ while (1) switch (_context6.prev = _context6.next) {
415
+ case 0:
416
+ if (!(this.matchesCount === 0 || !this.isActive)) {
417
+ _context6.next = 4;
418
+ break;
419
+ }
420
+ this.currentIndex = undefined;
421
+ _context6.next = 10;
422
+ break;
423
+ case 4:
424
+ if (this.lastReplacementPosition) {
425
+ (_this$cell$editor3 = this.cell.editor) === null || _this$cell$editor3 === void 0 || _this$cell$editor3.setCursorPosition(this.lastReplacementPosition);
426
+ this.lastReplacementPosition = null;
427
+ }
428
+
429
+ // This starts from the cursor position
430
+ _context6.next = 7;
431
+ return this.editorHighlighter.highlightNext();
432
+ case 7:
433
+ match = _context6.sent;
434
+ if (match) {
435
+ this.currentIndex = this.editorHighlighter.currentIndex;
436
+ } else {
437
+ this.currentIndex = undefined;
438
+ }
439
+ return _context6.abrupt("return", match);
440
+ case 10:
441
+ return _context6.abrupt("return", Promise.resolve(this.getCurrentMatch()));
442
+ case 11:
443
+ case "end":
444
+ return _context6.stop();
445
+ }
446
+ }, _callee6, this);
447
+ }));
448
+ function highlightNext() {
449
+ return _highlightNext.apply(this, arguments);
450
+ }
451
+ return highlightNext;
452
+ }()
453
+ /**
454
+ * Highlight the previous match.
455
+ *
456
+ * @returns The previous match if there is one.
457
+ */
458
+ )
459
+ }, {
460
+ key: "highlightPrevious",
461
+ value: (function () {
462
+ var _highlightPrevious = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
463
+ var match;
464
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
465
+ while (1) switch (_context7.prev = _context7.next) {
466
+ case 0:
467
+ if (!(this.matchesCount === 0 || !this.isActive)) {
468
+ _context7.next = 4;
469
+ break;
470
+ }
471
+ this.currentIndex = undefined;
472
+ _context7.next = 9;
473
+ break;
474
+ case 4:
475
+ _context7.next = 6;
476
+ return this.editorHighlighter.highlightPrevious();
477
+ case 6:
478
+ match = _context7.sent;
479
+ if (match) {
480
+ this.currentIndex = this.editorHighlighter.currentIndex;
481
+ } else {
482
+ this.currentIndex = undefined;
483
+ }
484
+ return _context7.abrupt("return", match);
485
+ case 9:
486
+ return _context7.abrupt("return", Promise.resolve(this.getCurrentMatch()));
487
+ case 10:
488
+ case "end":
489
+ return _context7.stop();
490
+ }
491
+ }, _callee7, this);
492
+ }));
493
+ function highlightPrevious() {
494
+ return _highlightPrevious.apply(this, arguments);
495
+ }
496
+ return highlightPrevious;
497
+ }()
498
+ /**
499
+ * Replace the currently selected match with the provided text.
500
+ *
501
+ * If no match is selected, it won't do anything.
502
+ *
503
+ * @param newText The replacement text.
504
+ * @returns Whether a replace occurred.
505
+ */
506
+ )
507
+ }, {
508
+ key: "replaceCurrentMatch",
509
+ value: function replaceCurrentMatch(newText) {
510
+ if (!this.isActive) {
511
+ return Promise.resolve(false);
512
+ }
513
+ var occurred = false;
514
+ if (this.currentIndex !== undefined && this.currentIndex < this.editorHighlighter.matches.length) {
515
+ var editor = this.cell.editor;
516
+ var selection = editor === null || editor === void 0 ? void 0 : editor.getSelectionValue();
517
+ var match = this.getCurrentMatch();
518
+ if (!match) {
519
+ return Promise.resolve(occurred);
520
+ }
521
+ // If cursor is not on a selection, highlight the next match
522
+ if (selection !== (match === null || match === void 0 ? void 0 : match.text)) {
523
+ this.currentIndex = undefined;
524
+ // The next will be highlighted as a consequence of this returning false
525
+ } else {
526
+ var _editor$getCursorPosi;
527
+ this.editorHighlighter.matches.splice(this.currentIndex, 1);
528
+ this.currentIndex = undefined;
529
+ // Store the current position to highlight properly the next search hit
530
+ this.lastReplacementPosition = (_editor$getCursorPosi = editor === null || editor === void 0 ? void 0 : editor.getCursorPosition()) !== null && _editor$getCursorPosi !== void 0 ? _editor$getCursorPosi : null;
531
+ editor === null || editor === void 0 || editor.replaceSelection(newText, {
532
+ start: editor.getPositionAt(match.position),
533
+ end: editor.getPositionAt(match.position + match.text.length)
534
+ });
535
+ occurred = true;
536
+ }
537
+ }
538
+ return Promise.resolve(occurred);
539
+ }
540
+ }, {
541
+ key: "getCurrentMatch",
542
+ value:
543
+ /**
544
+ * Get the current match if it exists.
545
+ *
546
+ * @returns The current match
547
+ */
548
+ function getCurrentMatch() {
549
+ if (this.currentIndex === undefined) {
550
+ return undefined;
551
+ } else {
552
+ var match = undefined;
553
+ if (this.currentIndex < this.editorHighlighter.matches.length) {
554
+ match = this.editorHighlighter.matches[this.currentIndex];
555
+ }
556
+ return match;
557
+ }
558
+ }
559
+ }]);
560
+ return CodeEditorCellSearchProvider;
561
+ }(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "editorHighlighter", [_dec2], {
562
+ configurable: true,
563
+ enumerable: true,
564
+ writable: true,
565
+ initializer: null
566
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "currentIndex", [_dec3], {
567
+ configurable: true,
568
+ enumerable: true,
569
+ writable: true,
570
+ initializer: function initializer() {
571
+ return undefined;
572
+ }
573
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "filters", [_dec4], {
574
+ configurable: true,
575
+ enumerable: true,
576
+ writable: true,
577
+ initializer: null
578
+ })), _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);