mirror of
https://github.com/anyproto/anytype-ts.git
synced 2025-06-08 05:57:02 +09:00
iconCache refactoring
This commit is contained in:
parent
a3d759913b
commit
2bcbb7ccf4
2 changed files with 38 additions and 37 deletions
|
@ -99,8 +99,6 @@ const FontSize = {
|
|||
|
||||
const Ghost = require('img/icon/ghost.svg');
|
||||
|
||||
const iconCache: Map<string, string> = new Map();
|
||||
|
||||
const CheckboxTask = {
|
||||
'': {
|
||||
0: require('img/icon/object/checkbox0.svg'),
|
||||
|
@ -332,21 +330,8 @@ const IconObject = observer(forwardRef<IconObjectRefProps, Props>((props, ref) =
|
|||
};
|
||||
|
||||
const typeIcon = (id: string, option: number, color?: string) => {
|
||||
const key = [ id, iconSize, option ].join('-');
|
||||
|
||||
let ret = '';
|
||||
if (iconCache.has(key)) {
|
||||
ret = iconCache.get(key);
|
||||
} else {
|
||||
try {
|
||||
const newColor = color || bgByOption(option);
|
||||
|
||||
ret = U.Common.updateSvg(require(`img/icon/type/default/${id}.svg`), { size, fill: newColor, stroke: newColor });
|
||||
iconCache.set(key, ret);
|
||||
} catch (e) { /**/ };
|
||||
};
|
||||
|
||||
return ret;
|
||||
const newColor = color || bgByOption(option);
|
||||
return U.Common.updateSvg(require(`img/icon/type/default/${id}.svg`), { id, size, fill: newColor, stroke: newColor });
|
||||
};
|
||||
|
||||
switch (layout) {
|
||||
|
@ -484,7 +469,7 @@ const IconObject = observer(forwardRef<IconObjectRefProps, Props>((props, ref) =
|
|||
const img = node.find('img');
|
||||
|
||||
img.attr({
|
||||
src: U.Common.updateSvg(require('img/icon/error.svg'), { size, fill: J.Theme[theme]?.error }),
|
||||
src: U.Common.updateSvg(require('img/icon/error.svg'), { id: 'error', size, fill: J.Theme[theme]?.error }),
|
||||
class: `iconCommon c${IconSize[size]}`
|
||||
});
|
||||
};
|
||||
|
|
|
@ -8,6 +8,7 @@ const katex = require('katex');
|
|||
require('katex/dist/contrib/mhchem');
|
||||
|
||||
const TEST_HTML = /<[^>]*>/;
|
||||
const iconCache: Map<string, string> = new Map();
|
||||
|
||||
class UtilCommon {
|
||||
|
||||
|
@ -1129,30 +1130,45 @@ class UtilCommon {
|
|||
};
|
||||
|
||||
updateSvg (src: string, param: any) {
|
||||
const { size, fill, stroke } = param;
|
||||
const chunk = src.split('base64,')[1];
|
||||
const decoded = atob(chunk).replace(/_COLOR_VAR_/g, fill);
|
||||
const obj = $(decoded);
|
||||
const attr: any = {};
|
||||
const id = String(param.id || '');
|
||||
const size = Number(param.size) || 0;
|
||||
const fill = String(param.fill || '');
|
||||
const stroke = String(param.stroke || '');
|
||||
const key = [ id, size, fill, stroke ].join('-');
|
||||
|
||||
if (size) {
|
||||
attr.width = size;
|
||||
attr.height = size;
|
||||
if (iconCache[key]) {
|
||||
return iconCache[key];
|
||||
};
|
||||
|
||||
if (fill) {
|
||||
attr.fill = fill;
|
||||
};
|
||||
let ret = '';
|
||||
try {
|
||||
const chunk = src.split('base64,')[1];
|
||||
const decoded = atob(chunk).replace(/_COLOR_VAR_/g, fill);
|
||||
const obj = $(decoded);
|
||||
const attr: any = {};
|
||||
|
||||
if (stroke) {
|
||||
attr.stroke = stroke;
|
||||
};
|
||||
if (size) {
|
||||
attr.width = size;
|
||||
attr.height = size;
|
||||
};
|
||||
|
||||
if (this.objectLength(attr)) {
|
||||
obj.attr(attr);
|
||||
};
|
||||
|
||||
return 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(obj[0].outerHTML)));
|
||||
if (fill) {
|
||||
attr.fill = fill;
|
||||
};
|
||||
|
||||
if (stroke) {
|
||||
attr.stroke = stroke;
|
||||
};
|
||||
|
||||
if (this.objectLength(attr)) {
|
||||
obj.attr(attr);
|
||||
};
|
||||
|
||||
ret = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(obj[0].outerHTML)));
|
||||
} catch (e) { /**/ };
|
||||
|
||||
iconCache[key] = ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue