Skip to content

Commit

Permalink
handle opening .bloompub
Browse files Browse the repository at this point in the history
  • Loading branch information
hatton committed Jun 25, 2020
1 parent 9752d1f commit c30af7b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"archiver": "^3.1.1",
"bloom-player": "^1.1.49",
"fs": "^0.0.1-security",
"path": "^0.12.7",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"temp": "^0.9.1",
Expand Down
21 changes: 11 additions & 10 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import { hot } from "react-hot-loader/root";
import * as fs from "fs";
import * as unzipper from "unzipper";
import * as temp from "temp";
//import bloomPlayerJS from "./bloomPlayer.min.js";
//import bloomPlayerHtml from "../../node_modules/bloom-player/dist/bloomplayer.htm";

const bloomPlayerHtml = "bloomplayer.htm";
//const bloomdPath = "D:\\temp\\The Moon and the Cap.bloomd";
const App: React.FunctionComponent<{ bloomdPath: string }> = props => {
const App: React.FunctionComponent<{ zipFilePath: string }> = (props) => {
const [htmPath, setHtmPath] = useState("");

useEffect(() => {
console.log("bloom htmlpath=" + bloomPlayerHtml);
const slashIndex = props.bloomdPath
const slashIndex = props.zipFilePath
.replace(/\\/g, "/")

.lastIndexOf("/");
let bookTitle: string;
bookTitle = props.bloomdPath.substring(
bookTitle = props.zipFilePath.substring(
slashIndex + 1,
props.bloomdPath.length
props.zipFilePath.length
);
const filename = bookTitle.replace(".bloomd", ".htm");
const filename = bookTitle
.replace(/\.bloomd/gi, ".htm")
.replace(/\.bloompub/gi, ".htm");
temp.track();
temp.mkdir("bloom-reader-", (err, p) => {
fs.createReadStream(props.bloomdPath).pipe(unzipper.Extract({ path: p }));
fs.createReadStream(props.zipFilePath).pipe(
unzipper.Extract({ path: p })
);
console.log("booktitle = " + bookTitle);
console.log("filename = " + filename);
console.log("temp path = " + p);
Expand All @@ -38,7 +39,7 @@ const App: React.FunctionComponent<{ bloomdPath: string }> = props => {
1000
);
});
}, [props.bloomdPath]);
}, [props.zipFilePath]);

console.log("htmPath = " + htmPath);
return (
Expand Down
83 changes: 43 additions & 40 deletions src/renderer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
const electron = require("electron");
import { ipcRenderer, remote, app } from "electron";
import * as fs from "fs";
import App from "./App";
import { ipcRenderer, remote } from "electron";

updateMainMenu();
const path = ipcRenderer.sendSync("get-file-that-launched-me");
remote.getCurrentWindow().setTitle("Bloom Viewer");

const title = "BloomPub Viewer " + require("../../package.json").version;

remote.getCurrentWindow().setTitle(title);

//show initial book or notice
showBook(path);
if (path && fs.existsSync(path)) {
showBook(path);
} else {
showOpenFile();
}

function showBook(bloomdPath: string) {
console.log(`path ='${bloomdPath}'`);
if (path) {
ReactDOM.render(
<App bloomdPath={bloomdPath} />,
document.getElementById("root")
);
} else {
ReactDOM.render(
<h1>
Our apologies... please quit this app, then double click on a .bloomd
file to run open it.
</h1>,
document.getElementById("root")
);
}
function showBook(zipFilePath: string) {
electron.remote.app.addRecentDocument(zipFilePath);
ReactDOM.render(
<App zipFilePath={zipFilePath} />,
document.getElementById("root")
);
}

function updateMainMenu() {
const mainWindow = remote.getCurrentWindow();
const macMenu = {
label: `Bloom Viewer`,
label: `BloomPub Viewer`,
submenu: [
{
label: `Quit`,
accelerator: "Command+Q",
click() {
remote.app.quit();
}
}
]
},
},
],
};

const fileMenu = {
Expand All @@ -51,23 +50,10 @@ function updateMainMenu() {
label: "&" + `Open BloomPub...`,
accelerator: "Ctrl+O",
click: () => {
const options: Electron.OpenDialogOptions = {
properties: ["openFile"],
filters: [
{
name: "Bloom Digital Book",
extensions: ["bloomd", "bloompub"]
}
]
};
remote.dialog.showOpenDialog(options).then(result => {
if (!result.canceled && result.filePaths.length > 0) {
showBook(result.filePaths[0]);
}
});
}
}
]
showOpenFile();
},
},
],
};
if (fileMenu && process.platform !== "darwin") {
//fileMenu.submenu.push({ type: "separator" });
Expand All @@ -87,3 +73,20 @@ function updateMainMenu() {

remote.Menu.setApplicationMenu(menu);
}
function showOpenFile() {
const options: Electron.OpenDialogOptions = {
title: "Open BloomPub File",
properties: ["openFile"],
filters: [
{
name: "BloomPub Book",
extensions: ["bloomd", "bloompub"],
},
],
};
remote.dialog.showOpenDialog(options).then((result) => {
if (!result.canceled && result.filePaths.length > 0) {
showBook(result.filePaths[0]);
}
});
}

0 comments on commit c30af7b

Please sign in to comment.