Notes/src/public/app/widgets/api_log.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-08-15 11:33:44 +08:00
import { t } from "../services/i18n.js";
2022-09-17 23:06:17 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
const TPL = `
<div class="api-log-widget">
<style>
.api-log-widget {
padding: 15px;
flex-grow: 1;
max-height: 40%;
2022-09-18 08:48:01 +02:00
position: relative;
2022-09-17 23:06:17 +02:00
}
.hidden-api-log {
display: none;
}
.api-log-container {
overflow: auto;
height: 100%;
}
2022-09-18 08:48:01 +02:00
.close-api-log-button {
padding: 5px;
border: 1px solid var(--button-border-color);
background-color: var(--button-background-color);
border-radius: var(--button-border-radius);
2022-09-18 08:48:01 +02:00
color: var(--button-text-color);
position: absolute;
top: 10px;
right: 40px;
cursor: pointer;
}
2022-09-17 23:06:17 +02:00
</style>
2022-09-18 08:48:01 +02:00
2025-01-09 18:07:02 +02:00
<div class="bx bx-x close-api-log-button" title="${t("api_log.close")}"></div>
2022-09-17 23:06:17 +02:00
<div class="api-log-container"></div>
</div>`;
export default class ApiLogWidget extends NoteContextAwareWidget {
isEnabled() {
2025-01-09 18:07:02 +02:00
return this.note && this.note.mime.startsWith("application/javascript;env=") && super.isEnabled();
2022-09-17 23:06:17 +02:00
}
doRender() {
this.$widget = $(TPL);
2022-09-18 08:48:01 +02:00
this.toggle(false);
2022-09-17 23:06:17 +02:00
2025-01-09 18:07:02 +02:00
this.$logContainer = this.$widget.find(".api-log-container");
2022-09-18 08:48:01 +02:00
this.$closeButton = this.$widget.find(".close-api-log-button");
this.$closeButton.on("click", () => this.toggle(false));
2022-09-17 23:06:17 +02:00
}
async refreshWithNote(note) {
this.$logContainer.empty();
}
2025-01-09 18:07:02 +02:00
apiLogMessagesEvent({ messages, noteId }) {
2022-09-17 23:06:17 +02:00
if (!this.isNote(noteId)) {
return;
}
2022-09-18 08:48:01 +02:00
this.toggle(true);
2022-09-17 23:06:17 +02:00
for (const message of messages) {
this.$logContainer.append(message).append($("<br>"));
}
}
2022-09-18 08:48:01 +02:00
toggle(show) {
this.$widget.toggleClass("hidden-api-log", !show);
}
2022-09-17 23:06:17 +02:00
}