Separate components into SourceViewer and CompletionViewer

This commit is contained in:
2020-09-07 16:48:26 +05:30
parent a1090de269
commit c63d957d0f
8 changed files with 58 additions and 70 deletions
-38
View File
@@ -1,38 +0,0 @@
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
+13 -32
View File
@@ -1,45 +1,26 @@
import React from "react"; import React, { useState, useLayoutEffect } from "react";
import Highlight, { defaultProps } from "prism-react-renderer";
import { sourceMapping } from "./logic/sourceMapping"; import { SourceViewer } from "./components/SourceViewer";
import { CompletionViewer } from "./components/CompletionViewer";
import { completionMapping } from "./logic/completionMapping";
import { runEngine } from "./logic/runEngine";
import "./tailwind.output.css"; import "./tailwind.output.css";
import "./App.css"; import "./App.css";
const exampleCode = `var test = \`asd const sampleInputCode = `var test = \`asd
World!\`; abc()`; World!\`; abc()`;
function App() { function App() {
const [inputCode, setInputCode] = useState<string>(sampleInputCode);
useLayoutEffect(() => {
runEngine(inputCode);
}, [inputCode]);
return ( return (
<div> <div>
<Highlight {...defaultProps} code={exampleCode} language="jsx"> <SourceViewer code={inputCode} onChangeCode={setInputCode} />
{({ className, style, tokens, getLineProps, getTokenProps }) => (
sourceMapping.reset(),
(
<pre className={className} style={style}>
{tokens.map((line, i) => (
<div
data-line={JSON.stringify(line)}
{...getLineProps({ line, key: i })}
>
{line.map(function (token, key) {
const refFn = sourceMapping.registerToken(token);
return (
<span
ref={refFn}
data-token={JSON.stringify(token)}
{...getTokenProps({ token, key })}
/>
);
})}
{(sourceMapping.registerLineEnd(), null)}
</div>
))}
</pre>
)
)}
</Highlight>
</div> </div>
); );
} }
@@ -0,0 +1 @@
export function CompletionViewer() {}
+3
View File
@@ -0,0 +1,3 @@
import { CompletionViewer } from "./CompletionViewer";
export { CompletionViewer };
@@ -0,0 +1,38 @@
import React from "react";
import Highlight, { defaultProps } from "prism-react-renderer";
import { sourceMapping } from "../../logic/sourceMapping";
import "./SourceViewer.css";
interface ISourceViewerProps {
code: string;
onChangeCode(newCode: string): void;
}
export function SourceViewer(props: ISourceViewerProps) {
const { code } = props;
return (
<Highlight {...defaultProps} code={code} language="jsx">
{({ className, style, tokens, getLineProps, getTokenProps }) => (
// eslint-disable-next-line no-sequences
sourceMapping.reset(),
(
<pre className={className} style={style}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{line.map(function (token, key) {
const refFn = sourceMapping.registerToken(token);
return (
<span ref={refFn} {...getTokenProps({ token, key })} />
);
})}
{(sourceMapping.registerLineEnd(), null)}
</div>
))}
</pre>
)
)}
</Highlight>
);
}
+3
View File
@@ -0,0 +1,3 @@
import { SourceViewer } from "./SourceViewer";
export { SourceViewer };