@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
67 lines • 2.58 kB
JavaScript
/**
* 2024-11-04: Copied from PhotoFormWebpart
* Sample functions generated by ChatGPT for handling various types of file.
* 2024-11-03: Currently not used or tested.
*
*/
// Handle image files
const handleImage = (file, onImageData) => {
const reader = new FileReader();
reader.onloadend = function () {
onImageData(reader.result);
};
reader.readAsDataURL(file);
};
// Handle Excel files
const handleExcel = (file, onExcelData) => {
const reader = new FileReader();
reader.onloadend = function () {
onExcelData(reader.result);
};
reader.readAsArrayBuffer(file);
};
// Handle text files
const handleText = (file, onTextData) => {
const reader = new FileReader();
reader.onloadend = function () {
onTextData(reader.result);
};
reader.readAsText(file);
};
// Handle PowerPoint files
const handlePowerPoint = (file, onPowerPointData) => {
const reader = new FileReader();
reader.onloadend = function () {
onPowerPointData(reader.result);
};
reader.readAsArrayBuffer(file);
};
// Main clipboard handler
export const handlePaste = (e, options) => {
var _a;
const clipboardItems = (_a = e.clipboardData) === null || _a === void 0 ? void 0 : _a.items;
if (clipboardItems)
for (let i = 0; i < (clipboardItems === null || clipboardItems === void 0 ? void 0 : clipboardItems.length); i++) {
const item = clipboardItems[i];
const file = item.getAsFile();
if (!file)
continue; // Skip if no file is found
// Combine existence check and type check
if (options.onImageData && item.type.indexOf('image') !== -1) {
handleImage(file, options.onImageData);
}
else if (options.onExcelData && item.type.indexOf('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') !== -1) {
handleExcel(file, options.onExcelData);
}
else if (options.onTextData && item.type.indexOf('text/plain') !== -1) {
handleText(file, options.onTextData);
}
else if (options.onPowerPointData && item.type.indexOf('application/vnd.openxmlformats-officedocument.presentationml.presentation') !== -1) {
handlePowerPoint(file, options.onPowerPointData);
}
else {
console.warn('Unsupported file type');
}
}
};
//# sourceMappingURL=handlePasteFiles.js.map