@matdata/yasqe 4.7.4 → 4.8.0

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 (50) hide show
  1. package/build/ts/grammar/tokenizer.js +560 -0
  2. package/build/ts/grammar/tokenizer.js.map +1 -0
  3. package/build/ts/src/CodeMirror.d.ts +13 -4
  4. package/build/ts/src/CodeMirror.js +20 -0
  5. package/build/ts/src/CodeMirror.js.map +1 -0
  6. package/build/ts/src/autocompleters/classes.js +34 -0
  7. package/build/ts/src/autocompleters/classes.js.map +1 -0
  8. package/build/ts/src/autocompleters/index.d.ts +1 -0
  9. package/build/ts/src/autocompleters/index.js +255 -0
  10. package/build/ts/src/autocompleters/index.js.map +1 -0
  11. package/build/ts/src/autocompleters/prefixes.js +100 -0
  12. package/build/ts/src/autocompleters/prefixes.js.map +1 -0
  13. package/build/ts/src/autocompleters/properties.js +28 -0
  14. package/build/ts/src/autocompleters/properties.js.map +1 -0
  15. package/build/ts/src/autocompleters/variables.js +45 -0
  16. package/build/ts/src/autocompleters/variables.js.map +1 -0
  17. package/build/ts/src/defaults.d.ts +19 -16
  18. package/build/ts/src/defaults.js +125 -0
  19. package/build/ts/src/defaults.js.map +1 -0
  20. package/build/ts/src/imgs.js +8 -0
  21. package/build/ts/src/imgs.js.map +1 -0
  22. package/build/ts/src/index.d.ts +19 -15
  23. package/build/ts/src/index.js +775 -0
  24. package/build/ts/src/index.js.map +1 -0
  25. package/build/ts/src/prefixFold.js +85 -0
  26. package/build/ts/src/prefixFold.js.map +1 -0
  27. package/build/ts/src/prefixUtils.js +47 -0
  28. package/build/ts/src/prefixUtils.js.map +1 -0
  29. package/build/ts/src/sparql.js +182 -0
  30. package/build/ts/src/sparql.js.map +1 -0
  31. package/build/ts/src/tokenUtils.js +98 -0
  32. package/build/ts/src/tokenUtils.js.map +1 -0
  33. package/build/ts/src/tooltip.js +19 -0
  34. package/build/ts/src/tooltip.js.map +1 -0
  35. package/build/ts/src/trie.js +144 -0
  36. package/build/ts/src/trie.js.map +1 -0
  37. package/build/yasqe.min.css +2 -2
  38. package/build/yasqe.min.css.map +7 -1
  39. package/build/yasqe.min.js +69 -3
  40. package/build/yasqe.min.js.map +7 -1
  41. package/grammar/_tokenizer-table.js +4735 -4737
  42. package/grammar/tokenizer.ts +1 -1
  43. package/package.json +1 -1
  44. package/src/CodeMirror.ts +14 -14
  45. package/src/autocompleters/index.ts +4 -4
  46. package/src/autocompleters/show-hint.scss +1 -1
  47. package/src/defaults.ts +2 -2
  48. package/src/index.ts +2 -2
  49. package/build/yasqe.html +0 -108
  50. package/build/yasqe.min.js.LICENSE.txt +0 -3
