diff options
author | terminaldweller <thabogre@gmail.com> | 2022-03-24 12:36:31 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-03-24 12:36:31 +0000 |
commit | 7679fce6563bccd9d3c77038477671ba950d1481 (patch) | |
tree | 5c22115becbb37293e98eeb1fb32115e951d33fb /spring-front/src/components/Left.js | |
parent | wip (diff) | |
download | mdrtl-7679fce6563bccd9d3c77038477671ba950d1481.tar.gz mdrtl-7679fce6563bccd9d3c77038477671ba950d1481.zip |
wip
Diffstat (limited to 'spring-front/src/components/Left.js')
-rw-r--r-- | spring-front/src/components/Left.js | 127 |
1 files changed, 77 insertions, 50 deletions
diff --git a/spring-front/src/components/Left.js b/spring-front/src/components/Left.js index d55945d..3ab24e3 100644 --- a/spring-front/src/components/Left.js +++ b/spring-front/src/components/Left.js @@ -1,71 +1,98 @@ import React from "react"; +import ReactDOM from "react-dom"; import hljs from "highlight.js/lib/core"; -import markdown from "highlight.js/lib/languages/markdown"; import "highlight.js/styles/devibeans.css"; +import markdown from "highlight.js/lib/languages/markdown.js"; import "../index.css"; hljs.registerLanguage("markdown", markdown); -// https://css-tricks.com/creating-an-editable-textarea-that-supports-syntax-highlighted-code/ -function update(text) { - let result_element = document.querySelector("#highlighting-content"); - if (text[text.length - 1] == "\n") { - text += " "; +class Code extends React.Component { + constructor(props) { + super(props); } - result_element.innerHTML = text - .replace(new RegExp("&", "g"), "&") - .replace(new RegExp("<", "g"), "<"); - // Prism.highlightElement(result_element); - // return result_element; -} -function sync_scroll(element) { - let result_element = document.querySelector("#highlighting"); - result_element.scrollTop = element.scrollTop; - result_element.scrollLeft = element.scrollLeft; + render() { + return ( + <pre id="highlight" aria-hidden="true"> + <code id="highlight-content" class="language-markdown"></code> + </pre> + ); + } } -function check_tab(element, event) { - let code = element.value; - if (event.key == "Tab") { - event.preventDefault(); - let before_tab = code.slice(0, element.selectionStart); - let after_tab = code.slice(element.selectionEnd, element.value.length); - let cursor_pos = element.selectionEnd + 1; - element.value = before_tab + "\t" + after_tab; - element.selectionStart = cursor_pos; - element.selectionEnd = cursor_pos; - update(element.value); +class Editor extends React.Component { + constructor(props) { + super(props); + this.handleInput = this.handleInput.bind(this); + this.handleScroll = this.handleScroll.bind(this); + this.handleKeyDown = this.handleKeyDown.bind(this); + this.handleChange = this.handleChange.bind(this); + this.state = { value: "" }; + } + handleChange(event) { + this.setState({ value: event.target.value }); } -} -function CreateTextArea() { - return ( - <textarea - spellcheck="false" - name="editor" - className="editor" - id="editor" - onInput={(this.update, this.sync_scroll)} - onScroll={this.sync_scroll} - onKeyDown={this.check_tab} - ></textarea> - ); -} + handleInput(event) { + let text = this.state; + let result_element = document.querySelector("#highlight-content"); + if (text[text.length - 1] == "\n") { + text += " "; + } + result_element.innerHTML = text + .toString() + .replace(new RegExp("&", "g"), "&") + .replace(new RegExp("<", "g"), "<"); + let pretty = hljs.highlight(this.state, { languages: "markdown" }); + console.log("ugly:", this.state); + console.log("pretty:", pretty); + let result_element_2 = document.querySelector("#highlight"); + result_element_2.scrollTop = event.currentTarget.scrollTop; + result_element_2.scrollLeft = event.currentTarget.scrollLeft; + } -function CreatePreCode() { - return ( - <pre id="highlight" aria-hidden="true"> - <code className="language-markdown" id="highlight-content"></code> - </pre> - ); + handleScroll(event) { + let result_element = document.querySelector("#highlight"); + result_element.scrollTop = event.currentTarget.scrollTop; + result_element.scrollLeft = event.currentTarget.scrollLeft; + } + + handleKeyDown(event) { + let element = event.currentTarget; + let code = this.state; + if (event.key == "Tab") { + event.preventDefault(); + let before_tab = code.slice(0, element.selectionStart); + let after_tab = code.slice(element.selectionEnd, element.value.length); + let cursor_pos = element.selectionEnd + 1; + element.value = before_tab + "\t" + after_tab; + element.selectionStart = cursor_pos; + element.selectionEnd = cursor_pos; + this.handleInput(element.value, element); + } + } + + render() { + return ( + <textarea + spellcheck="false" + name="editor" + className="editor" + id="editor" + onInput={this.handleInput.bind(this)} + onScroll={this.handleScroll.bind(this)} + onKeyDown={this.handleKeyDown.bind(this)} + ></textarea> + ); + } } export default function Left() { return ( - <div class="split left"> - <CreateTextArea /> - <CreatePreCode /> + <div> + <Editor /> + <Code /> </div> ); } |