Notes/src/public/app/widgets/ribbon_widgets/script_executor.ts

77 lines
2.4 KiB
TypeScript
Raw Normal View History

import NoteContextAwareWidget from "../note_context_aware_widget.js";
import keyboardActionService from "../../services/keyboard_actions.js";
import { t } from "../../services/i18n.js";
2025-03-16 17:31:28 +02:00
import type FNote from "../../entities/fnote.js";
const TPL = `
<div class="script-runner-widget">
<style>
.script-runner-widget {
padding: 12px;
color: var(--muted-text-color);
}
2022-12-22 14:57:00 +01:00
.execute-description {
margin-bottom: 10px;
}
</style>
<div class="execute-description"></div>
2025-03-16 17:31:28 +02:00
<div style="display: flex; justify-content: space-around">
<button data-trigger-command="runActiveNote" class="execute-button btn btn-sm"></button>
</div>
</div>`;
export default class ScriptExecutorWidget extends NoteContextAwareWidget {
2025-03-16 17:31:28 +02:00
private $executeButton!: JQuery<HTMLElement>;
private $executeDescription!: JQuery<HTMLElement>;
isEnabled() {
2025-01-09 18:07:02 +02:00
return (
super.isEnabled() &&
this.note &&
(this.note.mime.startsWith("application/javascript") || this.isTriliumSqlite()) &&
(this.note.hasLabel("executeDescription") || this.note.hasLabel("executeButton"))
);
}
isTriliumSqlite() {
2025-03-16 17:31:28 +02:00
return this.note?.mime === "text/x-sqlite;schema=trilium";
}
getTitle() {
return {
show: this.isEnabled(),
activate: true,
2025-01-09 18:07:02 +02:00
title: this.isTriliumSqlite() ? t("script_executor.query") : t("script_executor.script"),
icon: "bx bx-run"
};
}
doRender() {
this.$widget = $(TPL);
this.contentSized();
2025-01-09 18:07:02 +02:00
this.$executeButton = this.$widget.find(".execute-button");
this.$executeDescription = this.$widget.find(".execute-description");
}
2025-03-16 17:31:28 +02:00
async refreshWithNote(note: FNote) {
2025-01-09 18:07:02 +02:00
const executeTitle = note.getLabelValue("executeButton") || (this.isTriliumSqlite() ? t("script_executor.execute_query") : t("script_executor.execute_script"));
this.$executeButton.text(executeTitle);
2025-01-09 18:07:02 +02:00
this.$executeButton.attr("title", executeTitle);
2022-12-22 14:57:00 +01:00
keyboardActionService.updateDisplayedShortcuts(this.$widget);
2025-01-09 18:07:02 +02:00
const executeDescription = note.getLabelValue("executeDescription");
if (executeDescription) {
this.$executeDescription.show().html(executeDescription);
} else {
this.$executeDescription.empty().hide();
}
}
}