markdown-up 5.2.2 → 5.3.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.
- package/README.md +20 -1
- package/lib/app.js +412 -177
- package/lib/appImports.js +4 -0
- package/lib/scriptLibrary.js +151 -0
- package/lib/voice.js +825 -0
- package/package.json +4 -3
package/lib/app.js
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
/** @module lib/app */
|
|
5
5
|
|
|
6
6
|
import {MarkdownScriptRuntime, markdownScriptCodeBlock} from './script.js';
|
|
7
|
+
import {MarkdownUpVoice, voiceElementsParagraphs} from './voice.js';
|
|
7
8
|
import {barescriptParseExpression, evaluateExpression} from 'bare-script/lib/runtime.js';
|
|
8
9
|
import {elementModelWrapCallbacks, markdownScriptFunctions} from './scriptLibrary.js';
|
|
9
10
|
import {
|
|
10
|
-
markdownElementsAsync, markdownParse, markdownTitle, schemaParse, schemaValidate, urlDecodeQueryString,
|
|
11
|
+
markdownElements, markdownElementsAsync, markdownParse, markdownTitle, schemaParse, schemaValidate, urlDecodeQueryString,
|
|
12
|
+
urlEncodeQueryString
|
|
11
13
|
} from 'bare-script/lib/include.js';
|
|
12
14
|
import {renderElements} from 'element-model/lib/elementModel.js';
|
|
13
15
|
import {valueJSON} from 'bare-script/lib/value.js';
|
|
@@ -40,6 +42,12 @@ struct MarkdownUpLocal
|
|
|
40
42
|
# The line height
|
|
41
43
|
optional float(>= 1, <= 10) lineHeight
|
|
42
44
|
|
|
45
|
+
# The voice playback voice
|
|
46
|
+
optional string(len > 0) voice
|
|
47
|
+
|
|
48
|
+
# The voice playback speaking rate
|
|
49
|
+
optional float(>= 0.5, <= 2) voiceRate
|
|
50
|
+
|
|
43
51
|
|
|
44
52
|
# The MarkdownUp session storage JSON schema
|
|
45
53
|
struct MarkdownUpSession
|
|
@@ -84,6 +92,38 @@ enum MarkdownUpView
|
|
|
84
92
|
const defaultHelpURL = 'https://craigahobbs.github.io/markdown-up-py/';
|
|
85
93
|
|
|
86
94
|
|
|
95
|
+
// The menu's font sizes (pt) and line heights (em)
|
|
96
|
+
const menuFontSizes = [8, 10, 12, 14, 18, 24];
|
|
97
|
+
const menuLineHeights = [1.2, 1.3, 1.5, 1.7, 2];
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
// The ID of the element the voice playback bar is rendered in, if the page has one - otherwise, the bar is rendered in
|
|
101
|
+
// its own element (with the default ID) at the bottom of the window
|
|
102
|
+
const voiceBarId = 'markdownUpVoice';
|
|
103
|
+
const voiceBarDefaultId = 'markdownUpVoiceDefault';
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
// The menu and voice playback bar icon paths (24 x 24)
|
|
107
|
+
const icons = {
|
|
108
|
+
'burger': 'M3,5 L21,5 M3,12 L21,12 M3,19 L21,19',
|
|
109
|
+
'close': 'M7,7 L17,17 M17,7 L7,17',
|
|
110
|
+
'darkMode': 'M16,3 A10,10,0,1,1,3,18 A14,14,0,0,0,17,3 Z',
|
|
111
|
+
'debug': 'M12,5 A4,7,0,1,0,12,19 A4,7,0,1,0,12,5 M9,9 L15,9 M9,9 L4,6 M9,12 L3,12 M9,15 L4,18 ' +
|
|
112
|
+
'M15,9 L20,6 M15,12 L21,12 M15,15 L20,18',
|
|
113
|
+
'fontSize': 'M4,21 L12,3 L20,21 M7.5,14 L16.5,14',
|
|
114
|
+
'help': 'M12,2 A10,10,0,1,0,12,22 A10,10,0,1,0,12,2 M9,9.5 A3,3,0,1,1,12,12.5 L12,14.5 M12,17.5 L12,18',
|
|
115
|
+
'lineHeight': 'M11,5 L21,5 M11,12 L21,12 M11,19 L21,19 M5,4 L5,20 M2.5,6.5 L5,4 L7.5,6.5 M2.5,17.5 L5,20 L7.5,17.5',
|
|
116
|
+
'link': 'M9,5 L19,5 L19,15 M19,5 L6,18',
|
|
117
|
+
'markdown': 'M5,2 L19,2 L19,22 L5,22 Z M8,7.5 L16,7.5 M8,12 L16,12 M8,16.5 L16,16.5',
|
|
118
|
+
'pause': 'M8,6 H11 V18 H8 Z M13,6 H16 V18 H13 Z',
|
|
119
|
+
'play': 'M9,6 L18,12 L9,18 Z',
|
|
120
|
+
'readAloud': 'M3,9 L7,9 L12,5 L12,19 L7,15 L3,15 Z M16,9 A4,4,0,0,1,16,15 M18.5,6 A8,8,0,0,1,18.5,18',
|
|
121
|
+
'rewind': 'M5,6 H8 V18 H5 Z M19,6 L10,12 L19,18 Z',
|
|
122
|
+
'skipBack': 'M12,6 L3,12 L12,18 Z M21,6 L12,12 L21,18 Z',
|
|
123
|
+
'skipForward': 'M3,6 L12,12 L3,18 Z M12,6 L21,12 L12,18 Z'
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
|
|
87
127
|
/**
|
|
88
128
|
* The MarkdownUp application
|
|
89
129
|
*/
|
|
@@ -115,6 +155,18 @@ export class MarkdownUp {
|
|
|
115
155
|
this.runtimeTimeoutId = null;
|
|
116
156
|
this.runtimeCount = 0;
|
|
117
157
|
|
|
158
|
+
// The page's content element model, as last rendered (for reading the page aloud)
|
|
159
|
+
this.pageElements = null;
|
|
160
|
+
|
|
161
|
+
// The voice playback. It's either the page read aloud (the menu's "Read aloud") or a script's (voiceSpeak) -
|
|
162
|
+
// voiceScript is the script's Markdown text and final arguments.
|
|
163
|
+
this.voice = new MarkdownUpVoice(window, () => this.renderVoice(true));
|
|
164
|
+
this.voiceReadAloud = false;
|
|
165
|
+
this.voiceScript = null;
|
|
166
|
+
|
|
167
|
+
// The voice playback bar's own element, if any (the page has no voice playback bar element)
|
|
168
|
+
this.voiceBar = null;
|
|
169
|
+
|
|
118
170
|
// The shared Web Audio state (for windowPlaySound), created on first use. The noise buffer is
|
|
119
171
|
// a one-second white-noise buffer reused for drum and explosion sounds.
|
|
120
172
|
this.audioContext = null;
|
|
@@ -130,6 +182,13 @@ export class MarkdownUp {
|
|
|
130
182
|
this.keyState[code] = false;
|
|
131
183
|
}
|
|
132
184
|
});
|
|
185
|
+
|
|
186
|
+
// The escape key closes the menu
|
|
187
|
+
this.window.addEventListener('keydown', (event) => {
|
|
188
|
+
if (event.key === 'Escape' && this.menu && this.paramsSession !== null && 'menu' in this.paramsSession) {
|
|
189
|
+
this.menuSessionToggle('menu');
|
|
190
|
+
}
|
|
191
|
+
});
|
|
133
192
|
}
|
|
134
193
|
|
|
135
194
|
|
|
@@ -180,6 +239,7 @@ export class MarkdownUp {
|
|
|
180
239
|
// Parse the hash parameters and render the application element model
|
|
181
240
|
let result;
|
|
182
241
|
let isError = false;
|
|
242
|
+
const urlPrev = this.params !== null ? (this.params.url ?? null) : null;
|
|
183
243
|
try {
|
|
184
244
|
// Validate hash parameters
|
|
185
245
|
const paramsPrev = this.params;
|
|
@@ -197,6 +257,14 @@ export class MarkdownUp {
|
|
|
197
257
|
isError = true;
|
|
198
258
|
}
|
|
199
259
|
|
|
260
|
+
// A new page ends the reading of the page aloud, a new document ends a script's voice playback, and rendering
|
|
261
|
+
// the page anew ends its voice lines
|
|
262
|
+
const url = this.params !== null ? (this.params.url ?? null) : null;
|
|
263
|
+
if (!forceRender && (this.voiceReadAloud || url !== urlPrev)) {
|
|
264
|
+
this.voice.close();
|
|
265
|
+
}
|
|
266
|
+
this.voice.sayStop();
|
|
267
|
+
|
|
200
268
|
// Set the colors
|
|
201
269
|
const isDarkMode = this.paramsLocal.darkMode ?? this.darkMode;
|
|
202
270
|
this.window.document.documentElement.style.colorScheme = isDarkMode ? 'dark' : 'light';
|
|
@@ -212,6 +280,7 @@ export class MarkdownUp {
|
|
|
212
280
|
// Call the application main and validate the result
|
|
213
281
|
this.runtimeCount += 1;
|
|
214
282
|
this.clearRuntimeCallbacks();
|
|
283
|
+
this.pageElements = null;
|
|
215
284
|
if (!isError) {
|
|
216
285
|
result = await this.main();
|
|
217
286
|
}
|
|
@@ -233,10 +302,9 @@ export class MarkdownUp {
|
|
|
233
302
|
this.window.addEventListener('resize', result.resize);
|
|
234
303
|
}
|
|
235
304
|
|
|
236
|
-
// Set the document keydown handler?
|
|
305
|
+
// Set the document keydown handler? (clearRuntimeCallbacks removes it)
|
|
237
306
|
if ('keyDown' in result) {
|
|
238
|
-
this.runtimeDocumentKeyDown =
|
|
239
|
-
this.window.document.addEventListener('keydown', async (event) => {
|
|
307
|
+
this.runtimeDocumentKeyDown = async (event) => {
|
|
240
308
|
// Is the active element something that wants this event?
|
|
241
309
|
const {activeElement} = this.window.document;
|
|
242
310
|
if (activeElement instanceof this.window.HTMLElement &&
|
|
@@ -254,11 +322,13 @@ export class MarkdownUp {
|
|
|
254
322
|
|
|
255
323
|
// Call the document keydown handler
|
|
256
324
|
await result.keyDown(event);
|
|
257
|
-
}
|
|
325
|
+
};
|
|
326
|
+
this.window.document.addEventListener('keydown', this.runtimeDocumentKeyDown);
|
|
258
327
|
}
|
|
259
328
|
|
|
260
329
|
// Render the element model
|
|
261
|
-
|
|
330
|
+
this.renderPage(result.elements);
|
|
331
|
+
this.renderVoice();
|
|
262
332
|
|
|
263
333
|
// Navigate?
|
|
264
334
|
// Note: This is done after render since it may have no effect (in which case we need to render)
|
|
@@ -415,6 +485,7 @@ export class MarkdownUp {
|
|
|
415
485
|
|
|
416
486
|
// Display the Markdown?
|
|
417
487
|
if (this.params.view === 'markdown') {
|
|
488
|
+
this.pageElements = markdownElements(markdownModel);
|
|
418
489
|
return {
|
|
419
490
|
'title': title,
|
|
420
491
|
'elements': [
|
|
@@ -432,6 +503,7 @@ export class MarkdownUp {
|
|
|
432
503
|
// Render the Markdown
|
|
433
504
|
const contentElements = await markdownElementsAsync(markdownModel, scriptMarkdownOptions);
|
|
434
505
|
elementModelWrapCallbacks(contentElements, scriptOptions);
|
|
506
|
+
this.pageElements = contentElements;
|
|
435
507
|
const result = {
|
|
436
508
|
'title': title,
|
|
437
509
|
'elements': [
|
|
@@ -528,6 +600,15 @@ export class MarkdownUp {
|
|
|
528
600
|
markdownOptions,
|
|
529
601
|
'params': this.params,
|
|
530
602
|
'urlFn': (url) => this.modifyURL(url),
|
|
603
|
+
'voice': {
|
|
604
|
+
'close': () => this.voiceScriptClose(),
|
|
605
|
+
'load': () => this.voice.prepare(),
|
|
606
|
+
'ready': () => this.voice.ready(),
|
|
607
|
+
'say': (text, voice, style, rate) => this.voice.say(text, voice, style, rate),
|
|
608
|
+
'setText': (text, final) => this.voiceScriptSetText(text, final),
|
|
609
|
+
'speak': (text, final, key) => this.voiceScriptSpeak(text, final, key),
|
|
610
|
+
'state': () => this.voiceScriptState()
|
|
611
|
+
},
|
|
531
612
|
'window': this.window
|
|
532
613
|
};
|
|
533
614
|
|
|
@@ -577,13 +658,16 @@ export class MarkdownUp {
|
|
|
577
658
|
if (elements !== null) {
|
|
578
659
|
const {body} = this.window.document;
|
|
579
660
|
if (runtime.documentReset === null) {
|
|
580
|
-
|
|
661
|
+
this.renderPage([this.burgerElements(), elements]);
|
|
662
|
+
this.pageElements = elements;
|
|
581
663
|
} else {
|
|
582
|
-
while (body.lastChild !== null && body.lastChild.id !== runtime.documentReset) {
|
|
664
|
+
while (body.lastChild !== null && body.lastChild !== this.voiceBar && body.lastChild.id !== runtime.documentReset) {
|
|
583
665
|
body.lastChild.remove();
|
|
584
666
|
}
|
|
585
667
|
renderElements(body, [elements], false);
|
|
668
|
+
this.pageElements = [pageElementsReset(this.pageElements, runtime.documentReset), elements];
|
|
586
669
|
}
|
|
670
|
+
this.renderVoice();
|
|
587
671
|
}
|
|
588
672
|
|
|
589
673
|
// Navigate?
|
|
@@ -634,46 +718,56 @@ export class MarkdownUp {
|
|
|
634
718
|
burgerElements() {
|
|
635
719
|
const paramString = urlEncodeQueryString(this.params);
|
|
636
720
|
const topHeaderId = `${paramString}${paramString !== '' ? '&' : ''}_top`;
|
|
721
|
+
const isMenu = 'menu' in this.paramsSession;
|
|
637
722
|
return [
|
|
638
723
|
!this.menu ? null : [
|
|
724
|
+
// Menu "burger" button
|
|
639
725
|
{
|
|
640
726
|
'html': 'div',
|
|
641
727
|
'attr': {'class': 'menu-burger'},
|
|
642
|
-
'elem':
|
|
643
|
-
'
|
|
644
|
-
'
|
|
645
|
-
'
|
|
646
|
-
|
|
728
|
+
'elem': {
|
|
729
|
+
'html': 'button',
|
|
730
|
+
'attr': {'type': 'button', 'class': 'menu-button', 'aria-label': 'Menu', 'aria-expanded': `${isMenu}`},
|
|
731
|
+
'elem': iconElements(icons.burger, 22),
|
|
732
|
+
'callback': clickCallback(() => this.menuSessionToggle('menu'))
|
|
733
|
+
}
|
|
647
734
|
},
|
|
648
735
|
|
|
649
|
-
// Popup menu
|
|
650
|
-
!
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
this.
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
'
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
736
|
+
// Popup menu - a click outside of it (on its backdrop) closes it
|
|
737
|
+
!isMenu ? null : [
|
|
738
|
+
{
|
|
739
|
+
'html': 'div',
|
|
740
|
+
'attr': {'class': 'menu-backdrop'},
|
|
741
|
+
'callback': clickCallback(() => this.menuSessionToggle('menu'))
|
|
742
|
+
},
|
|
743
|
+
{
|
|
744
|
+
'html': 'div',
|
|
745
|
+
'attr': {'class': 'menu', 'role': 'menu'},
|
|
746
|
+
'elem': [
|
|
747
|
+
menuItemElements('Read aloud', icons.readAloud, null, () => this.menuReadAloud()),
|
|
748
|
+
menuSeparatorElements(),
|
|
749
|
+
menuItemElements(
|
|
750
|
+
'Dark mode', icons.darkMode, !!(this.paramsLocal.darkMode ?? this.darkMode), () => this.menuDarkModeToggle()
|
|
751
|
+
),
|
|
752
|
+
this.menuStepperElements('Font size', icons.fontSize, 'fontSize', this.fontSize, menuFontSizes, 'pt'),
|
|
753
|
+
this.menuStepperElements('Line height', icons.lineHeight, 'lineHeight', this.lineHeight, menuLineHeights, ''),
|
|
754
|
+
menuSeparatorElements(),
|
|
755
|
+
menuItemElements(
|
|
756
|
+
'Markdown source', icons.markdown, this.params.view === 'markdown', () => this.menuViewToggle('markdown')
|
|
757
|
+
),
|
|
758
|
+
menuItemElements('Debug', icons.debug, 'debug' in this.paramsSession, () => this.menuSessionToggle('debug')),
|
|
759
|
+
{
|
|
760
|
+
'html': 'a',
|
|
761
|
+
'attr': {'class': 'menu-item', 'role': 'menuitem', 'href': this.helpURL},
|
|
762
|
+
'elem': [
|
|
763
|
+
iconElements(icons.help),
|
|
764
|
+
{'html': 'span', 'attr': {'class': 'menu-label'}, 'elem': {'text': 'Help'}},
|
|
765
|
+
iconElements(icons.link, 14)
|
|
766
|
+
]
|
|
767
|
+
}
|
|
768
|
+
]
|
|
769
|
+
}
|
|
770
|
+
]
|
|
677
771
|
],
|
|
678
772
|
|
|
679
773
|
// The top header ID
|
|
@@ -682,168 +776,309 @@ export class MarkdownUp {
|
|
|
682
776
|
}
|
|
683
777
|
|
|
684
778
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
779
|
+
// Create a menu stepper's element model - a local storage value with buttons to step it down and up through its values
|
|
780
|
+
menuStepperElements(label, icon, valueName, valueDefault, values, unit) {
|
|
781
|
+
const value = this.paramsLocal[valueName] ?? valueDefault;
|
|
782
|
+
const stepElements = (stepLabel, stepText, valueNew) => ({
|
|
783
|
+
'html': 'button',
|
|
784
|
+
'attr': {
|
|
785
|
+
'type': 'button',
|
|
786
|
+
'class': 'menu-step',
|
|
787
|
+
'aria-label': `${label} ${stepLabel}`,
|
|
788
|
+
'disabled': valueNew === null ? 'disabled' : null
|
|
689
789
|
},
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
menuValueToggle(valueName, icon) {
|
|
696
|
-
icon.checked = valueName in this.paramsSession;
|
|
697
|
-
return this.menuButton(
|
|
698
|
-
() => {
|
|
699
|
-
if (valueName in this.paramsSession) {
|
|
700
|
-
delete this.paramsSession[valueName];
|
|
790
|
+
'elem': {'text': stepText},
|
|
791
|
+
'callback': valueNew === null ? null : clickCallback(() => {
|
|
792
|
+
if (valueNew === valueDefault) {
|
|
793
|
+
delete this.paramsLocal[valueName];
|
|
701
794
|
} else {
|
|
702
|
-
this.
|
|
795
|
+
this.paramsLocal[valueName] = valueNew;
|
|
703
796
|
}
|
|
704
|
-
this.window.
|
|
797
|
+
this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
|
|
705
798
|
this.render(true);
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
|
|
799
|
+
})
|
|
800
|
+
});
|
|
801
|
+
return {
|
|
802
|
+
'html': 'div',
|
|
803
|
+
'attr': {'class': 'menu-item menu-stepper'},
|
|
804
|
+
'elem': [
|
|
805
|
+
iconElements(icon),
|
|
806
|
+
{'html': 'span', 'attr': {'class': 'menu-label'}, 'elem': {'text': label}},
|
|
807
|
+
stepElements('down', '−', values.findLast((valueOther) => valueOther < value) ?? null),
|
|
808
|
+
{'html': 'span', 'attr': {'class': 'menu-value'}, 'elem': {'text': `${value}${unit}`}},
|
|
809
|
+
stepElements('up', '+', values.find((valueOther) => valueOther > value) ?? null)
|
|
810
|
+
]
|
|
811
|
+
};
|
|
709
812
|
}
|
|
710
813
|
|
|
711
814
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
815
|
+
// Toggle a session storage value (e.g., the menu) and render
|
|
816
|
+
menuSessionToggle(valueName) {
|
|
817
|
+
if (valueName in this.paramsSession) {
|
|
818
|
+
delete this.paramsSession[valueName];
|
|
819
|
+
} else {
|
|
820
|
+
this.paramsSession[valueName] = 1;
|
|
821
|
+
}
|
|
822
|
+
this.window.sessionStorage.setItem('MarkdownUp', JSON.stringify(this.paramsSession));
|
|
823
|
+
this.render(true);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
// Toggle a view (e.g., the Markdown text view)
|
|
828
|
+
menuViewToggle(viewValue) {
|
|
829
|
+
const params = {...this.params};
|
|
830
|
+
if (params.view === viewValue) {
|
|
831
|
+
delete params.view;
|
|
832
|
+
} else {
|
|
833
|
+
params.view = viewValue;
|
|
834
|
+
}
|
|
835
|
+
this.window.location.href = `#${urlEncodeQueryString(params)}`;
|
|
726
836
|
}
|
|
727
837
|
|
|
728
838
|
|
|
729
|
-
|
|
839
|
+
// Toggle dark mode and render
|
|
840
|
+
menuDarkModeToggle() {
|
|
730
841
|
const isDarkMode = this.paramsLocal.darkMode ?? this.darkMode;
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
|
|
740
|
-
this.render(true);
|
|
741
|
-
},
|
|
742
|
-
icon
|
|
743
|
-
);
|
|
842
|
+
if (isDarkMode !== this.darkMode) {
|
|
843
|
+
delete this.paramsLocal.darkMode;
|
|
844
|
+
} else {
|
|
845
|
+
this.paramsLocal.darkMode = !isDarkMode;
|
|
846
|
+
}
|
|
847
|
+
this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
|
|
848
|
+
this.render(true);
|
|
744
849
|
}
|
|
745
850
|
|
|
746
851
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
if (
|
|
751
|
-
|
|
852
|
+
// Read the page aloud - the page as it's rendered now, if it has anything to read - and close the menu
|
|
853
|
+
menuReadAloud() {
|
|
854
|
+
const paragraphs = voiceElementsParagraphs(this.pageElements, true);
|
|
855
|
+
if (paragraphs.length !== 0) {
|
|
856
|
+
this.voiceReadAloud = true;
|
|
857
|
+
this.voiceScript = null;
|
|
858
|
+
this.voiceSettings();
|
|
859
|
+
this.voice.speak(paragraphs, true, null);
|
|
752
860
|
}
|
|
753
|
-
|
|
754
|
-
icon.text = (textFn !== null ? textFn(valueCurrent) : valueCurrent);
|
|
755
|
-
return this.menuButton(
|
|
756
|
-
() => {
|
|
757
|
-
this.paramsLocal[valueName] = valueNew;
|
|
758
|
-
this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
|
|
759
|
-
this.render(true);
|
|
760
|
-
},
|
|
761
|
-
icon
|
|
762
|
-
);
|
|
861
|
+
this.menuSessionToggle('menu');
|
|
763
862
|
}
|
|
764
863
|
|
|
765
864
|
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
// Compute the text block geometry
|
|
774
|
-
const textStrokeWidth = Math.round(1000 * 0.05 * size) / 1000;
|
|
775
|
-
const textLeft = 0.5 * textStrokeWidth;
|
|
776
|
-
const textRight = 12.5 * textStrokeWidth;
|
|
777
|
-
const textTop = size - 6.5 * textStrokeWidth;
|
|
778
|
-
const textBottom = size - 0.5 * textStrokeWidth;
|
|
779
|
-
const textBoxX = 0.5 * (textLeft + textRight);
|
|
780
|
-
const textBoxY = 0.5 * (textTop + textBottom);
|
|
781
|
-
const textBoxSize = (textBottom - textTop) - 1.75 * textStrokeWidth;
|
|
865
|
+
// Set the voice playback's voice and speaking rate to the preferred voice and speaking rate
|
|
866
|
+
voiceSettings() {
|
|
867
|
+
this.voice.voice = this.paramsLocal.voice ?? null;
|
|
868
|
+
this.voice.rate = this.paramsLocal.voiceRate ?? 1;
|
|
869
|
+
}
|
|
870
|
+
|
|
782
871
|
|
|
872
|
+
// The script library's voiceSpeak - start reading Markdown text aloud
|
|
873
|
+
voiceScriptSpeak(text, final, key) {
|
|
874
|
+
this.voiceReadAloud = false;
|
|
875
|
+
this.voiceScript = [text, final];
|
|
876
|
+
this.voiceSettings();
|
|
877
|
+
this.voice.speak(voiceMarkdownParagraphs(text), final, key);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
// The script library's voiceSetText - set the Markdown text of the script's voice playback, if any
|
|
882
|
+
voiceScriptSetText(text, final) {
|
|
883
|
+
if (this.voiceScriptState() !== null && (text !== this.voiceScript[0] || final !== this.voiceScript[1])) {
|
|
884
|
+
this.voiceScript = [text, final];
|
|
885
|
+
this.voice.setText(voiceMarkdownParagraphs(text), final);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
// The script library's voiceState - the state of the script's voice playback, if any (not the page read aloud)
|
|
891
|
+
voiceScriptState() {
|
|
892
|
+
return this.voiceReadAloud ? null : this.voice.state();
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
// The script library's voiceClose - close the script's voice playback, if any (not the page read aloud), and stop
|
|
897
|
+
// the voice lines
|
|
898
|
+
voiceScriptClose() {
|
|
899
|
+
this.voice.sayStop();
|
|
900
|
+
if (!this.voiceReadAloud) {
|
|
901
|
+
this.voice.close();
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
// Render the page's element model in the document body. The voice playback bar's own element, if any, is left in
|
|
907
|
+
// place (it's the body's first element) - an element that's replaced as it's clicked gets no click, so the bar's
|
|
908
|
+
// controls would fail while an application renders.
|
|
909
|
+
renderPage(elements) {
|
|
910
|
+
const {body} = this.window.document;
|
|
911
|
+
if (this.voiceBar === null) {
|
|
912
|
+
renderElements(body, elements);
|
|
913
|
+
} else {
|
|
914
|
+
while (body.lastChild !== this.voiceBar) {
|
|
915
|
+
body.lastChild.remove();
|
|
916
|
+
}
|
|
917
|
+
renderElements(body, elements, false);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
// Render the voice playback bar, if there's a voice playback - in the page's voice playback bar element, if it has
|
|
923
|
+
// one, or in the bar's own element at the bottom of the window. Unless the playback changed, a rendered bar is left
|
|
924
|
+
// as it is.
|
|
925
|
+
renderVoice(changed = false) {
|
|
926
|
+
const {document} = this.window;
|
|
927
|
+
const isPlayback = this.voice.state() !== null;
|
|
928
|
+
const pageBar = document.getElementById(voiceBarId);
|
|
929
|
+
|
|
930
|
+
// Remove the bar's own element when it isn't needed
|
|
931
|
+
if (this.voiceBar !== null && (pageBar !== null || !isPlayback)) {
|
|
932
|
+
this.voiceBar.remove();
|
|
933
|
+
this.voiceBar = null;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// No playback? Clear the page's voice playback bar element, if any.
|
|
937
|
+
if (!isPlayback) {
|
|
938
|
+
if (pageBar !== null && pageBar.firstChild !== null) {
|
|
939
|
+
pageBar.replaceChildren();
|
|
940
|
+
}
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// Create the bar's own element, if necessary - the body's first element, where rendering the page leaves it
|
|
945
|
+
if (pageBar === null && this.voiceBar === null) {
|
|
946
|
+
this.voiceBar = document.createElement('div');
|
|
947
|
+
this.voiceBar.id = voiceBarDefaultId;
|
|
948
|
+
this.voiceBar.className = 'voice-default';
|
|
949
|
+
document.body.prepend(this.voiceBar);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// Render the bar
|
|
953
|
+
const bar = pageBar ?? this.voiceBar;
|
|
954
|
+
if (changed || bar.firstChild === null) {
|
|
955
|
+
renderElements(bar, this.voiceElements());
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
// Create the voice playback bar's element model
|
|
961
|
+
voiceElements() {
|
|
962
|
+
const {voice} = this;
|
|
963
|
+
const {playing} = voice.state();
|
|
964
|
+
const textButtonElements = (label, text, onClick) => ({
|
|
965
|
+
'html': 'button',
|
|
966
|
+
'attr': {'type': 'button', 'class': 'voice-text', 'aria-label': label},
|
|
967
|
+
'elem': {'text': text},
|
|
968
|
+
'callback': clickCallback(onClick)
|
|
969
|
+
});
|
|
783
970
|
return {
|
|
784
971
|
'html': 'div',
|
|
785
|
-
'attr': {'
|
|
786
|
-
'elem':
|
|
787
|
-
'
|
|
788
|
-
'
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
'
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
}
|
|
811
|
-
},
|
|
812
|
-
(text === null ? null : {
|
|
813
|
-
'svg': 'rect',
|
|
814
|
-
'attr': {
|
|
815
|
-
'fill': background,
|
|
816
|
-
'stroke': stroke,
|
|
817
|
-
'stroke-width': textStrokeWidth,
|
|
818
|
-
'x': textLeft.toFixed(3),
|
|
819
|
-
'y': textTop.toFixed(3),
|
|
820
|
-
'width': (textRight - textLeft).toFixed(3),
|
|
821
|
-
'height': (textBottom - textTop).toFixed(3)
|
|
822
|
-
}
|
|
823
|
-
}),
|
|
824
|
-
(text === null ? null : {
|
|
825
|
-
'svg': 'text',
|
|
826
|
-
'attr': {
|
|
827
|
-
'fill': stroke,
|
|
828
|
-
'x': textBoxX.toFixed(3),
|
|
829
|
-
'y': textBoxY.toFixed(3),
|
|
830
|
-
'text-anchor': 'middle',
|
|
831
|
-
'dominant-baseline': 'middle',
|
|
832
|
-
'font-size': `${textBoxSize.toFixed(3)}px`,
|
|
833
|
-
'font-weight': 'bold'
|
|
834
|
-
},
|
|
835
|
-
'elem': {'text': text}
|
|
836
|
-
})
|
|
837
|
-
]
|
|
838
|
-
},
|
|
839
|
-
'callback': (element) => {
|
|
840
|
-
element.addEventListener('click', onClick);
|
|
841
|
-
}
|
|
972
|
+
'attr': {'class': 'voice', 'role': 'group', 'aria-label': 'Read aloud'},
|
|
973
|
+
'elem': [
|
|
974
|
+
voiceButtonElements('Rewind', icons.rewind, true, () => voice.rewind()),
|
|
975
|
+
voiceButtonElements('Skip back', icons.skipBack, true, () => voice.skipBack()),
|
|
976
|
+
(playing
|
|
977
|
+
? voiceButtonElements('Pause', icons.pause, true, () => voice.pause(), true)
|
|
978
|
+
: voiceButtonElements('Play', icons.play, true, () => voice.play(), true)),
|
|
979
|
+
voiceButtonElements('Skip forward', icons.skipForward, true, () => voice.skipForward()),
|
|
980
|
+
textButtonElements('Voice', voice.voiceLabel(), () => {
|
|
981
|
+
const voiceNext = voice.voiceNext();
|
|
982
|
+
if (voiceNext !== null) {
|
|
983
|
+
this.paramsLocal.voice = voiceNext;
|
|
984
|
+
this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
|
|
985
|
+
voice.setVoice(voiceNext);
|
|
986
|
+
}
|
|
987
|
+
}),
|
|
988
|
+
{'html': 'span', 'attr': {'class': 'voice-separator'}, 'elem': {'text': '|'}},
|
|
989
|
+
textButtonElements('Speaking rate', `${voice.rate}x`, () => {
|
|
990
|
+
const rateNext = voice.rateNext();
|
|
991
|
+
this.paramsLocal.voiceRate = rateNext;
|
|
992
|
+
this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
|
|
993
|
+
voice.setRate(rateNext);
|
|
994
|
+
}),
|
|
995
|
+
voiceButtonElements('Close', icons.close, false, () => voice.close())
|
|
996
|
+
]
|
|
842
997
|
};
|
|
843
998
|
}
|
|
844
999
|
}
|
|
845
1000
|
|
|
846
1001
|
|
|
1002
|
+
// Create an element model callback that adds an element's click event handler
|
|
1003
|
+
function clickCallback(onClick) {
|
|
1004
|
+
return (element) => {
|
|
1005
|
+
element.addEventListener('click', onClick);
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
// Create an icon's element model - an icon is drawn with lines or filled
|
|
1011
|
+
function iconElements(path, size = 18, fill = false) {
|
|
1012
|
+
return {
|
|
1013
|
+
'svg': 'svg',
|
|
1014
|
+
'attr': {'width': size, 'height': size, 'viewBox': '0 0 24 24', 'aria-hidden': 'true'},
|
|
1015
|
+
'elem': {
|
|
1016
|
+
'svg': 'path',
|
|
1017
|
+
'attr': {
|
|
1018
|
+
'fill': fill ? 'currentColor' : 'none',
|
|
1019
|
+
'stroke': fill ? 'none' : 'currentColor',
|
|
1020
|
+
'stroke-width': 2,
|
|
1021
|
+
'stroke-linecap': 'round',
|
|
1022
|
+
'stroke-linejoin': 'round',
|
|
1023
|
+
'd': path
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
// Create a menu item's element model - if checked isn't null, the item is a toggle
|
|
1031
|
+
function menuItemElements(label, icon, checked, onClick) {
|
|
1032
|
+
const isToggle = checked !== null;
|
|
1033
|
+
return {
|
|
1034
|
+
'html': 'button',
|
|
1035
|
+
'attr': {
|
|
1036
|
+
'type': 'button',
|
|
1037
|
+
'class': 'menu-item',
|
|
1038
|
+
'role': isToggle ? 'menuitemcheckbox' : 'menuitem',
|
|
1039
|
+
'aria-checked': isToggle ? `${checked}` : null
|
|
1040
|
+
},
|
|
1041
|
+
'elem': [
|
|
1042
|
+
iconElements(icon),
|
|
1043
|
+
{'html': 'span', 'attr': {'class': 'menu-label'}, 'elem': {'text': label}},
|
|
1044
|
+
!isToggle ? null : {'html': 'span', 'attr': {'class': 'menu-switch'}}
|
|
1045
|
+
],
|
|
1046
|
+
'callback': clickCallback(onClick)
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
// Create a menu separator's element model
|
|
1052
|
+
function menuSeparatorElements() {
|
|
1053
|
+
return {'html': 'div', 'attr': {'class': 'menu-separator', 'role': 'separator'}};
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
// Create a voice playback bar button's element model
|
|
1058
|
+
function voiceButtonElements(label, icon, fill, onClick, isPrimary = false) {
|
|
1059
|
+
return {
|
|
1060
|
+
'html': 'button',
|
|
1061
|
+
'attr': {'type': 'button', 'class': `voice-button${isPrimary ? ' voice-primary' : ''}`, 'aria-label': label},
|
|
1062
|
+
'elem': iconElements(icon, isPrimary ? 22 : 18, fill),
|
|
1063
|
+
'callback': clickCallback(onClick)
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
// Get Markdown text's paragraphs to read aloud
|
|
1069
|
+
function voiceMarkdownParagraphs(text) {
|
|
1070
|
+
return voiceElementsParagraphs(markdownElements(markdownParse(text)));
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
// Get the page elements that remain after a document reset - the top-level elements through the reset ID's element
|
|
1075
|
+
function pageElementsReset(pageElements, resetId) {
|
|
1076
|
+
const elements = [pageElements].flat(Infinity).filter((element) => element !== null);
|
|
1077
|
+
const ixReset = elements.findIndex((element) => (element.attr ?? null) !== null && element.attr.id === resetId);
|
|
1078
|
+
return elements.slice(0, ixReset + 1);
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
|
|
847
1082
|
// Create the main result for a "url" hash parameter resource URL that's not allowed
|
|
848
1083
|
function hashURLError(url) {
|
|
849
1084
|
return {
|