Add openAttachmentCustom

This commit is contained in:
siriusXT 2023-09-07 06:33:51 +00:00
parent 40971afe4f
commit 0424728012
3 changed files with 35 additions and 8 deletions

View File

@ -44,8 +44,7 @@ export default class RootCommandExecutor extends Component {
openNoteExternallyCommand() { openNoteExternallyCommand() {
const noteId = appContext.tabManager.getActiveContextNoteId(); const noteId = appContext.tabManager.getActiveContextNoteId();
const mime = appContext.tabManager.getActiveContextNoteMime() const mime = appContext.tabManager.getActiveContextNoteMime();
if (noteId) { if (noteId) {
openService.openNoteExternally(noteId, mime); openService.openNoteExternally(noteId, mime);
} }
@ -53,8 +52,9 @@ export default class RootCommandExecutor extends Component {
openNoteCustomCommand() { openNoteCustomCommand() {
const noteId = appContext.tabManager.getActiveContextNoteId(); const noteId = appContext.tabManager.getActiveContextNoteId();
const mime = appContext.tabManager.getActiveContextNoteMime();
if (noteId) { if (noteId) {
openService.openNoteCustom(noteId); openService.openNoteCustom(noteId, mime);
} }
} }

View File

@ -41,12 +41,13 @@ function downloadAttachment(attachmentId) {
download(url); download(url);
} }
async function openNoteCustom(noteId) { async function openCustom(type, entityId, mime) {
checkType(type);
if (!utils.isElectron() || utils.isMac()) { if (!utils.isElectron() || utils.isMac()) {
return; return;
} }
const resp = await server.post(`notes/${noteId}/save-to-tmp-dir`); const resp = await server.post(`${type}/${entityId}/save-to-tmp-dir`);
let filePath = resp.tmpFilePath; let filePath = resp.tmpFilePath;
const {exec} = utils.dynamicRequire('child_process'); const {exec} = utils.dynamicRequire('child_process');
const platform = process.platform; const platform = process.platform;
@ -71,7 +72,7 @@ async function openNoteCustom(noteId) {
const terminal = terminals[index]; const terminal = terminals[index];
if (!terminal) { if (!terminal) {
console.error('Open Note custom: No terminal found!'); console.error('Open Note custom: No terminal found!');
open(getFileUrl(noteId), {url: true}); open(getFileUrl(entityId), {url: true});
return; return;
} }
exec(`which ${terminal}`, (error, stdout, stderr) => { exec(`which ${terminal}`, (error, stdout, stderr) => {
@ -92,16 +93,20 @@ async function openNoteCustom(noteId) {
exec(command, (err, stdout, stderr) => { exec(command, (err, stdout, stderr) => {
if (err) { if (err) {
console.error("Open Note custom: ", err); console.error("Open Note custom: ", err);
open(getFileUrl(noteId), {url: true}); open(getFileUrl(entityId), {url: true});
return; return;
} }
}); });
} else { } else {
console.log('Currently "Open Note custom" only supports linux and windows systems'); console.log('Currently "Open Note custom" only supports linux and windows systems');
open(getFileUrl(noteId), {url: true}); open(getFileUrl(entityId), {url: true});
} }
} }
const openNoteCustom = async (noteId, mime) => await openCustom('notes', noteId, mime);
const openAttachmentCustom = async (attachmentId, mime) => await openCustom('attachments', attachmentId, mime);
function downloadRevision(noteId, revisionId) { function downloadRevision(noteId, revisionId) {
const url = getUrlForDownload(`api/revisions/${revisionId}/download`); const url = getUrlForDownload(`api/revisions/${revisionId}/download`);
@ -170,4 +175,5 @@ export default {
openNoteExternally, openNoteExternally,
openAttachmentExternally, openAttachmentExternally,
openNoteCustom, openNoteCustom,
openAttachmentCustom,
} }

View File

@ -5,6 +5,7 @@ import toastService from "../../services/toast.js";
import ws from "../../services/ws.js"; import ws from "../../services/ws.js";
import appContext from "../../components/app_context.js"; import appContext from "../../components/app_context.js";
import openService from "../../services/open.js"; import openService from "../../services/open.js";
import utils from "../../services/utils.js";
const TPL = ` const TPL = `
<div class="dropdown attachment-actions"> <div class="dropdown attachment-actions">
@ -32,6 +33,8 @@ const TPL = `
<div class="dropdown-menu dropdown-menu-right"> <div class="dropdown-menu dropdown-menu-right">
<a data-trigger-command="openAttachment" class="dropdown-item" <a data-trigger-command="openAttachment" class="dropdown-item"
title="File will be open in an external application and watched for changes. You'll then be able to upload the modified version back to Trilium.">Open externally</a> title="File will be open in an external application and watched for changes. You'll then be able to upload the modified version back to Trilium.">Open externally</a>
<a data-trigger-command="openAttachmentCustom" class="dropdown-item"
title="File will be open in an external application and watched for changes. You'll then be able to upload the modified version back to Trilium.">Open custom</a>
<a data-trigger-command="downloadAttachment" class="dropdown-item">Download</a> <a data-trigger-command="downloadAttachment" class="dropdown-item">Download</a>
<a data-trigger-command="renameAttachment" class="dropdown-item">Rename attachment</a> <a data-trigger-command="renameAttachment" class="dropdown-item">Rename attachment</a>
<a data-trigger-command="uploadNewAttachmentRevision" class="dropdown-item">Upload new revision</a> <a data-trigger-command="uploadNewAttachmentRevision" class="dropdown-item">Upload new revision</a>
@ -82,6 +85,20 @@ export default class AttachmentActionsWidget extends BasicWidget {
.append($('<span class="disabled-tooltip"> (?)</span>') .append($('<span class="disabled-tooltip"> (?)</span>')
.attr("title", "Opening attachment externally is available only from the detail page, please first click on the attachment detail first and repeat the action.") .attr("title", "Opening attachment externally is available only from the detail page, please first click on the attachment detail first and repeat the action.")
); );
const $openAttachmentCustomButton = this.$widget.find("[data-trigger-command='openAttachmentCustom']");
$openAttachmentCustomButton
.addClass("disabled")
.append($('<span class="disabled-tooltip"> (?)</span>')
.attr("title", "Opening attachment externally is available only from the detail page, please first click on the attachment detail first and repeat the action.")
);
}
if (!utils.isElectron()){
const $openAttachmentCustomButton = this.$widget.find("[data-trigger-command='openAttachmentCustom']");
$openAttachmentCustomButton
.addClass("disabled")
.append($('<span class="disabled-tooltip"> (?)</span>')
.attr("title", "Custom opening of attachments can only be done from the client.")
);
} }
} }
@ -89,6 +106,10 @@ export default class AttachmentActionsWidget extends BasicWidget {
await openService.openAttachmentExternally(this.attachmentId, this.attachment.mime); await openService.openAttachmentExternally(this.attachmentId, this.attachment.mime);
} }
async openAttachmentCustomCommand() {
await openService.openAttachmentCustom(this.attachmentId, this.attachment.mime);
}
async downloadAttachmentCommand() { async downloadAttachmentCommand() {
await openService.downloadAttachment(this.attachmentId); await openService.downloadAttachment(this.attachmentId);
} }