2025-08-21 20:56:37 +03:00
|
|
|
import { useCallback, useMemo } from "preact/hooks";
|
2025-08-21 20:16:06 +03:00
|
|
|
import Dropdown from "../react/Dropdown";
|
|
|
|
|
import { NOTE_TYPES } from "../../services/note_types";
|
2025-08-21 20:30:12 +03:00
|
|
|
import { FormDivider, FormListBadge, FormListItem } from "../react/FormList";
|
2025-08-21 20:16:06 +03:00
|
|
|
import { t } from "../../services/i18n";
|
2025-08-21 20:38:19 +03:00
|
|
|
import { useNoteContext, useNoteProperty, useTriliumOption } from "../react/hooks";
|
2025-08-21 20:30:12 +03:00
|
|
|
import mime_types from "../../services/mime_types";
|
2025-08-21 20:38:19 +03:00
|
|
|
import { NoteType } from "@triliumnext/commons";
|
2025-08-21 20:56:37 +03:00
|
|
|
import server from "../../services/server";
|
|
|
|
|
import dialog from "../../services/dialog";
|
2025-08-21 21:24:01 +03:00
|
|
|
import FormToggle from "../react/FormToggle";
|
|
|
|
|
import FNote from "../../entities/fnote";
|
|
|
|
|
import protected_session from "../../services/protected_session";
|
2025-08-21 20:16:06 +03:00
|
|
|
|
2025-08-21 19:27:18 +03:00
|
|
|
export default function BasicPropertiesTab() {
|
2025-08-21 21:24:01 +03:00
|
|
|
const { note } = useNoteContext();
|
|
|
|
|
|
2025-08-21 20:16:06 +03:00
|
|
|
return (
|
2025-08-21 21:24:01 +03:00
|
|
|
<div className="basic-properties-widget">
|
|
|
|
|
<NoteTypeWidget note={note} />
|
|
|
|
|
<ProtectedNoteSwitch note={note} />
|
2025-08-21 20:16:06 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 21:24:01 +03:00
|
|
|
function NoteTypeWidget({ note }: { note?: FNote | null }) {
|
2025-08-21 20:16:06 +03:00
|
|
|
const noteTypes = useMemo(() => NOTE_TYPES.filter((nt) => !nt.reserved && !nt.static), []);
|
2025-08-21 20:30:12 +03:00
|
|
|
const [ codeNotesMimeTypes ] = useTriliumOption("codeNotesMimeTypes");
|
|
|
|
|
const mimeTypes = useMemo(() => mime_types.getMimeTypes().filter(mimeType => mimeType.enabled), [ codeNotesMimeTypes ]);
|
2025-08-21 21:01:57 +03:00
|
|
|
const notSelectableNoteTypes = useMemo(() => NOTE_TYPES.filter((nt) => nt.reserved || nt.static).map((nt) => nt.type), []);
|
2025-08-21 20:38:19 +03:00
|
|
|
|
2025-08-21 20:56:37 +03:00
|
|
|
const currentNoteType = useNoteProperty(note, "type") ?? undefined;
|
|
|
|
|
const currentNoteMime = useNoteProperty(note, "mime");
|
|
|
|
|
|
|
|
|
|
const changeNoteType = useCallback(async (type: NoteType, mime?: string) => {
|
|
|
|
|
if (!note || (type === currentNoteType && mime === currentNoteMime)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Confirm change if the note already has a content.
|
|
|
|
|
if (type !== currentNoteType) {
|
|
|
|
|
const blob = await note.getBlob();
|
|
|
|
|
|
|
|
|
|
if (blob?.content && blob.content.trim().length &&
|
|
|
|
|
!await (dialog.confirm(t("note_types.confirm-change")))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await server.put(`notes/${note.noteId}/type`, { type, mime });
|
|
|
|
|
}, [ note, currentNoteType, currentNoteMime ]);
|
2025-08-21 20:16:06 +03:00
|
|
|
|
|
|
|
|
return (
|
2025-08-21 20:56:37 +03:00
|
|
|
<div className="note-type-container">
|
2025-08-21 20:38:19 +03:00
|
|
|
<span>{t("basic_properties.note_type")}:</span>
|
|
|
|
|
<Dropdown
|
|
|
|
|
dropdownContainerClassName="note-type-dropdown"
|
2025-08-21 20:56:37 +03:00
|
|
|
text={<span className="note-type-desc">{findTypeTitle(currentNoteType, currentNoteMime)}</span>}
|
2025-08-21 21:01:57 +03:00
|
|
|
disabled={notSelectableNoteTypes.includes(currentNoteType ?? "text")}
|
2025-08-21 20:38:19 +03:00
|
|
|
>
|
2025-08-21 20:56:37 +03:00
|
|
|
{noteTypes.map(({ isNew, isBeta, type, mime, title }) => {
|
2025-08-21 20:38:19 +03:00
|
|
|
const badges: FormListBadge[] = [];
|
2025-08-21 20:56:37 +03:00
|
|
|
if (isNew) {
|
2025-08-21 20:38:19 +03:00
|
|
|
badges.push({
|
|
|
|
|
className: "new-note-type-badge",
|
|
|
|
|
text: t("note_types.new-feature")
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-21 20:56:37 +03:00
|
|
|
if (isBeta) {
|
2025-08-21 20:38:19 +03:00
|
|
|
badges.push({
|
|
|
|
|
text: t("note_types.beta-feature")
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 20:56:37 +03:00
|
|
|
const checked = (type === currentNoteType);
|
|
|
|
|
if (type !== "code") {
|
2025-08-21 20:38:19 +03:00
|
|
|
return (
|
|
|
|
|
<FormListItem
|
2025-08-21 20:56:37 +03:00
|
|
|
checked={checked}
|
2025-08-21 20:38:19 +03:00
|
|
|
badges={badges}
|
2025-08-21 20:56:37 +03:00
|
|
|
onClick={() => changeNoteType(type, mime)}
|
|
|
|
|
>{title}</FormListItem>
|
2025-08-21 20:38:19 +03:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<FormDivider />
|
2025-08-21 20:56:37 +03:00
|
|
|
<FormListItem
|
|
|
|
|
checked={checked}
|
|
|
|
|
disabled
|
|
|
|
|
>
|
|
|
|
|
<strong>{title}</strong>
|
2025-08-21 20:38:19 +03:00
|
|
|
</FormListItem>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})}
|
|
|
|
|
|
2025-08-21 20:56:37 +03:00
|
|
|
{mimeTypes.map(({ title, mime }) => (
|
|
|
|
|
<FormListItem onClick={() => changeNoteType("code", mime)}>
|
|
|
|
|
{title}
|
|
|
|
|
</FormListItem>
|
2025-08-21 20:38:19 +03:00
|
|
|
))}
|
|
|
|
|
</Dropdown>
|
2025-08-21 20:56:37 +03:00
|
|
|
</div>
|
2025-08-21 20:16:06 +03:00
|
|
|
)
|
2025-08-21 20:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 21:24:01 +03:00
|
|
|
function ProtectedNoteSwitch({ note }: { note?: FNote | null }) {
|
|
|
|
|
const isProtected = useNoteProperty(note, "isProtected");
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="protected-note-switch-container">
|
|
|
|
|
<FormToggle
|
|
|
|
|
currentValue={isProtected}
|
|
|
|
|
onChange={(shouldProtect) => note && protected_session.protectNote(note.noteId, shouldProtect, false)}
|
|
|
|
|
switchOnName={t("protect_note.toggle-on")} switchOnTooltip={t("protect_note.toggle-on-hint")}
|
|
|
|
|
switchOffName={t("protect_note.toggle-off")} switchOffTooltip={t("protect_note.toggle-off-hint")}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 21:01:57 +03:00
|
|
|
function findTypeTitle(type?: NoteType, mime?: string | null) {
|
2025-08-21 20:38:19 +03:00
|
|
|
if (type === "code") {
|
|
|
|
|
const mimeTypes = mime_types.getMimeTypes();
|
|
|
|
|
const found = mimeTypes.find((mt) => mt.mime === mime);
|
|
|
|
|
|
|
|
|
|
return found ? found.title : mime;
|
|
|
|
|
} else {
|
|
|
|
|
const noteType = NOTE_TYPES.find((nt) => nt.type === type);
|
|
|
|
|
|
|
|
|
|
return noteType ? noteType.title : type;
|
|
|
|
|
}
|
2025-08-21 19:27:18 +03:00
|
|
|
}
|