Skip to content

Commit 695a8cf

Browse files
committed
fix #7759 via a combination of scary javascript hacks AND whitelisting pandas
1 parent 8bd72d4 commit 695a8cf

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

src/packages/frontend/jupyter/output-messages/mime-types/html.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const Html = ({
1313
index?: number;
1414
trust?: boolean;
1515
}) => {
16-
if (!trust) {
17-
<HTML value={value} />;
16+
if (!trust || !requiresStableUnsafeHtml(value)) {
17+
return <HTML value={value} />;
1818
}
1919
return (
2020
<div style={{ margin: "5px 0" }}>
@@ -34,3 +34,20 @@ export default Html;
3434
// that looks good and is meant to be rendered on the frontend.
3535
// See https://github.com/sagemathinc/cocalc/issues/5925
3636
register("text/html", 5, Html);
37+
38+
// Heuristics to only use plain stateless html.
39+
function requiresStableUnsafeHtml(value: string) {
40+
if (!value) {
41+
return false;
42+
}
43+
if (value.includes(".bk-notebook-logo")) {
44+
// bokeh -- needs state
45+
return true;
46+
}
47+
if (value.includes(`class="dataframe"`)) {
48+
// pandas
49+
return false;
50+
}
51+
// default for now
52+
return true;
53+
}

src/packages/frontend/jupyter/output-messages/stable-unsafe-html.tsx

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@ const cache = new TTL<string, any>({
6464
});
6565

6666
// make it really standout:
67-
// const PADDING = 5;
68-
// const STYLE = {
69-
// border: "1px solid #ccc",
70-
// borderRadius: "5px",
71-
// padding: `${PADDING}px`,
72-
// background: "#eee",
73-
// } as const;
74-
75-
// make it blend in
76-
const PADDING = 0;
77-
const STYLE = {} as const;
67+
const PADDING = 5;
68+
const STYLE = {
69+
border: "1px solid #ccc",
70+
borderRadius: "5px",
71+
padding: `${PADDING}px`,
72+
} as const;
73+
74+
// // make it blend in
75+
// const PADDING = 0;
76+
// const STYLE = {} as const;
7877

7978
interface Props {
8079
docId: string;
@@ -180,6 +179,22 @@ export default function StableUnsafeHtml({
180179
}
181180
}
182181
}
182+
183+
// This below sort of makes it so in some cases scrolling still works.
184+
// It's flaky and not great, but perhaps the best we can do.
185+
// It breaks clicking on the html for 1s after scrolling it.
186+
let timer: any = 0;
187+
//elt.addEventListener("wheel", () => {
188+
elt.style.pointerEvents = "none";
189+
if (timer) {
190+
clearTimeout(timer);
191+
}
192+
timer = setTimeout(() => {
193+
timer = 0;
194+
// Re-enable pointer events after the scroll
195+
elt.style.pointerEvents = "auto";
196+
}, 1000);
197+
// });
183198
}
184199
}, []);
185200

@@ -192,6 +207,20 @@ export default function StableUnsafeHtml({
192207
elt.process_smc_links();
193208
// @ts-ignore
194209
elt.katex({ preProcess: true });
210+
211+
let timer: any = 0;
212+
const elt0 = elt[0];
213+
elt0.addEventListener("wheel", () => {
214+
elt0.style.pointerEvents = "none";
215+
if (timer) {
216+
clearTimeout(timer);
217+
}
218+
timer = setTimeout(() => {
219+
timer = 0;
220+
elt0.style.pointerEvents = "auto";
221+
}, 2000);
222+
});
223+
195224
cache.set(globalKey, elt);
196225
$("body").append(elt);
197226
return elt;

0 commit comments

Comments
 (0)