fix: impoer external components
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
import ePub from 'epubjs';
|
||||
|
||||
export default function readEpubContent(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e: any) {
|
||||
const arrayBuffer = e.target.result;
|
||||
const book = ePub(arrayBuffer);
|
||||
|
||||
book.loaded?.spine?.then?.((spine: any) => {
|
||||
const chapterPromises = spine.spineItems?.map?.((chapter: any) => {
|
||||
return book.load(chapter.href).then((content: any) => {
|
||||
return content.body?.textContent || '';
|
||||
});
|
||||
});
|
||||
Promise.all(chapterPromises)
|
||||
.then((chaptersText) => {
|
||||
const result = chaptersText.join('');
|
||||
resolve(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
import { saveAs } from 'file-saver';
|
||||
import XLSX from 'xlsx';
|
||||
|
||||
export default function readExcelContent(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e: any) {
|
||||
const arrayBuffer = e.target.result;
|
||||
const workbook = XLSX.read(arrayBuffer, { type: 'string' });
|
||||
const ws = workbook.Sheets[workbook.SheetNames[0]]; // get the first worksheet
|
||||
const data = XLSX.utils.sheet_to_json(ws);
|
||||
resolve(JSON.stringify(data));
|
||||
};
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param jsonData raw JSON data to export
|
||||
* @param fields export fields (keys in the JSON objects)
|
||||
* @param fieldLabels custom the table header labels
|
||||
* @param formatMap custom the cell format functions
|
||||
* @param fileName file name for the exported Excel file
|
||||
*/
|
||||
|
||||
interface FormatMap {
|
||||
[key: string]: (value: any, row?: any) => any;
|
||||
}
|
||||
|
||||
interface ExportSheetConfig {
|
||||
sheetName: string;
|
||||
jsonData: any[];
|
||||
fields: string[];
|
||||
fieldLabels?: Record<string, string>;
|
||||
formatMap?: FormatMap;
|
||||
}
|
||||
|
||||
interface ExportExcelOptions {
|
||||
sheets: ExportSheetConfig[];
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
export function exportJsonToExcel({
|
||||
sheets,
|
||||
fileName = 'data.xlsx'
|
||||
}: ExportExcelOptions) {
|
||||
const workbook = XLSX.utils.book_new();
|
||||
|
||||
sheets.forEach((sheet) => {
|
||||
const { sheetName, jsonData, fields, fieldLabels, formatMap } = sheet;
|
||||
|
||||
// 1. format data
|
||||
const formattedData = jsonData.map((row) => {
|
||||
const result: Record<string, any> = {};
|
||||
|
||||
for (const field of fields) {
|
||||
const rawValue = row[field];
|
||||
const formatFn = formatMap?.[field];
|
||||
result[field] = formatFn ? formatFn(rawValue, row) : rawValue;
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
// 2. convert to worksheet
|
||||
const worksheet = XLSX.utils.json_to_sheet(formattedData, {
|
||||
header: fields
|
||||
});
|
||||
|
||||
// 3. customize headers
|
||||
if (fieldLabels) {
|
||||
const headerRow = fields.map((key) => fieldLabels[key] || key);
|
||||
XLSX.utils.sheet_add_aoa(worksheet, [headerRow], { origin: 'A1' });
|
||||
}
|
||||
|
||||
// 4. add to workbook
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
|
||||
});
|
||||
|
||||
// 5. export file
|
||||
const excelBuffer = XLSX.write(workbook, {
|
||||
bookType: 'xlsx',
|
||||
type: 'array'
|
||||
});
|
||||
|
||||
const blob = new Blob([excelBuffer], {
|
||||
type: 'application/octet-stream'
|
||||
});
|
||||
|
||||
saveAs(blob, fileName);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import _ from 'lodash';
|
||||
import tinycolor from 'tinycolor2';
|
||||
|
||||
export const isNotEmptyValue = (value: any) => {
|
||||
if (Array.isArray(value)) {
|
||||
@@ -245,25 +244,6 @@ export const isOnline = () => {
|
||||
return window.navigator.onLine;
|
||||
};
|
||||
|
||||
export const genColors = ({
|
||||
color,
|
||||
alpha1,
|
||||
alpha2
|
||||
}: {
|
||||
color: string;
|
||||
alpha1?: number;
|
||||
alpha2?: number;
|
||||
}) => {
|
||||
const base = tinycolor(color);
|
||||
const alpha_start = alpha1 || base.getAlpha();
|
||||
const alpha_end = alpha2 || base.getAlpha();
|
||||
|
||||
return [
|
||||
base.setAlpha(alpha_start).toRgbString(),
|
||||
base.setAlpha(alpha_end).toRgbString()
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse a command-line parameter string into an array format.
|
||||
* @param paramsString - The parameter string to parse (supports multiline).
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import * as pdfjsLib from 'pdfjs-dist';
|
||||
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
|
||||
'pdfjs-dist/build/pdf.worker.mjs',
|
||||
// @ts-ignore
|
||||
import.meta.url
|
||||
).href;
|
||||
|
||||
const readPDFContent = (file: File): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = async function (e: any) {
|
||||
try {
|
||||
const arrayBuffer = e.target.result;
|
||||
const loadingTask = pdfjsLib.getDocument({ data: arrayBuffer });
|
||||
|
||||
const pdf = await loadingTask.promise;
|
||||
const numPages = pdf.numPages;
|
||||
|
||||
const pagePromises = [];
|
||||
|
||||
for (let i = 1; i <= numPages; i++) {
|
||||
const pagePromise = pdf.getPage(i).then(function (page) {
|
||||
return page.getTextContent().then(function (textContent) {
|
||||
return textContent.items
|
||||
.map(function (item: any) {
|
||||
return item.str;
|
||||
})
|
||||
.join(' ');
|
||||
});
|
||||
});
|
||||
pagePromises.push(pagePromise);
|
||||
}
|
||||
|
||||
const pageTexts = await Promise.all(pagePromises);
|
||||
const result = pageTexts?.join(' ');
|
||||
resolve(result);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
};
|
||||
|
||||
export default readPDFContent;
|
||||
@@ -1,40 +0,0 @@
|
||||
import JSZip from 'jszip';
|
||||
|
||||
const readPptxContent = (file: File): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e: any) {
|
||||
const arrayBuffer = e.target.result;
|
||||
JSZip.loadAsync(arrayBuffer).then(function (zip: any) {
|
||||
const slideFiles = Object.keys(zip.files).filter(function (fileName) {
|
||||
return (
|
||||
fileName.startsWith('ppt/slides/slide') && fileName.endsWith('.xml')
|
||||
);
|
||||
});
|
||||
|
||||
let slideText = '';
|
||||
const slidePromises = slideFiles.map((slideFile) =>
|
||||
zip
|
||||
.file(slideFile)
|
||||
.async('string')
|
||||
.then(function (content: any) {
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(content, 'application/xml');
|
||||
const texts = xmlDoc.getElementsByTagName('a:t');
|
||||
for (let i = 0; i < texts.length; i++) {
|
||||
slideText += texts[i].textContent + '\n';
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
Promise.all(slidePromises).then(() => {
|
||||
resolve(slideText);
|
||||
});
|
||||
});
|
||||
};
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
};
|
||||
|
||||
export default readPptxContent;
|
||||
@@ -1,18 +0,0 @@
|
||||
import mammoth from 'mammoth';
|
||||
|
||||
export default function readWordContent(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e: any) {
|
||||
const arrayBuffer = e.target.result;
|
||||
mammoth
|
||||
.extractRawText({ arrayBuffer })
|
||||
.then((result) => {
|
||||
resolve(result.value);
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
};
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user