Skip to content

Commit 4d87799

Browse files
author
Connor
committed
build out ui
1 parent d09eb79 commit 4d87799

File tree

8 files changed

+14792
-59
lines changed

8 files changed

+14792
-59
lines changed

package-lock.json

+14,495
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"compromise": "^13.1.1",
10+
"node-sass": "^4.13.1",
911
"react": "^16.13.1",
1012
"react-dom": "^16.13.1",
13+
"react-markdown": "^4.3.1",
1114
"react-scripts": "3.4.1"
1215
},
1316
"scripts": {

public/index.html

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8" />
5-
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6-
<meta name="viewport" content="width=device-width, initial-scale=1" />
7-
<meta name="theme-color" content="#000000" />
8-
<meta
9-
name="description"
10-
content="Web site created using create-react-app"
11-
/>
12-
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13-
<!--
3+
<head>
4+
<meta charset="utf-8"/>
5+
<link href="%PUBLIC_URL%/favicon.ico" rel="icon"/>
6+
<meta content="width=device-width, initial-scale=1" name="viewport"/>
7+
<meta content="#ffffff" name="theme-color"/>
8+
<meta content="natural language parsing + markdown" name="description"/>
9+
<link href="%PUBLIC_URL%/logo192.png" rel="apple-touch-icon"/>
10+
<!--
1411
manifest.json provides metadata used when your web app is installed on a
1512
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
1613
-->
17-
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18-
<!--
14+
<link href="%PUBLIC_URL%/manifest.json" rel="manifest"/>
15+
<!--
1916
Notice the use of %PUBLIC_URL% in the tags above.
2017
It will be replaced with the URL of the `public` folder during the build.
2118
Only files inside the `public` folder can be referenced from the HTML.
@@ -24,12 +21,12 @@
2421
work correctly both with client-side routing and a non-root public URL.
2522
Learn how to configure a non-root public URL by running `npm run build`.
2623
-->
27-
<title>React App</title>
28-
</head>
29-
<body>
30-
<noscript>You need to enable JavaScript to run this app.</noscript>
31-
<div id="root"></div>
32-
<!--
24+
<title>NLP Markdown</title>
25+
</head>
26+
<body>
27+
<noscript>You need to enable JavaScript to run this app.</noscript>
28+
<div id="app"></div>
29+
<!--
3330
This HTML file is a template.
3431
If you open it directly in the browser, you will see an empty page.
3532
@@ -39,5 +36,5 @@
3936
To begin the development, run `npm start` or `yarn start`.
4037
To create a production bundle, use `npm run build` or `yarn build`.
4138
-->
42-
</body>
39+
</body>
4340
</html>

src/App.js

+126-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,131 @@
11
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
2+
import nlp from 'compromise'
3+
const ReactMarkdown = require('react-markdown')
44

5-
function App() {
5+
const CorpusContext = React.createContext()
6+
7+
let currentWord = -1
8+
9+
let Line = (props) => {
10+
const corpusContext = React.useContext(CorpusContext)
11+
let words
12+
13+
if(!corpusContext.parseEntire) {
14+
const dump = nlp(props.children)
15+
words = dump.words().map(word => ({
16+
text: word.text ? word.text().trim() : "",
17+
id: word.list[0] ? word.list[0].start : "",
18+
raw: "",
19+
tags: word.json() ? word.json()[0].terms[0].tags : []
20+
}))
21+
}else {
22+
words = nlp.tokenize(props.children).terms().map(word => ({
23+
text: word.text ? word.text().trim(): "",
24+
id: word.list[0] ? word.list[0].start : "",
25+
raw: word.json() ? word.json() : "",
26+
tags: []
27+
}))
28+
}
29+
// console.log("words:", words)
30+
if(words.world) words = []
31+
32+
return (
33+
<>
34+
{
35+
words
36+
.filter(word => word.text !== "")
37+
.map(word => <Word key={word.id} raw={word.raw} tags={word.tags}>{ word.text }</Word>)
38+
}
39+
</>
40+
)
41+
}
42+
43+
let Word = (props) => {
44+
const corpusContext = React.useContext(CorpusContext)
45+
let tagString
46+
47+
if(!corpusContext.parseEntire) {
48+
tagString = props.tags ? props.tags.join(" ") : ""
49+
}else {
50+
currentWord++
51+
const thisWordIndex = currentWord
52+
const thisWordData = corpusContext.terms[thisWordIndex]
53+
54+
tagString = thisWordData ? thisWordData.terms.map(term => term.tags).reduce((acc, val) => {
55+
return acc.concat(val)
56+
}, []).join(" ") : ""
57+
}
58+
59+
// console.log(props.children, props.raw, corpusContext[thisWord])
60+
61+
return (
62+
<span className={"word " + tagString}>
63+
<span className="word__content">
64+
{props.children}
65+
</span>
66+
<span>&nbsp;</span>
67+
<span className="word__hover">
68+
{tagString}
69+
</span>
70+
</span>
71+
)
72+
}
73+
74+
let Renderer = (props) => {
75+
// console.log(props)
76+
// const corpusData = React.useContext(CorpusContext)
77+
// console.log("corpusData:", corpusData)
78+
// corpusData.debug()
79+
80+
return (
81+
<>
82+
{props.children}
83+
</>
84+
)
85+
}
86+
87+
let Editor = () => {
88+
const [rawContent, setRawContent] = React.useState(localStorage.getItem("content") ? localStorage.getItem("content") : "")
89+
const [corpusData, setCorpusData] = React.useState(nlp(""))
90+
const [parseEntire, setParseEntire] = React.useState(true)
91+
92+
let handleInputChange = (e) => {
93+
localStorage.setItem("content", e.target.value)
94+
setRawContent(e.target.value)
95+
}
96+
97+
let handleParseChange = (e) => {
98+
currentWord = -1
99+
setParseEntire(!parseEntire)
100+
}
101+
102+
React.useEffect(() => {
103+
// get innerText || textContent of #renderer
104+
let renderer = document.getElementById("renderer")
105+
let plainText = renderer.textContent || renderer.innerText
106+
107+
currentWord = -1
108+
// console.log(plainText)
109+
// set corpusData to nlp() of innerText
110+
const dump = nlp(plainText)
111+
console.log(dump)
112+
setCorpusData(dump.terms().json())
113+
}, [rawContent])
114+
6115
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
23-
);
116+
<>
117+
<textarea onChange={handleInputChange} value={localStorage.getItem("content") ? localStorage.getItem("content") : ""}></textarea>
118+
{/* <button onClick={handleParseChange}>Parse Mode: {parseEntire ? "Entire" : "Text"}</button> */}
119+
<div id="renderer">
120+
<CorpusContext.Provider value={{terms: corpusData, parseEntire: parseEntire}}>
121+
<ReactMarkdown source={rawContent} rawSourcePos={true} renderers={{
122+
text: Line,
123+
root: Renderer
124+
}} />
125+
</CorpusContext.Provider>
126+
</div>
127+
</>
128+
)
24129
}
25130

26-
export default App;
131+
export default Editor;

src/App.css src/App.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
$blue: #61dafb;
2+
13
.App {
24
text-align: center;
35
}
@@ -25,7 +27,7 @@
2527
}
2628

2729
.App-link {
28-
color: #61dafb;
30+
color: $blue;
2931
}
3032

3133
@keyframes App-logo-spin {

src/index.css

-13
This file was deleted.

src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import './index.css';
4-
import App from './App';
3+
import './index.scss';
4+
import Editor from './App';
55
import * as serviceWorker from './serviceWorker';
66

77
ReactDOM.render(
88
<React.StrictMode>
9-
<App />
9+
<Editor />
1010
</React.StrictMode>,
11-
document.getElementById('root')
11+
document.getElementById('app')
1212
);
1313

1414
// If you want your app to work offline and load faster, you can change

0 commit comments

Comments
 (0)