Skip to content

Commit 1d26a54

Browse files
refactor: adds configuration for each examples directory for base url and assets path, makes loadNutrient util async
1 parent 3f70b52 commit 1d26a54

File tree

73 files changed

+702
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+702
-388
lines changed

frameworks/angular/TS/src/app/utils/loadNutrientViewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type NutrientViewer from "@nutrient-sdk/viewer";
22

3-
export function loadNutrientViewer(): typeof NutrientViewer {
3+
export async function loadNutrientViewer(): Promise<typeof NutrientViewer> {
44
if (!window.NutrientViewer) {
55
throw new Error("NutrientViewer not found on window object");
66
}

frameworks/angular/TS/src/examples/basic-viewer/implementation.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55
*/
66

77
import type NutrientViewer from "@nutrient-sdk/viewer";
8+
import { nutrientConfig } from "../nutrient-config";
89

910
/**
1011
* Load a basic PDF viewer
11-
* @param nutrientViewer - The NutrientViewer object (from CDN)
12+
* @param nutrientViewer - The NutrientViewer object (from CDN or package)
1213
* @param container - The container element to mount the viewer
1314
* @param document - URL to the PDF document
1415
* @returns Promise that resolves when the viewer is loaded
1516
*/
1617
export function loadBasicViewer(
1718
nutrientViewer: typeof NutrientViewer,
1819
container: HTMLElement,
19-
document = "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",
20+
document = nutrientConfig.documentUrl,
2021
) {
21-
return nutrientViewer.load({
22+
const config = {
2223
container,
2324
document,
24-
});
25+
...(nutrientConfig.baseUrl && { baseUrl: nutrientConfig.baseUrl }),
26+
};
27+
28+
return nutrientViewer.load(config);
2529
}
2630

2731
/**

frameworks/angular/TS/src/examples/custom-overlays/implementation.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type NutrientViewer from "@nutrient-sdk/viewer";
22
import type { Configuration } from "@nutrient-sdk/viewer";
3+
import { nutrientConfig } from "../nutrient-config";
34

45
/**
56
* Load a PDF viewer with custom overlays functionality
@@ -12,7 +13,7 @@ import type { Configuration } from "@nutrient-sdk/viewer";
1213
export async function loadCustomOverlaysViewer(
1314
nutrientViewer: typeof NutrientViewer,
1415
container: HTMLElement,
15-
document = "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",
16+
document = nutrientConfig.documentUrl,
1617
) {
1718
if (!nutrientViewer) {
1819
throw new Error("NutrientViewer is required");
@@ -21,11 +22,14 @@ export async function loadCustomOverlaysViewer(
2122
// Ensure there's only one NutrientViewer instance
2223
nutrientViewer.unload(container);
2324

24-
// Load the viewer with custom overlays configuration
25-
return load(nutrientViewer, {
25+
const config = {
2626
container,
2727
document,
28-
});
28+
...(nutrientConfig.baseUrl && { baseUrl: nutrientConfig.baseUrl }),
29+
};
30+
31+
// Load the viewer with custom overlays configuration
32+
return load(nutrientViewer, config);
2933
}
3034

3135
/**

frameworks/angular/TS/src/examples/magazine-mode/implementation.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type NutrientViewer from "@nutrient-sdk/viewer";
22
import type { Configuration } from "@nutrient-sdk/viewer";
3+
import { nutrientConfig } from "../nutrient-config";
34

45
type ToolbarItem =
56
| { type: "sidebar-bookmarks" }
@@ -40,13 +41,16 @@ interface ExtendedHTMLElement extends HTMLElement {
4041
export function loadMagazineViewer(
4142
nutrientViewer: typeof NutrientViewer,
4243
container: HTMLElement,
43-
document = "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",
44+
document = nutrientConfig.documentUrl,
4445
) {
45-
// Load the viewer with magazine-specific configuration
46-
return load(nutrientViewer, {
46+
const config = {
4747
container,
4848
document,
49-
});
49+
...(nutrientConfig.baseUrl && { baseUrl: nutrientConfig.baseUrl }),
50+
};
51+
52+
// Load the viewer with magazine-specific configuration
53+
return load(nutrientViewer, config);
5054
}
5155

5256
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Nutrient Web SDK Configuration
3+
*/
4+
5+
export interface NutrientConfig {
6+
/** Base URL for Nutrient Web SDK assets */
7+
baseUrl?: string;
8+
/** Default document URL for examples */
9+
documentUrl: string;
10+
}
11+
12+
/**
13+
* Default configuration for Nutrient Web SDK
14+
*/
15+
export const nutrientConfig: NutrientConfig = {
16+
// For CDN installations, this should be undefined
17+
// For package installations, this will be set to your local asset path
18+
baseUrl: undefined,
19+
20+
// Default document for examples - you can change this to your own PDF
21+
documentUrl: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",
22+
};

frameworks/next/JS/app/basic-viewer/page.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ function BasicViewerPage() {
1717

1818
if (!container) return;
1919

20-
let nutrientViewer;
20+
let nutrientViewer = null;
2121

22-
try {
23-
nutrientViewer = loadNutrientViewer();
22+
(async () => {
23+
nutrientViewer = await loadNutrientViewer();
2424

25-
loadBasicViewer(nutrientViewer, container);
26-
} catch (error) {
27-
console.error("Failed to load Nutrient Viewer:", error);
28-
}
25+
if (container && nutrientViewer) {
26+
loadBasicViewer(nutrientViewer, container);
27+
}
28+
})();
2929

3030
return () => {
3131
if (nutrientViewer && container) {

frameworks/next/JS/app/custom-overlays/page.jsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ function CustomOverlaysPage() {
1818

1919
let nutrientViewer = null;
2020

21-
try {
22-
nutrientViewer = loadNutrientViewer();
21+
(async () => {
22+
nutrientViewer = await loadNutrientViewer();
2323

24-
nutrientViewer.unload(container);
24+
if (container && nutrientViewer) {
25+
unloadCustomOverlaysViewer(nutrientViewer, container);
2526

26-
loadCustomOverlaysViewer(nutrientViewer, container);
27-
} catch (error) {
28-
console.error("Failed to load Nutrient Viewer:", error);
29-
}
27+
loadCustomOverlaysViewer(nutrientViewer, container);
28+
}
29+
})();
3030

3131
return () => {
3232
if (nutrientViewer && container) {
@@ -44,7 +44,6 @@ function CustomOverlaysPage() {
4444
borderBottom: "1px solid #ddd",
4545
display: "flex",
4646
alignItems: "center",
47-
gap: "1rem",
4847
}}
4948
>
5049
<Link

frameworks/next/JS/app/magazine-mode/page.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ function MagazineModePage() {
1616

1717
if (!container) return;
1818

19-
let nutrientViewer;
19+
let nutrientViewer = null;
2020

21-
try {
22-
nutrientViewer = loadNutrientViewer();
21+
(async () => {
22+
nutrientViewer = await loadNutrientViewer();
2323

24-
nutrientViewer.unload(container);
24+
if (container && nutrientViewer) {
25+
unloadMagazineViewer(nutrientViewer, container);
2526

26-
loadMagazineViewer(nutrientViewer, container);
27-
} catch (error) {
28-
console.error("Failed to load Nutrient Viewer:", error);
29-
}
27+
loadMagazineViewer(nutrientViewer, container);
28+
}
29+
})();
3030

3131
return () => {
3232
if (nutrientViewer && container) {

frameworks/next/JS/app/utils/loadNutrientViewer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function loadNutrientViewer() {
1+
export async function loadNutrientViewer() {
22
if (!window.NutrientViewer) {
33
throw new Error("NutrientViewer not found on window object");
44
}

frameworks/next/JS/examples/basic-viewer/implementation.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
/**
2-
* Basic Nutrient Viewer Implementation for React (TypeScript)
2+
* Basic Nutrient Viewer Implementation for React (JavaScript)
33
*
44
* This is the simplest way to load a PDF document with Nutrient Web SDK.
55
*/
66

7+
import { nutrientConfig } from "../nutrient-config.js";
8+
79
/**
810
* Load a basic PDF viewer
9-
* @param nutrientViewer - The NutrientViewer object (from CDN)
11+
* @param nutrientViewer - The NutrientViewer object (from CDN or package)
1012
* @param container - The container element to mount the viewer
1113
* @param document - URL to the PDF document
1214
* @returns Promise that resolves when the viewer is loaded
1315
*/
1416
export function loadBasicViewer(
1517
nutrientViewer,
1618
container,
17-
document = "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",
19+
document = nutrientConfig.documentUrl,
1820
) {
19-
return nutrientViewer.load({
21+
const config = {
2022
container,
2123
document,
22-
});
24+
};
25+
26+
// Add baseUrl if configured (for package installations)
27+
if (nutrientConfig.baseUrl) {
28+
config.baseUrl = nutrientConfig.baseUrl;
29+
}
30+
31+
return nutrientViewer.load(config);
2332
}
2433

2534
/**

0 commit comments

Comments
 (0)