Browse Source

fix(capacitor): upload assets

charlie 4 months ago
parent
commit
8a35115e2f

+ 1 - 0
gulpfile.js

@@ -113,6 +113,7 @@ const common = {
         'node_modules/marked/marked.min.js',
         'node_modules/@highlightjs/cdn-assets/highlight.min.js',
         'node_modules/@ionic/core/dist/ionic/**',
+        'node_modules/@isomorphic-git/lightning-fs/dist/lightning-fs.min.js',
         'node_modules/react/umd/react.production.min.js',
         'node_modules/react/umd/react.development.js',
         'node_modules/react-dom/umd/react-dom.production.min.js',

+ 12 - 0
resources/capacitor/index.html

@@ -9,6 +9,18 @@
 </head>
 <body>
 <div id="root"></div>
+<script src="./js/magic_portal.js"></script>
+<script>let worker = new Worker('./js/worker.js')
+const portal = new MagicPortal(worker);
+(async () => {
+  const fs = await portal.get('fs')
+  window.fs = fs
+  const pfs = await portal.get('pfs')
+  window.pfs = pfs
+  const workerThread = await portal.get('workerThread')
+  window.workerThread = workerThread
+})()
+</script>
 <script defer src="./js/highlight.min.js"></script>
 <script defer src="./js/interact.min.js"></script>
 <script defer src="./js/marked.min.js"></script>

File diff suppressed because it is too large
+ 0 - 0
resources/capacitor/js/magic_portal.js


+ 60 - 0
resources/capacitor/js/worker.js

@@ -0,0 +1,60 @@
+importScripts(
+  // Batched optimization
+  "./lightning-fs.min.js",
+  // Fixed a bug
+  "./magic_portal.js"
+);
+
+const detect = () => {
+  if (typeof window !== 'undefined' && !self.skipWaiting) {
+    return 'window'
+  } else if (typeof self !== 'undefined' && !self.skipWaiting) {
+    return 'Worker'
+  } else if (typeof self !== 'undefined' && self.skipWaiting) {
+    return 'ServiceWorker'
+  }
+};
+
+const fsName = 'logseq';
+const createFS = () => new LightningFS(fsName);
+let fs = createFS();
+let pfs = fs.promises;
+
+if (detect() === 'Worker') {
+  const portal = new MagicPortal(self);
+  portal.set('fs', fs);
+  portal.set('pfs', pfs);
+
+  portal.set('workerThread', {
+    rimraf: async function (path) {
+      // try {
+      //   // First assume path is itself a file
+      //   await pfs.unlink(path)
+      //   // if that worked we're done
+      //   return
+      // } catch (err) {
+      //   // Otherwise, path must be a directory
+      //   if (err.code !== 'EISDIR') throw err
+      // }
+      // Knowing path is a directory,
+      // first, assume everything inside path is a file.
+      let files = await pfs.readdir(path);
+      for (let file of files) {
+        let child = path + '/' + file
+        try {
+          await pfs.unlink(child)
+        } catch (err) {
+          if (err.code !== 'EISDIR') throw err
+        }
+      }
+      // Assume what's left are directories and recurse.
+      let dirs = await pfs.readdir(path)
+      for (let dir of dirs) {
+        let child = path + '/' + dir
+        await rimraf(child, pfs)
+      }
+      // Finally, delete the empty directory
+      await pfs.rmdir(path)
+    }
+  });
+}

Some files were not shown because too many files changed in this diff