Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions examples/electron-nodeintegration/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
<html>
<head>
<meta charset="UTF-8" />
<!--
Content Security Policy for the viewer. It allows the app's own files,
the WebAssembly engine ('wasm-unsafe-eval'), the workers, images and
fonts the viewer creates at runtime (blob: and data: URLs), and the
viewer's usage metrics endpoint.
https://www.electronjs.org/docs/latest/tutorial/security#7-define-a-content-security-policy
-->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'wasm-unsafe-eval' blob:; worker-src 'self' blob:; style-src 'self' 'unsafe-inline' blob:; img-src 'self' data: blob:; font-src 'self' data: blob:; connect-src 'self' data: blob: https://dam.our.services.nutrient-powered.io"
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
Expand Down Expand Up @@ -47,18 +58,6 @@
<header></header>
<div id="root"></div>

<script>
/**
* We append `process.platform` ass a CSS class to `<body>` 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}`);

/**
* Invoke the JavaScript for the renderer process.
*/
require("./renderer.js");
</script>
<script src="./renderer.js"></script>
</body>
</html>
11 changes: 8 additions & 3 deletions examples/electron-nodeintegration/src/lib/drag-and-drop.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions examples/electron-nodeintegration/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 `<body>` 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.
Expand Down Expand Up @@ -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"],
};

Expand Down
114 changes: 12 additions & 102 deletions examples/electron/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
<html>
<head>
<meta charset="UTF-8" />
<!--
Content Security Policy for the viewer. It allows the app's own files,
the WebAssembly engine ('wasm-unsafe-eval'), the workers, images and
fonts the viewer creates at runtime (blob: and data: URLs), and the
viewer's usage metrics endpoint.
https://www.electronjs.org/docs/latest/tutorial/security#7-define-a-content-security-policy
-->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'wasm-unsafe-eval' blob:; worker-src 'self' blob:; style-src 'self' 'unsafe-inline' blob:; img-src 'self' data: blob:; font-src 'self' data: blob:; connect-src 'self' data: blob: https://dam.our.services.nutrient-powered.io"
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
Expand Down Expand Up @@ -49,107 +60,6 @@

<script src="../node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer.js"></script>

<script type="module">
import { dragAndDrop } from "./lib/drag-and-drop.js";
import { makeToolbarItems } from "./lib/toolbar.js";

/**
* We append `process.platform` ass a CSS class to `<body>` 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",
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");
</script>
<script type="module" src="./renderer.js"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions examples/electron/src/lib/drag-and-drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion examples/electron/src/preload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { contextBridge } = require("electron");
const { contextBridge, webUtils } = require("electron");
const { pathToFileURL } = require("node:url");

const {
documentExport,
Expand All @@ -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,
});
104 changes: 104 additions & 0 deletions examples/electron/src/renderer.js
Original file line number Diff line number Diff line change
@@ -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 `<body>` 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");
Loading