Open notes with custom software

This commit is contained in:
SnnBcd xt 2023-05-16 11:57:28 +00:00
parent 412fd105c9
commit 9fc474504b
3 changed files with 86 additions and 1 deletions

View File

@ -54,6 +54,15 @@ export default class RootCommandExecutor extends Component {
openService.openNoteExternally(noteId, mime);
}
}
openNoteCustomCommand() {
const noteId = appContext.tabManager.getActiveContextNoteId();
const mime = appContext.tabManager.getActiveContextNoteMime()
if (noteId) {
openService.openNoteCustom(noteId, mime);
}
}
enterProtectedSessionCommand() {
protectedSessionService.enterProtectedSession();

View File

@ -47,6 +47,78 @@ async function openNoteExternally(noteId, mime) {
}
}
async function openNoteCustom(noteId, mime) {
if (utils.isElectron()) {
const resp = await server.post(`notes/${noteId}/save-to-tmp-dir`);
const filePath = resp.tmpFilePath;
const { exec } = utils.dynamicRequire('child_process');
const platform = process.platform;
if (platform === 'linux') {
const terminals = ['gnome-terminal', 'konsole', 'xterm', 'xfce4-terminal', 'mate-terminal', 'rxvt', 'terminator', 'terminology'];
let foundTerminal = false;
let availableTerminal = null;
const openFileWithTerminal = (terminal) => {
const command = `${terminal} -e 'mimeopen -d "${filePath}"'`;
console.log(`Open Note custom: ${command}. `);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Open Note custom: Failed to open file with ${terminal}: ${error}`);
searchTerminal(terminals.indexOf(terminal) + 1);
} else {
console.log(`Open Note custom: File opened with ${terminal}. `);
console.log(`Open Note custom: ${stdout}. `);
}
});
};
const searchTerminal = (index) => {
const terminal = terminals[index];
if (!terminal) {
console.error('Open Note custom: No terminal found!');
open(getFileUrl(noteId), { url: true });
return;
}
exec(`which ${terminal}`, (error, stdout, stderr) => {
if (stdout.trim()) {
foundTerminal = true;
availableTerminal = terminal;
}
if (foundTerminal) {
openFileWithTerminal(availableTerminal);
} else {
searchTerminal(index + 1);
}
});
};
searchTerminal(0);
} else if (platform === 'win32') {
if (filePath.indexOf("/") !== -1) {
//Note that the path separator must be \ instead of /
filePath = filePath.replace(/\//g, "\\");
}
const command = `rundll32.exe shell32.dll,OpenAs_RunDLL ` + filePath;
exec(command, (err, stdout, stderr) => {
if (err) {
console.error("Open Note custom: ", err);
open(getFileUrl(noteId), { url: true });
return;
}
});
} else {
console.log('Currently "Open Note custom" only supports linux and windows systems');
open(getFileUrl(noteId), { url: true });
}
}
else {
// allow browser to handle opening common file
if (mime === "application/pdf" || mime.startsWith("image") || mime.startsWith("audio") || mime.startsWith("video")) {
window.open(getOpenFileUrl(noteId));
}
else {
window.location.href = getFileUrl(noteId);
}
}
}
function downloadNoteRevision(noteId, noteRevisionId) {
const url = getUrlForDownload(`api/notes/${noteId}/revisions/${noteRevisionId}/download`);
@ -76,6 +148,7 @@ export default {
download,
downloadFileNote,
openNoteExternally,
openNoteCustom,
downloadNoteRevision,
getUrlForDownload
}

View File

@ -28,7 +28,8 @@ const TPL = `
<a data-trigger-command="renderActiveNote" class="dropdown-item render-note-button"><kbd data-command="renderActiveNote"></kbd> Re-render note</a>
<a data-trigger-command="findInText" class="dropdown-item find-in-text-button">Search in note <kbd data-command="findInText"></a>
<a data-trigger-command="showNoteSource" class="dropdown-item show-source-button"><kbd data-command="showNoteSource"></kbd> Note source</a>
<a data-trigger-command="openNoteExternally" class="dropdown-item open-note-externally-button"><kbd data-command="openNoteExternally"></kbd> Open note externally11</a>
<a data-trigger-command="openNoteExternally" class="dropdown-item open-note-externally-button"><kbd data-command="openNoteExternally"></kbd> Open note externally</a>
<a data-trigger-command="openNoteCustom" class="dropdown-item open-note-custom-button"><kbd data-command="openNoteCustom"></kbd> Open note custom (beta)</a>
<a class="dropdown-item import-files-button">Import files</a>
<a class="dropdown-item export-note-button">Export note</a>
<a class="dropdown-item delete-note-button">Delete note</a>
@ -67,6 +68,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget {
this.$widget.on('click', '.dropdown-item', () => this.$widget.find("[data-toggle='dropdown']").dropdown('toggle'));
this.$openNoteExternallyButton = this.$widget.find(".open-note-externally-button");
this.$openNoteCustomButton = this.$widget.find(".open-note-custom-button");
this.$deleteNoteButton = this.$widget.find(".delete-note-button");
this.$deleteNoteButton.on("click", () => {
@ -88,6 +90,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget {
this.$renderNoteButton.toggle(note.type === 'render');
this.$openNoteExternallyButton.toggle(utils.isElectron());
this.$openNoteCustomButton.toggle(utils.isElectron());
}
toggleDisabled($el, enable) {