@hanzogui/font 2.0.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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/cjs/index.cjs +80 -0
- package/dist/cjs/index.native.js +88 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/esm/index.js +57 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +57 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +62 -0
- package/dist/esm/index.native.js.map +1 -0
- package/package.json +48 -0
- package/src/index.ts +84 -0
- package/types/index.d.ts +22 -0
- package/types/index.d.ts.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
## addFont
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: !0
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
__copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
13
|
+
get: () => from[key],
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
19
|
+
value: !0
|
|
20
|
+
}), mod);
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
addFont: () => addFont
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_constants = require("@hanzogui/constants"),
|
|
27
|
+
import_web = require("@hanzogui/web");
|
|
28
|
+
function addFont(props) {
|
|
29
|
+
const config = (0, import_web.getConfig)(),
|
|
30
|
+
{
|
|
31
|
+
fontFamilyName: fontFamilyNameIn,
|
|
32
|
+
fontFamily: fontFamilyIn
|
|
33
|
+
} = props;
|
|
34
|
+
if (process.env.NODE_ENV === "development") {
|
|
35
|
+
if (!config) throw new Error("No config");
|
|
36
|
+
const fontFamily = config.fonts[fontFamilyNameIn];
|
|
37
|
+
if (!props.update && fontFamily) return {
|
|
38
|
+
fontFamily
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
config.fonts[fontFamilyNameIn] = fontFamilyIn;
|
|
42
|
+
const sep =
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
process.env.NODE_ENV === "development" ? config.cssStyleSeparator || " " : "";
|
|
45
|
+
function declarationsToRuleSet(decs, selector = "") {
|
|
46
|
+
return `:root${selector} {${sep}${[...decs].join(`;${sep}`)}${sep}}`;
|
|
47
|
+
}
|
|
48
|
+
if (import_constants.isWeb) {
|
|
49
|
+
const fontFamilyToken = (0, import_web.createVariables)(fontFamilyIn, "f", !0),
|
|
50
|
+
parsedFontFamily = (0, import_web.parseFont)(fontFamilyToken),
|
|
51
|
+
fontFamilyNameParsed = `$${fontFamilyNameIn}`;
|
|
52
|
+
if (config.fontsParsed[fontFamilyNameParsed] = parsedFontFamily, props.insertCSS) {
|
|
53
|
+
const [ff_name, ff_language] = fontFamilyNameParsed.includes("_") ? fontFamilyNameParsed.split("_") : [fontFamilyNameParsed],
|
|
54
|
+
fontVars = (0, import_web.registerFontVariables)(parsedFontFamily),
|
|
55
|
+
fontDeclaration = {
|
|
56
|
+
[fontFamilyNameIn]: {
|
|
57
|
+
name: ff_name.slice(1),
|
|
58
|
+
declarations: fontVars,
|
|
59
|
+
language: ff_language
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name,
|
|
64
|
+
declarations,
|
|
65
|
+
language = "default"
|
|
66
|
+
} = fontDeclaration[fontFamilyNameIn],
|
|
67
|
+
fontSelector = `.font_${name}`,
|
|
68
|
+
langSelector = `:root .t_lang-${name}-${language} ${fontSelector}`,
|
|
69
|
+
selectors = language === "default" ? ` ${fontSelector}, ${langSelector}` : langSelector,
|
|
70
|
+
cssRuleSets = declarationsToRuleSet(declarations, selectors),
|
|
71
|
+
id = `t_font_style_${fontFamilyNameIn}`,
|
|
72
|
+
existing = document.querySelector(`#${id}`),
|
|
73
|
+
style = document.createElement("style");
|
|
74
|
+
return style.id = id, style.appendChild(document.createTextNode(`${cssRuleSets}`)), document.head.appendChild(style), existing && existing.parentElement?.removeChild(existing), {
|
|
75
|
+
fontFamilyToken,
|
|
76
|
+
fontDeclaration
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: !0
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
__copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
21
|
+
value: !0
|
|
22
|
+
}), mod);
|
|
23
|
+
var index_exports = {};
|
|
24
|
+
__export(index_exports, {
|
|
25
|
+
addFont: () => addFont
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_constants = require("@hanzogui/constants"),
|
|
29
|
+
import_web = require("@hanzogui/web");
|
|
30
|
+
function addFont(props) {
|
|
31
|
+
var config = (0, import_web.getConfig)(),
|
|
32
|
+
{
|
|
33
|
+
fontFamilyName: fontFamilyNameIn,
|
|
34
|
+
fontFamily: fontFamilyIn
|
|
35
|
+
} = props;
|
|
36
|
+
if (process.env.NODE_ENV === "development") {
|
|
37
|
+
if (!config) throw new Error("No config");
|
|
38
|
+
var fontFamily = config.fonts[fontFamilyNameIn];
|
|
39
|
+
if (!props.update && fontFamily) return {
|
|
40
|
+
fontFamily
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
config.fonts[fontFamilyNameIn] = fontFamilyIn;
|
|
44
|
+
var sep =
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
process.env.NODE_ENV === "development" ? config.cssStyleSeparator || " " : "";
|
|
47
|
+
function declarationsToRuleSet(decs) {
|
|
48
|
+
var selector = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "";
|
|
49
|
+
return `:root${selector} {${sep}${[...decs].join(`;${sep}`)}${sep}}`;
|
|
50
|
+
}
|
|
51
|
+
if (import_constants.isWeb) {
|
|
52
|
+
var fontFamilyToken = (0, import_web.createVariables)(fontFamilyIn, "f", !0),
|
|
53
|
+
parsedFontFamily = (0, import_web.parseFont)(fontFamilyToken),
|
|
54
|
+
fontFamilyNameParsed = `$${fontFamilyNameIn}`;
|
|
55
|
+
if (config.fontsParsed[fontFamilyNameParsed] = parsedFontFamily, props.insertCSS) {
|
|
56
|
+
var [ff_name, ff_language] = fontFamilyNameParsed.includes("_") ? fontFamilyNameParsed.split("_") : [fontFamilyNameParsed],
|
|
57
|
+
fontVars = (0, import_web.registerFontVariables)(parsedFontFamily),
|
|
58
|
+
fontDeclaration = {
|
|
59
|
+
[fontFamilyNameIn]: {
|
|
60
|
+
name: ff_name.slice(1),
|
|
61
|
+
declarations: fontVars,
|
|
62
|
+
language: ff_language
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name,
|
|
67
|
+
declarations,
|
|
68
|
+
language = "default"
|
|
69
|
+
} = fontDeclaration[fontFamilyNameIn],
|
|
70
|
+
fontSelector = `.font_${name}`,
|
|
71
|
+
langSelector = `:root .t_lang-${name}-${language} ${fontSelector}`,
|
|
72
|
+
selectors = language === "default" ? ` ${fontSelector}, ${langSelector}` : langSelector,
|
|
73
|
+
cssRuleSets = declarationsToRuleSet(declarations, selectors),
|
|
74
|
+
id = `t_font_style_${fontFamilyNameIn}`,
|
|
75
|
+
existing = document.querySelector(`#${id}`),
|
|
76
|
+
style = document.createElement("style");
|
|
77
|
+
if (style.id = id, style.appendChild(document.createTextNode(`${cssRuleSets}`)), document.head.appendChild(style), existing) {
|
|
78
|
+
var _existing_parentElement;
|
|
79
|
+
(_existing_parentElement = existing.parentElement) === null || _existing_parentElement === void 0 || _existing_parentElement.removeChild(existing);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
fontFamilyToken,
|
|
83
|
+
fontDeclaration
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","__export","addFont","module","exports","import_constants","require","import_web","props","config","getConfig","fontFamilyName","fontFamilyNameIn","fontFamily","fontFamilyIn","process","env","NODE_ENV","Error","fonts","update","sep","cssStyleSeparator","declarationsToRuleSet","decs","selector","arguments","length","join","isWeb","fontFamilyToken","createVariables","parsedFontFamily","parseFont","fontFamilyNameParsed","fontsParsed","insertCSS","ff_name","ff_language","includes","split","fontVars","registerFontVariables","fontDeclaration","name","slice","declarations","language","fontSelector","langSelector","selectors","cssRuleSets","id","existing","document","querySelector","style","createElement","appendChild","createTextNode","head","_existing_parentElement","parentElement","removeChild"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAA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A,IAAAF,gBAAA,EAAAxB,KAAA,CAAA4B,SAAA;MACF,IAEM,CAAAC,OAAA,EAAAC,WAAA,IAAAJ,oBAAA,CAAAK,QAAA,QAAAL,oBAAA,CAAAM,KAAA,SACJN,oBAAA;QACAO,QAAA,OAAAlC,UAAA,CAAAmC,qBAAA,EAAAV,gBAAA;QAAAW,eAAA;UACA,CAAA/B,gBAAW;YACTgC,IAAA,EAAAP,OAAA,CAAAQ,KAAgB;YAUpBC,YAAM,EAAKL,QACX;YAMEM,QAAA,EAAAT;UACA;QACF;QAAA;UAAAM,IAAA;UAAAE,YAAA;UAAAC,QAAA;QAAA,IAAAJ,eAAA,CAAA/B,gBAAA;QAAAoC,YAAA,YAAAJ,IAAA;QAAAK,YAAA,oBAAAL,IAAA,IAAAG,QAAA,IAAAC,YAAA;QAAAE,SAAA,GAAAH,QAAA,qBAAAC,YAAA,KAAAC,YAAA,KAAAA,YAAA;QAAAE,WAAA,GAAA5B,qBAAA,CAAAuB,YAAA,EAAAI,SAAA;QAAAE,EAAA,mBAAAxC,gBAAA;QAAAyC,QAAA,GAAAC,QAAA,CAAAC,aAAA,KAAAH,EAAA;QAAAI,KAAA,GAAAF,QAAA,CAAAG,aAAA;MACF,IAAAD,KAAA,CAAAJ,EAAA,GAAAA,EAAA,EAAAI,KAAA,CAAAE,WAAA,CAAAJ,QAAA,CAAAK,cAAA,IAAAR,WAAA,MAAAG,QAAA,CAAAM,IAAA,CAAAF,WAAA,CAAAF,KAAA,GAAAH,QAAA;QACF,IAAAQ,uBAAA;QACF,CAAAA,uBAAA,GAAAR,QAAA,CAAAS,aAAA,cAAAD,uBAAA,eAAAA,uBAAA,CAAAE,WAAA,CAAAV,QAAA","ignoreList":[]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isWeb } from "@hanzogui/constants";
|
|
2
|
+
import { createVariables, getConfig, parseFont, registerFontVariables } from "@hanzogui/web";
|
|
3
|
+
function addFont(props) {
|
|
4
|
+
const config = getConfig(),
|
|
5
|
+
{
|
|
6
|
+
fontFamilyName: fontFamilyNameIn,
|
|
7
|
+
fontFamily: fontFamilyIn
|
|
8
|
+
} = props;
|
|
9
|
+
if (process.env.NODE_ENV === "development") {
|
|
10
|
+
if (!config) throw new Error("No config");
|
|
11
|
+
const fontFamily = config.fonts[fontFamilyNameIn];
|
|
12
|
+
if (!props.update && fontFamily) return {
|
|
13
|
+
fontFamily
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
config.fonts[fontFamilyNameIn] = fontFamilyIn;
|
|
17
|
+
const sep =
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
process.env.NODE_ENV === "development" ? config.cssStyleSeparator || " " : "";
|
|
20
|
+
function declarationsToRuleSet(decs, selector = "") {
|
|
21
|
+
return `:root${selector} {${sep}${[...decs].join(`;${sep}`)}${sep}}`;
|
|
22
|
+
}
|
|
23
|
+
if (isWeb) {
|
|
24
|
+
const fontFamilyToken = createVariables(fontFamilyIn, "f", !0),
|
|
25
|
+
parsedFontFamily = parseFont(fontFamilyToken),
|
|
26
|
+
fontFamilyNameParsed = `$${fontFamilyNameIn}`;
|
|
27
|
+
if (config.fontsParsed[fontFamilyNameParsed] = parsedFontFamily, props.insertCSS) {
|
|
28
|
+
const [ff_name, ff_language] = fontFamilyNameParsed.includes("_") ? fontFamilyNameParsed.split("_") : [fontFamilyNameParsed],
|
|
29
|
+
fontVars = registerFontVariables(parsedFontFamily),
|
|
30
|
+
fontDeclaration = {
|
|
31
|
+
[fontFamilyNameIn]: {
|
|
32
|
+
name: ff_name.slice(1),
|
|
33
|
+
declarations: fontVars,
|
|
34
|
+
language: ff_language
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name,
|
|
39
|
+
declarations,
|
|
40
|
+
language = "default"
|
|
41
|
+
} = fontDeclaration[fontFamilyNameIn],
|
|
42
|
+
fontSelector = `.font_${name}`,
|
|
43
|
+
langSelector = `:root .t_lang-${name}-${language} ${fontSelector}`,
|
|
44
|
+
selectors = language === "default" ? ` ${fontSelector}, ${langSelector}` : langSelector,
|
|
45
|
+
cssRuleSets = declarationsToRuleSet(declarations, selectors),
|
|
46
|
+
id = `t_font_style_${fontFamilyNameIn}`,
|
|
47
|
+
existing = document.querySelector(`#${id}`),
|
|
48
|
+
style = document.createElement("style");
|
|
49
|
+
return style.id = id, style.appendChild(document.createTextNode(`${cssRuleSets}`)), document.head.appendChild(style), existing && existing.parentElement?.removeChild(existing), {
|
|
50
|
+
fontFamilyToken,
|
|
51
|
+
fontDeclaration
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export { addFont };
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isWeb","createVariables","getConfig","parseFont","registerFontVariables","addFont","props","config","fontFamilyName","fontFamilyNameIn","fontFamily","fontFamilyIn","process","env","NODE_ENV","Error","fonts","update","sep","cssStyleSeparator","declarationsToRuleSet","decs","selector","join","fontFamilyToken","parsedFontFamily","fontFamilyNameParsed","fontsParsed","insertCSS","ff_name","ff_language","includes","split","fontVars","fontDeclaration","name","slice","declarations","language","fontSelector","langSelector","selectors","cssRuleSets","id","existing","document","querySelector","style","createElement","appendChild","createTextNode","head","parentElement","removeChild"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,KAAA,QAAa;AACtB,SACEC,eAAA,EACAC,SAAA,EACAC,SAAA,EACAC,qBAAA,QACK;AAGA,SAASC,QAAQC,KAAA,EAMrB;EACD,MAAMC,MAAA,GAASL,SAAA,CAAU;IACnB;MAAEM,cAAA,EAAgBC,gBAAA;MAAkBC,UAAA,EAAYC;IAAa,IAAIL,KAAA;EAEvE,IAAIM,OAAA,CAAQC,GAAA,CAAIC,QAAA,KAAa,eAAe;IAC1C,IAAI,CAACP,MAAA,EACH,MAAM,IAAIQ,KAAA,CAAM,WAAW;IAE7B,MAAML,UAAA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5BK,YAAA,GAAe,iBAAiBL,IAAI,IAAIG,QAAQ,IAAIC,YAAY;QAChEE,SAAA,GACJH,QAAA,KAAa,YAAY,IAAIC,YAAY,KAAKC,YAAY,KAAKA,YAAA;QAC3DE,WAAA,GAActB,qBAAA,CAAsBiB,YAAA,EAAcI,SAAS;QAE3DE,EAAA,GAAK,gBAAgBlC,gBAAgB;QACrCmC,QAAA,GAAWC,QAAA,CAASC,aAAA,CAAc,IAAIH,EAAE,EAAE;QAC1CI,KAAA,GAAQF,QAAA,CAASG,aAAA,CAAc,OAAO;MAC5C,OAAAD,KAAA,CAAMJ,EAAA,GAAKA,EAAA,EACXI,KAAA,CAAME,WAAA,CAAYJ,QAAA,CAASK,cAAA,CAAe,GAAGR,WAAW,EAAE,CAAC,GAC3DG,QAAA,CAASM,IAAA,CAAKF,WAAA,CAAYF,KAAK,GAC3BH,QAAA,IACFA,QAAA,CAASQ,aAAA,EAAeC,WAAA,CAAYT,QAAQ,GAEvC;QACLpB,eAAA;QACAU;MACF;IACF;EACF;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isWeb } from "@hanzogui/constants";
|
|
2
|
+
import { createVariables, getConfig, parseFont, registerFontVariables } from "@hanzogui/web";
|
|
3
|
+
function addFont(props) {
|
|
4
|
+
const config = getConfig(),
|
|
5
|
+
{
|
|
6
|
+
fontFamilyName: fontFamilyNameIn,
|
|
7
|
+
fontFamily: fontFamilyIn
|
|
8
|
+
} = props;
|
|
9
|
+
if (process.env.NODE_ENV === "development") {
|
|
10
|
+
if (!config) throw new Error("No config");
|
|
11
|
+
const fontFamily = config.fonts[fontFamilyNameIn];
|
|
12
|
+
if (!props.update && fontFamily) return {
|
|
13
|
+
fontFamily
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
config.fonts[fontFamilyNameIn] = fontFamilyIn;
|
|
17
|
+
const sep =
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
process.env.NODE_ENV === "development" ? config.cssStyleSeparator || " " : "";
|
|
20
|
+
function declarationsToRuleSet(decs, selector = "") {
|
|
21
|
+
return `:root${selector} {${sep}${[...decs].join(`;${sep}`)}${sep}}`;
|
|
22
|
+
}
|
|
23
|
+
if (isWeb) {
|
|
24
|
+
const fontFamilyToken = createVariables(fontFamilyIn, "f", !0),
|
|
25
|
+
parsedFontFamily = parseFont(fontFamilyToken),
|
|
26
|
+
fontFamilyNameParsed = `$${fontFamilyNameIn}`;
|
|
27
|
+
if (config.fontsParsed[fontFamilyNameParsed] = parsedFontFamily, props.insertCSS) {
|
|
28
|
+
const [ff_name, ff_language] = fontFamilyNameParsed.includes("_") ? fontFamilyNameParsed.split("_") : [fontFamilyNameParsed],
|
|
29
|
+
fontVars = registerFontVariables(parsedFontFamily),
|
|
30
|
+
fontDeclaration = {
|
|
31
|
+
[fontFamilyNameIn]: {
|
|
32
|
+
name: ff_name.slice(1),
|
|
33
|
+
declarations: fontVars,
|
|
34
|
+
language: ff_language
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name,
|
|
39
|
+
declarations,
|
|
40
|
+
language = "default"
|
|
41
|
+
} = fontDeclaration[fontFamilyNameIn],
|
|
42
|
+
fontSelector = `.font_${name}`,
|
|
43
|
+
langSelector = `:root .t_lang-${name}-${language} ${fontSelector}`,
|
|
44
|
+
selectors = language === "default" ? ` ${fontSelector}, ${langSelector}` : langSelector,
|
|
45
|
+
cssRuleSets = declarationsToRuleSet(declarations, selectors),
|
|
46
|
+
id = `t_font_style_${fontFamilyNameIn}`,
|
|
47
|
+
existing = document.querySelector(`#${id}`),
|
|
48
|
+
style = document.createElement("style");
|
|
49
|
+
return style.id = id, style.appendChild(document.createTextNode(`${cssRuleSets}`)), document.head.appendChild(style), existing && existing.parentElement?.removeChild(existing), {
|
|
50
|
+
fontFamilyToken,
|
|
51
|
+
fontDeclaration
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export { addFont };
|
|
57
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isWeb","createVariables","getConfig","parseFont","registerFontVariables","addFont","props","config","fontFamilyName","fontFamilyNameIn","fontFamily","fontFamilyIn","process","env","NODE_ENV","Error","fonts","update","sep","cssStyleSeparator","declarationsToRuleSet","decs","selector","join","fontFamilyToken","parsedFontFamily","fontFamilyNameParsed","fontsParsed","insertCSS","ff_name","ff_language","includes","split","fontVars","fontDeclaration","name","slice","declarations","language","fontSelector","langSelector","selectors","cssRuleSets","id","existing","document","querySelector","style","createElement","appendChild","createTextNode","head","parentElement","removeChild"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,KAAA,QAAa;AACtB,SACEC,eAAA,EACAC,SAAA,EACAC,SAAA,EACAC,qBAAA,QACK;AAGA,SAASC,QAAQC,KAAA,EAMrB;EACD,MAAMC,MAAA,GAASL,SAAA,CAAU;IACnB;MAAEM,cAAA,EAAgBC,gBAAA;MAAkBC,UAAA,EAAYC;IAAa,IAAIL,KAAA;EAEvE,IAAIM,OAAA,CAAQC,GAAA,CAAIC,QAAA,KAAa,eAAe;IAC1C,IAAI,CAACP,MAAA,EACH,MAAM,IAAIQ,KAAA,CAAM,WAAW;IAE7B,MAAML,UAAA,GAAaH,MAAA,CAAOS,KAAA,CAAMP,gBAAgB;IAChD,IAAI,CAACH,KAAA,CAAMW,MAAA,IAAUP,UAAA,EACnB,OAAO;MAAEA;IAAW;EAExB;EAEAH,MAAA,CAAOS,KAAA,CAAMP,gBAAgB,IAAIE,YAAA;EAEjC,MAAMO,GAAA;EAAA;EAEJN,OAAA,CAAQC,GAAA,CAAIC,QAAA,KAAa,gBAAgBP,MAAA,CAAOY,iBAAA,IAAqB,MAAM;EAC7E,SAASC,sBAAsBC,IAAA,EAAgBC,QAAA,GAAW,IAAI;IAC5D,OAAO,QAAQA,QAAQ,KAAKJ,GAAG,GAAG,CAAC,G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AA,CAAME,WAAA,CAAYJ,QAAA,CAASK,cAAA,CAAe,GAAGR,WAAW,EAAE,CAAC,GAC3DG,QAAA,CAASM,IAAA,CAAKF,WAAA,CAAYF,KAAK,GAC3BH,QAAA,IACFA,QAAA,CAASQ,aAAA,EAAeC,WAAA,CAAYT,QAAQ,GAEvC;QACLpB,eAAA;QACAU;MACF;IACF;EACF;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { isWeb } from "@hanzogui/constants";
|
|
2
|
+
import { createVariables, getConfig, parseFont, registerFontVariables } from "@hanzogui/web";
|
|
3
|
+
function addFont(props) {
|
|
4
|
+
var config = getConfig(),
|
|
5
|
+
{
|
|
6
|
+
fontFamilyName: fontFamilyNameIn,
|
|
7
|
+
fontFamily: fontFamilyIn
|
|
8
|
+
} = props;
|
|
9
|
+
if (process.env.NODE_ENV === "development") {
|
|
10
|
+
if (!config) throw new Error("No config");
|
|
11
|
+
var fontFamily = config.fonts[fontFamilyNameIn];
|
|
12
|
+
if (!props.update && fontFamily) return {
|
|
13
|
+
fontFamily
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
config.fonts[fontFamilyNameIn] = fontFamilyIn;
|
|
17
|
+
var sep =
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
process.env.NODE_ENV === "development" ? config.cssStyleSeparator || " " : "";
|
|
20
|
+
function declarationsToRuleSet(decs) {
|
|
21
|
+
var selector = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "";
|
|
22
|
+
return `:root${selector} {${sep}${[...decs].join(`;${sep}`)}${sep}}`;
|
|
23
|
+
}
|
|
24
|
+
if (isWeb) {
|
|
25
|
+
var fontFamilyToken = createVariables(fontFamilyIn, "f", !0),
|
|
26
|
+
parsedFontFamily = parseFont(fontFamilyToken),
|
|
27
|
+
fontFamilyNameParsed = `$${fontFamilyNameIn}`;
|
|
28
|
+
if (config.fontsParsed[fontFamilyNameParsed] = parsedFontFamily, props.insertCSS) {
|
|
29
|
+
var [ff_name, ff_language] = fontFamilyNameParsed.includes("_") ? fontFamilyNameParsed.split("_") : [fontFamilyNameParsed],
|
|
30
|
+
fontVars = registerFontVariables(parsedFontFamily),
|
|
31
|
+
fontDeclaration = {
|
|
32
|
+
[fontFamilyNameIn]: {
|
|
33
|
+
name: ff_name.slice(1),
|
|
34
|
+
declarations: fontVars,
|
|
35
|
+
language: ff_language
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name,
|
|
40
|
+
declarations,
|
|
41
|
+
language = "default"
|
|
42
|
+
} = fontDeclaration[fontFamilyNameIn],
|
|
43
|
+
fontSelector = `.font_${name}`,
|
|
44
|
+
langSelector = `:root .t_lang-${name}-${language} ${fontSelector}`,
|
|
45
|
+
selectors = language === "default" ? ` ${fontSelector}, ${langSelector}` : langSelector,
|
|
46
|
+
cssRuleSets = declarationsToRuleSet(declarations, selectors),
|
|
47
|
+
id = `t_font_style_${fontFamilyNameIn}`,
|
|
48
|
+
existing = document.querySelector(`#${id}`),
|
|
49
|
+
style = document.createElement("style");
|
|
50
|
+
if (style.id = id, style.appendChild(document.createTextNode(`${cssRuleSets}`)), document.head.appendChild(style), existing) {
|
|
51
|
+
var _existing_parentElement;
|
|
52
|
+
(_existing_parentElement = existing.parentElement) === null || _existing_parentElement === void 0 || _existing_parentElement.removeChild(existing);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
fontFamilyToken,
|
|
56
|
+
fontDeclaration
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export { addFont };
|
|
62
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isWeb","createVariables","getConfig","parseFont","registerFontVariables","addFont","props","config","fontFamilyName","fontFamilyNameIn","fontFamily","fontFamilyIn","process","env","NODE_ENV","Error","fonts","update","sep","cssStyleSeparator","declarationsToRuleSet","decs","selector","arguments","length","join","fontFamilyToken","parsedFontFamily","fontFamilyNameParsed","fontsParsed","insertCSS","ff_name","ff_language","includes","split","fontVars","fontDeclaration","name","slice","declarations","language","fontSelector","langSelector","selectors","cssRuleSets","id","existing","document","querySelector","style","createElement","appendChild","createTextNode","head","_existing_parentElement","parentElement","removeChild"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,KAAA,QAAa;AACtB,SAAAC,eAAA,EAAAC,SAAA,EAAAC,SAAA,EAAAC,qBAAA;AAAA,SACEC,QAAAC,KAAA;EACA,IAAAC,MAAA,GAAAL,SAAA;IAAA;MAAAM,cAAA,EAAAC,gBAAA;MAAAC,UAAA,EAAAC;IAAA,IAAAL,KAAA;EACA,IAAAM,OAAA,CAAAC,GAAA,CAAAC,QAAA;IACA,KAAAP,MAAA,QACK,IAAAQ,KAAA;IAGA,IAAAL,UAAiB,GAAAH,MAMrB,CAAAS,KAAA,CAAAP,gBAAA;IACD,IAAM,CAAAH,KAAA,CAAAW,MAAS,IAAAP,UACP,EAEJ;MACEA;IACF;EAEF;EACAH,MAAI,CAACS,KAAA,CAAMP,gBAAU,IAAAE,YAAA;EACnB,IAAAO,GAAA;EAEJ;EAEAN,OAAO,CAAAC,GAAM,CAAAC,QAAA,kBAAoB,GAAAP,MAAA,CAAAY,iBAAA,YAEjC;EAAM,SAAAC,sBAAAC,IAAA;IAEJ,IAAAC,QAAY,GAAAC,SAAA,CAAaC,MAAA,QAAAD,SAAuB,iBAAAA,SAAqB,CAAM;IAAA,eAAAD,QAAA,KAAAJ,GAAA,IAC7E,GAAAG,IAAS,CACP,CAAAI,IAAA,CAAO,IAAAP,GAAA,EAAQ,IAAAA,GAAQ;EACzB;EAEA,IAAIlB,KAAA,EAAO;IACT,IAAA0B,eAAM,GAAAzB,eAAkB,CAAAU,YAAgB,KAAc,IAAK;MAAIgB,gBACzD,GAAAxB,SAAmB,CAAAuB,eAAU;MAAeE,oBAC5C,OAAuBnB,gBAAI,EAAgB;IAG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+
{
|
|
2
|
+
"name": "@hanzogui/font",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"source": "src/index.ts",
|
|
5
|
+
"files": [
|
|
6
|
+
"src",
|
|
7
|
+
"types",
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "dist/cjs",
|
|
13
|
+
"module": "dist/esm",
|
|
14
|
+
"types": "./types/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./types/index.d.ts",
|
|
19
|
+
"react-native": "./dist/esm/index.native.js",
|
|
20
|
+
"browser": "./dist/esm/index.mjs",
|
|
21
|
+
"module": "./dist/esm/index.mjs",
|
|
22
|
+
"import": "./dist/esm/index.mjs",
|
|
23
|
+
"require": "./dist/cjs/index.cjs",
|
|
24
|
+
"default": "./dist/esm/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "hanzo-gui-build",
|
|
32
|
+
"watch": "hanzo-gui-build --watch",
|
|
33
|
+
"clean": "hanzo-gui-build clean",
|
|
34
|
+
"clean:build": "hanzo-gui-build clean:build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@hanzogui/constants": "workspace:*",
|
|
38
|
+
"@hanzogui/web": "workspace:*"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@hanzogui/build": "workspace:*",
|
|
42
|
+
"react": ">=19"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": ">=19"
|
|
46
|
+
},
|
|
47
|
+
"skipPublishIfUnchanged": false
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { isWeb } from '@hanzogui/constants'
|
|
2
|
+
import {
|
|
3
|
+
createVariables,
|
|
4
|
+
getConfig,
|
|
5
|
+
parseFont,
|
|
6
|
+
registerFontVariables,
|
|
7
|
+
} from '@hanzogui/web'
|
|
8
|
+
import type { CreateGuiProps } from '@hanzogui/web'
|
|
9
|
+
|
|
10
|
+
export function addFont(props: {
|
|
11
|
+
fontFamilyName: string
|
|
12
|
+
fontFamily: CreateGuiProps['fonts'][keyof CreateGuiProps['fonts']]
|
|
13
|
+
insertCSS?: boolean
|
|
14
|
+
// TODO make sure to add updateFont
|
|
15
|
+
update?: boolean
|
|
16
|
+
}) {
|
|
17
|
+
const config = getConfig()
|
|
18
|
+
const { fontFamilyName: fontFamilyNameIn, fontFamily: fontFamilyIn } = props
|
|
19
|
+
|
|
20
|
+
if (process.env.NODE_ENV === 'development') {
|
|
21
|
+
if (!config) {
|
|
22
|
+
throw new Error('No config')
|
|
23
|
+
}
|
|
24
|
+
const fontFamily = config.fonts[fontFamilyNameIn]
|
|
25
|
+
if (!props.update && fontFamily) {
|
|
26
|
+
return { fontFamily }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
config.fonts[fontFamilyNameIn] = fontFamilyIn
|
|
31
|
+
|
|
32
|
+
const sep =
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
process.env.NODE_ENV === 'development' ? config.cssStyleSeparator || ' ' : ''
|
|
35
|
+
function declarationsToRuleSet(decs: string[], selector = '') {
|
|
36
|
+
return `:root${selector} {${sep}${[...decs].join(`;${sep}`)}${sep}}`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (isWeb) {
|
|
40
|
+
const fontFamilyToken = createVariables(fontFamilyIn, 'f', true)
|
|
41
|
+
const parsedFontFamily = parseFont(fontFamilyToken)
|
|
42
|
+
const fontFamilyNameParsed = `$${fontFamilyNameIn}`
|
|
43
|
+
config.fontsParsed[fontFamilyNameParsed] = parsedFontFamily
|
|
44
|
+
|
|
45
|
+
if (props.insertCSS) {
|
|
46
|
+
const [ff_name, ff_language] = fontFamilyNameParsed.includes('_')
|
|
47
|
+
? fontFamilyNameParsed.split('_')
|
|
48
|
+
: [fontFamilyNameParsed]
|
|
49
|
+
const fontVars = registerFontVariables(parsedFontFamily)
|
|
50
|
+
const fontDeclaration = {
|
|
51
|
+
[fontFamilyNameIn]: {
|
|
52
|
+
name: ff_name.slice(1),
|
|
53
|
+
declarations: fontVars,
|
|
54
|
+
language: ff_language,
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const {
|
|
59
|
+
name,
|
|
60
|
+
declarations,
|
|
61
|
+
language = 'default',
|
|
62
|
+
} = fontDeclaration[fontFamilyNameIn]
|
|
63
|
+
const fontSelector = `.font_${name}`
|
|
64
|
+
const langSelector = `:root .t_lang-${name}-${language} ${fontSelector}`
|
|
65
|
+
const selectors =
|
|
66
|
+
language === 'default' ? ` ${fontSelector}, ${langSelector}` : langSelector
|
|
67
|
+
const cssRuleSets = declarationsToRuleSet(declarations, selectors)
|
|
68
|
+
|
|
69
|
+
const id = `t_font_style_${fontFamilyNameIn}`
|
|
70
|
+
const existing = document.querySelector(`#${id}`)
|
|
71
|
+
const style = document.createElement('style')
|
|
72
|
+
style.id = id
|
|
73
|
+
style.appendChild(document.createTextNode(`${cssRuleSets}`))
|
|
74
|
+
document.head.appendChild(style)
|
|
75
|
+
if (existing) {
|
|
76
|
+
existing.parentElement?.removeChild(existing)
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
fontFamilyToken,
|
|
80
|
+
fontDeclaration,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CreateGuiProps } from '@hanzogui/web';
|
|
2
|
+
export declare function addFont(props: {
|
|
3
|
+
fontFamilyName: string;
|
|
4
|
+
fontFamily: CreateGuiProps['fonts'][keyof CreateGuiProps['fonts']];
|
|
5
|
+
insertCSS?: boolean;
|
|
6
|
+
update?: boolean;
|
|
7
|
+
}): {
|
|
8
|
+
fontFamily: import("@hanzogui/web").GenericFont<string | number | symbol>;
|
|
9
|
+
fontFamilyToken?: undefined;
|
|
10
|
+
fontDeclaration?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
fontFamilyToken: never;
|
|
13
|
+
fontDeclaration: {
|
|
14
|
+
[x: string]: {
|
|
15
|
+
name: string;
|
|
16
|
+
declarations: string[];
|
|
17
|
+
language: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
fontFamily?: undefined;
|
|
21
|
+
} | undefined;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAEnD,wBAAgB,OAAO,CAAC,KAAK,EAAE;IAC7B,cAAc,EAAE,MAAM,CAAA;IACtB,UAAU,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;IAClE,SAAS,CAAC,EAAE,OAAO,CAAA;IAEnB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;;;;;;;;;;;;;;cAoEA"}
|