mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Drag&Drop: Dynamically load sheets module (#69536)
dynamically load sheets module
This commit is contained in:
parent
24502c4c4a
commit
e0ce6c3a94
@ -2,7 +2,6 @@ import { Accept } from 'react-dropzone';
|
|||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
import { toDataFrame } from '@grafana/data';
|
import { toDataFrame } from '@grafana/data';
|
||||||
import { readSpreadsheet } from 'app/core/utils/sheet';
|
|
||||||
|
|
||||||
import { FileImportResult } from './types';
|
import { FileImportResult } from './types';
|
||||||
|
|
||||||
@ -27,24 +26,30 @@ export function formatFileTypes(acceptedFiles: Accept) {
|
|||||||
export function filesToDataframes(files: File[]): Observable<FileImportResult> {
|
export function filesToDataframes(files: File[]): Observable<FileImportResult> {
|
||||||
return new Observable<FileImportResult>((subscriber) => {
|
return new Observable<FileImportResult>((subscriber) => {
|
||||||
let completedFiles = 0;
|
let completedFiles = 0;
|
||||||
files.forEach((file) => {
|
import('app/core/utils/sheet')
|
||||||
const reader = new FileReader();
|
.then((sheet) => {
|
||||||
reader.readAsArrayBuffer(file);
|
files.forEach((file) => {
|
||||||
reader.onload = () => {
|
const reader = new FileReader();
|
||||||
const result = reader.result;
|
reader.readAsArrayBuffer(file);
|
||||||
if (result && result instanceof ArrayBuffer) {
|
reader.onload = () => {
|
||||||
if (file.type === 'application/json') {
|
const result = reader.result;
|
||||||
const decoder = new TextDecoder('utf-8');
|
if (result && result instanceof ArrayBuffer) {
|
||||||
const json = JSON.parse(decoder.decode(result));
|
if (file.type === 'application/json') {
|
||||||
subscriber.next({ dataFrames: [toDataFrame(json)], file: file });
|
const decoder = new TextDecoder('utf-8');
|
||||||
} else {
|
const json = JSON.parse(decoder.decode(result));
|
||||||
subscriber.next({ dataFrames: readSpreadsheet(result), file: file });
|
subscriber.next({ dataFrames: [toDataFrame(json)], file: file });
|
||||||
}
|
} else {
|
||||||
if (++completedFiles >= files.length) {
|
subscriber.next({ dataFrames: sheet.readSpreadsheet(result), file: file });
|
||||||
subscriber.complete();
|
}
|
||||||
}
|
if (++completedFiles >= files.length) {
|
||||||
}
|
subscriber.complete();
|
||||||
};
|
}
|
||||||
});
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
throw 'Failed to load sheets module';
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user