markdown-up 3.12.1 → 4.0.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.
@@ -1,71 +1,12 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
- import {dataTableElements, validateDataTable} from './dataTable.js';
5
- import {escapeMarkdownText, getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
6
- import {lineChartElements, validateLineChart} from './lineChart.js';
7
- import {markdownElements, markdownHeaderId as markdownModelHeaderId} from 'markdown-model/lib/elements.js';
8
4
  import {valueArgsModel, valueArgsValidate} from 'bare-script/lib/value.js';
9
5
  import {validateElements} from 'element-model/lib/elementModel.js';
10
6
 
11
7
 
12
- /* eslint-disable id-length */
13
-
14
-
15
8
  // Constants
16
- const defaultFontFamily = 'Arial, Helvetica, sans-serif';
17
- const fontWidthRatio = 0.6;
18
9
  const pixelsPerPoint = 4 / 3;
19
- const svgPrecision = 8;
20
-
21
-
22
- //
23
- // Data functions
24
- //
25
-
26
-
27
- // $function: dataLineChart
28
- // $group: Data
29
- // $doc: Draw a line chart
30
- // $arg data: The data array
31
- // $arg lineChart: The [line chart model](model.html#var.vName='LineChart')
32
- function dataLineChart(args, options) {
33
- const [data, lineChart] = valueArgsValidate(dataLineChartArgs, args);
34
- const {runtime} = options;
35
-
36
- // Render the line chart
37
- const elements = lineChartElements(data, validateLineChart(lineChart), options);
38
- runtime.setElements();
39
- runtime.addElements({'html': 'p', 'elem': elements});
40
-
41
- // Set the drawing size (the chart is now the active drawing)
42
- runtime.drawingWidth = elements.attr.width;
43
- runtime.drawingHeight = elements.attr.height;
44
- }
45
-
46
- const dataLineChartArgs = valueArgsModel([
47
- {'name': 'data', 'type': 'array'},
48
- {'name': 'lineChart', 'type': 'object'}
49
- ]);
50
-
51
-
52
- // $function: dataTable
53
- // $group: Data
54
- // $doc: Draw a data table
55
- // $arg data: The data array
56
- // $arg dataTable: Optional (default is null). The [data table model](model.html#var.vName='DataTable').
57
- function dataTable(args, options) {
58
- const [data, dataTableModel] = valueArgsValidate(dataTableArgs, args);
59
- const {runtime} = options;
60
- runtime.setElements();
61
- const dataTableValidated = (dataTableModel !== null ? validateDataTable(dataTableModel, options) : null);
62
- runtime.addElements(dataTableElements(data, dataTableValidated, options));
63
- }
64
-
65
- const dataTableArgs = valueArgsModel([
66
- {'name': 'data', 'type': 'array'},
67
- {'name': 'dataTable', 'type': 'object', 'nullable': true}
68
- ]);
69
10
 
70
11
 
71
12
  //
@@ -74,7 +15,7 @@ const dataTableArgs = valueArgsModel([
74
15
 
75
16
 
76
17
  // $function: documentFontSize
77
- // $group: Document
18
+ // $group: document
78
19
  // $doc: Get the document font size
79
20
  // $return: The document font size, in pixels
80
21
  function documentFontSize(unusedArgs, options) {
@@ -83,7 +24,7 @@ function documentFontSize(unusedArgs, options) {
83
24
 
84
25
 
85
26
  // $function: documentInputValue
86
- // $group: Document
27
+ // $group: document
87
28
  // $doc: Get an input element's value
88
29
  // $arg id: The input element ID
89
30
  // $return: The input element value or null if the element does not exist
@@ -99,7 +40,7 @@ const documentInputValueArgs = valueArgsModel([
99
40
 
100
41
 
101
42
  // $function: documentSetFocus
102
- // $group: Document
43
+ // $group: document
103
44
  // $doc: Set focus to an element
104
45
  // $arg id: The element ID
105
46
  function documentSetFocus(args, options) {
@@ -114,7 +55,7 @@ const documentSetFocusArgs = valueArgsModel([
114
55
 
115
56
 
116
57
  // $function: documentSetKeyDown
117
- // $group: Document
58
+ // $group: document
118
59
  // $doc: Set the document keydown event handler. For example:
119
60
  // $doc:
120
61
  // $doc: ```barescript
@@ -187,7 +128,7 @@ const documentSetKeyDownArgs = valueArgsModel([
187
128
 
188
129
 
189
130
  // $function: documentSetReset
190
- // $group: Document
131
+ // $group: document
191
132
  // $doc: Set the document reset element
192
133
  // $arg id: The element ID
193
134
  function documentSetReset(args, options) {
@@ -202,7 +143,7 @@ const documentSetResetArgs = valueArgsModel([
202
143
 
203
144
 
204
145
  // $function: documentSetTitle
205
- // $group: Document
146
+ // $group: document
206
147
  // $doc: Set the document title
207
148
  // $arg title: The document title string
208
149
  function documentSetTitle(args, options) {
@@ -217,7 +158,7 @@ const documentSetTitleArgs = valueArgsModel([
217
158
 
218
159
 
219
160
  // $function: documentURL
220
- // $group: Document
161
+ // $group: document
221
162
  // $doc: Fix-up relative URLs
222
163
  // $arg url: The URL
223
164
  // $return: The fixed-up URL
@@ -231,496 +172,13 @@ const documentURLArgs = valueArgsModel([
231
172
  ]);
232
173
 
233
174
 
234
- //
235
- // Drawing functions
236
- //
237
-
238
-
239
- // $function: drawArc
240
- // $group: Drawing
241
- // $doc: Draw an arc curve from the current point to the end point
242
- // $arg rx: The arc ellipse's x-radius
243
- // $arg ry: The arc ellipse's y-radius
244
- // $arg angle: The rotation (in degrees) of the ellipse relative to the x-axis
245
- // $arg largeArcFlag: Either large arc (1) or small arc (0)
246
- // $arg sweepFlag: Either clockwise turning arc (1) or counterclockwise turning arc (0)
247
- // $arg x: The x-coordinate of the end point
248
- // $arg y: The y-coordinate of the end point
249
- function drawArc(args, options) {
250
- const [rx, ry, angle, largeArcFlag, sweepFlag, x, y] = valueArgsValidate(drawArcArgs, args);
251
- const {runtime} = options;
252
- runtime.setDrawingPath();
253
- runtime.drawingPath.push(
254
- `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
255
- `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
256
- );
257
- }
258
-
259
- const drawArcArgs = valueArgsModel([
260
- {'name': 'rx', 'type': 'number'},
261
- {'name': 'ry', 'type': 'number'},
262
- {'name': 'angle', 'type': 'number'},
263
- {'name': 'largeArcFlag', 'type': 'boolean'},
264
- {'name': 'sweepFlag', 'type': 'boolean'},
265
- {'name': 'x', 'type': 'number'},
266
- {'name': 'y', 'type': 'number'},
267
- ]);
268
-
269
-
270
- // $function: drawCircle
271
- // $group: Drawing
272
- // $doc: Draw a circle
273
- // $arg cx: The x-coordinate of the center of the circle
274
- // $arg cy: The y-coordinate of the center of the circle
275
- // $arg r: The radius of the circle
276
- function drawCircle(args, options) {
277
- const [cx, cy, r] = valueArgsValidate(drawCircleArgs, args);
278
- const {runtime} = options;
279
- const svg = runtime.setDrawing();
280
- svg.elem.push({
281
- 'svg': 'circle',
282
- 'attr': {
283
- 'fill': runtime.drawingPathFill,
284
- 'stroke': runtime.drawingPathStroke,
285
- 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
286
- 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
287
- 'cx': cx,
288
- 'cy': cy,
289
- 'r': r
290
- }
291
- });
292
- }
293
-
294
- const drawCircleArgs = valueArgsModel([
295
- {'name': 'cx', 'type': 'number'},
296
- {'name': 'cy', 'type': 'number'},
297
- {'name': 'r', 'type': 'number'}
298
- ]);
299
-
300
-
301
- // $function: drawClose
302
- // $group: Drawing
303
- // $doc: Close the current drawing path
304
- function drawClose(unusedArgs, options) {
305
- const {runtime} = options;
306
- runtime.setDrawingPath();
307
- runtime.drawingPath.push('Z');
308
- }
309
-
310
-
311
- // $function: drawEllipse
312
- // $group: Drawing
313
- // $doc: Draw an ellipse
314
- // $arg cx: The x-coordinate of the center of the ellipse
315
- // $arg cy: The y-coordinate of the center of the ellipse
316
- // $arg rx: The x-radius of the ellipse
317
- // $arg ry: The y-radius of the ellipse
318
- function drawEllipse(args, options) {
319
- const [cx, cy, rx, ry] = valueArgsValidate(drawEllipseArgs, args);
320
- const {runtime} = options;
321
- const svg = runtime.setDrawing();
322
- svg.elem.push({
323
- 'svg': 'ellipse',
324
- 'attr': {
325
- 'fill': runtime.drawingPathFill,
326
- 'stroke': runtime.drawingPathStroke,
327
- 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
328
- 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
329
- 'cx': cx,
330
- 'cy': cy,
331
- 'rx': rx,
332
- 'ry': ry
333
- }
334
- });
335
- }
336
-
337
- const drawEllipseArgs = valueArgsModel([
338
- {'name': 'cx', 'type': 'number'},
339
- {'name': 'cy', 'type': 'number'},
340
- {'name': 'rx', 'type': 'number'},
341
- {'name': 'ry', 'type': 'number'}
342
- ]);
343
-
344
-
345
- // $function: drawHLine
346
- // $group: Drawing
347
- // $doc: Draw a horizontal line from the current point to the end point
348
- // $arg x: The x-coordinate of the end point
349
- function drawHLine(args, options) {
350
- const [x] = valueArgsValidate(drawHLineArgs, args);
351
- const {runtime} = options;
352
- runtime.setDrawingPath();
353
- runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
354
- }
355
-
356
- const drawHLineArgs = valueArgsModel([
357
- {'name': 'x', 'type': 'number'}
358
- ]);
359
-
360
-
361
- // $function: drawHeight
362
- // $group: Drawing
363
- // $doc: Get the current drawing's height
364
- // $return: The current drawing's height
365
- function drawHeight(unusedArgs, options) {
366
- return options.runtime.drawingHeight;
367
- }
368
-
369
-
370
- // $function: drawImage
371
- // $group: Drawing
372
- // $doc: Draw an image
373
- // $arg x: The x-coordinate of the center of the image
374
- // $arg y: The y-coordinate of the center of the image
375
- // $arg width: The width of the image
376
- // $arg height: The height of the image
377
- // $arg href: The image resource URL
378
- function drawImage(args, options) {
379
- const [x, y, width, height, href] = valueArgsValidate(drawImageArgs, args);
380
- const {runtime} = options;
381
- const svg = runtime.setDrawing();
382
- svg.elem.push({
383
- 'svg': 'image',
384
- 'attr': {
385
- 'x': x,
386
- 'y': y,
387
- 'width': width,
388
- 'height': height,
389
- 'href': options.urlFn(href)
390
- }
391
- });
392
- }
393
-
394
- const drawImageArgs = valueArgsModel([
395
- {'name': 'x', 'type': 'number'},
396
- {'name': 'y', 'type': 'number'},
397
- {'name': 'width', 'type': 'number'},
398
- {'name': 'height', 'type': 'number'},
399
- {'name': 'href', 'type': 'string'}
400
- ]);
401
-
402
-
403
- // $function: drawLine
404
- // $group: Drawing
405
- // $doc: Draw a line from the current point to the end point
406
- // $arg x: The x-coordinate of the end point
407
- // $arg y: The y-coordinate of the end point
408
- function drawLine(args, options) {
409
- const [x, y] = valueArgsValidate(drawLineArgs, args);
410
- const {runtime} = options;
411
- runtime.setDrawingPath();
412
- runtime.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
413
- }
414
-
415
- const drawLineArgs = valueArgsModel([
416
- {'name': 'x', 'type': 'number'},
417
- {'name': 'y', 'type': 'number'}
418
- ]);
419
-
420
-
421
- // $function: drawMove
422
- // $group: Drawing
423
- // $doc: Move the path's drawing point
424
- // $arg x: The x-coordinate of the new drawing point
425
- // $arg y: The y-coordinate of the new drawing point
426
- function drawMove(args, options) {
427
- const [x, y] = valueArgsValidate(drawMoveArgs, args);
428
- const {runtime} = options;
429
- runtime.setDrawingPath();
430
- runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
431
- }
432
-
433
- const drawMoveArgs = valueArgsModel([
434
- {'name': 'x', 'type': 'number'},
435
- {'name': 'y', 'type': 'number'}
436
- ]);
437
-
438
-
439
- // $function: drawNew
440
- // $group: Drawing
441
- // $doc: Create a new drawing
442
- // $arg width: The width of the drawing
443
- // $arg height: The height of the drawing
444
- function drawNew(args, options) {
445
- const [width, height] = valueArgsValidate(drawNewArgs, args);
446
- const {runtime} = options;
447
- runtime.drawingWidth = width;
448
- runtime.drawingHeight = height;
449
- runtime.setDrawing(true);
450
- }
451
-
452
- const drawNewArgs = valueArgsModel([
453
- {'name': 'width', 'type': 'number'},
454
- {'name': 'height', 'type': 'number'}
455
- ]);
456
-
457
-
458
- // $function: drawOnClick
459
- // $group: Drawing
460
- // $doc: Set the most recent drawing object's on-click event handler
461
- // $arg callback: The on-click event callback function (x, y)
462
- function drawOnClick(args, options) {
463
- const [callback] = valueArgsValidate(drawOnClickArgs, args);
464
- const {runtime} = options;
465
- const svg = runtime.setDrawing();
466
- const clickElement = (svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1]);
467
- clickElement.attr.style = 'cursor: pointer;';
468
- clickElement.callback = (element) => {
469
- element.addEventListener('click', async (event) => {
470
- const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
471
- options.statementCount = 0;
472
- try {
473
- await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
474
- } catch ({message}) {
475
- if ('logFn' in options && options.debug) {
476
- options.logFn(`MarkdownUp: Error executing drawOnClick callback: ${message}`);
477
- }
478
- }
479
- options.runtimeUpdateFn();
480
- });
481
- };
482
- }
483
-
484
- const drawOnClickArgs = valueArgsModel([
485
- {'name': 'callback', 'type': 'function'}
486
- ]);
487
-
488
-
489
- // $function: drawPathRect
490
- // $group: Drawing
491
- // $doc: Draw a rectangle as a path
492
- // $arg x: The x-coordinate of the top-left of the rectangle
493
- // $arg y: The y-coordinate of the top-left of the rectangle
494
- // $arg width: The width of the rectangle
495
- // $arg height: The height of the rectangle
496
- function drawPathRect(args, options) {
497
- const [x, y, width, height] = valueArgsValidate(drawPathRectArgs, args);
498
- const {runtime} = options;
499
- runtime.setDrawingPath();
500
- runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
501
- runtime.drawingPath.push(`H ${(x + width).toFixed(svgPrecision)}`);
502
- runtime.drawingPath.push(`V ${(y + height).toFixed(svgPrecision)}`);
503
- runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
504
- runtime.drawingPath.push('Z');
505
- }
506
-
507
- const drawPathRectArgs = valueArgsModel([
508
- {'name': 'x', 'type': 'number'},
509
- {'name': 'y', 'type': 'number'},
510
- {'name': 'width', 'type': 'number'},
511
- {'name': 'height', 'type': 'number'}
512
- ]);
513
-
514
-
515
- // $function: drawRect
516
- // $group: Drawing
517
- // $doc: Draw a rectangle
518
- // $arg x: The x-coordinate of the top-left of the rectangle
519
- // $arg y: The y-coordinate of the top-left of the rectangle
520
- // $arg width: The width of the rectangle
521
- // $arg height: The height of the rectangle
522
- // $arg rx: Optional (default is null). The horizontal corner radius of the rectangle.
523
- // $arg ry: Optional (default is null). The vertical corner radius of the rectangle.
524
- function drawRect(args, options) {
525
- const [x, y, width, height, rx = null, ry = null] = valueArgsValidate(drawRectArgs, args);
526
- const {runtime} = options;
527
- const svg = runtime.setDrawing();
528
- const element = {
529
- 'svg': 'rect',
530
- 'attr': {
531
- 'fill': runtime.drawingPathFill,
532
- 'stroke': runtime.drawingPathStroke,
533
- 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
534
- 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
535
- 'x': x,
536
- 'y': y,
537
- 'width': width,
538
- 'height': height
539
- }
540
- };
541
- if (rx !== null) {
542
- element.attr.rx = rx;
543
- }
544
- if (ry !== null) {
545
- element.attr.ry = ry;
546
- }
547
- svg.elem.push(element);
548
- }
549
-
550
- const drawRectArgs = valueArgsModel([
551
- {'name': 'x', 'type': 'number'},
552
- {'name': 'y', 'type': 'number'},
553
- {'name': 'width', 'type': 'number'},
554
- {'name': 'height', 'type': 'number'},
555
- {'name': 'rx', 'type': 'number', 'nullable': true},
556
- {'name': 'ry', 'type': 'number', 'nullable': true}
557
- ]);
558
-
559
-
560
- // $function: drawStyle
561
- // $group: Drawing
562
- // $doc: Set the current drawing styles
563
- // $arg stroke: Optional (default is 'black'). The stroke color.
564
- // $arg strokeWidth: Optional (default is 1). The stroke width.
565
- // $arg fill: Optional (default is 'none'). The fill color.
566
- // $arg strokeDashArray: Optional (default is 'none'). The stroke
567
- // $arg strokeDashArray: [dash array](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray#usage_notes).
568
- function drawStyle(args, options) {
569
- const [stroke, strokeWidth, fill, strokeDashArray] = valueArgsValidate(drawStyleArgs, args);
570
- const {runtime} = options;
571
- if (stroke !== runtime.drawingPathStroke || strokeWidth !== runtime.drawingPathStrokeWidth ||
572
- strokeDashArray !== runtime.drawingPathStrokeDashArray || fill !== runtime.drawingPathFill
573
- ) {
574
- runtime.setDrawing();
575
- runtime.drawingPathStroke = stroke;
576
- runtime.drawingPathStrokeWidth = strokeWidth;
577
- runtime.drawingPathStrokeDashArray = strokeDashArray;
578
- runtime.drawingPathFill = fill;
579
- }
580
- }
581
-
582
- const drawStyleArgs = valueArgsModel([
583
- {'name': 'stroke', 'type': 'string', 'default': 'black'},
584
- {'name': 'strokeWidth', 'type': 'number', 'default': 1},
585
- {'name': 'fill', 'type': 'string', 'default': 'none'},
586
- {'name': 'strokeDashArray', 'type': 'string', 'default': 'none'}
587
- ]);
588
-
589
-
590
- // $function: drawText
591
- // $group: Drawing
592
- // $doc: Draw text
593
- // $arg text: The text to draw
594
- // $arg x: The x-coordinate of the text
595
- // $arg y: The y-coordinate of the text
596
- // $arg textAnchor: Optional (default is 'middle'). The
597
- // $arg textAnchor: [text anchor](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor#usage_notes) style.
598
- // $arg dominantBaseline: Optional (default is 'middle'). The
599
- // $arg dominantBaseline: [dominant baseline](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline#usage_notes)
600
- // $arg dominantBaseline: style.
601
- function drawText(args, options) {
602
- const [text, x, y, textAnchor, dominantBaseline] = valueArgsValidate(drawTextArgs, args);
603
- const {runtime} = options;
604
- const svg = runtime.setDrawing();
605
- svg.elem.push({
606
- 'svg': 'text',
607
- 'attr': {
608
- 'fill': runtime.drawingFontFill,
609
- 'font-family': runtime.drawingFontFamily,
610
- 'font-size': runtime.drawingFontSizePx.toFixed(svgPrecision),
611
- 'text-anchor': textAnchor,
612
- 'dominant-baseline': dominantBaseline,
613
- 'font-weight': (runtime.drawingFontBold ? 'bold' : 'normal'),
614
- 'font-style': (runtime.drawingFontItalic ? 'italic' : 'normal'),
615
- 'x': x,
616
- 'y': y
617
- },
618
- 'elem': {'text': text}
619
- });
620
- }
621
-
622
- const drawTextArgs = valueArgsModel([
623
- {'name': 'text', 'type': 'string'},
624
- {'name': 'x', 'type': 'number'},
625
- {'name': 'y', 'type': 'number'},
626
- {'name': 'textAnchor', 'type': 'string', 'default': 'middle'},
627
- {'name': 'dominantBaseline', 'type': 'string', 'default': 'middle'}
628
- ]);
629
-
630
-
631
- // $function: drawTextHeight
632
- // $group: Drawing
633
- // $doc: Compute the text's height to fit the width
634
- // $arg text: The text
635
- // $arg width: The width of the text. If 0, the default font size (in pixels) is returned.
636
- // $return: The text's height, in pixels
637
- function drawTextHeight(args, options) {
638
- const [text, width] = valueArgsValidate(drawTextHeightArgs, args);
639
- return width > 0 ? width / (fontWidthRatio * text.length) : options.runtime.drawingFontSizePx;
640
- }
641
-
642
- const drawTextHeightArgs = valueArgsModel([
643
- {'name': 'text', 'type': 'string'},
644
- {'name': 'width', 'type': 'number'},
645
- ]);
646
-
647
-
648
- // $function: drawTextStyle
649
- // $group: Drawing
650
- // $doc: Set the current text drawing styles
651
- // $arg fontSizePx: Optional (default is null, the default font size). The text font size, in pixels.
652
- // $arg textFill: Optional (default is 'black'). The text fill color.
653
- // $arg bold: Optional (default is false). If true, text is bold.
654
- // $arg italic: Optional (default is false). If true, text is italic.
655
- // $arg fontFamily: Optional (default is null, the default font family). The text font family.
656
- function drawTextStyle(args, options) {
657
- const [fontSizePx, textFill, bold, italic, fontFamily] = valueArgsValidate(drawTextStyleArgs, args);
658
- const {runtime} = options;
659
- runtime.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : options.fontSize * pixelsPerPoint);
660
- runtime.drawingFontFill = textFill;
661
- runtime.drawingFontBold = bold;
662
- runtime.drawingFontItalic = italic;
663
- runtime.drawingFontFamily = fontFamily;
664
- }
665
-
666
- const drawTextStyleArgs = valueArgsModel([
667
- {'name': 'fontSizePx', 'type': 'number', 'nullable': true},
668
- {'name': 'textFill', 'type': 'string', 'default': 'black'},
669
- {'name': 'bold', 'type': 'boolean', 'default': false},
670
- {'name': 'italic', 'type': 'boolean', 'default': false},
671
- {'name': 'fontFamily', 'type': 'string', 'default': defaultFontFamily},
672
- ]);
673
-
674
-
675
- // $function: drawTextWidth
676
- // $group: Drawing
677
- // $doc: Compute the text's width
678
- // $arg text: The text
679
- // $arg fontSizePx: The text font size, in pixels
680
- // $return: The text's width, in pixels
681
- function drawTextWidth(args) {
682
- const [text, fontSizePx] = valueArgsValidate(drawTextWidthArgs, args);
683
- return fontWidthRatio * fontSizePx * text.length;
684
- }
685
-
686
- const drawTextWidthArgs = valueArgsModel([
687
- {'name': 'text', 'type': 'string'},
688
- {'name': 'fontSizePx', 'type': 'number'}
689
- ]);
690
-
691
-
692
- // $function: drawVLine
693
- // $group: Drawing
694
- // $doc: Draw a vertical line from the current point to the end point
695
- // $arg y: The y-coordinate of the end point
696
- function drawVLine(args, options) {
697
- const [y] = valueArgsValidate(drawVLineArgs, args);
698
- const {runtime} = options;
699
- runtime.setDrawingPath();
700
- runtime.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
701
- }
702
-
703
- const drawVLineArgs = valueArgsModel([
704
- {'name': 'y', 'type': 'number'}
705
- ]);
706
-
707
-
708
- // $function: drawWidth
709
- // $group: Drawing
710
- // $doc: Get the current drawing's width
711
- // $return: The current drawing's width
712
- function drawWidth(unusedArgs, options) {
713
- return options.runtime.drawingWidth;
714
- }
715
-
716
-
717
175
  //
718
176
  // Element Model functions
719
177
  //
720
178
 
721
179
 
722
180
  // $function: elementModelRender
723
- // $group: Element Model
181
+ // $group: elementModel
724
182
  // $doc: Render an [element model](https://github.com/craigahobbs/element-model#readme)
725
183
  // $doc:
726
184
  // $doc: **Note:** Element model "callback" members are a map of event name (e.g., "click") to
@@ -737,7 +195,7 @@ function elementModelRender([elements = null], options) {
737
195
  }
738
196
 
739
197
 
740
- function elementModelWrapCallbacks(elements, options) {
198
+ export function elementModelWrapCallbacks(elements, options) {
741
199
  // Ignore non-objects
742
200
  if (elements === null || typeof elements !== 'object') {
743
201
  return;
@@ -758,34 +216,39 @@ function elementModelWrapCallbacks(elements, options) {
758
216
  }
759
217
 
760
218
  // Element callback attribute must be map of event => callback
761
- if ('callback' in elements) {
219
+ const elementEvents = elements.callback ?? null;
220
+ if (elementEvents !== null && typeof elementEvents === 'object') {
762
221
  // Wrap the event handler function
763
- const elementEvents = elements.callback;
764
- if (elementEvents !== null) {
765
- elements.callback = (element) => {
766
- // On element render, add a listener for each event
767
- for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
768
- element.addEventListener(elementEvent, async (event) => {
769
- // Determine the event callback args
770
- const eventArgs = [];
771
- if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
772
- eventArgs.push(event.keyCode);
773
- }
222
+ elements.callback = (element) => {
223
+ // On element render, add a listener for each event
224
+ for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
225
+ element.addEventListener(elementEvent, async (event) => {
226
+ // Determine the event callback args
227
+ const eventArgs = [];
228
+ if (elementEvent === 'click' && event.target?.namespaceURI === 'http://www.w3.org/2000/svg') {
229
+ const svg = event.target.ownerSVGElement || event.target;
230
+ const boundingRect = svg.getBoundingClientRect();
231
+ eventArgs.push(event.clientX - boundingRect.left);
232
+ eventArgs.push(event.clientY - boundingRect.top);
233
+ eventArgs.push(boundingRect.width);
234
+ eventArgs.push(boundingRect.height);
235
+ } else if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
236
+ eventArgs.push(event.keyCode);
237
+ }
774
238
 
775
- // Call the event handler
776
- options.statementCount = 0;
777
- try {
778
- await elementEventCallback(eventArgs, options);
779
- } catch ({message}) {
780
- if ('logFn' in options && options.debug) {
781
- options.logFn(`MarkdownUp: Error executing elementModelRender callback: ${message}`);
782
- }
239
+ // Call the event handler
240
+ options.statementCount = 0;
241
+ try {
242
+ await elementEventCallback(eventArgs, options);
243
+ } catch ({message}) {
244
+ if ('logFn' in options && options.debug) {
245
+ options.logFn(`MarkdownUp: Error executing elementModelRender callback: ${message}`);
783
246
  }
784
- options.runtimeUpdateFn();
785
- });
786
- }
787
- };
788
- }
247
+ }
248
+ options.runtimeUpdateFn();
249
+ });
250
+ }
251
+ };
789
252
  }
790
253
  }
791
254
 
@@ -796,7 +259,7 @@ function elementModelWrapCallbacks(elements, options) {
796
259
 
797
260
 
798
261
  // $function: localStorageClear
799
- // $group: Local Storage
262
+ // $group: localStorage
800
263
  // $doc: Clear all keys from the browser's local storage
801
264
  function localStorageClear(unusedArgs, options) {
802
265
  options.window.localStorage.clear();
@@ -804,7 +267,7 @@ function localStorageClear(unusedArgs, options) {
804
267
 
805
268
 
806
269
  // $function: localStorageGet
807
- // $group: Local Storage
270
+ // $group: localStorage
808
271
  // $doc: Get a browser local storage key's value
809
272
  // $arg key: The key string
810
273
  // $return: The local storage value string or null if the key does not exist
@@ -819,7 +282,7 @@ const localStorageGetArgs = valueArgsModel([
819
282
 
820
283
 
821
284
  // $function: localStorageRemove
822
- // $group: Local Storage
285
+ // $group: localStorage
823
286
  // $doc: Remove a browser local storage key
824
287
  // $arg key: The key string
825
288
  function localStorageRemove(args, options) {
@@ -833,7 +296,7 @@ const localStorageRemoveArgs = valueArgsModel([
833
296
 
834
297
 
835
298
  // $function: localStorageSet
836
- // $group: Local Storage
299
+ // $group: localStorage
837
300
  // $doc: Set a browser local storage key's value
838
301
  // $arg key: The key string
839
302
  // $arg value: The value string
@@ -853,75 +316,8 @@ const localStorageSetArgs = valueArgsModel([
853
316
  //
854
317
 
855
318
 
856
- // $function: markdownElements
857
- // $group: Markdown
858
- // $doc: Generate an element model from a Markdown model
859
- // $arg markdownModel: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
860
- // $arg generic: Optional (default is false). If true, render markdown elements in a generic context.
861
- // $return: The rendered Markdown [element model](https://github.com/craigahobbs/element-model#readme)
862
- function markdownElementsFn(args, options) {
863
- const [markdownModel, generic] = valueArgsValidate(markdownElementsArgs, args);
864
-
865
- // Create the markdown-model options
866
- const markdownOptions = {...options.markdownOptions};
867
- delete markdownOptions.codeBlocks;
868
- if (generic) {
869
- delete markdownOptions.copyFn;
870
- delete markdownOptions.urlFn;
871
- delete markdownOptions.usedHeaderIds;
872
- }
873
-
874
- return markdownElements(markdownModel, markdownOptions);
875
- }
876
-
877
- const markdownElementsArgs = valueArgsModel([
878
- {'name': 'markdownModel', 'type': 'object'},
879
- {'name': 'generic', 'type': 'boolean', 'default': false}
880
- ]);
881
-
882
-
883
- // $function: markdownEscape
884
- // $group: Markdown
885
- // $doc: Escape text for inclusion in Markdown text
886
- // $arg text: The text
887
- // $return: The escaped text
888
- function markdownEscape(args) {
889
- const [text] = valueArgsValidate(markdownEscapeArgs, args);
890
- return escapeMarkdownText(text);
891
- }
892
-
893
- const markdownEscapeArgs = valueArgsModel([
894
- {'name': 'text', 'type': 'string'}
895
- ]);
896
-
897
-
898
- // $function: markdownHeaderId
899
- // $group: Markdown
900
- // $doc: Compute the Markdown header element ID for some text
901
- // $arg text: The text
902
- // $return: The header element ID
903
- function markdownHeaderId(args) {
904
- const [text] = valueArgsValidate(markdownHeaderIdArgs, args);
905
- return markdownModelHeaderId(text);
906
- }
907
-
908
- const markdownHeaderIdArgs = valueArgsModel([
909
- {'name': 'text', 'type': 'string'}
910
- ]);
911
-
912
-
913
- // $function: markdownParse
914
- // $group: Markdown
915
- // $doc: Parse Markdown text
916
- // $arg lines...: The Markdown text lines (may contain nested arrays of un-split lines)
917
- // $return: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
918
- function markdownParse(lines) {
919
- return parseMarkdown(lines.flat());
920
- }
921
-
922
-
923
319
  // $function: markdownPrint
924
- // $group: Markdown
320
+ // $group: markdown
925
321
  // $doc: Render Markdown text
926
322
  // $arg lines...: The Markdown text lines (may contain nested arrays of un-split lines)
927
323
  function markdownPrint(lines, options) {
@@ -931,28 +327,13 @@ function markdownPrint(lines, options) {
931
327
  }
932
328
 
933
329
 
934
- // $function: markdownTitle
935
- // $group: Markdown
936
- // $doc: Compute the title of a [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
937
- // $arg markdownModel: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
938
- // $return: The Markdown title or null if there is no title
939
- function markdownTitle(args) {
940
- const [markdownModel] = valueArgsValidate(markdownTitleArgs, args);
941
- return getMarkdownTitle(markdownModel);
942
- }
943
-
944
- const markdownTitleArgs = valueArgsModel([
945
- {'name': 'markdownModel', 'type': 'object'},
946
- ]);
947
-
948
-
949
330
  //
950
331
  // Session storage functions
951
332
  //
952
333
 
953
334
 
954
335
  // $function: sessionStorageClear
955
- // $group: Session Storage
336
+ // $group: sessionStorage
956
337
  // $doc: Clear all keys from the browser's session storage
957
338
  function sessionStorageClear(unusedArgs, options) {
958
339
  return options.window.sessionStorage.clear();
@@ -960,7 +341,7 @@ function sessionStorageClear(unusedArgs, options) {
960
341
 
961
342
 
962
343
  // $function: sessionStorageGet
963
- // $group: Session Storage
344
+ // $group: sessionStorage
964
345
  // $doc: Get a browser session storage key's value
965
346
  // $arg key: The key string
966
347
  // $return: The session storage value string or null if the key does not exist
@@ -975,7 +356,7 @@ const sessionStorageGetArgs = valueArgsModel([
975
356
 
976
357
 
977
358
  // $function: sessionStorageRemove
978
- // $group: Session Storage
359
+ // $group: sessionStorage
979
360
  // $doc: Remove a browser session storage key
980
361
  // $arg key: The key string
981
362
  function sessionStorageRemove(args, options) {
@@ -989,7 +370,7 @@ const sessionStorageRemoveArgs = valueArgsModel([
989
370
 
990
371
 
991
372
  // $function: sessionStorageSet
992
- // $group: Session Storage
373
+ // $group: sessionStorage
993
374
  // $doc: Set a browser session storage key's value
994
375
  // $arg key: The key string
995
376
  // $arg value: The value string
@@ -1004,35 +385,13 @@ const sessionStorageSetArgs = valueArgsModel([
1004
385
  ]);
1005
386
 
1006
387
 
1007
- //
1008
- // URL functions
1009
- //
1010
-
1011
-
1012
- // $function: urlObjectCreate
1013
- // $group: URL
1014
- // $doc: Create an object URL (i.e. a file download URL)
1015
- // $arg data: The object data string
1016
- // $arg contentType: Optional (default is "text/plain"). The object content type.
1017
- // $return: The object URL string
1018
- function urlObjectCreate(args, options) {
1019
- const [data, contentType] = valueArgsValidate(urlObjectCreateArgs, args);
1020
- return options.window.URL.createObjectURL(new Blob([data], {'type': contentType}));
1021
- }
1022
-
1023
- const urlObjectCreateArgs = valueArgsModel([
1024
- {'name': 'data', 'type': 'string'},
1025
- {'name': 'contentType', 'type': 'string', 'default': 'text/plain'}
1026
- ]);
1027
-
1028
-
1029
388
  //
1030
389
  // Window functions
1031
390
  //
1032
391
 
1033
392
 
1034
393
  // $function: windowClipboardRead
1035
- // $group: Window
394
+ // $group: window
1036
395
  // $doc: Read text from the clipboard
1037
396
  // $return: The clipboard text
1038
397
  async function windowClipboardRead(args, options) {
@@ -1041,21 +400,24 @@ async function windowClipboardRead(args, options) {
1041
400
 
1042
401
 
1043
402
  // $function: windowClipboardWrite
1044
- // $group: Window
403
+ // $group: window
1045
404
  // $doc: Write text to the clipboard
1046
405
  // $arg text: The text to write
406
+ // $arg type: The clipboard content type (default is "text/plain")
1047
407
  async function windowClipboardWrite(args, options) {
1048
- const [text] = valueArgsValidate(windowClipboardWriteArgs, args);
1049
- await options.window.navigator.clipboard.writeText(text);
408
+ const [text, type] = valueArgsValidate(windowClipboardWriteArgs, args);
409
+ const blob = new Blob([text], {type});
410
+ await options.window.navigator.clipboard.write([new options.window.ClipboardItem({[type]: blob})]);
1050
411
  }
1051
412
 
1052
413
  const windowClipboardWriteArgs = valueArgsModel([
1053
- {'name': 'text', 'type': 'string'}
414
+ {'name': 'text', 'type': 'string'},
415
+ {'name': 'type', 'type': 'string', 'default': 'text/plain'}
1054
416
  ]);
1055
417
 
1056
418
 
1057
419
  // $function: windowHeight
1058
- // $group: Window
420
+ // $group: window
1059
421
  // $doc: Get the browser window's height
1060
422
  // $return: The browser window's height
1061
423
  function windowHeight(unusedArgs, options) {
@@ -1064,7 +426,7 @@ function windowHeight(unusedArgs, options) {
1064
426
 
1065
427
 
1066
428
  // $function: windowSetLocation
1067
- // $group: Window
429
+ // $group: window
1068
430
  // $doc: Navigate the browser window to a location URL
1069
431
  // $arg url: The new location URL
1070
432
  function windowSetLocation(args, options) {
@@ -1079,7 +441,7 @@ const windowSetLocationArgs = valueArgsModel([
1079
441
 
1080
442
 
1081
443
  // $function: windowSetResize
1082
- // $group: Window
444
+ // $group: window
1083
445
  // $doc: Set the browser window resize event handler
1084
446
  // $arg callback: The window resize callback function
1085
447
  function windowSetResize(args, options) {
@@ -1106,7 +468,7 @@ const windowSetResizeArgs = valueArgsModel([
1106
468
 
1107
469
 
1108
470
  // $function: windowSetTimeout
1109
- // $group: Window
471
+ // $group: window
1110
472
  // $doc: Set the browser window timeout event handler
1111
473
  // $arg callback: The window timeout callback function
1112
474
  // $arg delay: The delay, in milliseconds, to ellapse before calling the timeout
@@ -1137,8 +499,25 @@ const windowSetTimeoutArgs = valueArgsModel([
1137
499
  ]);
1138
500
 
1139
501
 
502
+ // $function: windowURLObject
503
+ // $group: window
504
+ // $doc: Create an object URL (i.e. a file download URL)
505
+ // $arg data: The object data string
506
+ // $arg contentType: Optional (default is "text/plain"). The object content type.
507
+ // $return: The object URL string
508
+ function windowURLObject(args, options) {
509
+ const [data, contentType] = valueArgsValidate(windowURLObjectArgs, args);
510
+ return options.window.URL.createObjectURL(new Blob([data], {'type': contentType}));
511
+ }
512
+
513
+ const windowURLObjectArgs = valueArgsModel([
514
+ {'name': 'data', 'type': 'string'},
515
+ {'name': 'contentType', 'type': 'string', 'default': 'text/plain'}
516
+ ]);
517
+
518
+
1140
519
  // $function: windowWidth
1141
- // $group: Window
520
+ // $group: window
1142
521
  // $doc: Get the browser window's width
1143
522
  // $return: The browser window's width
1144
523
  function windowWidth(unusedArgs, options) {
@@ -1148,8 +527,6 @@ function windowWidth(unusedArgs, options) {
1148
527
 
1149
528
  // markdown-script library functions
1150
529
  export const markdownScriptFunctions = {
1151
- dataLineChart,
1152
- dataTable,
1153
530
  documentFontSize,
1154
531
  documentInputValue,
1155
532
  documentSetFocus,
@@ -1157,47 +534,22 @@ export const markdownScriptFunctions = {
1157
534
  documentSetReset,
1158
535
  documentSetTitle,
1159
536
  documentURL,
1160
- drawArc,
1161
- drawCircle,
1162
- drawClose,
1163
- drawEllipse,
1164
- drawHLine,
1165
- drawHeight,
1166
- drawImage,
1167
- drawLine,
1168
- drawMove,
1169
- drawNew,
1170
- drawOnClick,
1171
- drawPathRect,
1172
- drawRect,
1173
- drawStyle,
1174
- drawText,
1175
- drawTextHeight,
1176
- drawTextStyle,
1177
- drawTextWidth,
1178
- drawVLine,
1179
- drawWidth,
1180
537
  elementModelRender,
1181
538
  localStorageClear,
1182
539
  localStorageGet,
1183
540
  localStorageRemove,
1184
541
  localStorageSet,
1185
- 'markdownElements': markdownElementsFn,
1186
- markdownEscape,
1187
- markdownHeaderId,
1188
- markdownParse,
1189
542
  markdownPrint,
1190
- markdownTitle,
1191
543
  sessionStorageClear,
1192
544
  sessionStorageGet,
1193
545
  sessionStorageRemove,
1194
546
  sessionStorageSet,
1195
- urlObjectCreate,
1196
547
  windowClipboardRead,
1197
548
  windowClipboardWrite,
1198
549
  windowHeight,
1199
550
  windowSetLocation,
1200
551
  windowSetResize,
1201
552
  windowSetTimeout,
553
+ windowURLObject,
1202
554
  windowWidth
1203
555
  };