-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextField.js
65 lines (62 loc) · 1.76 KB
/
TextField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Component from "./Component.js"
import Input from "./Input.js"
export default class TextField extends Component {
createHTMLElement() {
this.container = document.createElement("textarea")
}
static events = {
textLoad : "textLoad"
}
addListeners() {
super.addListeners()
this.container.addEventListener("wheel", (evt) => {
this.onmousewheel(evt)
})
}
onmousewheel(evt) {
if (evt.shiftKey) {
evt.preventDefault()
const FONT_CHANGE = 0.03
let sign = evt.deltaY < 0 ? 1 : -1
let font_size = Math.max(0.25, this.getFontSize() + sign * FONT_CHANGE)
this.setFontSize(font_size)
}
}
loadText(text) {
this.container.value = text
this.dispatchEvent(TextField.events.textLoad)
}
getText() {
return this.container.value
}
clear() {
this.container.value = ""
return this
}
uploadFile() {
let loadFileInput = new Input()
.setType("file")
.addAttributes({ accept: ".txt" })
loadFileInput.addEventListener(Input.events.change, () => {
let reader = new FileReader()
reader.onload = (event) => {
this.loadText(event.target.result)
}
reader.readAsText(loadFileInput.getFile())
})
loadFileInput.build().getContainer().click()
}
saveAsTXT(filename) {
let text = this.getText()
if (text.length == 0) return
let blob = new Blob([text], { type: "text/txt" })
let url = URL.createObjectURL(blob)
let downloadLink = document.createElement("a")
downloadLink.href = url
downloadLink.download = `${filename}.txt`
document.body.appendChild(downloadLink)
downloadLink.click()
document.body.removeChild(downloadLink)
return true
}
}