-
-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useLayoutEffect, useRef, CSSProperties } from 'react'; | ||
import useIsBrowser from '@docusaurus/useIsBrowser'; | ||
import Head from '@docusaurus/Head'; | ||
|
||
import useScripts from '@site/src/hooks/useScripts'; | ||
import './styles.css'; | ||
|
||
const replReady = () => { | ||
return ( | ||
globalThis.jQuery && | ||
globalThis.jQuery.terminal | ||
); | ||
} | ||
|
||
import { initTerminal, destroyTerminal } from './terminal'; | ||
|
||
export interface TerminalProps extends CSSProperties { | ||
'--rows': number; | ||
} | ||
|
||
export default function Interpreter(): JSX.Element { | ||
const ref = useRef<HTMLDivElement>(); | ||
|
||
const isProd = process.env.NODE_ENV === 'production'; | ||
const isBrowser = useIsBrowser(); | ||
const isStatic = isProd && !isBrowser && !globalThis.jQuery; | ||
|
||
useScripts(!globalThis.jQuery && [ | ||
'https://cdn.jsdelivr.net/npm/jquery', | ||
'https://cdn.jsdelivr.net/combine/npm/jquery.terminal/js/jquery.terminal.min.js,npm/js-polyfills/keyboard.js' | ||
]); | ||
|
||
useLayoutEffect(() => { | ||
(function loop() { | ||
if (replReady() && styleReady()) { | ||
initTerminal(); | ||
} else { | ||
setTimeout(loop, 100); | ||
} | ||
})(); | ||
return destroyTerminal; | ||
}, []); | ||
|
||
function styleReady() { | ||
// hack to prevent initalizaing of jQuery Terminal before style is loaded | ||
return !!getComputedStyle(ref.current).getPropertyValue('--base-background'); | ||
} | ||
|
||
const terminalStyle = { | ||
'--rows': 15 | ||
} as TerminalProps; | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<link rel="preconnect" href="https://cdn.jsdelivr.net" /> | ||
<link href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet"/> | ||
{isStatic && <script src="https://cdn.jsdelivr.net/npm/jquery" />} | ||
{isStatic && <script src="https://cdn.jsdelivr.net/combine/npm/jquery.terminal/js/jquery.terminal.min.js,npm/js-polyfills/keyboard.js" />} | ||
</Head> | ||
<div className="terminal marker" ref={ref}></div> | ||
<div className="term" style={terminalStyle} /> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.term { | ||
--background: white; | ||
--color:#14232d; | ||
} | ||
[data-theme='dark'] .term { | ||
--background: #14232d; | ||
--color: white; | ||
} | ||
.terminal.marker { | ||
position: absolute; | ||
left: -9999999999px; | ||
} | ||
.term { | ||
--rows: 11; | ||
text-align: left; | ||
resize: vertical; | ||
overflow: hidden; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function initTerminal() { | ||
const $ = globalThis.$; | ||
const $term = $('.term'); | ||
$term.empty(); | ||
|
||
$term.terminal(); | ||
|
||
return $term; | ||
}; | ||
|
||
export function destroyTerminal() { | ||
const $ = globalThis.$; | ||
const $term = $('.term'); | ||
$term.terminal().destroy(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useEffect } from 'react'; | ||
|
||
function getScript(script: string) { | ||
return new Promise((resolve, reject) => { | ||
const $script = document.createElement("script"); | ||
$script.onload = resolve; | ||
$script.onerror = reject; | ||
$script.src = script; | ||
document.head.appendChild($script); | ||
}); | ||
} | ||
|
||
export default function useScripts(scripts?: string[]) { | ||
useEffect(() => { | ||
(function loop() { | ||
if (scripts?.length) { | ||
const script = scripts.shift(); | ||
getScript(script).then(loop); | ||
} | ||
})(); | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters