markdown-up 2.4.1 → 3.0.1

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.
@@ -4,8 +4,9 @@
4
4
  import {dataTableElements, validateDataTable} from './dataTable.js';
5
5
  import {escapeMarkdownText, getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
6
6
  import {lineChartElements, validateLineChart} from './lineChart.js';
7
+ import {valueArgsModel, valueArgsValidate} from 'bare-script/lib/value.js';
7
8
  import {encodeQueryString} from 'schema-markdown/lib/encode.js';
8
- import {markdownHeaderId} from 'markdown-model/lib/elements.js';
9
+ import {markdownHeaderId as markdownModelHeaderId} from 'markdown-model/lib/elements.js';
9
10
  import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
10
11
  import {validateElements} from 'element-model/lib/elementModel.js';
11
12
 
@@ -20,647 +21,651 @@ const pixelsPerPoint = 4 / 3;
20
21
  const svgPrecision = 8;
21
22
 
22
23
 
23
- // markdown-script library functions
24
- export const markdownScriptFunctions = {
25
- //
26
- // Data functions
27
- //
28
-
29
- // $function: dataLineChart
30
- // $group: Data
31
- // $doc: Draw a line chart
32
- // $arg data: The data array
33
- // $arg lineChart: The [line chart model](model.html#var.vName='LineChart')
34
- 'dataLineChart': ([data, lineChart], options) => {
35
- const {runtime} = options;
36
-
37
- // Render the line chart
38
- const elements = lineChartElements(data, validateLineChart(lineChart), options);
39
- runtime.setElements();
40
- runtime.addElements({'html': 'p', 'elem': elements});
41
-
42
- // Set the drawing size (the chart is now the active drawing)
43
- runtime.drawingWidth = elements.attr.width;
44
- runtime.drawingHeight = elements.attr.height;
45
- },
46
-
47
- // $function: dataTable
48
- // $group: Data
49
- // $doc: Draw a data table
50
- // $arg data: The data array
51
- // $arg dataTable: Optional (default is null). The [data table model](model.html#var.vName='DataTable').
52
- 'dataTable': ([data, dataTable = null], options) => {
53
- const {runtime} = options;
54
- runtime.setElements();
55
- const dataTableValidated = (dataTable !== null ? validateDataTable(dataTable, options) : null);
56
- runtime.addElements(dataTableElements(data, dataTableValidated, options));
57
- },
58
-
59
-
60
- //
61
- // Document functions
62
- //
63
-
64
- // $function: documentFontSize
65
- // $group: Document
66
- // $doc: Get the document font size
67
- // $return: The document font size, in pixels
68
- 'documentFontSize': (unused, options) => options.fontSize * pixelsPerPoint,
69
-
70
- // $function: documentInputValue
71
- // $group: Document
72
- // $doc: Get an input element's value
73
- // $arg id: The input element ID
74
- // $return: The input element value or null if the element does not exist
75
- 'documentInputValue': ([id], options) => {
76
- const element = options.window.document.getElementById(id) ?? null;
77
- return (element !== null ? (element.value ?? null) : null);
78
- },
79
-
80
- // $function: documentSetFocus
81
- // $group: Document
82
- // $doc: Set focus to an element
83
- // $arg id: The element ID
84
- 'documentSetFocus': ([id], options) => {
85
- const {runtime} = options;
86
- runtime.documentFocus = id;
87
- },
88
-
89
- // $function: documentSetReset
90
- // $group: Document
91
- // $doc: Set the document reset element
92
- // $arg id: The element ID
93
- 'documentSetReset': ([id], options) => {
94
- const {runtime} = options;
95
- runtime.documentReset = id;
96
- },
97
-
98
- // $function: documentSetTitle
99
- // $group: Document
100
- // $doc: Set the document title
101
- // $arg title: The document title string
102
- 'documentSetTitle': ([title], options) => {
103
- const {runtime} = options;
104
- runtime.documentTitle = title;
105
- },
106
-
107
- // $function: documentURL
108
- // $group: Document
109
- // $doc: Fix-up relative URLs
110
- // $arg url: The URL
111
- // $return: The fixed-up URL
112
- 'documentURL': ([url], options) => options.urlFn(url),
113
-
114
-
115
- //
116
- // Drawing functions
117
- //
118
-
119
- // $function: drawArc
120
- // $group: Drawing
121
- // $doc: Draw an arc curve from the current point to the end point
122
- // $arg rx: The arc ellipse's x-radius
123
- // $arg ry: The arc ellipse's y-radius
124
- // $arg angle: The rotation (in degrees) of the ellipse relative to the x-axis
125
- // $arg largeArcFlag: Either large arc (1) or small arc (0)
126
- // $arg sweepFlag: Either clockwise turning arc (1) or counterclockwise turning arc (0)
127
- // $arg x: The x-coordinate of the end point
128
- // $arg y: The y-coordinate of the end point
129
- 'drawArc': ([rx, ry, angle, largeArcFlag, sweepFlag, x, y], options) => {
130
- const {runtime} = options;
131
- runtime.setDrawingPath();
132
- runtime.drawingPath.push(
133
- `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
134
- `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
135
- );
136
- },
137
-
138
- // $function: drawCircle
139
- // $group: Drawing
140
- // $doc: Draw a circle
141
- // $arg cx: The x-coordinate of the center of the circle
142
- // $arg cy: The y-coordinate of the center of the circle
143
- // $arg r: The radius of the circle
144
- 'drawCircle': ([cx, cy, r], options) => {
145
- const {runtime} = options;
146
- const svg = runtime.setDrawing();
147
- svg.elem.push({
148
- 'svg': 'circle',
149
- 'attr': {
150
- 'fill': runtime.drawingPathFill,
151
- 'stroke': runtime.drawingPathStroke,
152
- 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
153
- 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
154
- 'cx': cx,
155
- 'cy': cy,
156
- 'r': r
157
- }
158
- });
159
- },
160
-
161
- // $function: drawClose
162
- // $group: Drawing
163
- // $doc: Close the current drawing path
164
- 'drawClose': (unused, options) => {
165
- const {runtime} = options;
166
- runtime.setDrawingPath();
167
- runtime.drawingPath.push('Z');
168
- },
169
-
170
- // $function: drawEllipse
171
- // $group: Drawing
172
- // $doc: Draw an ellipse
173
- // $arg cx: The x-coordinate of the center of the ellipse
174
- // $arg cy: The y-coordinate of the center of the ellipse
175
- // $arg rx: The x-radius of the ellipse
176
- // $arg ry: The y-radius of the ellipse
177
- 'drawEllipse': ([cx, cy, rx, ry], options) => {
178
- const {runtime} = options;
179
- const svg = runtime.setDrawing();
180
- svg.elem.push({
181
- 'svg': 'ellipse',
182
- 'attr': {
183
- 'fill': runtime.drawingPathFill,
184
- 'stroke': runtime.drawingPathStroke,
185
- 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
186
- 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
187
- 'cx': cx,
188
- 'cy': cy,
189
- 'rx': rx,
190
- 'ry': ry
191
- }
192
- });
193
- },
194
-
195
- // $function: drawHLine
196
- // $group: Drawing
197
- // $doc: Draw a horizontal line from the current point to the end point
198
- // $arg x: The x-coordinate of the end point
199
- 'drawHLine': ([x], options) => {
200
- const {runtime} = options;
201
- runtime.setDrawingPath();
202
- runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
203
- },
204
-
205
- // $function: drawHeight
206
- // $group: Drawing
207
- // $doc: Get the current drawing's height
208
- // $return: The current drawing's height
209
- 'drawHeight': (unused, options) => options.runtime.drawingHeight,
210
-
211
- // $function: drawImage
212
- // $group: Drawing
213
- // $doc: Draw an image
214
- // $arg x: The x-coordinate of the center of the image
215
- // $arg y: The y-coordinate of the center of the image
216
- // $arg width: The width of the image
217
- // $arg height: The height of the image
218
- // $arg href: The image resource URL
219
- 'drawImage': ([x, y, width, height, href], options) => {
220
- const {runtime} = options;
221
- const svg = runtime.setDrawing();
222
- svg.elem.push({
223
- 'svg': 'image',
224
- 'attr': {
225
- 'x': x,
226
- 'y': y,
227
- 'width': width,
228
- 'height': height,
229
- 'href': options.urlFn(href)
230
- }
231
- });
232
- },
233
-
234
- // $function: drawLine
235
- // $group: Drawing
236
- // $doc: Draw a line from the current point to the end point
237
- // $arg x: The x-coordinate of the end point
238
- // $arg y: The y-coordinate of the end point
239
- 'drawLine': ([x, y], options) => {
240
- const {runtime} = options;
241
- runtime.setDrawingPath();
242
- runtime.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
243
- },
244
-
245
- // $function: drawMove
246
- // $group: Drawing
247
- // $doc: Move the path's drawing point
248
- // $arg x: The x-coordinate of the new drawing point
249
- // $arg y: The y-coordinate of the new drawing point
250
- 'drawMove': ([x, y], options) => {
251
- const {runtime} = options;
252
- runtime.setDrawingPath();
253
- runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
254
- },
255
-
256
- // $function: drawNew
257
- // $group: Drawing
258
- // $doc: Create a new drawing
259
- // $arg width: The width of the drawing
260
- // $arg height: The height of the drawing
261
- 'drawNew': ([width, height], options) => {
262
- const {runtime} = options;
263
- runtime.drawingWidth = width;
264
- runtime.drawingHeight = height;
265
- runtime.setDrawing(true);
266
- },
267
-
268
- // $function: drawOnClick
269
- // $group: Drawing
270
- // $doc: Set the most recent drawing object's on-click event handler
271
- // $arg callback: The on-click event callback function (x, y)
272
- 'drawOnClick': ([callback], options) => {
273
- const {runtime} = options;
274
- const svg = runtime.setDrawing();
275
- const clickElement = (svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1]);
276
- clickElement.attr.style = 'cursor: pointer;';
277
- clickElement.callback = (element) => {
278
- element.addEventListener('click', async (event) => {
279
- const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
280
- options.statementCount = 0;
281
- try {
282
- await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
283
- } catch ({message}) {
284
- if ('logFn' in options && options.debug) {
285
- options.logFn(`MarkdownUp: Error executing drawOnClick callback: ${message}`);
286
- }
287
- }
288
- options.runtimeUpdateFn();
289
- });
290
- };
291
- },
292
-
293
- // $function: drawPathRect
294
- // $group: Drawing
295
- // $doc: Draw a rectangle as a path
296
- // $arg x: The x-coordinate of the top-left of the rectangle
297
- // $arg y: The y-coordinate of the top-left of the rectangle
298
- // $arg width: The width of the rectangle
299
- // $arg height: The height of the rectangle
300
- 'drawPathRect': ([x, y, width, height], options) => {
301
- const {runtime} = options;
302
- runtime.setDrawingPath();
303
- runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
304
- runtime.drawingPath.push(`H ${(x + width).toFixed(svgPrecision)}`);
305
- runtime.drawingPath.push(`V ${(y + height).toFixed(svgPrecision)}`);
306
- runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
307
- runtime.drawingPath.push('Z');
308
- },
309
-
310
- // $function: drawRect
311
- // $group: Drawing
312
- // $doc: Draw a rectangle
313
- // $arg x: The x-coordinate of the top-left of the rectangle
314
- // $arg y: The y-coordinate of the top-left of the rectangle
315
- // $arg width: The width of the rectangle
316
- // $arg height: The height of the rectangle
317
- // $arg rx: Optional (default is null). The horizontal corner radius of the rectangle.
318
- // $arg ry: Optional (default is null). The vertical corner radius of the rectangle.
319
- 'drawRect': ([x, y, width, height, rx = null, ry = null], options) => {
320
- const {runtime} = options;
321
- const svg = runtime.setDrawing();
322
- const element = {
323
- 'svg': 'rect',
324
- 'attr': {
325
- 'fill': runtime.drawingPathFill,
326
- 'stroke': runtime.drawingPathStroke,
327
- 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
328
- 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
329
- 'x': x,
330
- 'y': y,
331
- 'width': width,
332
- 'height': height
333
- }
334
- };
335
- if (rx !== null) {
336
- element.attr.rx = rx;
337
- }
338
- if (ry !== null) {
339
- element.attr.ry = ry;
24
+ //
25
+ // Data functions
26
+ //
27
+
28
+
29
+ // $function: dataLineChart
30
+ // $group: Data
31
+ // $doc: Draw a line chart
32
+ // $arg data: The data array
33
+ // $arg lineChart: The [line chart model](model.html#var.vName='LineChart')
34
+ function dataLineChart(args, options) {
35
+ const [data, lineChart] = valueArgsValidate(dataLineChartArgs, args);
36
+ const {runtime} = options;
37
+
38
+ // Render the line chart
39
+ const elements = lineChartElements(data, validateLineChart(lineChart), options);
40
+ runtime.setElements();
41
+ runtime.addElements({'html': 'p', 'elem': elements});
42
+
43
+ // Set the drawing size (the chart is now the active drawing)
44
+ runtime.drawingWidth = elements.attr.width;
45
+ runtime.drawingHeight = elements.attr.height;
46
+ }
47
+
48
+ const dataLineChartArgs = valueArgsModel([
49
+ {'name': 'data', 'type': 'array'},
50
+ {'name': 'lineChart', 'type': 'object'}
51
+ ]);
52
+
53
+
54
+ // $function: dataTable
55
+ // $group: Data
56
+ // $doc: Draw a data table
57
+ // $arg data: The data array
58
+ // $arg dataTable: Optional (default is null). The [data table model](model.html#var.vName='DataTable').
59
+ function dataTable(args, options) {
60
+ const [data, dataTableModel] = valueArgsValidate(dataTableArgs, args);
61
+ const {runtime} = options;
62
+ runtime.setElements();
63
+ const dataTableValidated = (dataTableModel !== null ? validateDataTable(dataTableModel, options) : null);
64
+ runtime.addElements(dataTableElements(data, dataTableValidated, options));
65
+ }
66
+
67
+ const dataTableArgs = valueArgsModel([
68
+ {'name': 'data', 'type': 'array'},
69
+ {'name': 'dataTable', 'type': 'object', 'nullable': true}
70
+ ]);
71
+
72
+
73
+ //
74
+ // Document functions
75
+ //
76
+
77
+
78
+ // $function: documentFontSize
79
+ // $group: Document
80
+ // $doc: Get the document font size
81
+ // $return: The document font size, in pixels
82
+ function documentFontSize(unusedArgs, options) {
83
+ return options.fontSize * pixelsPerPoint;
84
+ }
85
+
86
+
87
+ // $function: documentInputValue
88
+ // $group: Document
89
+ // $doc: Get an input element's value
90
+ // $arg id: The input element ID
91
+ // $return: The input element value or null if the element does not exist
92
+ function documentInputValue(args, options) {
93
+ const [id] = valueArgsValidate(documentInputValueArgs, args);
94
+ const element = options.window.document.getElementById(id) ?? null;
95
+ return (element !== null ? (element.value ?? null) : null);
96
+ }
97
+
98
+ const documentInputValueArgs = valueArgsModel([
99
+ {'name': 'id', 'type': 'string'}
100
+ ]);
101
+
102
+
103
+ // $function: documentSetFocus
104
+ // $group: Document
105
+ // $doc: Set focus to an element
106
+ // $arg id: The element ID
107
+ function documentSetFocus(args, options) {
108
+ const [id] = valueArgsValidate(documentSetFocusArgs, args);
109
+ const {runtime} = options;
110
+ runtime.documentFocus = id;
111
+ }
112
+
113
+ const documentSetFocusArgs = valueArgsModel([
114
+ {'name': 'id', 'type': 'string'}
115
+ ]);
116
+
117
+
118
+ // $function: documentSetReset
119
+ // $group: Document
120
+ // $doc: Set the document reset element
121
+ // $arg id: The element ID
122
+ function documentSetReset(args, options) {
123
+ const [id] = valueArgsValidate(documentSetResetArgs, args);
124
+ const {runtime} = options;
125
+ runtime.documentReset = id;
126
+ }
127
+
128
+ const documentSetResetArgs = valueArgsModel([
129
+ {'name': 'id', 'type': 'string'}
130
+ ]);
131
+
132
+
133
+ // $function: documentSetTitle
134
+ // $group: Document
135
+ // $doc: Set the document title
136
+ // $arg title: The document title string
137
+ function documentSetTitle(args, options) {
138
+ const [title] = valueArgsValidate(documentSetTitleArgs, args);
139
+ const {runtime} = options;
140
+ runtime.documentTitle = title;
141
+ }
142
+
143
+ const documentSetTitleArgs = valueArgsModel([
144
+ {'name': 'title', 'type': 'string'}
145
+ ]);
146
+
147
+
148
+ // $function: documentURL
149
+ // $group: Document
150
+ // $doc: Fix-up relative URLs
151
+ // $arg url: The URL
152
+ // $return: The fixed-up URL
153
+ function documentURL(args, options) {
154
+ const [url] = valueArgsValidate(documentURLArgs, args);
155
+ return options.urlFn(url);
156
+ }
157
+
158
+ const documentURLArgs = valueArgsModel([
159
+ {'name': 'url', 'type': 'string'}
160
+ ]);
161
+
162
+
163
+ //
164
+ // Drawing functions
165
+ //
166
+
167
+
168
+ // $function: drawArc
169
+ // $group: Drawing
170
+ // $doc: Draw an arc curve from the current point to the end point
171
+ // $arg rx: The arc ellipse's x-radius
172
+ // $arg ry: The arc ellipse's y-radius
173
+ // $arg angle: The rotation (in degrees) of the ellipse relative to the x-axis
174
+ // $arg largeArcFlag: Either large arc (1) or small arc (0)
175
+ // $arg sweepFlag: Either clockwise turning arc (1) or counterclockwise turning arc (0)
176
+ // $arg x: The x-coordinate of the end point
177
+ // $arg y: The y-coordinate of the end point
178
+ function drawArc(args, options) {
179
+ const [rx, ry, angle, largeArcFlag, sweepFlag, x, y] = valueArgsValidate(drawArcArgs, args);
180
+ const {runtime} = options;
181
+ runtime.setDrawingPath();
182
+ runtime.drawingPath.push(
183
+ `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
184
+ `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
185
+ );
186
+ }
187
+
188
+ const drawArcArgs = valueArgsModel([
189
+ {'name': 'rx', 'type': 'number'},
190
+ {'name': 'ry', 'type': 'number'},
191
+ {'name': 'angle', 'type': 'number'},
192
+ {'name': 'largeArcFlag', 'type': 'boolean'},
193
+ {'name': 'sweepFlag', 'type': 'boolean'},
194
+ {'name': 'x', 'type': 'number'},
195
+ {'name': 'y', 'type': 'number'},
196
+ ]);
197
+
198
+
199
+ // $function: drawCircle
200
+ // $group: Drawing
201
+ // $doc: Draw a circle
202
+ // $arg cx: The x-coordinate of the center of the circle
203
+ // $arg cy: The y-coordinate of the center of the circle
204
+ // $arg r: The radius of the circle
205
+ function drawCircle(args, options) {
206
+ const [cx, cy, r] = valueArgsValidate(drawCircleArgs, args);
207
+ const {runtime} = options;
208
+ const svg = runtime.setDrawing();
209
+ svg.elem.push({
210
+ 'svg': 'circle',
211
+ 'attr': {
212
+ 'fill': runtime.drawingPathFill,
213
+ 'stroke': runtime.drawingPathStroke,
214
+ 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
215
+ 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
216
+ 'cx': cx,
217
+ 'cy': cy,
218
+ 'r': r
340
219
  }
341
- svg.elem.push(element);
342
- },
343
-
344
- // $function: drawStyle
345
- // $group: Drawing
346
- // $doc: Set the current drawing styles
347
- // $arg stroke: Optional (default is 'black'). The stroke color.
348
- // $arg strokeWidth: Optional (default is 1). The stroke width.
349
- // $arg fill: Optional (default is 'none'). The fill color.
350
- // $arg strokeDashArray: Optional (default is 'none'). The stroke
351
- // $arg strokeDashArray: [dash array](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray#usage_notes).
352
- 'drawStyle': ([stroke = 'black', strokeWidth = 1, fill = 'none', strokeDashArray = 'none'], options) => {
353
- const {runtime} = options;
354
- if (stroke !== runtime.drawingPathStroke || strokeWidth !== runtime.drawingPathStrokeWidth ||
355
- strokeDashArray !== runtime.drawingPathStrokeDashArray || fill !== runtime.drawingPathFill
356
- ) {
357
- runtime.setDrawing();
358
- runtime.drawingPathStroke = stroke;
359
- runtime.drawingPathStrokeWidth = strokeWidth;
360
- runtime.drawingPathStrokeDashArray = strokeDashArray;
361
- runtime.drawingPathFill = fill;
220
+ });
221
+ }
222
+
223
+ const drawCircleArgs = valueArgsModel([
224
+ {'name': 'cx', 'type': 'number'},
225
+ {'name': 'cy', 'type': 'number'},
226
+ {'name': 'r', 'type': 'number'}
227
+ ]);
228
+
229
+
230
+ // $function: drawClose
231
+ // $group: Drawing
232
+ // $doc: Close the current drawing path
233
+ function drawClose(unusedArgs, options) {
234
+ const {runtime} = options;
235
+ runtime.setDrawingPath();
236
+ runtime.drawingPath.push('Z');
237
+ }
238
+
239
+
240
+ // $function: drawEllipse
241
+ // $group: Drawing
242
+ // $doc: Draw an ellipse
243
+ // $arg cx: The x-coordinate of the center of the ellipse
244
+ // $arg cy: The y-coordinate of the center of the ellipse
245
+ // $arg rx: The x-radius of the ellipse
246
+ // $arg ry: The y-radius of the ellipse
247
+ function drawEllipse(args, options) {
248
+ const [cx, cy, rx, ry] = valueArgsValidate(drawEllipseArgs, args);
249
+ const {runtime} = options;
250
+ const svg = runtime.setDrawing();
251
+ svg.elem.push({
252
+ 'svg': 'ellipse',
253
+ 'attr': {
254
+ 'fill': runtime.drawingPathFill,
255
+ 'stroke': runtime.drawingPathStroke,
256
+ 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
257
+ 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
258
+ 'cx': cx,
259
+ 'cy': cy,
260
+ 'rx': rx,
261
+ 'ry': ry
362
262
  }
363
- },
364
-
365
- // $function: drawText
366
- // $group: Drawing
367
- // $doc: Draw text
368
- // $arg text: The text to draw
369
- // $arg x: The x-coordinate of the text
370
- // $arg y: The y-coordinate of the text
371
- // $arg textAnchor: Optional (default is 'middle'). The
372
- // $arg textAnchor: [text anchor](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor#usage_notes) style.
373
- // $arg dominantBaseline: Optional (default is 'middle'). The
374
- // $arg dominantBaseline: [dominant baseline](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline#usage_notes)
375
- // $arg dominantBaseline: style.
376
- 'drawText': ([text, x, y, textAnchor = 'middle', dominantBaseline = 'middle'], options) => {
377
- const {runtime} = options;
378
- const svg = runtime.setDrawing();
379
- svg.elem.push({
380
- 'svg': 'text',
381
- 'attr': {
382
- 'fill': runtime.drawingFontFill,
383
- 'font-family': runtime.drawingFontFamily,
384
- 'font-size': runtime.drawingFontSizePx.toFixed(svgPrecision),
385
- 'text-anchor': textAnchor,
386
- 'dominant-baseline': dominantBaseline,
387
- 'font-weight': (runtime.drawingFontBold ? 'bold' : 'normal'),
388
- 'font-style': (runtime.drawingFontItalic ? 'italic' : 'normal'),
389
- 'x': x,
390
- 'y': y
391
- },
392
- 'elem': {'text': text}
393
- });
394
- },
395
-
396
- // $function: drawTextHeight
397
- // $group: Drawing
398
- // $doc: Compute the text's height to fit the width
399
- // $arg text: The text
400
- // $arg width: The width of the text. If 0, the default font size (in pixels) is returned.
401
- // $return: The text's height, in pixels
402
- 'drawTextHeight': ([text, width], options) => (width > 0 ? width / (fontWidthRatio * text.length) : options.runtime.drawingFontSizePx),
403
-
404
- // $function: drawTextStyle
405
- // $group: Drawing
406
- // $doc: Set the current text drawing styles
407
- // $arg fontSizePx: Optional (default is null, the default font size). The text font size, in pixels.
408
- // $arg textFill: Optional (default is 'black'). The text fill color.
409
- // $arg bold: Optional (default is false). If true, text is bold.
410
- // $arg italic: Optional (default is false). If true, text is italic.
411
- // $arg fontFamily: Optional (default is null, the default font family). The text font family.
412
- 'drawTextStyle': ([fontSizePx = null, textFill = 'black', bold = false, italic = false, fontFamily = null], options) => {
413
- const {runtime} = options;
414
- runtime.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : options.fontSize * pixelsPerPoint);
415
- runtime.drawingFontFill = textFill;
416
- runtime.drawingFontBold = bold;
417
- runtime.drawingFontItalic = italic;
418
- runtime.drawingFontFamily = (fontFamily !== null ? fontFamily : defaultFontFamily);
419
- },
420
-
421
- // $function: drawTextWidth
422
- // $group: Drawing
423
- // $doc: Compute the text's width
424
- // $arg text: The text
425
- // $arg fontSizePx: The text font size, in pixels
426
- // $return: The text's width, in pixels
427
- 'drawTextWidth': ([text, fontSizePx]) => fontWidthRatio * fontSizePx * text.length,
428
-
429
- // $function: drawVLine
430
- // $group: Drawing
431
- // $doc: Draw a vertical line from the current point to the end point
432
- // $arg y: The y-coordinate of the end point
433
- 'drawVLine': ([y], options) => {
434
- const {runtime} = options;
435
- runtime.setDrawingPath();
436
- runtime.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
437
- },
438
-
439
- // $function: drawWidth
440
- // $group: Drawing
441
- // $doc: Get the current drawing's width
442
- // $return: The current drawing's width
443
- 'drawWidth': (unused, options) => options.runtime.drawingWidth,
444
-
445
-
446
- //
447
- // Element Model functions
448
- //
449
-
450
- // $function: elementModelRender
451
- // $group: Element Model
452
- // $doc: Render an [element model](https://github.com/craigahobbs/element-model#readme)
453
- // $doc:
454
- // $doc: **Note:** Element model "callback" members are a map of event name (e.g., "click") to
455
- // $doc: event callback function. The following events have callback arguments:
456
- // $doc: - **keydown** - keyCode
457
- // $doc: - **keypress** - keyCode
458
- // $doc: - **keyup** - keyCode
459
- // $arg element: The [element model](https://github.com/craigahobbs/element-model#readme)
460
- 'elementModelRender': ([elements], options) => {
461
- const {runtime} = options;
462
- runtime.setElements();
463
- elementModelWrapCallbacks(elements, options);
464
- runtime.addElements(validateElements(elements));
465
- },
466
-
467
-
468
- //
469
- // Local storage functions
470
- //
471
-
472
- // $function: localStorageClear
473
- // $group: Local Storage
474
- // $doc: Clear all keys from the browser's local storage
475
- 'localStorageClear': (unused, options) => options.window.localStorage.clear(),
476
-
477
- // $function: localStorageGet
478
- // $group: Local Storage
479
- // $doc: Get a browser local storage key's value
480
- // $arg key: The key string
481
- // $return: The local storage value string or null if the key does not exist
482
- 'localStorageGet': ([key], options) => options.window.localStorage.getItem(key),
483
-
484
- // $function: localStorageRemove
485
- // $group: Local Storage
486
- // $doc: Remove a browser local storage key
487
- // $arg key: The key string
488
- 'localStorageRemove': ([key], options) => options.window.localStorage.removeItem(key),
489
-
490
- // $function: localStorageSet
491
- // $group: Local Storage
492
- // $doc: Set a browser local storage key's value
493
- // $arg key: The key string
494
- // $arg value: The value string
495
- 'localStorageSet': ([key, value], options) => options.window.localStorage.setItem(key, value),
496
-
497
-
498
- //
499
- // Markdown functions
500
- //
501
-
502
- // $function: markdownEscape
503
- // $group: Markdown
504
- // $doc: Escape text for inclusion in Markdown text
505
- // $arg text: The text
506
- // $return: The escaped text
507
- 'markdownEscape': ([text]) => escapeMarkdownText(text),
508
-
509
- // $function: markdownHeaderId
510
- // $group: Markdown
511
- // $doc: Compute the Markdown header element ID for some text
512
- // $arg text: The text
513
- // $return: The header element ID
514
- 'markdownHeaderId': ([text]) => markdownHeaderId(text),
515
-
516
- // $function: markdownParse
517
- // $group: Markdown
518
- // $doc: Parse Markdown text
519
- // $arg lines...: The Markdown text lines (may contain nested arrays of un-split lines)
520
- // $return: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
521
- 'markdownParse': (lines) => parseMarkdown(lines.flat()),
522
-
523
- // $function: markdownPrint
524
- // $group: Markdown
525
- // $doc: Render Markdown text
526
- // $arg lines...: The Markdown text lines (may contain nested arrays of un-split lines)
527
- 'markdownPrint': (lines, options) => {
528
- const {runtime} = options;
529
- runtime.setMarkdown();
530
- runtime.addMarkdown(lines);
531
- },
532
-
533
- // $function: markdownTitle
534
- // $group: Markdown
535
- // $doc: Compute the title of a [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
536
- // $arg markdownModel: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
537
- // $return: The Markdown title or null if there is no title
538
- 'markdownTitle': ([markdownModel]) => getMarkdownTitle(markdownModel),
539
-
540
-
541
- //
542
- // Schema functions
543
- //
544
-
545
- // $function: schemaElements
546
- // $group: Schema
547
- // $doc: Get a schema type's documentation [element model](https://github.com/craigahobbs/element-model#readme).
548
- // $doc: Render the element model with the [elementModelRender](#var.vName='elementModelRender') function.
549
- // $arg types: The [type model](https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='Types')
550
- // $arg typeName: The type name
551
- // $arg actionURLs: Optional (default is null). The
552
- // $arg actionURLs: [action URL overrides](https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='ActionURL').
553
- // $return: The schema type's documentation [element model](https://github.com/craigahobbs/element-model#readme)
554
- 'schemaElements': ([types, typeName, actionURLs = null], options) => {
555
- const params = encodeQueryString(options.params);
556
- const schemaOptions = {params};
557
- if (actionURLs !== null) {
558
- schemaOptions.actionURLs = actionURLs;
263
+ });
264
+ }
265
+
266
+ const drawEllipseArgs = valueArgsModel([
267
+ {'name': 'cx', 'type': 'number'},
268
+ {'name': 'cy', 'type': 'number'},
269
+ {'name': 'rx', 'type': 'number'},
270
+ {'name': 'ry', 'type': 'number'}
271
+ ]);
272
+
273
+
274
+ // $function: drawHLine
275
+ // $group: Drawing
276
+ // $doc: Draw a horizontal line from the current point to the end point
277
+ // $arg x: The x-coordinate of the end point
278
+ function drawHLine(args, options) {
279
+ const [x] = valueArgsValidate(drawHLineArgs, args);
280
+ const {runtime} = options;
281
+ runtime.setDrawingPath();
282
+ runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
283
+ }
284
+
285
+ const drawHLineArgs = valueArgsModel([
286
+ {'name': 'x', 'type': 'number'}
287
+ ]);
288
+
289
+
290
+ // $function: drawHeight
291
+ // $group: Drawing
292
+ // $doc: Get the current drawing's height
293
+ // $return: The current drawing's height
294
+ function drawHeight(unusedArgs, options) {
295
+ return options.runtime.drawingHeight;
296
+ }
297
+
298
+
299
+ // $function: drawImage
300
+ // $group: Drawing
301
+ // $doc: Draw an image
302
+ // $arg x: The x-coordinate of the center of the image
303
+ // $arg y: The y-coordinate of the center of the image
304
+ // $arg width: The width of the image
305
+ // $arg height: The height of the image
306
+ // $arg href: The image resource URL
307
+ function drawImage(args, options) {
308
+ const [x, y, width, height, href] = valueArgsValidate(drawImageArgs, args);
309
+ const {runtime} = options;
310
+ const svg = runtime.setDrawing();
311
+ svg.elem.push({
312
+ 'svg': 'image',
313
+ 'attr': {
314
+ 'x': x,
315
+ 'y': y,
316
+ 'width': width,
317
+ 'height': height,
318
+ 'href': options.urlFn(href)
559
319
  }
560
- return schemaMarkdownDoc(types, typeName, schemaOptions);
561
- },
562
-
563
-
564
- //
565
- // Session storage functions
566
- //
567
-
568
- // $function: sessionStorageClear
569
- // $group: Session Storage
570
- // $doc: Clear all keys from the browser's session storage
571
- 'sessionStorageClear': (unused, options) => options.window.sessionStorage.clear(),
572
-
573
- // $function: sessionStorageGet
574
- // $group: Session Storage
575
- // $doc: Get a browser session storage key's value
576
- // $arg key: The key string
577
- // $return: The session storage value string or null if the key does not exist
578
- 'sessionStorageGet': ([key], options) => options.window.sessionStorage.getItem(key),
579
-
580
- // $function: sessionStorageSet
581
- // $group: Session Storage
582
- // $doc: Set a browser session storage key's value
583
- // $arg key: The key string
584
- // $arg value: The value string
585
- 'sessionStorageSet': ([key, value], options) => options.window.sessionStorage.setItem(key, value),
586
-
587
- // $function: sessionStorageRemove
588
- // $group: Session Storage
589
- // $doc: Remove a browser session storage key
590
- // $arg key: The key string
591
- 'sessionStorageRemove': ([key], options) => options.window.sessionStorage.removeItem(key),
592
-
593
-
594
- //
595
- // Window functions
596
- //
597
-
598
- // $function: windowHeight
599
- // $group: Window
600
- // $doc: Get the browser window's height
601
- // $return: The browser window's height
602
- 'windowHeight': (unused, options) => options.window.innerHeight,
603
-
604
- // $function: windowSetLocation
605
- // $group: Window
606
- // $doc: Navigate the browser window to a location URL
607
- // $arg url: The new location URL
608
- 'windowSetLocation': ([location], options) => {
609
- const {runtime} = options;
610
- runtime.windowLocation = options.urlFn(location);
611
- },
612
-
613
- // $function: windowSetResize
614
- // $group: Window
615
- // $doc: Set the browser window resize event handler
616
- // $arg callback: The window resize callback function
617
- 'windowSetResize': ([callback], options) => {
618
- const {runtime} = options;
619
- runtime.windowResize = async () => {
320
+ });
321
+ }
322
+
323
+ const drawImageArgs = valueArgsModel([
324
+ {'name': 'x', 'type': 'number'},
325
+ {'name': 'y', 'type': 'number'},
326
+ {'name': 'width', 'type': 'number'},
327
+ {'name': 'height', 'type': 'number'},
328
+ {'name': 'href', 'type': 'string'}
329
+ ]);
330
+
331
+
332
+ // $function: drawLine
333
+ // $group: Drawing
334
+ // $doc: Draw a line from the current point to the end point
335
+ // $arg x: The x-coordinate of the end point
336
+ // $arg y: The y-coordinate of the end point
337
+ function drawLine(args, options) {
338
+ const [x, y] = valueArgsValidate(drawLineArgs, args);
339
+ const {runtime} = options;
340
+ runtime.setDrawingPath();
341
+ runtime.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
342
+ }
343
+
344
+ const drawLineArgs = valueArgsModel([
345
+ {'name': 'x', 'type': 'number'},
346
+ {'name': 'y', 'type': 'number'}
347
+ ]);
348
+
349
+
350
+ // $function: drawMove
351
+ // $group: Drawing
352
+ // $doc: Move the path's drawing point
353
+ // $arg x: The x-coordinate of the new drawing point
354
+ // $arg y: The y-coordinate of the new drawing point
355
+ function drawMove(args, options) {
356
+ const [x, y] = valueArgsValidate(drawMoveArgs, args);
357
+ const {runtime} = options;
358
+ runtime.setDrawingPath();
359
+ runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
360
+ }
361
+
362
+ const drawMoveArgs = valueArgsModel([
363
+ {'name': 'x', 'type': 'number'},
364
+ {'name': 'y', 'type': 'number'}
365
+ ]);
366
+
367
+
368
+ // $function: drawNew
369
+ // $group: Drawing
370
+ // $doc: Create a new drawing
371
+ // $arg width: The width of the drawing
372
+ // $arg height: The height of the drawing
373
+ function drawNew(args, options) {
374
+ const [width, height] = valueArgsValidate(drawNewArgs, args);
375
+ const {runtime} = options;
376
+ runtime.drawingWidth = width;
377
+ runtime.drawingHeight = height;
378
+ runtime.setDrawing(true);
379
+ }
380
+
381
+ const drawNewArgs = valueArgsModel([
382
+ {'name': 'width', 'type': 'number'},
383
+ {'name': 'height', 'type': 'number'}
384
+ ]);
385
+
386
+
387
+ // $function: drawOnClick
388
+ // $group: Drawing
389
+ // $doc: Set the most recent drawing object's on-click event handler
390
+ // $arg callback: The on-click event callback function (x, y)
391
+ function drawOnClick(args, options) {
392
+ const [callback] = valueArgsValidate(drawOnClickArgs, args);
393
+ const {runtime} = options;
394
+ const svg = runtime.setDrawing();
395
+ const clickElement = (svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1]);
396
+ clickElement.attr.style = 'cursor: pointer;';
397
+ clickElement.callback = (element) => {
398
+ element.addEventListener('click', async (event) => {
399
+ const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
620
400
  options.statementCount = 0;
621
401
  try {
622
- await callback([], options);
402
+ await callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], options);
623
403
  } catch ({message}) {
624
404
  if ('logFn' in options && options.debug) {
625
- options.logFn(`MarkdownUp: Error executing windowSetResize callback: ${message}`);
405
+ options.logFn(`MarkdownUp: Error executing drawOnClick callback: ${message}`);
626
406
  }
627
407
  }
628
408
  options.runtimeUpdateFn();
629
- };
630
- },
631
-
632
- // $function: windowSetTimeout
633
- // $group: Window
634
- // $doc: Set the browser window timeout event handler
635
- // $arg callback: The window timeout callback function
636
- // $arg delay: The delay, in milliseconds, to ellapse before calling the timeout
637
- 'windowSetTimeout': ([callback, delay], options) => {
638
- const {runtime} = options;
639
- runtime.windowTimeout = [
640
- async () => {
641
- options.statementCount = 0;
642
- try {
643
- await callback([], options);
644
- } catch ({message}) {
645
- if ('logFn' in options && options.debug) {
646
- options.logFn(`MarkdownUp: Error executing windowSetTimeout callback: ${message}`);
647
- }
648
- }
649
- options.runtimeUpdateFn();
650
- },
651
- delay
652
- ];
653
- },
654
-
655
- // $function: windowWidth
656
- // $group: Window
657
- // $doc: Get the browser window's width
658
- // $return: The browser window's width
659
- 'windowWidth': (unused, options) => options.window.innerWidth
660
- };
409
+ });
410
+ };
411
+ }
412
+
413
+ const drawOnClickArgs = valueArgsModel([
414
+ {'name': 'callback', 'type': 'function'}
415
+ ]);
416
+
417
+
418
+ // $function: drawPathRect
419
+ // $group: Drawing
420
+ // $doc: Draw a rectangle as a path
421
+ // $arg x: The x-coordinate of the top-left of the rectangle
422
+ // $arg y: The y-coordinate of the top-left of the rectangle
423
+ // $arg width: The width of the rectangle
424
+ // $arg height: The height of the rectangle
425
+ function drawPathRect(args, options) {
426
+ const [x, y, width, height] = valueArgsValidate(drawPathRectArgs, args);
427
+ const {runtime} = options;
428
+ runtime.setDrawingPath();
429
+ runtime.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
430
+ runtime.drawingPath.push(`H ${(x + width).toFixed(svgPrecision)}`);
431
+ runtime.drawingPath.push(`V ${(y + height).toFixed(svgPrecision)}`);
432
+ runtime.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
433
+ runtime.drawingPath.push('Z');
434
+ }
435
+
436
+ const drawPathRectArgs = valueArgsModel([
437
+ {'name': 'x', 'type': 'number'},
438
+ {'name': 'y', 'type': 'number'},
439
+ {'name': 'width', 'type': 'number'},
440
+ {'name': 'height', 'type': 'number'}
441
+ ]);
442
+
443
+
444
+ // $function: drawRect
445
+ // $group: Drawing
446
+ // $doc: Draw a rectangle
447
+ // $arg x: The x-coordinate of the top-left of the rectangle
448
+ // $arg y: The y-coordinate of the top-left of the rectangle
449
+ // $arg width: The width of the rectangle
450
+ // $arg height: The height of the rectangle
451
+ // $arg rx: Optional (default is null). The horizontal corner radius of the rectangle.
452
+ // $arg ry: Optional (default is null). The vertical corner radius of the rectangle.
453
+ function drawRect(args, options) {
454
+ const [x, y, width, height, rx = null, ry = null] = valueArgsValidate(drawRectArgs, args);
455
+ const {runtime} = options;
456
+ const svg = runtime.setDrawing();
457
+ const element = {
458
+ 'svg': 'rect',
459
+ 'attr': {
460
+ 'fill': runtime.drawingPathFill,
461
+ 'stroke': runtime.drawingPathStroke,
462
+ 'stroke-width': runtime.drawingPathStrokeWidth.toFixed(svgPrecision),
463
+ 'stroke-dasharray': runtime.drawingPathStrokeDashArray,
464
+ 'x': x,
465
+ 'y': y,
466
+ 'width': width,
467
+ 'height': height
468
+ }
469
+ };
470
+ if (rx !== null) {
471
+ element.attr.rx = rx;
472
+ }
473
+ if (ry !== null) {
474
+ element.attr.ry = ry;
475
+ }
476
+ svg.elem.push(element);
477
+ }
478
+
479
+ const drawRectArgs = valueArgsModel([
480
+ {'name': 'x', 'type': 'number'},
481
+ {'name': 'y', 'type': 'number'},
482
+ {'name': 'width', 'type': 'number'},
483
+ {'name': 'height', 'type': 'number'},
484
+ {'name': 'rx', 'type': 'number', 'nullable': true},
485
+ {'name': 'ry', 'type': 'number', 'nullable': true}
486
+ ]);
487
+
488
+
489
+ // $function: drawStyle
490
+ // $group: Drawing
491
+ // $doc: Set the current drawing styles
492
+ // $arg stroke: Optional (default is 'black'). The stroke color.
493
+ // $arg strokeWidth: Optional (default is 1). The stroke width.
494
+ // $arg fill: Optional (default is 'none'). The fill color.
495
+ // $arg strokeDashArray: Optional (default is 'none'). The stroke
496
+ // $arg strokeDashArray: [dash array](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray#usage_notes).
497
+ function drawStyle(args, options) {
498
+ const [stroke, strokeWidth, fill, strokeDashArray] = valueArgsValidate(drawStyleArgs, args);
499
+ const {runtime} = options;
500
+ if (stroke !== runtime.drawingPathStroke || strokeWidth !== runtime.drawingPathStrokeWidth ||
501
+ strokeDashArray !== runtime.drawingPathStrokeDashArray || fill !== runtime.drawingPathFill
502
+ ) {
503
+ runtime.setDrawing();
504
+ runtime.drawingPathStroke = stroke;
505
+ runtime.drawingPathStrokeWidth = strokeWidth;
506
+ runtime.drawingPathStrokeDashArray = strokeDashArray;
507
+ runtime.drawingPathFill = fill;
508
+ }
509
+ }
510
+
511
+ const drawStyleArgs = valueArgsModel([
512
+ {'name': 'stroke', 'type': 'string', 'default': 'black'},
513
+ {'name': 'strokeWidth', 'type': 'number', 'default': 1},
514
+ {'name': 'fill', 'type': 'string', 'default': 'none'},
515
+ {'name': 'strokeDashArray', 'type': 'string', 'default': 'none'}
516
+ ]);
517
+
518
+
519
+ // $function: drawText
520
+ // $group: Drawing
521
+ // $doc: Draw text
522
+ // $arg text: The text to draw
523
+ // $arg x: The x-coordinate of the text
524
+ // $arg y: The y-coordinate of the text
525
+ // $arg textAnchor: Optional (default is 'middle'). The
526
+ // $arg textAnchor: [text anchor](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor#usage_notes) style.
527
+ // $arg dominantBaseline: Optional (default is 'middle'). The
528
+ // $arg dominantBaseline: [dominant baseline](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline#usage_notes)
529
+ // $arg dominantBaseline: style.
530
+ function drawText(args, options) {
531
+ const [text, x, y, textAnchor, dominantBaseline] = valueArgsValidate(drawTextArgs, args);
532
+ const {runtime} = options;
533
+ const svg = runtime.setDrawing();
534
+ svg.elem.push({
535
+ 'svg': 'text',
536
+ 'attr': {
537
+ 'fill': runtime.drawingFontFill,
538
+ 'font-family': runtime.drawingFontFamily,
539
+ 'font-size': runtime.drawingFontSizePx.toFixed(svgPrecision),
540
+ 'text-anchor': textAnchor,
541
+ 'dominant-baseline': dominantBaseline,
542
+ 'font-weight': (runtime.drawingFontBold ? 'bold' : 'normal'),
543
+ 'font-style': (runtime.drawingFontItalic ? 'italic' : 'normal'),
544
+ 'x': x,
545
+ 'y': y
546
+ },
547
+ 'elem': {'text': text}
548
+ });
549
+ }
550
+
551
+ const drawTextArgs = valueArgsModel([
552
+ {'name': 'text', 'type': 'string'},
553
+ {'name': 'x', 'type': 'number'},
554
+ {'name': 'y', 'type': 'number'},
555
+ {'name': 'textAnchor', 'type': 'string', 'default': 'middle'},
556
+ {'name': 'dominantBaseline', 'type': 'string', 'default': 'middle'}
557
+ ]);
558
+
559
+
560
+ // $function: drawTextHeight
561
+ // $group: Drawing
562
+ // $doc: Compute the text's height to fit the width
563
+ // $arg text: The text
564
+ // $arg width: The width of the text. If 0, the default font size (in pixels) is returned.
565
+ // $return: The text's height, in pixels
566
+ function drawTextHeight(args, options) {
567
+ const [text, width] = valueArgsValidate(drawTextHeightArgs, args);
568
+ return width > 0 ? width / (fontWidthRatio * text.length) : options.runtime.drawingFontSizePx;
569
+ }
570
+
571
+ const drawTextHeightArgs = valueArgsModel([
572
+ {'name': 'text', 'type': 'string'},
573
+ {'name': 'width', 'type': 'number'},
574
+ ]);
575
+
576
+
577
+ // $function: drawTextStyle
578
+ // $group: Drawing
579
+ // $doc: Set the current text drawing styles
580
+ // $arg fontSizePx: Optional (default is null, the default font size). The text font size, in pixels.
581
+ // $arg textFill: Optional (default is 'black'). The text fill color.
582
+ // $arg bold: Optional (default is false). If true, text is bold.
583
+ // $arg italic: Optional (default is false). If true, text is italic.
584
+ // $arg fontFamily: Optional (default is null, the default font family). The text font family.
585
+ function drawTextStyle(args, options) {
586
+ const [fontSizePx, textFill, bold, italic, fontFamily] = valueArgsValidate(drawTextStyleArgs, args);
587
+ const {runtime} = options;
588
+ runtime.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : options.fontSize * pixelsPerPoint);
589
+ runtime.drawingFontFill = textFill;
590
+ runtime.drawingFontBold = bold;
591
+ runtime.drawingFontItalic = italic;
592
+ runtime.drawingFontFamily = fontFamily;
593
+ }
594
+
595
+ const drawTextStyleArgs = valueArgsModel([
596
+ {'name': 'fontSizePx', 'type': 'number', 'nullable': true},
597
+ {'name': 'textFill', 'type': 'string', 'default': 'black'},
598
+ {'name': 'bold', 'type': 'boolean', 'default': false},
599
+ {'name': 'italic', 'type': 'boolean', 'default': false},
600
+ {'name': 'fontFamily', 'type': 'string', 'default': defaultFontFamily},
601
+ ]);
602
+
603
+
604
+ // $function: drawTextWidth
605
+ // $group: Drawing
606
+ // $doc: Compute the text's width
607
+ // $arg text: The text
608
+ // $arg fontSizePx: The text font size, in pixels
609
+ // $return: The text's width, in pixels
610
+ function drawTextWidth(args) {
611
+ const [text, fontSizePx] = valueArgsValidate(drawTextWidthArgs, args);
612
+ return fontWidthRatio * fontSizePx * text.length;
613
+ }
614
+
615
+ const drawTextWidthArgs = valueArgsModel([
616
+ {'name': 'text', 'type': 'string'},
617
+ {'name': 'fontSizePx', 'type': 'number'}
618
+ ]);
619
+
620
+
621
+ // $function: drawVLine
622
+ // $group: Drawing
623
+ // $doc: Draw a vertical line from the current point to the end point
624
+ // $arg y: The y-coordinate of the end point
625
+ function drawVLine(args, options) {
626
+ const [y] = valueArgsValidate(drawVLineArgs, args);
627
+ const {runtime} = options;
628
+ runtime.setDrawingPath();
629
+ runtime.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
630
+ }
631
+
632
+ const drawVLineArgs = valueArgsModel([
633
+ {'name': 'y', 'type': 'number'}
634
+ ]);
635
+
636
+
637
+ // $function: drawWidth
638
+ // $group: Drawing
639
+ // $doc: Get the current drawing's width
640
+ // $return: The current drawing's width
641
+ function drawWidth(unusedArgs, options) {
642
+ return options.runtime.drawingWidth;
643
+ }
644
+
645
+
646
+ //
647
+ // Element Model functions
648
+ //
649
+
650
+
651
+ // $function: elementModelRender
652
+ // $group: Element Model
653
+ // $doc: Render an [element model](https://github.com/craigahobbs/element-model#readme)
654
+ // $doc:
655
+ // $doc: **Note:** Element model "callback" members are a map of event name (e.g., "click") to
656
+ // $doc: event callback function. The following events have callback arguments:
657
+ // $doc: - **keydown** - keyCode
658
+ // $doc: - **keypress** - keyCode
659
+ // $doc: - **keyup** - keyCode
660
+ // $arg element: The [element model](https://github.com/craigahobbs/element-model#readme)
661
+ function elementModelRender([elements = null], options) {
662
+ const {runtime} = options;
663
+ runtime.setElements();
664
+ elementModelWrapCallbacks(elements, options);
665
+ runtime.addElements(validateElements(elements));
666
+ }
661
667
 
