@@ -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
0 commit comments