markdown-up 1.7.5 → 1.8.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.
package/lib/script.js CHANGED
@@ -1,18 +1,10 @@
1
1
  // Licensed under the MIT License
2
2
  // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
3
 
4
- /** @module lib/script */
5
-
6
- import {encodeQueryString} from 'schema-markdown/lib/encode.js';
7
4
  import {executeScriptAsync} from 'calc-script/lib/runtimeAsync.js';
8
5
  import {markdownElements} from 'markdown-model/lib/elements.js';
9
6
  import {parseMarkdown} from 'markdown-model/lib/parser.js';
10
7
  import {parseScript} from 'calc-script/lib/parser.js';
11
- import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
12
- import {validateElements} from 'element-model/lib/elementModel.js';
13
-
14
-
15
- /* eslint-disable id-length, max-len */
16
8
 
17
9
 
18
10
  /**
@@ -21,25 +13,22 @@ import {validateElements} from 'element-model/lib/elementModel.js';
21
13
  *
22
14
  * @typedef {Object} MarkdownScriptOptions
23
15
  * @property {number} fontSize - The font size, in points
24
- * @property {Object} params - The hash parameters object
16
+ * @property {Object} params - The [hash parameters object]{@link https://craigahobbs.github.io/markdown-up/#cmd.help=1}
25
17
  * @property {Object} runtime - The [markdown-script runtime state]{@link module:lib/script.MarkdownScriptRuntime}
26
18
  * @property {Object} [variables] - The map of variable name to variable value
27
19
  * @property {Object} window - The web browser window object
28
20
  * @property {function} runtimeUpdateFn - The [runtime update callback function]{@link module:lib/util~MarkdownScriptRuntimeUpdateFn}
21
+ *
22
+ * @ignore
29
23
  */
30
24
 
25
+
31
26
  /**
32
27
  * A runtime update callback function
33
28
  *
34
29
  * @callback MarkdownScriptRuntimeUpdateFn
35
- */
36
-
37
- /**
38
- * A URL modifier function
39
30
  *
40
- * @callback URLFn
41
- * @param {string} url - The URL
42
- * @returns {string} The modified URL
31
+ * @ignore
43
32
  */
44
33
 
45
34
 
@@ -50,6 +39,8 @@ import {validateElements} from 'element-model/lib/elementModel.js';
50
39
  * @param {object} codeBlock - The code block model
