2022-09-04 23:09:42 +02:00
|
|
|
import server from "../../services/server.js";
|
|
|
|
import ws from "../../services/ws.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../../components/app_context.js";
|
2022-09-04 23:09:42 +02:00
|
|
|
import toastService from "../../services/toast.js";
|
|
|
|
import treeService from "../../services/tree.js";
|
|
|
|
import NoteContextAwareWidget from "../note_context_aware_widget.js";
|
|
|
|
import keyboardActionService from "../../services/keyboard_actions.js";
|
|
|
|
|
|
|
|
const TPL = `
|
2022-12-20 23:20:59 +01:00
|
|
|
<div class="code-buttons-widget">
|
2022-09-04 23:09:42 +02:00
|
|
|
<style>
|
2022-12-20 23:20:59 +01:00
|
|
|
.code-buttons-widget {
|
|
|
|
display: flex;
|
|
|
|
gap: 10px;
|
|
|
|
}
|
2022-09-04 23:09:42 +02:00
|
|
|
</style>
|
2022-12-20 23:20:59 +01:00
|
|
|
|
|
|
|
<button data-trigger-command="runActiveNote" class="execute-button floating-button btn" title="Execute script">
|
2022-09-04 23:09:42 +02:00
|
|
|
<span class="bx bx-run"></span>
|
|
|
|
</button>
|
|
|
|
|
2022-12-20 23:20:59 +01:00
|
|
|
<button class="trilium-api-docs-button floating-button btn" title="Open Trilium API docs">
|
2022-09-04 23:09:42 +02:00
|
|
|
<span class="bx bx-help-circle"></span>
|
|
|
|
</button>
|
|
|
|
|
2022-12-20 23:20:59 +01:00
|
|
|
<button class="save-to-note-button floating-button btn">
|
2022-09-04 23:09:42 +02:00
|
|
|
<span class="bx bx-save"></span>
|
|
|
|
</button>
|
|
|
|
</div>`;
|
|
|
|
|
2022-12-20 23:20:59 +01:00
|
|
|
export default class CodeButtonsWidget extends NoteContextAwareWidget {
|
2022-09-04 23:09:42 +02:00
|
|
|
isEnabled() {
|
|
|
|
return super.isEnabled()
|
|
|
|
&& this.note
|
|
|
|
&& (this.note.mime.startsWith('application/javascript') || this.note.mime === 'text/x-sqlite;schema=trilium');
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
|
|
|
this.$openTriliumApiDocsButton = this.$widget.find(".trilium-api-docs-button");
|
|
|
|
this.$openTriliumApiDocsButton.on("click", () => {
|
2022-12-20 23:20:59 +01:00
|
|
|
toastService.showMessage("Opening API docs...");
|
|
|
|
|
2022-09-04 23:09:42 +02:00
|
|
|
if (this.note.mime.endsWith("frontend")) {
|
|
|
|
window.open("https://zadam.github.io/trilium/frontend_api/FrontendScriptApi.html", "_blank");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.open("https://zadam.github.io/trilium/backend_api/BackendScriptApi.html", "_blank");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$executeButton = this.$widget.find('.execute-button');
|
|
|
|
this.$saveToNoteButton = this.$widget.find('.save-to-note-button');
|
|
|
|
this.$saveToNoteButton.on('click', async () => {
|
|
|
|
const {notePath} = await server.post("special-notes/save-sql-console", {sqlConsoleNoteId: this.noteId});
|
|
|
|
|
|
|
|
await ws.waitForMaxKnownEntityChangeId();
|
|
|
|
|
|
|
|
await appContext.tabManager.getActiveContext().setNote(notePath);
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
toastService.showMessage(`SQL Console note has been saved into ${await treeService.getNotePathTitle(notePath)}`);
|
2022-09-04 23:09:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
keyboardActionService.updateDisplayedShortcuts(this.$widget);
|
|
|
|
|
2022-12-20 23:20:59 +01:00
|
|
|
this.contentSized();
|
|
|
|
|
2022-09-04 23:09:42 +02:00
|
|
|
super.doRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshWithNote(note) {
|
|
|
|
this.$executeButton.toggle(
|
|
|
|
note.mime.startsWith('application/javascript')
|
|
|
|
|| note.mime === 'text/x-sqlite;schema=trilium'
|
|
|
|
);
|
|
|
|
|
|
|
|
this.$saveToNoteButton.toggle(
|
|
|
|
note.mime === 'text/x-sqlite;schema=trilium'
|
2022-12-21 16:11:00 +01:00
|
|
|
&& !note.getAllNotePaths().find(notePathArr => !notePathArr.includes('_hidden'))
|
2022-09-04 23:09:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
this.$openTriliumApiDocsButton.toggle(note.mime.startsWith('application/javascript;env='));
|
|
|
|
}
|
2022-09-17 23:06:17 +02:00
|
|
|
|
|
|
|
async noteTypeMimeChangedEvent({noteId}) {
|
|
|
|
if (this.isNote(noteId)) {
|
|
|
|
await this.refresh();
|
|
|
|
}
|
|
|
|
}
|
2022-09-04 23:09:42 +02:00
|
|
|
}
|