Skip to content

Commit

Permalink
Wait until component succesfully mounts to remove script tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshvarun committed Aug 31, 2023
1 parent 1873c66 commit 2a99fc2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
4 changes: 0 additions & 4 deletions jest.config.js

This file was deleted.

48 changes: 24 additions & 24 deletions src/blog-cells-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,34 @@ declare var Babel: any;
let MODULE_KEYS: string[] = [];

Babel.registerPlugin("autoprefixer", ({ types }) => {
return {
visitor: {
ReferencedIdentifier: (path, state) => {
// Skip if this variable has been bound.
const name = path.node.name;
if (path.scope.hasBinding(name)) return;

// Skip if this variable is not defined on the module.
if (!MODULE_KEYS.includes(name)) return;

// Replace the identifier with a lookup.
path.replaceWith(
types.memberExpression(
types.identifier("$"),
types.identifier(name),
false
)
);
},
return {
visitor: {
ReferencedIdentifier: (path, state) => {
// Skip if this variable has been bound.
const name = path.node.name;
if (path.scope.hasBinding(name)) return;

// Skip if this variable is not defined on the module.
if (!MODULE_KEYS.includes(name)) return;

// Replace the identifier with a lookup.
path.replaceWith(
types.memberExpression(
types.identifier("$"),
types.identifier(name),
false
)
);
},
};
},
};
});

function transform(code: string, moduleKeys: string[] = []): string {
MODULE_KEYS = moduleKeys;
return Babel.transform(code, {
plugins: ["autoprefixer"],
}).code!;
MODULE_KEYS = moduleKeys;
return Babel.transform(code, {
plugins: ["autoprefixer"],
}).code!;
}

function formatArgs(args: any[]): string {
Expand Down
2 changes: 1 addition & 1 deletion src/blog-cells.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ script[type="text/notebook-cell"] {

margin-top: 25px;
margin-bottom: 25px;
}
}
20 changes: 17 additions & 3 deletions src/blog-cells.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Cell extends React.Component<
code: string;
autoRun: boolean;
hideable: boolean;
onMount?: () => void;
},
any
> {
Expand Down Expand Up @@ -144,6 +145,10 @@ class Cell extends React.Component<
],
});
editors.push(this.codeMirror);

if (this.props.onMount) {
this.props.onMount();
}
}

async run(code) {
Expand Down Expand Up @@ -207,17 +212,26 @@ domLoaded.then(() => {
const scripts = document.querySelectorAll(
"script[type='text/notebook-cell'], pre.notebook-cell"
) as NodeListOf<HTMLScriptElement>;

for (const script of scripts) {
const code = script.textContent?.trim() || "";
const autoRun = script.dataset.autorun === "true";
const hidden = script.dataset.hidden === "true";

const editor = document.createElement("div");
script.after(editor);
script.remove();

const root = ReactDOM.createRoot(editor);
root.render(<Cell code={code} autoRun={autoRun} hideable={hidden} />);
root.render(
<Cell
code={code}
autoRun={autoRun}
hideable={hidden}
onMount={() => {
// Remove the script tag once the cell succesfully mounts.
script.remove();
}}
/>
);
}
});

0 comments on commit 2a99fc2

Please sign in to comment.