@twreporter/react-components 8.11.1 → 8.12.0-rc.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/CHANGELOG.md +18 -0
- package/lib/hook/index.js +10 -1
- package/lib/hook/use-font-face-observer.js +65 -0
- package/lib/text/utils/webfonts.js +67 -0
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,24 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [8.12.0-rc.0](https://github.com/twreporter/twreporter-npm-packages/compare/@twreporter/react-components@8.11.1...@twreporter/react-components@8.12.0-rc.0) (2022-08-10)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **react-components:** add callback as dep of use-font-observer-hook ([9f91ce9](https://github.com/twreporter/twreporter-npm-packages/commit/9f91ce928b0e94c8b29600f44438ea37f45e5c03))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* **react-components:** add NotoSansTC fontface ([046cdb5](https://github.com/twreporter/twreporter-npm-packages/commit/046cdb501b5133fb120cf66401929145ad2c79da))
|
|
17
|
+
* **react-components:** add use-font-face-observer hook ([f2ffa6d](https://github.com/twreporter/twreporter-npm-packages/commit/f2ffa6d9215737046d246aa34dd468a2ff0d15f2))
|
|
18
|
+
* **react-components:** export font files as `fontGCSFiles` ([a8b940c](https://github.com/twreporter/twreporter-npm-packages/commit/a8b940c796dc039cb769f09b8d6bcc0002a22bcf))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
6
24
|
## [8.11.1](https://github.com/twreporter/twreporter-npm-packages/compare/@twreporter/react-components@8.11.1-rc.0...@twreporter/react-components@8.11.1) (2022-08-09)
|
|
7
25
|
|
|
8
26
|
**Note:** Version bump only for package @twreporter/react-components
|
package/lib/hook/index.js
CHANGED
|
@@ -9,13 +9,22 @@ Object.defineProperty(exports, "useOutsideClick", {
|
|
|
9
9
|
return _useOutsideClick["default"];
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
|
+
Object.defineProperty(exports, "useFontFaceObserver", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _useFontFaceObserver["default"];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
12
18
|
exports["default"] = void 0;
|
|
13
19
|
|
|
14
20
|
var _useOutsideClick = _interopRequireDefault(require("./use-outside-click"));
|
|
15
21
|
|
|
22
|
+
var _useFontFaceObserver = _interopRequireDefault(require("./use-font-face-observer"));
|
|
23
|
+
|
|
16
24
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
17
25
|
|
|
18
26
|
var _default = {
|
|
19
|
-
useOutsideClick: _useOutsideClick["default"]
|
|
27
|
+
useOutsideClick: _useOutsideClick["default"],
|
|
28
|
+
useFontFaceObserver: _useFontFaceObserver["default"]
|
|
20
29
|
};
|
|
21
30
|
exports["default"] = _default;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
|
|
10
|
+
var _fontfaceobserverEs = _interopRequireDefault(require("fontfaceobserver-es"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
|
|
14
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
15
|
+
|
|
16
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
17
|
+
|
|
18
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
19
|
+
|
|
20
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
21
|
+
|
|
22
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
23
|
+
|
|
24
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
25
|
+
|
|
26
|
+
var useFontFaceObserver = function useFontFaceObserver() {
|
|
27
|
+
var fontFaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
28
|
+
var callback = arguments.length > 1 ? arguments[1] : undefined;
|
|
29
|
+
|
|
30
|
+
var _useState = (0, _react.useState)(false),
|
|
31
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
32
|
+
isResolved = _useState2[0],
|
|
33
|
+
setIsResolved = _useState2[1];
|
|
34
|
+
|
|
35
|
+
var fontFacesString = JSON.stringify(fontFaces);
|
|
36
|
+
(0, _react.useEffect)(function () {
|
|
37
|
+
var promises = JSON.parse(fontFacesString).map(function (_ref) {
|
|
38
|
+
var family = _ref.family,
|
|
39
|
+
weight = _ref.weight,
|
|
40
|
+
style = _ref.style,
|
|
41
|
+
stretch = _ref.stretch;
|
|
42
|
+
return new _fontfaceobserverEs["default"](family, {
|
|
43
|
+
weight: weight,
|
|
44
|
+
style: style,
|
|
45
|
+
stretch: stretch
|
|
46
|
+
}).load('測試文字');
|
|
47
|
+
});
|
|
48
|
+
Promise.all(promises).then(function () {
|
|
49
|
+
if (typeof callback !== 'function') {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
callback();
|
|
54
|
+
return setIsResolved(true);
|
|
55
|
+
})["catch"](function (e) {
|
|
56
|
+
// eslint-disable-next-line no-console
|
|
57
|
+
console.error("An error occurred during font loading");
|
|
58
|
+
console.log(e);
|
|
59
|
+
});
|
|
60
|
+
}, [fontFacesString, callback]);
|
|
61
|
+
return isResolved;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
var _default = useFontFaceObserver;
|
|
65
|
+
exports["default"] = _default;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _font = require("@twreporter/core/lib/constants/font");
|
|
9
|
+
|
|
10
|
+
var _forEach = _interopRequireDefault(require("lodash/forEach"));
|
|
11
|
+
|
|
12
|
+
var _keys = _interopRequireDefault(require("lodash/keys"));
|
|
13
|
+
|
|
14
|
+
var _reduce = _interopRequireDefault(require("lodash/reduce"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
17
|
+
|
|
18
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
+
|
|
20
|
+
var _ = {
|
|
21
|
+
forEach: _forEach["default"],
|
|
22
|
+
keys: _keys["default"],
|
|
23
|
+
reduce: _reduce["default"]
|
|
24
|
+
};
|
|
25
|
+
var baseGCSDir = 'https://www.twreporter.org/assets/font/';
|
|
26
|
+
var fileExt = '.woff2';
|
|
27
|
+
|
|
28
|
+
var gcsFontFolder = _defineProperty({}, _font.fonts.notoSansTC, 'NotoSansTC');
|
|
29
|
+
|
|
30
|
+
var fontWeightKeys = _.keys(_font.fontWeight); // add @font-face to global style to use self-hosted font for performance reasons, to be more precise please check the issue below:
|
|
31
|
+
// https://twreporter-org.atlassian.net/browse/TWREPORTER-318?atlOrigin=eyJpIjoiNjg4OTQ2MWU2MGIxNGEzMGE0NDY2ZDNmZGRhOWExZDEiLCJwIjoiaiJ9
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
var getFontFaces = function getFontFaces(_ref) {
|
|
35
|
+
var font = _ref.font,
|
|
36
|
+
folder = _ref.folder;
|
|
37
|
+
|
|
38
|
+
var fontFaceCSSTemplate = function fontFaceCSSTemplate(_ref2) {
|
|
39
|
+
var fontWeightKey = _ref2.fontWeightKey;
|
|
40
|
+
return "\n @font-face {\n font-family: \"".concat(font, "\";\n font-weight: ").concat(_font.fontWeight[fontWeightKey], ";\n font-display: swap;\n src: url(\"").concat(baseGCSDir).concat(folder, "/").concat(fontWeightKey).concat(fileExt, "\");\n }\n ");
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return _.reduce(fontWeightKeys, function (fontFaces, fontWeightKey) {
|
|
44
|
+
return fontFaces + fontFaceCSSTemplate({
|
|
45
|
+
fontWeightKey: fontWeightKey
|
|
46
|
+
});
|
|
47
|
+
}, '');
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
var fontFaces = _defineProperty({}, _font.fonts.notoSansTC, getFontFaces({
|
|
51
|
+
font: _font.fonts.notoSansTC,
|
|
52
|
+
folder: gcsFontFolder[_font.fonts.notoSansTC]
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
var fontGCSFiles = [];
|
|
56
|
+
|
|
57
|
+
_.forEach(fontFaces, function (fontFace, font) {
|
|
58
|
+
_.forEach(fontWeightKeys, function (fontWeightKey) {
|
|
59
|
+
fontGCSFiles.push("".concat(baseGCSDir).concat(gcsFontFolder[font], "/").concat(fontWeightKey).concat(fileExt));
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
var _default = {
|
|
64
|
+
fontFaces: fontFaces,
|
|
65
|
+
fontGCSFiles: fontGCSFiles
|
|
66
|
+
};
|
|
67
|
+
exports["default"] = _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twreporter/react-components",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.12.0-rc.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"repository": "https://github.com/twreporter/twreporter-npm-packages.git",
|
|
6
6
|
"author": "twreporter <developer@twreporter.org>",
|
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
"chromatic": "npx chromatic --exit-zero-on-changes"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@twreporter/core": "^1.
|
|
18
|
+
"@twreporter/core": "^1.5.0-rc.0",
|
|
19
19
|
"@twreporter/redux": "^7.2.2",
|
|
20
|
+
"fontfaceobserver-es": "^3.3.3",
|
|
20
21
|
"hoist-non-react-statics": "^2.3.1",
|
|
21
22
|
"lodash": "^4.0.0",
|
|
22
23
|
"memoize-one": "^5.0.5",
|
|
@@ -43,5 +44,5 @@
|
|
|
43
44
|
"babel-loader": "^8.2.4",
|
|
44
45
|
"chromatic": "^6.5.4"
|
|
45
46
|
},
|
|
46
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "1581b4cbe2575efda67131f0d6954cb7dfd3a82d"
|
|
47
48
|
}
|