markdown-up 1.7.2 → 1.7.3
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.
- package/README.md +43 -26
- package/lib/app.js +304 -204
- package/lib/barChart.js +5 -6
- package/lib/data.js +27 -31
- package/lib/dataTable.js +5 -6
- package/lib/lineChart.js +5 -6
- package/lib/model.js +5 -11
- package/lib/script.js +577 -179
- package/lib/util.js +19 -23
- package/package.json +5 -5
package/lib/app.js
CHANGED
|
@@ -21,13 +21,11 @@ import {validateType} from 'schema-markdown/lib/schema.js';
|
|
|
21
21
|
// The application's hash parameter type model
|
|
22
22
|
const markdownUpTypesSmd = `\
|
|
23
23
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
# [markdown-up](https://github.com/craigahobbs/markdown-up#readme)
|
|
24
|
+
# [MarkdownUp](https://craigahobbs.github.io/markdown-up/) is a Markdown viewer application.
|
|
27
25
|
#
|
|
28
26
|
# ## Hash Parameters
|
|
29
27
|
#
|
|
30
|
-
#
|
|
28
|
+
# MarkdownUp recognizes the following hash parameters:
|
|
31
29
|
#
|
|
32
30
|
struct MarkdownUp
|
|
33
31
|
|
|
@@ -37,7 +35,7 @@ struct MarkdownUp
|
|
|
37
35
|
# Optional command
|
|
38
36
|
optional Command cmd
|
|
39
37
|
|
|
40
|
-
# The menu visibility (default is
|
|
38
|
+
# The menu visibility (default is collapsed)
|
|
41
39
|
optional int(== 1) menu
|
|
42
40
|
|
|
43
41
|
# The font size
|
|
@@ -53,20 +51,20 @@ struct MarkdownUp
|
|
|
53
51
|
optional int(== 1) debug
|
|
54
52
|
|
|
55
53
|
|
|
56
|
-
#
|
|
54
|
+
# A MarkdownUp application command
|
|
57
55
|
union Command
|
|
58
56
|
|
|
59
|
-
# Display the
|
|
57
|
+
# Display the hash parameters documentation
|
|
60
58
|
int(== 1) help
|
|
61
59
|
|
|
62
|
-
# Show the
|
|
60
|
+
# Show the resource's Markdown text
|
|
63
61
|
int(== 1) markdown
|
|
64
62
|
`;
|
|
65
63
|
const markdownUpTypes = new SchemaMarkdownParser(markdownUpTypesSmd).types;
|
|
66
64
|
|
|
67
65
|
|
|
68
66
|
/**
|
|
69
|
-
*
|
|
67
|
+
* The MarkdownUp application options
|
|
70
68
|
*
|
|
71
69
|
* @typedef {Object} MarkdownUpOptions
|
|
72
70
|
* @param {number} [fontSize = 12] - The font size, in points
|
|
@@ -87,14 +85,14 @@ const markdownUpTypes = new SchemaMarkdownParser(markdownUpTypesSmd).types;
|
|
|
87
85
|
* @property {string} markdownText - The markdown text
|
|
88
86
|
* @property {boolean} menu - If true, show the menu
|
|
89
87
|
* @property {string} url - The resource URL
|
|
90
|
-
* @property {?number}
|
|
88
|
+
* @property {?number} runtimeTimeoutId - The runtime-set timeout ID
|
|
91
89
|
*/
|
|
92
90
|
export class MarkdownUp {
|
|
93
91
|
/**
|
|
94
92
|
* Create an application instance
|
|
95
93
|
*
|
|
96
94
|
* @param {Object} window - The web browser window object
|
|
97
|
-
* @property {
|
|
95
|
+
* @property {Object} [options] - The [application options]{@link module:lib/app~MarkdownUpOptions}
|
|
98
96
|
*/
|
|
99
97
|
constructor(window, {fontSize = 12, lineHeight = 1.2, markdownText = null, menu = true, url = 'README.md'} = {}) {
|
|
100
98
|
this.window = window;
|
|
@@ -104,9 +102,11 @@ export class MarkdownUp {
|
|
|
104
102
|
this.markdownText = markdownText;
|
|
105
103
|
this.menu = menu;
|
|
106
104
|
this.url = url;
|
|
107
|
-
this.
|
|
105
|
+
this.runtimeWindowResize = null;
|
|
106
|
+
this.runtimeTimeoutId = null;
|
|
108
107
|
}
|
|
109
108
|
|
|
109
|
+
|
|
110
110
|
/**
|
|
111
111
|
* Run the application. This method calls render and subscribes to any hash parameter changes to
|
|
112
112
|
* re-render on any hash parameter change.
|
|
@@ -116,14 +116,21 @@ export class MarkdownUp {
|
|
|
116
116
|
this.window.addEventListener('hashchange', () => this.render(), false);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
|
|
119
120
|
/**
|
|
120
|
-
*
|
|
121
|
+
* Render the application
|
|
121
122
|
*/
|
|
122
123
|
async render() {
|
|
123
|
-
// Clear the
|
|
124
|
-
if (this.
|
|
125
|
-
this.window.clearTimeout(this.
|
|
126
|
-
this.
|
|
124
|
+
// Clear the runtime timeout ID, if one is set
|
|
125
|
+
if (this.runtimeTimeoutId !== null) {
|
|
126
|
+
this.window.clearTimeout(this.runtimeTimeoutId);
|
|
127
|
+
this.runtimeTimeoutId = null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Clear the window resize event handler
|
|
131
|
+
if (this.runtimeWindowResize !== null) {
|
|
132
|
+
this.window.removeEventListener('resize', this.runtimeWindowResize);
|
|
133
|
+
this.runtimeWindowResize = null;
|
|
127
134
|
}
|
|
128
135
|
|
|
129
136
|
// Parse the hash parameters and render the application element model
|
|
@@ -146,18 +153,23 @@ export class MarkdownUp {
|
|
|
146
153
|
isError = true;
|
|
147
154
|
}
|
|
148
155
|
|
|
149
|
-
// Navigate?
|
|
150
|
-
if ('location' in result && result.location !== null) {
|
|
151
|
-
this.window.location.href = result.location;
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
156
|
// Set the window title
|
|
156
157
|
const title = result.title ?? null;
|
|
157
158
|
if (title !== null) {
|
|
158
159
|
this.window.document.title = title;
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
// Timeout?
|
|
163
|
+
if ('timeout' in result) {
|
|
164
|
+
this.runtimeTimeoutId = this.window.setTimeout(...result.timeout);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Window resize event handler?
|
|
168
|
+
if ('resize' in result) {
|
|
169
|
+
this.runtimeWindowResize = result.resize;
|
|
170
|
+
this.window.addEventListener('resize', result.resize);
|
|
171
|
+
}
|
|
172
|
+
|
|
161
173
|
// Set the font size
|
|
162
174
|
const fontSize = this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize;
|
|
163
175
|
this.window.document.documentElement.style.setProperty('--markdown-model-font-size', `${fontSize}pt`);
|
|
@@ -169,6 +181,25 @@ export class MarkdownUp {
|
|
|
169
181
|
// Render the element model
|
|
170
182
|
renderElements(this.window.document.body, result.elements);
|
|
171
183
|
|
|
184
|
+
// Focus?
|
|
185
|
+
if ('focus' in result) {
|
|
186
|
+
const element = this.window.document.getElementById(result.focus);
|
|
187
|
+
if (element !== null) {
|
|
188
|
+
element.focus();
|
|
189
|
+
if ('selectionStart' in element) {
|
|
190
|
+
element.selectionStart = element.value.length;
|
|
191
|
+
element.selectionEnd = element.value.length;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Navigate?
|
|
197
|
+
// Note: This is done after render since it may have no effect (in which case we want to render).
|
|
198
|
+
if ('location' in result && result.location !== null) {
|
|
199
|
+
this.window.location.href = result.location;
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
172
203
|
// If there is a URL hash ID, re-navigate to go there since it was just rendered. After the
|
|
173
204
|
// first render, re-render is short-circuited by the unchanged hash param check above.
|
|
174
205
|
if (!isError && getHashID(this.window.location.hash) !== null) {
|
|
@@ -176,6 +207,7 @@ export class MarkdownUp {
|
|
|
176
207
|
}
|
|
177
208
|
}
|
|
178
209
|
|
|
210
|
+
|
|
179
211
|
/**
|
|
180
212
|
* Parse and validate the hash parameters
|
|
181
213
|
*
|
|
@@ -192,139 +224,127 @@ export class MarkdownUp {
|
|
|
192
224
|
this.params = validateType(markdownUpTypes, 'MarkdownUp', params);
|
|
193
225
|
}
|
|
194
226
|
|
|
227
|
+
|
|
195
228
|
/**
|
|
196
|
-
* The
|
|
197
|
-
* https://craigahobbs.github.io/element-app/module-lib_app.ElementApplication.html#main}.
|
|
229
|
+
* The application's main entry point
|
|
198
230
|
*
|
|
199
|
-
* @
|
|
200
|
-
* @returns {Object} [MainResult]{@link https://craigahobbs.github.io/element-app/module-lib_app.html#~MainResult}
|
|
231
|
+
* @returns {Object}
|
|
201
232
|
*/
|
|
202
233
|
async main() {
|
|
203
|
-
//
|
|
234
|
+
// Help?
|
|
235
|
+
if ('cmd' in this.params && 'help' in this.params.cmd) {
|
|
236
|
+
return {
|
|
237
|
+
'title': 'MarkdownUp',
|
|
238
|
+
'elements': [
|
|
239
|
+
schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)}),
|
|
240
|
+
this.burgerElements()
|
|
241
|
+
]
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Get the Markdown text
|
|
204
246
|
const urlOverride = 'url' in this.params && this.params.url !== '';
|
|
205
247
|
const url = urlOverride ? this.params.url : this.url;
|
|
206
248
|
let markdownText = null;
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
249
|
+
if (this.markdownText !== null && !urlOverride) {
|
|
250
|
+
({markdownText} = this);
|
|
251
|
+
} else {
|
|
252
|
+
// Fetch the Markdown text resource URL
|
|
253
|
+
const response = await this.window.fetch(url);
|
|
254
|
+
if (!response.ok) {
|
|
255
|
+
const status = response.statusText;
|
|
256
|
+
return {
|
|
257
|
+
'title': 'MarkdownUp',
|
|
258
|
+
'elements': {
|
|
259
|
+
'html': 'p',
|
|
260
|
+
'elem': {'text': `Error: Could not fetch "${url}"${status === '' ? '' : ` - ${JSON.stringify(status)}`}`}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
219
263
|
}
|
|
220
|
-
|
|
221
|
-
markdownTitle = getMarkdownTitle(markdownModel);
|
|
264
|
+
markdownText = await response.text();
|
|
222
265
|
}
|
|
223
266
|
|
|
224
|
-
//
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
267
|
+
// Parse the Markdown and get the title
|
|
268
|
+
const markdownModel = parseMarkdown(markdownText);
|
|
269
|
+
const markdownTitle = getMarkdownTitle(markdownModel);
|
|
270
|
+
|
|
271
|
+
// Display the Markdown?
|
|
272
|
+
if ('cmd' in this.params && 'markdown' in this.params.cmd) {
|
|
273
|
+
return {
|
|
274
|
+
'title': markdownTitle,
|
|
275
|
+
'elements': [
|
|
276
|
+
{'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}},
|
|
277
|
+
this.burgerElements()
|
|
278
|
+
]
|
|
279
|
+
};
|
|
235
280
|
}
|
|
236
281
|
|
|
237
|
-
//
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
|
|
246
|
-
const params = {...this.params};
|
|
247
|
-
params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
|
|
248
|
-
return `#${encodeQueryString(params)}`;
|
|
249
|
-
};
|
|
282
|
+
// Render the Markdown
|
|
283
|
+
const scriptOptions = this.createScriptOptions(url);
|
|
284
|
+
const result = {
|
|
285
|
+
'title': markdownTitle,
|
|
286
|
+
'elements': [
|
|
287
|
+
// Add a top hash ID, if necessary
|
|
288
|
+
Object.keys(this.params).length === 0
|
|
289
|
+
? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
|
|
250
290
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
291
|
+
// Render the markdown
|
|
292
|
+
await markdownElementsAsync(markdownModel, {
|
|
293
|
+
'codeBlocks': {
|
|
294
|
+
'bar-chart': (codeBlock) => barChartCodeBlock(codeBlock, scriptOptions),
|
|
295
|
+
'data-table': (codeBlock) => dataTableCodeBlock(codeBlock, scriptOptions),
|
|
296
|
+
'line-chart': (codeBlock) => lineChartCodeBlock(codeBlock, scriptOptions),
|
|
297
|
+
'markdown-script': (codeBlock) => markdownScriptCodeBlock(codeBlock, scriptOptions)
|
|
298
|
+
},
|
|
299
|
+
'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
|
|
300
|
+
'headerIds': true
|
|
301
|
+
}),
|
|
262
302
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (!(name in this.params)) {
|
|
267
|
-
params[name] = value;
|
|
268
|
-
} else {
|
|
269
|
-
delete params[name];
|
|
270
|
-
}
|
|
271
|
-
return `#${encodeQueryString(params)}`;
|
|
303
|
+
// Popup menu burger
|
|
304
|
+
this.burgerElements()
|
|
305
|
+
]
|
|
272
306
|
};
|
|
273
307
|
|
|
274
|
-
//
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
308
|
+
// Set any runtime side-effects
|
|
309
|
+
if (scriptOptions.runtime.windowLocation !== null) {
|
|
310
|
+
result.location = scriptOptions.runtime.windowLocation;
|
|
311
|
+
}
|
|
312
|
+
if (scriptOptions.runtime.documentFocus !== null) {
|
|
313
|
+
result.focus = scriptOptions.runtime.documentFocus;
|
|
314
|
+
}
|
|
315
|
+
if (scriptOptions.runtime.documentTitle !== null) {
|
|
316
|
+
result.title = scriptOptions.runtime.documentTitle;
|
|
317
|
+
}
|
|
318
|
+
if (scriptOptions.runtime.windowTimeout !== null) {
|
|
319
|
+
result.timeout = scriptOptions.runtime.windowTimeout;
|
|
320
|
+
}
|
|
321
|
+
if (scriptOptions.runtime.windowResize !== null) {
|
|
322
|
+
result.resize = scriptOptions.runtime.windowResize;
|
|
323
|
+
}
|
|
283
324
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
287
327
|
|
|
288
|
-
// Relative URL?
|
|
289
|
-
if (isRelativeURL(rawURL)) {
|
|
290
|
-
return `${getBaseURL(url)}${rawURL}`;
|
|
291
|
-
}
|
|
292
328
|
|
|
293
|
-
|
|
329
|
+
createScriptOptions(resourceURL) {
|
|
330
|
+
// Create the script options object
|
|
331
|
+
const scriptOptions = {
|
|
332
|
+
// eslint-disable-next-line require-await
|
|
333
|
+
'fetchFn': async (fetchURL, init) => this.window.fetch(fetchURL, init),
|
|
334
|
+
'fontSize': this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize,
|
|
335
|
+
'params': this.params,
|
|
336
|
+
'urlFn': (url) => this.modifyURL(url, resourceURL),
|
|
337
|
+
'window': this.window
|
|
294
338
|
};
|
|
295
339
|
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
340
|
+
// Add log function, if debugging
|
|
341
|
+
if ('debug' in this.params) {
|
|
342
|
+
scriptOptions.logFn = (text) => {
|
|
299
343
|
this.window.console.log(text);
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
// The fetch function
|
|
304
|
-
// eslint-disable-next-line require-await
|
|
305
|
-
const fetchFn = async (fetchURL, init) => this.window.fetch(urlFn(fetchURL), init);
|
|
306
|
-
|
|
307
|
-
// The navigate timeout function
|
|
308
|
-
const navigateTimeoutFn = (navigateURL, delay) => {
|
|
309
|
-
const timeoutCallback = () => {
|
|
310
|
-
this.window.location.href = urlFn(navigateURL);
|
|
311
344
|
};
|
|
312
|
-
|
|
313
|
-
};
|
|
314
|
-
// eslint-disable-next-line lines-around-comment
|
|
345
|
+
}
|
|
315
346
|
|
|
316
|
-
//
|
|
317
|
-
const fontSize = 0.9 * (this.params !== null && 'fontSize' in this.params ? this.params.fontSize : this.fontSize);
|
|
318
|
-
const scriptOptions = {
|
|
319
|
-
fetchFn,
|
|
320
|
-
fontSize,
|
|
321
|
-
'localStorage': this.window.localStorage,
|
|
322
|
-
logFn,
|
|
323
|
-
navigateTimeoutFn,
|
|
324
|
-
'params': this.params,
|
|
325
|
-
'sessionStorage': this.window.sessionStorage,
|
|
326
|
-
urlFn
|
|
327
|
-
};
|
|
347
|
+
// Add hash parameter variables, if any
|
|
328
348
|
if ('var' in this.params) {
|
|
329
349
|
scriptOptions.variables = {};
|
|
330
350
|
for (const varName of Object.keys(this.params.var)) {
|
|
@@ -332,84 +352,171 @@ export class MarkdownUp {
|
|
|
332
352
|
scriptOptions.variables[varName] = evaluateExpression(varExpr, scriptOptions.variables);
|
|
333
353
|
}
|
|
334
354
|
}
|
|
335
|
-
scriptOptions.runtime = new MarkdownScriptRuntime(scriptOptions);
|
|
336
|
-
const result = {
|
|
337
|
-
'title': markdownTitle,
|
|
338
|
-
'elements': [
|
|
339
|
-
// Command?
|
|
340
|
-
cmdElements !== null ? cmdElements : null,
|
|
341
355
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
356
|
+
// Create the markdown-script runtime
|
|
357
|
+
const runtime = new MarkdownScriptRuntime(scriptOptions);
|
|
358
|
+
scriptOptions.runtime = runtime;
|
|
359
|
+
scriptOptions.runtimeUpdateFn = () => {
|
|
360
|
+
// Did script set the document title?
|
|
361
|
+
if (runtime.documentTitle !== null) {
|
|
362
|
+
this.window.document.title = runtime.documentTitle;
|
|
363
|
+
}
|
|
345
364
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
},
|
|
354
|
-
urlFn,
|
|
355
|
-
'headerIds': true
|
|
356
|
-
}),
|
|
365
|
+
// Did the script set a timeout?
|
|
366
|
+
if (runtime.windowTimeout !== null) {
|
|
367
|
+
if (this.runtimeTimeoutId !== null) {
|
|
368
|
+
this.window.clearTimeout(this.runtimeTimeoutId);
|
|
369
|
+
}
|
|
370
|
+
this.runtimeTimeoutId = this.window.setTimeout(...runtime.windowTimeout);
|
|
371
|
+
}
|
|
357
372
|
|
|
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
|
-
'html': 'a',
|
|
383
|
-
'attr': {'href': linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
|
|
384
|
-
'elem': fontSizeSVGElements
|
|
385
|
-
},
|
|
386
|
-
{
|
|
387
|
-
'html': 'a',
|
|
388
|
-
'attr': {'href': linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
|
|
389
|
-
'elem': lineHeightSVGElements
|
|
390
|
-
},
|
|
391
|
-
{
|
|
392
|
-
'html': 'a',
|
|
393
|
-
'attr': {'href': linkCommandToggle('help', '1'), 'aria-label': 'Help'},
|
|
394
|
-
'elem': helpSVGElements
|
|
395
|
-
}
|
|
396
|
-
]
|
|
373
|
+
// Reset the runtime
|
|
374
|
+
const {isDocumentReset} = runtime;
|
|
375
|
+
const {windowLocation} = runtime;
|
|
376
|
+
const documentElements = runtime.reset();
|
|
377
|
+
|
|
378
|
+
// Render the elment model
|
|
379
|
+
renderElements(
|
|
380
|
+
this.window.document.body,
|
|
381
|
+
[
|
|
382
|
+
documentElements,
|
|
383
|
+
isDocumentReset ? this.burgerElements() : null
|
|
384
|
+
],
|
|
385
|
+
isDocumentReset
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
// Focus?
|
|
389
|
+
if (runtime.documentFocus !== null) {
|
|
390
|
+
const element = this.window.document.getElementById(runtime.documentFocus);
|
|
391
|
+
if (element !== null) {
|
|
392
|
+
element.focus();
|
|
393
|
+
if ('selectionStart' in element) {
|
|
394
|
+
element.selectionStart = element.value.length;
|
|
395
|
+
element.selectionEnd = element.value.length;
|
|
396
|
+
}
|
|
397
397
|
}
|
|
398
|
-
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Navigate?
|
|
401
|
+
// Note: This is done after render since it may have no effect (in which case we want to render).
|
|
402
|
+
if (windowLocation !== null) {
|
|
403
|
+
this.window.location.href = windowLocation;
|
|
404
|
+
}
|
|
399
405
|
};
|
|
400
406
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
407
|
+
return scriptOptions;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
modifyURL(url, resourceURL) {
|
|
412
|
+
// Hash URL?
|
|
413
|
+
if (url.startsWith('#')) {
|
|
414
|
+
// Fixup the "url" param if its relative
|
|
415
|
+
const hashParams = decodeQueryString(url.slice(1));
|
|
416
|
+
if ('url' in hashParams && hashParams.url !== '' && isRelativeURL(hashParams.url)) {
|
|
417
|
+
hashParams.url = `${getBaseURL(resourceURL)}${hashParams.url}`;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Encode hash parameters with a hash ID
|
|
421
|
+
const paramString = encodeQueryString({...this.params, ...hashParams});
|
|
422
|
+
const hashID = getHashID(url);
|
|
423
|
+
return `#${paramString}${hashID !== null && paramString !== '' ? '&' : ''}${hashID !== null ? hashID : ''}`;
|
|
404
424
|
}
|
|
405
425
|
|
|
406
|
-
|
|
426
|
+
// Relative URL?
|
|
427
|
+
if (isRelativeURL(url)) {
|
|
428
|
+
return `${getBaseURL(resourceURL)}${url}`;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return url;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
burgerElements() {
|
|
436
|
+
return !this.menu ? null : [
|
|
437
|
+
{
|
|
438
|
+
'html': 'div',
|
|
439
|
+
'attr': {'class': 'menu-burger'},
|
|
440
|
+
'elem': [
|
|
441
|
+
{
|
|
442
|
+
'html': 'a',
|
|
443
|
+
'attr': {'href': this.linkValueToggle('menu', '1'), 'aria-label': 'Menu'},
|
|
444
|
+
'elem': menuSVGElements
|
|
445
|
+
}
|
|
446
|
+
]
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
// Popup menu
|
|
450
|
+
!('menu' in this.params) ? null : {
|
|
451
|
+
'html': 'div',
|
|
452
|
+
'attr': {'class': 'menu'},
|
|
453
|
+
'elem': [
|
|
454
|
+
{
|
|
455
|
+
'html': 'a',
|
|
456
|
+
'attr': {'href': this.linkCommandToggle('markdown', '1'), 'aria-label': 'Show Markdown'},
|
|
457
|
+
'elem': markdownSVGElements
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
'html': 'a',
|
|
461
|
+
'attr': {'href': this.linkCycle('fontSize', this.fontSize, 2), 'aria-label': 'Font size'},
|
|
462
|
+
'elem': fontSizeSVGElements
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
'html': 'a',
|
|
466
|
+
'attr': {'href': this.linkCycle('lineHeight', this.lineHeight, 0.2, 1), 'aria-label': 'Line height'},
|
|
467
|
+
'elem': lineHeightSVGElements
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
'html': 'a',
|
|
471
|
+
'attr': {'href': this.linkCommandToggle('help', '1'), 'aria-label': 'Help'},
|
|
472
|
+
'elem': helpSVGElements
|
|
473
|
+
}
|
|
474
|
+
]
|
|
475
|
+
}
|
|
476
|
+
];
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
linkCycle(paramName, defaultValue, delta, fixed = null) {
|
|
481
|
+
const value = paramName in this.params ? this.params[paramName] : defaultValue;
|
|
482
|
+
const {attr} = markdownUpTypes.MarkdownUp.struct.members.find((member) => member.name === paramName);
|
|
483
|
+
let valueNew = value + delta;
|
|
484
|
+
if (valueNew > attr.lte) {
|
|
485
|
+
valueNew = attr.gte;
|
|
486
|
+
}
|
|
487
|
+
valueNew = Math.max(attr.gte, Math.min(attr.lte, valueNew));
|
|
488
|
+
const params = {...this.params};
|
|
489
|
+
params[paramName] = fixed === null ? `${valueNew}` : `${valueNew.toFixed(fixed)}`;
|
|
490
|
+
return `#${encodeQueryString(params)}`;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
linkCommandToggle(command, value) {
|
|
495
|
+
const params = {...this.params};
|
|
496
|
+
if ('cmd' in this.params && command in this.params.cmd) {
|
|
497
|
+
delete params.cmd;
|
|
498
|
+
} else {
|
|
499
|
+
params.cmd = {};
|
|
500
|
+
params.cmd[command] = `${value}`;
|
|
501
|
+
}
|
|
502
|
+
return `#${encodeQueryString(params)}`;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
linkValueToggle(name, value) {
|
|
507
|
+
const params = {...this.params};
|
|
508
|
+
if (!(name in this.params)) {
|
|
509
|
+
params[name] = value;
|
|
510
|
+
} else {
|
|
511
|
+
delete params[name];
|
|
512
|
+
}
|
|
513
|
+
return `#${encodeQueryString(params)}`;
|
|
407
514
|
}
|
|
408
515
|
}
|
|
409
516
|
|
|
410
517
|
|
|
411
518
|
// Get a URL's hash ID
|
|
412
|
-
|
|
519
|
+
function getHashID(url) {
|
|
413
520
|
const matchId = url.match(rHashId);
|
|
414
521
|
return matchId !== null ? matchId[1] : null;
|
|
415
522
|
}
|
|
@@ -417,13 +524,6 @@ export function getHashID(url) {
|
|
|
417
524
|
const rHashId = /[#&]([^=]+)$/;
|
|
418
525
|
|
|
419
526
|
|
|
420
|
-
// Encode hash parameters with a hash ID
|
|
421
|
-
export function encodeHashID(params, hashID) {
|
|
422
|
-
const paramString = encodeQueryString(params);
|
|
423
|
-
return `#${paramString}${hashID !== null && paramString !== '' ? '&' : ''}${hashID !== null ? hashID : ''}`;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
527
|
// Icon sizes
|
|
428
528
|
const burgerSize = '24';
|
|
429
529
|
const iconSize = '36';
|