fix(capacitor): upload assets

This commit is contained in:
charlie
2025-05-28 09:20:24 +08:00
parent 03880fc47e
commit 8a35115e2f
4 changed files with 74 additions and 0 deletions

View File

@@ -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',

View File

@@ -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>

View File

@@ -0,0 +1 @@
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.MagicPortal=t()}(this,function(){var e=function(e){var t=this;this.rpc_counter=0,this.channel=e,this.foreign=new Map,this.local=new Map,this.calls=new Map,this.queue=[],this.connectionEstablished=!1,this.channel.addEventListener("message",function(e){var n=e.data;if(n&&"object"==typeof n)switch(n.type){case"MP_INIT":return t.onInit(n);case"MP_SET":return t.onSet(n);case"MP_CALL":return t.onCall(n);case"MP_RETURN":return t.onReturn(n)}}),this.channel.postMessage({type:"MP_INIT",id:1,reply:!0})};e.prototype.onInit=function(e){this.connectionEstablished=!0;var t=this.queue;this.queue=[];for(var n=0,o=t;n<o.length;n+=1){this.channel.postMessage(o[n])}e.reply&&this.channel.postMessage({type:"MP_INIT",reply:!1})},e.prototype.onSet=function(e){for(var t=this,n={},o=e.object,i=function(){var i=r[s],c=!e.void.includes(i);n[i]=function(){for(var e=[],n=arguments.length;n--;)e[n]=arguments[n];return t.rpc_counter=(t.rpc_counter+1)%Number.MAX_SAFE_INTEGER,new Promise(function(n,s){t.postMessage({type:"MP_CALL",object:o,method:i,id:t.rpc_counter,args:e,reply:c}),c?t.calls.set(t.rpc_counter,{resolve:n,reject:s}):n()})}},s=0,r=e.methods;s<r.length;s+=1)i();var c=this.foreign.get(e.object);this.foreign.set(e.object,n),"function"==typeof c&&c(n)},e.prototype.onCall=function(e){var t=this,n=this.local.get(e.object);n&&n[e.method].apply(n,e.args).then(function(n){return e.reply&&t.channel.postMessage({type:"MP_RETURN",id:e.id,result:n})}).catch(function(n){return t.channel.postMessage({type:"MP_RETURN",id:e.id,error:n.message})})},e.prototype.onReturn=function(e){if(this.calls.has(e.id)){var t=this.calls.get(e.id),n=t.resolve,o=t.reject;this.calls.delete(e.id),e.error?o(e.error):n(e.result)}},e.prototype.postMessage=function(e){this.connectionEstablished?this.channel.postMessage(e):this.queue.push(e)},e.prototype.set=function(e,t,n){void 0===n&&(n={}),this.local.set(e,t);var o=Object.entries(t).filter(function(e){return"function"==typeof e[1]}).map(function(e){return e[0]});this.postMessage({type:"MP_SET",object:e,methods:o,void:n.void||[]})},e.prototype.get=function(e){return new Promise(function(t,n){var o=this;return this.foreign.has(e)?t(this.foreign.get(e)):t(new Promise(function(t,n){return o.foreign.set(e,t)}))}.bind(this))};return function(t){var n=new e(t);Object.defineProperties(this,{get:{writable:!1,configurable:!1,value:n.get.bind(n)},set:{writable:!1,configurable:!1,value:n.set.bind(n)}})}});

View File

@@ -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)
}
});
}