mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-08 09:12:29 +08:00
feat(client/ts): port note_title
This commit is contained in:
parent
55ce673f1b
commit
15c63f52dc
@ -71,7 +71,7 @@ export interface ExecuteCommandData extends CommandData {
|
|||||||
export type CommandMappings = {
|
export type CommandMappings = {
|
||||||
"api-log-messages": CommandData;
|
"api-log-messages": CommandData;
|
||||||
focusTree: CommandData,
|
focusTree: CommandData,
|
||||||
focusOnDetail: Required<CommandData>;
|
focusOnDetail: CommandData;
|
||||||
focusOnSearchDefinition: Required<CommandData>;
|
focusOnSearchDefinition: Required<CommandData>;
|
||||||
searchNotes: CommandData & {
|
searchNotes: CommandData & {
|
||||||
searchString?: string;
|
searchString?: string;
|
||||||
@ -238,6 +238,9 @@ type EventMappings = {
|
|||||||
beforeNoteSwitch: {
|
beforeNoteSwitch: {
|
||||||
noteContext: NoteContext;
|
noteContext: NoteContext;
|
||||||
};
|
};
|
||||||
|
beforeNoteContextRemove: {
|
||||||
|
ntxIds: string[];
|
||||||
|
};
|
||||||
noteSwitched: {
|
noteSwitched: {
|
||||||
noteContext: NoteContext;
|
noteContext: NoteContext;
|
||||||
notePath: string | null;
|
notePath: string | null;
|
||||||
|
@ -158,7 +158,7 @@ export default class LoadResults {
|
|||||||
return Object.keys(this.noteIdToComponentId);
|
return Object.keys(this.noteIdToComponentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
isNoteReloaded(noteId: string, componentId = null) {
|
isNoteReloaded(noteId: string | undefined, componentId: string | null = null) {
|
||||||
if (!noteId) {
|
if (!noteId) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,9 @@ import type NoteContext from "../components/note_context.js";
|
|||||||
class NoteContextAwareWidget extends BasicWidget {
|
class NoteContextAwareWidget extends BasicWidget {
|
||||||
protected noteContext?: NoteContext;
|
protected noteContext?: NoteContext;
|
||||||
|
|
||||||
isNoteContext(ntxId: string | null | undefined) {
|
isNoteContext(ntxId: string | string[] | null | undefined) {
|
||||||
if (Array.isArray(ntxId)) {
|
if (Array.isArray(ntxId)) {
|
||||||
return this.noteContext && ntxId.includes(this.noteContext.ntxId);
|
return this.noteContext && this.noteContext.ntxId && ntxId.includes(this.noteContext.ntxId);
|
||||||
} else {
|
} else {
|
||||||
return this.noteContext && this.noteContext.ntxId === ntxId;
|
return this.noteContext && this.noteContext.ntxId === ntxId;
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,11 @@ import NoteContextAwareWidget from "./note_context_aware_widget.js";
|
|||||||
import protectedSessionHolder from "../services/protected_session_holder.js";
|
import protectedSessionHolder from "../services/protected_session_holder.js";
|
||||||
import server from "../services/server.js";
|
import server from "../services/server.js";
|
||||||
import SpacedUpdate from "../services/spaced_update.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 branchService from "../services/branches.js";
|
||||||
import shortcutService from "../services/shortcuts.js";
|
import shortcutService from "../services/shortcuts.js";
|
||||||
import utils from "../services/utils.js";
|
import utils from "../services/utils.js";
|
||||||
|
import type FNote from "../entities/fnote.js";
|
||||||
|
|
||||||
const TPL = `
|
const TPL = `
|
||||||
<div class="note-title-widget">
|
<div class="note-title-widget">
|
||||||
@ -33,13 +34,20 @@ const TPL = `
|
|||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
export default class NoteTitleWidget extends NoteContextAwareWidget {
|
export default class NoteTitleWidget extends NoteContextAwareWidget {
|
||||||
|
|
||||||
|
private $noteTitle!: JQuery<HTMLElement>;
|
||||||
|
private deleteNoteOnEscape: boolean;
|
||||||
|
private spacedUpdate: SpacedUpdate;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.spacedUpdate = new SpacedUpdate(async () => {
|
this.spacedUpdate = new SpacedUpdate(async () => {
|
||||||
const title = this.$noteTitle.val();
|
const title = this.$noteTitle.val();
|
||||||
|
|
||||||
|
if (this.note) {
|
||||||
protectedSessionHolder.touchProtectedSessionIfNecessary(this.note);
|
protectedSessionHolder.touchProtectedSessionIfNecessary(this.note);
|
||||||
|
}
|
||||||
|
|
||||||
await server.put(`notes/${this.noteId}/title`, { title }, this.componentId);
|
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", () => {
|
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));
|
branchService.deleteNotes(Object.values(this.noteContext.note.parentToBranch));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
shortcutService.bindElShortcut(this.$noteTitle, "return", () => {
|
shortcutService.bindElShortcut(this.$noteTitle, "return", () => {
|
||||||
this.triggerCommand("focusOnDetail", { ntxId: this.noteContext.ntxId });
|
this.triggerCommand("focusOnDetail", { ntxId: this.noteContext?.ntxId });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshWithNote(note) {
|
async refreshWithNote(note: FNote) {
|
||||||
const isReadOnly = (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) || utils.isLaunchBarConfig(note.noteId) || this.noteContext.viewScope.viewMode !== "default";
|
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.$noteTitle.prop("readonly", isReadOnly);
|
||||||
|
|
||||||
this.setProtectedStatus(note);
|
this.setProtectedStatus(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param {FNote} note */
|
setProtectedStatus(note: FNote) {
|
||||||
setProtectedStatus(note) {
|
|
||||||
this.$noteTitle.toggleClass("protected", !!note.isProtected);
|
this.$noteTitle.toggleClass("protected", !!note.isProtected);
|
||||||
}
|
}
|
||||||
|
|
||||||
async beforeNoteSwitchEvent({ noteContext }) {
|
async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) {
|
||||||
if (this.isNoteContext(noteContext.ntxId)) {
|
if (this.isNoteContext(noteContext.ntxId)) {
|
||||||
await this.spacedUpdate.updateNowIfNecessary();
|
await this.spacedUpdate.updateNowIfNecessary();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async beforeNoteContextRemoveEvent({ ntxIds }) {
|
async beforeNoteContextRemoveEvent({ ntxIds }: EventData<"beforeNoteContextRemove">) {
|
||||||
if (this.isNoteContext(ntxIds)) {
|
if (this.isNoteContext(ntxIds)) {
|
||||||
await this.spacedUpdate.updateNowIfNecessary();
|
await this.spacedUpdate.updateNowIfNecessary();
|
||||||
}
|
}
|
||||||
@ -112,8 +119,8 @@ export default class NoteTitleWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entitiesReloadedEvent({ loadResults }) {
|
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
|
||||||
if (loadResults.isNoteReloaded(this.noteId)) {
|
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
|
// not updating the title specifically since the synced title might be older than what the user is currently typing
|
||||||
this.setProtectedStatus(this.note);
|
this.setProtectedStatus(this.note);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user