-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
32 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters