markdown-up 4.1.0 → 4.2.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/app.js CHANGED
@@ -117,6 +117,56 @@ export class MarkdownUp {
117
117
  this.runtimeWindowResize = null;
118
118
  this.runtimeTimeoutId = null;
119
119
  this.runtimeCount = 0;
120
+
121
+ // The shared Web Audio state (for windowPlaySound), created on first use. The noise buffer is
122
+ // a one-second white-noise buffer reused for drum and explosion sounds.
123
+ this.audioContext = null;
124
+ this.audioNoiseBuffer = null;
125
+
126
+ // Track the live keyboard state (for windowKeyState polling). Keys are physical key codes
127
+ // (e.g. "ArrowUp"); "ctrl"/"shift"/"alt"/"meta" hold the latest modifier state.
128
+ this.keyState = {};
129
+ this.window.addEventListener('keydown', (event) => this.updateKeyState(event, true));
130
+ this.window.addEventListener('keyup', (event) => this.updateKeyState(event, false));
131
+ this.window.addEventListener('blur', () => {
132
+ for (const code of Object.keys(this.keyState)) {
133
+ this.keyState[code] = false;
134
+ }
135
+ });
136
+ }
137
+
138
+
139
+ // Record a keyboard key's down/up state along with the current modifier flags
140
+ updateKeyState(event, down) {
141
+ this.keyState[event.code] = down;
142
+ this.keyState.ctrl = event.ctrlKey;
143
+ this.keyState.shift = event.shiftKey;
144
+ this.keyState.alt = event.altKey;
145
+ this.keyState.meta = event.metaKey;
146
+ }
147
+
148
+
149
+ // Get the shared Web Audio context, creating it on first use
150
+ getAudioContext() {
151
+ if (this.audioContext === null) {
152
+ this.audioContext = new this.window.AudioContext();
153
+ }
154
+ return this.audioContext;
155
+ }
156
+
157
+
158
+ // Get the shared one-second white-noise buffer (for windowPlaySound drums), creating it on first use
159
+ getAudioNoiseBuffer() {
160
+ const audioContext = this.getAudioContext();
161
+ if (this.audioNoiseBuffer === null) {
162
+ const {sampleRate} = audioContext;
163
+ this.audioNoiseBuffer = audioContext.createBuffer(1, sampleRate, sampleRate);
164
+ const data = this.audioNoiseBuffer.getChannelData(0);
165
+ for (let ix = 0; ix < data.length; ix++) {
166
+ data[ix] = Math.random() * 2 - 1;
167
+ }
168
+ }
169
+ return this.audioNoiseBuffer;
120
170
  }
121
171
 
122
172
 
@@ -464,10 +514,13 @@ export class MarkdownUp {
464
514
  this.window.console.log(text);
465
515
  };
