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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
//@ts-check
/**
* Create The menubar
* @returns {HTMLMenuElement}
*/
export function createMenu() {
const header = document.createElement('header')
document.body.append(header)
const menu = document.createElement('menu')
header.append(menu)
menu.role = 'menubar'
return menu
}
/**
* @param {HTMLSelectElement} select
* @param {string} value
*/
function addSelectOption(select, value, text = value) {
const opt = document.createElement("option")
opt.textContent = text
opt.value = value
select.add(opt)
}
/**
* @param {HTMLMenuElement} menu
* @returns {HTMLLIElement}
*/
export function createMenuItem(menu) {
const menuItem = document.createElement('li')
menu.append(menuItem)
menuItem.role = 'menuitem'
return menuItem
}
export function createEditor() {
const editor = document.createElement('pre')
document.body.appendChild(editor)
editor.contentEditable = 'true'
editor.id = "editor"
return editor
}
/**
* @param {HTMLMenuElement} menu
* @param {HTMLPreElement} editor
*/
export function createFontSize(menu, editor) {
const menuitem = createMenuItem(menu)
const fontSize = document.createElement('select')
menuitem.appendChild(fontSize)
Object.entries({
['xx-small']: 'Very Very Small',
['x-small']: 'Very Very Small',
small: 'Small',
medium: 'Medium',
large: 'Large',
['x-large']: 'Very Large',
['xx-large']: 'Very Very Large',
['xxx-large']: 'Very Very Very Large',
}).forEach(([key, value]) => {
addSelectOption(fontSize, key, value)
})
fontSize.value = 'medium'
fontSize.addEventListener('change', async function () {
editor.style.fontSize = fontSize.value
})
}
/**
* @param {HTMLMenuElement} menu
*/
export function createPrinter(menu) {
const menuitem = createMenuItem(menu)
const print = document.createElement('button')
menuitem.appendChild(print)
print.textContent = 'Print'
print.addEventListener('click', async function () {
window.print()
})
}
/**
* @param {HTMLMenuElement} menu
* @param {HTMLPreElement} editor
* @param {HTMLInputElement} fileName
*/
export function createSaveFile(menu, editor, fileName) {
const menuitem = createMenuItem(menu)
const save = document.createElement('button')
menuitem.appendChild(save)
save.textContent = 'Save'
save.addEventListener('click',
async function () {
saveFile(editor.textContent ?? "", fileName.value)
}
)
}
/**
* @param {HTMLMenuElement} menu
* @param {HTMLPreElement} editor
* @param {HTMLInputElement} fileName
*/
export function createOpenFile(menu, editor, fileName) {
const menuitem = createMenuItem(menu)
const fileInput = document.createElement('input')
fileInput.type = 'file'
fileInput.id = 'fileInput'
const label = document.createElement('label')
label.textContent = 'Open'
label.htmlFor = fileInput.id
fileInput.addEventListener('change', async function () {
const files = this.files ?? new FileList()
const file = files[0]
editor.textContent = await file.text()
fileName.value = file.name
})
menuitem.appendChild(label)
menuitem.appendChild(fileInput)
}
/**
* @param {HTMLMenuElement} menu
*/
export async function createFileName(menu) {
const menuitem = createMenuItem(menu)
const fileName = document.createElement('input')
menuitem.append(fileName)
fileName.type = "text"
fileName.value = "untitled"
async function title() {
return document.title = `Text Editor - ${this.value}`
}
fileName.addEventListener("input", title)
await title.call(fileName)
return fileName
}
/**
* @param {BlobPart} content
* @param {string} filename
*/
function saveFile(content, filename) {
const blob = new Blob([content], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(url)
}
|