2022-12-20 23:46:44 +01:00
|
|
|
import NoteContextAwareWidget from "../note_context_aware_widget.js";
|
|
|
|
import keyboardActionService from "../../services/keyboard_actions.js";
|
2024-08-06 10:16:56 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2025-03-16 17:31:28 +02:00
|
|
|
import type FNote from "../../entities/fnote.js";
|
2022-12-20 23:46:44 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-12-20 23:46:44 +01:00
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="execute-description"></div>
|
2025-03-16 17:31:28 +02:00
|
|
|
|
2022-12-20 23:46:44 +01: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>;
|
|
|
|
|
2022-12-20 23:46:44 +01:00
|
|
|
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"))
|
|
|
|
);
|
2022-12-20 23:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isTriliumSqlite() {
|
2025-03-16 17:31:28 +02:00
|
|
|
return this.note?.mime === "text/x-sqlite;schema=trilium";
|
2022-12-20 23:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2022-12-20 23:46:44 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2022-12-20 23:46:44 +01:00
|
|
|
}
|
|
|
|
|
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"));
|
2022-12-20 23:46:44 +01:00
|
|
|
|
|
|
|
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);
|
2022-12-20 23:46:44 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const executeDescription = note.getLabelValue("executeDescription");
|
2022-12-20 23:46:44 +01:00
|
|
|
|
|
|
|
if (executeDescription) {
|
|
|
|
this.$executeDescription.show().html(executeDescription);
|
|
|
|
} else {
|
|
|
|
this.$executeDescription.empty().hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|