console
const submit = document.getElementById("submit");
const output = document.querySelector("#output ul");
const input = document.getElementById("input_area");
class Line {
constructor(data) {
this.type = data.type;
}
create() {
const frag = document.createDocumentFragment();
const dom = document.createElement('li');
dom.setAttribute('class', `line ${this.type}`);
dom.innerHTML = this.html;
output.appendChild(dom);
}
}
class InLine extends Line {
constructor(data) {
super(data);
this.html = [
`<span class="line-deco">></span>`,
`<span class="line-text">${data.text}</span>`,
].join('');
}
}
class OutLine extends Line {
constructor(data) {
super(data);
this.html = [
`<span class="line-deco out"><▪</span>`,
`<span class="line-text">${data.text}</span>`,
].join('');
}
}
const fun = {
send(data) {
this.renderCalc(data);
let {text, type} = data;
let result = {};
try {
result = {
text: eval(text),
type: 'log'
};
} catch ({message}) {
result = {
text: message,
type: 'error'
};
}
this.renderResult(result);
},
renderCalc(data) {
const line = new InLine(data);
line.create();
},
renderResult(data) {
const line = new OutLine(data);
line.create();
}
}
submit.addEventListener("click", () => {
fun.send({
text: input.value,
type: 'input'
})
});
<div id="main">
<div class="box" id="output">
<ul>
</ul>
</div>
<div class="box" id="input">
<textarea name="" id="input_area"></textarea>
</div>
<div id="submit">Run</div>
</div>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#main {
width: 100%;
height: 100%;
}
#main .box {
border: 1px solid #e1e1e1;
box-sizing: border-box;
font-size: 15px;
}
#output {
height: 80vh;
}
#output ul {
padding: 10px 5px;
margin: 0;
}
#output .line {
list-style: none;
border-bottom: 1px dashed #e1e1e1;
line-height: 30px;
}
#output .line .line-deco {
font-weight: bold;
color: #0041d2;
margin: 0 10px 0 0px;
}
#output .line .line-deco.out {
color: #717171;
}
#output .line .line-text.log {
color: darkslategray;
}
#output .line .line-text.error {
color: red;
}
#output .line .line-text.info {
color: blue;
}
#input {
height: calc( 20vh - 40px);
}
#input textarea {
height: 100%;
width: 100%;
outline: none;
border-width: 0;
font-size: inherit;
}
#submit {
line-height: 40px;
vertical-align: middle;
background-color: darkcyan;
text-align: center;
font-weight: bold;
color: #ffffff;
}
#submit:hover {
background-color: #065d5d;
}