From 7679fce6563bccd9d3c77038477671ba950d1481 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Thu, 24 Mar 2022 17:06:31 +0430 Subject: wip --- spring-front/src/components/Left.js | 127 ++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 50 deletions(-) (limited to 'spring-front/src/components') 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 ( + + ); + } } -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 ( - - ); -} + 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 ( - - ); + 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 ( + + ); + } } export default function Left() { return ( -
- - +
+ +
); } -- cgit v1.2.3