Skip to content

Commit 3b70c7b

Browse files
committed
Add demo font
This commit adds a button that'll auto-load a font from the server, so Wakamai Fondue can be tried with the click of a button instead of forcing the user to scour their OS for a font to drop. Using Bungee for now!
1 parent 1730667 commit 3b70c7b

8 files changed

Lines changed: 94 additions & 43 deletions

File tree

public/Bungee-Regular.ttf

124 KB
Binary file not shown.

src/App.js

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,58 @@ export default {
1515
dragging: false
1616
}),
1717
methods: {
18-
grabFont(e) {
18+
getFont(e) {
1919
e.preventDefault();
20-
const that = this; // Is this the best way to deal with context?
21-
22-
// Turn off drag feedback
2320
this.dragging = false;
2421

25-
// Loop over all files
22+
const that = this;
23+
24+
// Loop over all uploaded files
2625
let files = e.target.files || e.dataTransfer.files;
2726
if (!files) return;
28-
[...files].forEach((file, key) => {
29-
// Convert file to base64 string...
30-
const reader = new FileReader();
31-
reader.readAsDataURL(file);
32-
reader.onload = function() {
33-
// ...and try to pass to Font.js
34-
const font = new window.Font(`font${key}`);
35-
font.loadFont(reader.result, file.name);
36-
font.onload = e => {
37-
that.injectStyleSheet(file, key);
38-
that.font = e.detail.font;
39-
that.$nextTick(() =>
40-
document.getElementById("report").scrollIntoView()
41-
);
42-
window.font = e.detail.font; // DEV DEBUG ONLY !!
43-
};
44-
font.onerror = function(error) {
45-
// TODO: error handling
46-
console.log(error);
47-
};
27+
[...files].forEach(file => {
28+
const filename = file.name;
29+
that.loadFont(file, filename, that);
30+
});
31+
},
32+
getExampleFont(filename) {
33+
const that = this;
34+
35+
// Grab font from server
36+
const request = new XMLHttpRequest();
37+
request.open("GET", `/${filename}`, true);
38+
request.responseType = "blob";
39+
request.send();
40+
41+
request.onload = function() {
42+
const file = request.response;
43+
that.loadFont(file, filename, that);
44+
};
45+
},
46+
loadFont(file, filename, that) {
47+
const reader = new FileReader();
48+
reader.readAsDataURL(file);
49+
reader.onload = function() {
50+
const filedata = reader.result;
51+
const font = new window.Font(filename);
52+
font.loadFont(filedata, filename);
53+
font.onload = e => {
54+
that.injectStyleSheet(file);
55+
that.font = e.detail.font;
56+
that.$nextTick(() =>
57+
document.getElementById("report").scrollIntoView()
58+
);
59+
window.font = e.detail.font; // DEV DEBUG ONLY !!
4860
};
49-
reader.onerror = function(error) {
61+
font.onerror = function(error) {
5062
// TODO: error handling
5163
console.log(error);
5264
};
53-
});
65+
};
66+
reader.onerror = function(error) {
67+
// TODO: error handling
68+
console.log(error);
69+
};
5470
},
5571
injectStyleSheet(file) {
5672
// Use the "uploaded" font on the page

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@mouseout="dragging = false"
77
:class="{ dragging }"
88
>
9-
<Hero v-on:filePickFont="grabFont" />
9+
<Hero v-on:getFont="getFont" v-on:getExampleFont="getExampleFont" />
1010
<Report :font="font" />
1111
</main>
1212
</template>

src/components/Filedrop.css

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,31 @@
4848
opacity: 0;
4949
}
5050

51+
.demo-font {
52+
/* Stacking tricks to prevent button click
53+
from being seen as label click */
54+
position: relative;
55+
z-index: 1;
56+
padding: 0.25rem 0.5rem;
57+
border-radius: 0.2rem;
58+
margin: 1rem 0 0 0;
59+
border: 0;
60+
background: none;
61+
font-size: 0.85rem;
62+
background: var(--green);
63+
color: white;
64+
cursor: pointer;
65+
}
66+
5167
.info {
5268
position: relative;
5369
display: flex;
70+
flex-direction: column;
5471
justify-content: center;
5572
align-items: center;
5673
height: 100%;
5774
}
5875

59-
/* Styles when font is being dragged */
60-
6176
.dragging .filedrop {
6277
position: fixed;
6378
background: rgba(0, 0, 0, 0.75);

src/components/Filedrop.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44

55
export default {
66
methods: {
7-
filePickFont(e) {
7+
getFont(e) {
88
// Quick and clean way to emit to grandparent
99
// that a file has been picked
1010
let vm = this.$parent;
1111
while (vm) {
12-
vm.$emit("filePickFont", e);
12+
vm.$emit("getFont", e);
13+
vm = vm.$parent;
14+
}
15+
},
16+
getExampleFont(filename) {
17+
// Quick and clean way to emit to grandparent
18+
// that a file has been picked
19+
let vm = this.$parent;
20+
while (vm) {
21+
vm.$emit("getExampleFont", filename);
1322
vm = vm.$parent;
1423
}
1524
}

src/components/Filedrop.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
<template>
22
<div class="filedrop">
33
<label class="upload">
4+
<div class="background"></div>
45
<input
56
type="file"
67
name="file"
7-
v-on:change="filePickFont"
8+
v-on:change="getFont"
89
accept=".woff,.woff2,.ttf,.otf"
910
/>
10-
<div class="background"></div>
1111
<div class="info">
1212
Drop a font!
13+
<button
14+
type="button"
15+
v-on:click="getExampleFont('Bungee-Regular.ttf')"
16+
class="demo-font"
17+
>
18+
Try with Bungee!
19+
</button>
1320
</div>
1421
</label>
1522
</div>

src/components/report/Color.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
</template>
1414
</p>
1515
<h3 v-if="palettes.length">Palettes</h3>
16-
<ul v-for="palette in palettes" :key="palette" class="palette">
16+
<ul
17+
v-for="palette in palettes"
18+
:key="`palette_${palette}`"
19+
class="palette"
20+
>
1721
<li
1822
v-for="color in palette"
1923
:key="color"

src/components/report/Sliders.css

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
.button {
3434
padding: 0.25rem 0.5rem;
3535
border-radius: 0.2rem;
36-
margin: 0;
37-
border: 0;
38-
background: none;
39-
font-size: 0.85rem;
40-
background: var(--green);
41-
color: white;
42-
cursor: pointer;
36+
margin: 0;
37+
border: 0;
38+
background: none;
39+
font-size: 0.85rem;
40+
background: var(--green);
41+
color: white;
42+
cursor: pointer;
4343
}
4444

4545
.axis-slider button.hide {
@@ -54,7 +54,7 @@
5454
}
5555

5656
.instances .button {
57-
background: var(--yellow);
57+
background: var(--yellow);
5858
}
5959

6060
.instances .button.active {

0 commit comments

Comments
 (0)