|
1 | | -import { join, isAbsolute } from "path"; |
2 | | -import { type NmriumState, read } from "nmr-load-save"; |
3 | | -import { fileCollectionFromWebSource, fileCollectionFromPath } from "filelist-utils"; |
4 | | -import playwright from 'playwright'; |
5 | | - |
| 1 | +import { join, isAbsolute } from 'path' |
| 2 | +import { type NmriumState } from '@zakodium/nmrium-core' |
| 3 | +import init from '@zakodium/nmrium-core-plugins' |
| 4 | +import { |
| 5 | + fileCollectionFromWebSource, |
| 6 | + fileCollectionFromPath, |
| 7 | +} from 'filelist-utils' |
| 8 | +import playwright from 'playwright' |
6 | 9 |
|
7 | 10 | interface Snapshot { |
8 | | - image: string, |
9 | | - id: string; |
| 11 | + image: string |
| 12 | + id: string |
10 | 13 | } |
11 | 14 |
|
| 15 | +const core = init() |
12 | 16 |
|
13 | 17 | function generateNMRiumURL() { |
14 | | - const baseURL = process.env['BASE_NMRIUM_URL'] || ''; |
15 | | - const url = new URL(baseURL) |
16 | | - url.searchParams.append('workspace', "embedded") |
17 | | - return url.toString() |
| 18 | + const baseURL = process.env['BASE_NMRIUM_URL'] || '' |
| 19 | + const url = new URL(baseURL) |
| 20 | + url.searchParams.append('workspace', 'embedded') |
| 21 | + return url.toString() |
18 | 22 | } |
19 | 23 |
|
20 | | - |
21 | 24 | async function captureSpectraViewAsBase64(nmriumState: Partial<NmriumState>) { |
22 | | - const { data: { spectra } = { spectra: [] }, version } = nmriumState; |
23 | | - const browser = await playwright.chromium.launch() |
24 | | - const context = await browser.newContext(playwright.devices['Desktop Chrome HiDPI']) |
25 | | - const page = await context.newPage() |
26 | | - |
27 | | - const url = generateNMRiumURL() |
28 | | - |
29 | | - await page.goto(url) |
| 25 | + const { data: { spectra } = { spectra: [] }, version } = nmriumState |
| 26 | + const browser = await playwright.chromium.launch() |
| 27 | + const context = await browser.newContext( |
| 28 | + playwright.devices['Desktop Chrome HiDPI'] |
| 29 | + ) |
| 30 | + const page = await context.newPage() |
30 | 31 |
|
31 | | - await page.locator('text=Loading').waitFor({ state: 'hidden' }); |
| 32 | + const url = generateNMRiumURL() |
32 | 33 |
|
33 | | - let snapshots: Snapshot[] = [] |
| 34 | + await page.goto(url) |
34 | 35 |
|
35 | | - for (const spectrum of spectra || []) { |
36 | | - const spectrumObject = { |
37 | | - version, |
38 | | - data: { |
39 | | - spectra: [{ ...spectrum }], |
40 | | - } |
| 36 | + await page.locator('text=Loading').waitFor({ state: 'hidden' }) |
41 | 37 |
|
42 | | - } |
| 38 | + let snapshots: Snapshot[] = [] |
43 | 39 |
|
44 | | - // convert typed array to array |
45 | | - const stringObject = JSON.stringify(spectrumObject, (key, value: unknown) => { |
46 | | - return ArrayBuffer.isView(value) ? Array.from(value as unknown as Iterable<unknown>) : value |
47 | | - }) |
| 40 | + for (const spectrum of spectra || []) { |
| 41 | + const spectrumObject = { |
| 42 | + version, |
| 43 | + data: { |
| 44 | + spectra: [{ ...spectrum }], |
| 45 | + }, |
| 46 | + } |
48 | 47 |
|
49 | | - // load the spectrum into NMRium using the custom event |
50 | | - await page.evaluate( |
51 | | - ` |
| 48 | + // convert typed array to array |
| 49 | + const stringObject = JSON.stringify( |
| 50 | + spectrumObject, |
| 51 | + (key, value: unknown) => { |
| 52 | + return ArrayBuffer.isView(value) |
| 53 | + ? Array.from(value as unknown as Iterable<unknown>) |
| 54 | + : value |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + // load the spectrum into NMRium using the custom event |
| 59 | + await page.evaluate( |
| 60 | + ` |
52 | 61 | window.postMessage({ type: "nmr-wrapper:load", data:{data: ${stringObject},type:"nmrium"}}, '*'); |
53 | 62 | ` |
54 | | - ) |
55 | | - |
56 | | - //wait for NMRium process and load spectra |
57 | | - await page.locator('text=Loading').waitFor({ state: 'hidden' }); |
58 | | - |
59 | | - // take a snapshot for the spectrum |
60 | | - try { |
61 | | - const snapshot = await page.locator('#nmrSVG .container').screenshot() |
62 | | - |
63 | | - snapshots.push({ |
64 | | - image: snapshot.toString('base64'), |
65 | | - id: spectrum.id, |
66 | | - }) |
67 | | - } catch (e) { |
68 | | - console.log(e) |
69 | | - } |
70 | | - } |
| 63 | + ) |
71 | 64 |
|
72 | | - await context.close() |
73 | | - await browser.close() |
| 65 | + //wait for NMRium process and load spectra |
| 66 | + await page.locator('text=Loading').waitFor({ state: 'hidden' }) |
74 | 67 |
|
75 | | - return snapshots; |
76 | | -} |
| 68 | + // take a snapshot for the spectrum |
| 69 | + try { |
| 70 | + const snapshot = await page.locator('#nmrSVG .container').screenshot() |
77 | 71 |
|
78 | | -async function loadSpectrumFromURL(url: string, enableSnapshot = false) { |
79 | | - const { pathname: relativePath, origin: baseURL } = new URL(url); |
80 | | - const source = { |
81 | | - entries: [ |
82 | | - { |
83 | | - relativePath, |
84 | | - } |
85 | | - ], |
86 | | - baseURL |
87 | | - }; |
88 | | - const fileCollection = await fileCollectionFromWebSource(source, {}); |
89 | | - |
90 | | - const { |
91 | | - nmriumState: { data, version }, |
92 | | - } = await read(fileCollection); |
93 | | - |
94 | | - let images: Snapshot[] = [] |
95 | | - |
96 | | - if (enableSnapshot) { |
97 | | - images = await captureSpectraViewAsBase64({ data, version }); |
| 72 | + snapshots.push({ |
| 73 | + image: snapshot.toString('base64'), |
| 74 | + id: spectrum.id, |
| 75 | + }) |
| 76 | + } catch (e) { |
| 77 | + console.log(e) |
98 | 78 | } |
| 79 | + } |
99 | 80 |
|
| 81 | + await context.close() |
| 82 | + await browser.close() |
100 | 83 |
|
101 | | - return { data, version, images }; |
| 84 | + return snapshots |
102 | 85 | } |
103 | 86 |
|
| 87 | +async function loadSpectrumFromURL(url: string, enableSnapshot = false) { |
| 88 | + const { pathname: relativePath, origin: baseURL } = new URL(url) |
| 89 | + const source = { |
| 90 | + entries: [ |
| 91 | + { |
| 92 | + relativePath, |
| 93 | + }, |
| 94 | + ], |
| 95 | + baseURL, |
| 96 | + } |
| 97 | + const fileCollection = await fileCollectionFromWebSource(source, {}) |
| 98 | + |
| 99 | + const { |
| 100 | + nmriumState: { data, version }, |
| 101 | + } = await core.read(fileCollection) |
| 102 | + |
| 103 | + let images: Snapshot[] = [] |
| 104 | + |
| 105 | + if (enableSnapshot) { |
| 106 | + images = await captureSpectraViewAsBase64({ data, version }) |
| 107 | + } |
| 108 | + |
| 109 | + return { data, version, images } |
| 110 | +} |
104 | 111 |
|
105 | 112 | async function loadSpectrumFromFilePath(path: string, enableSnapshot = false) { |
106 | | - const dirPath = isAbsolute(path) ? path : join(process.cwd(), path) |
| 113 | + const dirPath = isAbsolute(path) ? path : join(process.cwd(), path) |
107 | 114 |
|
108 | | - const fileCollection = await fileCollectionFromPath(dirPath, {}); |
| 115 | + const fileCollection = await fileCollectionFromPath(dirPath, {}) |
109 | 116 |
|
110 | | - const { |
111 | | - nmriumState: { data, version } |
112 | | - } = await read(fileCollection); |
| 117 | + const { |
| 118 | + nmriumState: { data, version }, |
| 119 | + } = await core.read(fileCollection) |
113 | 120 |
|
114 | | - let images: Snapshot[] = [] |
| 121 | + let images: Snapshot[] = [] |
115 | 122 |
|
116 | | - if (enableSnapshot) { |
117 | | - images = await captureSpectraViewAsBase64({ data, version }); |
118 | | - } |
| 123 | + if (enableSnapshot) { |
| 124 | + images = await captureSpectraViewAsBase64({ data, version }) |
| 125 | + } |
119 | 126 |
|
120 | | - |
121 | | - return { data, version, images }; |
| 127 | + return { data, version, images } |
122 | 128 | } |
123 | 129 |
|
124 | | - |
125 | | -export { |
126 | | - loadSpectrumFromFilePath, |
127 | | - loadSpectrumFromURL |
128 | | -}; |
129 | | - |
| 130 | +export { loadSpectrumFromFilePath, loadSpectrumFromURL } |
0 commit comments