mirror of
https://github.com/bendtherules/completion-viz.git
synced 2026-08-18 13:42:51 +00:00
Add basic code view and sourceMapping
This commit is contained in:
+38
-17
@@ -1,24 +1,45 @@
|
|||||||
import React from 'react';
|
import React from "react";
|
||||||
import logo from './logo.svg';
|
import Highlight, { defaultProps } from "prism-react-renderer";
|
||||||
import './App.css';
|
|
||||||
|
import { sourceMapping } from "./sourceMapping";
|
||||||
|
|
||||||
|
import "./tailwind.output.css";
|
||||||
|
|
||||||
|
import "./App.css";
|
||||||
|
|
||||||
|
const exampleCode = `var test = \`asd
|
||||||
|
World!\`; abc()`;
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div>
|
||||||
<header className="App-header">
|
<Highlight {...defaultProps} code={exampleCode} language="jsx">
|
||||||
<img src={logo} className="App-logo" alt="logo" />
|
{({ className, style, tokens, getLineProps, getTokenProps }) => (
|
||||||
<p>
|
sourceMapping.reset(),
|
||||||
Edit <code>src/App.tsx</code> and save to reload.
|
(
|
||||||
</p>
|
<pre className={className} style={style}>
|
||||||
<a
|
{tokens.map((line, i) => (
|
||||||
className="App-link"
|
<div
|
||||||
href="https://reactjs.org"
|
data-line={JSON.stringify(line)}
|
||||||
target="_blank"
|
{...getLineProps({ line, key: i })}
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
>
|
||||||
Learn React
|
{line.map(function (token, key) {
|
||||||
</a>
|
const refFn = sourceMapping.registerToken(token);
|
||||||
</header>
|
return (
|
||||||
|
<span
|
||||||
|
ref={refFn}
|
||||||
|
data-token={JSON.stringify(token)}
|
||||||
|
{...getTokenProps({ token, key })}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{(sourceMapping.registerLineEnd(), null)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</pre>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</Highlight>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ body {
|
|||||||
sans-serif;
|
sans-serif;
|
||||||
-webkit-font-smoothing: antialiased;
|
-webkit-font-smoothing: antialiased;
|
||||||
-moz-osx-font-smoothing: grayscale;
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
font-size: 24px;
|
||||||
}
|
}
|
||||||
|
|
||||||
code {
|
code {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
|
|||||||
import './index.css';
|
import './index.css';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import * as serviceWorker from './serviceWorker';
|
import * as serviceWorker from './serviceWorker';
|
||||||
|
import { sourceMapping } from './sourceMapping';
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
@@ -11,6 +12,8 @@ ReactDOM.render(
|
|||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log(sourceMapping);
|
||||||
|
|
||||||
// If you want your app to work offline and load faster, you can change
|
// If you want your app to work offline and load faster, you can change
|
||||||
// unregister() to register() below. Note this comes with some pitfalls.
|
// unregister() to register() below. Note this comes with some pitfalls.
|
||||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export type Token = {
|
||||||
|
types: string[];
|
||||||
|
content: string;
|
||||||
|
empty?: boolean;
|
||||||
|
};
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { Token } from "./prism-types";
|
||||||
|
|
||||||
|
interface TMappingELement {
|
||||||
|
start: number;
|
||||||
|
end: number;
|
||||||
|
ref: { current: HTMLElement | null };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TSourceMapping {
|
||||||
|
mappingArray: Array<TMappingELement>;
|
||||||
|
lastEnd: number;
|
||||||
|
registerToken(t: Token): (ele: HTMLElement | null) => void;
|
||||||
|
registerLineEnd: () => void;
|
||||||
|
reset: () => void;
|
||||||
|
getElementsInRange(start: number, end: number): HTMLElement[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const sourceMapping: TSourceMapping = {
|
||||||
|
mappingArray: [],
|
||||||
|
lastEnd: 0,
|
||||||
|
registerToken(t: Token) {
|
||||||
|
const obj: TMappingELement = {
|
||||||
|
start: this.lastEnd,
|
||||||
|
end: this.lastEnd + t.content.length,
|
||||||
|
ref: { current: null },
|
||||||
|
};
|
||||||
|
this.mappingArray.push(obj);
|
||||||
|
this.lastEnd = this.mappingArray[this.mappingArray.length - 1].end;
|
||||||
|
|
||||||
|
console.log(obj, t);
|
||||||
|
|
||||||
|
|
||||||
|
return function updateHTMLElement(ele) {
|
||||||
|
obj.ref.current = ele;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
registerLineEnd() {
|
||||||
|
this.lastEnd += 1;
|
||||||
|
if (this.mappingArray.length > 0) {
|
||||||
|
this.mappingArray[this.mappingArray.length - 1].end = this.lastEnd;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
reset() {
|
||||||
|
this.mappingArray = [];
|
||||||
|
this.lastEnd = 0;
|
||||||
|
},
|
||||||
|
getElementsInRange(start, end) {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user