@bigbinary/neeto-molecules 3.7.32 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,581 @@
1
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
+ import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
3
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
+ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
5
+ import _regeneratorRuntime from '@babel/runtime/regenerator';
6
+ import { memo, useState, useEffect, useMemo } from 'react';
7
+ import { toLabelAndValue, noop } from '@bigbinary/neeto-cist';
8
+ import { globalProps } from '@bigbinary/neeto-commons-frontend/initializers';
9
+ import Select from '@bigbinary/neetoui/Select';
10
+ import { pluck, mergeLeft } from 'ramda';
11
+ import { jsx } from 'react/jsx-runtime';
12
+
13
+ /*! *****************************************************************************
14
+ Copyright (c) Microsoft Corporation. All rights reserved.
15
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
16
+ this file except in compliance with the License. You may obtain a copy of the
17
+ License at http://www.apache.org/licenses/LICENSE-2.0
18
+
19
+ THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
+ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
21
+ WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
22
+ MERCHANTABLITY OR NON-INFRINGEMENT.
23
+
24
+ See the Apache Version 2.0 License for specific language governing permissions
25
+ and limitations under the License.
26
+ ***************************************************************************** */
27
+
28
+ var __assign = function() {
29
+ __assign = Object.assign || function __assign(t) {
30
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
31
+ s = arguments[i];
32
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
33
+ }
34
+ return t;
35
+ };
36
+ return __assign.apply(this, arguments);
37
+ };
38
+
39
+ function __rest(s, e) {
40
+ var t = {};
41
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
42
+ t[p] = s[p];
43
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
44
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
45
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
46
+ t[p[i]] = s[p[i]];
47
+ }
48
+ return t;
49
+ }
50
+
51
+ function __awaiter(thisArg, _arguments, P, generator) {
52
+ return new (P || (P = Promise))(function (resolve, reject) {
53
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
54
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
55
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
56
+ step((generator = generator.apply(thisArg, [])).next());
57
+ });
58
+ }
59
+
60
+ function __generator(thisArg, body) {
61
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
62
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
63
+ function verb(n) { return function (v) { return step([n, v]); }; }
64
+ function step(op) {
65
+ if (f) throw new TypeError("Generator is already executing.");
66
+ while (_) try {
67
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
68
+ if (y = 0, t) op = [op[0] & 2, t.value];
69
+ switch (op[0]) {
70
+ case 0: case 1: t = op; break;
71
+ case 4: _.label++; return { value: op[1], done: false };
72
+ case 5: _.label++; y = op[1]; op = [0]; continue;
73
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
74
+ default:
75
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
76
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
77
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
78
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
79
+ if (t[2]) _.ops.pop();
80
+ _.trys.pop(); continue;
81
+ }
82
+ op = body.call(thisArg, _);
83
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
84
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
85
+ }
86
+ }
87
+
88
+ function styleInject(css, ref) {
89
+ if ( ref === void 0 ) ref = {};
90
+ var insertAt = ref.insertAt;
91
+
92
+ if (typeof document === 'undefined') { return; }
93
+
94
+ var head = document.head || document.getElementsByTagName('head')[0];
95
+ var style = document.createElement('style');
96
+ style.type = 'text/css';
97
+
98
+ if (insertAt === 'top') {
99
+ if (head.firstChild) {
100
+ head.insertBefore(style, head.firstChild);
101
+ } else {
102
+ head.appendChild(style);
103
+ }
104
+ } else {
105
+ head.appendChild(style);
106
+ }
107
+
108
+ if (style.styleSheet) {
109
+ style.styleSheet.cssText = css;
110
+ } else {
111
+ style.appendChild(document.createTextNode(css));
112
+ }
113
+ }
114
+
115
+ var css = "@charset \"UTF-8\";\ndiv[id^=font-picker] {\n position: relative;\n display: inline-block;\n width: 200px;\n box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);\n}\ndiv[id^=font-picker] * {\n box-sizing: border-box;\n}\ndiv[id^=font-picker] p {\n margin: 0;\n padding: 0;\n}\ndiv[id^=font-picker] button {\n color: inherit;\n font-size: inherit;\n background: none;\n border: 0;\n outline: none;\n cursor: pointer;\n}\ndiv[id^=font-picker] .dropdown-button {\n display: flex;\n align-items: center;\n justify-content: space-between;\n width: 100%;\n height: 35px;\n padding: 0 10px;\n background: #cbcbcb;\n}\ndiv[id^=font-picker] .dropdown-button:hover, div[id^=font-picker] .dropdown-button:focus {\n background: #bebebe;\n}\ndiv[id^=font-picker] .dropdown-button .dropdown-font-name {\n overflow: hidden;\n white-space: nowrap;\n}\ndiv[id^=font-picker] .dropdown-icon {\n margin-left: 10px;\n}\n@-webkit-keyframes spinner {\n to {\n transform: rotate(360deg);\n }\n}\n@keyframes spinner {\n to {\n transform: rotate(360deg);\n }\n}\ndiv[id^=font-picker] .dropdown-icon.loading::before {\n display: block;\n width: 10px;\n height: 10px;\n border: 2px solid #b2b2b2;\n border-top-color: #000000;\n border-radius: 50%;\n -webkit-animation: spinner 0.6s linear infinite;\n animation: spinner 0.6s linear infinite;\n content: \"\";\n}\ndiv[id^=font-picker] .dropdown-icon.finished::before {\n display: block;\n width: 0;\n height: 0;\n margin: 0 2px;\n border-top: 6px solid #000000;\n border-right: 5px solid transparent;\n border-left: 5px solid transparent;\n transition: transform 0.3s;\n content: \"\";\n}\ndiv[id^=font-picker] .dropdown-icon.error::before {\n content: \"⚠\";\n}\ndiv[id^=font-picker].expanded .dropdown-icon.finished::before {\n transform: rotate(-180deg);\n}\ndiv[id^=font-picker].expanded ul {\n max-height: 200px;\n}\ndiv[id^=font-picker] ul {\n position: absolute;\n z-index: 1;\n width: 100%;\n max-height: 0;\n margin: 0;\n padding: 0;\n overflow-x: hidden;\n overflow-y: auto;\n background: #eaeaea;\n box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);\n transition: 0.3s;\n -webkit-overflow-scrolling: touch;\n}\ndiv[id^=font-picker] ul li {\n height: 35px;\n list-style: none;\n}\ndiv[id^=font-picker] ul li button {\n display: flex;\n align-items: center;\n width: 100%;\n height: 100%;\n padding: 0 10px;\n white-space: nowrap;\n}\ndiv[id^=font-picker] ul li button:hover, div[id^=font-picker] ul li button:focus {\n background: #dddddd;\n}\ndiv[id^=font-picker] ul li button.active-font {\n background: #d1d1d1;\n}";
116
+ styleInject(css);
117
+
118
+ function getFontId(fontFamily) {
119
+ return fontFamily.replace(/\s+/g, "-").toLowerCase();
120
+ }
121
+ function validatePickerId(pickerId) {
122
+ if (pickerId.match(/[^0-9a-z]/i)) {
123
+ throw Error("The `pickerId` parameter may only contain letters and digits");
124
+ }
125
+ }
126
+
127
+ function get(url) {
128
+ return new Promise(function (resolve, reject) {
129
+ var request = new XMLHttpRequest();
130
+ request.overrideMimeType("application/json");
131
+ request.open("GET", url, true);
132
+ request.onreadystatechange = function () {
133
+ if (request.readyState === 4) {
134
+ if (request.status !== 200) {
135
+ reject(new Error("Response has status code " + request.status));
136
+ }
137
+ else {
138
+ resolve(request.responseText);
139
+ }
140
+ }
141
+ };
142
+ request.send();
143
+ });
144
+ }
145
+
146
+ var LIST_BASE_URL = "https://www.googleapis.com/webfonts/v1/webfonts";
147
+ function getFontList(apiKey) {
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ var url, response, json, fontsOriginal;
150
+ return __generator(this, function (_a) {
151
+ switch (_a.label) {
152
+ case 0:
153
+ url = new URL(LIST_BASE_URL);
154
+ url.searchParams.append("sort", "popularity");
155
+ url.searchParams.append("key", apiKey);
156
+ return [4, get(url.href)];
157
+ case 1:
158
+ response = _a.sent();
159
+ json = JSON.parse(response);
160
+ fontsOriginal = json.items;
161
+ return [2, fontsOriginal.map(function (fontOriginal) {
162
+ var family = fontOriginal.family, subsets = fontOriginal.subsets, others = __rest(fontOriginal, ["family", "subsets"]);
163
+ return __assign(__assign({}, others), { family: family, id: getFontId(family), scripts: subsets });
164
+ })];
165
+ }
166
+ });
167
+ });
168
+ }
169
+
170
+ var previewFontsStylesheet = document.createElement("style");
171
+ document.head.appendChild(previewFontsStylesheet);
172
+ function applyFontPreview(previewFont, selectorSuffix) {
173
+ var fontId = getFontId(previewFont.family);
174
+ var style = "\n\t\t\t#font-button-" + fontId + selectorSuffix + " {\n\t\t\t\tfont-family: \"" + previewFont.family + "\";\n\t\t\t}\n\t\t";
175
+ previewFontsStylesheet.appendChild(document.createTextNode(style));
176
+ }
177
+ function getActiveFontStylesheet(selectorSuffix) {
178
+ var stylesheetId = "active-font-" + selectorSuffix;
179
+ var activeFontStylesheet = document.getElementById(stylesheetId);
180
+ if (!activeFontStylesheet) {
181
+ activeFontStylesheet = document.createElement("style");
182
+ activeFontStylesheet.id = stylesheetId;
183
+ document.head.appendChild(activeFontStylesheet);
184
+ }
185
+ return activeFontStylesheet;
186
+ }
187
+ function applyActiveFont(activeFont, previousFontFamily, selectorSuffix) {
188
+ var style = "\n\t\t.apply-font" + selectorSuffix + " {\n\t\t\tfont-family: \"" + activeFont.family + "\"" + (previousFontFamily ? ", \"" + previousFontFamily + "\"" : "") + ";\n\t\t}\n\t";
189
+ var activeFontStylesheet = getActiveFontStylesheet(selectorSuffix);
190
+ activeFontStylesheet.innerHTML = style;
191
+ }
192
+
193
+ var PREVIEW_ATTRIBUTE_NAME = "data-is-preview";
194
+ function getStylesheetId(fontId) {
195
+ return "font-" + fontId;
196
+ }
197
+ function stylesheetExists(fontId, isPreview) {
198
+ var stylesheetNode = document.getElementById(getStylesheetId(fontId));
199
+ if (isPreview === null || isPreview === undefined) {
200
+ return stylesheetNode !== null;
201
+ }
202
+ return (stylesheetNode !== null &&
203
+ stylesheetNode.getAttribute(PREVIEW_ATTRIBUTE_NAME) === isPreview.toString());
204
+ }
205
+ function createStylesheet(fontId, isPreview) {
206
+ var stylesheetNode = document.createElement("style");
207
+ stylesheetNode.id = getStylesheetId(fontId);
208
+ stylesheetNode.setAttribute(PREVIEW_ATTRIBUTE_NAME, isPreview.toString());
209
+ document.head.appendChild(stylesheetNode);
210
+ }
211
+ function fillStylesheet(fontId, styles) {
212
+ var stylesheetId = getStylesheetId(fontId);
213
+ var stylesheetNode = document.getElementById(stylesheetId);
214
+ if (stylesheetNode) {
215
+ stylesheetNode.textContent = styles;
216
+ }
217
+ else {
218
+ console.error("Could not fill stylesheet: Stylesheet with ID \"" + stylesheetId + "\" not found");
219
+ }
220
+ }
221
+ function setStylesheetType(fontId, isPreview) {
222
+ var stylesheetId = getStylesheetId(fontId);
223
+ var stylesheetNode = document.getElementById(stylesheetId);
224
+ if (stylesheetNode) {
225
+ stylesheetNode.setAttribute(PREVIEW_ATTRIBUTE_NAME, isPreview.toString());
226
+ }
227
+ else {
228
+ console.error("Could not change stylesheet type: Stylesheet with ID \"" + stylesheetId + "\" not found");
229
+ }
230
+ }
231
+
232
+ function getMatches(regex, str) {
233
+ var matches = [];
234
+ var match;
235
+ do {
236
+ match = regex.exec(str);
237
+ if (match) {
238
+ matches.push(match[1]);
239
+ }
240
+ } while (match);
241
+ return matches;
242
+ }
243
+
244
+ var FONT_FACE_REGEX = /@font-face {([\s\S]*?)}/gm;
245
+ var FONT_FAMILY_REGEX = /font-family: ['"](.*?)['"]/gm;
246
+ function extractFontStyles(allFontStyles) {
247
+ var rules = getMatches(FONT_FACE_REGEX, allFontStyles);
248
+ var fontStyles = {};
249
+ rules.forEach(function (rule) {
250
+ var fontFamily = getMatches(FONT_FAMILY_REGEX, rule)[0];
251
+ var fontId = getFontId(fontFamily);
252
+ if (!(fontId in fontStyles)) {
253
+ fontStyles[fontId] = "";
254
+ }
255
+ fontStyles[fontId] += "@font-face {\n" + rule + "\n}\n\n";
256
+ });
257
+ return fontStyles;
258
+ }
259
+
260
+ var FONT_BASE_URL = "https://fonts.googleapis.com/css";
261
+ function getStylesheet(fonts, scripts, variants, previewsOnly) {
262
+ return __awaiter(this, void 0, void 0, function () {
263
+ var url, variantsStr, familiesStr, familyNamesConcat, downloadChars;
264
+ return __generator(this, function (_a) {
265
+ url = new URL(FONT_BASE_URL);
266
+ variantsStr = variants.join(",");
267
+ familiesStr = fonts.map(function (font) { return font.family + ":" + variantsStr; });
268
+ url.searchParams.append("family", familiesStr.join("|"));
269
+ url.searchParams.append("subset", scripts.join(","));
270
+ if (previewsOnly) {
271
+ familyNamesConcat = fonts.map(function (font) { return font.family; }).join("");
272
+ downloadChars = familyNamesConcat
273
+ .split("")
274
+ .filter(function (char, pos, self) { return self.indexOf(char) === pos; })
275
+ .join("");
276
+ url.searchParams.append("text", downloadChars);
277
+ }
278
+ url.searchParams.append("font-display", "swap");
279
+ return [2, get(url.href)];
280
+ });
281
+ });
282
+ }
283
+
284
+ function loadFontPreviews(fonts, scripts, variants, selectorSuffix) {
285
+ return __awaiter(this, void 0, void 0, function () {
286
+ var fontsArray, fontsToFetch, response, fontStyles;
287
+ return __generator(this, function (_a) {
288
+ switch (_a.label) {
289
+ case 0:
290
+ fontsArray = Array.from(fonts.values());
291
+ fontsToFetch = fontsArray
292
+ .map(function (font) { return font.id; })
293
+ .filter(function (fontId) { return !stylesheetExists(fontId); });
294
+ fontsToFetch.forEach(function (fontId) { return createStylesheet(fontId, true); });
295
+ return [4, getStylesheet(fontsArray, scripts, variants, true)];
296
+ case 1:
297
+ response = _a.sent();
298
+ fontStyles = extractFontStyles(response);
299
+ fontsArray.forEach(function (font) {
300
+ applyFontPreview(font, selectorSuffix);
301
+ if (fontsToFetch.includes(font.id)) {
302
+ if (!(font.id in fontStyles)) {
303
+ console.error("Missing styles for font \"" + font.family + "\" (fontId \"" + font.id + "\") in Google Fonts response");
304
+ return;
305
+ }
306
+ fillStylesheet(font.id, fontStyles[font.id]);
307
+ }
308
+ });
309
+ return [2];
310
+ }
311
+ });
312
+ });
313
+ }
314
+ function loadActiveFont(font, previousFontFamily, scripts, variants, selectorSuffix) {
315
+ return __awaiter(this, void 0, void 0, function () {
316
+ var fontStyle;
317
+ return __generator(this, function (_a) {
318
+ switch (_a.label) {
319
+ case 0:
320
+ if (!stylesheetExists(font.id, false)) return [3, 1];
321
+ applyActiveFont(font, previousFontFamily, selectorSuffix);
322
+ return [3, 3];
323
+ case 1:
324
+ if (stylesheetExists(font.id, true)) {
325
+ setStylesheetType(font.id, false);
326
+ }
327
+ else {
328
+ createStylesheet(font.id, false);
329
+ }
330
+ return [4, getStylesheet([font], scripts, variants, false)];
331
+ case 2:
332
+ fontStyle = _a.sent();
333
+ applyActiveFont(font, previousFontFamily, selectorSuffix);
334
+ fillStylesheet(font.id, fontStyle);
335
+ _a.label = 3;
336
+ case 3: return [2];
337
+ }
338
+ });
339
+ });
340
+ }
341
+
342
+ var FONT_FAMILY_DEFAULT = "Open Sans";
343
+ var OPTIONS_DEFAULTS = {
344
+ pickerId: "",
345
+ families: [],
346
+ categories: [],
347
+ scripts: ["latin"],
348
+ variants: ["regular"],
349
+ filter: function () { return true; },
350
+ limit: 50,
351
+ sort: "alphabet",
352
+ };
353
+
354
+ var FontManager = (function () {
355
+ function FontManager(apiKey, defaultFamily, _a, onChange) {
356
+ if (defaultFamily === void 0) { defaultFamily = FONT_FAMILY_DEFAULT; }
357
+ var _b = _a.pickerId, pickerId = _b === void 0 ? OPTIONS_DEFAULTS.pickerId : _b, _c = _a.families, families = _c === void 0 ? OPTIONS_DEFAULTS.families : _c, _d = _a.categories, categories = _d === void 0 ? OPTIONS_DEFAULTS.categories : _d, _e = _a.scripts, scripts = _e === void 0 ? OPTIONS_DEFAULTS.scripts : _e, _f = _a.variants, variants = _f === void 0 ? OPTIONS_DEFAULTS.variants : _f, _g = _a.filter, filter = _g === void 0 ? OPTIONS_DEFAULTS.filter : _g, _h = _a.limit, limit = _h === void 0 ? OPTIONS_DEFAULTS.limit : _h, _j = _a.sort, sort = _j === void 0 ? OPTIONS_DEFAULTS.sort : _j;
358
+ if (onChange === void 0) { onChange = function () { }; }
359
+ this.fonts = new Map();
360
+ validatePickerId(pickerId);
361
+ this.selectorSuffix = pickerId ? "-" + pickerId : "";
362
+ this.apiKey = apiKey;
363
+ this.options = {
364
+ pickerId: pickerId,
365
+ families: families,
366
+ categories: categories,
367
+ scripts: scripts,
368
+ variants: variants,
369
+ filter: filter,
370
+ limit: limit,
371
+ sort: sort,
372
+ };
373
+ this.onChange = onChange;
374
+ this.addFont(defaultFamily, false);
375
+ this.setActiveFont(defaultFamily, false);
376
+ }
377
+ FontManager.prototype.init = function () {
378
+ return __awaiter(this, void 0, void 0, function () {
379
+ var fonts, _loop_1, this_1, i, state_1, fontsToLoad;
380
+ return __generator(this, function (_a) {
381
+ switch (_a.label) {
382
+ case 0: return [4, getFontList(this.apiKey)];
383
+ case 1:
384
+ fonts = _a.sent();
385
+ _loop_1 = function (i) {
386
+ var font = fonts[i];
387
+ if (this_1.fonts.size >= this_1.options.limit) {
388
+ return "break";
389
+ }
390
+ if (!this_1.fonts.has(font.family) &&
391
+ (this_1.options.families.length === 0 || this_1.options.families.includes(font.family)) &&
392
+ (this_1.options.categories.length === 0 || this_1.options.categories.includes(font.category)) &&
393
+ this_1.options.scripts.every(function (script) { return font.scripts.includes(script); }) &&
394
+ this_1.options.variants.every(function (variant) { return font.variants.includes(variant); }) &&
395
+ this_1.options.filter(font) === true) {
396
+ this_1.fonts.set(font.family, font);
397
+ }
398
+ };
399
+ this_1 = this;
400
+ for (i = 0; i < fonts.length; i += 1) {
401
+ state_1 = _loop_1(i);
402
+ if (state_1 === "break")
403
+ break;
404
+ }
405
+ fontsToLoad = new Map(this.fonts);
406
+ fontsToLoad["delete"](this.activeFontFamily);
407
+ loadFontPreviews(fontsToLoad, this.options.scripts, this.options.variants, this.selectorSuffix);
408
+ return [2, this.fonts];
409
+ }
410
+ });
411
+ });
412
+ };
413
+ FontManager.prototype.getFonts = function () {
414
+ return this.fonts;
415
+ };
416
+ FontManager.prototype.addFont = function (fontFamily, downloadPreview) {
417
+ if (downloadPreview === void 0) { downloadPreview = true; }
418
+ var font = {
419
+ family: fontFamily,
420
+ id: getFontId(fontFamily),
421
+ };
422
+ this.fonts.set(fontFamily, font);
423
+ if (downloadPreview) {
424
+ var fontMap = new Map();
425
+ fontMap.set(fontFamily, font);
426
+ loadFontPreviews(fontMap, this.options.scripts, this.options.variants, this.selectorSuffix);
427
+ }
428
+ };
429
+ FontManager.prototype.removeFont = function (fontFamily) {
430
+ this.fonts["delete"](fontFamily);
431
+ };
432
+ FontManager.prototype.getActiveFont = function () {
433
+ var activeFont = this.fonts.get(this.activeFontFamily);
434
+ if (!activeFont) {
435
+ throw Error("Cannot get active font: \"" + this.activeFontFamily + "\" is not in the font list");
436
+ }
437
+ else {
438
+ return activeFont;
439
+ }
440
+ };
441
+ FontManager.prototype.setActiveFont = function (fontFamily, runOnChange) {
442
+ var _this = this;
443
+ if (runOnChange === void 0) { runOnChange = true; }
444
+ var previousFontFamily = this.activeFontFamily;
445
+ var activeFont = this.fonts.get(fontFamily);
446
+ if (!activeFont) {
447
+ throw Error("Cannot update active font: \"" + fontFamily + "\" is not in the font list");
448
+ }
449
+ this.activeFontFamily = fontFamily;
450
+ loadActiveFont(activeFont, previousFontFamily, this.options.scripts, this.options.variants, this.selectorSuffix).then(function () {
451
+ if (runOnChange) {
452
+ _this.onChange(activeFont);
453
+ }
454
+ });
455
+ };
456
+ FontManager.prototype.setOnChange = function (onChange) {
457
+ this.onChange = onChange;
458
+ };
459
+ return FontManager;
460
+ }());
461
+
462
+ var defaultFilterOptions = function defaultFilterOptions(font) {
463
+ return !UNWANTED_FAMILIES.includes(font.family);
464
+ };
465
+ var sortAndFormatFonts = function sortAndFormatFonts(availableFonts) {
466
+ return pluck("family", availableFonts).sort(function (a, b) {
467
+ return a.localeCompare(b);
468
+ }).map(toLabelAndValue);
469
+ };
470
+
471
+ var UNWANTED_FAMILIES = ["Material Icons", "Material Icons Rounded", "Material Icons Sharp", "Material Icons Two Tone"];
472
+ var DEFAULT_PICKER_PROPS = {
473
+ pickerId: OPTIONS_DEFAULTS.pickerId,
474
+ families: OPTIONS_DEFAULTS.families,
475
+ categories: OPTIONS_DEFAULTS.categories,
476
+ scripts: OPTIONS_DEFAULTS.scripts,
477
+ variants: OPTIONS_DEFAULTS.variants,
478
+ filter: defaultFilterOptions,
479
+ limit: OPTIONS_DEFAULTS.limit,
480
+ sort: OPTIONS_DEFAULTS.sort
481
+ };
482
+
483
+ var _excluded = ["activeFontFamily", "fontManagerOptions", "onChange"];
484
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
485
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
486
+ var GoogleFontPicker = function GoogleFontPicker(_ref) {
487
+ var activeFontFamily = _ref.activeFontFamily,
488
+ _ref$fontManagerOptio = _ref.fontManagerOptions,
489
+ fontManagerOptions = _ref$fontManagerOptio === void 0 ? {
490
+ limit: 300
491
+ } : _ref$fontManagerOptio,
492
+ _ref$onChange = _ref.onChange,
493
+ onChange = _ref$onChange === void 0 ? noop : _ref$onChange,
494
+ otherProps = _objectWithoutProperties(_ref, _excluded);
495
+ var _useState = useState(activeFontFamily),
496
+ _useState2 = _slicedToArray(_useState, 2),
497
+ selectedFontFamily = _useState2[0],
498
+ setSelectedFontFamily = _useState2[1];
499
+ var _useState3 = useState(false),
500
+ _useState4 = _slicedToArray(_useState3, 2),
501
+ isLoading = _useState4[0],
502
+ setIsLoading = _useState4[1];
503
+ var _useState5 = useState([]),
504
+ _useState6 = _slicedToArray(_useState5, 2),
505
+ fonts = _useState6[0],
506
+ setFonts = _useState6[1];
507
+ var options = mergeLeft(fontManagerOptions, DEFAULT_PICKER_PROPS);
508
+ useEffect(function () {
509
+ initializeFontManager();
510
+ }, []);
511
+ var fontManager = useMemo(function () {
512
+ return new FontManager(globalProps.googleFontsApiKey, selectedFontFamily, options, onChange);
513
+ }, []);
514
+ var initializeFontManager = /*#__PURE__*/function () {
515
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
516
+ var availableFonts;
517
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
518
+ while (1) switch (_context.prev = _context.next) {
519
+ case 0:
520
+ _context.prev = 0;
521
+ setIsLoading(true);
522
+ _context.next = 4;
523
+ return fontManager.init();
524
+ case 4:
525
+ availableFonts = Array.from(fontManager.getFonts().values());
526
+ setFonts(sortAndFormatFonts(availableFonts));
527
+ _context.next = 11;
528
+ break;
529
+ case 8:
530
+ _context.prev = 8;
531
+ _context.t0 = _context["catch"](0);
532
+ throw _context.t0.message;
533
+ case 11:
534
+ _context.prev = 11;
535
+ setIsLoading(false);
536
+ return _context.finish(11);
537
+ case 14:
538
+ case "end":
539
+ return _context.stop();
540
+ }
541
+ }, _callee, null, [[0, 8, 11, 14]]);
542
+ }));
543
+ return function initializeFontManager() {
544
+ return _ref2.apply(this, arguments);
545
+ };
546
+ }();
547
+ var handleChange = function handleChange(fontFamily) {
548
+ var value = fontFamily.value;
549
+ fontManager.setActiveFont(value, false);
550
+ setSelectedFontFamily(value);
551
+ onChange(value);
552
+ };
553
+ return /*#__PURE__*/jsx(Select, _objectSpread(_objectSpread({
554
+ "data-testid": "google-font-picker",
555
+ options: fonts,
556
+ strategy: "fixed",
557
+ value: toLabelAndValue(selectedFontFamily),
558
+ styles: {
559
+ option: function option(styles, _ref3) {
560
+ var data = _ref3.data;
561
+ return _objectSpread(_objectSpread({}, styles), {}, {
562
+ fontFamily: data.value
563
+ });
564
+ },
565
+ singleValue: function singleValue(styles, _ref4) {
566
+ var data = _ref4.data;
567
+ return _objectSpread(_objectSpread({}, styles), {}, {
568
+ fontFamily: data.value
569
+ });
570
+ }
571
+ }
572
+ }, _objectSpread(_objectSpread({}, otherProps), {}, {
573
+ isLoading: isLoading
574
+ })), {}, {
575
+ onChange: handleChange
576
+ }));
577
+ };
578
+ var index = /*#__PURE__*/memo(GoogleFontPicker);
579
+
580
+ export { index as default };
581
+ //# sourceMappingURL=GoogleFontPicker.js.map