markdown-up 1.7.2 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/script.js CHANGED
@@ -3,72 +3,74 @@
3
3
 
4
4
  /** @module lib/script */
5
5
 
6
- import {encodeMarkdownText, getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
7
- import {validateType, validateTypeModel} from 'schema-markdown/lib/schema.js';
8
- import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
9
6
  import {encodeQueryString} from 'schema-markdown/lib/encode.js';
10
7
  import {executeScriptAsync} from 'calc-script/lib/runtimeAsync.js';
11
- import {markdownElementsAsync} from 'markdown-model/lib/elements.js';
8
+ import {markdownElements} from 'markdown-model/lib/elements.js';
9
+ import {parseMarkdown} from 'markdown-model/lib/parser.js';
12
10
  import {parseScript} from 'calc-script/lib/parser.js';
13
11
  import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
14
- import {typeModel} from 'schema-markdown/lib/typeModel.js';
12
+ import {validateElements} from 'element-model/lib/elementModel.js';
15
13
 
16
14
 
15
+ /* eslint-disable id-length, max-len */
16
+
17
+
18
+ /**
19
+ * The markdown-script code block function options (based on
20
+ * [calc-script's options]{@link https://craigahobbs.github.io/calc-script/module-lib_runtime.html#~ExecuteScriptOptions}).
21
+ *
22
+ * @typedef {Object} MarkdownScriptOptions
23
+ * @property {number} fontSize - The font size, in points
24
+ * @property {Object} params - The hash parameters object
25
+ * @property {Object} runtime - The [markdown-script runtime state]{@link module:lib/script.MarkdownScriptRuntime}
26
+ * @property {Object} [variables] - The map of variable name to variable value
27
+ * @property {Object} window - The web browser window object
28
+ * @property {function} runtimeUpdateFn - The [runtime update callback function]{@link module:lib/util~MarkdownScriptRuntimeUpdateFn}
29
+ */
30
+
31
+ /**
32
+ * A runtime update callback function
33
+ *
34
+ * @callback MarkdownScriptRuntimeUpdateFn
35
+ */
36
+
17
37
  /**
18
- * markdown-script code block function
38
+ * A URL modifier function
39
+ *
40
+ * @callback URLFn
41
+ * @param {string} url - The URL
42
+ * @returns {string} The modified URL
43
+ */
44
+
45
+
46
+ /**
47
+ * The markdown-script code block function
19
48
  *
20
49
  * @async
21
- * @param {string} language - The code block language
22
- * @param {string[]} lines - The code block's text lines
23
- * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
50
+ * @param {object} codeBlock - The code block model
51
+ * @param {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
24
52
  * @returns {Object} The generated element model
25
53
  */
26
- export async function markdownScriptCodeBlock(language, lines, options = null) {
27
- // Get/create the script's runtime
28
- const runtime = (
29
- options !== null && 'runtime' in options && options.runtime !== null
30
- ? options.runtime
31
- : new MarkdownScriptRuntime(options)
32
- );
33
-
54
+ export async function markdownScriptCodeBlock(codeBlock, options) {
34
55
  // Add the options variables to the runtime's globals
35
- if (options !== null && 'variables' in options) {
36
- Object.assign(runtime.globals, options.variables);
56
+ if ('variables' in options) {
57
+ Object.assign(options.globals, options.variables);
37
58
  }
38
59
 
39
60
  // Execute the calculation script
40
61
  let errorMessage = null;
41
62
  try {
42
- await executeScriptAsync(parseScript(lines), runtime.globals, options);
63
+ await executeScriptAsync(parseScript(codeBlock.lines, codeBlock.startLineNumber + 1), options);
43
64
  } catch ({message}) {
44
65
  errorMessage = message;
45
- } finally {
46
- runtime.finishDrawingPath();
47
- }
48
-
49
- // Create the markdownElements options
50
- const markdownElementsOptions = {'headerIds': true};
51
- if (options !== null && 'urlFn' in options) {
52
- markdownElementsOptions.urlFn = options.urlFn;
53
- }
54
-
55
- // Render the element model parts
56
- const elements = [];
57
- for (const part of runtime.elementParts) {
58
- if ('drawing' in part) {
59
- elements.push({'html': 'p', 'elem': part.drawing});
60
- } else if ('markdown' in part) {
61
- // eslint-disable-next-line no-await-in-loop
62
- elements.push(await markdownElementsAsync(parseMarkdown(part.markdown), markdownElementsOptions));
63
- } else {
64
- elements.push(part.elements);
65
- }
66
66
  }
67
- runtime.elementParts.length = 0;
67
+
68
+ // Reset the runtime
69
+ let elements = options.runtime.resetElements();
68
70
 
69
71
  // If an error occurred, render the error message
70
72
  if (errorMessage !== null) {
71
- elements.push({'html': 'p', 'elem': {'html': 'pre', 'elem': {'text': errorMessage}}});
73
+ elements = [elements, {'html': 'pre', 'elem': {'text': errorMessage}}];
72
74
  }
73
75
 
74
76
  return elements;
@@ -78,115 +80,43 @@ export async function markdownScriptCodeBlock(language, lines, options = null) {
78
80
  // markdown-script runtime constants
79
81
  const defaultDrawingWidth = 300;
80
82
  const defaultDrawingHeight = 200;
81
- const svgPrecision = 8;
82
- const fontWidthRatio = 0.6;
83
- const pixelsPerPoint = 4 / 3;
84
83
  const defaultFontFamily = 'Arial, Helvetica, sans-serif';
85
- const defaultFontSizePx = 12 * pixelsPerPoint;
86
-
87
-
88
- /**
89
- * @typedef {Object} ElementPart
90
- * @property {Object} drawing - SVG element model
91
- * @property {string[]} markdown - Markdown text lines
92
- */
84
+ const pixelsPerPoint = 4 / 3;
85
+ const svgPrecision = 8;
93
86
 
94
87
 
95
88
  /**
96
- * markdown-script runtime state
89
+ * The markdown-script runtime state
97
90
  *
98
- * @property {Object} globals - The global variables
99
- * @property {module:lib/util~MarkdownScriptOptions} [options = null] - Markdown script options
100
- * @property {module:lib/script~ElementPart[]} elementParts - The element model parts to render
101
- * @property {?string} documentTitle - The document title
91
+ * @property {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
92
+ * @property {?Object[]} elements - The runtime-generated element model
93
+ * @property {?string} documentFocus - The the runtime-set element ID to set input focus
94
+ * @property {?string} documentTitle - The the runtime-set document title
95
+ * @property {boolean} isDocumentReset - If true, the runtime requested a document-reset
96
+ * @property {?string} windowLocation - The the runtime-set document location
97
+ * @property {?function} windowTimeout - The the runtime-set timeout args (callback, delay)
102
98
  */
103
99
  export class MarkdownScriptRuntime {
104
100
  /**
105
- * @param {module:lib/util~MarkdownScriptOptions} [options = null] - Markdown script options
101
+ * @param {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
106
102
  */
107
- constructor(options = null) {
103
+ constructor(options) {
108
104
  this.options = options;
109
-
110
- // The runtime's global variables
111
- this.globals = {
112
- // Drawing functions
113
- 'drawArc': (args) => this.drawArc(...args),
114
- 'drawCircle': (args) => this.drawCircle(...args),
115
- 'drawClose': () => this.drawClose(),
116
- 'drawEllipse': (args) => this.drawEllipse(...args),
117
- 'drawHLine': (args) => this.drawHLine(...args),
118
- 'drawLine': (args) => this.drawLine(...args),
119
- 'drawMove': (args) => this.drawMove(...args),
120
- 'drawOnClick': (args) => this.drawOnClick(...args),
121
- 'drawRect': (args) => this.drawRect(...args),
122
- 'drawStyle': (args) => this.drawStyle(...args),
123
- 'drawText': (args) => this.drawText(...args),
124
- 'drawTextStyle': (args) => this.drawTextStyle(...args),
125
- 'drawVLine': (args) => this.drawVLine(...args),
126
- 'getDrawingHeight': () => this.drawingHeight,
127
- 'getDrawingWidth': () => this.drawingWidth,
128
- 'getTextHeight': (args) => this.drawTextHeight(...args),
129
- 'getTextWidth': (args) => this.drawTextWidth(...args),
130
- 'setDrawingSize': (args) => this.setDrawingSize(...args),
131
-
132
- // Local storage functions
133
- 'localStorageGetItem': ([key]) => (
134
- options !== null && 'localStorage' in options ? options.localStorage.getItem(key) : null
135
- ),
136
- 'localStorageSetItem': ([key, value]) => (
137
- options !== null && 'localStorage' in options ? options.localStorage.setItem(key, value) : null
138
- ),
139
- 'localStorageRemoveItem': ([key]) => (
140
- options !== null && 'localStorage' in options ? options.localStorage.removeItem(key) : null
141
- ),
142
- 'localStorageClear': () => (
143
- options !== null && 'localStorage' in options ? options.localStorage.clear() : null
144
- ),
145
-
146
- // Markdown functions
147
- 'markdownEncode': (args) => encodeMarkdownText(...args),
148
- 'markdownParse': (lines) => parseMarkdown(MarkdownScriptRuntime.flattenAndSplitLines(lines)),
149
- 'markdownPrint': (lines) => this.markdownPrint(lines),
150
- 'markdownTitle': ([markdownModel]) => getMarkdownTitle(markdownModel),
151
-
152
- // Schema functions
153
- 'schemaParse': (lines) => MarkdownScriptRuntime.schemaParse(lines),
154
- 'schemaTypeModel': () => typeModel,
155
- 'schemaValidate': ([types, typeName, value]) => validateType(types, typeName, value),
156
- 'schemaValidateTypeModel': ([types]) => validateTypeModel(types),
157
- 'schemaPrint': ([types, typeName, actionURLs]) => this.schemaPrint(types, typeName, actionURLs),
158
-
159
- // Session storage functions
160
- 'sessionStorageGetItem': ([key]) => (
161
- options !== null && 'sessionStorage' in options ? options.sessionStorage.getItem(key) : null
162
- ),
163
- 'sessionStorageSetItem': ([key, value]) => (
164
- options !== null && 'sessionStorage' in options ? options.sessionStorage.setItem(key, value) : null
165
- ),
166
- 'sessionStorageRemoveItem': ([key]) => (
167
- options !== null && 'sessionStorage' in options ? options.sessionStorage.removeItem(key) : null
168
- ),
169
- 'sessionStorageClear': () => (
170
- options !== null && 'sessionStorage' in options ? options.sessionStorage.clear() : null
171
- ),
172
-
173
- // Utility functions
174
- 'setDocumentTitle': ([title]) => {
175
- this.documentTitle = title;
176
- },
177
- 'setNavigateTimeout': (args) => this.setNavigateTimeout(...args)
178
- };
179
-
180
- // The document's title
105
+ this.elements = null;
106
+ this.documentFocus = null;
181
107
  this.documentTitle = null;
108
+ this.isDocumentReset = false;
109
+ this.windowLocation = null;
110
+ this.windowResize = null;
111
+ this.windowTimeout = null;
182
112
 
183
- // Element model parts ('drawing', 'markdown')
184
- this.elementParts = [];
113
+ // Drawing-path and Markdown function state
114
+ this.drawingPath = null;
115
+ this.markdown = null;
185
116
 
186
117
  // Drawing state
187
118
  this.drawingWidth = defaultDrawingWidth;
188
119
  this.drawingHeight = defaultDrawingHeight;
189
- this.drawingPath = [];
190
120
 
191
121
  // Drawing path style
192
122
  this.drawingPathStroke = 'black';
@@ -196,83 +126,176 @@ export class MarkdownScriptRuntime {
196
126
 
197
127
  // Drawing text style
198
128
  this.drawingFontFamily = defaultFontFamily;
199
- this.drawingFontSizePx = (
200
- options !== null && 'fontSize' in options && options.fontSize !== null ? options.fontSize * pixelsPerPoint : defaultFontSizePx
201
- );
129
+ this.drawingFontSizePx = options.fontSize * pixelsPerPoint;
202
130
  this.drawingFontFill = 'black';
203
131
  this.drawingFontBold = false;
204
132
  this.drawingFontItalic = false;
205
133
  }
206
134
 
207
- setDrawing(newDrawing = false) {
208
- let part = this.elementParts.length !== 0 ? this.elementParts[this.elementParts.length - 1] : null;
209
- const isDrawing = part !== null && 'drawing' in part;
210
- if (!isDrawing || (newDrawing && isDrawing)) {
211
- part = {
212
- 'drawing': {
213
- 'svg': 'svg',
214
- 'attr': {
215
- 'width': this.drawingWidth,
216
- 'height': this.drawingHeight
217
- },
218
- 'elem': []
219
- }
220
- };
221
- this.elementParts.push(part);
222
- }
223
- return part.drawing;
135
+
136
+ reset() {
137
+ this.documentFocus = null;
138
+ this.documentTitle = null;
139
+ this.isDocumentReset = false;
140
+ this.windowLocation = null;
141
+ this.windowTimeout = null;
142
+ return this.resetElements();
224
143
  }
225
144
 
226
- setElements(elements) {
227
- const part = this.elementParts.length !== 0 ? this.elementParts[this.elementParts.length - 1] : null;
228
- const isDrawing = part !== null && 'drawing' in part;
229
- if (isDrawing) {
230
- this.finishDrawingPath();
145
+
146
+ resetElements() {
147
+ let elements = null;
148
+ if (this.elements !== null) {
149
+ this.setElements();
150
+ ({elements} = this);
231
151
  }
232
- this.elementParts.push({'elements': elements});
152
+ this.elements = null;
153
+ return elements;
233
154
  }
234
155
 
235
- setMarkdown() {
236
- let part = this.elementParts.length !== 0 ? this.elementParts[this.elementParts.length - 1] : null;
237
- const isDrawing = part !== null && 'drawing' in part;
238
- if (isDrawing) {
239
- this.finishDrawingPath();
156
+
157
+ setElements() {
158
+ if (this.elements === null) {
159
+ this.elements = [];
240
160
  }
241
- const isMarkdown = part !== null && 'markdown' in part;
242
- if (!isMarkdown) {
243
- part = {'markdown': []};
244
- this.elementParts.push(part);
161
+ this.finishDrawingPath();
162
+ this.finishMarkdown();
163
+ }
164
+
165
+
166
+ getDrawingSVG() {
167
+ const par = (this.elements.length !== 0 ? this.elements[this.elements.length - 1] : null);
168
+ const svg = (par !== null && par.html === 'p' && 'elem' in par ? par.elem : null);
169
+ return svg !== null && svg.svg === 'svg' ? svg : null;
170
+ }
171
+
172
+
173
+ setDrawing(newDrawing = false) {
174
+ this.setElements();
175
+ let svg = this.getDrawingSVG();
176
+ if (svg === null || newDrawing) {
177
+ svg = {
178
+ 'svg': 'svg',
179
+ 'attr': {
180
+ 'width': this.drawingWidth,
181
+ 'height': this.drawingHeight
182
+ },
183
+ 'elem': []
184
+ };
185
+ this.elements.push({'html': 'p', 'elem': svg});
245
186
  }
246
- return part.markdown;
187
+ return svg;
247
188
  }
248
189
 
249
- finishDrawingPath() {
250
- if (this.drawingPath.length !== 0) {
251
- this.elementParts[this.elementParts.length - 1].drawing.elem.push({
190
+
191
+ setDrawingPath() {
192
+ if (this.drawingPath === null) {
193
+ const svg = this.setDrawing();
194
+ svg.elem.push({
252
195
  'svg': 'path',
253
196
  'attr': {
254
197
  'fill': this.drawingPathFill,
255
198
  'stroke': this.drawingPathStroke,
256
199
  'stroke-width': this.drawingPathStrokeWidth,
257
200
  'stroke-dasharray': this.drawingPathStrokeDashArray,
258
- 'd': this.drawingPath.join(' ')
201
+ 'd': ''
259
202
  }
260
203
  });
261
- this.drawingPath.length = 0;
204
+ this.drawingPath = [];
205
+ }
206
+ }
207
+
208
+
209
+ finishDrawingPath() {
210
+ if (this.drawingPath !== null) {
211
+ const svg = this.getDrawingSVG();
212
+ const path = svg.elem[svg.elem.length - 1];
213
+ // eslint-disable-next-line id-length
214
+ path.attr.d = this.drawingPath.join(' ');
215
+ this.drawingPath = null;
216
+ }
217
+ }
218
+
219
+
220
+ setMarkdown() {
221
+ if (this.markdown === null) {
222
+ this.setElements();
223
+ this.markdown = [];
224
+ }
225
+ }
226
+
227
+
228
+ finishMarkdown() {
229
+ if (this.markdown !== null) {
230
+ const markdownOptions = {'headerIds': true, 'urlFn': this.options.urlFn};
231
+ this.elements.push(markdownElements(parseMarkdown(this.markdown), markdownOptions));
232
+ this.markdown = null;
262
233
  }
263
234
  }
264
235
 
265
- drawArc(rx, ry, xAxisRotation, largeArcFlag, sweepFlag, px, py) {
266
- this.setDrawing();
236
+
237
+ //
238
+ // Document functions
239
+ //
240
+
241
+
242
+ documentReset() {
243
+ this.isDocumentReset = true;
244
+ }
245
+
246
+
247
+ setDocumentFocus(id) {
248
+ this.documentFocus = id;
249
+ }
250
+
251
+
252
+ setDocumentTitle(title) {
253
+ this.documentTitle = title;
254
+ }
255
+
256
+
257
+ setWindowLocation(location) {
258
+ this.windowLocation = this.options.urlFn(location);
259
+ }
260
+
261
+
262
+ setWindowResize(callback) {
263
+ this.windowResize = () => {
264
+ this.options.statementCount = 0;
265
+ callback([], this.options);
266
+ this.options.runtimeUpdateFn();
267
+ };
268
+ }
269
+
270
+
271
+ setWindowTimeout(callback, delay) {
272
+ this.windowTimeout = [
273
+ () => {
274
+ this.options.statementCount = 0;
275
+ callback([], this.options);
276
+ this.options.runtimeUpdateFn();
277
+ },
278
+ delay
279
+ ];
280
+ }
281
+
282
+
283
+ //
284
+ // Drawing functions
285
+ //
286
+
287
+
288
+ drawArc(rx, ry, angle, largeArcFlag, sweepFlag, x, y) {
289
+ this.setDrawingPath();
267
290
  this.drawingPath.push(
268
- `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${xAxisRotation.toFixed(svgPrecision)} ` +
269
- `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${px.toFixed(svgPrecision)} ${py.toFixed(svgPrecision)}`
291
+ `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
292
+ `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
270
293
  );
271
294
  }
272
295
 
273
- drawCircle(cx, cy, radius) {
296
+
297
+ drawCircle(cx, cy, r) {
274
298
  const svg = this.setDrawing();
275
- this.finishDrawingPath();
276
299
  svg.elem.push({
277
300
  'svg': 'circle',
278
301
  'attr': {
@@ -282,19 +305,20 @@ export class MarkdownScriptRuntime {
282
305
  'stroke-dasharray': this.drawingPathStrokeDashArray,
283
306
  'cx': cx,
284
307
  'cy': cy,
285
- 'r': radius
308
+ 'r': r
286
309
  }
287
310
  });
288
311
  }
289
312
 
313
+
290
314
  drawClose() {
291
- this.setDrawing();
315
+ this.setDrawingPath();
292
316
  this.drawingPath.push('Z');
293
317
  }
294
318
 
319
+
295
320
  drawEllipse(cx, cy, rx, ry) {
296
321
  const svg = this.setDrawing();
297
- this.finishDrawingPath();
298
322
  svg.elem.push({
299
323
  'svg': 'ellipse',
300
324
  'attr': {
@@ -310,35 +334,56 @@ export class MarkdownScriptRuntime {
310
334
  });
311
335
  }
312
336
 
313
- drawHLine(px) {
314
- this.setDrawing();
315
- this.drawingPath.push(`H ${px.toFixed(svgPrecision)}`);
337
+
338
+ drawHLine(x) {
339
+ this.setDrawingPath();
340
+ this.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
316
341
  }
317
342
 
318
- drawLine(px, py) {
319
- this.setDrawing();
320
- this.drawingPath.push(`L ${px.toFixed(svgPrecision)} ${py.toFixed(svgPrecision)}`);
343
+
344
+ drawImage(x, y, width, height, href) {
345
+ const svg = this.setDrawing();
346
+ svg.elem.push({
347
+ 'svg': 'image',
348
+ 'attr': {
349
+ 'x': x,
350
+ 'y': y,
351
+ 'width': width,
352
+ 'height': height,
353
+ 'href': this.options.urlFn(href)
354
+ }
355
+ });
321
356
  }
322
357
 
323
- drawMove(px, py) {
324
- this.setDrawing();
325
- this.drawingPath.push(`M ${px.toFixed(svgPrecision)} ${py.toFixed(svgPrecision)}`);
358
+
359
+ drawLine(x, y) {
360
+ this.setDrawingPath();
361
+ this.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
326
362
  }
327
363
 
364
+
365
+ drawMove(x, y) {
366
+ this.setDrawingPath();
367
+ this.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
368
+ }
369
+
370
+
328
371
  drawOnClick(callback) {
329
372
  const svg = this.setDrawing();
330
373
  const clickElement = svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1];
331
374
  clickElement.callback = (element) => {
332
375
  element.addEventListener('click', (event) => {
333
376
  const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
334
- callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top]);
377
+ this.options.statementCount = 0;
378
+ callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], this.options);
379
+ this.options.runtimeUpdateFn();
335
380
  });
336
381
  };
337
382
  }
338
383
 
339
- drawRect(px, py, width, height, rx = null, ry = null) {
384
+
385
+ drawRect(x, y, width, height, rx = null, ry = null) {
340
386
  const svg = this.setDrawing();
341
- this.finishDrawingPath();
342
387
  const element = {
343
388
  'svg': 'rect',
344
389
  'attr': {
@@ -346,8 +391,8 @@ export class MarkdownScriptRuntime {
346
391
  'stroke': this.drawingPathStroke,
347
392
  'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
348
393
  'stroke-dasharray': this.drawingPathStrokeDashArray,
349
- 'x': px,
350
- 'y': py,
394
+ 'x': x,
395
+ 'y': y,
351
396
  'width': width,
352
397
  'height': height
353
398
  }
@@ -361,10 +406,12 @@ export class MarkdownScriptRuntime {
361
406
  svg.elem.push(element);
362
407
  }
363
408
 
409
+
364
410
  drawStyle(stroke = 'black', strokeWidth = 1, fill = 'none', strokeDashArray = 'none') {
365
411
  if (stroke !== this.drawingPathStroke || strokeWidth !== this.drawingPathStrokeWidth ||
366
- strokeDashArray !== this.drawingPathStrokeDashArray || fill !== this.drawingPathFill) {
367
- this.finishDrawingPath();
412
+ strokeDashArray !== this.drawingPathStrokeDashArray || fill !== this.drawingPathFill
413
+ ) {
414
+ this.setDrawing();
368
415
  this.drawingPathStroke = stroke;
369
416
  this.drawingPathStrokeWidth = strokeWidth;
370
417
  this.drawingPathStrokeDashArray = strokeDashArray;
@@ -372,9 +419,9 @@ export class MarkdownScriptRuntime {
372
419
  }
373
420
  }
374
421
 
375
- drawText(text, px, py, textAnchor = 'middle', dominantBaseline = 'middle') {
422
+
423
+ drawText(text, x, y, textAnchor = 'middle', dominantBaseline = 'middle') {
376
424
  const svg = this.setDrawing();
377
- this.finishDrawingPath();
378
425
  svg.elem.push({
379
426
  'svg': 'text',
380
427
  'attr': {
@@ -385,78 +432,109 @@ export class MarkdownScriptRuntime {
385
432
  'dominant-baseline': dominantBaseline,
386
433
  'font-weight': (this.drawingFontBold ? 'bold' : 'normal'),
387
434
  'font-style': (this.drawingFontItalic ? 'italic' : 'normal'),
388
- 'x': px,
389
- 'y': py
435
+ 'x': x,
436
+ 'y': y
390
437
  },
391
438
  'elem': {'text': text}
392
439
  });
393
440
  }
394
441
 
395
- drawTextHeight(text, width) {
396
- return width > 0 ? width / (fontWidthRatio * text.length) : this.drawingFontSizePx;
397
- }
398
442
 
399
- drawTextStyle(fontSizePx = defaultFontSizePx, textFill = 'black', bold = false, italic = false, fontFamily = defaultFontFamily) {
400
- this.drawingFontSizePx = fontSizePx;
443
+ drawTextStyle(fontSizePx = null, textFill = 'black', bold = false, italic = false, fontFamily = null) {
444
+ this.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : this.options.fontSize * pixelsPerPoint);
401
445
  this.drawingFontFill = textFill;
402
446
  this.drawingFontBold = bold;
403
447
  this.drawingFontItalic = italic;
404
- this.drawingFontFamily = fontFamily;
448
+ this.drawingFontFamily = (fontFamily !== null ? fontFamily : defaultFontFamily);
405
449
  }
406
450
 
407
- drawTextWidth(text) {
408
- return fontWidthRatio * this.drawiingFontSizePx * text.length;
451
+
452
+ drawVLine(y) {
453
+ this.setDrawingPath();
454
+ this.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
409
455
  }
410
456
 
411
- drawVLine(py) {
412
- this.setDrawing();
413
- this.drawingPath.push(`V ${py.toFixed(svgPrecision)}`);
457
+
458
+ setDrawingSize(width, height) {
459
+ this.drawingWidth = width;
460
+ this.drawingHeight = height;
461
+ this.setDrawing(true);
414
462
  }
415
463
 
416
- markdownPrint(lines) {
417
- const markdownLines = this.setMarkdown();
418
- markdownLines.push(...MarkdownScriptRuntime.flattenAndSplitLines(lines));
464
+
465
+ //
466
+ // Element Model functions
467
+ //
468
+
469
+
470
+ elementModelRender(elements) {
471
+ this.setElements();
472
+ this.elementModelWrapCallbacks(elements);
473
+ this.elements.push(validateElements(elements));
419
474
  }
420
475
 
421
- static schemaParse(lines) {
422
- const parser = new SchemaMarkdownParser();
423
- parser.parse(MarkdownScriptRuntime.flattenAndSplitLines(lines));
424
- return parser.types;
476
+
477
+ elementModelWrapCallbacks(elements) {
478
+ if (Array.isArray(elements)) {
479
+ for (const childElements of elements) {
480
+ this.elementModelWrapCallbacks(childElements);
481
+ }
482
+ } else if (typeof elements === 'object') {
483
+ if ('elem' in elements) {
484
+ this.elementModelWrapCallbacks(elements.elem);
485
+ }
486
+
487
+ // Element callback attribute must be map of event => callback
488
+ if ('callback' in elements) {
489
+ const elementEvents = elements.callback;
490
+ if (elementEvents !== null && typeof elementEvents === 'object') {
491
+ // Wrap the event handler function
492
+ elements.callback = (element) => {
493
+ // On element render, add a listener for each event
494
+ for (const [elementEvent, elementEventCallback] of Object.entries(elementEvents)) {
495
+ element.addEventListener(elementEvent, (event) => {
496
+ // Determine the event callback args
497
+ const eventArgs = [];
498
+ if (elementEvent === 'keydown' || elementEvent === 'keypress' || elementEvent === 'keyup') {
499
+ eventArgs.push(event.keyCode);
500
+ }
501
+
502
+ // Call the event handler
503
+ this.options.statementCount = 0;
504
+ elementEventCallback(eventArgs, this.options);
505
+ this.options.runtimeUpdateFn();
506
+ });
507
+ }
508
+ };
509
+ }
510
+ }
511
+ }
425
512
  }
426
513
 
427
- static flattenAndSplitLines(lines) {
428
- return lines.flat().reduce(
429
- (result, line) => {
430
- result.push(...line.split(rLineSplit));
431
- return result;
432
- },
433
- []
434
- );
514
+
515
+ //
516
+ // Markdown functions
517
+ //
518
+
519
+
520
+ markdownPrint(lines) {
521
+ this.setMarkdown();
522
+ this.markdown.push(...lines.flat());
435
523
  }
436
524
 
525
+
526
+ //
527
+ // Schema functions
528
+ //
529
+
530
+
437
531
  schemaPrint(types, typeName, actionURLs = null) {
438
- const params = (this.options !== null && 'params' in this.options ? encodeQueryString(this.options.params) : null);
532
+ const params = encodeQueryString(this.options.params);
439
533
  const options = {params};
440
534
  if (actionURLs !== null) {
441
535
  options.actionURLs = actionURLs;
442
536
  }
443
- this.setElements(schemaMarkdownDoc(types, typeName, {params, actionURLs}));
444
- }
445
-
446
- setDrawingSize(width, height) {
447
- this.finishDrawingPath();
448
- this.drawingWidth = width;
449
- this.drawingHeight = height;
450
- this.setDrawing(true);
451
- }
452
-
453
- setNavigateTimeout(url, delay = 0) {
454
- if (this.options !== null && 'navigateTimeoutFn' in this.options && this.navigateTimeoutFn !== null) {
455
- this.options.navigateTimeoutFn(url, delay);
456
- }
537
+ this.setElements();
538
+ this.elements.push(schemaMarkdownDoc(types, typeName, options));
457
539
  }
458
540
  }
459
-
460
-
461
- // Line-splitting regex
462
- const rLineSplit = /\r?\n/;