-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
230 lines (198 loc) · 8.38 KB
/
script.js
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
import 'flickity';
import 'flickity-hash';
import '@fortawesome/fontawesome-free/js/fontawesome.min.js';
import '@fortawesome/fontawesome-free/js/regular.min.js';
import '@fortawesome/fontawesome-free/js/solid.min.js';
import hljs from 'highlight.js/lib/common';
import './style.css';
import { userRoutine } from '../dist/user-routine.min';
import { runRegressionTests } from './regression-tests';
import { codeCardFunctionArray } from './code-cards.min';
const state = {
processTime: 2000,
actionStringIdCounter: 0,
}
function main() {
/* Enable userRoutine access from console */
window.userRoutine = userRoutine;
syntaxHighlightCodeCards();
/*
* URL parameters
*/
const urlParams = new URLSearchParams(window.location.search);
console.info(`URL parameter options:
* action=quick-regression-test
* action=full-regression-test
`);
if (urlParams.get('action') === 'quick-regression-test') {
console.log('Quick regression tests activated');
runRegressionTests();
state.processTime = 20;
} else if (urlParams.get('action') === 'full-regression-test') {
console.log('Full regression tests activated');
runRegressionTests(500, true, true, true);
state.processTime = 2000;
}
/* Add demo code card event listeners */
document.querySelectorAll('button.run').forEach((runButton) => {
runButton.addEventListener('click', runExample)
});
document.querySelectorAll('button.copy-code').forEach((copyButton) => {
copyButton.addEventListener('click', copyCodeOnClick)
});
document.querySelectorAll('button.copy-link').forEach((copyButton) => {
copyButton.addEventListener('click', copyLinkOnClick)
});
document.querySelectorAll('code span.action-keyword').forEach((actionKeywordSpan) => {
actionKeywordSpan.addEventListener('click', showTooltipOnActionKeywordClick);
});
document.querySelectorAll('code span.selector').forEach((selectorSpan) => {
selectorSpan.addEventListener('click', showTooltipOnSelectorClick);
});
document.querySelectorAll('code span.argument').forEach((argumentSpan) => {
argumentSpan.addEventListener('click', showTooltipOnArgumentClick);
});
/* Add mock-app event listeners */
document.querySelector('button.duplicate').addEventListener('click', duplicateText);
document.querySelector('button.process').addEventListener('click', startMockLongProcess);
/* Add initial highlight removal event listener; click helps with mobile */
document.querySelector('.code-carousel').addEventListener('mouseover', removeInitialCodeHighlightIfNeeded);
document.querySelector('.code-carousel').addEventListener('click', removeInitialCodeHighlightIfNeeded);
}
function removeInitialCodeHighlightIfNeeded(event) {
if(event.target.classList.contains('custom-highlight')) {
document.querySelectorAll('.initial-highlight').forEach(element => {
element.classList.remove('initial-highlight');
});
document.querySelector('.code-carousel').removeEventListener('mouseover', removeInitialCodeHighlightIfNeeded);
document.querySelector('.code-carousel').removeEventListener('click', removeInitialCodeHighlightIfNeeded);
}
}
/* Code Card Functions */
function runExample() {
const codeCard = event.target.parentNode.parentNode;
const codeCardId = Number(codeCard.getAttribute('data-card-id'));
codeCardFunctionArray[codeCardId]();
}
function copyCodeOnClick(event) {
const codeCard = event.target.parentNode.parentNode;
const codeCardId = Number(codeCard.getAttribute('data-card-id'));
const code = codeCard.querySelector('pre > code').textContent;
navigator.clipboard.writeText(code);
showUserRoutineTooltip(
`figure:nth-child(${codeCardId + 1}) button.copy-code`,
'✔️ Code copied!'
);
}
function copyLinkOnClick(event) {
const codeCard = event.target.parentNode.parentNode;
const codeCardId = Number(codeCard.getAttribute('data-card-id'));
const figureId = codeCard.parentNode.id;
const link = location.protocol + '//' + location.host + location.pathname + '#' + figureId;
navigator.clipboard.writeText(link);
showUserRoutineTooltip(
`figure:nth-child(${codeCardId + 1}) button.copy-link`,
'✔️ Link copied!',
);
}
function showTooltipOnActionKeywordClick(event) {
const selectorId = event.target.getAttribute('data-action-keyword-id');
showUserRoutineTooltip(
`[data-action-keyword-id="${selectorId}"]`,
'Action keyword',
);
}
function showTooltipOnSelectorClick(event) {
const selectorId = event.target.getAttribute('data-selector-id');
showUserRoutineTooltip(
`[data-selector-id="${selectorId}"]`,
'CSS selector',
);
}
function showTooltipOnArgumentClick(event) {
const selectorId = event.target.getAttribute('data-argument-id');
showUserRoutineTooltip(
`[data-argument-id="${selectorId}"]`,
'Data used by action',
);
}
function showUserRoutineTooltip(cssSelector, message, speed = 1.5) {
userRoutine(
`comment ${cssSelector.replaceAll(' ', '>>')} ${message}`,
{
displayMessage: false, logProgress: false,
logResult: false, simultaneousAllowed: true, keyboardControls: false,
globalDelay: 0, displaySpeed: speed
}
);
}
function syntaxHighlightCodeCards() {
hljs.highlightAll();
// TODO: Figure out why "Welcome" in "Welcome to the demo" is disappearing
/* Custom highlight action string */
const actionCodesWithSelectors = [
'app', 'awa', '!aw', 'cli', 'com', 'exi', '!ex', 'fil', 'val', '!va', 'wri',
];
const actionCodesWithoutSelectors = ['log', 'nav', 'wai'];
document.querySelectorAll('.hljs .hljs-string').forEach((stringElement) => {
const stringWithQuotes = stringElement.textContent;
const codeString = stringWithQuotes.substring(1, stringWithQuotes.length - 1);
const actionParts = codeString.split(' ');
const actionCode = actionParts[0].substring(0, 3);
const isActionStringWithSelector = isLowerCase(actionCode[0]) && actionCodesWithSelectors.includes(actionCode);
const isActionStringWithoutSelector = isLowerCase(actionCode[0]) && actionCodesWithoutSelectors.includes(actionCode);
const initHighlightClass = state.actionStringIdCounter === 0 ? 'custom-highlight initial-highlight' : 'custom-highlight';
if (isActionStringWithSelector) {
const newActionParts = [];
newActionParts.push(`<span class="action-keyword ${initHighlightClass}"
data-action-keyword-id="${state.actionStringIdCounter}">${actionParts[0]}</span>`);
newActionParts.push(`<span class="selector ${initHighlightClass}"
data-selector-id="${state.actionStringIdCounter}">${actionParts[1]}</span>`);
if (actionParts[2]) {
const parts = [...actionParts];
const argumentString = parts.splice(2).join(' ');
newActionParts.push(`<span class="argument ${initHighlightClass}"
data-argument-id="${state.actionStringIdCounter}">${argumentString}</span>`);
}
stringElement.innerHTML = `'${newActionParts.join(' ')}'`;
state.actionStringIdCounter++;
} else if (isActionStringWithoutSelector) {
const newActionParts = [];
const parts = [...actionParts];
const argumentString = parts.splice(1).join(' ');
newActionParts.push(`<span class="action-keyword" title="Action keyword"
data-action-keyword-id="${state.actionStringIdCounter}">${actionParts[0]}</span>`);
newActionParts.push(`<span class="argument" title="Data used by action"
data-argument-id="${state.actionStringIdCounter}">${argumentString}</span>`);
stringElement.innerHTML = `'${newActionParts.join(' ')}'`;
state.actionStringIdCounter++;
}
});
}
function isLowerCase(string) {
return string === string.toLowerCase();
}
/* Mock App Functions */
function duplicateText() {
const outputArea = document.querySelector('pre.output');
outputArea.textContent = '';
const text = document.querySelector('input.text').value;
const count = document.querySelector('input.count').value;
for (let i = 0; i < Number(count); i++) {
outputArea.textContent += (text + " ")
}
}
function startMockLongProcess() {
const processOutput = document.querySelector('.result');
processOutput.textContent = '';
processOutput.textContent = 'Processing...';
setTimeout(() => {
processOutput.textContent = 'Almost there...'
}, state.processTime);
setTimeout(() => {
processOutput.textContent = 'Process complete!';
/* Reset process time after any automated tests */
state.processTime = 2000;
}, state.processTime * 2);
}
main();