load-script-icon.js 691 B

123456789101112131415161718192021222324
  1. import { sendCmdDirectly } from '@/common/index';
  2. const KEY = 'safeIcon';
  3. /**
  4. * Sets script's safeIcon property after the image is successfully loaded
  5. * @param {VMScript} script
  6. * @param {Object} [cache]
  7. */
  8. export async function loadScriptIcon(script, cache = {}) {
  9. const { icon } = script.meta;
  10. const url = script.custom?.pathMap?.[icon] || icon;
  11. if (!url || url !== script[KEY]) {
  12. // creates an observable property so Vue will see the change after `await`
  13. script[KEY] = null;
  14. if (url) {
  15. script[KEY] = cache[url]
  16. || url.startsWith('data:') && url
  17. || await sendCmdDirectly('GetImageData', url)
  18. || null;
  19. }
  20. }
  21. return script[KEY];
  22. }