Просмотр исходного кода

Update electron.mjs to desktop.mjs

Lutz Roeder 11 месяцев назад
Родитель
Сommit
f289c86446
5 измененных файлов с 33 добавлено и 29 удалено
  1. 1 1
      package.py
  2. 1 1
      source/app.js
  3. 12 12
      source/browser.js
  4. 18 14
      source/desktop.mjs
  5. 1 1
      source/index.js

+ 1 - 1
package.py

@@ -38,7 +38,7 @@ def _build():
     shutil.copyfile(
         os.path.join(root_dir, "pyproject.toml"),
         os.path.join(dist_pypi_dir, "pyproject.toml"))
-    os.remove(os.path.join(dist_pypi_dir, "netron", "electron.mjs"))
+    os.remove(os.path.join(dist_pypi_dir, "netron", "desktop.mjs"))
     os.remove(os.path.join(dist_pypi_dir, "netron", "app.js"))
 
 def _install():

+ 1 - 1
source/app.js

@@ -660,7 +660,7 @@ app.View = class {
             width: size.width > 1024 ? 1024 : size.width,
             height: size.height > 768 ? 768 : size.height,
             webPreferences: {
-                preload: path.join(dirname, 'electron.mjs'),
+                preload: path.join(dirname, 'desktop.mjs'),
                 nodeIntegration: true,
                 enableDeprecatedPaste: true
             }

+ 12 - 12
source/browser.js

@@ -1,9 +1,9 @@
 
 import * as base from './base.js';
 
-const host = {};
+const browser = {};
 
-host.BrowserHost = class {
+browser.Host = class {
 
     constructor() {
         this._window = window;
@@ -431,7 +431,7 @@ host.BrowserHost = class {
                     stream = await this._request(url, null, null, progress);
                 }
             }
-            context = new host.BrowserHost.Context(this, url, identifier, name, stream);
+            context = new browser.Context(this, url, identifier, name, stream);
             this._telemetry.set('session_engaged', 1);
         } catch (error) {
             await this._view.error(error, 'Model load request failed.');
@@ -443,7 +443,7 @@ host.BrowserHost = class {
 
     async _open(file, files) {
         this._view.show('welcome spinner');
-        const context = new host.BrowserHost.BrowserFileContext(this, file, files);
+        const context = new browser.BrowserFileContext(this, file, files);
         try {
             await context.open();
             await this._openContext(context);
@@ -475,7 +475,7 @@ host.BrowserHost = class {
             const encoder = new TextEncoder();
             const buffer = encoder.encode(file.content);
             const stream = new base.BinaryStream(buffer);
-            const context = new host.BrowserHost.Context(this, '', identifier, null, stream);
+            const context = new browser.Context(this, '', identifier, null, stream);
             await this._openContext(context);
         } catch (error) {
             await this._view.error(error, 'Error while loading Gist.');
@@ -593,7 +593,7 @@ host.BrowserHost = class {
     }
 };
 
-host.BrowserHost.BrowserFileContext = class {
+browser.BrowserFileContext = class {
 
     constructor(host, file, blobs) {
         this._host = host;
@@ -640,7 +640,7 @@ host.BrowserHost.BrowserFileContext = class {
                             const slice = blob.slice(position, Math.min(position + size, blob.size));
                             reader.readAsArrayBuffer(slice);
                         } else {
-                            const stream = new host.BrowserHost.FileStream(chunks, size, 0, position);
+                            const stream = new browser.FileStream(chunks, size, 0, position);
                             resolve(stream);
                         }
                     }
@@ -688,7 +688,7 @@ host.BrowserHost.BrowserFileContext = class {
     }
 };
 
-host.BrowserHost.FileStream = class {
+browser.FileStream = class {
 
     constructor(chunks, size, start, length) {
         this._chunks = chunks;
@@ -707,7 +707,7 @@ host.BrowserHost.FileStream = class {
     }
 
     stream(length) {
-        const file = new host.BrowserHost.FileStream(this._chunks, this._size, this._start + this._position, length);
+        const file = new browser.FileStream(this._chunks, this._size, this._start + this._position, length);
         this.skip(length);
         return file;
     }
@@ -795,7 +795,7 @@ host.BrowserHost.FileStream = class {
     }
 };
 
-host.BrowserHost.Context = class {
+browser.Context = class {
 
     constructor(host, url, identifier, name, stream) {
         this._host = host;
@@ -877,7 +877,7 @@ if (!('scrollBehavior' in window.document.documentElement.style)) {
 }
 
 if (typeof window !== 'undefined' && window.exports) {
-    window.exports.browser = host;
+    window.exports.browser = browser;
 }
 
-export const BrowserHost = host.BrowserHost;
+export const Host = browser.Host;

+ 18 - 14
source/electron.mjs → source/desktop.mjs

@@ -9,9 +9,9 @@ import * as path from 'path';
 import * as url from 'url';
 import * as view from './view.js';
 
-const host = {};
+const desktop = {};
 
-host.ElectronHost = class {
+desktop.Host = class {
 
     constructor() {
         this._document = window.document;
@@ -350,7 +350,7 @@ host.ElectronHost = class {
                 } else if (encoding) {
                     reject(new Error(`The file '${file}' size (${stat.size.toString()}) for encoding '${encoding}' is greater than 2 GB.`));
                 } else {
-                    resolve(new host.ElectronHost.FileStream(pathname, 0, stat.size, stat.mtimeMs));
+                    resolve(new desktop.FileStream(pathname, 0, stat.size, stat.mtimeMs));
                 }
             });
         });
@@ -420,7 +420,7 @@ host.ElectronHost = class {
         if (stat.isFile()) {
             const dirname = path.dirname(location);
             const stream = await this.request(basename, null, dirname);
-            return new host.ElectronHost.Context(this, dirname, basename, stream);
+            return new desktop.Context(this, dirname, basename, stream);
         } else if (stat.isDirectory()) {
             const entries = new Map();
             const walk = (dir) => {
@@ -430,14 +430,14 @@ host.ElectronHost = class {
                     if (stat.isDirectory()) {
                         walk(pathname);
                     } else if (stat.isFile()) {
-                        const stream = new host.ElectronHost.FileStream(pathname, 0, stat.size, stat.mtimeMs);
+                        const stream = new desktop.FileStream(pathname, 0, stat.size, stat.mtimeMs);
                         const name = pathname.split(path.sep).join(path.posix.sep);
                         entries.set(name, stream);
                     }
                 }
             };
             walk(location);
-            return new host.ElectronHost.Context(this, location, basename, null, entries);
+            return new desktop.Context(this, location, basename, null, entries);
         }
         throw new Error(`Unsupported path stat '${JSON.stringify(stat)}'.`);
     }
@@ -607,7 +607,7 @@ host.ElectronHost = class {
     }
 };
 
-host.ElectronHost.FileStream = class {
+desktop.FileStream = class {
 
     constructor(file, start, length, mtime) {
         this._file = file;
@@ -626,7 +626,7 @@ host.ElectronHost.FileStream = class {
     }
 
     stream(length) {
-        const file = new host.ElectronHost.FileStream(this._file, this._position, length, this._mtime);
+        const file = new desktop.FileStream(this._file, this._start + this._position, length, this._mtime);
         this.skip(length);
         return file;
     }
@@ -703,7 +703,7 @@ host.ElectronHost.FileStream = class {
     }
 };
 
-host.ElectronHost.Context = class {
+desktop.Context = class {
 
     constructor(host, folder, identifier, stream, entries) {
         this._host = host;
@@ -738,8 +738,12 @@ host.ElectronHost.Context = class {
     }
 };
 
-window.addEventListener('load', () => {
-    const value = new host.ElectronHost();
-    window.__view__ = new view.View(value);
-    window.__view__.start();
-});
+if (typeof window !== 'undefined') {
+    window.addEventListener('load', () => {
+        const value = new desktop.Host();
+        window.__view__ = new view.View(value);
+        window.__view__.start();
+    });
+}
+
+export const FileStream = desktop.FileStream;

+ 1 - 1
source/index.js

@@ -113,7 +113,7 @@ window.addEventListener('load', function() {
         if (error) {
             window.exports.terminate(error.message);
         } else {
-            var host = new window.exports.browser.BrowserHost();
+            var host = new window.exports.browser.Host();
             window.__view__ = new window.exports.view.View(host);
             window.__view__.start();
         }