2024-01-22 21:59:59 -05:00
|
|
|
import React, { ReactNode, useState } from "react";
|
2024-01-21 21:11:38 -05:00
|
|
|
import "./Editor.css";
|
|
|
|
import { Highlight } from "./highlight";
|
2024-01-22 21:59:59 -05:00
|
|
|
import { buildShadow } from "./shadow";
|
2024-01-23 21:27:12 -05:00
|
|
|
import OrgAst from "./OrgAst";
|
2024-01-12 22:07:59 -05:00
|
|
|
|
2024-01-21 21:11:38 -05:00
|
|
|
function Editor({ defaultValue = "I have a text value." }) {
|
2024-01-12 21:42:12 -05:00
|
|
|
function handleChange(event: React.ChangeEvent<HTMLTextAreaElement>) {
|
2024-01-12 23:15:06 -05:00
|
|
|
setValue(event.target.value);
|
2024-01-12 21:42:12 -05:00
|
|
|
}
|
|
|
|
|
2024-01-12 23:15:06 -05:00
|
|
|
const [value, setValue] = useState(defaultValue);
|
|
|
|
|
2024-01-21 15:51:40 -05:00
|
|
|
const [highlights, setHighlights] = useState<Array<Highlight>>([]);
|
|
|
|
|
|
|
|
function addHighlight(start: number, end: number) {
|
2024-01-22 21:59:59 -05:00
|
|
|
let new_highlights = [...highlights, new Highlight(start, end)];
|
|
|
|
new_highlights.sort(function (a,b) {
|
|
|
|
if (a.start < b.start) return -1;
|
|
|
|
if (a.start > b.start) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
setHighlights(new_highlights);
|
2024-01-21 15:51:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearHighlights() {
|
|
|
|
setHighlights([]);
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:52:40 -05:00
|
|
|
if (highlights.length === 0) {
|
2024-01-21 21:11:38 -05:00
|
|
|
addHighlight(1, 5);
|
2024-01-21 18:52:40 -05:00
|
|
|
}
|
|
|
|
|
2024-01-12 21:42:12 -05:00
|
|
|
return (
|
2024-01-12 22:07:59 -05:00
|
|
|
<div className="Editor">
|
2024-01-12 22:16:31 -05:00
|
|
|
<div className="Editor-textwrapper">
|
2024-01-21 21:11:38 -05:00
|
|
|
<textarea
|
|
|
|
onChange={handleChange}
|
|
|
|
className="Editor-textarea"
|
|
|
|
value={value}
|
|
|
|
/>
|
2024-01-22 21:59:59 -05:00
|
|
|
<div className="Editor-underlay">{buildShadow(highlights, value)}</div>
|
2024-01-12 22:16:31 -05:00
|
|
|
</div>
|
2024-01-23 21:27:12 -05:00
|
|
|
<OrgAst addHighlight={addHighlight} value={value} />
|
2024-01-12 22:07:59 -05:00
|
|
|
</div>
|
2024-01-12 21:42:12 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Editor;
|