51
40
  * @param {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
52
41
  * @returns {Object} The generated element model
42
+ *
43
+ * @ignore
53
44
  */
54
45
  export async function markdownScriptCodeBlock(codeBlock, options) {
55
46
  // Add the options variables to the runtime's globals
@@ -77,12 +68,11 @@ export async function markdownScriptCodeBlock(codeBlock, options) {
77
68
  }
78
69
 
79
70
 
80
- // markdown-script runtime constants
71
+ // Constants
81
72
  const defaultDrawingWidth = 300;
82
73
  const defaultDrawingHeight = 200;
83
74
  const defaultFontFamily = 'Arial, Helvetica, sans-serif';
84
75
  const pixelsPerPoint = 4 / 3;
85
- const svgPrecision = 8;
86
76
 
87
77
 
88
78
  /**
@@ -94,7 +84,10 @@ const svgPrecision = 8;
94
84
  * @property {?string} documentTitle - The the runtime-set document title
95
85
  * @property {boolean} isDocumentReset - If true, the runtime requested a document-reset
96
86
  * @property {?string} windowLocation - The the runtime-set document location
97
- * @property {?function} windowTimeout - The the runtime-set timeout args (callback, delay)
87
+ * @property {?function} windowResize - The the runtime-set resize callback
88
+ * @property {?Array} windowTimeout - The the runtime-set timeout args (callback, delay)
89
+ *
90
+ * @ignore
98
91
  */
99
92
  export class MarkdownScriptRuntime {
100
93
  /**
@@ -133,16 +126,19 @@ export class MarkdownScriptRuntime {
133
126
  }
134
127
 
135
128
 
129
+ // Reset the runtime state
136
130
  reset() {
137
131
  this.documentFocus = null;
138
132
  this.documentTitle = null;
139
133
  this.isDocumentReset = false;
140
134
  this.windowLocation = null;
135
+ this.windowResize = null;
141
136
  this.windowTimeout = null;
142
137
  return this.resetElements();
143
138
  }
144
139
 
145
140
 
141
+ // Reset the runtime elements
146
142
  resetElements() {
147
143
  let elements = null;
148
144
  if (this.elements !== null) {
@@ -154,6 +150,7 @@ export class MarkdownScriptRuntime {
154
150
  }
155
151
 
156
152
 
153
+ // Helper method to prepare runtime for adding elements
157
154
  setElements() {
158
155
  if (this.elements === null) {
159
156
  this.elements = [];
@@ -163,13 +160,13 @@ export class MarkdownScriptRuntime {
163
160
  }
164
161
 
165
162
 
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;
163
+ // Helper method to add runtime elements
164
+ addElements(elements) {
165
+ this.elements.push(elements);
170
166
  }
171
167
 
172
168
 
169
+ // Helper method to start an SVG drawing
173
170
  setDrawing(newDrawing = false) {
174
171
  this.setElements();
175
172
  let svg = this.getDrawingSVG();
@@ -188,6 +185,15 @@ export class MarkdownScriptRuntime {
188
185
  }
189
186
 
190
187
 
188
+ // Helper method to get the current SVG drawing element
189
+ getDrawingSVG() {
190
+ const par = (this.elements.length !== 0 ? this.elements[this.elements.length - 1] : null);
191
+ const svg = (par !== null && par.html === 'p' && 'elem' in par ? par.elem : null);
192
+ return svg !== null && svg.svg === 'svg' ? svg : null;
193
+ }
194
+
195
+
196
+ // Helper method to start an SVG drawing path
191
197
  setDrawingPath() {
192
198
  if (this.drawingPath === null) {
193
199
  const svg = this.setDrawing();
@@ -206,6 +212,7 @@ export class MarkdownScriptRuntime {
206
212
  }
207
213
 
208
214
 
215
+ // Helper method to finish the current SVG drawing path
209
216
  finishDrawingPath() {
210
217
  if (this.drawingPath !== null) {
211
218
  const svg = this.getDrawingSVG();
@@ -217,6 +224,7 @@ export class MarkdownScriptRuntime {
217
224
  }
218
225
 
219
226
 
227
+ // Helper method to start a Markdown block
220
228
  setMarkdown() {
221
229
  if (this.markdown === null) {
222
230
  this.setElements();
@@ -225,316 +233,18 @@ export class MarkdownScriptRuntime {
225
233
  }
226
234
 
227
235
 
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;
233
- }
234
- }
235
-
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();
290
- this.drawingPath.push(
291
- `A ${rx.toFixed(svgPrecision)} ${ry.toFixed(svgPrecision)} ${angle.toFixed(svgPrecision)} ` +
292
- `${largeArcFlag ? 1 : 0} ${sweepFlag ? 1 : 0} ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`
293
- );
294
- }
295
-
296
-
297
- drawCircle(cx, cy, r) {
298
- const svg = this.setDrawing();
299
- svg.elem.push({
300
- 'svg': 'circle',
301
- 'attr': {
302
- 'fill': this.drawingPathFill,
303
- 'stroke': this.drawingPathStroke,
304
- 'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
305
- 'stroke-dasharray': this.drawingPathStrokeDashArray,
306
- 'cx': cx,
307
- 'cy': cy,
308
- 'r': r
309
- }
310
- });
311
- }
312
-
313
-
314
- drawClose() {
315
- this.setDrawingPath();
316
- this.drawingPath.push('Z');
317
- }
318
-
319
-
320
- drawEllipse(cx, cy, rx, ry) {
321
- const svg = this.setDrawing();
322
- svg.elem.push({
323
- 'svg': 'ellipse',
324
- 'attr': {
325
- 'fill': this.drawingPathFill,
326
- 'stroke': this.drawingPathStroke,
327
- 'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
328
- 'stroke-dasharray': this.drawingPathStrokeDashArray,
329
- 'cx': cx,
330
- 'cy': cy,
331
- 'rx': rx,
332
- 'ry': ry
333
- }
334
- });
335
- }
336
-
337
-
338
- drawHLine(x) {
339
- this.setDrawingPath();
340
- this.drawingPath.push(`H ${x.toFixed(svgPrecision)}`);
341
- }
342
-
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
- });
356
- }
357
-
358
-
359
- drawLine(x, y) {
360
- this.setDrawingPath();
361
- this.drawingPath.push(`L ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
362
- }
363
-
364
-
365
- drawMove(x, y) {
366
- this.setDrawingPath();
367
- this.drawingPath.push(`M ${x.toFixed(svgPrecision)} ${y.toFixed(svgPrecision)}`);
368
- }
369
-
370
-
371
- drawOnClick(callback) {
372
- const svg = this.setDrawing();
373
- const clickElement = svg.elem.length === 0 ? svg : svg.elem[svg.elem.length - 1];
374
- clickElement.callback = (element) => {
375
- element.addEventListener('click', (event) => {
376
- const boundingRect = event.target.ownerSVGElement.getBoundingClientRect();
377
- this.options.statementCount = 0;
378
- callback([event.clientX - boundingRect.left, event.clientY - boundingRect.top], this.options);
379
- this.options.runtimeUpdateFn();
380
- });
381
- };
382
- }
383
-
384
-
385
- drawRect(x, y, width, height, rx = null, ry = null) {
386
- const svg = this.setDrawing();
387
- const element = {
388
- 'svg': 'rect',
389
- 'attr': {
390
- 'fill': this.drawingPathFill,
391
- 'stroke': this.drawingPathStroke,
392
- 'stroke-width': this.drawingPathStrokeWidth.toFixed(svgPrecision),
393
- 'stroke-dasharray': this.drawingPathStrokeDashArray,
394
- 'x': x,
395
- 'y': y,
396
- 'width': width,
397
- 'height': height
398
- }
399
- };
400
- if (rx !== null) {
401
- element.attr.rx = rx;
402
- }
403
- if (ry !== null) {
404
- element.attr.ry = ry;
405
- }
406
- svg.elem.push(element);
407
- }
408
-
409
-
410
- drawStyle(stroke = 'black', strokeWidth = 1, fill = 'none', strokeDashArray = 'none') {
411
- if (stroke !== this.drawingPathStroke || strokeWidth !== this.drawingPathStrokeWidth ||
412
- strokeDashArray !== this.drawingPathStrokeDashArray || fill !== this.drawingPathFill
413
- ) {
414
- this.setDrawing();
415
- this.drawingPathStroke = stroke;
416
- this.drawingPathStrokeWidth = strokeWidth;
417
- this.drawingPathStrokeDashArray = strokeDashArray;
418
- this.drawingPathFill = fill;
419
- }
420
- }
421
-
422
-
423
- drawText(text, x, y, textAnchor = 'middle', dominantBaseline = 'middle') {
424
- const svg = this.setDrawing();
425
- svg.elem.push({
426
- 'svg': 'text',
427
- 'attr': {
428
- 'fill': this.drawingFontFill,
429
- 'font-family': this.drawingFontFamily,
430
- 'font-size': this.drawingFontSizePx.toFixed(svgPrecision),
431
- 'text-anchor': textAnchor,
432
- 'dominant-baseline': dominantBaseline,
433
- 'font-weight': (this.drawingFontBold ? 'bold' : 'normal'),
434
- 'font-style': (this.drawingFontItalic ? 'italic' : 'normal'),
435
- 'x': x,
436
- 'y': y
437
- },
438
- 'elem': {'text': text}
439
- });
440
- }
441
-
442
-
443
- drawTextStyle(fontSizePx = null, textFill = 'black', bold = false, italic = false, fontFamily = null) {
444
- this.drawingFontSizePx = (fontSizePx !== null ? fontSizePx : this.options.fontSize * pixelsPerPoint);
445
- this.drawingFontFill = textFill;
446
- this.drawingFontBold = bold;
447
- this.drawingFontItalic = italic;
448
- this.drawingFontFamily = (fontFamily !== null ? fontFamily : defaultFontFamily);
449
- }
450
-
451
-
452
- drawVLine(y) {
453
- this.setDrawingPath();
454
- this.drawingPath.push(`V ${y.toFixed(svgPrecision)}`);
455
- }
456
-
457
-
458
- setDrawingSize(width, height) {
459
- this.drawingWidth = width;
460
- this.drawingHeight = height;
461
- this.setDrawing(true);
462
- }
463
-
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));
474
- }
475
-
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
- }
512
- }
513
-
514
-
515
- //
516
- // Markdown functions
517
- //
518
-
519
-
520
- markdownPrint(lines) {
521
- this.setMarkdown();
236
+ // Helper method to flatten and add Markdown text lines
237
+ addMarkdown(lines) {
522
238
  this.markdown.push(...lines.flat());
523
239
  }
524
240
 
525
241
 
526
- //
527
- // Schema functions
528
- //
529
-
530
-
531
- schemaPrint(types, typeName, actionURLs = null) {
532
- const params = encodeQueryString(this.options.params);
533
- const options = {params};
534
- if (actionURLs !== null) {
535
- options.actionURLs = actionURLs;
242
+ // Helper method to finish the current Markdown block and add its elements
243
+ finishMarkdown() {
244
+ if (this.markdown !== null) {
245
+ const markdownOptions = {'headerIds': true, 'urlFn': this.options.urlFn};
246
+ this.addElements(markdownElements(parseMarkdown(this.markdown), markdownOptions));
247
+ this.markdown = null;
536
248
  }
537
- this.setElements();
538
- this.elements.push(schemaMarkdownDoc(types, typeName, options));
539
249
  }
540
250
  }