Skip to content

Commit 6142e6a

Browse files
Merge pull request #48 from ayoubmata81/fix/open-file-from-cli-args
fix(desktop): open file passed as CLI argument on launch
2 parents 60d4c00 + ca0148f commit 6142e6a

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

desktop-app/neutralino.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"enabled": true,
1414
"writeToLogFile": true
1515
},
16-
"nativeAllowList": ["app.*", "os.*", "debug.log"],
16+
"nativeAllowList": ["app.*", "os.*", "filesystem.readFile", "debug.log"],
1717
"globalVariables": {},
1818
"modes": {
1919
"window": {

desktop-app/resources/js/main.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,31 @@ if (NL_OS != "Darwin") {
9797
// TODO: Fix https://github.com/neutralinojs/neutralinojs/issues/615
9898
setTray();
9999
}
100+
101+
// Open file passed as command-line argument (e.g. when double-clicking a .md file)
102+
(async function loadInitialFile() {
103+
const args = Array.isArray(NL_ARGS) ? NL_ARGS : (() => { try { return JSON.parse(NL_ARGS); } catch(e) { return []; } })();
104+
const filePath = args.find(a => typeof a === 'string' && /\.(md|markdown)$/i.test(a));
105+
if (!filePath) return;
106+
107+
try {
108+
const content = await Neutralino.filesystem.readFile(filePath);
109+
110+
function applyContent() {
111+
const editor = document.getElementById('markdown-editor');
112+
const dropzone = document.getElementById('dropzone');
113+
if (!editor) return;
114+
editor.value = content;
115+
editor.dispatchEvent(new Event('input'));
116+
if (dropzone) dropzone.style.display = 'none';
117+
}
118+
119+
if (document.readyState === 'loading') {
120+
document.addEventListener('DOMContentLoaded', applyContent);
121+
} else {
122+
setTimeout(applyContent, 0);
123+
}
124+
} catch (e) {
125+
console.warn('Could not open initial file:', e);
126+
}
127+
})();

0 commit comments

Comments
 (0)