-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
168 lines (152 loc) · 4.72 KB
/
index.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
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
/**
* External dependencies
*/
import { paramCase as kebabCase } from 'change-case';
/**
* Internal dependencies
*/
import { FONT_WEIGHTS, FONT_STYLES } from './constants';
export function setUIValuesNeeded( font, extraValues = {} ) {
if ( ! font.name && ( font.fontFamily || font.slug ) ) {
font.name = font.fontFamily || font.slug;
}
return {
...font,
...extraValues,
};
}
export function isUrlEncoded( url ) {
if ( typeof url !== 'string' ) {
return false;
}
return url !== decodeURIComponent( url );
}
export function getFontFaceVariantName( face ) {
const weightName = FONT_WEIGHTS[ face.fontWeight ] || face.fontWeight;
const styleName =
face.fontStyle === 'normal'
? ''
: FONT_STYLES[ face.fontStyle ] || face.fontStyle;
return `${ weightName } ${ styleName }`;
}
export function mergeFontFaces( existing = [], incoming = [] ) {
const map = new Map();
for ( const face of existing ) {
map.set( `${ face.fontWeight }${ face.fontStyle }`, face );
}
for ( const face of incoming ) {
// This will overwrite if the src already exists, keeping it unique.
map.set( `${ face.fontWeight }${ face.fontStyle }`, face );
}
return Array.from( map.values() );
}
export function mergeFontFamilies( existing = [], incoming = [] ) {
const map = new Map();
// Add the existing array to the map.
for ( const font of existing ) {
map.set( font.slug, { ...font } );
}
// Add the incoming array to the map, overwriting existing values excepting fontFace that need to be merged.
for ( const font of incoming ) {
if ( map.has( font.slug ) ) {
const { fontFace: incomingFontFaces, ...restIncoming } = font;
const existingFont = map.get( font.slug );
// Merge the fontFaces existing with the incoming fontFaces.
const mergedFontFaces = mergeFontFaces(
existingFont.fontFace,
incomingFontFaces
);
// Except for the fontFace key all the other keys are overwritten with the incoming values.
map.set( font.slug, {
...restIncoming,
fontFace: mergedFontFaces,
} );
} else {
map.set( font.slug, { ...font } );
}
}
return Array.from( map.values() );
}
/*
* Loads the font face from a URL and adds it to the browser.
* It also adds it to the iframe document.
*/
export async function loadFontFaceInBrowser( fontFace, source, addTo = 'all' ) {
let dataSource;
if ( typeof source === 'string' ) {
dataSource = `url(${ source })`;
// eslint-disable-next-line no-undef
} else if ( source instanceof File ) {
dataSource = await source.arrayBuffer();
}
// eslint-disable-next-line no-undef
const newFont = new FontFace( fontFace.fontFamily, dataSource, {
style: fontFace.fontStyle,
weight: fontFace.fontWeight,
} );
const loadedFace = await newFont.load();
if ( addTo === 'document' || addTo === 'all' ) {
document.fonts.add( loadedFace );
}
if ( addTo === 'iframe' || addTo === 'all' ) {
const iframeDocument = document.querySelector(
'iframe[name="editor-canvas"]'
).contentDocument;
iframeDocument.fonts.add( loadedFace );
}
}
export function getDisplaySrcFromFontFace( input, urlPrefix ) {
if ( ! input ) {
return;
}
let src;
if ( Array.isArray( input ) ) {
src = input[ 0 ];
} else {
src = input;
}
// If it is a theme font, we need to make the url absolute
if ( src.startsWith( 'file:.' ) && urlPrefix ) {
src = src.replace( 'file:.', urlPrefix );
}
if ( ! isUrlEncoded( src ) ) {
src = encodeURI( src );
}
return src;
}
// This function replicates one behavior of _wp_to_kebab_case().
// Additional context: https://github.com/WordPress/gutenberg/issues/53695
export function wpKebabCase( str ) {
// If a string contains a digit followed by a number, insert a dash between them.
return kebabCase( str ).replace(
/([a-zA-Z])(\d)|(\d)([a-zA-Z])/g,
'$1$3-$2$4'
);
}
export function makeFormDataFromFontFamilies( fontFamilies ) {
const formData = new FormData();
const newFontFamilies = fontFamilies.map( ( family, familyIndex ) => {
family.slug = wpKebabCase( family.slug );
if ( family?.fontFace ) {
family.fontFace = family.fontFace.map( ( face, faceIndex ) => {
if ( face.file ) {
// Slugified file name because the it might contain spaces or characters treated differently on the server.
const fileId = `file-${ familyIndex }-${ faceIndex }`;
// Add the files to the formData
formData.append( fileId, face.file, face.file.name );
// remove the file object from the face object the file is referenced by the uploadedFile key
const { file, ...faceWithoutFileProperty } = face;
const newFace = {
...faceWithoutFileProperty,
uploadedFile: fileId,
};
return newFace;
}
return face;
} );
}
return family;
} );
formData.append( 'font_families', JSON.stringify( newFontFamilies ) );
return formData;
}