mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-11 19:22:31 +08:00
chore(client/ts): port note_type
This commit is contained in:
parent
6706332be3
commit
50d37bbcb1
@ -3,8 +3,19 @@ import mimeTypesService from "../services/mime_types.js";
|
|||||||
import NoteContextAwareWidget from "./note_context_aware_widget.js";
|
import NoteContextAwareWidget from "./note_context_aware_widget.js";
|
||||||
import dialogService from "../services/dialog.js";
|
import dialogService from "../services/dialog.js";
|
||||||
import { t } from "../services/i18n.js";
|
import { t } from "../services/i18n.js";
|
||||||
|
import type FNote from "../entities/fnote.js";
|
||||||
|
import type { NoteType } from "../entities/fnote.js";
|
||||||
|
import type { EventData } from "../components/app_context.js";
|
||||||
|
|
||||||
const NOTE_TYPES = [
|
interface NoteTypeMapping {
|
||||||
|
type: NoteType;
|
||||||
|
mime?: string;
|
||||||
|
title: string;
|
||||||
|
isBeta?: boolean;
|
||||||
|
selectable: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const NOTE_TYPES: NoteTypeMapping[] = [
|
||||||
// The suggested note type ordering method: insert the item into the corresponding group,
|
// The suggested note type ordering method: insert the item into the corresponding group,
|
||||||
// then ensure the items within the group are ordered alphabetically.
|
// then ensure the items within the group are ordered alphabetically.
|
||||||
|
|
||||||
@ -67,9 +78,16 @@ const TPL = `
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
export default class NoteTypeWidget extends NoteContextAwareWidget {
|
export default class NoteTypeWidget extends NoteContextAwareWidget {
|
||||||
|
|
||||||
|
private dropdown!: bootstrap.Dropdown;
|
||||||
|
private $noteTypeDropdown!: JQuery<HTMLElement>;
|
||||||
|
private $noteTypeButton!: JQuery<HTMLElement>;
|
||||||
|
private $noteTypeDesc!: JQuery<HTMLElement>;
|
||||||
|
|
||||||
doRender() {
|
doRender() {
|
||||||
this.$widget = $(TPL);
|
this.$widget = $(TPL);
|
||||||
|
|
||||||
|
//@ts-ignore
|
||||||
this.dropdown = bootstrap.Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']"));
|
this.dropdown = bootstrap.Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']"));
|
||||||
|
|
||||||
this.$widget.on("show.bs.dropdown", () => this.renderDropdown());
|
this.$widget.on("show.bs.dropdown", () => this.renderDropdown());
|
||||||
@ -81,7 +99,7 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
this.$widget.on("click", ".dropdown-item", () => this.dropdown.toggle());
|
this.$widget.on("click", ".dropdown-item", () => this.dropdown.toggle());
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshWithNote(note) {
|
async refreshWithNote(note: FNote) {
|
||||||
this.$noteTypeButton.prop("disabled", () => NOT_SELECTABLE_NOTE_TYPES.includes(note.type));
|
this.$noteTypeButton.prop("disabled", () => NOT_SELECTABLE_NOTE_TYPES.includes(note.type));
|
||||||
|
|
||||||
this.$noteTypeDesc.text(await this.findTypeTitle(note.type, note.mime));
|
this.$noteTypeDesc.text(await this.findTypeTitle(note.type, note.mime));
|
||||||
@ -93,8 +111,12 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
async renderDropdown() {
|
async renderDropdown() {
|
||||||
this.$noteTypeDropdown.empty();
|
this.$noteTypeDropdown.empty();
|
||||||
|
|
||||||
|
if (!this.note) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (const noteType of NOTE_TYPES.filter((nt) => nt.selectable)) {
|
for (const noteType of NOTE_TYPES.filter((nt) => nt.selectable)) {
|
||||||
let $typeLink;
|
let $typeLink: JQuery<HTMLElement>;
|
||||||
|
|
||||||
const $title = $("<span>").text(noteType.title);
|
const $title = $("<span>").text(noteType.title);
|
||||||
if (noteType.isBeta) {
|
if (noteType.isBeta) {
|
||||||
@ -110,7 +132,9 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
const type = $typeLink.attr("data-note-type");
|
const type = $typeLink.attr("data-note-type");
|
||||||
const noteType = NOTE_TYPES.find((nt) => nt.type === type);
|
const noteType = NOTE_TYPES.find((nt) => nt.type === type);
|
||||||
|
|
||||||
this.save(noteType.type, noteType.mime);
|
if (noteType) {
|
||||||
|
this.save(noteType.type, noteType.mime);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.$noteTypeDropdown.append('<div class="dropdown-divider"></div>');
|
this.$noteTypeDropdown.append('<div class="dropdown-divider"></div>');
|
||||||
@ -136,7 +160,7 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
.on("click", (e) => {
|
.on("click", (e) => {
|
||||||
const $link = $(e.target).closest(".dropdown-item");
|
const $link = $(e.target).closest(".dropdown-item");
|
||||||
|
|
||||||
this.save("code", $link.attr("data-mime-type"));
|
this.save("code", $link.attr("data-mime-type") ?? "");
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.note.type === "code" && this.note.mime === mimeType.mime) {
|
if (this.note.type === "code" && this.note.mime === mimeType.mime) {
|
||||||
@ -149,7 +173,7 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async findTypeTitle(type, mime) {
|
async findTypeTitle(type: NoteType, mime: string) {
|
||||||
if (type === "code") {
|
if (type === "code") {
|
||||||
const mimeTypes = mimeTypesService.getMimeTypes();
|
const mimeTypes = mimeTypesService.getMimeTypes();
|
||||||
const found = mimeTypes.find((mt) => mt.mime === mime);
|
const found = mimeTypes.find((mt) => mt.mime === mime);
|
||||||
@ -162,12 +186,12 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async save(type, mime) {
|
async save(type: NoteType, mime?: string) {
|
||||||
if (type === this.note.type && mime === this.note.mime) {
|
if (type === this.note?.type && mime === this.note?.mime) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type !== this.note.type && !(await this.confirmChangeIfContent())) {
|
if (type !== this.note?.type && !(await this.confirmChangeIfContent())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,16 +199,20 @@ export default class NoteTypeWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async confirmChangeIfContent() {
|
async confirmChangeIfContent() {
|
||||||
|
if (!this.note) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const blob = await this.note.getBlob();
|
const blob = await this.note.getBlob();
|
||||||
|
|
||||||
if (!blob.content || !blob.content.trim().length) {
|
if (!blob?.content || !blob.content.trim().length) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await dialogService.confirm(t("note_types.confirm-change"));
|
return await dialogService.confirm(t("note_types.confirm-change"));
|
||||||
}
|
}
|
||||||
|
|
||||||
async entitiesReloadedEvent({ loadResults }) {
|
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
|
||||||
if (loadResults.isNoteReloaded(this.noteId)) {
|
if (loadResults.isNoteReloaded(this.noteId)) {
|
||||||
this.refresh();
|
this.refresh();
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user