import './polyfills/index'; import React from 'react'; import {render as renderReact} from 'react-dom'; import axios from 'axios'; import copy from 'copy-to-clipboard'; import {normalizeLink} from '../src/utils/normalizeLink'; import qs from 'qs'; import { toast, alert, confirm, ToastComponent, AlertComponent, render as renderAmis } from '../src/index'; import '../src/locale/en'; export function embed( container: string | HTMLElement, schema: any, data: any, env: any ) { if (typeof container === 'string') { container = document.querySelector(container) as HTMLElement; } if (!container) { console.error('选择器不对,页面上没有此元素'); return; } else if (container.tagName === 'BODY') { let div = document.createElement('div'); container.appendChild(div); container = div; } container.classList.add('amis-scope'); let scoped: any; const attachmentAdpator = (response: any) => { if ( response && response.headers && response.headers['content-disposition'] ) { const disposition = response.headers['content-disposition']; let filename = ''; if (disposition && disposition.indexOf('attachment') !== -1) { let filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i; let matches = filenameRegex.exec(disposition); if (matches != null && matches[1]) { filename = matches[1].replace(/['"]/g, ''); } // 很可能是中文被 url-encode 了 if (filename && filename.replace(/[^%]/g, '').length > 2) { filename = decodeURIComponent(filename); } let type = response.headers['content-type']; let blob = response.data.toString() === '[object Blob]' ? response.data : new Blob([response.data], {type: type}); if (typeof window.navigator.msSaveBlob !== 'undefined') { // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed." window.navigator.msSaveBlob(blob, filename); } else { let URL = window.URL || (window as any).webkitURL; let downloadUrl = URL.createObjectURL(blob); if (filename) { // use HTML5 a[download] attribute to specify filename let a = document.createElement('a'); // safari doesn't support this yet if (typeof a.download === 'undefined') { (window as any).location = downloadUrl; } else { a.href = downloadUrl; a.download = filename; document.body.appendChild(a); a.click(); } } else { (window as any).location = downloadUrl; } setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup } return { ...response, data: { status: 0, msg: '文件即将开始下载。。' } }; } } else if (response.data.toString() === '[object Blob]') { return new Promise((resolve, reject) => { let reader = new FileReader(); reader.addEventListener('loadend', e => { const text = reader.result as string; try { resolve({ ...response, data: { ...JSON.parse(text) } }); } catch (e) { reject(e); } }); reader.readAsText(response.data); }); } return response; }; const responseAdpater = (api: any) => (value: any) => { let response = value.data; if (env && env.responseAdpater) { const url = api.url; const idx = api.url.indexOf('?'); const query = ~idx ? qs.parse(api.url.substring(idx)) : {}; const request = { ...api, query: query, body: api.data }; response = env.responseAdpater(api, response, query, request); } else { if (response.hasOwnProperty('errno')) { response.status = response.errno; response.msg = response.errmsg; } else if (response.hasOwnProperty('no')) { response.status = response.no; response.msg = response.error; } } const result = { ...value, data: response }; return result; }; renderReact(