Skip to content

Commit 2568a6f

Browse files
committed
feat: imrpove plugin loading
- load themes plugin on acode startup and load others after successfully starting acode
1 parent e7c5efa commit 2568a6f

File tree

3 files changed

+102
-11
lines changed

3 files changed

+102
-11
lines changed

biome.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@
44
"organizeImports": { "enabled": true },
55
"linter": {
66
"enabled": true,
7-
"rules": { "recommended": false }
7+
"rules": {
8+
"recommended": false,
9+
"complexity": {
10+
"noForEach": "warn",
11+
"noStaticOnlyClass": "error",
12+
"noUselessSwitchCase": "error",
13+
"useFlatMap": "error"
14+
},
15+
"style": {
16+
"noNegationElse": "off",
17+
"useForOf": "error",
18+
"useNodejsImportProtocol": "error",
19+
"useNumberNamespace": "error"
20+
},
21+
"suspicious": {
22+
"noDoubleEquals": "error",
23+
"noThenProperty": "error",
24+
"useIsArray": "error"
25+
}
26+
}
827
},
28+
"javascript": { "globals": ["Global1"] },
929
"files": {
1030
"include": ["src/**/*", "utils/**/*.js", "www/**/*.js", "www/res/**/*.css"],
1131
"ignore": [

src/lib/loadPlugins.js

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,85 @@
11
import fsOperation from "../fileSystem";
22
import Url from "../utils/Url";
33
import loadPlugin from "./loadPlugin";
4+
import settings from "./settings";
45

5-
export default async function loadPlugins() {
6+
// theme-related keywords for determining theme plugins
7+
const THEME_IDENTIFIERS = new Set([
8+
"theme",
9+
"catppuccin",
10+
"pine",
11+
"githubdark",
12+
"radiant",
13+
"rdtheme",
14+
"ayumirage",
15+
"dust",
16+
"synthwave",
17+
"dragon",
18+
"mint",
19+
"monokai",
20+
"lumina_code",
21+
"sweet",
22+
"moonlight",
23+
"bluloco",
24+
]);
25+
26+
export default async function loadPlugins(onlyTheme = false) {
627
const plugins = await fsOperation(PLUGIN_DIR).lsDir();
728
const results = [];
829
const failedPlugins = [];
30+
const loadedPlugins = new Set();
931

1032
if (plugins.length > 0) {
1133
toast(strings["loading plugins"]);
1234
}
1335

36+
let pluginsToLoad = [];
37+
const currentTheme = settings.value.appTheme;
38+
39+
if (onlyTheme) {
40+
// Only load theme plugins matching current theme
41+
pluginsToLoad = plugins.filter((pluginDir) => {
42+
const pluginId = Url.basename(pluginDir.url);
43+
return isThemePlugin(pluginId) && !loadedPlugins.has(pluginId);
44+
});
45+
} else {
46+
// Load non-theme plugins that aren't loaded yet
47+
pluginsToLoad = plugins.filter((pluginDir) => {
48+
const pluginId = Url.basename(pluginDir.url);
49+
return !isThemePlugin(pluginId) && !loadedPlugins.has(pluginId);
50+
});
51+
}
52+
1453
// Load plugins concurrently
15-
const loadPromises = plugins.map(async (pluginDir) => {
54+
const loadPromises = pluginsToLoad.map(async (pluginDir) => {
1655
const pluginId = Url.basename(pluginDir.url);
56+
57+
if (onlyTheme && currentTheme) {
58+
const pluginIdLower = pluginId.toLowerCase();
59+
const currentThemeLower = currentTheme.toLowerCase();
60+
let matchFound = false;
61+
if (pluginIdLower.includes(currentThemeLower)) {
62+
matchFound = true;
63+
}
64+
65+
if (!matchFound && !isThemePlugin(pluginId)) {
66+
return;
67+
}
68+
}
69+
1770
try {
1871
await loadPlugin(pluginId);
72+
loadedPlugins.add(pluginId);
1973
results.push(true);
2074
} catch (error) {
21-
window.log("error", `Failed to load plugin: ${pluginId}`);
22-
window.log("error", error);
23-
toast(`Failed to load plugin: ${pluginId}`);
75+
console.error(`Error loading plugin ${pluginId}:`, error);
2476
failedPlugins.push(pluginId);
2577
results.push(false);
2678
}
2779
});
2880

2981
await Promise.allSettled(loadPromises);
82+
3083
if (failedPlugins.length > 0) {
3184
setTimeout(() => {
3285
cleanupFailedPlugins(failedPlugins).catch((error) => {
@@ -37,6 +90,13 @@ export default async function loadPlugins() {
3790
return results.filter(Boolean).length;
3891
}
3992

93+
function isThemePlugin(pluginId) {
94+
// Convert to lowercase for case-insensitive matching
95+
const id = pluginId.toLowerCase();
96+
// Check if any theme identifier is present in the plugin ID
97+
return Array.from(THEME_IDENTIFIERS).some((theme) => id.includes(theme));
98+
}
99+
40100
async function cleanupFailedPlugins(pluginIds) {
41101
for (const pluginId of pluginIds) {
42102
try {

src/lib/main.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,23 @@ async function onDeviceReady() {
233233
window.log("error", error);
234234
toast(`Error: ${error.message}`);
235235
} finally {
236-
setTimeout(() => {
236+
setTimeout(async () => {
237237
document.body.removeAttribute("data-small-msg");
238238
app.classList.remove("loading", "splash");
239239
applySettings.afterRender();
240+
241+
// load plugins
242+
try {
243+
await loadPlugins();
244+
} catch (error) {
245+
window.log("error", "Failed to load theme plugins!");
246+
window.log("error", error);
247+
toast("Failed to load theme plugins!");
248+
}
249+
applySettings.afterRender();
240250
}, 500);
241251
}
252+
242253
// Check for app updates
243254
if (navigator.onLine) {
244255
cordova.plugin.http.sendRequest(
@@ -430,13 +441,13 @@ async function loadApp() {
430441

431442
new EditorFile();
432443

433-
//load plugins
444+
// load theme plugins
434445
try {
435-
await loadPlugins();
446+
await loadPlugins(true);
436447
} catch (error) {
437-
window.log("error", "Plugins loading failed!");
448+
window.log("error", "Failed to load theme plugins!");
438449
window.log("error", error);
439-
toast("Plugins loading failed!");
450+
toast("Failed to load theme plugins!");
440451
}
441452

442453
acode.setLoadingMessage("Loading folders...");

0 commit comments

Comments
 (0)