662
668
 
663
- // Helper function for elementModelRender function
664
669
  function elementModelWrapCallbacks(elements, options) {
665
670
  // Ignore non-objects
666
671
  if (elements === null || typeof elements !== 'object') {
@@ -712,3 +717,367 @@ function elementModelWrapCallbacks(elements, options) {
712
717
  }
713
718
  }
714
719
  }
720
+
721
+
722
+ //
723
+ // Local storage functions
724
+ //
725
+
726
+
727
+ // $function: localStorageClear
728
+ // $group: Local Storage
729
+ // $doc: Clear all keys from the browser's local storage
730
+ function localStorageClear(unusedArgs, options) {
731
+ options.window.localStorage.clear();
732
+ }
733
+
734
+
735
+ // $function: localStorageGet
736
+ // $group: Local Storage
737
+ // $doc: Get a browser local storage key's value
738
+ // $arg key: The key string
739
+ // $return: The local storage value string or null if the key does not exist
740
+ function localStorageGet(args, options) {
741
+ const [key] = valueArgsValidate(localStorageGetArgs, args);
742
+ return options.window.localStorage.getItem(key);
743
+ }
744
+
745
+ const localStorageGetArgs = valueArgsModel([
746
+ {'name': 'key', 'type': 'string'}
747
+ ]);
748
+
749
+
750
+ // $function: localStorageRemove
751
+ // $group: Local Storage
752
+ // $doc: Remove a browser local storage key
753
+ // $arg key: The key string
754
+ function localStorageRemove(args, options) {
755
+ const [key] = valueArgsValidate(localStorageRemoveArgs, args);
756
+ return options.window.localStorage.removeItem(key);
757
+ }
758
+
759
+ const localStorageRemoveArgs = valueArgsModel([
760
+ {'name': 'key', 'type': 'string'}
761
+ ]);
762
+
763
+
764
+ // $function: localStorageSet
765
+ // $group: Local Storage
766
+ // $doc: Set a browser local storage key's value
767
+ // $arg key: The key string
768
+ // $arg value: The value string
769
+ function localStorageSet(args, options) {
770
+ const [key, value] = valueArgsValidate(localStorageSetArgs, args);
771
+ return options.window.localStorage.setItem(key, value);
772
+ }
773
+
774
+ const localStorageSetArgs = valueArgsModel([
775
+ {'name': 'key', 'type': 'string'},
776
+ {'name': 'value'}
777
+ ]);
778
+
779
+
780
+ //
781
+ // Markdown functions
782
+ //
783
+
784
+
785
+ // $function: markdownEscape
786
+ // $group: Markdown
787
+ // $doc: Escape text for inclusion in Markdown text
788
+ // $arg text: The text
789
+ // $return: The escaped text
790
+ function markdownEscape(args) {
791
+ const [text] = valueArgsValidate(markdownEscapeArgs, args);
792
+ return escapeMarkdownText(text);
793
+ }
794
+
795
+ const markdownEscapeArgs = valueArgsModel([
796
+ {'name': 'text', 'type': 'string'}
797
+ ]);
798
+
799
+
800
+ // $function: markdownHeaderId
801
+ // $group: Markdown
802
+ // $doc: Compute the Markdown header element ID for some text
803
+ // $arg text: The text
804
+ // $return: The header element ID
805
+ function markdownHeaderId(args) {
806
+ const [text] = valueArgsValidate(markdownHeaderIdArgs, args);
807
+ return markdownModelHeaderId(text);
808
+ }
809
+
810
+ const markdownHeaderIdArgs = valueArgsModel([
811
+ {'name': 'text', 'type': 'string'}
812
+ ]);
813
+
814
+
815
+ // $function: markdownParse
816
+ // $group: Markdown
817
+ // $doc: Parse Markdown text
818
+ // $arg lines...: The Markdown text lines (may contain nested arrays of un-split lines)
819
+ // $return: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
820
+ function markdownParse(lines) {
821
+ return parseMarkdown(lines.flat());
822
+ }
823
+
824
+
825
+ // $function: markdownPrint
826
+ // $group: Markdown
827
+ // $doc: Render Markdown text
828
+ // $arg lines...: The Markdown text lines (may contain nested arrays of un-split lines)
829
+ function markdownPrint(lines, options) {
830
+ const {runtime} = options;
831
+ runtime.setMarkdown();
832
+ runtime.addMarkdown(lines);
833
+ }
834
+
835
+
836
+ // $function: markdownTitle
837
+ // $group: Markdown
838
+ // $doc: Compute the title of a [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
839
+ // $arg markdownModel: The [Markdown model](https://craigahobbs.github.io/markdown-model/model/#var.vName='Markdown')
840
+ // $return: The Markdown title or null if there is no title
841
+ function markdownTitle(args) {
842
+ const [markdownModel] = valueArgsValidate(markdownTitleArgs, args);
843
+ return getMarkdownTitle(markdownModel);
844
+ }
845
+
846
+ const markdownTitleArgs = valueArgsModel([
847
+ {'name': 'markdownModel', 'type': 'object'},
848
+ ]);
849
+
850
+
851
+ //
852
+ // Schema functions
853
+ //
854
+
855
+
856
+ // $function: schemaElements
857
+ // $group: Schema
858
+ // $doc: Get a schema type's documentation [element model](https://github.com/craigahobbs/element-model#readme).
859
+ // $doc: Render the element model with the [elementModelRender](#var.vName='elementModelRender') function.
860
+ // $arg types: The [type model](https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='Types')
861
+ // $arg typeName: The type name
862
+ // $arg actionURLs: Optional (default is null). The
863
+ // $arg actionURLs: [action URL overrides](https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='ActionURL').
864
+ // $return: The schema type's documentation [element model](https://github.com/craigahobbs/element-model#readme)
865
+ function schemaElements(args, options) {
866
+ const [types, typeName, actionURLs = null] = valueArgsValidate(schemaElementsArgs, args);
867
+ const params = encodeQueryString(options.params);
868
+ const schemaOptions = {params};
869
+ if (actionURLs !== null) {
870
+ schemaOptions.actionURLs = actionURLs;
871
+ }
872
+ return schemaMarkdownDoc(types, typeName, schemaOptions);
873
+ }
874
+
875
+ const schemaElementsArgs = valueArgsModel([
876
+ {'name': 'types', 'type': 'object'},
877
+ {'name': 'typeName', 'type': 'string'},
878
+ {'name': 'actionURLs', 'type': 'array', 'nullable': true}
879
+ ]);
880
+
881
+
882
+ //
883
+ // Session storage functions
884
+ //
885
+
886
+
887
+ // $function: sessionStorageClear
888
+ // $group: Session Storage
889
+ // $doc: Clear all keys from the browser's session storage
890
+ function sessionStorageClear(unusedArgs, options) {
891
+ return options.window.sessionStorage.clear();
892
+ }
893
+
894
+
895
+ // $function: sessionStorageGet
896
+ // $group: Session Storage
897
+ // $doc: Get a browser session storage key's value
898
+ // $arg key: The key string
899
+ // $return: The session storage value string or null if the key does not exist
900
+ function sessionStorageGet(args, options) {
901
+ const [key] = valueArgsValidate(sessionStorageGetArgs, args);
902
+ return options.window.sessionStorage.getItem(key);
903
+ }
904
+
905
+ const sessionStorageGetArgs = valueArgsModel([
906
+ {'name': 'key', 'type': 'string'}
907
+ ]);
908
+
909
+
910
+ // $function: sessionStorageRemove
911
+ // $group: Session Storage
912
+ // $doc: Remove a browser session storage key
913
+ // $arg key: The key string
914
+ function sessionStorageRemove(args, options) {
915
+ const [key] = valueArgsValidate(sessionStorageRemoveArgs, args);
916
+ return options.window.sessionStorage.removeItem(key);
917
+ }
918
+
919
+ const sessionStorageRemoveArgs = valueArgsModel([
920
+ {'name': 'key', 'type': 'string'}
921
+ ]);
922
+
923
+
924
+ // $function: sessionStorageSet
925
+ // $group: Session Storage
926
+ // $doc: Set a browser session storage key's value
927
+ // $arg key: The key string
928
+ // $arg value: The value string
929
+ function sessionStorageSet(args, options) {
930
+ const [key, value] = valueArgsValidate(sessionStorageSetArgs, args);
931
+ return options.window.sessionStorage.setItem(key, value);
932
+ }
933
+
934
+ const sessionStorageSetArgs = valueArgsModel([
935
+ {'name': 'key', 'type': 'string'},
936
+ {'name': 'value'}
937
+ ]);
938
+
939
+
940
+ //
941
+ // Window functions
942
+ //
943
+
944
+
945
+ // $function: windowHeight
946
+ // $group: Window
947
+ // $doc: Get the browser window's height
948
+ // $return: The browser window's height
949
+ function windowHeight(unusedArgs, options) {
950
+ return options.window.innerHeight;
951
+ }
952
+
953
+
954
+ // $function: windowSetLocation
955
+ // $group: Window
956
+ // $doc: Navigate the browser window to a location URL
957
+ // $arg url: The new location URL
958
+ function windowSetLocation(args, options) {
959
+ const [location] = valueArgsValidate(windowSetLocationArgs, args);
960
+ const {runtime} = options;
961
+ runtime.windowLocation = options.urlFn(location);
962
+ }
963
+
964
+ const windowSetLocationArgs = valueArgsModel([
965
+ {'name': 'url', 'type': 'string'}
966
+ ]);
967
+
968
+
969
+ // $function: windowSetResize
970
+ // $group: Window
971
+ // $doc: Set the browser window resize event handler
972
+ // $arg callback: The window resize callback function
973
+ function windowSetResize(args, options) {
974
+ const [callback] = valueArgsValidate(windowSetResizeArgs, args);
975
+ const {runtime} = options;
976
+ runtime.windowResize = async () => {
977
+ options.statementCount = 0;
978
+ try {
979
+ await callback([], options);
980
+ } catch ({message}) {
981
+ if ('logFn' in options && options.debug) {
982
+ options.logFn(`MarkdownUp: Error executing windowSetResize callback: ${message}`);
983
+ }
984
+ }
985
+ options.runtimeUpdateFn();
986
+ };
987
+ }
988
+
989
+ const windowSetResizeArgs = valueArgsModel([
990
+ {'name': 'callback', 'type': 'function'}
991
+ ]);
992
+
993
+
994
+ // $function: windowSetTimeout
995
+ // $group: Window
996
+ // $doc: Set the browser window timeout event handler
997
+ // $arg callback: The window timeout callback function
998
+ // $arg delay: The delay, in milliseconds, to ellapse before calling the timeout
999
+ function windowSetTimeout(args, options) {
1000
+ const [callback, delay] = valueArgsValidate(windowSetTimeoutArgs, args);
1001
+ const {runtime} = options;
1002
+ runtime.windowTimeout = [
1003
+ async () => {
1004
+ options.statementCount = 0;
1005
+ try {
1006
+ await callback([], options);
1007
+ } catch ({message}) {
1008
+ if ('logFn' in options && options.debug) {
1009
+ options.logFn(`MarkdownUp: Error executing windowSetTimeout callback: ${message}`);
1010
+ }
1011
+ }
1012
+ options.runtimeUpdateFn();
1013
+ },
1014
+ delay
1015
+ ];
1016
+ }
1017
+
1018
+ const windowSetTimeoutArgs = valueArgsModel([
1019
+ {'name': 'callback', 'type': 'function'},
1020
+ {'name': 'delay', 'type': 'number'}
1021
+ ]);
1022
+
1023
+
1024
+ // $function: windowWidth
1025
+ // $group: Window
1026
+ // $doc: Get the browser window's width
1027
+ // $return: The browser window's width
1028
+ function windowWidth(unusedArgs, options) {
1029
+ return options.window.innerWidth;
1030
+ }
1031
+
1032
+
1033
+ // markdown-script library functions
1034
+ export const markdownScriptFunctions = {
1035
+ dataLineChart,
1036
+ dataTable,
1037
+ documentFontSize,
1038
+ documentInputValue,
1039
+ documentSetFocus,
1040
+ documentSetReset,
1041
+ documentSetTitle,
1042
+ documentURL,
1043
+ drawArc,
1044
+ drawCircle,
1045
+ drawClose,
1046
+ drawEllipse,
1047
+ drawHLine,
1048
+ drawHeight,
1049
+ drawImage,
1050
+ drawLine,
1051
+ drawMove,
1052
+ drawNew,
1053
+ drawOnClick,
1054
+ drawPathRect,
1055
+ drawRect,
1056
+ drawStyle,
1057
+ drawText,
1058
+ drawTextHeight,
1059
+ drawTextStyle,
1060
+ drawTextWidth,
1061
+ drawVLine,
1062
+ drawWidth,
1063
+ elementModelRender,
1064
+ localStorageClear,
1065
+ localStorageGet,
1066
+ localStorageRemove,
1067
+ localStorageSet,
1068
+ markdownEscape,
1069
+ markdownHeaderId,
1070
+ markdownParse,
1071
+ markdownPrint,
1072
+ markdownTitle,
1073
+ schemaElements,
1074
+ sessionStorageClear,
1075
+ sessionStorageGet,
1076
+ sessionStorageRemove,
1077
+ sessionStorageSet,
1078
+ windowHeight,
1079
+ windowSetLocation,
1080
+ windowSetResize,
1081
+ windowSetTimeout,
1082
+ windowWidth
1083
+ };