-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
115 lines (100 loc) · 2.71 KB
/
index.js
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
const MONITOR_INTERVAL = 200;
export class DocumentBodyDimensionsMutationObserverMonitor {
constructor() {
this.lastHeight = -1;
this.onMutation = entries => {
const height = document.body.clientHeight;
if (height !== this.lastHeight) {
this.lastHeight = height;
postHeight(this.lastHeight);
}
};
}
start() {
this.observer = new MutationObserver(this.onMutation);
this.observer.observe(document.body, {
childList: true,
attributes: true,
subtree: true
});
}
}
export class DocumentBodyDimensionsResizeObserverMonitor {
constructor() {
if (typeof window.ResizeObserver === "undefined") {
throw Error("ResizeObserver is not supported");
}
this.lastHeight = -1;
this.onResize = entries => {
for (let entry of entries) {
const height = entry.contentRect.height;
if (height !== this.lastHeight) {
this.lastHeight = height;
postHeight(this.lastHeight);
}
}
};
}
start() {
this.observer = new ResizeObserver(this.onResize);
this.observer.observe(document.body);
}
}
export class DocumentBodyDimensionsPollingMonitor {
constructor() {
this.lastHeight = -1;
this.timer = undefined;
this.checkAndSchedule = () => {
if (!this.monitoring) return;
this.check();
this.monitoring = setTimeout(this.checkAndSchedule, MONITOR_INTERVAL);
};
}
start() {
if (!this.monitoring) {
this.monitoring = true;
this.checkAndSchedule();
}
}
stop() {
if (this.monitoring) {
clearTimeout(this.monitoring);
}
this.monitoring = undefined;
}
check() {
const height = document.body.clientHeight;
if (height !== this.lastHeight) {
this.lastHeight = height;
postHeight(this.lastHeight);
}
}
}
function postHeight(height) {
window.parent.postMessage(
{
type: "iframeSize",
height
},
"*"
);
}
export const monitor = () => {
if (typeof window.ResizeObserver !== "undefined") {
new DocumentBodyDimensionsResizeObserverMonitor().start();
} else {
new DocumentBodyDimensionsMutationObserverMonitor().start();
}
};
import { Runtime, Inspector } from "@observablehq/runtime";
export const embed = async (slug, into, cells, inputs = {}) => {
const moduleUrl = "https://api.observablehq.com/" + slug + ".js?v=3";
const define = (await import(moduleUrl)).default;
const inspect = Inspector.into(into);
const filter = cells ? name => cells.includes(name) : name => true;
const main = new Runtime().module(define, name => filter(name) ? inspect() : true);
for (let name of Object.keys(inputs)) {
main.redefine(name, inputs[name]);
}
return main;
};