@@ -0,0 +1,775 @@
1
+ import "./scss/yasqe.scss";
2
+ import "./scss/buttons.scss";
3
+ import { findFirstPrefixLine } from "./prefixFold";
4
+ import { getPrefixesFromQuery, addPrefixes, removePrefixes } from "./prefixUtils";
5
+ import { getPreviousNonWsToken, getNextNonWsToken, getCompleteToken } from "./tokenUtils";
6
+ import { Storage as YStorage } from "@matdata/yasgui-utils";
7
+ import * as queryString from "query-string";
8
+ import tooltip from "./tooltip";
9
+ import { drawSvgStringAsElement, addClass, removeClass } from "@matdata/yasgui-utils";
10
+ import * as Sparql from "./sparql";
11
+ import * as imgs from "./imgs";
12
+ import * as Autocompleter from "./autocompleters";
13
+ import { merge, escape } from "lodash-es";
14
+ import getDefaults from "./defaults";
15
+ import CodeMirror from "./CodeMirror";
16
+ export class Yasqe extends CodeMirror {
17
+ constructor(parent, conf = {}) {
18
+ super();
19
+ this.autocompleters = {};
20
+ this.prevQueryValid = false;
21
+ this.queryValid = true;
22
+ this.isFullscreen = false;
23
+ this.handleHashChange = () => {
24
+ var _a, _b;
25
+ (_b = (_a = this.config).consumeShareLink) === null || _b === void 0 ? void 0 : _b.call(_a, this);
26
+ };
27
+ this.notificationEls = {};
28
+ if (!parent)
29
+ throw new Error("No parent passed as argument. Dont know where to draw YASQE");
30
+ this.rootEl = document.createElement("div");
31
+ this.rootEl.className = "yasqe";
32
+ parent.appendChild(this.rootEl);
33
+ this.config = merge({}, Yasqe.defaults, conf);
34
+ const cm = CodeMirror(this.rootEl, this.config);
35
+ for (const key of Object.getOwnPropertyNames(Yasqe.prototype)) {
36
+ cm[key] = Yasqe.prototype[key].bind(this);
37
+ }
38
+ Object.assign(this, CodeMirror.prototype, cm);
39
+ this.storage = new YStorage(Yasqe.storageNamespace);
40
+ this.drawButtons();
41
+ const storageId = this.getStorageId();
42
+ if (storageId) {
43
+ const persConf = this.storage.get(storageId);
44
+ if (persConf && typeof persConf === "string") {
45
+ this.persistentConfig = { query: persConf, editorHeight: this.config.editorHeight };
46
+ }
47
+ else {
48
+ this.persistentConfig = persConf;
49
+ }
50
+ if (!this.persistentConfig)
51
+ this.persistentConfig = { query: this.getValue(), editorHeight: this.config.editorHeight };
52
+ if (this.persistentConfig && this.persistentConfig.query)
53
+ this.setValue(this.persistentConfig.query);
54
+ }
55
+ this.config.autocompleters.forEach((c) => this.enableCompleter(c).then(() => { }, console.warn));
56
+ if (this.config.consumeShareLink) {
57
+ this.config.consumeShareLink(this);
58
+ window.addEventListener("hashchange", this.handleHashChange);
59
+ }
60
+ this.checkSyntax();
61
+ if (this.persistentConfig && this.persistentConfig.editorHeight) {
62
+ this.getWrapperElement().style.height = this.persistentConfig.editorHeight;
63
+ }
64
+ else if (this.config.editorHeight) {
65
+ this.getWrapperElement().style.height = this.config.editorHeight;
66
+ }
67
+ if (this.config.resizeable)
68
+ this.drawResizer();
69
+ if (this.config.collapsePrefixesOnLoad)
70
+ this.collapsePrefixes(true);
71
+ this.registerEventListeners();
72
+ }
73
+ handleChange() {
74
+ this.checkSyntax();
75
+ this.updateQueryButton();
76
+ }
77
+ handleBlur() {
78
+ this.saveQuery();
79
+ }
80
+ handleChanges() {
81
+ this.checkSyntax();
82
+ this.updateQueryButton();
83
+ }
84
+ handleCursorActivity() {
85
+ this.autocomplete(true);
86
+ }
87
+ handleQuery(_yasqe, req, abortController) {
88
+ this.req = req;
89
+ this.abortController = abortController;
90
+ this.updateQueryButton();
91
+ }
92
+ handleQueryResponse(_yasqe, _response, duration) {
93
+ this.lastQueryDuration = duration;
94
+ this.req = undefined;
95
+ this.updateQueryButton();
96
+ }
97
+ handleQueryAbort(_yasqe, _req) {
98
+ this.req = undefined;
99
+ this.updateQueryButton();
100
+ }
101
+ registerEventListeners() {
102
+ this.on("change", this.handleChange);
103
+ this.on("blur", this.handleBlur);
104
+ this.on("changes", this.handleChanges);
105
+ this.on("cursorActivity", this.handleCursorActivity);
106
+ this.on("query", this.handleQuery);
107
+ this.on("queryResponse", this.handleQueryResponse);
108
+ this.on("queryAbort", this.handleQueryAbort);
109
+ }
110
+ unregisterEventListeners() {
111
+ this.off("change", this.handleChange);
112
+ this.off("blur", this.handleBlur);
113
+ this.off("changes", this.handleChanges);
114
+ this.off("cursorActivity", this.handleCursorActivity);
115
+ this.off("query", this.handleQuery);
116
+ this.off("queryResponse", this.handleQueryResponse);
117
+ this.off("queryAbort", this.handleQueryAbort);
118
+ }
119
+ emit(event, ...data) {
120
+ CodeMirror.signal(this, event, this, ...data);
121
+ }
122
+ getStorageId(getter) {
123
+ const persistenceId = getter || this.config.persistenceId;
124
+ if (!persistenceId)
125
+ return undefined;
126
+ if (typeof persistenceId === "string")
127
+ return persistenceId;
128
+ return persistenceId(this);
129
+ }
130
+ drawButtons() {
131
+ const buttons = document.createElement("div");
132
+ buttons.className = "yasqe_buttons";
133
+ this.getWrapperElement().appendChild(buttons);
134
+ if (this.config.pluginButtons) {
135
+ const pluginButtons = this.config.pluginButtons();
136
+ if (!pluginButtons)
137
+ return;
138
+ if (Array.isArray(pluginButtons)) {
139
+ for (const button of pluginButtons) {
140
+ buttons.append(button);
141
+ }
142
+ }
143
+ else {
144
+ buttons.appendChild(pluginButtons);
145
+ }
146
+ }
147
+ if (this.config.createShareableLink) {
148
+ var svgShare = drawSvgStringAsElement(imgs.share);
149
+ const shareLinkWrapper = document.createElement("button");
150
+ shareLinkWrapper.className = "yasqe_share";
151
+ shareLinkWrapper.title = "Share query";
152
+ shareLinkWrapper.setAttribute("aria-label", "Share query");
153
+ shareLinkWrapper.appendChild(svgShare);
154
+ buttons.appendChild(shareLinkWrapper);
155
+ shareLinkWrapper.addEventListener("click", (event) => showSharePopup(event));
156
+ shareLinkWrapper.addEventListener("keydown", (event) => {
157
+ if (event.code === "Enter") {
158
+ showSharePopup(event);
159
+ }
160
+ });
161
+ const showSharePopup = (event) => {
162
+ event.stopPropagation();
163
+ let popup = document.createElement("div");
164
+ popup.className = "yasqe_sharePopup";
165
+ buttons.appendChild(popup);
166
+ document.body.addEventListener("click", (event) => {
167
+ if (popup && event.target !== popup && !popup.contains(event.target)) {
168
+ popup.remove();
169
+ popup = undefined;
170
+ }
171
+ }, true);
172
+ var input = document.createElement("input");
173
+ input.type = "text";
174
+ input.value = this.config.createShareableLink(this);
175
+ input.onfocus = function () {
176
+ input.select();
177
+ };
178
+ input.onmouseup = function () {
179
+ return false;
180
+ };
181
+ popup.innerHTML = "";
182
+ var inputWrapper = document.createElement("div");
183
+ inputWrapper.className = "inputWrapper";
184
+ inputWrapper.appendChild(input);
185
+ popup.appendChild(inputWrapper);
186
+ const popupInputButtons = [];
187
+ const createShortLink = this.config.createShortLink;
188
+ if (createShortLink) {
189
+ popup.className = popup.className += " enableShort";
190
+ const shortBtn = document.createElement("button");
191
+ popupInputButtons.push(shortBtn);
192
+ shortBtn.innerHTML = "Shorten";
193
+ shortBtn.className = "yasqe_btn yasqe_btn-sm shorten";
194
+ popup.appendChild(shortBtn);
195
+ shortBtn.onclick = () => {
196
+ popupInputButtons.forEach((button) => (button.disabled = true));
197
+ createShortLink(this, input.value).then((value) => {
198
+ input.value = value;
199
+ input.focus();
200
+ }, (err) => {
201
+ const errSpan = document.createElement("span");
202
+ errSpan.className = "shortlinkErr";
203
+ let textContent = "An error has occurred";
204
+ if (typeof err === "string" && err.length !== 0) {
205
+ textContent = err;
206
+ }
207
+ else if (err.message && err.message.length !== 0) {
208
+ textContent = err.message;
209
+ }
210
+ errSpan.textContent = textContent;
211
+ input.replaceWith(errSpan);
212
+ });
213
+ };
214
+ }
215
+ const curlBtn = document.createElement("button");
216
+ popupInputButtons.push(curlBtn);
217
+ curlBtn.innerText = "cURL";
218
+ curlBtn.className = "yasqe_btn yasqe_btn-sm curl";
219
+ popup.appendChild(curlBtn);
220
+ curlBtn.onclick = () => {
221
+ popupInputButtons.forEach((button) => (button.disabled = true));
222
+ input.value = this.getAsCurlString();
223
+ input.focus();
224
+ popup === null || popup === void 0 ? void 0 : popup.appendChild(curlBtn);
225
+ };
226
+ const svgPos = svgShare.getBoundingClientRect();
227
+ popup.style.top = svgShare.offsetTop + svgPos.height + "px";
228
+ popup.style.left = svgShare.offsetLeft + svgShare.clientWidth - popup.clientWidth + "px";
229
+ input.focus();
230
+ };
231
+ }
232
+ if (this.config.showQueryButton) {
233
+ this.queryBtn = document.createElement("button");
234
+ addClass(this.queryBtn, "yasqe_queryButton");
235
+ const queryEl = drawSvgStringAsElement(imgs.query);
236
+ addClass(queryEl, "queryIcon");
237
+ this.queryBtn.appendChild(queryEl);
238
+ const warningIcon = drawSvgStringAsElement(imgs.warning);
239
+ addClass(warningIcon, "warningIcon");
240
+ this.queryBtn.appendChild(warningIcon);
241
+ this.queryBtn.onclick = () => {
242
+ if (this.config.queryingDisabled)
243
+ return;
244
+ if (this.req) {
245
+ this.abortQuery();
246
+ }
247
+ else {
248
+ this.query().catch(() => { });
249
+ }
250
+ };
251
+ this.queryBtn.title = "Run query";
252
+ this.queryBtn.setAttribute("aria-label", "Run query");
253
+ buttons.appendChild(this.queryBtn);
254
+ this.updateQueryButton();
255
+ }
256
+ this.fullscreenBtn = document.createElement("button");
257
+ addClass(this.fullscreenBtn, "yasqe_fullscreenButton");
258
+ const fullscreenIcon = drawSvgStringAsElement(imgs.fullscreen);
259
+ addClass(fullscreenIcon, "fullscreenIcon");
260
+ this.fullscreenBtn.appendChild(fullscreenIcon);
261
+ const fullscreenExitIcon = drawSvgStringAsElement(imgs.fullscreenExit);
262
+ addClass(fullscreenExitIcon, "fullscreenExitIcon");
263
+ this.fullscreenBtn.appendChild(fullscreenExitIcon);
264
+ this.fullscreenBtn.onclick = () => {
265
+ this.toggleFullscreen();
266
+ };
267
+ this.fullscreenBtn.title = "Toggle fullscreen (F11)";
268
+ this.fullscreenBtn.setAttribute("aria-label", "Toggle fullscreen");
269
+ buttons.appendChild(this.fullscreenBtn);
270
+ }
271
+ toggleFullscreen() {
272
+ var _a, _b;
273
+ this.isFullscreen = !this.isFullscreen;
274
+ if (this.isFullscreen) {
275
+ addClass(this.rootEl, "fullscreen");
276
+ (_a = this.fullscreenBtn) === null || _a === void 0 ? void 0 : _a.setAttribute("title", "Exit fullscreen (F11)");
277
+ }
278
+ else {
279
+ removeClass(this.rootEl, "fullscreen");
280
+ (_b = this.fullscreenBtn) === null || _b === void 0 ? void 0 : _b.setAttribute("title", "Toggle fullscreen (F11)");
281
+ }
282
+ this.refresh();
283
+ }
284
+ getIsFullscreen() {
285
+ return this.isFullscreen;
286
+ }
287
+ drawResizer() {
288
+ if (this.resizeWrapper)
289
+ return;
290
+ this.resizeWrapper = document.createElement("div");
291
+ addClass(this.resizeWrapper, "resizeWrapper");
292
+ const chip = document.createElement("div");
293
+ addClass(chip, "resizeChip");
294
+ this.resizeWrapper.appendChild(chip);
295
+ this.resizeWrapper.addEventListener("mousedown", this.initDrag, false);
296
+ this.resizeWrapper.addEventListener("dblclick", this.expandEditor);
297
+ this.rootEl.appendChild(this.resizeWrapper);
298
+ }
299
+ initDrag() {
300
+ document.documentElement.addEventListener("mousemove", this.doDrag, false);
301
+ document.documentElement.addEventListener("mouseup", this.stopDrag, false);
302
+ }
303
+ calculateDragOffset(event, rootEl) {
304
+ let parentOffset = 0;
305
+ if (rootEl.offsetParent)
306
+ parentOffset = rootEl.offsetParent.offsetTop;
307
+ let scrollOffset = 0;
308
+ let parentElement = rootEl.parentElement;
309
+ while (parentElement) {
310
+ scrollOffset += parentElement.scrollTop;
311
+ parentElement = parentElement.parentElement;
312
+ }
313
+ return event.clientY - parentOffset - this.rootEl.offsetTop + scrollOffset;
314
+ }
315
+ doDrag(event) {
316
+ this.getWrapperElement().style.height = this.calculateDragOffset(event, this.rootEl) + "px";
317
+ }
318
+ stopDrag() {
319
+ document.documentElement.removeEventListener("mousemove", this.doDrag, false);
320
+ document.documentElement.removeEventListener("mouseup", this.stopDrag, false);
321
+ this.emit("resize", this.getWrapperElement().style.height);
322
+ if (this.getStorageId() && this.persistentConfig) {
323
+ this.persistentConfig.editorHeight = this.getWrapperElement().style.height;
324
+ this.saveQuery();
325
+ }
326
+ this.refresh();
327
+ }
328
+ duplicateLine() {
329
+ const cur = this.getDoc().getCursor();
330
+ if (cur) {
331
+ const line = this.getDoc().getLine(cur.line);
332
+ this.getDoc().replaceRange(line + "\n" + line, { ch: 0, line: cur.line }, { ch: line.length, line: cur.line });
333
+ }
334
+ }
335
+ updateQueryButton(status) {
336
+ if (!this.queryBtn)
337
+ return;
338
+ if (this.config.queryingDisabled) {
339
+ addClass(this.queryBtn, "query_disabled");
340
+ this.queryBtn.title = this.config.queryingDisabled;
341
+ }
342
+ else {
343
+ removeClass(this.queryBtn, "query_disabled");
344
+ this.queryBtn.title = "Run query";
345
+ this.queryBtn.setAttribute("aria-label", "Run query");
346
+ }
347
+ if (!status) {
348
+ status = this.queryValid ? "valid" : "error";
349
+ }
350
+ if (status != this.queryStatus) {
351
+ removeClass(this.queryBtn, "query_" + this.queryStatus);
352
+ addClass(this.queryBtn, "query_" + status);
353
+ this.queryStatus = status;
354
+ }
355
+ if (this.req && this.queryBtn.className.indexOf("busy") < 0) {
356
+ this.queryBtn.className = this.queryBtn.className += " busy";
357
+ }
358
+ if (!this.req && this.queryBtn.className.indexOf("busy") >= 0) {
359
+ this.queryBtn.className = this.queryBtn.className.replace("busy", "");
360
+ }
361
+ }
362
+ handleLocalStorageQuotaFull(_e) {
363
+ console.warn("Localstorage quota exceeded. Clearing all queries");
364
+ Yasqe.clearStorage();
365
+ }
366
+ saveQuery() {
367
+ const storageId = this.getStorageId();
368
+ if (!storageId || !this.persistentConfig)
369
+ return;
370
+ this.persistentConfig.query = this.getValue();
371
+ this.storage.set(storageId, this.persistentConfig, this.config.persistencyExpire, this.handleLocalStorageQuotaFull);
372
+ }
373
+ getQueryType() {
374
+ return this.getOption("queryType");
375
+ }
376
+ getQueryMode() {
377
+ switch (this.getQueryType()) {
378
+ case "INSERT":
379
+ case "DELETE":
380
+ case "LOAD":
381
+ case "CLEAR":
382
+ case "CREATE":
383
+ case "DROP":
384
+ case "COPY":
385
+ case "MOVE":
386
+ case "ADD":
387
+ return "update";
388
+ default:
389
+ return "query";
390
+ }
391
+ }
392
+ getVariablesFromQuery() {
393
+ const token = this.getTokenAt({ line: this.getDoc().lastLine(), ch: this.getDoc().getLine(this.getDoc().lastLine()).length }, true);
394
+ const vars = [];
395
+ for (var v in token.state.variables) {
396
+ vars.push(v);
397
+ }
398
+ return vars.sort();
399
+ }
400
+ autoformatSelection(start, end) {
401
+ var text = this.getValue();
402
+ text = text.substring(start, end);
403
+ return Yasqe.autoformatString(text);
404
+ }
405
+ static autoformatString(text) {
406
+ var breakAfterArray = [
407
+ ["keyword", "ws", "string-2", "ws", "variable-3"],
408
+ ["keyword", "ws", "variable-3"],
409
+ ];
410
+ var breakBeforeCharacters = ["}"];
411
+ var getBreakType = function (stringVal) {
412
+ if (stringVal === "{")
413
+ return 1;
414
+ if (stringVal === ".")
415
+ return 1;
416
+ if (stringVal === ";") {
417
+ if (stackTrace.length > 2 && stackTrace[stackTrace.length - 2] === "punc")
418
+ return 0;
419
+ return 1;
420
+ }
421
+ for (var i = 0; i < breakAfterArray.length; i++) {
422
+ if (stackTrace.valueOf().toString() === breakAfterArray[i].valueOf().toString()) {
423
+ return 1;
424
+ }
425
+ }
426
+ for (var i = 0; i < breakBeforeCharacters.length; i++) {
427
+ if (currentLine.trim() !== "" && stringVal == breakBeforeCharacters[i]) {
428
+ return -1;
429
+ }
430
+ }
431
+ return 0;
432
+ };
433
+ var formattedQuery = "";
434
+ var currentLine = "";
435
+ var stackTrace = [];
436
+ Yasqe.runMode(text, "sparql11", function (stringVal, type) {
437
+ stackTrace.push(type);
438
+ var breakType = getBreakType(stringVal);
439
+ if (breakType != 0) {
440
+ if (breakType == 1) {
441
+ formattedQuery += stringVal + "\n";
442
+ currentLine = "";
443
+ }
444
+ else {
445
+ formattedQuery += "\n" + stringVal;
446
+ currentLine = stringVal;
447
+ }
448
+ stackTrace = [];
449
+ }
450
+ else {
451
+ currentLine += stringVal;
452
+ formattedQuery += stringVal;
453
+ }
454
+ if (stackTrace.length == 1 && stackTrace[0] == "sp-ws")
455
+ stackTrace = [];
456
+ });
457
+ return formattedQuery.replace(/\n\s*\n/g, "\n").trim();
458
+ }
459
+ commentLines() {
460
+ var startLine = this.getDoc().getCursor("start").line;
461
+ var endLine = this.getDoc().getCursor("end").line;
462
+ var min = Math.min(startLine, endLine);
463
+ var max = Math.max(startLine, endLine);
464
+ var linesAreCommented = true;
465
+ for (var i = min; i <= max; i++) {
466
+ var line = this.getDoc().getLine(i);
467
+ if (line.length == 0 || line.substring(0, 1) != "#") {
468
+ linesAreCommented = false;
469
+ break;
470
+ }
471
+ }
472
+ for (var i = min; i <= max; i++) {
473
+ if (linesAreCommented) {
474
+ this.getDoc().replaceRange("", {
475
+ line: i,
476
+ ch: 0,
477
+ }, {
478
+ line: i,
479
+ ch: 1,
480
+ });
481
+ }
482
+ else {
483
+ this.getDoc().replaceRange("#", {
484
+ line: i,
485
+ ch: 0,
486
+ });
487
+ }
488
+ }
489
+ }
490
+ autoformat() {
491
+ if (!this.getDoc().somethingSelected())
492
+ this.execCommand("selectAll");
493
+ const from = this.getDoc().getCursor("start");
494
+ var to = {
495
+ line: this.getDoc().getCursor("end").line,
496
+ ch: this.getDoc().getSelection().length,
497
+ };
498
+ var absStart = this.getDoc().indexFromPos(from);
499
+ var absEnd = this.getDoc().indexFromPos(to);
500
+ const res = this.autoformatSelection(absStart, absEnd);
501
+ this.operation(() => {
502
+ this.getDoc().replaceRange(res, from, to);
503
+ var startLine = this.getDoc().posFromIndex(absStart).line;
504
+ var endLine = this.getDoc().posFromIndex(absStart + res.length).line;
505
+ for (var i = startLine; i <= endLine; i++) {
506
+ this.indentLine(i, "smart");
507
+ }
508
+ });
509
+ }
510
+ getQueryWithValues(values) {
511
+ if (!values)
512
+ return this.getValue();
513
+ var injectString;
514
+ if (typeof values === "string") {
515
+ injectString = values;
516
+ }
517
+ else {
518
+ if (!(values instanceof Array))
519
+ values = [values];
520
+ var variables = values.reduce(function (vars, valueObj) {
521
+ for (var v in valueObj) {
522
+ vars[v] = v;
523
+ }
524
+ return vars;
525
+ }, {});
526
+ var varArray = [];
527
+ for (var v in variables) {
528
+ varArray.push(v);
529
+ }
530
+ if (!varArray.length)
531
+ return this.getValue();
532
+ injectString = "VALUES (" + varArray.join(" ") + ") {\n";
533
+ values.forEach(function (valueObj) {
534
+ injectString += "( ";
535
+ varArray.forEach(function (variable) {
536
+ injectString += valueObj[variable] || "UNDEF";
537
+ });
538
+ injectString += " )\n";
539
+ });
540
+ injectString += "}\n";
541
+ }
542
+ if (!injectString)
543
+ return this.getValue();
544
+ var newQuery = "";
545
+ var injected = false;
546
+ var gotSelect = false;
547
+ Yasqe.runMode(this.getValue(), "sparql11", function (stringVal, className, _row, _col, _state) {
548
+ if (className === "keyword" && stringVal.toLowerCase() === "select")
549
+ gotSelect = true;
550
+ newQuery += stringVal;
551
+ if (gotSelect && !injected && className === "punc" && stringVal === "{") {
552
+ injected = true;
553
+ newQuery += "\n" + injectString;
554
+ }
555
+ });
556
+ return newQuery;
557
+ }
558
+ getValueWithoutComments() {
559
+ var cleanedQuery = "";
560
+ Yasqe.runMode(this.getValue(), "sparql11", function (stringVal, className) {
561
+ if (className != "comment") {
562
+ cleanedQuery += stringVal;
563
+ }
564
+ });
565
+ return cleanedQuery;
566
+ }
567
+ setCheckSyntaxErrors(isEnabled) {
568
+ this.config.syntaxErrorCheck = isEnabled;
569
+ this.checkSyntax();
570
+ }
571
+ checkSyntax() {
572
+ this.queryValid = true;
573
+ this.clearGutter("gutterErrorBar");
574
+ var state;
575
+ for (var l = 0; l < this.getDoc().lineCount(); ++l) {
576
+ var precise = false;
577
+ if (!this.prevQueryValid) {
578
+ precise = true;
579
+ }
580
+ var token = this.getTokenAt({
581
+ line: l,
582
+ ch: this.getDoc().getLine(l).length,
583
+ }, precise);
584
+ var state = token.state;
585
+ this.setOption("queryType", state.queryType);
586
+ if (state.OK == false) {
587
+ if (!this.config.syntaxErrorCheck) {
588
+ const els = this.getWrapperElement().querySelectorAll(".sp-error");
589
+ for (let i = 0; i < els.length; i++) {
590
+ var el = els[i];
591
+ if (el.style)
592
+ el.style.color = "black";
593
+ }
594
+ return;
595
+ }
596
+ const warningEl = drawSvgStringAsElement(imgs.warning);
597
+ if (state.errorMsg) {
598
+ tooltip(this, warningEl, escape(token.state.errorMsg));
599
+ }
600
+ else if (state.possibleCurrent && state.possibleCurrent.length > 0) {
601
+ var expectedEncoded = [];
602
+ state.possibleCurrent.forEach(function (expected) {
603
+ expectedEncoded.push("<strong style='text-decoration:underline'>" + escape(expected) + "</strong>");
604
+ });
605
+ tooltip(this, warningEl, "This line is invalid. Expected: " + expectedEncoded.join(", "));
606
+ }
607
+ warningEl.className = "parseErrorIcon";
608
+ this.setGutterMarker(l, "gutterErrorBar", warningEl);
609
+ this.queryValid = false;
610
+ break;
611
+ }
612
+ }
613
+ }
614
+ getCompleteToken(token, cur) {
615
+ return getCompleteToken(this, token, cur);
616
+ }
617
+ getPreviousNonWsToken(line, token) {
618
+ return getPreviousNonWsToken(this, line, token);
619
+ }
620
+ getNextNonWsToken(lineNumber, charNumber) {
621
+ return getNextNonWsToken(this, lineNumber, charNumber);
622
+ }
623
+ showNotification(key, message) {
624
+ if (!this.notificationEls[key]) {
625
+ const notificationContainer = document.createElement("div");
626
+ addClass(notificationContainer, "notificationContainer");
627
+ this.getWrapperElement().appendChild(notificationContainer);
628
+ this.notificationEls[key] = document.createElement("div");
629
+ addClass(this.notificationEls[key], "notification", "notif_" + key);
630
+ notificationContainer.appendChild(this.notificationEls[key]);
631
+ }
632
+ for (const notificationId in this.notificationEls) {
633
+ if (notificationId !== key)
634
+ this.hideNotification(notificationId);
635
+ }
636
+ const el = this.notificationEls[key];
637
+ addClass(el, "active");
638
+ el.innerText = message;
639
+ }
640
+ hideNotification(key) {
641
+ if (this.notificationEls[key]) {
642
+ removeClass(this.notificationEls[key], "active");
643
+ }
644
+ }
645
+ enableCompleter(name) {
646
+ if (!Yasqe.Autocompleters[name])
647
+ return Promise.reject(new Error("Autocompleter " + name + " is not a registered autocompleter"));
648
+ if (this.config.autocompleters.indexOf(name) < 0)
649
+ this.config.autocompleters.push(name);
650
+ const autocompleter = (this.autocompleters[name] = new Autocompleter.Completer(this, Yasqe.Autocompleters[name]));
651
+ return autocompleter.initialize();
652
+ }
653
+ disableCompleter(name) {
654
+ this.config.autocompleters = this.config.autocompleters.filter((a) => a !== name);
655
+ this.autocompleters[name] = undefined;
656
+ }
657
+ autocomplete(fromAutoShow = false) {
658
+ if (this.getDoc().somethingSelected())
659
+ return;
660
+ for (let i in this.config.autocompleters) {
661
+ const autocompleter = this.autocompleters[this.config.autocompleters[i]];
662
+ if (!autocompleter || !autocompleter.autocomplete(fromAutoShow))
663
+ continue;
664
+ }
665
+ }
666
+ collapsePrefixes(collapse = true) {
667
+ const firstPrefixLine = findFirstPrefixLine(this);
668
+ if (firstPrefixLine === undefined)
669
+ return;
670
+ this.foldCode(firstPrefixLine, CodeMirror.fold.prefix, collapse ? "fold" : "unfold");
671
+ }
672
+ getPrefixesFromQuery() {
673
+ return getPrefixesFromQuery(this);
674
+ }
675
+ addPrefixes(prefixes) {
676
+ return addPrefixes(this, prefixes);
677
+ }
678
+ removePrefixes(prefixes) {
679
+ return removePrefixes(this, prefixes);
680
+ }
681
+ updateWidget() {
682
+ if (this.cursorCoords &&
683
+ this.state.completionActive &&
684
+ this.state.completionActive.widget) {
685
+ const newTop = this.cursorCoords(null).bottom;
686
+ this.state.completionActive.widget.hints.style.top = newTop + "px";
687
+ }
688
+ }
689
+ query(config) {
690
+ if (this.config.queryingDisabled)
691
+ return Promise.reject("Querying is disabled.");
692
+ this.abortQuery();
693
+ return Sparql.executeQuery(this, config);
694
+ }
695
+ getUrlParams() {
696
+ let urlParams = {};
697
+ if (window.location.hash.length > 1) {
698
+ urlParams = queryString.parse(location.hash);
699
+ }
700
+ if ((!urlParams || !("query" in urlParams)) && window.location.search.length > 1) {
701
+ urlParams = queryString.parse(window.location.search);
702
+ }
703
+ return urlParams;
704
+ }
705
+ configToQueryParams() {
706
+ var urlParams = {};
707
+ if (window.location.hash.length > 1)
708
+ urlParams = queryString.parse(window.location.hash);
709
+ urlParams["query"] = this.getValue();
710
+ return urlParams;
711
+ }
712
+ queryParamsToConfig(params) {
713
+ if (params && params.query && typeof params.query === "string") {
714
+ this.setValue(params.query);
715
+ }
716
+ }
717
+ getAsCurlString(config) {
718
+ return Sparql.getAsCurlString(this, config);
719
+ }
720
+ abortQuery() {
721
+ if (this.req) {
722
+ if (this.abortController) {
723
+ this.abortController.abort();
724
+ }
725
+ this.emit("queryAbort", this, this.req);
726
+ }
727
+ }
728
+ expandEditor() {
729
+ this.setSize(null, "100%");
730
+ }
731
+ destroy() {
732
+ var _a, _b;
733
+ this.abortQuery();
734
+ this.unregisterEventListeners();
735
+ (_a = this.resizeWrapper) === null || _a === void 0 ? void 0 : _a.removeEventListener("mousedown", this.initDrag, false);
736
+ (_b = this.resizeWrapper) === null || _b === void 0 ? void 0 : _b.removeEventListener("dblclick", this.expandEditor);
737
+ for (const autocompleter in this.autocompleters) {
738
+ this.disableCompleter(autocompleter);
739
+ }
740
+ window.removeEventListener("hashchange", this.handleHashChange);
741
+ this.rootEl.remove();
742
+ }
743
+ static clearStorage() {
744
+ const storage = new YStorage(Yasqe.storageNamespace);
745
+ storage.removeNamespace();
746
+ }
747
+ static registerAutocompleter(value, enable = true) {
748
+ const name = value.name;
749
+ Yasqe.Autocompleters[name] = value;
750
+ if (enable && Yasqe.defaults.autocompleters.indexOf(name) < 0)
751
+ Yasqe.defaults.autocompleters.push(name);
752
+ }
753
+ static forkAutocompleter(fromCompleter, newCompleter, enable = true) {
754
+ if (!Yasqe.Autocompleters[fromCompleter])
755
+ throw new Error("Autocompleter " + fromCompleter + " does not exist");
756
+ if (!(newCompleter === null || newCompleter === void 0 ? void 0 : newCompleter.name)) {
757
+ throw new Error("Expected a name for newly registered autocompleter");
758
+ }
759
+ const name = newCompleter.name;
760
+ Yasqe.Autocompleters[name] = Object.assign(Object.assign({}, Yasqe.Autocompleters[fromCompleter]), newCompleter);
761
+ if (enable && Yasqe.defaults.autocompleters.indexOf(name) < 0)
762
+ Yasqe.defaults.autocompleters.push(name);
763
+ }
764
+ }
765
+ Yasqe.storageNamespace = "triply";
766
+ Yasqe.Sparql = Sparql;
767
+ Yasqe.runMode = CodeMirror.runMode;
768
+ Yasqe.Autocompleters = {};
769
+ Yasqe.defaults = getDefaults();
770
+ Object.assign(CodeMirror.prototype, Yasqe.prototype);
771
+ Autocompleter.completers.forEach((c) => {
772
+ Yasqe.registerAutocompleter(c);
773
+ });
774
+ export default Yasqe;
775
+ //# sourceMappingURL=index.js.map