Skip to content

Commit

Permalink
Merge remote-tracking branch 'amis/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCole21 committed Nov 26, 2020
2 parents 8b94777 + 01a61f1 commit 29daf95
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
88 changes: 87 additions & 1 deletion examples/embed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,90 @@ export function embed(
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]*)/;
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) {
Expand Down Expand Up @@ -173,7 +257,9 @@ export function embed(
}

data && (config.data = data);
return axios(url, config).then(responseAdpater(api));
return axios(url, config)
.then(attachmentAdpator)
.then(responseAdpater(api));
},
isCancel: (value: any) => (axios as any).isCancel(value),
copy: (contents: string, options: any = {}) => {
Expand Down
1 change: 1 addition & 0 deletions examples/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mapping: {
'react-dropzone': __moduleId('react-dropzone'),
'classnames': __moduleId('classnames'),
'axios': __moduleId('axios'),
'exceljs': __moduleId('exceljs'),
'moment': __moduleId('moment'),
'mobx': __moduleId('mobx'),
'mobx-state-tree': __moduleId('mobx-state-tree'),
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/Form/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class ButtonGroupControl extends React.Component<
}
);
});
} else if (buttons) {
} else if (Array.isArray(buttons)) {
body = buttons.map((button, key) =>
render(
`button/${key}`,
Expand Down
5 changes: 4 additions & 1 deletion src/renderers/Form/City.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export class CityPicker extends React.Component<
onChange={this.handleStreetChange}
onBlur={this.handleStreetEnd}
placeholder={__('请输入街道信息')}
disabled={disabled}
/>
) : null}
</div>
Expand Down Expand Up @@ -405,7 +406,8 @@ export class LocationControl extends React.Component<LocationControlProps> {
allowDistrict,
extractValue,
joinValues,
allowStreet
allowStreet,
disabled
} = this.props;
return (
<ThemedCity
Expand All @@ -416,6 +418,7 @@ export class LocationControl extends React.Component<LocationControlProps> {
extractValue={extractValue}
joinValues={joinValues}
allowStreet={allowStreet}
disabled={disabled}
/>
);
}
Expand Down
16 changes: 12 additions & 4 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,18 @@ export function wrapAdaptor(promise: Promise<fetcherResult>, api: ApiObject) {
const adaptor = api.adaptor;
return adaptor
? promise
.then(response => ({
...response,
data: adaptor((response as any).data, response, api)
}))
.then(async response => {
let result = adaptor((response as any).data, response, api);

if (result?.then) {
result = await result;
}

return {
...response,
data: result
};
})
.then(responseAdaptor)
: promise.then(responseAdaptor);
}
Expand Down

0 comments on commit 29daf95

Please sign in to comment.