Skip to content

Commit 6379f73

Browse files
committed
really minimal proof of concept: this might work!
1 parent 14e5f75 commit 6379f73

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/packages/frontend/jupyter/output-messages/immortal.tsx renamed to src/packages/frontend/jupyter/output-messages/immortal-dom-node.tsx

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
/*
2+
Create an immortal DOM node. This is a way to render HTML that stays stable
3+
irregardless of it being unmounted/remounted.
4+
This supports virtualization, window splitting, etc., without loss of state.
5+
*/
6+
17
import { useCallback, useEffect, useRef } from "react";
28
import $ from "jquery";
39

4-
// This is just an initial default height; the actual height of the iframe should
10+
// This is just an initial default height; the actual height of the should
511
// resize to the content.
6-
const HEIGHT = "70vh";
12+
const HEIGHT = "50vh";
713

814
interface Props {
9-
key: string;
15+
globalKey: string;
1016
html: string;
17+
zIndex?: number;
1118
}
1219

13-
const immortals: { [key: string]: any } = {};
20+
const immortals: { [globalKey: string]: any } = {};
21+
22+
const Z_INDEX = 1;
1423

15-
export default function Immortal({ key, html }: Props) {
24+
export default function ImmortalDomNode({
25+
globalKey,
26+
html,
27+
zIndex = Z_INDEX, // todo: support changing?
28+
}: Props) {
1629
const divRef = useRef<any>(null);
1730
const eltRef = useRef<any>(null);
1831
const intervalRef = useRef<any>(null);
@@ -45,17 +58,18 @@ export default function Immortal({ key, html }: Props) {
4558
return;
4659
}
4760
let elt;
48-
if (immortals[key] == null) {
49-
elt = immortals[key] = $(
50-
`<div style="border:0;overflow:hidden;width:100%;height:${HEIGHT};position:absolute;left:130px"/>${html}</div>`,
61+
if (immortals[globalKey] == null) {
62+
elt = immortals[globalKey] = $(
63+
`<div id="${globalKey}" style="border:0;overflow:hidden;width:100%;height:${HEIGHT};position:absolute;left:130px;z-index:${zIndex}"/>${html}</div>`,
5164
);
5265
$("body").append(elt);
5366
} else {
54-
elt = immortals[key];
67+
elt = immortals[globalKey];
5568
elt.show();
5669
}
5770
eltRef.current = elt[0];
58-
intervalRef.current = setInterval(position, 500);
71+
intervalRef.current = setInterval(position, 1000);
72+
position();
5973

6074
return () => {
6175
// unmounting so hide
@@ -66,5 +80,10 @@ export default function Immortal({ key, html }: Props) {
6680
};
6781
}, []);
6882

69-
return <div ref={divRef} />;
83+
return (
84+
<div
85+
ref={divRef}
86+
style={{ border: "1px solid black", height: HEIGHT }}
87+
></div>
88+
);
7089
}

src/packages/frontend/jupyter/output-messages/javascript.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { List } from "immutable";
77
import $ from "jquery";
8-
import React, { useRef, useState } from "react";
8+
import { useEffect, useRef, useState } from "react";
99
import { is_array } from "@cocalc/util/misc";
1010
import { javascript_eval } from "./javascript-eval";
1111
import ShowError from "@cocalc/frontend/components/error";
@@ -24,7 +24,7 @@ export const Javascript: React.FC<JavascriptProps> = (
2424

2525
const [errors, set_errors] = useState<string | undefined>(undefined);
2626

27-
React.useEffect(() => {
27+
useEffect(() => {
2828
if (value == null || node.current == null) {
2929
return;
3030
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import register from "./register";
2-
import HTML from "@cocalc/frontend/components/html-ssr";
2+
//import HTML from "@cocalc/frontend/components/html-ssr";
3+
import ImmortalDomNode from "../immortal-dom-node";
4+
import { sha1 } from "@cocalc/util/misc";
35

46
const Html = ({ value }: { value: string }) => {
7+
// <HTML value={value} />
8+
59
return (
610
<div style={{ margin: "5px 0" }}>
7-
<HTML value={value} />
11+
<ImmortalDomNode html={value} globalKey={sha1(value)} />
812
</div>
913
);
1014
};

0 commit comments

Comments
 (0)