-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (165 loc) · 6.63 KB
/
script.js
File metadata and controls
203 lines (165 loc) · 6.63 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import * as opentype from "./modules/opentype.module.js";
import Font from "./modules/Font.js";
import ControlBar from "./modules/Controls.js";
import pangrams from "./modules/pangrams.js";
const mainEl = document.getElementById('content')
const landing = document.getElementById('landing')
const previewZone = document.getElementById('font-preview')
const userText = document.getElementById('userText')
const fontfaceContainer = document.getElementById('previewFont')
const fileOpener = document.getElementById('uploadfont')
HTMLElement.prototype.setAttributes = function(attrs){
for(var key in attrs) {
this.setAttribute(key, attrs[key]);
}
}
HTMLElement.prototype.appendChildren = function(...nodes){
for(const node of nodes){
this.appendChild(node)
}
}
const showErrorMessage = (err)=>{
console.log(err)
}
const getFileURL = (fontFile)=>{
return new Promise((res, rej)=>{
const reader = new FileReader();
reader.readAsDataURL(fontFile);
reader.onload = (e)=>{
res(e.target.result)
}
reader.onerror = rej
})
}
const setPreviewText = (str, state)=>{
userText.textContent = str;
state.currentText = str;
}
const displayFontData = ()=>{
if(!mainEl.querySelector('control-bar')){
const controlBar = new ControlBar()
mainEl.appendChild(controlBar)
userText.style.fontFamily = "preview, sans-serif";
userText.style.fontWeight = "unset";
landing.style.visibility = "hidden";
// const selectedfontLocale = currentFont.controls
const selectedLanguage = currentFont.selectedLanguage?.htmlTag || 'en';
const langTag = selectedLanguage in pangrams ? selectedLanguage : 'en'
const textIdx = Math.floor(Math.random() * pangrams[langTag].length)
setPreviewText(pangrams[langTag][textIdx], controlBar.controls.currentState)
return
}
const controlBar = mainEl.querySelector('control-bar');
controlBar.updateFont()
}
const setFontFace = (fontFace)=>{
const newStyle = document.createElement("style");
newStyle.appendChild(document.createTextNode(fontFace));
if(fontfaceContainer.children.length > 0){
fontfaceContainer.replaceChildren(newStyle)
return
}
fontfaceContainer.appendChild(newStyle);
}
const parseFont = async (fontFile)=>{
try {
const opentypeFont = opentype.parse(await fontFile.arrayBuffer())
return opentypeFont
} catch (error) {
showErrorMessage(error)
}
}
const onFontUploaded = async (fontFile)=>{
const fileName = fontFile.name;
const opentypeFont = await parseFont(fontFile)
const url = await getFileURL(fontFile);
window.currentFont = new Font(opentypeFont, url, fileName)
setFontFace(currentFont.fontFace)
displayFontData(currentFont)
}
const fileOpenHandler = (ev)=>{
const file = ev.target.files[0];
window.currentFontFile = file;
onFontUploaded(currentFontFile)
}
const dropHandler = (ev)=>{
ev.preventDefault();
if (ev.dataTransfer.items.length > 1) {
showErrorMessage('Please Select Only One File!!')
}
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
[...ev.dataTransfer.items].forEach(async (item, i) => {
// If dropped items aren't files, reject them
if (item.kind === "file") {
window.currentFontFile =await item.getAsFile()
if ( ['.otf', '.ttf', 'woff'].indexOf(currentFontFile.name.slice(-4)) < 0) {
showErrorMessage('Please Only Drop Font File!!')
return false
}
onFontUploaded(currentFontFile)
}
});
} else {
// Use DataTransfer interface to access the file(s)
[...ev.dataTransfer.files].forEach((file, i) => {
console.log(`… file[${i}].name = ${file.name}`);
});
}
}
const dragOverHandler = (ev)=>{
ev.preventDefault();
}
const controlsDragHandler = ()=>{
let isNarrow = document.body.offsetWidth < 768 ? true : false;
let initialCoord;
let movement = 0
let target = 0
let flexBasisPx = isNarrow ? 0 : 200;
let flexBasisVp = isNarrow ? 0.4 : 0.1;
let viewPort = isNarrow ? document.body.clientHeight : document.body.clientWidth
let controlsMinSize = isNarrow ? 400 : 300;
let controlsMaxSize = isNarrow ? 0.6 * viewPort : 0.5 * viewPort;
let mouseDowned = false
let controlsClicked = false
mainEl.addEventListener('mousedown', (ev)=>{
isNarrow = document.body.offsetWidth < 768 ? true : false;
initialCoord = isNarrow ? ev.clientY : ev.clientX
mouseDowned = true;
if (isNarrow){
target = document.elementsFromPoint(ev.clientX, ev.clientY).find(e=>e.id==='control-bar') ? document.elementsFromPoint(ev.clientX, ev.clientY).find(e=>e.id==='control-bar') : false;
controlsClicked = target && target.offsetHeight - initialCoord < 7 ? true : false;
}else{
target = ev.target.id === 'control-bar' ? ev.target : false;
controlsClicked = target && initialCoord - target.offsetLeft < 7 ? true : false;
}
})
mainEl.addEventListener('mousemove', (ev)=>{
if (mouseDowned && controlsClicked){
movement = isNarrow ? ev.clientY - initialCoord : initialCoord - ev.clientX
const [t, p] = isNarrow ? ['40vh', '60vh'] : ['10vw', '90vw']
target.style.flexBasis = `calc(${flexBasisPx + movement}px + ${t})`
previewZone.style.flexBasis = `calc(${- flexBasisPx - movement}px + ${p})`
}
})
mainEl.addEventListener('mouseup', (ev)=>{
if (mouseDowned && controlsClicked){
mouseDowned = false
if (flexBasisPx + movement + (viewPort * flexBasisVp ) < controlsMinSize){
flexBasisPx = controlsMinSize - (viewPort * flexBasisVp)
}else if(flexBasisPx + movement + (viewPort * flexBasisVp ) > (controlsMaxSize)){
flexBasisPx = controlsMaxSize - (viewPort * flexBasisVp)
}else{
flexBasisPx = flexBasisPx + movement
}
movement = 0;
const [t, p] = isNarrow ? ['40vh', '60vh'] : ['10vw', '90vw']
target.style.flexBasis = `calc(${flexBasisPx + movement}px + ${t})`
previewZone.style.flexBasis = `calc(${- flexBasisPx - movement}px + ${p})`
}
})
}
mainEl.addEventListener('drop', dropHandler)
mainEl.addEventListener('dragover', dragOverHandler)
fileOpener.addEventListener("change", fileOpenHandler);
controlsDragHandler()