-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugPlugin.tsx
153 lines (137 loc) · 4.08 KB
/
DebugPlugin.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import classNames from 'classnames';
import {
log,
} from 'colorful-logging';
import {
Article,
Typography,
} from '../../bundles/componentsBundle';
import {
getNiceValueString,
} from '../functions/getNiceValueString';
import {
IPlugin,
} from './IPlugin';
import {
IPluginMethodBaseArgs,
IPluginMethodChildArgs,
IPluginMethodStateMutationArgs,
IPluginMethodStateChangingArgs
} from './IPluginMethodArgs';
import * as React from 'react';
import styles from './DebugPlugin.less';
export class DebugPlugin implements IPlugin {
public readonly afterStoryInit = ({
passageObject,
lastLinkTags,
}: IPluginMethodBaseArgs & IPluginMethodStateMutationArgs) => {
log('---- afterStoryInit ----');
log('The story is initializing.');
log(`Current passage is: ${passageObject.name}`);
log('The previous link tags after story initialization were:\n' +
getNiceValueString(lastLinkTags));
};
public readonly beforePassageChange = ({
passageObject: { name },
}: IPluginMethodBaseArgs) => {
log('---- beforePassageChange ----');
log(`PassageContainer will render the passage named ${name}.`);
};
public readonly beforeRender = ({
children,
passageObject: {
name,
tags,
},
storyState: currentStoryState,
}: IPluginMethodBaseArgs & IPluginMethodChildArgs) => {
log('---- beforeRender ----');
log(`PassageContainer is rendering the passage named ${name}.`);
log('The passage\'s tags are:');
log(getNiceValueString(tags))
log('The story state before render is:');
log(JSON.stringify(currentStoryState, null, 2));
return (
<>
{children}
{/* Inject the debug widget at the bottom of the page. */}
<Article className={classNames(
styles.debugContainer,
'debugContainer',
)}>
<Typography
className={classNames(
styles.debugStateTitleContainer,
'debugStateTitle',
)}
paragraph={true}
>
<Typography
className={classNames(styles.debugStateTitle)}
component="strong"
>
Current story state:
</Typography>
</Typography>
<Typography
className={classNames(
styles.debugReadoutContainer,
'debugReadoutContainer',
)}
paragraph={true}
>
<Typography
className={classNames(
styles.debugStateReadout,
'debugStateReadout',
)}
component="code"
>
{getNiceValueString(currentStoryState)}
</Typography>
</Typography>
</Article>
</>
);
};
public readonly afterPassageChange = ({
passageObject: { name },
}: IPluginMethodBaseArgs) =>
{
log('---- afterRender ----');
log(`PassageContainer has rendered the passage named ${name}.`);
};
public readonly afterStoryStateChange = ({
storyState,
updatedStateProps,
}: IPluginMethodBaseArgs & IPluginMethodStateChangingArgs) => {
log('---- afterStoryStateChange ----');
log('The following modifications to the story state are being made:');
log(getNiceValueString(updatedStateProps));
if (document && typeof document.querySelector === 'function') {
const readout = document.querySelector('.debugStateReadout');
if (readout) {
readout.textContent = getNiceValueString(storyState);
}
}
};
public readonly beforeRestart = ({
storyState,
passageObject: { name },
}: IPluginMethodBaseArgs) => {
log('---- beforeRestart ----');
log('The story is restarting.');
log(`The passage on which the user restarted was named ${name}.`);
log('The story state at restart was:');
log(getNiceValueString(storyState));
if (document &&
typeof document.querySelector === 'function')
{
/* Wipe the readout. */
const readout = document.querySelector('.debugStateReadout');
if (readout) {
readout.textContent = '{}';
}
}
};
}