Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add storage for canvas #6399

Merged
merged 9 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-guests-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/cache-canvas': patch
---

feat: add storage for canvas
28 changes: 26 additions & 2 deletions packages/cache-canvas/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ declare global {
call: Function;
};
_windvane_backControl: Function | null;
__megability_bridge__: {
syncCall: Function;
};
}
}

Expand Down Expand Up @@ -141,8 +144,29 @@ export const CacheCanvas = forwardRef((props: CacheCanvasProps, ref) => {
}
<script
dangerouslySetInnerHTML={{
__html: `
const base64Data = localStorage.getItem('${cacheKey}');
__html: `
let base64Data = '';
if (
window.__megability_bridge__ &&
window.__megability_bridge__.syncCall
) {
const canIUse = window.__megability_bridge__.syncCall('ability', 'available', {
ability: 'KVStorage',
api: 'getItem',
});

if (canIUse) {
const res = window.__megability_bridge__.syncCall('KVStorage', 'getItem', { key: '${cacheKey}' });
if (res && res.statusCode === 0 && res.data) {
base64Data = res.data;
}
}
}

if (!base64Data) {
localStorage.getItem('${cacheKey}');
answershuto marked this conversation as resolved.
Show resolved Hide resolved
}

const fallback = document.getElementById('fallback-${id}');
if (base64Data) {
const img = document.getElementById('canvas-img-${id}');
Expand Down
43 changes: 41 additions & 2 deletions packages/cache-canvas/src/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@ const cache = {};
export const Storage = {
setItem: (key, value) => {
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: 'KVStorage',
api: 'setItem',
});

if (canIUse) {
const res = window.__megability_bridge__.syncCall('KVStorage', 'setItem', {
key,
value,
});
if (res && res.statusCode === 0) {
return;
}
}
}

if (typeof window !== 'undefined' && window.localStorage) {
return localStorage.setItem(key, value);
}

Expand All @@ -14,7 +35,25 @@ export const Storage = {
},
getItem: (key) => {
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: 'KVStorage',
api: 'getItem',
});

if (canIUse) {
const res = window.__megability_bridge__.syncCall('KVStorage', 'getItem', { key });
if (res && res.statusCode === 0 && res.data) {
return res.data;
}
}
}

if (typeof window !== 'undefined' && window.localStorage) {
return localStorage.getItem(key);
}

Expand Down
Loading