markdown-up 1.8.8 → 1.8.10
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 +10 -5
- package/lib/app.js +24 -18
- package/lib/data.js +44 -10
- package/lib/script.js +8 -8
- package/lib/scriptLibrary.js +108 -44
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,14 +3,19 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/markdown-up)
|
|
4
4
|
[](https://github.com/craigahobbs/markdown-up/blob/main/LICENSE)
|
|
5
5
|
|
|
6
|
-
[MarkdownUp](https://craigahobbs.github.io/markdown-up/)
|
|
7
|
-
|
|
6
|
+
[MarkdownUp](https://craigahobbs.github.io/markdown-up/) is a Markdown viewer.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## Links
|
|
10
|
+
|
|
11
|
+
- [MarkdownUp Application](https://craigahobbs.github.io/markdown-up/)
|
|
12
|
+
- [Source code](https://github.com/craigahobbs/markdown-up)
|
|
8
13
|
|
|
9
14
|
|
|
10
15
|
## View Local Markdown Files
|
|
11
16
|
|
|
12
17
|
To view local Markdown files, use the
|
|
13
|
-
[MarkdownUp launcher](https://
|
|
18
|
+
[MarkdownUp launcher](https://github.com/craigahobbs/markdown-up-py#readme)
|
|
14
19
|
from a terminal prompt:
|
|
15
20
|
|
|
16
21
|
~~~
|
|
@@ -142,8 +147,8 @@ CSV, manipulating data, and rendering tables and charts. For more information, r
|
|
|
142
147
|
|
|
143
148
|
## Development
|
|
144
149
|
|
|
145
|
-
|
|
146
|
-
|
|
150
|
+
This package is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme).
|
|
151
|
+
It was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme) as follows:
|
|
147
152
|
|
|
148
153
|
~~~
|
|
149
154
|
template-specialize javascript-template/template/ markdown-up/ -k package markdown-up -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs'
|
package/lib/app.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
/** @module lib/app */
|
|
5
5
|
|
|
6
6
|
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
|
|
7
|
-
import {decodeQueryString, encodeQueryString} from 'schema-markdown/lib/encode.js';
|
|
7
|
+
import {decodeQueryString, encodeQueryString, jsonStringifySortKeys} from 'schema-markdown/lib/encode.js';
|
|
8
8
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
9
9
|
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
10
10
|
import {markdownElementsAsync} from 'markdown-model/lib/elements.js';
|
|
@@ -131,7 +131,7 @@ export class MarkdownUp {
|
|
|
131
131
|
this.updateParams();
|
|
132
132
|
|
|
133
133
|
// Skip the render if the page params haven't changed
|
|
134
|
-
if (!forceRender && paramsPrev !== null &&
|
|
134
|
+
if (!forceRender && paramsPrev !== null && jsonStringifySortKeys(paramsPrev) === jsonStringifySortKeys(this.params)) {
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
137
|
} catch ({message}) {
|
|
@@ -268,8 +268,8 @@ export class MarkdownUp {
|
|
|
268
268
|
return {
|
|
269
269
|
'title': 'MarkdownUp',
|
|
270
270
|
'elements': [
|
|
271
|
-
|
|
272
|
-
this.
|
|
271
|
+
this.burgerElements(),
|
|
272
|
+
schemaMarkdownDoc(markdownUpTypes, 'MarkdownUp', {'params': encodeQueryString(this.params)})
|
|
273
273
|
]
|
|
274
274
|
};
|
|
275
275
|
}
|
|
@@ -332,8 +332,8 @@ export class MarkdownUp {
|
|
|
332
332
|
return {
|
|
333
333
|
'title': markdownTitle,
|
|
334
334
|
'elements': [
|
|
335
|
-
|
|
336
|
-
|
|
335
|
+
this.burgerElements(),
|
|
336
|
+
{'html': 'div', 'attr': {'class': 'markdown'}, 'elem': {'text': markdownText}}
|
|
337
337
|
]
|
|
338
338
|
};
|
|
339
339
|
}
|
|
@@ -342,6 +342,9 @@ export class MarkdownUp {
|
|
|
342
342
|
const result = {
|
|
343
343
|
'title': markdownTitle,
|
|
344
344
|
'elements': [
|
|
345
|
+
// Menu "burger"
|
|
346
|
+
this.burgerElements(),
|
|
347
|
+
|
|
345
348
|
// Add a top hash ID, if necessary
|
|
346
349
|
Object.keys(this.params).length === 0
|
|
347
350
|
? null : {'html': 'div', 'attr': {'id': encodeQueryString(this.params), 'style': 'display=none'}},
|
|
@@ -353,10 +356,7 @@ export class MarkdownUp {
|
|
|
353
356
|
},
|
|
354
357
|
'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
|
|
355
358
|
'headerIds': true
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
// Popup menu burger
|
|
359
|
-
this.burgerElements()
|
|
359
|
+
})
|
|
360
360
|
]
|
|
361
361
|
};
|
|
362
362
|
|
|
@@ -444,12 +444,18 @@ export class MarkdownUp {
|
|
|
444
444
|
}
|
|
445
445
|
|
|
446
446
|
// Reset the runtime and render
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
447
|
+
const elements = runtime.resetElements();
|
|
448
|
+
if (elements !== null) {
|
|
449
|
+
const {body} = this.window.document;
|
|
450
|
+
if (runtime.documentReset === null) {
|
|
451
|
+
renderElements(body, [this.burgerElements(), elements]);
|
|
452
|
+
} else {
|
|
453
|
+
while (body.lastChild !== null && body.lastChild.id !== runtime.documentReset) {
|
|
454
|
+
body.lastChild.remove();
|
|
455
|
+
}
|
|
456
|
+
renderElements(body, [elements], false);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
453
459
|
|
|
454
460
|
// Focus?
|
|
455
461
|
if (runtime.documentFocus !== null) {
|
|
@@ -458,8 +464,8 @@ export class MarkdownUp {
|
|
|
458
464
|
|
|
459
465
|
// Navigate?
|
|
460
466
|
// Note: This is done after render since it may have no effect (in which case we want to render).
|
|
461
|
-
if (windowLocation !== null) {
|
|
462
|
-
this.window.location.href = windowLocation;
|
|
467
|
+
if (runtime.windowLocation !== null) {
|
|
468
|
+
this.window.location.href = runtime.windowLocation;
|
|
463
469
|
}
|
|
464
470
|
|
|
465
471
|
// Reset the runtime
|
package/lib/data.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
/** @module lib/data */
|
|
5
5
|
|
|
6
6
|
import {evaluateExpression} from 'calc-script/lib/runtime.js';
|
|
7
|
+
import {jsonStringifySortKeys} from 'schema-markdown/lib/encode.js';
|
|
7
8
|
import {parseExpression} from 'calc-script/lib/parser.js';
|
|
8
9
|
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
9
10
|
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
@@ -181,9 +182,10 @@ const rDatetime = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2
|
|
|
181
182
|
* @param {?string} [rightExpr = null] - The right join [expression]{@link https://craigahobbs.github.io/calc-script/language/#expressions}
|
|
182
183
|
* @param {boolean} [isLeftJoin = false] - If true, perform a left join (always include left row)
|
|
183
184
|
* @param {?Object} [variables = null] - Additional variables for expression evaluation
|
|
185
|
+
* @param {?Object} [options = null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
184
186
|
* @returns {Object[]} The joined data array
|
|
185
187
|
*/
|
|
186
|
-
export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null) {
|
|
188
|
+
export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null, options = null) {
|
|
187
189
|
// Compute the map of row field name to joined row field name
|
|
188
190
|
const leftNames = {};
|
|
189
191
|
for (const row of leftData) {
|
|
@@ -212,15 +214,25 @@ export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeft
|
|
|
212
214
|
}
|
|
213
215
|
}
|
|
214
216
|
|
|
217
|
+
// Create the evaluation options object
|
|
218
|
+
let evalOptions = options;
|
|
219
|
+
if (variables !== null) {
|
|
220
|
+
evalOptions = (options !== null ? {...options} : {});
|
|
221
|
+
if ('globals' in evalOptions) {
|
|
222
|
+
evalOptions.globals = {...evalOptions.globals, ...variables};
|
|
223
|
+
} else {
|
|
224
|
+
evalOptions.globals = variables;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
215
228
|
// Parse the left and right expressions
|
|
216
|
-
const evalOptions = (variables !== null ? {'globals': variables} : null);
|
|
217
229
|
const leftExpression = parseExpression(joinExpr);
|
|
218
230
|
const rightExpression = (rightExpr !== null ? parseExpression(rightExpr) : leftExpression);
|
|
219
231
|
|
|
220
232
|
// Bucket the right rows by the right expression value
|
|
221
233
|
const rightCategoryRows = {};
|
|
222
234
|
for (const rightRow of rightData) {
|
|
223
|
-
const categoryKey =
|
|
235
|
+
const categoryKey = jsonStringifySortKeys(evaluateExpression(rightExpression, evalOptions, rightRow));
|
|
224
236
|
if (!(categoryKey in rightCategoryRows)) {
|
|
225
237
|
rightCategoryRows[categoryKey] = [];
|
|
226
238
|
}
|
|
@@ -230,7 +242,7 @@ export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeft
|
|
|
230
242
|
// Join the left with the right
|
|
231
243
|
const data = [];
|
|
232
244
|
for (const leftRow of leftData) {
|
|
233
|
-
const categoryKey =
|
|
245
|
+
const categoryKey = jsonStringifySortKeys(evaluateExpression(leftExpression, evalOptions, leftRow));
|
|
234
246
|
if (categoryKey in rightCategoryRows) {
|
|
235
247
|
for (const rightRow of rightCategoryRows[categoryKey]) {
|
|
236
248
|
const joinRow = {...leftRow};
|
|
@@ -255,14 +267,25 @@ export function joinData(leftData, rightData, joinExpr, rightExpr = null, isLeft
|
|
|
255
267
|
* @param {string} fieldName - The calculated field name
|
|
256
268
|
* @param {string} expr - The calculated field expression
|
|
257
269
|
* @param {?Object} [variables = null] - Additional variables for expression evaluation
|
|
270
|
+
* @param {?Object} [options = null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
258
271
|
* @returns {Object[]} The updated data array
|
|
259
272
|
*/
|
|
260
|
-
export function addCalculatedField(data, fieldName, expr, variables = null) {
|
|
273
|
+
export function addCalculatedField(data, fieldName, expr, variables = null, options = null) {
|
|
261
274
|
// Parse the calculation expression
|
|
262
275
|
const calcExpr = parseExpression(expr);
|
|
263
276
|
|
|
277
|
+
// Create the evaluation options object
|
|
278
|
+
let evalOptions = options;
|
|
279
|
+
if (variables !== null) {
|
|
280
|
+
evalOptions = (options !== null ? {...options} : {});
|
|
281
|
+
if ('globals' in evalOptions) {
|
|
282
|
+
evalOptions.globals = {...evalOptions.globals, ...variables};
|
|
283
|
+
} else {
|
|
284
|
+
evalOptions.globals = variables;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
264
288
|
// Compute the calculated field for each row
|
|
265
|
-
const evalOptions = (variables !== null ? {'globals': variables} : null);
|
|
266
289
|
for (const row of data) {
|
|
267
290
|
row[fieldName] = evaluateExpression(calcExpr, evalOptions, row);
|
|
268
291
|
}
|
|
@@ -276,16 +299,27 @@ export function addCalculatedField(data, fieldName, expr, variables = null) {
|
|
|
276
299
|
* @param {Object[]} data - The data array
|
|
277
300
|
* @param {string} expr - The boolean filter [expression]{@link https://craigahobbs.github.io/calc-script/language/#expressions}
|
|
278
301
|
* @param {?Object} [variables = null] - Additional variables for expression evaluation
|
|
302
|
+
* @param {?Object} [options = null] - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
279
303
|
* @returns {Object[]} The filtered data array
|
|
280
304
|
*/
|
|
281
|
-
export function filterData(data, expr, variables = null) {
|
|
305
|
+
export function filterData(data, expr, variables = null, options = null) {
|
|
282
306
|
const result = [];
|
|
283
307
|
|
|
284
308
|
// Parse the filter expression
|
|
285
309
|
const filterExpr = parseExpression(expr);
|
|
286
310
|
|
|
311
|
+
// Create the evaluation options object
|
|
312
|
+
let evalOptions = options;
|
|
313
|
+
if (variables !== null) {
|
|
314
|
+
evalOptions = (options !== null ? {...options} : {});
|
|
315
|
+
if ('globals' in evalOptions) {
|
|
316
|
+
evalOptions.globals = {...evalOptions.globals, ...variables};
|
|
317
|
+
} else {
|
|
318
|
+
evalOptions.globals = variables;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
287
322
|
// Filter the data
|
|
288
|
-
const evalOptions = (variables !== null ? {'globals': variables} : null);
|
|
289
323
|
for (const row of data) {
|
|
290
324
|
if (evaluateExpression(filterExpr, evalOptions, row)) {
|
|
291
325
|
result.push(row);
|
|
@@ -377,7 +411,7 @@ export function aggregateData(data, aggregation) {
|
|
|
377
411
|
|
|
378
412
|
// Get or create the aggregate row
|
|
379
413
|
let aggregateRow;
|
|
380
|
-
const rowKey = (categoryValues !== null ?
|
|
414
|
+
const rowKey = (categoryValues !== null ? jsonStringifySortKeys(categoryValues) : '');
|
|
381
415
|
if (rowKey in categoryRows) {
|
|
382
416
|
aggregateRow = categoryRows[rowKey];
|
|
383
417
|
} else {
|
|
@@ -461,7 +495,7 @@ export function topData(data, count, categoryFields = null) {
|
|
|
461
495
|
const categoryOrder = [];
|
|
462
496
|
for (const row of data) {
|
|
463
497
|
const categoryKey = categoryFields === null ? ''
|
|
464
|
-
:
|
|
498
|
+
: jsonStringifySortKeys(categoryFields.map((field) => (field in row ? row[field] : null)));
|
|
465
499
|
if (!(categoryKey in categoryRows)) {
|
|
466
500
|
categoryRows[categoryKey] = [];
|
|
467
501
|
categoryOrder.push(categoryKey);
|
package/lib/script.js
CHANGED
|
@@ -111,12 +111,12 @@ const pixelsPerPoint = 4 / 3;
|
|
|
111
111
|
*
|
|
112
112
|
* @property {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
|
|
113
113
|
* @property {?Object[]} elements - The runtime-generated element model
|
|
114
|
-
* @property {?string} documentFocus - The
|
|
115
|
-
* @property {?string}
|
|
116
|
-
* @property {
|
|
117
|
-
* @property {?string} windowLocation - The
|
|
118
|
-
* @property {?function} windowResize - The
|
|
119
|
-
* @property {?Array} windowTimeout - The
|
|
114
|
+
* @property {?string} documentFocus - The runtime-set input-focus element ID
|
|
115
|
+
* @property {?string} documentReset - The runtime-set document-reset element ID
|
|
116
|
+
* @property {?string} documentTitle - The runtime-set document title
|
|
117
|
+
* @property {?string} windowLocation - The runtime-set document location
|
|
118
|
+
* @property {?function} windowResize - The runtime-set resize callback
|
|
119
|
+
* @property {?Array} windowTimeout - The runtime-set timeout args (callback, delay)
|
|
120
120
|
*
|
|
121
121
|
* @ignore
|
|
122
122
|
*/
|
|
@@ -128,8 +128,8 @@ export class MarkdownScriptRuntime {
|
|
|
128
128
|
this.options = options;
|
|
129
129
|
this.elements = null;
|
|
130
130
|
this.documentFocus = null;
|
|
131
|
+
this.documentReset = null;
|
|
131
132
|
this.documentTitle = null;
|
|
132
|
-
this.isDocumentReset = false;
|
|
133
133
|
this.windowLocation = null;
|
|
134
134
|
this.windowResize = null;
|
|
135
135
|
this.windowTimeout = null;
|
|
@@ -160,8 +160,8 @@ export class MarkdownScriptRuntime {
|
|
|
160
160
|
// Reset the runtime state
|
|
161
161
|
reset() {
|
|
162
162
|
this.documentFocus = null;
|
|
163
|
+
this.documentReset = null;
|
|
163
164
|
this.documentTitle = null;
|
|
164
|
-
this.isDocumentReset = false;
|
|
165
165
|
this.windowLocation = null;
|
|
166
166
|
this.windowResize = null;
|
|
167
167
|
this.windowTimeout = null;
|
package/lib/scriptLibrary.js
CHANGED
|
@@ -45,7 +45,9 @@ export const markdownScriptFunctions = {
|
|
|
45
45
|
// $arg expr: The calculated field expression
|
|
46
46
|
// $arg variables: Optional (default is null). A variables object the expression evaluation.
|
|
47
47
|
// $return: The updated data array
|
|
48
|
-
'dataCalculatedField': ([data, fieldName, expr, variables = null]) =>
|
|
48
|
+
'dataCalculatedField': ([data, fieldName, expr, variables = null], options) => (
|
|
49
|
+
addCalculatedField(data, fieldName, expr, variables, options)
|
|
50
|
+
),
|
|
49
51
|
|
|
50
52
|
// $function: dataFilter
|
|
51
53
|
// $group: Data
|
|
@@ -54,7 +56,7 @@ export const markdownScriptFunctions = {
|
|
|
54
56
|
// $arg expr: The filter expression
|
|
55
57
|
// $arg variables: Optional (default is null). A variables object the expression evaluation.
|
|
56
58
|
// $return: The filtered data array
|
|
57
|
-
'dataFilter': ([data, expr, variables = null]) => filterData(data, expr, variables),
|
|
59
|
+
'dataFilter': ([data, expr, variables = null], options) => filterData(data, expr, variables, options),
|
|
58
60
|
|
|
59
61
|
// $function: dataJoin
|
|
60
62
|
// $group: Data
|
|
@@ -67,8 +69,8 @@ export const markdownScriptFunctions = {
|
|
|
67
69
|
// $arg isLeftJoin: Optional (default is false). If true, perform a left join (always include left row).
|
|
68
70
|
// $arg variables: Optional (default is null). A variables object for join expression evaluation.
|
|
69
71
|
// $return: The joined data array
|
|
70
|
-
'dataJoin': ([leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null]) => (
|
|
71
|
-
joinData(leftData, rightData, joinExpr, rightExpr, isLeftJoin, variables)
|
|
72
|
+
'dataJoin': ([leftData, rightData, joinExpr, rightExpr = null, isLeftJoin = false, variables = null], options) => (
|
|
73
|
+
joinData(leftData, rightData, joinExpr, rightExpr, isLeftJoin, variables, options)
|
|
72
74
|
),
|
|
73
75
|
|
|
74
76
|
// $function: dataLineChart
|
|
@@ -144,14 +146,6 @@ export const markdownScriptFunctions = {
|
|
|
144
146
|
// Document functions
|
|
145
147
|
//
|
|
146
148
|
|
|
147
|
-
// $function: documentReset
|
|
148
|
-
// $group: Document
|
|
149
|
-
// $doc: Reset the document. This is useful within callback functions that re-render the document.
|
|
150
|
-
'documentReset': (unused, options) => {
|
|
151
|
-
const {runtime} = options;
|
|
152
|
-
runtime.isDocumentReset = true;
|
|
153
|
-
},
|
|
154
|
-
|
|
155
149
|
// $function: documentURL
|
|
156
150
|
// $group: Document
|
|
157
151
|
// $doc: Fix-up relative URLs
|
|
@@ -196,6 +190,15 @@ export const markdownScriptFunctions = {
|
|
|
196
190
|
runtime.documentFocus = id;
|
|
197
191
|
},
|
|
198
192
|
|
|
193
|
+
// $function: setDocumentReset
|
|
194
|
+
// $group: Document
|
|
195
|
+
// $doc: Set the document reset element
|
|
196
|
+
// $arg id: The element ID
|
|
197
|
+
'setDocumentReset': ([id], options) => {
|
|
198
|
+
const {runtime} = options;
|
|
199
|
+
runtime.documentReset = id;
|
|
200
|
+
},
|
|
201
|
+
|
|
199
202
|
// $function: setDocumentTitle
|
|
200
203
|
// $group: Document
|
|
201
204
|
// $doc: Set the document title
|
|
@@ -220,9 +223,15 @@ export const markdownScriptFunctions = {
|
|
|
220
223
|
// $arg callback: The window resize callback function
|
|
221
224
|
'setWindowResize': ([callback], options) => {
|
|
222
225
|
const {runtime} = options;
|
|
223
|
-
runtime.windowResize = () => {
|
|
226
|
+
runtime.windowResize = async () => {
|
|
224
227
|
options.statementCount = 0;
|
|
225
|
-
|
|
228
|
+
try {
|
|
229
|
+
await callback([], options);
|
|
230
|
+
} catch ({message}) {
|
|
231
|
+
if ('logFn' in options) {
|
|
232
|
+
options.logFn(`MarkdownUp: Error executing setWindowResize callback: ${message}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
226
235
|
options.runtimeUpdateFn();
|
|
227
236
|
};
|
|
228
237
|
},
|
|
@@ -235,9 +244,15 @@ export const markdownScriptFunctions = {
|
|
|
235
244
|
'setWindowTimeout': ([callback, delay], options) => {
|
|
236
245
|
const {runtime} = options;
|
|
237
246
|
runtime.windowTimeout = [
|
|
238
|
-
() => {
|
|
247
|
+
async () => {
|
|
239
248
|
options.statementCount = 0;
|
|
240
|
-
|
|
249
|
+
try {
|
|
250
|
+
await callback([], options);
|
|
251
|
+
} catch ({message}) {
|
|
252
|
+
if ('logFn' in options) {
|
|
253
|
+
options.logFn(`MarkdownUp: Error executing setWindowTimeout callback: ${message}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
241
256
|
options.runtimeUpdateFn();
|
|
242
257
|
},
|
|
243
258
|
delay
|
|
@@ -387,17 +402,41 @@ export const markdownScriptFunctions = {
|
|
|
387
402
|
'drawOnClick': ([callback], options) => {
|
|
388
403
|
const {runtime} = options;
|
|
389
404
|
const svg = runtime.setDrawing();
|
|
390
|
-
const clickElement = svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1];
|
|
405
|
+
const clickElement = (svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1]);
|
|
406
|
+
clickElement.attr.style = 'cursor: pointer;';
|
|
391
407
|
clickElement.callback = (element) => {
|
|
392
|
-
element.addEventListener('click', (event) => {
|
|
408
|
+
element.addEventListener('click', async (event) => {
|
|
393
409
|
const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
|
|
394
410
|
options.statementCount = 0;
|
|
395
|
-
|
|
411
|
+
try {
|
|
412
|
+
await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
|
|
413
|
+
} catch ({message}) {
|
|
414
|
+
if ('logFn' in options) {
|
|
415
|
+
options.logFn(`MarkdownUp: Error executing drawOnClick callback: ${message}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
396
418
|
options.runtimeUpdateFn();
|
|
397
419
|
});
|
|
398
420
|
};
|
|
399
421
|
},
|
|
400
422
|
|
|
423
|
+
// $function: drawPathRect
|
|
424
|
+
// $group: Drawing
|
|
425
|
+
// $doc: Draw a rectangle as a path
|
|
426
|
+
// $arg x: The x-coordinate of the top-left of the rectangle
|
|
427
|
+
// $arg y: The y-coordinate of the top-left of the rectangle
|
|
428
|
+
// $arg width: The width of the rectangle
|
|
429
|
+
// $arg height: The height of the rectangle
|
|
430
|
+
'drawPathRect': ([x, y, width, height], options) => {
|
|
431
|
+
const {runtime} = options;
|
|
432
|
+
runtime.setDrawingPath();
|
|
433
|
+
runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
|
|
434
|
+
runtime.drawingPath.push(`H ${(x + width).toFixed(svgPrecision)}`);
|
|
435
|
+
runtime.drawingPath.push(`V ${(y + height).toFixed(svgPrecision)}`);
|
|
436
|
+
runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
|
|
437
|
+
runtime.drawingPath.push('Z');
|
|
438
|
+
},
|
|
439
|
+
|
|
401
440
|
// $function: drawRect
|
|
402
441
|
// $group: Drawing
|
|
403
442
|
// $doc: Draw a rectangle
|
|
@@ -559,6 +598,10 @@ export const markdownScriptFunctions = {
|
|
|
559
598
|
// $function: elementModelRender
|
|
560
599
|
// $group: Element Model
|
|
561
600
|
// $doc: Render an [element model](https://github.com/craigahobbs/element-model#readme)
|
|
601
|
+
// $doc:
|
|
602
|
+
// $doc: **Note:** Element model "callback" members are a map of event name (e.g., "click") to
|
|
603
|
+
// $doc: event callback function. Data can be passed to the callback functions by setting a
|
|
604
|
+
// $doc: "callbackData" member.
|
|
562
605
|
// $arg element: The [element model](https://github.com/craigahobbs/element-model#readme)
|
|
563
606
|
'elementModelRender': ([elements], options) => {
|
|
564
607
|
const {runtime} = options;
|
|
@@ -697,38 +740,59 @@ export const markdownScriptFunctions = {
|
|
|
697
740
|
|
|
698
741
|
// Helper function for elementModelRender function
|
|
699
742
|
function elementModelWrapCallbacks(elements, options) {
|
|
743
|
+
// Ignore non-objects
|
|
744
|
+
if (elements === null || typeof elements !== 'object') {
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Array?
|
|
700
749
|
if (Array.isArray(elements)) {
|
|
701
750
|
for (const childElements of elements) {
|
|
702
751
|
elementModelWrapCallbacks(childElements, options);
|
|
703
752
|
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// Wrap child elements
|
|
757
|
+
const elementsElem = elements.elem ?? null;
|
|
758
|
+
if (elementsElem !== null) {
|
|
759
|
+
elementModelWrapCallbacks(elementsElem, options);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// Element callback attribute must be map of event => callback
|
|
763
|
+
const elementEvents = elements.callback ?? null;
|
|
764
|
+
if (elementEvents !== null && typeof elementEvents === 'object') {
|
|
765
|
+
// Get the callback data, if any
|
|
766
|
+
let callbackData = null;
|
|
767
|
+
if ('callbackData' in elements) {
|
|
768
|
+
({callbackData} = elements);
|
|
769
|
+
delete elements.callbackData;
|
|
707
770
|
}
|
|
708
771
|
|
|
709
|
-
//
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
options.
|
|
728
|
-
}
|
|
772
|
+
// Wrap the event handler function
|
|
773
|
+
elements.callback = (element) => {
|
|
774
|
+
// On element render, add a listener for each event
|
|
775
|
+
for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
|
|
776
|
+
element.addEventListener(elementEvent, async (event) => {
|
|
777
|
+
// Determine the event callback args
|
|
778
|
+
const eventArgs = [];
|
|
779
|
+
if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
|
|
780
|
+
eventArgs.push(event.keyCode);
|
|
781
|
+
}
|
|
782
|
+
eventArgs.push(callbackData);
|
|
783
|
+
|
|
784
|
+
// Call the event handler
|
|
785
|
+
options.statementCount = 0;
|
|
786
|
+
try {
|
|
787
|
+
await elementEventCallback(eventArgs, options);
|
|
788
|
+
} catch ({message}) {
|
|
789
|
+
if ('logFn' in options) {
|
|
790
|
+
options.logFn(`MarkdownUp: Error executing elementModelRender callback: ${message}`);
|
|
791
|
+
}
|
|
729
792
|
}
|
|
730
|
-
|
|
793
|
+
options.runtimeUpdateFn();
|
|
794
|
+
});
|
|
731
795
|
}
|
|
732
|
-
}
|
|
796
|
+
};
|
|
733
797
|
}
|
|
734
798
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "markdown-up",
|
|
4
|
-
"version": "1.8.
|
|
4
|
+
"version": "1.8.10",
|
|
5
5
|
"description": "MarkdownUp is a Markdown viewer",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"markdown",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"ava": "~5.1",
|
|
33
33
|
"c8": "~7.12",
|
|
34
|
-
"eslint": "~8.
|
|
34
|
+
"eslint": "~8.32",
|
|
35
35
|
"jsdoc": "~4.0",
|
|
36
|
-
"jsdom": "~
|
|
36
|
+
"jsdom": "~21.1"
|
|
37
37
|
}
|
|
38
38
|
}
|