@arcblock/ux 2.4.44 → 2.4.45

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,108 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /* eslint-disable prefer-destructuring */
9
+
10
+ /* eslint-disable no-bitwise */
11
+ // copy from https://etherscan.io/jss/blockies.js
12
+ const randseed = new Array(4);
13
+
14
+ function seedrand(seed) {
15
+ for (let i = 0; i < randseed.length; i++) {
16
+ randseed[i] = 0;
17
+ }
18
+
19
+ for (let i = 0; i < seed.length; i++) {
20
+ randseed[i % 4] = (randseed[i % 4] << 5) - randseed[i % 4] + seed.charCodeAt(i);
21
+ }
22
+ }
23
+
24
+ function rand() {
25
+ const t = randseed[0] ^ randseed[0] << 11;
26
+ randseed[0] = randseed[1];
27
+ randseed[1] = randseed[2];
28
+ randseed[2] = randseed[3];
29
+ randseed[3] = randseed[3] ^ randseed[3] >> 19 ^ t ^ t >> 8;
30
+ return (randseed[3] >>> 0) / (1 << 31 >>> 0);
31
+ }
32
+
33
+ function createColor() {
34
+ const h = Math.floor(rand() * 360);
35
+ const s = "".concat(rand() * 60 + 40, "%");
36
+ const l = "".concat((rand() + rand() + rand() + rand()) * 25, "%");
37
+ const color = "hsl(".concat(h, ",").concat(s, ",").concat(l, ")");
38
+ return color;
39
+ }
40
+
41
+ function createImageData(size) {
42
+ const width = size;
43
+ const height = size;
44
+ const dataWidth = Math.ceil(width / 2);
45
+ const mirrorWidth = width - dataWidth;
46
+ const data = [];
47
+
48
+ for (let y = 0; y < height; y++) {
49
+ let row = [];
50
+
51
+ for (let x = 0; x < dataWidth; x++) {
52
+ row[x] = Math.floor(rand() * 2.3);
53
+ }
54
+
55
+ const r = row.slice(0, mirrorWidth);
56
+ r.reverse();
57
+ row = row.concat(r);
58
+
59
+ for (let i = 0; i < row.length; i++) {
60
+ data.push(row[i]);
61
+ }
62
+ }
63
+
64
+ return data;
65
+ }
66
+
67
+ function createCanvas(imageData, color, scale, bgcolor, spotcolor) {
68
+ const c = document.createElement('canvas');
69
+ const width = Math.sqrt(imageData.length); // eslint-disable-next-line no-multi-assign
70
+
71
+ c.width = c.height = width * scale;
72
+ const cc = c.getContext('2d');
73
+ cc.fillStyle = bgcolor;
74
+ cc.fillRect(0, 0, c.width, c.height);
75
+ cc.fillStyle = color;
76
+
77
+ for (let i = 0; i < imageData.length; i++) {
78
+ const row = Math.floor(i / width);
79
+ const col = i % width;
80
+ cc.fillStyle = imageData[i] === 1 ? color : spotcolor;
81
+
82
+ if (imageData[i]) {
83
+ cc.fillRect(col * scale, row * scale, scale, scale);
84
+ }
85
+ }
86
+
87
+ return c;
88
+ }
89
+
90
+ function createIcon(opts) {
91
+ // eslint-disable-next-line no-param-reassign
92
+ opts = opts || {};
93
+ const size = opts.size || 8;
94
+ const scale = opts.scale || 4;
95
+ const seed = opts.seed || Math.floor(Math.random() * 10 ** 16).toString(16);
96
+ seedrand(seed);
97
+ const color = opts.color || createColor();
98
+ const bgcolor = opts.bgcolor || createColor();
99
+ const spotcolor = opts.spotcolor || createColor();
100
+ const imageData = createImageData(size);
101
+ const canvas = createCanvas(imageData, color, scale, bgcolor, spotcolor);
102
+ return canvas;
103
+ }
104
+
105
+ var _default = {
106
+ createIcon
107
+ };
108
+ exports.default = _default;
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = AvatarWithErrorBoundary;
7
+
8
+ var _react = require("react");
9
+
10
+ var _propTypes = _interopRequireDefault(require("prop-types"));
11
+
12
+ var _reactErrorBoundary = require("react-error-boundary");
13
+
14
+ var _didMotif = require("@arcblock/did-motif");
15
+
16
+ var _Box = _interopRequireDefault(require("@mui/material/Box"));
17
+
18
+ var _Img = _interopRequireDefault(require("../Img"));
19
+
20
+ var _Util = require("../Util");
21
+
22
+ var _Theme = require("../Theme");
23
+
24
+ var _didMotif2 = _interopRequireDefault(require("./did-motif"));
25
+
26
+ var _etherscanBlockies = _interopRequireDefault(require("./etherscan-blockies"));
27
+
28
+ var _jsxRuntime = require("react/jsx-runtime");
29
+
30
+ var _templateObject, _templateObject2;
31
+
32
+ const _excluded = ["did", "size", "src", "variant", "animation", "shape"];
33
+
34
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
+
36
+ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
37
+
38
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
39
+
40
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
41
+
42
+ 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; }
43
+
44
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
45
+
46
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
47
+
48
+ // 参考: asset-chain @arcblock/did
49
+ const isEthereumDid = did => {
50
+ const address = did.replace('did:abt:', ''); // check if it has the basic requirements of an address
51
+
52
+ if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
53
+ return false;
54
+ }
55
+
56
+ return true;
57
+ }; // 参考: https://github.com/blocklet/block-explorer/issues/478#issuecomment-1038954976
58
+
59
+
60
+ function Avatar(props) {
61
+ const [imgError, setImgError] = (0, _react.useState)(false);
62
+ const newProps = (0, _Util.mergeProps)(props, Avatar, []);
63
+
64
+ const {
65
+ did = '',
66
+ size,
67
+ src,
68
+ variant,
69
+ animation,
70
+ shape
71
+ } = newProps,
72
+ rest = _objectWithoutProperties(newProps, _excluded); // ethereum blockies
73
+
74
+
75
+ const blockyIcon = (0, _react.useMemo)(() => {
76
+ if (isEthereumDid(did)) {
77
+ return _etherscanBlockies.default.createIcon({
78
+ seed: did.replace('did:abt:', '').toLowerCase(),
79
+ size: 8,
80
+ scale: 16
81
+ }).toDataURL();
82
+ }
83
+
84
+ return null;
85
+ }, [did]); // 如果显式传入 src 则直接使用 src
86
+
87
+ if (src && !imgError) {
88
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledImg, _objectSpread(_objectSpread({
89
+ width: size,
90
+ src: src,
91
+ alt: did,
92
+ onError: () => setImgError(true)
93
+ }, rest), {}, {
94
+ className: "avatar-img--".concat(variant, " ").concat(rest.className)
95
+ }));
96
+ } // 对于 eth address, 渲染成 blocky icon, 形状与 account role type 的 did motif 相似都为矩形, 高宽比为 0.7
97
+
98
+
99
+ if (blockyIcon) {
100
+ // blocky icon 要与灰色卡片矩形留有空间
101
+ const padding = size > 24 ? 4 : 2;
102
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(BlockyIconContainer, _objectSpread(_objectSpread({
103
+ $size: size
104
+ }, rest), {}, {
105
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
106
+ className: "blocky-icon-inner",
107
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Img.default, {
108
+ width: size * 0.7 - padding * 2,
109
+ src: blockyIcon,
110
+ alt: did
111
+ })
112
+ })
113
+ }));
114
+ }
115
+
116
+ if (did) {
117
+ // 渲染 did motif
118
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_didMotif2.default, _objectSpread({
119
+ did: did.replace('did:abt:', ''),
120
+ size: size,
121
+ animation: animation,
122
+ shape: _didMotif.Shape[(shape || '').toUpperCase()],
123
+ responsive: newProps.responsive
124
+ }, rest));
125
+ }
126
+
127
+ throw new Error("Invalid DID: ".concat(did));
128
+ }
129
+
130
+ Avatar.propTypes = {
131
+ did: _propTypes.default.string.isRequired,
132
+ size: _propTypes.default.number,
133
+ variant: _propTypes.default.oneOf(['circle', 'rounded', 'default']),
134
+ // animation 仅对 did motif 有效
135
+ animation: _propTypes.default.bool,
136
+ // shape 仅对 did motif 有效, 明确指定 motif shape, 而非由 did role type 自动推断 shape
137
+ shape: _propTypes.default.oneOf(['', 'rectangle', 'square', 'hexagon', 'circle'])
138
+ };
139
+ Avatar.defaultProps = {
140
+ size: 36,
141
+ variant: 'default',
142
+ animation: false,
143
+ shape: ''
144
+ };
145
+ const BlockyIconContainer = (0, _Theme.styled)('div')(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n display: flex;\n align-items: center;\n width: ", "px;\n height: ", "px;\n .blocky-icon-inner {\n box-sizing: border-box;\n display: flex;\n justify-content: center;\n align-items: center;\n width: ", "px;\n height: ", "px;\n border-radius: ", "px;\n background: #ddd;\n }\n"])), props => props.$size, props => props.$size, props => props.$size, props => props.$size * 0.7, props => Math.min(10, Math.floor(0.1 * props.$size + 2)));
146
+ const StyledImg = (0, _Theme.styled)(_Img.default)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n &.avatar-img--rounded {\n border-radius: 4px;\n overflow: hidden;\n }\n &.avatar-img--circle {\n border-radius: 100%;\n overflow: hidden;\n }\n"])));
147
+
148
+ function AvatarWithErrorBoundary(props) {
149
+ const size = props.size || 36;
150
+ const borderRadius = {
151
+ rounded: '4px',
152
+ circle: '100%'
153
+ }[props.variant] || 0;
154
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactErrorBoundary.ErrorBoundary // eslint-disable-next-line react/no-unstable-nested-components
155
+ , {
156
+ fallbackRender: () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_Box.default, {
157
+ width: size,
158
+ height: size,
159
+ bgcolor: "grey.300",
160
+ borderRadius: borderRadius
161
+ }),
162
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Avatar, _objectSpread({}, props))
163
+ });
164
+ }
165
+
166
+ AvatarWithErrorBoundary.propTypes = Avatar.propTypes;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = DidLogo;
7
+
8
+ var _propTypes = _interopRequireDefault(require("prop-types"));
9
+
10
+ var _DidLogo = _interopRequireDefault(require("@arcblock/icons/lib/DidLogo"));
11
+
12
+ var _jsxRuntime = require("react/jsx-runtime");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ const defaultStyle = {
17
+ width: 'auto',
18
+ height: '1em',
19
+ fill: 'currentColor'
20
+ };
21
+
22
+ function DidLogo(_ref) {
23
+ let {
24
+ style,
25
+ size,
26
+ className
27
+ } = _ref;
28
+ const height = Number(size) > 0 ? "".concat(Number(size), "px") : size;
29
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DidLogo.default, {
30
+ className: "".concat(className).trim(),
31
+ style: Object.assign({}, defaultStyle, style, height ? {
32
+ height
33
+ } : {})
34
+ });
35
+ }
36
+
37
+ DidLogo.propTypes = {
38
+ style: _propTypes.default.object,
39
+ size: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
40
+ className: _propTypes.default.string
41
+ };
42
+ DidLogo.defaultProps = {
43
+ style: defaultStyle,
44
+ size: 0,
45
+ className: ''
46
+ };