Skip to content

Commit

Permalink
✨ feat: 优化抓取 base64 图片方法
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 23, 2021
1 parent 3b8d191 commit 07dd128
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
12 changes: 7 additions & 5 deletions src/function/setting.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
type FetchBase64 = (url: string) => Promise<string>;

interface Config {
fetch?: typeof fetch;
fetchBase64?: FetchBase64;
}

declare global {
interface Window {
HTML2SKETCH_FETCH: typeof fetch;
HTML2SKETCH_FETCH_BASE64: (url: string) => Promise<string>;
}
}
/**
* 进行全局配置
*/
export const setConfig = (config: Config) => {
const { fetch } = config;
if (typeof fetch === 'function') {
window.HTML2SKETCH_FETCH = fetch;
const { fetchBase64 } = config;
if (typeof fetchBase64 === 'function') {
window.HTML2SKETCH_FETCH_BASE64 = fetchBase64;
}
};
17 changes: 6 additions & 11 deletions src/models/Layer/Bitmap.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import BaseLayer from '../Base/BaseLayer';
import { defaultExportOptions } from '../utils';
import { uuid } from '../../utils/utils';
import fetch from '../../utils/fetch';
import { fetchBase64 } from '../../utils/fetch';

import {
blobToBase64,
initImageURL,
getBase64ImageString,
} from '../../utils/image';
import { BaseLayerParams, SketchFormat } from '../../types';
import { initImageURL } from '../../utils/image';
import type { BaseLayerParams } from '../../types';
import { SketchFormat } from '../../types';

interface BitmapInitParams extends BaseLayerParams {
url: string;
Expand Down Expand Up @@ -46,15 +43,13 @@ class Bitmap extends BaseLayer {
if (!this.url.startsWith('http')) return;

try {
const data = await fetch(this.url);
const blob = await data.blob();
const dataURL = await blobToBase64(blob);
const base64 = getBase64ImageString(dataURL);
const base64 = await fetchBase64(this.url);
if (base64) {
this.base64 = base64;
}
} catch (e) {
console.warn('网络或图片资源可能存在问题...');
console.error(e);
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/models/Style/Image.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import SketchFormat from '@sketch-hq/sketch-file-format-ts';
import BaseStyle from '../Base/BaseStyle';
import {
blobToBase64,
initImageURL,
getBase64ImageString,
} from '../../utils/image';
import fetch from '../../utils/fetch';
import { initImageURL } from '../../utils/image';
import { fetchBase64 } from '../../utils/fetch';

/**
* 图片资产
Expand All @@ -24,10 +20,8 @@ class Image extends BaseStyle {
*/
async init() {
if (this.url.startsWith('http')) {
const data = await fetch(this.url);
const blob = await data.blob();
const dataURL = await blobToBase64(blob);
const base64 = getBase64ImageString(dataURL);
const base64 = await fetchBase64(this.url);

if (base64) {
this.base64 = base64;
}
Expand Down
20 changes: 15 additions & 5 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const innerFetch: typeof fetch = (input, init) => {
const _fetch = window.HTML2SKETCH_FETCH ?? fetch;
return _fetch(input, init);
};
import { blobToBase64, getBase64ImageString } from './image';

/**
*
* @param url
*/
export const fetchBase64 = async (url: string) => {
if (window.HTML2SKETCH_FETCH_BASE64) {
return await window.HTML2SKETCH_FETCH_BASE64(url);
}

export default innerFetch;
const data = await fetch(url);
const blob = await data.blob();
const dataURL = await blobToBase64(blob);
return getBase64ImageString(dataURL);
};
1 change: 0 additions & 1 deletion src/utils/svg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-ignore
import getSvgoInstance from 'svgo-browser/lib/get-svgo-instance';
import fetch from './fetch';

/**
* svg 基本样式
Expand Down

0 comments on commit 07dd128

Please sign in to comment.