466
516
  const scriptOptions = {
517
+ 'audioContextFn': () => this.getAudioContext(),
518
+ 'audioNoiseBufferFn': () => this.getAudioNoiseBuffer(),
467
519
  'debug': 'debug' in this.paramsSession,
468
520
  // eslint-disable-next-line require-await
469
521
  'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
470
522
  'fontSize': this.paramsLocal.fontSize ?? this.fontSize,
523
+ 'keyStateFn': () => this.keyState,
471
524
  logFn,
472
525
  markdownOptions,
473
526
  'params': this.params,
@@ -183,6 +183,7 @@ const documentURLArgs = valueArgsModel([
183
183
  // $doc:
184
184
  // $doc: **Note:** Element model "callback" members are a map of event name (e.g., "click") to
185
185
  // $doc: event callback function. The following events have callback arguments:
186
+ // $doc: - **click** - For an SVG element, the click's x and y coordinates and the SVG's width and height
186
187
  // $doc: - **keydown** - keyCode
187
188
  // $doc: - **keypress** - keyCode
188
189
  // $doc: - **keyup** - keyCode
@@ -425,6 +426,270 @@ function windowHeight(unusedArgs, options) {
425
426
  }
426
427
 
427
428
 
429
+ // $function: windowKeyState
430
+ // $group: window
431
+ // $doc: Test whether a key combination is currently held down. Unlike `documentSetKeyDown`, which
432
+ // $doc: fires a callback once per key press, this polls the live keyboard state and is intended for
433
+ // $doc: game loops and animations. The modifier-key states must match exactly, so by default the key
434
+ // $doc: must be pressed with no modifier keys held. For example:
435
+ // $doc:
436
+ // $doc: ```barescript
437
+ // $doc: if windowKeyState('ArrowLeft'):
438
+ // $doc: playerX = playerX - playerSpeed
439
+ // $doc: endif
440
+ // $doc: if windowKeyState('ArrowRight'):
441
+ // $doc: playerX = playerX + playerSpeed
442
+ // $doc: endif
443
+ // $doc: ```
444
+ // $doc:
445
+ // $doc: The key is matched against the physical key code (e.g., "ArrowUp", "KeyW", "Space", "Enter").
446
+ // $arg key: The physical key code (e.g., "ArrowUp", "ArrowDown", "KeyA", "KeyW", "Space")
447
+ // $arg ctrl: If true, the control key must be down; if false (the default), it must be up
448
+ // $arg shift: If true, the shift key must be down; if false (the default), it must be up
449
+ // $arg alt: If true, the alt key must be down; if false (the default), it must be up
450
+ // $arg meta: If true, the meta (command) key must be down; if false (the default), it must be up
451
+ // $return: true if the key is down and all modifier-key states match, false otherwise
452
+ function windowKeyState(args, options) {
453
+ const [key, ctrl, shift, alt, meta] = valueArgsValidate(windowKeyStateArgs, args);
454
+ const keyState = options.keyStateFn();
455
+ return !!keyState[key] &&
456
+ !ctrl === !keyState.ctrl && !shift === !keyState.shift && !alt === !keyState.alt && !meta === !keyState.meta;
457
+ }
458
+
459
+ const windowKeyStateArgs = valueArgsModel([
460
+ {'name': 'key', 'type': 'string'},
461
+ {'name': 'ctrl', 'type': 'boolean'},
462
+ {'name': 'shift', 'type': 'boolean'},
463
+ {'name': 'alt', 'type': 'boolean'},
464
+ {'name': 'meta', 'type': 'boolean'}
465
+ ]);
466
+
467
+
468
+ // $function: windowPlaySound
469
+ // $group: window
470
+ // $doc: Play a generated sound effect. Unknown sounds are ignored. The sound name is one of:
471
+ // $doc:
472
+ // $doc: - **UI** - "beep", "click", "error", "success", "warning"
473
+ // $doc: - **Arcade** - "coin", "jump", "laser", "explosion", "powerup", "powerdown", "hit", "blip", "gameover"
474
+ // $doc: - **Notes** - "noteC4", "noteCs4", "noteD4", "noteDs4", "noteE4", "noteF4", "noteFs4", "noteG4",
475
+ // $doc: "noteGs4", "noteA4", "noteAs4", "noteB4", "noteC5"
476
+ // $doc: - **Drums** - "drumKick", "drumSnare", "drumHihat", "drumOpenhat", "drumTomLow", "drumTomMid",
477
+ // $doc: "drumTomHigh", "drumClap", "drumCrash", "drumRide"
478
+ // $arg sound: The sound name
479
+ function windowPlaySound(args, options) {
480
+ const [sound] = valueArgsValidate(windowPlaySoundArgs, args);
481
+
482
+ // Unknown sound? Debug-log and do nothing else.
483
+ const tones = windowPlaySoundTones[sound] ?? null;
484
+ if (tones === null) {
485
+ if (options.debug) {
486
+ options.logFn(`MarkdownUp: Unknown windowPlaySound sound "${sound}"`);
487
+ }
488
+ return;
489
+ }
490
+
491
+ // Get the audio context and resume it if suspended (required by most browsers)
492
+ const audioContext = options.audioContextFn();
493
+ if (audioContext.state === 'suspended') {
494
+ audioContext.resume();
495
+ }
496
+
497
+ // Gain envelope: a near-silent floor (exponential ramps cannot reach zero) and a short attack (seconds)
498
+ const silence = 0.0001;
499
+ const attack = 0.005;
500
+
501
+ // Schedule each tone in the sound's sequence
502
+ let startTime = audioContext.currentTime;
503
+ for (const tone of tones) {
504
+ const {type, frequency, duration, volume} = tone;
505
+ const frequencyEnd = tone.frequencyEnd ?? null;
506
+ const filter = tone.filter ?? null;
507
+ startTime += (tone.gap ?? 0) / 1000;
508
+ const stopTime = startTime + duration / 1000;
509
+
510
+ // Create the source node - a white-noise burst (for drums and explosions) or an oscillator tone
511
+ let source;
512
+ if (type === 'noise') {
513
+ source = audioContext.createBufferSource();
514
+ source.buffer = options.audioNoiseBufferFn();
515
+ // Loop the (one-second) noise buffer so tones longer than it still sustain
516
+ source.loop = true;
517
+ } else {
518
+ source = audioContext.createOscillator();
519
+ source.type = type;
520
+ source.frequency.setValueAtTime(frequency, startTime);
521
+ if (frequencyEnd !== null) {
522
+ source.frequency.exponentialRampToValueAtTime(frequencyEnd, stopTime);
523
+ }
524
+ }
525
+
526
+ // Optional band filter (e.g., the high frequencies of a cymbal or the low boom of an explosion)
527
+ let output = source;
528
+ if (filter !== null) {
529
+ const filterNode = audioContext.createBiquadFilter();
530
+ filterNode.type = filter.type;
531
+ filterNode.frequency.value = filter.frequency;
532
+ source.connect(filterNode);
533
+ output = filterNode;
534
+ }
535
+
536
+ // Apply a percussive gain envelope, clamping the attack to the stop time so the ramp events
537
+ // stay ordered even for very short tones
538
+ const gain = audioContext.createGain();
539
+ const attackTime = Math.min(startTime + attack, stopTime);
540
+ gain.gain.setValueAtTime(silence, startTime);
541
+ gain.gain.exponentialRampToValueAtTime(volume, attackTime);
542
+ gain.gain.exponentialRampToValueAtTime(silence, stopTime);
543
+ output.connect(gain);
544
+ gain.connect(audioContext.destination);
545
+
546
+ source.start(startTime);
547
+ source.stop(stopTime);
548
+ startTime = stopTime;
549
+ }
550
+ }
551
+
552
+ const windowPlaySoundArgs = valueArgsModel([
553
+ {'name': 'sound', 'type': 'string'}
554
+ ]);
555
+
556
+
557
+ // The generated sounds, each a sequence of tones. A tone is one of an oscillator ("sine", "square",
558
+ // "triangle", "sawtooth") or a "noise" burst. "frequencyEnd" ramps the oscillator pitch for sweeps,
559
+ // "filter" band-passes the source (e.g. cymbals), and "gap" is the silence, in milliseconds, before it.
560
+ const windowPlaySoundTones = {
561
+ // UI
562
+ 'beep': [
563
+ {'type': 'sine', 'frequency': 880, 'duration': 180, 'volume': 0.3}
564
+ ],
565
+ 'click': [
566
+ {'type': 'square', 'frequency': 1200, 'duration': 40, 'volume': 0.2}
567
+ ],
568
+ 'error': [
569
+ {'type': 'square', 'frequency': 350, 'duration': 250, 'volume': 0.4}
570
+ ],
571
+ 'success': [
572
+ {'type': 'sine', 'frequency': 660, 'duration': 120, 'volume': 0.3},
573
+ {'type': 'sine', 'frequency': 990, 'duration': 200, 'volume': 0.3}
574
+ ],
575
+ 'warning': [
576
+ {'type': 'triangle', 'frequency': 440, 'duration': 160, 'volume': 0.4},
577
+ {'type': 'triangle', 'frequency': 440, 'duration': 160, 'volume': 0.4, 'gap': 80}
578
+ ],
579
+
580
+ // Arcade
581
+ 'coin': [
582
+ {'type': 'square', 'frequency': 988, 'duration': 80, 'volume': 0.3},
583
+ {'type': 'square', 'frequency': 1319, 'duration': 200, 'volume': 0.3}
584
+ ],
585
+ 'jump': [
586
+ {'type': 'square', 'frequency': 300, 'frequencyEnd': 800, 'duration': 150, 'volume': 0.3}
587
+ ],
588
+ 'laser': [
589
+ {'type': 'sawtooth', 'frequency': 1200, 'frequencyEnd': 200, 'duration': 250, 'volume': 0.4}
590
+ ],
591
+ 'explosion': [
592
+ {'type': 'noise', 'duration': 1600, 'volume': 0.9, 'filter': {'type': 'lowpass', 'frequency': 800}}
593
+ ],
594
+ 'powerup': [
595
+ {'type': 'square', 'frequency': 523, 'duration': 80, 'volume': 0.3},
596
+ {'type': 'square', 'frequency': 659, 'duration': 80, 'volume': 0.3},
597
+ {'type': 'square', 'frequency': 784, 'duration': 80, 'volume': 0.3},
598
+ {'type': 'square', 'frequency': 1047, 'duration': 160, 'volume': 0.3}
599
+ ],
600
+ 'powerdown': [
601
+ {'type': 'square', 'frequency': 1047, 'duration': 80, 'volume': 0.3},
602
+ {'type': 'square', 'frequency': 784, 'duration': 80, 'volume': 0.3},
603
+ {'type': 'square', 'frequency': 659, 'duration': 80, 'volume': 0.3},
604
+ {'type': 'square', 'frequency': 523, 'duration': 160, 'volume': 0.3}
605
+ ],
606
+ 'hit': [
607
+ {'type': 'square', 'frequency': 400, 'frequencyEnd': 100, 'duration': 120, 'volume': 0.3}
608
+ ],
609
+ 'blip': [
610
+ {'type': 'square', 'frequency': 1000, 'duration': 40, 'volume': 0.25}
611
+ ],
612
+ 'gameover': [
613
+ {'type': 'square', 'frequency': 392, 'duration': 180, 'volume': 0.3},
614
+ {'type': 'square', 'frequency': 330, 'duration': 180, 'volume': 0.3},
615
+ {'type': 'square', 'frequency': 262, 'duration': 320, 'volume': 0.3}
616
+ ],
617
+
618
+ // Musical notes (one chromatic octave, C4 - C5, equal temperament)
619
+ 'noteC4': [
620
+ {'type': 'triangle', 'frequency': 261.63, 'duration': 400, 'volume': 0.3}
621
+ ],
622
+ 'noteCs4': [
623
+ {'type': 'triangle', 'frequency': 277.18, 'duration': 400, 'volume': 0.3}
624
+ ],
625
+ 'noteD4': [
626
+ {'type': 'triangle', 'frequency': 293.66, 'duration': 400, 'volume': 0.3}
627
+ ],
628
+ 'noteDs4': [
629
+ {'type': 'triangle', 'frequency': 311.13, 'duration': 400, 'volume': 0.3}
630
+ ],
631
+ 'noteE4': [
632
+ {'type': 'triangle', 'frequency': 329.63, 'duration': 400, 'volume': 0.3}
633
+ ],
634
+ 'noteF4': [
635
+ {'type': 'triangle', 'frequency': 349.23, 'duration': 400, 'volume': 0.3}
636
+ ],
637
+ 'noteFs4': [
638
+ {'type': 'triangle', 'frequency': 369.99, 'duration': 400, 'volume': 0.3}
639
+ ],
640
+ 'noteG4': [
641
+ {'type': 'triangle', 'frequency': 392.00, 'duration': 400, 'volume': 0.3}
642
+ ],
643
+ 'noteGs4': [
644
+ {'type': 'triangle', 'frequency': 415.30, 'duration': 400, 'volume': 0.3}
645
+ ],
646
+ 'noteA4': [
647
+ {'type': 'triangle', 'frequency': 440.00, 'duration': 400, 'volume': 0.3}
648
+ ],
649
+ 'noteAs4': [
650
+ {'type': 'triangle', 'frequency': 466.16, 'duration': 400, 'volume': 0.3}
651
+ ],
652
+ 'noteB4': [
653
+ {'type': 'triangle', 'frequency': 493.88, 'duration': 400, 'volume': 0.3}
654
+ ],
655
+ 'noteC5': [
656
+ {'type': 'triangle', 'frequency': 523.25, 'duration': 400, 'volume': 0.3}
657
+ ],
658
+
659
+ // Drum set
660
+ 'drumKick': [
661
+ {'type': 'sine', 'frequency': 150, 'frequencyEnd': 50, 'duration': 180, 'volume': 0.9}
662
+ ],
663
+ 'drumSnare': [
664
+ {'type': 'noise', 'duration': 200, 'volume': 0.6, 'filter': {'type': 'highpass', 'frequency': 1500}}
665
+ ],
666
+ 'drumHihat': [
667
+ {'type': 'noise', 'duration': 50, 'volume': 0.4, 'filter': {'type': 'highpass', 'frequency': 7000}}
668
+ ],
669
+ 'drumOpenhat': [
670
+ {'type': 'noise', 'duration': 300, 'volume': 0.4, 'filter': {'type': 'highpass', 'frequency': 7000}}
671
+ ],
672
+ 'drumTomLow': [
673
+ {'type': 'sine', 'frequency': 160, 'frequencyEnd': 80, 'duration': 260, 'volume': 0.7}
674
+ ],
675
+ 'drumTomMid': [
676
+ {'type': 'sine', 'frequency': 220, 'frequencyEnd': 110, 'duration': 220, 'volume': 0.7}
677
+ ],
678
+ 'drumTomHigh': [
679
+ {'type': 'sine', 'frequency': 300, 'frequencyEnd': 150, 'duration': 200, 'volume': 0.7}
680
+ ],
681
+ 'drumClap': [
682
+ {'type': 'noise', 'duration': 120, 'volume': 0.5, 'filter': {'type': 'highpass', 'frequency': 1000}}
683
+ ],
684
+ 'drumCrash': [
685
+ {'type': 'noise', 'duration': 800, 'volume': 0.5, 'filter': {'type': 'highpass', 'frequency': 5000}}
686
+ ],
687
+ 'drumRide': [
688
+ {'type': 'noise', 'duration': 500, 'volume': 0.35, 'filter': {'type': 'highpass', 'frequency': 8000}}
689
+ ]
690
+ };
691
+
692
+
428
693
  // $function: windowSetLocation
429
694
  // $group: window
430
695
  // $doc: Navigate the browser window to a location URL
@@ -547,6 +812,8 @@ export const markdownScriptFunctions = {
547
812
  windowClipboardRead,
548
813
  windowClipboardWrite,
549
814
  windowHeight,
815
+ windowKeyState,
816
+ windowPlaySound,
550
817
  windowSetLocation,
551
818
  windowSetResize,
552
819
  windowSetTimeout,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "4.1.0",
4
+ "version": "4.2.0",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -24,13 +24,13 @@
24
24
  "dependencies": {
25
25
  "bare-script": "~4.2",
26
26
  "element-model": "~1.2",
27
- "schema-markdown": "~1.2"
27
+ "schema-markdown": "~1.3"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@eslint/js": "~10.0",
31
31
  "c8": "~11.0",
32
- "eslint": "~10.2",
33
- "globals": "~17.5",
32
+ "eslint": "~10.6",
33
+ "globals": "~17.7",
34
34
  "jsdoc": "~4.0",
35
35
  "jsdom": "~29.1"
36
36
  }