mirror of
https://github.com/zadam/trilium.git
synced 2025-10-29 10:39:00 +01:00
chore(react/note_title): delete new notes on escape
This commit is contained in:
parent
8a543d4513
commit
51e8a80ca3
@ -8,7 +8,6 @@ export default class NoteTitleWidget extends NoteContextAwareWidget {
|
|||||||
|
|
||||||
private $noteTitle!: JQuery<HTMLElement>;
|
private $noteTitle!: JQuery<HTMLElement>;
|
||||||
private deleteNoteOnEscape: boolean;
|
private deleteNoteOnEscape: boolean;
|
||||||
private spacedUpdate: SpacedUpdate;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
@ -20,15 +19,7 @@ export default class NoteTitleWidget extends NoteContextAwareWidget {
|
|||||||
this.$widget = $(TPL);
|
this.$widget = $(TPL);
|
||||||
this.$noteTitle = this.$widget.find(".note-title");
|
this.$noteTitle = this.$widget.find(".note-title");
|
||||||
this.$noteTitle.on("blur", () => {
|
this.$noteTitle.on("blur", () => {
|
||||||
this.spacedUpdate.updateNowIfNecessary();
|
|
||||||
|
|
||||||
this.deleteNoteOnEscape = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
shortcutService.bindElShortcut(this.$noteTitle, "esc", () => {
|
|
||||||
if (this.deleteNoteOnEscape && this.noteContext?.isActive() && this.noteContext?.note) {
|
|
||||||
branchService.deleteNotes(Object.values(this.noteContext.note.parentToBranch));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -45,17 +36,4 @@ export default class NoteTitleWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
focusOnTitleEvent() {
|
|
||||||
if (this.noteContext && this.noteContext.isActive()) {
|
|
||||||
this.$noteTitle.trigger("focus");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
focusAndSelectTitleEvent({ isNewNote } = { isNewNote: false }) {
|
|
||||||
if (this.noteContext && this.noteContext.isActive()) {
|
|
||||||
this.$noteTitle.trigger("focus").trigger("select");
|
|
||||||
|
|
||||||
this.deleteNoteOnEscape = isNewNote;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef, useState } from "preact/hooks";
|
||||||
import { t } from "../services/i18n";
|
import { t } from "../services/i18n";
|
||||||
import FormTextBox from "./react/FormTextBox";
|
import FormTextBox from "./react/FormTextBox";
|
||||||
import { useBeforeUnload, useNoteContext, useNoteProperty, useSpacedUpdate } from "./react/hooks";
|
import { useNoteContext, useNoteProperty, useSpacedUpdate, useTriliumEventBeta } from "./react/hooks";
|
||||||
import protected_session_holder from "../services/protected_session_holder";
|
import protected_session_holder from "../services/protected_session_holder";
|
||||||
import server from "../services/server";
|
import server from "../services/server";
|
||||||
import "./note_title.css";
|
import "./note_title.css";
|
||||||
import { isLaunchBarConfig } from "../services/utils";
|
import { isLaunchBarConfig } from "../services/utils";
|
||||||
import appContext from "../components/app_context";
|
import appContext from "../components/app_context";
|
||||||
|
import branches from "../services/branches";
|
||||||
|
|
||||||
export default function NoteTitleWidget() {
|
export default function NoteTitleWidget() {
|
||||||
const { note, noteId, componentId, viewScope, noteContext, parentComponent } = useNoteContext();
|
const { note, noteId, componentId, viewScope, noteContext, parentComponent } = useNoteContext();
|
||||||
@ -17,6 +18,7 @@ export default function NoteTitleWidget() {
|
|||||||
const [ isReadOnly, setReadOnly ] = useState<boolean>(false);
|
const [ isReadOnly, setReadOnly ] = useState<boolean>(false);
|
||||||
const [ navigationTitle, setNavigationTitle ] = useState<string | null>(null);
|
const [ navigationTitle, setNavigationTitle ] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Manage read-only
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const isReadOnly = note === null
|
const isReadOnly = note === null
|
||||||
|| note === undefined
|
|| note === undefined
|
||||||
@ -26,12 +28,14 @@ export default function NoteTitleWidget() {
|
|||||||
setReadOnly(isReadOnly);
|
setReadOnly(isReadOnly);
|
||||||
}, [ note, note?.noteId, note?.isProtected, viewScope?.viewMode ]);
|
}, [ note, note?.noteId, note?.isProtected, viewScope?.viewMode ]);
|
||||||
|
|
||||||
|
// Manage the title for read-only notes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isReadOnly) {
|
if (isReadOnly) {
|
||||||
noteContext?.getNavigationTitle().then(setNavigationTitle);
|
noteContext?.getNavigationTitle().then(setNavigationTitle);
|
||||||
}
|
}
|
||||||
}, [isReadOnly]);
|
}, [isReadOnly]);
|
||||||
|
|
||||||
|
// Save changes to title.
|
||||||
const spacedUpdate = useSpacedUpdate(async () => {
|
const spacedUpdate = useSpacedUpdate(async () => {
|
||||||
if (!note) {
|
if (!note) {
|
||||||
return;
|
return;
|
||||||
@ -40,13 +44,31 @@ export default function NoteTitleWidget() {
|
|||||||
await server.put<void>(`notes/${noteId}/title`, { title: newTitle.current }, componentId);
|
await server.put<void>(`notes/${noteId}/title`, { title: newTitle.current }, componentId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Prevent user from navigating away if the spaced update is not done.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
appContext.addBeforeUnloadListener(() => spacedUpdate.isAllSavedAndTriggerUpdate());
|
appContext.addBeforeUnloadListener(() => spacedUpdate.isAllSavedAndTriggerUpdate());
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Manage focus.
|
||||||
|
const textBoxRef = useRef<HTMLInputElement>(null);
|
||||||
|
const isNewNote = useRef<boolean>();
|
||||||
|
useTriliumEventBeta("focusOnTitle", () => {
|
||||||
|
if (noteContext?.isActive() && textBoxRef.current) {
|
||||||
|
console.log(textBoxRef.current);
|
||||||
|
textBoxRef.current.focus();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
useTriliumEventBeta("focusAndSelectTitle", ({ isNewNote: _isNewNote } ) => {
|
||||||
|
if (noteContext?.isActive() && textBoxRef.current) {
|
||||||
|
textBoxRef.current.focus();
|
||||||
|
isNewNote.current = _isNewNote;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="note-title-widget">
|
<div className="note-title-widget">
|
||||||
{note && <FormTextBox
|
{note && <FormTextBox
|
||||||
|
inputRef={textBoxRef}
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
currentValue={(!isReadOnly ? title : navigationTitle) ?? ""}
|
currentValue={(!isReadOnly ? title : navigationTitle) ?? ""}
|
||||||
placeholder={t("note_title.placeholder")}
|
placeholder={t("note_title.placeholder")}
|
||||||
@ -64,6 +86,14 @@ export default function NoteTitleWidget() {
|
|||||||
parentComponent.triggerCommand("focusOnDetail", { ntxId: noteContext?.ntxId });
|
parentComponent.triggerCommand("focusOnDetail", { ntxId: noteContext?.ntxId });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (e.key === "Escape" && isNewNote.current && noteContext?.isActive() && note) {
|
||||||
|
branches.deleteNotes(Object.values(note.parentToBranch));
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onBlur={() => {
|
||||||
|
spacedUpdate.updateNowIfNecessary();
|
||||||
|
isNewNote.current = false;
|
||||||
}}
|
}}
|
||||||
/>}
|
/>}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -101,7 +101,7 @@ export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000)
|
|||||||
// Update callback ref when it changes
|
// Update callback ref when it changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
callbackRef.current = callback;
|
callbackRef.current = callback;
|
||||||
});
|
}, [callback]);
|
||||||
|
|
||||||
// Create SpacedUpdate instance only once
|
// Create SpacedUpdate instance only once
|
||||||
if (!spacedUpdateRef.current) {
|
if (!spacedUpdateRef.current) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user