diff --git a/examples/electron-nodeintegration/src/index.html b/examples/electron-nodeintegration/src/index.html index 5cf008cc..42d87034 100644 --- a/examples/electron-nodeintegration/src/index.html +++ b/examples/electron-nodeintegration/src/index.html @@ -2,6 +2,17 @@
+ + - + diff --git a/examples/electron-nodeintegration/src/lib/drag-and-drop.js b/examples/electron-nodeintegration/src/lib/drag-and-drop.js index 49062a30..c968a54b 100644 --- a/examples/electron-nodeintegration/src/lib/drag-and-drop.js +++ b/examples/electron-nodeintegration/src/lib/drag-and-drop.js @@ -1,3 +1,6 @@ +const { webUtils } = require("electron"); +const { pathToFileURL } = require("node:url"); + /** * Initializes drag and drop handlers for a given `NutrientViewer.Instance`. When a * file is detected, the `onDrop` handler will be called. @@ -9,13 +12,15 @@ module.exports = function dragAndDrop(instance, onDrop) { }); instance.contentWindow.addEventListener("drop", (event) => { if (event?.dataTransfer?.files?.length) { - const file = event.dataTransfer.files[0].path; + const droppedFile = event.dataTransfer.files[0]; - if (event.dataTransfer.files[0].type !== "application/pdf") { + if (droppedFile.type !== "application/pdf") { return; } - onDrop(file); + // Electron removed `File.path` in v32; `webUtils` replaces it. The viewer + // opens the file from a `file://` URL. + onDrop(pathToFileURL(webUtils.getPathForFile(droppedFile)).href); } event.preventDefault(); diff --git a/examples/electron-nodeintegration/src/renderer.js b/examples/electron-nodeintegration/src/renderer.js index 229e3732..a46d809d 100644 --- a/examples/electron-nodeintegration/src/renderer.js +++ b/examples/electron-nodeintegration/src/renderer.js @@ -2,6 +2,8 @@ // be executed in the renderer process for that window. // All of the Node.js APIs are available in this process. +const path = require("node:path"); +const { pathToFileURL } = require("node:url"); const NutrientViewer = require("@nutrient-sdk/viewer"); const { @@ -12,6 +14,13 @@ const { const dragAndDrop = require("./lib/drag-and-drop"); const makeToolbarItems = require("./lib/toolbar"); +/** + * We append `process.platform` as a CSS class to `` so we can offset + * the frame less window alternative on macOS. + * https://electronjs.org/docs/api/frameless-window#alternatives-on-macos + */ +document.body.classList.add(`platform-${process.platform}`); + /** * We store the `NutrientViewer.Instance` in this variable so we can access it from * everywhere. @@ -71,6 +80,12 @@ async function load(document) { const configuration = { document, container: "#root", + // Load the viewer's assets from the installed npm package. The folder must + // be the one that contains `nutrient-viewer-lib/`, given as an absolute URL. + baseUrl: pathToFileURL( + `${path.dirname(require.resolve("@nutrient-sdk/viewer"))}/`, + ).href, + useCDN: false, styleSheets: ["./nutrient.css"], }; diff --git a/examples/electron/src/index.html b/examples/electron/src/index.html index 30e2eb99..c9706652 100644 --- a/examples/electron/src/index.html +++ b/examples/electron/src/index.html @@ -2,6 +2,17 @@ + + - + diff --git a/examples/electron/src/lib/drag-and-drop.js b/examples/electron/src/lib/drag-and-drop.js index c3134cc6..047eda19 100644 --- a/examples/electron/src/lib/drag-and-drop.js +++ b/examples/electron/src/lib/drag-and-drop.js @@ -9,13 +9,13 @@ export function dragAndDrop(instance, onDrop) { }); instance.contentWindow.addEventListener("drop", (event) => { if (event?.dataTransfer?.files?.length) { - const file = event.dataTransfer.files[0].path; + const droppedFile = event.dataTransfer.files[0]; - if (event.dataTransfer.files[0].type !== "application/pdf") { + if (droppedFile.type !== "application/pdf") { return; } - onDrop(file); + onDrop(window.electron.getFileUrl(droppedFile)); } event.preventDefault(); diff --git a/examples/electron/src/preload.js b/examples/electron/src/preload.js index f593dca7..00223be8 100644 --- a/examples/electron/src/preload.js +++ b/examples/electron/src/preload.js @@ -1,4 +1,5 @@ -const { contextBridge } = require("electron"); +const { contextBridge, webUtils } = require("electron"); +const { pathToFileURL } = require("node:url"); const { documentExport, @@ -15,4 +16,7 @@ contextBridge.exposeInMainWorld("electron", { documentExport, documentImport, askUserToDiscardChanges, + // Returns a `file://` URL for a dropped `File`, which the viewer can open. + // Electron removed `File.path` in v32; `webUtils` replaces it. + getFileUrl: (file) => pathToFileURL(webUtils.getPathForFile(file)).href, }); diff --git a/examples/electron/src/renderer.js b/examples/electron/src/renderer.js new file mode 100644 index 00000000..522f2717 --- /dev/null +++ b/examples/electron/src/renderer.js @@ -0,0 +1,104 @@ +import { dragAndDrop } from "./lib/drag-and-drop.js"; +import { makeToolbarItems } from "./lib/toolbar.js"; + +/** + * We append `process.platform` ass a CSS class to `` so we can offset + * the frame less window alternative on macOS. + * https://electronjs.org/docs/api/frameless-window#alternatives-on-macos + */ +document.body.classList.add(`platform-${window.electron.processPlatform()}`); + +/** + * We store the `NutrientViewer.Instance` in this variable so we can access it from + * everywhere. + */ +let instance = null; + +const { documentExport, documentImport, askUserToDiscardChanges } = + window.electron; + +/** + * Creates an onAnnotationsChange handler that keeps track of changes. + * + * We skip the first call since `annotations.change` fires when the PDF is + * initialized and populated with annotations. + */ +let hasUnsavedAnnotations = false; + +function createOnAnnotationsChange() { + let initialized = false; + + return () => { + if (initialized) { + hasUnsavedAnnotations = true; + } else { + initialized = true; + } + }; +} + +/** + * If there is an existing running instance of Nutrient, it is destroyed before + * a creating a new one. + * + * This process will make sure that the WebAssembly module is optimally reused. + */ +async function load(document) { + if (instance) { + NutrientViewer.unload(instance); + hasUnsavedAnnotations = false; + instance = null; + } + + // Create our custom toolbar + const toolbarItems = makeToolbarItems( + NutrientViewer.defaultToolbarItems, + function exportFile() { + documentExport(instance, () => { + hasUnsavedAnnotations = false; + }); + }, + function importFile() { + if (hasUnsavedAnnotations) { + askUserToDiscardChanges(() => documentImport(load)); + } else { + documentImport(load); + } + }, + ); + + // Set up the configuration object. A custom style sheet is used to customize + // the look and feel of Nutrient. + const configuration = { + document, + container: "#root", + // Load the viewer's assets from the installed npm package. The folder must + // be the one that contains `nutrient-viewer-lib/`, given as an absolute URL. + baseUrl: new URL( + "../node_modules/@nutrient-sdk/viewer/dist/", + window.location.href, + ).href, + useCDN: false, + styleSheet: ["./nutrient.css"], + // appName must match the license's bundle ID + appName: "nutrient-electron-example", + // Add when using a license key + // licenseKey: "LICENSE KEY GOES HERE", + }; + + instance = await NutrientViewer.load(configuration); + + instance.setToolbarItems(toolbarItems); + instance.addEventListener("annotations.change", createOnAnnotationsChange()); + + dragAndDrop(instance, (file) => { + if (hasUnsavedAnnotations) { + askUserToDiscardChanges(() => load(file)); + } else { + load(file); + } + }); +} + +// Open a default document, when the app is started. +window.onload = () => load("./assets/example.pdf");