> }) => {
const { setVisibility } = props;
if (isXS()) {
return (
{
setVisibility('visible');
}}>
)
}
return (
}
onClick={() => {
setVisibility('visible');
}}
>
Share
)
}
const GitHubLink = () => {
if (isXS()) {
return (
)
}
return (
)
};
const TabPanel = (props: { children: any, value: number, index: number }) => {
const { children, value, index, ...other } = props;
return (
{value === index && (
{children}
)}
);
}
const TerminalComponent = (v: any) => {
const terminalRef = useRef(null)
const [terminalInstance, setTerminalInstance] = useState(null)
const fitAddon = new FitAddon();
const out = v.out;
useEffect(() => {
const instance = new Terminal({
cursorInactiveStyle: 'none',
cursorStyle: 'bar',
letterSpacing: 4,
fontFamily: 'monospace',
fontSize: 16,
fontWeightBold: 'bold',
convertEol: true,
theme: {
background: themeBlack,
cursor: themeBlack,
brightRed: '#da433a',
},
});
instance.loadAddon(fitAddon);
if (terminalRef.current) {
instance.open(terminalRef.current)
}
setTerminalInstance(instance)
return () => {
instance.dispose()
setTerminalInstance(null)
}
}, [
terminalRef,
]);
useEffect(() => {
if (!terminalInstance) {
return;
}
terminalInstance.loadAddon(fitAddon);
fitAddon.fit();
terminalInstance.clear();
terminalInstance.writeln(out);
const handleResize = () => fitAddon.fit()
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize);
}
}, [terminalRef, terminalInstance, out]);
return (
)
}
const CustomTooltip = styled(({ className, ...props }: TooltipProps) => (
))({
[`& .${tooltipClasses.tooltip}`]: {
fontSize: 16,
whiteSpace: 'pre-wrap',
},
});
const EmptyTokenContainer = () => {
return (
)
};
const TokenContainer = ({ children }: { children: any }) => {
return (
{children}
)
};
const Lexer = (v: any) => {
if (!v.tokens) {
return
}
const tokens = v.tokens as Token[];
return (
<>
{
tokens.map((tk, idx) => {
if (tokens.length > idx + 1) {
return tokenToComponent(tk, tokens[idx + 1]);
}
return tokenToComponent(tk, null)
})
}
>
)
};
const groupedTokenToComponent = (g: GroupedToken, groups: string[]) => {
if (g.token) {
return tokenToComponentWithoutTip(g.token);
}
return groupTokenToComponent(g.group, groups);
};
const tokenGroupToKey = (g: TokenGroup): string => {
return g.type + g.tokens.map(tk => groupedTokenToKey(tk)).join('-');
};
const groupedTokenToKey = (g: GroupedToken): string => {
if (g.group) {
return tokenGroupToKey(g.group)
}
return `${g.token.offset}`
}
const groupTokenToComponent = (g: TokenGroup, groups: string[]) => {
if (!g) {
return <>>
}
const groupColor = () => {
switch (g.type) {
case "directive":
return "dimgray";
case "directive_name":
return "orange"
case "document":
return "hotpink";
case "document_body":
return "olive"
case "anchor":
return "deepskyblue";
case "anchor_name":
return "brown"
case "alias":
return "limegreen";
case "literal":
return "gold"
case "folded":
return "gold"
case "scalar_tag":
return "blueviolet";
case "map_key":
return "coral";
case "map_key_value":
return "teal"
}
return 'white';
}
const newGroups = [...groups, g.type];
return (
')}
>
{
g.tokens.map(tk => {
return groupedTokenToComponent(tk, newGroups);
})
}
)
};
const tokenToComponent = (tk: Token, nextTk: Token | null) => {
if (!tk) {
return <>>
}
const orgs = tk.origin.split('\n').map((v, idx) => {
if (idx > 0) {
return ["\n", v];
}
return v;
});
const color = () => {
if (nextTk && nextTk.type === 'MappingValue') {
return '#008b8b';
}
switch (tk.type) {
case "String":
return '#ff7f50';
}
return 'black';
};
const prop = `type: ${tk.type}
origin: ${JSON.stringify(tk.origin)}
value: ${JSON.stringify(tk.value)}
line: ${tk.line}
column: ${tk.column}
`
return (
{
orgs.map((v, index) => {
return (
{v}
)
})
}
)
};
const tokenToComponentWithoutTip = (tk: Token) => {
if (!tk) {
return <>>
}
const orgs = tk.origin.split('\n').map((v, idx) => {
if (idx > 0) {
return ["\n", v];
}
return v;
});
return (
{
orgs.map((v, index) => {
return (
{v}
)
})
}
)
};
const ParserGroup = (v: any) => {
if (!v.tokens) {
return
}
const tokens = v.tokens as GroupedToken[];
return (
{
tokens.map(tk => { return groupedTokenToComponent(tk, []); })
}
)
};
const AST = (v: any) => {
if (!v || !v.svg) {
return
}
const parser = new DOMParser()
const dom = parser.parseFromString(v.svg, 'text/xml');
const g = dom.getElementById('graph0');
if (!g) {
return
}
const viewBox = g.parentElement!.getAttribute('viewBox')!;
return (
)
}
function App() {
const [content, setContent] = useState('');
const editorRef = useRef(null);
const workerRef = useRef(null);
const [tokens, setTokens] = useState([]);
const [groupedTokens, setGroupedTokens] = useState([]);
const [out, setOut] = useState('');
const [svg, setSvg] = useState('');
const [tabIndex, setTabIndex] = useState(0);
useEffect(() => {
workerRef.current = new yamlWorker();
workerRef.current.onmessage = (event) => {
const v = event.data as YAMLProcessResult;
switch (v.type) {
case YAMLProcessResultType.Decode:
setOut(v.result as string);
break;
case YAMLProcessResultType.Lexer:
if (typeof v.result === 'string') {
console.error(v.result);
} else {
setTokens(v.result as Token[]);
}
break;
case YAMLProcessResultType.ParserGroup:
if (typeof v.result === 'string') {
console.error(v.result);
} else {
setGroupedTokens(v.result as GroupedToken[]);
}
break;
case YAMLProcessResultType.Parser:
setSvg(v.result as string);
break;
default:
break;
}
}
return () => {
workerRef.current?.terminate();
};
}, []);
const onCodeChange = () => {
if (!editorRef || !editorRef.current) {
return
}
const code = editorRef.current.getValue()!;
setContent(code);
if (workerRef.current) {
workerRef.current.postMessage({
code: code,
tabIndex: tabIndex,
});
}
};
loader.init().then((monaco) => {
monaco.editor.defineTheme('go-yaml-theme', {
base: 'vs-dark',
inherit: true,
rules: [],
colors: {
'editor.background': themeBlack,
'editor.selectionHighlightBorder': themeBlack,
'editor.lineHighlightBackground': themeBlack,
'editor.selectionBackground': themeBlack,
},
});
});
const search = window.location.search;
const yamlDataBinary = new URLSearchParams(search).get('content');
const onMount = (editor: editor.IStandaloneCodeEditor) => {
editorRef.current = editor;
if (yamlDataBinary) {
const code = atob(yamlDataBinary)
editor.setValue(code);
setContent(code);
if (workerRef.current) {
workerRef.current.postMessage({
code: code,
tabIndex: tabIndex,
});
}
}
};
const onTabChange = (_: React.SyntheticEvent, newValue: number) => {
setTabIndex(newValue);
if (workerRef.current) {
workerRef.current.postMessage({
code: content,
tabIndex: newValue,
});
}
};
const tabProps = (index: number) => {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
};
return (
<>
} iconPosition="end" style={{ marginLeft: 20 }} label="OUTPUT" {...tabProps(0)} />
} iconPosition="end" style={{ marginLeft: 0 }} label="AST" {...tabProps(1)} />
} iconPosition="end" style={{ marginLeft: 0 }} label="GROUPED TOKENS" {...tabProps(2)} />
>
)
}
export default App