-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathexercise-preview-page.tsx
More file actions
90 lines (75 loc) · 2.55 KB
/
Copy pathexercise-preview-page.tsx
File metadata and controls
90 lines (75 loc) · 2.55 KB
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
import {View} from "@khanacademy/wonder-blocks-core";
import {spacing} from "@khanacademy/wonder-blocks-tokens";
import {StyleSheet} from "aphrodite";
import * as React from "react";
import {usePreviewPresenter} from "../../preview/use-preview-presenter";
import {PreviewRenderer} from "./preview-renderer";
/**
* The exercise preview page. This page is loaded inside an iframe and should
* only be used to support editor previews in Storybook!
*/
const ExercisePreviewPage = () => {
const {content, isMobile, hasLintGutter, reportHeight} =
usePreviewPresenter();
const containerRef = React.useRef<HTMLDivElement>(null);
const lastHeightRef = React.useRef<number | null>(null);
// Handle body overflow for article-all type (allows scrolling)
React.useEffect(() => {
if (content?.type === "article-all") {
document.body.style.overflow = "scroll";
} else {
document.body.style.overflow = "hidden";
}
}, [content?.type]);
// Send height updates to parent
React.useEffect(() => {
if (!containerRef.current) {
return;
}
const sendHeightUpdate = () => {
const height = containerRef.current?.scrollHeight;
if (height && height !== lastHeightRef.current) {
lastHeightRef.current = height;
// NOTE(Jeremy) I'm not sure where this comes from but it's how
// production does it.
const bottomMargin = 30;
reportHeight(height + bottomMargin);
}
};
// Send initial height
sendHeightUpdate();
// Also send on resize
let resizeObserver: ResizeObserver | undefined;
const container = containerRef.current;
if (container != null) {
resizeObserver = new ResizeObserver(sendHeightUpdate);
resizeObserver.observe(container);
}
return () => {
resizeObserver?.disconnect();
};
}, [content, reportHeight]);
if (!content) {
return (
<View style={styles.loading}>
<div>Loading preview...</div>
</View>
);
}
return (
<div ref={containerRef}>
<PreviewRenderer
content={content}
isMobile={isMobile}
hasLintGutter={hasLintGutter}
/>
</div>
);
};
const styles = StyleSheet.create({
loading: {
padding: spacing.medium_16,
textAlign: "center",
},
});
export default ExercisePreviewPage;