-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathMacroRunner.js
More file actions
800 lines (738 loc) · 33.7 KB
/
MacroRunner.js
File metadata and controls
800 lines (738 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global path, jsPromise*/
/**
* Utilities functions for running macros.
* Eg:
* await __PR.openFile("a.html");
* __PR.setCursors(["17:28", "17:28-17:30"])
* __PR.expectCursorsToBe(["17:28", "17:28-17:30"])
* __PR.keydown(["BACK_SPACE"])
* __PR.typeAtCursor("hello")
* __PR.validateText(`a`, "16:14-16:15")
* __PR.validateAllMarks("startTagSyncEdit", ["16:14-16:15"]); // All marks of type startTagSyncEdit should be there
* __PR.validateMarks("startTagSyncEdit", ["16:14-16:15"], 1); // 1 is total marks of type startTagSyncEdit
*
* This can be later extended to run macros. But since this uses eval, the
* security posture must be changed. One way is to:
* 1. create an iframe that contains the macro panel and codemirror surface in a sandboxed or 3rd party context. This
* will create origin isolation in browser so that extensions cannot read or write to the ifrmae macro code.
* 2. The iframe should be created in an extensions and once created, only that iframe should be tested to run evaled
* code. So the iframe will post message with code to eval and we will only eval that.
* 3. The iframe can request to save data to eval which we need to carefully handle.
* 4. Now this is a problem only when we securely sandbox extensions in the future, as for now an extension can run
* eval itself and pretty much all of this is no-op till we have extension sandbox. So this is not the security
* model now.
*/
define(function (require, exports, module) {
const FileViewController = brackets.getModule("project/FileViewController"),
CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
KeyEvent = brackets.getModule("utils/KeyEvent"),
Commands = brackets.getModule("command/Commands"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
MainViewManager = brackets.getModule("view/MainViewManager"),
FileUtils = brackets.getModule("file/FileUtils"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Editor = brackets.getModule("editor/Editor"),
Dialogs = brackets.getModule("widgets/Dialogs"),
_ = brackets.getModule("thirdparty/lodash"),
ProjectManager = brackets.getModule("project/ProjectManager");
/**
* Open a project relative file or absolute file path. if no leading slash, path is assumed to be project relative
* @param filePath
* @returns {Promise<null>}
*/
function openFile(filePath) {
if(filePath.startsWith('/')) {
return jsPromise(FileViewController.openFileAndAddToWorkingSet(filePath));
}
const projectFilePath = path.join(ProjectManager.getProjectRoot().fullPath, filePath);
return jsPromise(FileViewController.openFileAndAddToWorkingSet(projectFilePath));
}
/**
* Reads a text file and returns a promise that resolves to the text
* @param filePath - project relative or full path
* @param {boolean?} bypassCache - an optional argument, if specified will read from disc instead of using cache.
* @returns {Promise<String>}
*/
function readTextFile(filePath, bypassCache) {
if(!filePath.startsWith('/')) {
filePath = path.join(ProjectManager.getProjectRoot().fullPath, filePath);
}
const file = FileSystem.getFileForPath(filePath);
return jsPromise(FileUtils.readAsText(file, bypassCache));
}
/**
* Asynchronously writes a file as UTF-8 encoded text.
* @param filePath - project relative or full path
* @param {String} text
* @param {boolean} allowBlindWrite Indicates whether or not CONTENTS_MODIFIED
* errors---which can be triggered if the actual file contents differ from
* the FileSystem's last-known contents---should be ignored.
* @return {Promise<null>} promise that will be resolved when
* file writing completes, or rejected with a FileSystemError string constant.
*/
function writeTextFile(filePath, text, allowBlindWrite) {
if(!filePath.startsWith('/')) {
filePath = path.join(ProjectManager.getProjectRoot().fullPath, filePath);
}
const file = FileSystem.getFileForPath(filePath);
return jsPromise(FileUtils.writeText(file, text, allowBlindWrite));
}
/**
* deletes a file or dir at given path
* @param filePath - project relative or full path
* @return {Promise<null>} promise that will be resolved when path removed
*/
function deletePath(filePath) {
if(!filePath.startsWith('/')) {
filePath = path.join(ProjectManager.getProjectRoot().fullPath, filePath);
}
return new Promise((resolve, reject) => {
window.fs.unlink(filePath, (err)=>{
if (err) {
reject(err);
return;
}
resolve();
});
});
}
/**
* Set cursor positions or text selections in the active CodeMirror editor based on a specified format.
* The input should be an array of strings where each string can denote a cursor position ("line:char")
* or a text selection range ("line:char-line:char"). For a selection, the first part is the anchor and
* the second is the head of the selection.
*
* Example usage: ["1:2", "2:2-3:4"]
*
* @param {Array<string>} selections - An array of strings defining cursor positions or selection ranges.
* @throws {Error} Throws an error if no active editor is found or if there are parsing issues with the input.
*/
function setCursors(selections) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found to set cursor at: ${selections}`);
}
// Parse the selection strings to CodeMirror positions
const parsedSelections = selections.map(selection => {
const parts = selection.split('-');
if (parts.length === 1) {
const [line, ch] = parts[0].split(':').map(Number);
if (isNaN(line) || isNaN(ch)) {
throw new Error(`Invalid cursor format: ${parts[0]} for ${selections}`);
}
return {start: {line: line - 1, ch: ch - 1}, end: {line: line - 1, ch: ch - 1}};
} else if (parts.length === 2) {
const [fromLine, fromCh] = parts[0].split(':').map(Number);
const [toLine, toCh] = parts[1].split(':').map(Number);
if (isNaN(fromLine) || isNaN(fromCh) || isNaN(toLine) || isNaN(toCh)) {
throw new Error(`Invalid selection range format: ${selection}`);
}
return {start: {line: fromLine - 1, ch: fromCh -1}, end: {line: toLine - 1, ch: toCh - 1}};
} else {
throw new Error(`Invalid format: ${selection}`);
}
});
// Set the selections in the editor
activeEditor.setSelections(parsedSelections);
}
/**
* gets cursor selections array that can be used in the setCursors API
* @param editor
* @returns {*}
*/
function computeCursors(editor, addQuotes) {
const selections = editor.getSelections();
return selections.map(selection => {
const start = selection.start;
const end = selection.end;
let cursor;
// Check if the selection is a cursor (start and end are the same)
if (start.line === end.line && start.ch === end.ch) {
cursor = `${start.line + 1}:${start.ch + 1}`;
} else {
cursor = `${start.line + 1}:${start.ch + 1}-${end.line + 1}:${end.ch + 1}`;
}
return addQuotes ? `"${cursor}"` : cursor;
});
}
/**
* Validates the currently active editor has selections as given here
*/
function expectCursorsToBe(expectedSelections) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found for expectCursorsToBe: ${expectedSelections}`);
}
const currentSelections = computeCursors(activeEditor);
if(currentSelections.length !== expectedSelections.length) {
throw new Error(`expectCursorsToBe: [${expectedSelections.join(", ")}] `+
`but got [${currentSelections.join(", ")}]`);
}
for(let i = 0; i < currentSelections.length; i++) {
if(!currentSelections.includes(`${expectedSelections[i]}`) ||
!expectedSelections.includes(currentSelections[i])){
throw new Error(`expectCursorsToBe: [${expectedSelections.join(", ")}] `+
`but got [${currentSelections.join(", ")}]`);
}
}
}
/**
* Simulate a key event.
* @param {Number} key Key code available as One of the KeyEvent.DOM_VK_*
* @param {String} event Key event to simulate. one of keydown, keyup or keypress
* @param {HTMLElement} element Element to receive event
* @param {KeyboardEventInit} options Optional arguments for key event
*/
function raiseKeyEvent(key, event, element, options) {
const doc = element.ownerDocument;
if(typeof options === 'undefined') {
options = {
view: doc.defaultView,
bubbles: true,
cancelable: true,
keyIdentifer: key
};
} else {
options.view = doc.defaultView;
options.bubbles = true;
options.cancelable = true;
options.keyIdentifier = key;
}
const oEvent = new KeyboardEvent(event, options);
if (event !== "keydown" && event !== "keyup" && event !== "keypress") {
console.log("SpecRunnerUtils.simulateKeyEvent() - unsupported keyevent: " + event);
return;
}
// Chromium Hack: need to override the 'which' property.
// Note: this code is not designed to work in IE, Safari,
// or other browsers. Well, maybe with Firefox. YMMV.
Object.defineProperty(oEvent, 'keyCode', {
get: function () {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'which', {
get: function () {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'charCode', {
get: function () {
return this.keyCodeVal;
}
});
oEvent.keyCodeVal = key;
if (oEvent.keyCode !== key) {
console.log("SpecRunnerUtils.simulateKeyEvent() - keyCode mismatch: " + oEvent.keyCode);
}
element.dispatchEvent(oEvent);
}
/**
* @param {Array<string>} keysArray An array of Key strings available as One of the KeyEvent.DOM_VK_* without the
* `KeyEvent.DOM_VK_` prefix. Eg: use `["ESCAPE"]` instead of fully specifying [`DOM_VK_ESCAPE`]
* E.g: __PR.keydown(["BACK_SPACE"]) or __PR.keydown(["BACK_SPACE"], {ctrlKey: true})
* @param {object} modifiers to modify the key
* @param {boolean} modifiers.ctrlKey
* @param {boolean} modifiers.altKey
* @param {boolean} modifiers.shiftKey
* @param {boolean} modifiers.metaKey
* @param keysArray
*/
function keydown(keysArray, modifiers) {
for(let key of keysArray) {
if(typeof key === "string"){
if(!key.startsWith("DOM_VK_")){
key = "DOM_VK_"+key;
}
key = KeyEvent[key];
if(!key){
throw new Error(`Invalid key "${key}"`);
}
}
raiseKeyEvent(key, "keydown", document.activeElement, modifiers);
}
}
function typeAtCursor(text, origin) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found to typeAtCursor: ${text}`);
}
const selections = activeEditor.getSelections();
// Insert text at each cursor or the head of each selection.
// We perform the insertions in reverse order to avoid affecting the indices of subsequent insertions.
for (let selection of selections) {
activeEditor.replaceRange(text, selection.start, selection.end, origin);
}
}
// converts string of from "ln:ch" to pos object
function _toPos(posString) {
const pos = posString.split(":");
return {line: Number(pos[0]) - 1, ch: Number(pos[1]) - 1 };
}
/**
* Verify if the given text is same as what is in between the given selection.
* @param {string} text
* @param {string} selection of the form "ln:ch-ln:ch"
*/
function validateText(text, selection) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found to validateText: ${text} at selection ${selection}`);
}
const from = selection.split("-")[0], to = selection.split("-")[1];
const selectedText = activeEditor.getTextBetween(_toPos(from), _toPos(to));
if(selectedText !== text){
throw new Error(`validateText: expected text at [${selection}] to be "${text}" but got "${selectedText}"`);
}
}
function _getMarkLocations(markType, whichAPI, selections) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found to ${whichAPI}: "${markType}" for selection "${selections}"`);
}
const marks = activeEditor.getAllMarks(markType);
const marksLocations = [];
for(let mark of marks){
const loc = mark.find();
marksLocations.push(`${loc.from.line+1}:${loc.from.ch+1}-${loc.to.line+1}:${loc.to.ch+1}`);
}
return marksLocations;
}
/**
* validates all marks of the given mark type
* @param {string} markType
* @param {Array<string>} selections - An array of strings defining cursor positions or selection ranges.
*/
function validateAllMarks(markType, selections) {
const marksLocations = _getMarkLocations(markType, "validateAllMarks", selections);
if(!selections || marksLocations.length !== selections.length){
throw new Error(`validateAllMarks expected marks "${markType}" at: [${selections&&selections.join(", ")}] `+
`but got marked locations [${marksLocations.join(", ")}]`);
}
for(let i = 0; i < selections.length; i++) {
if(!selections.includes(`${marksLocations[i]}`) ||
!marksLocations.includes(selections[i])){
throw new Error(`validateAllMarks expected marks "${markType}" at: [${selections.join(", ")}] `+
`but got marked locations [${marksLocations.join(", ")}]`);
}
}
}
function validateEqual(obj1, obj2, message = "") {
if(!_.isEqual(obj1, obj2)){
throw new Error(`validateEqual: ${ message ? message + "\n" : ""
} expected ${JSON.stringify(obj1)} to equal ${JSON.stringify(obj2)}`);
}
}
function validateNotEqual(obj1, obj2) {
if(_.isEqual(obj1, obj2)){
throw new Error(`validateEqual: expected ${JSON.stringify(obj1)} to NOT equal ${JSON.stringify(obj2)}`);
}
}
/**
* validates if the given mark type is present in the specified selections
* @param {string} markType
* @param {Array<string>} selections - An array of strings defining cursor positions or selection ranges.
* @param {number} [totalMarkCount] optional to validate against the total number of expected marks of the type
*/
function validateMarks(markType, selections, totalMarkCount) {
const marksLocations = _getMarkLocations(markType, "validateMarks", selections);
if(!selections){
return;
}
if(totalMarkCount !== undefined && marksLocations.length !== totalMarkCount){
throw new Error(`validateMarks expected mark count for "${markType}" to be: ${totalMarkCount} `+
`but got ${marksLocations.length}`);
}
for(let selection of selections) {
if(!marksLocations.includes(selection)){
throw new Error(`validateMarks expected marks "${markType}" to be at: [${selections.join(", ")}] `+
`but got marked locations [${marksLocations.join(", ")}]`);
}
}
}
function closeFile() {
return jsPromise(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }));
}
function closeAll() {
return jsPromise(CommandManager.execute(Commands.FILE_CLOSE_ALL, { _forceClose: true }));
}
function execCommand(commandID, arg) {
return jsPromise(CommandManager.execute(commandID, arg));
}
function undo() {
return execCommand(Commands.EDIT_UNDO);
}
function redo() {
return execCommand(Commands.EDIT_REDO);
}
function setPreference(key, value){
PreferencesManager.set(key, value);
}
function getPreference(key){
return PreferencesManager.get(key);
}
// Helper function to get full path (reusing existing openFile logic)
function _getFullPath(filePath) {
if(filePath.startsWith('/')) {
return filePath;
}
return path.join(ProjectManager.getProjectRoot().fullPath, filePath);
}
const EDITING = {
setEditorSpacing: function (useTabs, spaceOrTabCount, isAutoMode) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found to setEditorSpacing`);
}
const fullPath = activeEditor.document.file.fullPath;
if(Editor.Editor.getAutoTabSpaces(fullPath) !== isAutoMode){
Editor.Editor.setAutoTabSpaces(isAutoMode, fullPath);
isAutoMode && Editor.Editor._autoDetectTabSpaces(activeEditor, true, true);
}
Editor.Editor.setUseTabChar(useTabs, fullPath);
if(useTabs) {
Editor.Editor.setTabSize(spaceOrTabCount, fullPath);
} else {
Editor.Editor.setSpaceUnits(spaceOrTabCount, fullPath);
}
},
/**
* Split the editor pane vertically
*/
splitVertical: function() {
CommandManager.execute(Commands.CMD_SPLITVIEW_VERTICAL);
},
/**
* Split the editor pane horizontally
*/
splitHorizontal: function() {
CommandManager.execute(Commands.CMD_SPLITVIEW_HORIZONTAL);
},
/**
* Remove split pane and return to single pane view
*/
splitNone: function() {
CommandManager.execute(Commands.CMD_SPLITVIEW_NONE);
},
/**
* Gets the editor in the first pane (left/top)
* @return {?Editor} The editor in first pane or null if not available
*/
getFirstPaneEditor: function() {
return MainViewManager.getCurrentlyViewedEditor("first-pane");
},
/**
* Gets the editor in the second pane (right/bottom)
* @return {?Editor} The editor in second pane or null if not available
*/
getSecondPaneEditor: function() {
return MainViewManager.getCurrentlyViewedEditor("second-pane");
},
/**
* Checks if the view is currently split
* @return {boolean} True if view is split, false otherwise
*/
isSplit: function() {
return MainViewManager.getPaneCount() > 1;
},
/**
* Opens a file in the first pane (left/top)
* @param {string} filePath - Project relative or absolute file path
* @returns {Promise} A promise that resolves when the file is opened
*/
openFileInFirstPane: function(filePath) {
return jsPromise(CommandManager.execute(Commands.FILE_OPEN, {
fullPath: _getFullPath(filePath),
paneId: "first-pane"
}));
},
/**
* Opens a file in the second pane (right/bottom)
* @param {string} filePath - Project relative or absolute file path
* @returns {Promise} A promise that resolves when the file is opened
*/
openFileInSecondPane: function(filePath) {
return jsPromise(CommandManager.execute(Commands.FILE_OPEN, {
fullPath: _getFullPath(filePath),
paneId: "second-pane"
}));
},
/**
* Focus the first pane (left/top)
*/
focusFirstPane: function() {
MainViewManager.setActivePaneId("first-pane");
},
/**
* Focus the second pane (right/bottom)
*/
focusSecondPane: function() {
MainViewManager.setActivePaneId("second-pane");
}
};
/**
* Waits for a polling function to succeed or until a timeout is reached.
* The polling function is periodically invoked to check for success, and
* the function rejects with a timeout message if the timeout duration elapses.
*
* @param {function} pollFn - A function that returns `true` or a promise resolving to `true`/`false`
* to indicate success and stop waiting.
* The function will be called repeatedly until it succeeds or times out.
* @param {string|function} _timeoutMessageOrMessageFn - A helpful string message or an async function
* that returns a string message to reject with in case of timeout.
* Example:
* - String: "Condition not met within the allowed time."
* - Function: `async () => "Timeout while waiting for the process to complete."`
* @param {number} [timeoutms=2000] - The maximum time to wait in milliseconds before timing out. Defaults to 2 seconds.
* @param {number} [pollInterval=10] - The interval in milliseconds at which `pollFn` is invoked. Defaults to 10ms.
* @returns {Promise<void>} A promise that resolves when `pollFn` succeeds or rejects with a timeout message.
*
* @throws {Error} If `timeoutms` or `pollInterval` is not a number.
*
* @example
* // Example 1: Using a string as the timeout message
* awaitsFor(
* () => document.getElementById("element") !== null,
* "Element did not appear within the allowed time.",
* 5000,
* 100
* ).then(() => {
* console.log("Element appeared!");
* }).catch(err => {
* console.error(err.message);
* });
*
* @example
* // Example 2: Using a function as the timeout message
* awaitsFor(
* () => document.getElementById("element") !== null,
* async () => {
* const el = document.getElementById("element");
* return `expected ${el} to be null`;
* },
* 10000,
* 500
* ).then(() => {
* console.log("Element appeared!");
* }).catch(err => {
* console.error(err.message);
* });
*/
function awaitsFor(pollFn, _timeoutMessageOrMessageFn, timeoutms = 2000, pollInterval = 10){
if(typeof _timeoutMessageOrMessageFn === "number"){
timeoutms = _timeoutMessageOrMessageFn;
pollInterval = timeoutms;
}
if(!(typeof timeoutms === "number" && typeof pollInterval === "number")){
throw new Error("awaitsFor: invalid parameters when awaiting for " + _timeoutMessageOrMessageFn);
}
async function _getExpectMessage(_timeoutMessageOrMessageFn) {
try{
if(typeof _timeoutMessageOrMessageFn === "function") {
_timeoutMessageOrMessageFn = _timeoutMessageOrMessageFn();
if(_timeoutMessageOrMessageFn instanceof Promise){
_timeoutMessageOrMessageFn = await _timeoutMessageOrMessageFn;
}
}
} catch (e) {
_timeoutMessageOrMessageFn = "Error executing expected message function:" + e.stack;
}
return _timeoutMessageOrMessageFn;
}
function _timeoutPromise(promise, ms) {
const timeout = new Promise((_, reject) => {
setTimeout(async () => {
_timeoutMessageOrMessageFn = await _getExpectMessage(_timeoutMessageOrMessageFn);
reject(new Error(_timeoutMessageOrMessageFn || `Promise timed out after ${ms}ms`));
}, ms);
});
return Promise.race([promise, timeout]);
}
return new Promise((resolve, reject)=>{
let startTime = Date.now(),
lapsedTime;
async function pollingFn() {
try{
let result = pollFn();
// If pollFn returns a promise, await it
if (Object.prototype.toString.call(result) === "[object Promise]") {
// we cant simply check for result instanceof Promise as the Promise may be returned from
// an iframe and iframe has a different instance of Promise than this js context.
result = await _timeoutPromise(result, timeoutms);
}
if (result) {
resolve();
return;
}
lapsedTime = Date.now() - startTime;
if(lapsedTime>timeoutms){
_timeoutMessageOrMessageFn = await _getExpectMessage(_timeoutMessageOrMessageFn);
reject("awaitsFor timed out waiting for - " + _timeoutMessageOrMessageFn);
return;
}
setTimeout(pollingFn, pollInterval);
} catch (e) {
reject(e);
}
}
pollingFn();
});
}
async function waitForModalDialog(dialogClass, friendlyName, timeout = 2000) {
dialogClass = dialogClass || "";
friendlyName = friendlyName || dialogClass || "Modal Dialog";
await awaitsFor(()=>{
let $dlg = $(`.modal.instance${dialogClass}`);
return $dlg.length >= 1;
}, `Waiting for Modal Dialog to show ${friendlyName}`, timeout);
}
async function waitForModalDialogClosed(dialogClass, friendlyName, timeout = 2000) {
dialogClass = dialogClass || "";
friendlyName = friendlyName || dialogClass || "Modal Dialog";
await awaitsFor(()=>{
let $dlg = $(`.modal.instance${dialogClass}`);
return $dlg.length === 0;
}, `Waiting for Modal Dialog to not there ${friendlyName}`, timeout);
}
/** Clicks on a button within a specified dialog.
* This function identifies a dialog using its class and locates a button either by its selector or button ID.
* Validation to ensure the dialog and button exist and that the button is enabled before attempting to click.
*
* @param {string} selectorOrButtonID - The selector or button ID to identify the button to be clicked.
* Example (as selector): ".my-button-class".
* Example (as button ID): "ok".
* @param {string} dialogClass - The class of the dialog (optional). If omitted, defaults to an empty string.
* Example: "my-dialog-class".
* @param {boolean} isButtonID - If `true`, `selectorOrButtonid` is treated as a button ID.
* If `false`, it is treated as a jQuery selector. Default is `false`.
*
* @throws {Error} Throws an error if:
* - The specified dialog does not exist.
* - Multiple buttons match the given selector or ID.
* - No button matches the given selector or ID.
* - The button is disabled and cannot be clicked.
*
*/
function _clickDialogButtonWithSelector(selectorOrButtonID, dialogClass, isButtonID) {
dialogClass = dialogClass || "";
const $dlg = $(`.modal.instance${dialogClass}`);
if(!$dlg.length){
throw new Error(`No such dialog present: "${dialogClass}"`);
}
const $button = isButtonID ?
$dlg.find(".dialog-button[data-button-id='" + selectorOrButtonID + "']") :
$dlg.find(selectorOrButtonID);
if($button.length > 1){
throw new Error(`Multiple button in dialog "${selectorOrButtonID}"`);
} else if(!$button.length){
throw new Error(`No such button in dialog "${selectorOrButtonID}"`);
}
if($button.prop("disabled")) {
throw new Error(`Cannot click, button is disabled. "${selectorOrButtonID}"`);
}
$button.click();
}
/**
* Clicks on a button within a specified dialog using its button ID.
*
* @param {string} buttonID - The unique ID of the button to be clicked. usually One of the
* __PR.Dialogs.DIALOG_BTN_* symbolic constants or a custom id. You can find the button
* id in the dialog by inspecting the button and checking its `data-button-id` attribute
* Example: __PR.Dialogs.DIALOG_BTN_OK.
* @param {string} [dialogClass] - The class of the dialog containing the button. Optional, if only one dialog
* is present, you can omit this.
* Example: "my-dialog-class".
* @throws {Error} Throws an error if:
* - The specified dialog does not exist.
* - No button matches the given button ID.
* - Multiple buttons match the given button ID.
* - The button is disabled and cannot be clicked.
*
* @example
* // Example: Click a button by its ID
* __PR.clickDialogButtonID(__PR.Dialogs.DIALOG_BTN_OK, "my-dialog-class");
* __PR.clickDialogButtonID(__PR.Dialogs.DIALOG_BTN_OK); // if only 1 dialog is present, can omit the dialog class
* __PR.clickDialogButtonID("customBtnID", "my-dialog-class");
*/
function clickDialogButtonID(buttonID, dialogClass) {
_clickDialogButtonWithSelector(buttonID, dialogClass, true);
}
/**
* Clicks on a button within a specified dialog using a selector.
*
* @param {string} buttonSelector - A jQuery selector to identify the button to be clicked.
* Example: ".showImageBtn".
* @param {string} [dialogClass] - The class of the dialog containing the button. Optional, if only one dialog
* is present, you can omit this.
* Example: "my-dialog-class".
* @throws {Error} Throws an error if:
* - The specified dialog does not exist.
* - No button matches the given selector.
* - Multiple buttons match the given selector.
* - The button is disabled and cannot be clicked.
*
* @example
* // Example: Click a button using a selector
* __PR.clickDialogButton(".showImageBtn", "my-dialog-class");
* __PR.clickDialogButton(".showImageBtn"); // if only 1 dialog is present, can omit the dialog class
*/
function clickDialogButton(buttonSelector, dialogClass) {
_clickDialogButtonWithSelector(buttonSelector, dialogClass, false);
}
/**
* Saves the currently active file
* @returns {Promise<void>} A promise that resolves when file is saved to disc
*/
function saveActiveFile() {
return jsPromise(CommandManager.execute(Commands.FILE_SAVE));
}
const __PR= {
readTextFile, writeTextFile, deletePath,
openFile, setCursors, expectCursorsToBe, keydown, typeAtCursor, validateText, validateAllMarks, validateMarks,
closeFile, closeAll, undo, redo, setPreference, getPreference, validateEqual, validateNotEqual, execCommand,
saveActiveFile,
awaitsFor, waitForModalDialog, waitForModalDialogClosed, clickDialogButtonID, clickDialogButton,
EDITING, $, Commands, Dialogs
};
async function runMacro(macroText) {
let errors = [];
try{
const AsyncFunction = async function () {}.constructor;
const macroAsync = new AsyncFunction("__PR", "KeyEvent", macroText);
await macroAsync(__PR, KeyEvent);
} catch (e) {
console.error("Error executing macro: ", macroText, e);
errors.push({
lineNo: 0, line: '',
errorCode: `ERROR_EXEC`,
errorText: `${e}`
});
}
return errors;
}
if(Phoenix.isTestWindow) {
window.__PR = __PR;
}
exports.computeCursors = computeCursors;
exports.runMacro = runMacro;
});