feat(client/ts): port note_title

This commit is contained in:
Elian Doran 2025-01-28 14:07:56 +02:00
parent 55ce673f1b
commit 15c63f52dc
No known key found for this signature in database
4 changed files with 27 additions and 17 deletions

View File

@ -71,7 +71,7 @@ export interface ExecuteCommandData extends CommandData {
export type CommandMappings = {
"api-log-messages": CommandData;
focusTree: CommandData,
focusOnDetail: Required<CommandData>;
focusOnDetail: CommandData;
focusOnSearchDefinition: Required<CommandData>;
searchNotes: CommandData & {
searchString?: string;
@ -238,6 +238,9 @@ type EventMappings = {
beforeNoteSwitch: {
noteContext: NoteContext;
};
beforeNoteContextRemove: {
ntxIds: string[];
};
noteSwitched: {
noteContext: NoteContext;
notePath: string | null;

View File

@ -158,7 +158,7 @@ export default class LoadResults {
return Object.keys(this.noteIdToComponentId);
}
isNoteReloaded(noteId: string, componentId = null) {
isNoteReloaded(noteId: string | undefined, componentId: string | null = null) {
if (!noteId) {
return false;
}

View File

@ -10,9 +10,9 @@ import type NoteContext from "../components/note_context.js";
class NoteContextAwareWidget extends BasicWidget {
protected noteContext?: NoteContext;
isNoteContext(ntxId: string | null | undefined) {
isNoteContext(ntxId: string | string[] | null | undefined) {
if (Array.isArray(ntxId)) {
return this.noteContext && ntxId.includes(this.noteContext.ntxId);
return this.noteContext && this.noteContext.ntxId && ntxId.includes(this.noteContext.ntxId);
} else {
return this.noteContext && this.noteContext.ntxId === ntxId;
}

View File

@ -3,10 +3,11 @@ import NoteContextAwareWidget from "./note_context_aware_widget.js";
import protectedSessionHolder from "../services/protected_session_holder.js";
import server from "../services/server.js";
import SpacedUpdate from "../services/spaced_update.js";
import appContext from "../components/app_context.js";
import appContext, { type EventData } from "../components/app_context.js";
import branchService from "../services/branches.js";
import shortcutService from "../services/shortcuts.js";
import utils from "../services/utils.js";
import type FNote from "../entities/fnote.js";
const TPL = `
<div class="note-title-widget">
@ -33,13 +34,20 @@ const TPL = `
</div>`;
export default class NoteTitleWidget extends NoteContextAwareWidget {
private $noteTitle!: JQuery<HTMLElement>;
private deleteNoteOnEscape: boolean;
private spacedUpdate: SpacedUpdate;
constructor() {
super();
this.spacedUpdate = new SpacedUpdate(async () => {
const title = this.$noteTitle.val();
protectedSessionHolder.touchProtectedSessionIfNecessary(this.note);
if (this.note) {
protectedSessionHolder.touchProtectedSessionIfNecessary(this.note);
}
await server.put(`notes/${this.noteId}/title`, { title }, this.componentId);
});
@ -62,37 +70,36 @@ export default class NoteTitleWidget extends NoteContextAwareWidget {
});
shortcutService.bindElShortcut(this.$noteTitle, "esc", () => {
if (this.deleteNoteOnEscape && this.noteContext.isActive()) {
if (this.deleteNoteOnEscape && this.noteContext?.isActive() && this.noteContext?.note) {
branchService.deleteNotes(Object.values(this.noteContext.note.parentToBranch));
}
});
shortcutService.bindElShortcut(this.$noteTitle, "return", () => {
this.triggerCommand("focusOnDetail", { ntxId: this.noteContext.ntxId });
this.triggerCommand("focusOnDetail", { ntxId: this.noteContext?.ntxId });
});
}
async refreshWithNote(note) {
const isReadOnly = (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) || utils.isLaunchBarConfig(note.noteId) || this.noteContext.viewScope.viewMode !== "default";
async refreshWithNote(note: FNote) {
const isReadOnly = (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) || utils.isLaunchBarConfig(note.noteId) || this.noteContext?.viewScope?.viewMode !== "default";
this.$noteTitle.val(isReadOnly ? await this.noteContext.getNavigationTitle() : note.title);
this.$noteTitle.val(isReadOnly ? await this.noteContext?.getNavigationTitle() || "" : note.title);
this.$noteTitle.prop("readonly", isReadOnly);
this.setProtectedStatus(note);
}
/** @param {FNote} note */
setProtectedStatus(note) {
setProtectedStatus(note: FNote) {
this.$noteTitle.toggleClass("protected", !!note.isProtected);
}
async beforeNoteSwitchEvent({ noteContext }) {
async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) {
if (this.isNoteContext(noteContext.ntxId)) {
await this.spacedUpdate.updateNowIfNecessary();
}
}
async beforeNoteContextRemoveEvent({ ntxIds }) {
async beforeNoteContextRemoveEvent({ ntxIds }: EventData<"beforeNoteContextRemove">) {
if (this.isNoteContext(ntxIds)) {
await this.spacedUpdate.updateNowIfNecessary();
}
@ -112,8 +119,8 @@ export default class NoteTitleWidget extends NoteContextAwareWidget {
}
}
entitiesReloadedEvent({ loadResults }) {
if (loadResults.isNoteReloaded(this.noteId)) {
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (loadResults.isNoteReloaded(this.noteId) && this.note) {
// not updating the title specifically since the synced title might be older than what the user is currently typing
this.setProtectedStatus(this.note);
}