@pretty-chitty/core 2.0.0 → 2.1.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/README.md +8 -0
- package/dist/components/ActionLogDisplay.d.ts.map +1 -1
- package/dist/components/ActionLogDisplay.js +1 -1
- package/dist/components/ActionLogDisplay.js.map +1 -1
- package/dist/components/BottomBarButton.d.ts +3 -1
- package/dist/components/BottomBarButton.d.ts.map +1 -1
- package/dist/components/BottomBarButton.js +3 -2
- package/dist/components/BottomBarButton.js.map +1 -1
- package/dist/components/ContextGalleryDisplay.d.ts.map +1 -1
- package/dist/components/ContextGalleryDisplay.js +1 -1
- package/dist/components/ContextGalleryDisplay.js.map +1 -1
- package/dist/components/DemoWrapper.d.ts +30 -2
- package/dist/components/DemoWrapper.d.ts.map +1 -1
- package/dist/components/DemoWrapper.js +79 -36
- package/dist/components/DemoWrapper.js.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.d.ts.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.js +2 -2
- package/dist/components/FullScreenGalleryDisplay.js.map +1 -1
- package/dist/components/Gallery/BuiltItem.d.ts +6 -2
- package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
- package/dist/components/Gallery/BuiltItem.js +28 -9
- package/dist/components/Gallery/BuiltItem.js.map +1 -1
- package/dist/components/Gallery/GalleryController.d.ts +18 -0
- package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryController.js +67 -0
- package/dist/components/Gallery/GalleryController.js.map +1 -1
- package/dist/components/Gallery/GalleryViewer.d.ts +6 -1
- package/dist/components/Gallery/GalleryViewer.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryViewer.js +48 -10
- package/dist/components/Gallery/GalleryViewer.js.map +1 -1
- package/dist/components/Gallery/LayoutManager.d.ts +6 -0
- package/dist/components/Gallery/LayoutManager.d.ts.map +1 -1
- package/dist/components/Gallery/LayoutManager.js +23 -3
- package/dist/components/Gallery/LayoutManager.js.map +1 -1
- package/dist/components/Gallery/constants.d.ts +2 -0
- package/dist/components/Gallery/constants.d.ts.map +1 -1
- package/dist/components/Gallery/constants.js +2 -0
- package/dist/components/Gallery/constants.js.map +1 -1
- package/dist/components/Gallery/registry.d.ts +34 -0
- package/dist/components/Gallery/registry.d.ts.map +1 -0
- package/dist/components/Gallery/registry.js +17 -0
- package/dist/components/Gallery/registry.js.map +1 -0
- package/dist/components/GameDesigner.d.ts.map +1 -1
- package/dist/components/GameDesigner.js +42 -10
- package/dist/components/GameDesigner.js.map +1 -1
- package/dist/components/InlineGalleryDisplay.d.ts.map +1 -1
- package/dist/components/InlineGalleryDisplay.js +2 -2
- package/dist/components/InlineGalleryDisplay.js.map +1 -1
- package/dist/components/LiveButton.d.ts.map +1 -1
- package/dist/components/LiveButton.js +5 -2
- package/dist/components/LiveButton.js.map +1 -1
- package/dist/components/MatchViewer.d.ts +6 -1
- package/dist/components/MatchViewer.d.ts.map +1 -1
- package/dist/components/MatchViewer.js +9 -3
- package/dist/components/MatchViewer.js.map +1 -1
- package/dist/components/Panel/MultiPanel.d.ts.map +1 -1
- package/dist/components/Panel/MultiPanel.js +18 -0
- package/dist/components/Panel/MultiPanel.js.map +1 -1
- package/dist/components/Panel/PanelContents.d.ts.map +1 -1
- package/dist/components/Panel/PanelContents.js +92 -23
- package/dist/components/Panel/PanelContents.js.map +1 -1
- package/dist/components/Panel/PanelTabStack.d.ts.map +1 -1
- package/dist/components/Panel/PanelTabStack.js +1 -1
- package/dist/components/Panel/PanelTabStack.js.map +1 -1
- package/dist/components/PanelSpark.d.ts.map +1 -1
- package/dist/components/PanelSpark.js +3 -2
- package/dist/components/PanelSpark.js.map +1 -1
- package/dist/components/Playground.d.ts.map +1 -1
- package/dist/components/Playground.js +10 -4
- package/dist/components/Playground.js.map +1 -1
- package/dist/components/PromptControls.d.ts.map +1 -1
- package/dist/components/PromptControls.js +4 -2
- package/dist/components/PromptControls.js.map +1 -1
- package/dist/components/SparkLineChart.d.ts.map +1 -1
- package/dist/components/SparkLineChart.js +22 -26
- package/dist/components/SparkLineChart.js.map +1 -1
- package/dist/components/TimeControlBar.d.ts.map +1 -1
- package/dist/components/TimeControlBar.js +5 -3
- package/dist/components/TimeControlBar.js.map +1 -1
- package/dist/components/TokenizedMarkdown.d.ts +23 -0
- package/dist/components/TokenizedMarkdown.d.ts.map +1 -0
- package/dist/components/TokenizedMarkdown.js +144 -0
- package/dist/components/TokenizedMarkdown.js.map +1 -0
- package/dist/components/TokenizedMessage.d.ts +6 -0
- package/dist/components/TokenizedMessage.d.ts.map +1 -1
- package/dist/components/TokenizedMessage.js +20 -19
- package/dist/components/TokenizedMessage.js.map +1 -1
- package/dist/components/TopBarPlayers.d.ts.map +1 -1
- package/dist/components/TopBarPlayers.js +1 -1
- package/dist/components/TopBarPlayers.js.map +1 -1
- package/dist/components/Tutorial/BrowserNarration.d.ts +55 -0
- package/dist/components/Tutorial/BrowserNarration.d.ts.map +1 -0
- package/dist/components/Tutorial/BrowserNarration.js +240 -0
- package/dist/components/Tutorial/BrowserNarration.js.map +1 -0
- package/dist/components/Tutorial/TutorialBox.d.ts +26 -0
- package/dist/components/Tutorial/TutorialBox.d.ts.map +1 -0
- package/dist/components/Tutorial/TutorialBox.js +239 -0
- package/dist/components/Tutorial/TutorialBox.js.map +1 -0
- package/dist/components/Tutorial/TutorialDevPanel.d.ts +9 -0
- package/dist/components/Tutorial/TutorialDevPanel.d.ts.map +1 -0
- package/dist/components/Tutorial/TutorialDevPanel.js +92 -0
- package/dist/components/Tutorial/TutorialDevPanel.js.map +1 -0
- package/dist/components/Tutorial/TutorialOverlay.d.ts +7 -0
- package/dist/components/Tutorial/TutorialOverlay.d.ts.map +1 -0
- package/dist/components/Tutorial/TutorialOverlay.js +1132 -0
- package/dist/components/Tutorial/TutorialOverlay.js.map +1 -0
- package/dist/components/Tutorial/targetRects.d.ts +67 -0
- package/dist/components/Tutorial/targetRects.d.ts.map +1 -0
- package/dist/components/Tutorial/targetRects.js +241 -0
- package/dist/components/Tutorial/targetRects.js.map +1 -0
- package/dist/game/Chit.d.ts +7 -0
- package/dist/game/Chit.d.ts.map +1 -1
- package/dist/game/Chit.js +20 -5
- package/dist/game/Chit.js.map +1 -1
- package/dist/game/ClientTimeState.d.ts +14 -0
- package/dist/game/ClientTimeState.d.ts.map +1 -1
- package/dist/game/ClientTimeState.js +17 -0
- package/dist/game/ClientTimeState.js.map +1 -1
- package/dist/game/Game.d.ts +6 -0
- package/dist/game/Game.d.ts.map +1 -1
- package/dist/game/GameTheme.d.ts +3 -0
- package/dist/game/GameTheme.d.ts.map +1 -1
- package/dist/game/GameTheme.js +2 -0
- package/dist/game/GameTheme.js.map +1 -1
- package/dist/game/Match.d.ts +8 -2
- package/dist/game/Match.d.ts.map +1 -1
- package/dist/game/Match.js +4 -1
- package/dist/game/Match.js.map +1 -1
- package/dist/game/Pick.d.ts +2 -0
- package/dist/game/Pick.d.ts.map +1 -1
- package/dist/game/Pick.js +17 -1
- package/dist/game/Pick.js.map +1 -1
- package/dist/game/PlayerPromptStatusChit.d.ts +8 -0
- package/dist/game/PlayerPromptStatusChit.d.ts.map +1 -1
- package/dist/game/PlayerPromptStatusChit.js +5 -1
- package/dist/game/PlayerPromptStatusChit.js.map +1 -1
- package/dist/game/Prompt.d.ts +15 -0
- package/dist/game/Prompt.d.ts.map +1 -1
- package/dist/game/Prompt.js +16 -0
- package/dist/game/Prompt.js.map +1 -1
- package/dist/game/Turn.d.ts +15 -4
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +173 -60
- package/dist/game/Turn.js.map +1 -1
- package/dist/game/TurnState.d.ts +12 -1
- package/dist/game/TurnState.d.ts.map +1 -1
- package/dist/game/TurnState.js +15 -3
- package/dist/game/TurnState.js.map +1 -1
- package/dist/game/ai/AiClientPrompts.d.ts +20 -3
- package/dist/game/ai/AiClientPrompts.d.ts.map +1 -1
- package/dist/game/ai/AiClientPrompts.js +42 -6
- package/dist/game/ai/AiClientPrompts.js.map +1 -1
- package/dist/game/ai/AiPlayerView.js +2 -0
- package/dist/game/ai/AiPlayerView.js.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.d.ts +20 -1
- package/dist/game/clientTransport/ClientPrompts.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.js +30 -1
- package/dist/game/clientTransport/ClientPrompts.js.map +1 -1
- package/dist/game/clientTransport/ClientTime.d.ts +13 -0
- package/dist/game/clientTransport/ClientTime.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientTime.js +51 -11
- package/dist/game/clientTransport/ClientTime.js.map +1 -1
- package/dist/game/tutorial/TutorialController.d.ts +300 -0
- package/dist/game/tutorial/TutorialController.d.ts.map +1 -0
- package/dist/game/tutorial/TutorialController.js +1182 -0
- package/dist/game/tutorial/TutorialController.js.map +1 -0
- package/dist/game/tutorial/TutorialRuntime.d.ts +89 -0
- package/dist/game/tutorial/TutorialRuntime.d.ts.map +1 -0
- package/dist/game/tutorial/TutorialRuntime.js +261 -0
- package/dist/game/tutorial/TutorialRuntime.js.map +1 -0
- package/dist/game/tutorial/TutorialTheme.d.ts +28 -0
- package/dist/game/tutorial/TutorialTheme.d.ts.map +1 -0
- package/dist/game/tutorial/TutorialTheme.js +39 -0
- package/dist/game/tutorial/TutorialTheme.js.map +1 -0
- package/dist/game/tutorial/allowed.d.ts +2 -0
- package/dist/game/tutorial/allowed.d.ts.map +1 -0
- package/dist/game/tutorial/allowed.js +119 -0
- package/dist/game/tutorial/allowed.js.map +1 -0
- package/dist/game/tutorial/checks.d.ts +58 -0
- package/dist/game/tutorial/checks.d.ts.map +1 -0
- package/dist/game/tutorial/checks.js +144 -0
- package/dist/game/tutorial/checks.js.map +1 -0
- package/dist/game/tutorial/createDemoMatch.d.ts +47 -0
- package/dist/game/tutorial/createDemoMatch.d.ts.map +1 -0
- package/dist/game/tutorial/createDemoMatch.js +67 -0
- package/dist/game/tutorial/createDemoMatch.js.map +1 -0
- package/dist/game/tutorial/defineTutorial.d.ts +353 -0
- package/dist/game/tutorial/defineTutorial.d.ts.map +1 -0
- package/dist/game/tutorial/defineTutorial.js +86 -0
- package/dist/game/tutorial/defineTutorial.js.map +1 -0
- package/dist/game/tutorial/runTutorial.d.ts +63 -0
- package/dist/game/tutorial/runTutorial.d.ts.map +1 -0
- package/dist/game/tutorial/runTutorial.js +273 -0
- package/dist/game/tutorial/runTutorial.js.map +1 -0
- package/dist/hooks/usePanelNavigation.d.ts +28 -0
- package/dist/hooks/usePanelNavigation.d.ts.map +1 -0
- package/dist/hooks/usePanelNavigation.js +36 -0
- package/dist/hooks/usePanelNavigation.js.map +1 -0
- package/dist/hooks/useTimeController.d.ts +2 -1
- package/dist/hooks/useTimeController.d.ts.map +1 -1
- package/dist/hooks/useTimeController.js +6 -2
- package/dist/hooks/useTimeController.js.map +1 -1
- package/dist/hooks/useTutorial.d.ts +48 -0
- package/dist/hooks/useTutorial.d.ts.map +1 -0
- package/dist/hooks/useTutorial.js +16 -0
- package/dist/hooks/useTutorial.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.html +12 -0
- package/dist/index.js +19 -2
- package/dist/index.js.map +1 -1
- package/dist/main.0792d734cde2c688d5f7.hot-update.js +16991 -0
- package/dist/main.0792d734cde2c688d5f7.hot-update.js.map +1 -0
- package/dist/main.0792d734cde2c688d5f7.hot-update.json +1 -0
- package/dist/pretty-chitty.js +204513 -0
- package/dist/pretty-chitty.js.map +1 -0
- package/dist/rendering/CameraWrapperPerspective.d.ts +5 -0
- package/dist/rendering/CameraWrapperPerspective.d.ts.map +1 -1
- package/dist/rendering/CameraWrapperPerspective.js +16 -0
- package/dist/rendering/CameraWrapperPerspective.js.map +1 -1
- package/dist/rendering/ChitGalleryItemInstance.d.ts +1 -0
- package/dist/rendering/ChitGalleryItemInstance.d.ts.map +1 -1
- package/dist/rendering/ChitGalleryItemInstance.js +12 -0
- package/dist/rendering/ChitGalleryItemInstance.js.map +1 -1
- package/dist/rendering/ChitRenderInstance.d.ts +9 -1
- package/dist/rendering/ChitRenderInstance.d.ts.map +1 -1
- package/dist/rendering/ChitRenderInstance.js +28 -0
- package/dist/rendering/ChitRenderInstance.js.map +1 -1
- package/dist/rendering/RootChitRenderInstance.d.ts +2 -0
- package/dist/rendering/RootChitRenderInstance.d.ts.map +1 -1
- package/dist/rendering/RootChitRenderInstance.js +29 -0
- package/dist/rendering/RootChitRenderInstance.js.map +1 -1
- package/dist/utilities/Annotations.d.ts +15 -0
- package/dist/utilities/Annotations.d.ts.map +1 -1
- package/dist/utilities/Annotations.js +31 -7
- package/dist/utilities/Annotations.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +2 -0
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +4 -0
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/TokenizedText.d.ts +6 -0
- package/dist/utilities/TokenizedText.d.ts.map +1 -1
- package/dist/utilities/TokenizedText.js.map +1 -1
- package/dist/utilities/zIndex.d.ts +1 -0
- package/dist/utilities/zIndex.d.ts.map +1 -1
- package/dist/utilities/zIndex.js +1 -0
- package/dist/utilities/zIndex.js.map +1 -1
- package/package.json +3 -2
- package/src/library/components/ActionLogDisplay.tsx +1 -0
- package/src/library/components/BottomBarButton.tsx +5 -0
- package/src/library/components/ContextGalleryDisplay.tsx +2 -0
- package/src/library/components/DemoWrapper.tsx +147 -44
- package/src/library/components/FullScreenGalleryDisplay.tsx +3 -0
- package/src/library/components/Gallery/BuiltItem.ts +30 -9
- package/src/library/components/Gallery/GalleryController.ts +72 -1
- package/src/library/components/Gallery/GalleryViewer.tsx +54 -10
- package/src/library/components/Gallery/LayoutManager.ts +30 -6
- package/src/library/components/Gallery/constants.ts +2 -0
- package/src/library/components/Gallery/registry.ts +44 -0
- package/src/library/components/GameDesigner.tsx +121 -12
- package/src/library/components/InlineGalleryDisplay.tsx +5 -0
- package/src/library/components/LiveButton.tsx +8 -5
- package/src/library/components/MatchViewer.tsx +14 -3
- package/src/library/components/Panel/MultiPanel.tsx +21 -0
- package/src/library/components/Panel/PanelContents.tsx +70 -1
- package/src/library/components/Panel/PanelTabStack.tsx +1 -0
- package/src/library/components/PanelSpark.tsx +9 -4
- package/src/library/components/Playground.tsx +48 -21
- package/src/library/components/PromptControls.tsx +7 -3
- package/src/library/components/SparkLineChart.tsx +23 -27
- package/src/library/components/TimeControlBar.tsx +5 -3
- package/src/library/components/TokenizedMarkdown.tsx +224 -0
- package/src/library/components/TokenizedMessage.tsx +28 -32
- package/src/library/components/TopBarPlayers.tsx +12 -9
- package/src/library/components/Tutorial/BrowserNarration.ts +233 -0
- package/src/library/components/Tutorial/TutorialBox.tsx +506 -0
- package/src/library/components/Tutorial/TutorialDevPanel.tsx +297 -0
- package/src/library/components/Tutorial/TutorialOverlay.tsx +1301 -0
- package/src/library/components/Tutorial/targetRects.ts +273 -0
- package/src/library/game/Chit.ts +21 -5
- package/src/library/game/ClientTimeState.ts +18 -0
- package/src/library/game/Game.ts +7 -0
- package/src/library/game/GameTheme.ts +4 -0
- package/src/library/game/Match.ts +12 -2
- package/src/library/game/Pick.ts +16 -1
- package/src/library/game/PlayerPromptStatusChit.ts +5 -1
- package/src/library/game/Prompt.ts +20 -0
- package/src/library/game/Turn.ts +179 -61
- package/src/library/game/TurnState.ts +26 -3
- package/src/library/game/ai/AI_GUIDE.md +10 -0
- package/src/library/game/ai/AiClientPrompts.ts +60 -11
- package/src/library/game/ai/AiPlayerView.ts +2 -0
- package/src/library/game/clientTransport/ClientPrompts.ts +31 -1
- package/src/library/game/clientTransport/ClientTime.ts +54 -13
- package/src/library/game/serialize.test.ts +275 -0
- package/src/library/game/tutorial/TUTORIAL_GUIDE.md +411 -0
- package/src/library/game/tutorial/TutorialController.test.ts +731 -0
- package/src/library/game/tutorial/TutorialController.ts +1286 -0
- package/src/library/game/tutorial/TutorialRuntime.ts +311 -0
- package/src/library/game/tutorial/TutorialTheme.ts +67 -0
- package/src/library/game/tutorial/allowed.ts +145 -0
- package/src/library/game/tutorial/checks.ts +186 -0
- package/src/library/game/tutorial/createDemoMatch.ts +115 -0
- package/src/library/game/tutorial/defineTutorial.ts +414 -0
- package/src/library/game/tutorial/runTutorial.ts +331 -0
- package/src/library/game/undo.test.ts +282 -0
- package/src/library/hooks/usePanelNavigation.tsx +65 -0
- package/src/library/hooks/useTimeController.tsx +6 -2
- package/src/library/hooks/useTutorial.tsx +76 -0
- package/src/library/index.ts +57 -2
- package/src/library/rendering/CameraWrapperPerspective.ts +17 -0
- package/src/library/rendering/ChitGalleryItemInstance.ts +12 -0
- package/src/library/rendering/ChitRenderInstance.ts +34 -0
- package/src/library/rendering/RootChitRenderInstance.ts +30 -1
- package/src/library/utilities/Annotations.ts +35 -7
- package/src/library/utilities/CanvasStack/CanvasStack.ts +6 -1
- package/src/library/utilities/TokenizedText.ts +6 -0
- package/src/library/utilities/zIndex.ts +1 -0
|
@@ -15,11 +15,13 @@ export default function TimeControlBar({ autoLive = false }: { autoLive?: boolea
|
|
|
15
15
|
const [live, setLive] = useEventChannelState(timeState.live);
|
|
16
16
|
const [targetClock, setTargetClock] = useEventChannelState(timeState.targetClock);
|
|
17
17
|
const [maxClock] = useEventChannelState(timeController.maxClock);
|
|
18
|
+
const [hold] = useEventChannelState(timeState.timelineHold);
|
|
19
|
+
const end = hold === undefined ? maxClock.clock : Math.min(hold, maxClock.clock);
|
|
18
20
|
const modalState = useModalState();
|
|
19
21
|
const [settingsVisible, setSettingsVisible] = useEventChannelState(modalState.settingsVisible);
|
|
20
22
|
|
|
21
23
|
return (
|
|
22
|
-
<Box sx={{ width: "100%", height: "100%", position: "relative" }}>
|
|
24
|
+
<Box data-tutorial-target="time" sx={{ width: "100%", height: "100%", position: "relative" }}>
|
|
23
25
|
<Box sx={{ height: "6px", position: "absolute", width: "100%" }}>
|
|
24
26
|
{targetClock < maxClock.clock && !live && (
|
|
25
27
|
<Box
|
|
@@ -50,11 +52,11 @@ export default function TimeControlBar({ autoLive = false }: { autoLive?: boolea
|
|
|
50
52
|
}}
|
|
51
53
|
/>
|
|
52
54
|
<BottomBarButton
|
|
53
|
-
disabled={targetClock >=
|
|
55
|
+
disabled={targetClock >= end}
|
|
54
56
|
icon={FastForward}
|
|
55
57
|
label={"Forward"}
|
|
56
58
|
whileHolding={(n: number) => {
|
|
57
|
-
if (autoLive && targetClock + n >=
|
|
59
|
+
if (autoLive && targetClock + n >= end) {
|
|
58
60
|
setLive(true);
|
|
59
61
|
setTargetClock(targetClock + n);
|
|
60
62
|
} else {
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import React, { useMemo } from "react";
|
|
2
|
+
import Markdown, { Components } from "react-markdown";
|
|
3
|
+
import { Box } from "@mui/material";
|
|
4
|
+
import { Token } from "./TokenizedMessage";
|
|
5
|
+
import { TokenDefinition, TokenMap } from "../utilities/TokenizedText";
|
|
6
|
+
import { UpdatingCanvasImage } from "./UpdatingCanvasImage";
|
|
7
|
+
import { StaticImage } from "../utilities/StaticImage";
|
|
8
|
+
|
|
9
|
+
export interface TokenizedMarkdownProps {
|
|
10
|
+
/** Markdown. `:token:` references render as the game's icons, like log lines. */
|
|
11
|
+
text: string;
|
|
12
|
+
fontSize: number;
|
|
13
|
+
tokenMap?: TokenMap;
|
|
14
|
+
color?: string;
|
|
15
|
+
/** Sentence to emphasise (0-based, counted across the whole text); others are dimmed. -1 for none. */
|
|
16
|
+
activeSentence?: number;
|
|
17
|
+
/** Opacity of sentences other than the active one. Default 0.55 */
|
|
18
|
+
dimmedOpacity?: number;
|
|
19
|
+
/** Colour of the active sentence. Default: inherit */
|
|
20
|
+
accent?: string;
|
|
21
|
+
/** Resolves relative image URLs. Default: unchanged */
|
|
22
|
+
resolveImage?: (src: string) => string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const TOKEN_REGEX = /:([a-zA-Z0-9_-]+):/g;
|
|
26
|
+
const SENTENCE_END = /[.!?]+["')\]]*(?=\s|$)/g;
|
|
27
|
+
|
|
28
|
+
type HastNode = {
|
|
29
|
+
type: string;
|
|
30
|
+
tagName?: string;
|
|
31
|
+
value?: string;
|
|
32
|
+
properties?: Record<string, unknown>;
|
|
33
|
+
children?: HastNode[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A rehype plugin that turns `:token:` text into `<span data-token>` and wraps the text of each
|
|
38
|
+
* sentence in `<span data-sentence>`, counting sentences in document order. Keeps the structure
|
|
39
|
+
* react-markdown produced; only text nodes change.
|
|
40
|
+
*/
|
|
41
|
+
function rehypeTokensAndSentences(tokenMap: TokenMap) {
|
|
42
|
+
return () => (tree: HastNode) => {
|
|
43
|
+
let sentence = 0;
|
|
44
|
+
const walk = (node: HastNode, insideCode: boolean) => {
|
|
45
|
+
if (node.type === "element" && (node.tagName === "code" || node.tagName === "pre")) {
|
|
46
|
+
insideCode = true;
|
|
47
|
+
}
|
|
48
|
+
if (!node.children) return;
|
|
49
|
+
const next: HastNode[] = [];
|
|
50
|
+
for (const child of node.children) {
|
|
51
|
+
if (child.type === "text" && !insideCode && child.value) {
|
|
52
|
+
next.push(...splitText(child.value));
|
|
53
|
+
} else {
|
|
54
|
+
walk(child, insideCode);
|
|
55
|
+
next.push(child);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
node.children = next;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const span = (properties: Record<string, unknown>, children: HastNode[]): HastNode => ({
|
|
62
|
+
type: "element",
|
|
63
|
+
tagName: "span",
|
|
64
|
+
properties,
|
|
65
|
+
children,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const splitText = (value: string): HastNode[] => {
|
|
69
|
+
const out: HastNode[] = [];
|
|
70
|
+
let buffer = "";
|
|
71
|
+
const flushText = () => {
|
|
72
|
+
if (buffer) {
|
|
73
|
+
out.push(span({ dataSentence: sentence }, [{ type: "text", value: buffer }]));
|
|
74
|
+
buffer = "";
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
// Tokens first, then sentence ends inside the plain runs
|
|
78
|
+
let last = 0;
|
|
79
|
+
const tokenRegex = new RegExp(TOKEN_REGEX.source, "g");
|
|
80
|
+
let m: RegExpExecArray | null;
|
|
81
|
+
const pushRun = (run: string) => {
|
|
82
|
+
let runLast = 0;
|
|
83
|
+
const endRegex = new RegExp(SENTENCE_END.source, "g");
|
|
84
|
+
let e: RegExpExecArray | null;
|
|
85
|
+
while ((e = endRegex.exec(run)) !== null) {
|
|
86
|
+
buffer += run.substring(runLast, e.index + e[0].length);
|
|
87
|
+
runLast = e.index + e[0].length;
|
|
88
|
+
flushText();
|
|
89
|
+
sentence++;
|
|
90
|
+
}
|
|
91
|
+
buffer += run.substring(runLast);
|
|
92
|
+
};
|
|
93
|
+
while ((m = tokenRegex.exec(value)) !== null) {
|
|
94
|
+
const name = m[1];
|
|
95
|
+
if (!tokenMap[name]) continue;
|
|
96
|
+
pushRun(value.substring(last, m.index));
|
|
97
|
+
flushText();
|
|
98
|
+
out.push(span({ dataToken: name, dataSentence: sentence }, []));
|
|
99
|
+
last = m.index + m[0].length;
|
|
100
|
+
}
|
|
101
|
+
pushRun(value.substring(last));
|
|
102
|
+
flushText();
|
|
103
|
+
return out;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
walk(tree, false);
|
|
107
|
+
// Trailing text with no terminal punctuation still counts as a sentence; nothing to do.
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function MarkdownImage({
|
|
112
|
+
src,
|
|
113
|
+
alt,
|
|
114
|
+
tokenMap,
|
|
115
|
+
resolveImage,
|
|
116
|
+
}: {
|
|
117
|
+
src?: string;
|
|
118
|
+
alt?: string;
|
|
119
|
+
tokenMap: TokenMap;
|
|
120
|
+
resolveImage?: (src: string) => string;
|
|
121
|
+
}) {
|
|
122
|
+
if (!src) return null;
|
|
123
|
+
if (src.startsWith("token:")) {
|
|
124
|
+
const def: TokenDefinition | undefined = tokenMap[src.substring("token:".length)];
|
|
125
|
+
if (!def?.image) return <>{alt}</>;
|
|
126
|
+
if (typeof def.image === "string") {
|
|
127
|
+
return <Box component="img" src={def.image} alt={alt} sx={{ maxWidth: "100%", display: "block", my: 1 }} />;
|
|
128
|
+
}
|
|
129
|
+
const canvas = new StaticImage(def.image).get();
|
|
130
|
+
return (
|
|
131
|
+
<Box sx={{ my: 1 }}>
|
|
132
|
+
<UpdatingCanvasImage image={canvas} style={{ maxWidth: "100%", display: "block" }} />
|
|
133
|
+
</Box>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return (
|
|
137
|
+
<Box
|
|
138
|
+
component="img"
|
|
139
|
+
src={resolveImage ? resolveImage(src) : src}
|
|
140
|
+
alt={alt}
|
|
141
|
+
sx={{ maxWidth: "100%", height: "auto", display: "block", my: 1, borderRadius: 1 }}
|
|
142
|
+
/>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Markdown with the game's `:token:` icons inline, rendered with the same token map and `Token`
|
|
148
|
+
* component as the action log, plus optional per-sentence emphasis for narration. Images can be
|
|
149
|
+
* URLs or `token:<name>` to show a token's art at image size.
|
|
150
|
+
*/
|
|
151
|
+
export function TokenizedMarkdown({
|
|
152
|
+
text,
|
|
153
|
+
fontSize,
|
|
154
|
+
tokenMap = {},
|
|
155
|
+
color,
|
|
156
|
+
activeSentence = -1,
|
|
157
|
+
dimmedOpacity = 0.55,
|
|
158
|
+
accent,
|
|
159
|
+
resolveImage,
|
|
160
|
+
}: TokenizedMarkdownProps) {
|
|
161
|
+
const plugin = useMemo(() => rehypeTokensAndSentences(tokenMap), [tokenMap]);
|
|
162
|
+
|
|
163
|
+
const components = useMemo<Components>(
|
|
164
|
+
() => ({
|
|
165
|
+
// eslint-disable-next-line react/prop-types
|
|
166
|
+
span: (props) => {
|
|
167
|
+
// eslint-disable-next-line react/prop-types
|
|
168
|
+
const { node, children, ...rest } = props as any;
|
|
169
|
+
// eslint-disable-next-line react/prop-types
|
|
170
|
+
const p = node?.properties ?? {};
|
|
171
|
+
const tokenName = p.dataToken as string | undefined;
|
|
172
|
+
const sentence = p.dataSentence as number | undefined;
|
|
173
|
+
const emphasised = activeSentence < 0 || sentence === undefined || sentence === activeSentence;
|
|
174
|
+
const style: React.CSSProperties = {
|
|
175
|
+
opacity: emphasised ? 1 : dimmedOpacity,
|
|
176
|
+
color: emphasised && activeSentence >= 0 && accent ? accent : undefined,
|
|
177
|
+
transition: "opacity 0.25s linear, color 0.25s linear",
|
|
178
|
+
};
|
|
179
|
+
if (tokenName) {
|
|
180
|
+
return (
|
|
181
|
+
<span style={style}>
|
|
182
|
+
<Token name={tokenName} definition={tokenMap[tokenName]} fontSize={fontSize} />
|
|
183
|
+
</span>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return (
|
|
187
|
+
<span {...rest} style={style}>
|
|
188
|
+
{children}
|
|
189
|
+
</span>
|
|
190
|
+
);
|
|
191
|
+
},
|
|
192
|
+
// eslint-disable-next-line react/prop-types
|
|
193
|
+
img: (props) => {
|
|
194
|
+
// eslint-disable-next-line react/prop-types
|
|
195
|
+
const { src, alt } = props as any;
|
|
196
|
+
return <MarkdownImage src={src} alt={alt} tokenMap={tokenMap} resolveImage={resolveImage} />;
|
|
197
|
+
},
|
|
198
|
+
p: ({ children }) => <Box sx={{ my: 0.75, "&:first-of-type": { mt: 0 }, "&:last-of-type": { mb: 0 } }}>{children}</Box>,
|
|
199
|
+
ul: ({ children }) => <Box component="ul" sx={{ my: 0.5, pl: 2.5 }}>{children}</Box>,
|
|
200
|
+
ol: ({ children }) => <Box component="ol" sx={{ my: 0.5, pl: 2.5 }}>{children}</Box>,
|
|
201
|
+
li: ({ children }) => <Box component="li" sx={{ my: 0.25 }}>{children}</Box>,
|
|
202
|
+
h1: ({ children }) => <Box sx={{ fontSize: fontSize * 1.4, fontWeight: "bold", my: 0.5 }}>{children}</Box>,
|
|
203
|
+
h2: ({ children }) => <Box sx={{ fontSize: fontSize * 1.25, fontWeight: "bold", my: 0.5 }}>{children}</Box>,
|
|
204
|
+
h3: ({ children }) => <Box sx={{ fontSize: fontSize * 1.1, fontWeight: "bold", my: 0.5 }}>{children}</Box>,
|
|
205
|
+
a: ({ children, href }) => (
|
|
206
|
+
<a href={href} target="_blank" rel="noreferrer" style={{ color: accent ?? "inherit" }}>
|
|
207
|
+
{children}
|
|
208
|
+
</a>
|
|
209
|
+
),
|
|
210
|
+
blockquote: ({ children }) => (
|
|
211
|
+
<Box sx={{ borderLeft: `3px solid ${accent ?? "currentColor"}`, pl: 1.5, my: 0.75, opacity: 0.9 }}>{children}</Box>
|
|
212
|
+
),
|
|
213
|
+
}),
|
|
214
|
+
[activeSentence, dimmedOpacity, accent, tokenMap, fontSize, resolveImage],
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<Box sx={{ fontSize: `${fontSize}px`, lineHeight: 1.35, color }}>
|
|
219
|
+
<Markdown rehypePlugins={[plugin]} components={components} skipHtml>
|
|
220
|
+
{text}
|
|
221
|
+
</Markdown>
|
|
222
|
+
</Box>
|
|
223
|
+
);
|
|
224
|
+
}
|
|
@@ -23,44 +23,34 @@ function calculateBackgroundColor(textColor: string): string {
|
|
|
23
23
|
: color.lightness(95).hex(); // 95% brightness for dark colors
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
const LINE_HEIGHT = 1.1;
|
|
27
|
+
const DEFAULT_IMAGE_BLEED = 0.1;
|
|
28
|
+
|
|
26
29
|
// Token Image Component
|
|
27
30
|
interface TokenImageProps {
|
|
28
31
|
src: string | ImageSpec;
|
|
29
32
|
alt: string;
|
|
30
33
|
fontSize: number;
|
|
31
|
-
|
|
34
|
+
bleed: number;
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
function TokenImage({ src, alt, fontSize,
|
|
37
|
+
function TokenImage({ src, alt, fontSize, bleed }: TokenImageProps) {
|
|
38
|
+
// grow past the line box, then cancel the extra height with negative margins so lines don't get pushed apart
|
|
39
|
+
const bleedPx = bleed * fontSize;
|
|
40
|
+
const style = {
|
|
41
|
+
height: `${fontSize * LINE_HEIGHT + bleedPx * 2}px`,
|
|
42
|
+
marginTop: `${-bleedPx}px`,
|
|
43
|
+
marginBottom: `${-bleedPx}px`,
|
|
44
|
+
marginLeft: "1px",
|
|
45
|
+
marginRight: "3px",
|
|
46
|
+
position: "relative" as const,
|
|
47
|
+
};
|
|
48
|
+
|
|
35
49
|
if (typeof src !== "string") {
|
|
36
50
|
const updatingCanvas = new StaticImage(src).get();
|
|
37
|
-
return
|
|
38
|
-
<UpdatingCanvasImage
|
|
39
|
-
image={updatingCanvas}
|
|
40
|
-
style={{
|
|
41
|
-
height: `${fontSize - 4}px`,
|
|
42
|
-
marginRight: "4px",
|
|
43
|
-
transform: `scale(1.4)`,
|
|
44
|
-
position: "relative",
|
|
45
|
-
marginLeft: "2px",
|
|
46
|
-
}}
|
|
47
|
-
/>
|
|
48
|
-
);
|
|
51
|
+
return <UpdatingCanvasImage image={updatingCanvas} style={style} />;
|
|
49
52
|
} else {
|
|
50
|
-
return
|
|
51
|
-
<Box
|
|
52
|
-
component="img"
|
|
53
|
-
src={src}
|
|
54
|
-
alt={alt}
|
|
55
|
-
sx={{
|
|
56
|
-
height: `${fontSize - 4}px`,
|
|
57
|
-
marginRight: "4px",
|
|
58
|
-
transform: `scale(1.4)`,
|
|
59
|
-
position: "relative",
|
|
60
|
-
marginLeft: "2px",
|
|
61
|
-
}}
|
|
62
|
-
/>
|
|
63
|
-
);
|
|
53
|
+
return <Box component="img" src={src} alt={alt} sx={style} />;
|
|
64
54
|
}
|
|
65
55
|
}
|
|
66
56
|
|
|
@@ -93,7 +83,7 @@ interface TokenProps {
|
|
|
93
83
|
fontSize: number;
|
|
94
84
|
}
|
|
95
85
|
|
|
96
|
-
function Token({ name, definition, fontSize }: TokenProps) {
|
|
86
|
+
export function Token({ name, definition, fontSize }: TokenProps) {
|
|
97
87
|
if (!definition) {
|
|
98
88
|
return <>{`:${name}:`}</>;
|
|
99
89
|
}
|
|
@@ -107,7 +97,8 @@ function Token({ name, definition, fontSize }: TokenProps) {
|
|
|
107
97
|
display: "inline-flex",
|
|
108
98
|
alignItems: "center",
|
|
109
99
|
whiteSpace: "nowrap",
|
|
110
|
-
|
|
100
|
+
// an image has no text baseline, so center image tokens on the text instead
|
|
101
|
+
verticalAlign: definition.image ? "middle" : "baseline",
|
|
111
102
|
backgroundColor,
|
|
112
103
|
padding: backgroundColor ? "1px 2px" : 0,
|
|
113
104
|
margin: backgroundColor ? "-1px 0" : 0,
|
|
@@ -115,7 +106,12 @@ function Token({ name, definition, fontSize }: TokenProps) {
|
|
|
115
106
|
}}
|
|
116
107
|
>
|
|
117
108
|
{definition.image && (
|
|
118
|
-
<TokenImage
|
|
109
|
+
<TokenImage
|
|
110
|
+
src={definition.image}
|
|
111
|
+
alt={name}
|
|
112
|
+
fontSize={fontSize}
|
|
113
|
+
bleed={definition.imageBleed ?? (backgroundColor ? 0 : DEFAULT_IMAGE_BLEED)}
|
|
114
|
+
/>
|
|
119
115
|
)}
|
|
120
116
|
{definition.label && <TokenLabel label={definition.label} color={definition.color} fontSize={fontSize} />}
|
|
121
117
|
</Box>
|
|
@@ -130,7 +126,7 @@ export function TokenizedMessage({ message, fontSize, tokenMap = {} }: Tokenized
|
|
|
130
126
|
<Box
|
|
131
127
|
sx={{
|
|
132
128
|
fontSize: `${fontSize}px`,
|
|
133
|
-
lineHeight: `${fontSize *
|
|
129
|
+
lineHeight: `${fontSize * LINE_HEIGHT}px`,
|
|
134
130
|
}}
|
|
135
131
|
>
|
|
136
132
|
{elements.map((element) =>
|
|
@@ -138,15 +138,18 @@ export default function TopBarPlayers() {
|
|
|
138
138
|
<TopBarDropdown
|
|
139
139
|
label={
|
|
140
140
|
<Stack direction={"row"} sx={{ pt: 1, pb: 1, maxWidth: "100%" }}>
|
|
141
|
-
{
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
141
|
+
{/* The avatar cluster is what a tutorial points at with { ui: "players" }, not the whole strip */}
|
|
142
|
+
<Stack direction={"row"} data-tutorial-target="players" sx={{ pl: 1 }}>
|
|
143
|
+
{playerChits.map((player) => (
|
|
144
|
+
<PlayerImage
|
|
145
|
+
sx={{ ml: -1 }}
|
|
146
|
+
borderColor={player.promptStatus.latestPromptMessage ? theme.chitHighlightColor : undefined}
|
|
147
|
+
size={theme.topBarHeight - theme.spacing * 2}
|
|
148
|
+
key={player.id}
|
|
149
|
+
player={player}
|
|
150
|
+
/>
|
|
151
|
+
))}
|
|
152
|
+
</Stack>
|
|
150
153
|
<Typography
|
|
151
154
|
flex={1}
|
|
152
155
|
sx={{
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { NarrationPlayer } from "../../game/tutorial/TutorialController";
|
|
2
|
+
|
|
3
|
+
type Listener<T> = (v: T) => void;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Plays a step's narration in the browser: an audio file through an `<audio>` element, or, when the
|
|
7
|
+
* step's `audio` is `"tts"`, the browser's speech synthesis (sentence boundaries come from the
|
|
8
|
+
* synthesizer, so sentence cues are exact). Mobile browsers need a user gesture before either will
|
|
9
|
+
* play; the tutorial's Start tap provides it.
|
|
10
|
+
*/
|
|
11
|
+
export class BrowserNarration implements NarrationPlayer {
|
|
12
|
+
private audio?: HTMLAudioElement;
|
|
13
|
+
private utterances: SpeechSynthesisUtterance[] = [];
|
|
14
|
+
private timeListeners: Listener<number>[] = [];
|
|
15
|
+
private sentenceListeners: Listener<number>[] = [];
|
|
16
|
+
private endListeners: Listener<void>[] = [];
|
|
17
|
+
private _playing = false;
|
|
18
|
+
private _paused = false;
|
|
19
|
+
private startedAt = 0;
|
|
20
|
+
private pausedAt = 0;
|
|
21
|
+
private ttsTimer: any;
|
|
22
|
+
private _duration: number | undefined;
|
|
23
|
+
private rate = 1;
|
|
24
|
+
private generation = 0;
|
|
25
|
+
/** Recordings loading ahead of their step (see `preload`), by URL, oldest first */
|
|
26
|
+
private preloaded = new Map<string, HTMLAudioElement>();
|
|
27
|
+
|
|
28
|
+
constructor(private resolveUrl: (url: string) => string = (u) => u) {}
|
|
29
|
+
|
|
30
|
+
get playing() {
|
|
31
|
+
return this._playing;
|
|
32
|
+
}
|
|
33
|
+
get paused() {
|
|
34
|
+
return this._paused;
|
|
35
|
+
}
|
|
36
|
+
get duration() {
|
|
37
|
+
return this._duration;
|
|
38
|
+
}
|
|
39
|
+
get time() {
|
|
40
|
+
if (this.audio) return this.audio.currentTime;
|
|
41
|
+
if (!this._playing) return 0;
|
|
42
|
+
return (((this._paused ? this.pausedAt : performance.now()) - this.startedAt) / 1000) * this.rate;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Playback speed. Recorded audio changes speed immediately (pitch preserved). Speech synthesis
|
|
47
|
+
* queues every sentence up front, so a change there applies from the next step.
|
|
48
|
+
*/
|
|
49
|
+
setRate(rate: number) {
|
|
50
|
+
if (!(rate > 0)) return;
|
|
51
|
+
if (!this.audio && this._playing) {
|
|
52
|
+
// keep `time` continuous for the speech clock
|
|
53
|
+
const at = this._paused ? this.pausedAt : performance.now();
|
|
54
|
+
const t = ((at - this.startedAt) / 1000) * this.rate;
|
|
55
|
+
this.startedAt = at - (t * 1000) / rate;
|
|
56
|
+
}
|
|
57
|
+
this.rate = rate;
|
|
58
|
+
if (this.audio) this.audio.playbackRate = rate;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
onTime(cb: Listener<number>) {
|
|
62
|
+
this.timeListeners.push(cb);
|
|
63
|
+
return () => {
|
|
64
|
+
this.timeListeners = this.timeListeners.filter((c) => c !== cb);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
onSentence(cb: Listener<number>) {
|
|
68
|
+
this.sentenceListeners.push(cb);
|
|
69
|
+
return () => {
|
|
70
|
+
this.sentenceListeners = this.sentenceListeners.filter((c) => c !== cb);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
onEnd(cb: () => void) {
|
|
74
|
+
this.endListeners.push(cb);
|
|
75
|
+
return () => {
|
|
76
|
+
this.endListeners = this.endListeners.filter((c) => c !== cb);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Starts downloading a recording before its step, so it starts at once when the step comes. Keeps
|
|
82
|
+
* the last few: Back can return to one just played.
|
|
83
|
+
*/
|
|
84
|
+
preload(audio: string) {
|
|
85
|
+
if (audio === "tts" || typeof Audio === "undefined") return;
|
|
86
|
+
const url = this.resolveUrl(audio);
|
|
87
|
+
if (this.preloaded.has(url)) return;
|
|
88
|
+
const element = new Audio();
|
|
89
|
+
element.preload = "auto";
|
|
90
|
+
element.src = url;
|
|
91
|
+
element.load();
|
|
92
|
+
this.preloaded.set(url, element);
|
|
93
|
+
while (this.preloaded.size > 3) {
|
|
94
|
+
const [oldest] = this.preloaded.keys();
|
|
95
|
+
this.preloaded.delete(oldest);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async play(step: { audio: string; text: string; sentences: string[] }): Promise<void> {
|
|
100
|
+
this.stop();
|
|
101
|
+
const generation = ++this.generation;
|
|
102
|
+
this._playing = true;
|
|
103
|
+
this._paused = false;
|
|
104
|
+
if (step.audio === "tts") {
|
|
105
|
+
return this.speak(step.sentences, generation);
|
|
106
|
+
}
|
|
107
|
+
const url = this.resolveUrl(step.audio);
|
|
108
|
+
// Reuse the element that has been loading it, if any
|
|
109
|
+
const ready = this.preloaded.get(url);
|
|
110
|
+
this.preloaded.delete(url);
|
|
111
|
+
const audio = ready ?? new Audio(url);
|
|
112
|
+
if (ready) audio.currentTime = 0;
|
|
113
|
+
audio.playbackRate = this.rate;
|
|
114
|
+
audio.preservesPitch = true;
|
|
115
|
+
this.audio = audio;
|
|
116
|
+
this._duration = undefined;
|
|
117
|
+
audio.addEventListener("loadedmetadata", () => {
|
|
118
|
+
if (generation === this.generation) this._duration = audio.duration;
|
|
119
|
+
});
|
|
120
|
+
audio.addEventListener("timeupdate", () => {
|
|
121
|
+
if (generation === this.generation) this.timeListeners.forEach((cb) => cb(audio.currentTime));
|
|
122
|
+
});
|
|
123
|
+
audio.addEventListener("ended", () => {
|
|
124
|
+
if (generation !== this.generation) return;
|
|
125
|
+
this._playing = false;
|
|
126
|
+
this.endListeners.forEach((cb) => cb());
|
|
127
|
+
});
|
|
128
|
+
try {
|
|
129
|
+
await audio.play();
|
|
130
|
+
} catch (e) {
|
|
131
|
+
this._playing = false;
|
|
132
|
+
throw e;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private speak(sentences: string[], generation: number): Promise<void> {
|
|
137
|
+
const synth = typeof window !== "undefined" ? window.speechSynthesis : undefined;
|
|
138
|
+
if (!synth || sentences.length === 0) {
|
|
139
|
+
this._playing = false;
|
|
140
|
+
return Promise.reject(new Error("speech synthesis unavailable"));
|
|
141
|
+
}
|
|
142
|
+
synth.cancel();
|
|
143
|
+
this.startedAt = performance.now();
|
|
144
|
+
const text = (s: string) => s.replace(/[*_`#>[\]()!]/g, "").replace(/:[a-zA-Z0-9_-]+:/g, "");
|
|
145
|
+
// Rough estimate so proportional cues have something to work with: ~2.6 words/second
|
|
146
|
+
const words = sentences.join(" ").split(/\s+/).length;
|
|
147
|
+
this._duration = Math.max(1, words / 2.6);
|
|
148
|
+
let started: () => void = () => {};
|
|
149
|
+
let failed: (e: Error) => void = () => {};
|
|
150
|
+
const startedPromise = new Promise<void>((resolve, reject) => {
|
|
151
|
+
started = resolve;
|
|
152
|
+
failed = reject;
|
|
153
|
+
});
|
|
154
|
+
// Some browsers (and headless ones) accept the utterance and never speak: fall back after a moment
|
|
155
|
+
const watchdog = setTimeout(() => {
|
|
156
|
+
if (generation === this.generation && this._playing) {
|
|
157
|
+
this.stop();
|
|
158
|
+
failed(new Error("speech synthesis did not start"));
|
|
159
|
+
}
|
|
160
|
+
}, 2500);
|
|
161
|
+
this.utterances = sentences.map((s, i) => {
|
|
162
|
+
const u = new SpeechSynthesisUtterance(text(s));
|
|
163
|
+
u.rate = this.rate;
|
|
164
|
+
u.onstart = () => {
|
|
165
|
+
if (generation !== this.generation) return;
|
|
166
|
+
if (i === 0) {
|
|
167
|
+
clearTimeout(watchdog);
|
|
168
|
+
started();
|
|
169
|
+
}
|
|
170
|
+
this.sentenceListeners.forEach((cb) => cb(i));
|
|
171
|
+
};
|
|
172
|
+
u.onerror = () => {
|
|
173
|
+
if (generation !== this.generation) return;
|
|
174
|
+
clearTimeout(watchdog);
|
|
175
|
+
this.stop();
|
|
176
|
+
failed(new Error("speech synthesis error"));
|
|
177
|
+
};
|
|
178
|
+
u.onend = () => {
|
|
179
|
+
if (generation !== this.generation) return;
|
|
180
|
+
if (i === sentences.length - 1) {
|
|
181
|
+
this._playing = false;
|
|
182
|
+
clearInterval(this.ttsTimer);
|
|
183
|
+
this.endListeners.forEach((cb) => cb());
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
return u;
|
|
187
|
+
});
|
|
188
|
+
this.utterances.forEach((u) => synth.speak(u));
|
|
189
|
+
clearInterval(this.ttsTimer);
|
|
190
|
+
this.ttsTimer = setInterval(() => {
|
|
191
|
+
if (generation === this.generation && this._playing && !this._paused) {
|
|
192
|
+
this.timeListeners.forEach((cb) => cb(this.time));
|
|
193
|
+
}
|
|
194
|
+
}, 100);
|
|
195
|
+
return startedPromise;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
pause() {
|
|
199
|
+
if (!this._playing || this._paused) return;
|
|
200
|
+
this._paused = true;
|
|
201
|
+
if (this.audio) this.audio.pause();
|
|
202
|
+
else {
|
|
203
|
+
this.pausedAt = performance.now();
|
|
204
|
+
window.speechSynthesis?.pause();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
resume() {
|
|
209
|
+
if (!this._playing || !this._paused) return;
|
|
210
|
+
this._paused = false;
|
|
211
|
+
if (this.audio) void this.audio.play().catch(() => {});
|
|
212
|
+
else {
|
|
213
|
+
this.startedAt += performance.now() - this.pausedAt;
|
|
214
|
+
window.speechSynthesis?.resume();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
stop() {
|
|
219
|
+
this.generation++;
|
|
220
|
+
this._playing = false;
|
|
221
|
+
this._paused = false;
|
|
222
|
+
clearInterval(this.ttsTimer);
|
|
223
|
+
if (this.audio) {
|
|
224
|
+
this.audio.pause();
|
|
225
|
+
this.audio.src = "";
|
|
226
|
+
this.audio = undefined;
|
|
227
|
+
}
|
|
228
|
+
if (this.utterances.length && typeof window !== "undefined") {
|
|
229
|
+
window.speechSynthesis?.cancel();
|
|
230
|
+
this.utterances = [];
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|