diff --git a/src/libopencor/locAPI.ts b/src/libopencor/locAPI.ts index a479cfb..38c1139 100644 --- a/src/libopencor/locAPI.ts +++ b/src/libopencor/locAPI.ts @@ -5,20 +5,32 @@ import libOpenCOR from 'libopencor' // @ts-expect-error (window.locAPI may or may not be defined and that is why we test it) const _locAPI = window.locAPI ?? (await libOpenCOR()) -class LOCAPI { - // Some general methods. +// Some general methods. - cppVersion(): boolean { - return electronAPI !== undefined - } +export function cppVersion(): boolean { + return electronAPI !== undefined +} - wasmVersion(): boolean { - return !this.cppVersion() +export function wasmVersion(): boolean { + return !cppVersion() +} + +export function version(): string { + return cppVersion() ? _locAPI.version() : _locAPI.versionString() +} + +// File API. + +export class File { + _path: string + _contents: Uint8Array + + constructor(path: string, contents?: Uint8Array) { + this._path = path + this._contents = contents ?? new Uint8Array() } - version(): string { - return this.cppVersion() ? _locAPI.version() : _locAPI.versionString() + contents(): Uint8Array { + return this._contents } } - -export const locAPI: LOCAPI = new LOCAPI() diff --git a/src/libopencor/src/file.cpp b/src/libopencor/src/file.cpp new file mode 100644 index 0000000..46f466d --- /dev/null +++ b/src/libopencor/src/file.cpp @@ -0,0 +1,8 @@ +#include "file.h" + +#include + +void fileCreate(const Napi::CallbackInfo &pInfo) +{ + libOpenCOR::File::create(pInfo[0].ToString().Utf8Value()); +} diff --git a/src/libopencor/src/file.h b/src/libopencor/src/file.h new file mode 100644 index 0000000..3adfbb2 --- /dev/null +++ b/src/libopencor/src/file.h @@ -0,0 +1,3 @@ +#include + +void fileCreate(const Napi::CallbackInfo &pInfo); diff --git a/src/libopencor/src/main.cpp b/src/libopencor/src/main.cpp index fe65bc0..b6efc40 100644 --- a/src/libopencor/src/main.cpp +++ b/src/libopencor/src/main.cpp @@ -1,7 +1,16 @@ +#include "file.h" #include "version.h" Napi::Object init(Napi::Env pEnv, Napi::Object pExports) { + // Note: src/preload/index.ts must be in sync with this file. + + // File API. + + pExports.Set(Napi::String::New(pEnv, "fileCreate"), Napi::Function::New(pEnv, fileCreate)); + + // Version API. + pExports.Set(Napi::String::New(pEnv, "version"), Napi::Function::New(pEnv, version)); return pExports; diff --git a/src/preload/index.ts b/src/preload/index.ts index c9d78df..f421be7 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -61,8 +61,10 @@ electron.contextBridge.exposeInMainWorld('electronAPI', { }) }) -// Give our renderer process access to the C++ version of libOpenCOR. +// Give our renderer process access to the native node module for libOpenCOR. +// Note: src/libopencor/src/main.cpp must be in sync with this file. electron.contextBridge.exposeInMainWorld('locAPI', { + fileCreate: () => loc.fileCreate(), version: () => loc.version() }) diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index a274846..b150016 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -35,6 +35,7 @@ import * as vue from 'vue' import { fileContents, filePath, isRemoteFilePath, toastLife } from './common' import { electronAPI } from '../../electronAPI' +import * as locAPI from '../../libopencor/locAPI' const toast = useToast() @@ -87,7 +88,7 @@ function onSettings(): void { // Open a file. function openFile(filePath: string, fileContentsPromise?: Promise): void { - if (fileContentsPromise !== undefined) { + function addToast(file: locAPI.File) { function topContents(contents: string): string { const numberOfBytesShown = 100 @@ -97,25 +98,31 @@ function openFile(filePath: string, fileContentsPromise?: Promise): ) } + toast.add({ + severity: 'info', + summary: 'Opening a file', + detail: + filePath + + '\n\nRaw contents:\n' + + topContents(new TextDecoder().decode(file.contents())) + + '\n\nUint8Array:\n' + + topContents(String(file.contents())) + + '\n\nBase64:\n' + + topContents(btoa(file.contents().reduce((data, byte) => data + String.fromCharCode(byte), ''))), + life: toastLife + }) + } + + if (fileContentsPromise !== undefined) { showSpinningWheel() fileContentsPromise .then((fileContents) => { + const file = new locAPI.File(filePath, fileContents) + hideSpinningWheel() - toast.add({ - severity: 'info', - summary: 'Opening a file', - detail: - filePath + - '\n\nRaw contents:\n' + - topContents(new TextDecoder().decode(fileContents)) + - '\n\nUint8Array:\n' + - topContents(String(fileContents)) + - '\n\nBase64:\n' + - topContents(btoa(fileContents.reduce((data, byte) => data + String.fromCharCode(byte), ''))), - life: toastLife - }) + addToast(file) }) .catch((error: unknown) => { hideSpinningWheel() @@ -132,16 +139,13 @@ function openFile(filePath: string, fileContentsPromise?: Promise): showSpinningWheel() } + const file = new locAPI.File(filePath) + if (isRemoteFilePath(filePath)) { hideSpinningWheel() } - toast.add({ - severity: 'info', - summary: 'Opening a file', - detail: filePath, - life: toastLife - }) + addToast(file) } } diff --git a/src/renderer/src/components/AboutDialog.vue b/src/renderer/src/components/AboutDialog.vue index bdc48fc..f750121 100644 --- a/src/renderer/src/components/AboutDialog.vue +++ b/src/renderer/src/components/AboutDialog.vue @@ -44,7 +44,7 @@