|
| 1 | +import { commands, FileType, ProgressLocation, Uri, window, workspace } from "vscode"; |
| 2 | +import { Action, Demos, Subscription } from "../models"; |
| 3 | +import { Extension } from "./Extension"; |
| 4 | +import { COMMAND, General } from "../constants"; |
| 5 | +import { Notifications } from "./Notifications"; |
| 6 | +import { createImageSlide, parseWinPath } from "../utils"; |
| 7 | + |
| 8 | +export class ImportService { |
| 9 | + public static register() { |
| 10 | + const subscriptions: Subscription[] = Extension.getInstance().subscriptions; |
| 11 | + |
| 12 | + subscriptions.push(commands.registerCommand(COMMAND.importPowerPointImages, ImportService.importPowerPointImages)); |
| 13 | + } |
| 14 | + |
| 15 | + private static async importPowerPointImages() { |
| 16 | + // Ask the user to select the PowerPoint folder |
| 17 | + const selectedFolder = await window.showOpenDialog({ |
| 18 | + canSelectFolders: true, |
| 19 | + canSelectMany: false, |
| 20 | + openLabel: "Select folder with PowerPoint exported slide images", |
| 21 | + }); |
| 22 | + |
| 23 | + if (!selectedFolder || selectedFolder.length === 0) { |
| 24 | + Notifications.error("No folder selected."); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + await window.withProgress( |
| 29 | + { |
| 30 | + location: ProgressLocation.Notification, |
| 31 | + title: "Importing images...", |
| 32 | + cancellable: false, |
| 33 | + }, |
| 34 | + async (_) => { |
| 35 | + const folderUri = selectedFolder[0]; |
| 36 | + const wsFolder = Extension.getInstance().workspaceFolder; |
| 37 | + |
| 38 | + if (!wsFolder) { |
| 39 | + window.showErrorMessage("No workspace folder found."); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const files = await workspace.fs.readDirectory(folderUri); |
| 44 | + const imageFiles = files.filter( |
| 45 | + ([name, type]) => type === FileType.File && /\.(jpg|jpeg|png|gif)$/i.test(name) |
| 46 | + ); |
| 47 | + if (imageFiles.length === 0) { |
| 48 | + Notifications.error("No image files found in the selected folder."); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const createNewDemoFile = await window.showInformationMessage( |
| 53 | + "Do you want to add the slides in a new demo file?", |
| 54 | + { modal: true, detail: "This will create a new demo file with the slides." }, |
| 55 | + "Yes" |
| 56 | + ); |
| 57 | + |
| 58 | + let imageUris = imageFiles.map(([name]) => folderUri.with({ path: `${folderUri.path}/${name}` })); |
| 59 | + imageUris = imageUris.sort((a, b) => { |
| 60 | + const nameA = a.path.split("/").pop() || ""; |
| 61 | + const nameB = b.path.split("/").pop() || ""; |
| 62 | + return nameA.localeCompare(nameB, undefined, { numeric: true, sensitivity: "base" }); |
| 63 | + }); |
| 64 | + |
| 65 | + const slideFolder = Uri.joinPath(wsFolder.uri, General.demoFolder, General.slidesFolder); |
| 66 | + const slideFolderName = parseWinPath(folderUri.path).split("/").pop() || ""; |
| 67 | + const newSlideFolder = Uri.joinPath(slideFolder, slideFolderName); |
| 68 | + |
| 69 | + try { |
| 70 | + await workspace.fs.createDirectory(newSlideFolder); |
| 71 | + } catch (error) { |
| 72 | + Notifications.error("Failed to create slide folder."); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + let demo: Demos | undefined; |
| 77 | + if (createNewDemoFile === "Yes") { |
| 78 | + demo = { |
| 79 | + title: slideFolderName, |
| 80 | + description: "Imported from PowerPoint", |
| 81 | + demos: [], |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + for (const [_, imageUri] of imageUris.entries()) { |
| 86 | + const imageName = imageUri.path.split("/").pop() || ""; |
| 87 | + const imageNameWithoutExtension = imageName.split(".").slice(0, -1).join("."); |
| 88 | + const newImageUri = Uri.joinPath(newSlideFolder, "images", imageName); |
| 89 | + |
| 90 | + try { |
| 91 | + await workspace.fs.copy(imageUri, newImageUri, { |
| 92 | + overwrite: true, |
| 93 | + }); |
| 94 | + } catch (error) { |
| 95 | + Notifications.error(`Failed to copy image: ${imageUri.path}`); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + const slidePath = await createImageSlide(newImageUri, newSlideFolder); |
| 101 | + |
| 102 | + if (slidePath && demo && demo.demos) { |
| 103 | + demo.demos.push({ |
| 104 | + title: imageNameWithoutExtension, |
| 105 | + description: "", |
| 106 | + icons: { |
| 107 | + start: "vm", |
| 108 | + end: "pass-filled", |
| 109 | + }, |
| 110 | + steps: [{ action: Action.OpenSlide, path: slidePath }], |
| 111 | + }); |
| 112 | + } |
| 113 | + } catch (error) { |
| 114 | + Notifications.error(`Failed to create slide for image: ${imageUri.path}`); |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (demo) { |
| 120 | + const demoFileUri = Uri.joinPath(wsFolder.uri, General.demoFolder, `${slideFolderName}.json`); |
| 121 | + try { |
| 122 | + await workspace.fs.writeFile(demoFileUri, Buffer.from(JSON.stringify(demo, null, 2))); |
| 123 | + await window.showTextDocument(demoFileUri, { preview: false }); |
| 124 | + } catch (error) { |
| 125 | + Notifications.error("Failed to create demo file."); |
| 126 | + return; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Notifications.info("Slides created successfully!"); |
| 131 | + } |
| 132 | + ); |
| 133 | + } |
| 134 | +} |
0 commit comments