mirror of https://github.com/alibaba/ice.git
feat: add storage for canvas (#6399)
* feat: add storage for canvas * feat: add changeset * feat: add storage for script * feat: add set value * feat: modify userKVStorage * feat: modify getItem * feat: add bizID * chore: add @ice/runtime to dev dependencied
This commit is contained in:
parent
2e22ce4efc
commit
4d5112bfe3
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@ice/cache-canvas': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: add storage for canvas
|
||||||
|
|
@ -24,13 +24,15 @@ export default function Home() {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
console.log('canvas paint ready!');
|
console.log('canvas paint ready!');
|
||||||
resolve(true);
|
resolve(true);
|
||||||
}, 5000);
|
}, 10000);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h2 className={styles.title}>Home Page</h2>
|
<h2 className={styles.title}>Home Page</h2>
|
||||||
<CacheCanvas
|
<CacheCanvas
|
||||||
|
bizID={'test'}
|
||||||
ref={childRef}
|
ref={childRef}
|
||||||
id={GAME_CANVAS_ID}
|
id={GAME_CANVAS_ID}
|
||||||
init={initFunc}
|
init={initFunc}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,9 @@ declare global {
|
||||||
call: Function;
|
call: Function;
|
||||||
};
|
};
|
||||||
_windvane_backControl: Function | null;
|
_windvane_backControl: Function | null;
|
||||||
|
__megability_bridge__: {
|
||||||
|
syncCall: Function;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,6 +36,7 @@ export type RefCacheCanvas = {
|
||||||
|
|
||||||
export type CacheCanvasProps = {
|
export type CacheCanvasProps = {
|
||||||
id: string;
|
id: string;
|
||||||
|
bizID: string;
|
||||||
init: () => Promise<any>;
|
init: () => Promise<any>;
|
||||||
useCache?: Boolean;
|
useCache?: Boolean;
|
||||||
getSnapshot?: () => String;
|
getSnapshot?: () => String;
|
||||||
|
|
@ -50,6 +54,7 @@ export const CacheCanvas = forwardRef((props: CacheCanvasProps, ref) => {
|
||||||
fallback,
|
fallback,
|
||||||
style,
|
style,
|
||||||
className,
|
className,
|
||||||
|
bizID = '',
|
||||||
...rest
|
...rest
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
|
|
@ -69,9 +74,11 @@ export const CacheCanvas = forwardRef((props: CacheCanvasProps, ref) => {
|
||||||
}
|
}
|
||||||
// Cache base64 string when canvas rendered.
|
// Cache base64 string when canvas rendered.
|
||||||
if (renderedCanvas && strBase64) {
|
if (renderedCanvas && strBase64) {
|
||||||
Storage.setItem(cacheKey, strBase64);
|
Storage.setItem(cacheKey, strBase64, {
|
||||||
|
bizID,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, [id, renderedCanvas, cacheKey, getSnapshot]);
|
}, [id, renderedCanvas, cacheKey, getSnapshot, bizID]);
|
||||||
|
|
||||||
useImperativeHandle(ref, () => ({
|
useImperativeHandle(ref, () => ({
|
||||||
cacheCanvasToStorage: cacheCanvasFunc,
|
cacheCanvasToStorage: cacheCanvasFunc,
|
||||||
|
|
@ -126,13 +133,13 @@ export const CacheCanvas = forwardRef((props: CacheCanvasProps, ref) => {
|
||||||
<img
|
<img
|
||||||
className={className}
|
className={className}
|
||||||
style={style}
|
style={style}
|
||||||
src={Storage.getItem(cacheKey) || ''}
|
src={Storage.getItem(cacheKey, { bizID }) || ''}
|
||||||
id={`canvas-img-${id}`}
|
id={`canvas-img-${id}`}
|
||||||
/>
|
/>
|
||||||
{
|
{
|
||||||
(typeof fallback === 'function') && (<div
|
(typeof fallback === 'function') && (<div
|
||||||
id={`fallback-${id}`}
|
id={`fallback-${id}`}
|
||||||
style={isNode || Storage.getItem(cacheKey) ? { display: 'none' } : { display: 'block' }}
|
style={isNode || Storage.getItem(cacheKey, { bizID }) ? { display: 'none' } : { display: 'block' }}
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
fallback()
|
fallback()
|
||||||
|
|
@ -141,8 +148,29 @@ export const CacheCanvas = forwardRef((props: CacheCanvasProps, ref) => {
|
||||||
}
|
}
|
||||||
<script
|
<script
|
||||||
dangerouslySetInnerHTML={{
|
dangerouslySetInnerHTML={{
|
||||||
__html: `
|
__html: `
|
||||||
const base64Data = localStorage.getItem('${cacheKey}');
|
let base64Data = '';
|
||||||
|
if (
|
||||||
|
window.__megability_bridge__ &&
|
||||||
|
window.__megability_bridge__.syncCall
|
||||||
|
) {
|
||||||
|
const canIUse = window.__megability_bridge__.syncCall('ability', 'available', {
|
||||||
|
ability: 'userKVStorage',
|
||||||
|
api: 'getItem',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (canIUse) {
|
||||||
|
const res = window.__megability_bridge__.syncCall('userKVStorage', 'getItem', { key: '${cacheKey}', bizID: '${bizID}' });
|
||||||
|
if (res && res.statusCode === 0 && res.data && res.data.result) {
|
||||||
|
base64Data = res.data.result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!base64Data) {
|
||||||
|
base64Data = localStorage.getItem(${JSON.stringify(cacheKey)});
|
||||||
|
}
|
||||||
|
|
||||||
const fallback = document.getElementById('fallback-${id}');
|
const fallback = document.getElementById('fallback-${id}');
|
||||||
if (base64Data) {
|
if (base64Data) {
|
||||||
const img = document.getElementById('canvas-img-${id}');
|
const img = document.getElementById('canvas-img-${id}');
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,31 @@
|
||||||
const cache = {};
|
const cache = {};
|
||||||
|
|
||||||
export const Storage = {
|
export const Storage = {
|
||||||
setItem: (key, value) => {
|
setItem: (key, value, { bizID = '' }) => {
|
||||||
try {
|
try {
|
||||||
if (typeof window === 'object' && window.localStorage) {
|
if (
|
||||||
|
typeof window !== 'undefined' &&
|
||||||
|
window.__megability_bridge__ &&
|
||||||
|
window.__megability_bridge__.syncCall
|
||||||
|
) {
|
||||||
|
const canIUse = window.__megability_bridge__.syncCall('ability', 'available', {
|
||||||
|
ability: 'userKVStorage',
|
||||||
|
api: 'setItem',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (canIUse) {
|
||||||
|
const res = window.__megability_bridge__.syncCall('userKVStorage', 'setItem', {
|
||||||
|
key,
|
||||||
|
value,
|
||||||
|
bizID,
|
||||||
|
});
|
||||||
|
if (res && res.statusCode === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof window !== 'undefined' && window.localStorage) {
|
||||||
return localStorage.setItem(key, value);
|
return localStorage.setItem(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -12,9 +34,33 @@ export const Storage = {
|
||||||
console.error('Storage setItem error:', e);
|
console.error('Storage setItem error:', e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getItem: (key) => {
|
getItem: (key, { bizID = '' }) => {
|
||||||
try {
|
try {
|
||||||
if (typeof window === 'object' && window.localStorage) {
|
if (
|
||||||
|
typeof window !== 'undefined' &&
|
||||||
|
window.__megability_bridge__ &&
|
||||||
|
window.__megability_bridge__.syncCall
|
||||||
|
) {
|
||||||
|
const canIUse = window.__megability_bridge__.syncCall('ability', 'available', {
|
||||||
|
ability: 'userKVStorage',
|
||||||
|
api: 'getItem',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (canIUse) {
|
||||||
|
const res = window.__megability_bridge__.syncCall(
|
||||||
|
'userKVStorage',
|
||||||
|
'getItem',
|
||||||
|
{
|
||||||
|
key,
|
||||||
|
bizID,
|
||||||
|
});
|
||||||
|
if (res && res.statusCode === 0 && res.data && res.data.result) {
|
||||||
|
return res.data.result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof window !== 'undefined' && window.localStorage) {
|
||||||
return localStorage.getItem(key);
|
return localStorage.getItem(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@ice/app": "^3.2.8",
|
"@ice/app": "^3.2.8",
|
||||||
"webpack": "^5.86.0"
|
"webpack": "^5.86.0",
|
||||||
|
"@ice/runtime": "^1.2.5"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "http",
|
"type": "http",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue