mirror of
https://github.com/zadam/trilium.git
synced 2025-11-10 15:25:51 +01:00
chore(react/type_widget): bring back focusing after tab switch
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import { NoteType } from "@triliumnext/commons";
|
import { NoteType } from "@triliumnext/commons";
|
||||||
import { useNoteContext } from "./react/hooks"
|
import { useNoteContext, useTriliumEvent } from "./react/hooks"
|
||||||
import FNote from "../entities/fnote";
|
import FNote from "../entities/fnote";
|
||||||
import protected_session_holder from "../services/protected_session_holder";
|
import protected_session_holder from "../services/protected_session_holder";
|
||||||
import { useContext, useEffect, useMemo, useState } from "preact/hooks";
|
import { useEffect, useState } from "preact/hooks";
|
||||||
import NoteContext from "../components/note_context";
|
import NoteContext from "../components/note_context";
|
||||||
import Empty from "./type_widgets/Empty";
|
import Empty from "./type_widgets/Empty";
|
||||||
import { VNode } from "preact";
|
import { VNode } from "preact";
|
||||||
@@ -28,6 +28,7 @@ type ExtendedNoteType = Exclude<NoteType, "launcher" | "text" | "code"> | "empty
|
|||||||
*
|
*
|
||||||
* Apart from that:
|
* Apart from that:
|
||||||
* - It applies a full-height style depending on the content type (e.g. canvas notes).
|
* - It applies a full-height style depending on the content type (e.g. canvas notes).
|
||||||
|
* - Focuses the content when switching tabs.
|
||||||
*/
|
*/
|
||||||
export default function NoteDetail() {
|
export default function NoteDetail() {
|
||||||
const { note, type, noteContext, parentComponent } = useNoteInfo();
|
const { note, type, noteContext, parentComponent } = useNoteInfo();
|
||||||
@@ -43,6 +44,14 @@ export default function NoteDetail() {
|
|||||||
};
|
};
|
||||||
useEffect(() => setCorrespondingWidget(getCorrespondingWidget(type, props)), [ note, viewScope, type ]);
|
useEffect(() => setCorrespondingWidget(getCorrespondingWidget(type, props)), [ note, viewScope, type ]);
|
||||||
|
|
||||||
|
// Automatically focus the editor.
|
||||||
|
useTriliumEvent("activeNoteChanged", () => {
|
||||||
|
// Restore focus to the editor when switching tabs, but only if the note tree is not already focused.
|
||||||
|
if (!document.activeElement?.classList.contains("fancytree-title")) {
|
||||||
|
parentComponent.triggerCommand("focusOnDetail", { ntxId });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={`note-detail ${isFullHeight ? "full-height" : ""}`}>
|
<div class={`note-detail ${isFullHeight ? "full-height" : ""}`}>
|
||||||
{correspondingWidget || <p>Note detail goes here! {note?.title} of {type}</p>}
|
{correspondingWidget || <p>Note detail goes here! {note?.title} of {type}</p>}
|
||||||
|
|||||||
@@ -138,17 +138,6 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
|
|||||||
return this.typeWidgets[this.type];
|
return this.typeWidgets[this.type];
|
||||||
}
|
}
|
||||||
|
|
||||||
async focusOnDetailEvent({ ntxId }: EventData<"focusOnDetail">) {
|
|
||||||
if (this.noteContext?.ntxId !== ntxId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.refresh();
|
|
||||||
const widget = this.getTypeWidget();
|
|
||||||
await widget.initialized;
|
|
||||||
widget.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) {
|
async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) {
|
||||||
if (this.isNoteContext(noteContext.ntxId)) {
|
if (this.isNoteContext(noteContext.ntxId)) {
|
||||||
await this.spacedUpdate.updateNowIfNecessary();
|
await this.spacedUpdate.updateNowIfNecessary();
|
||||||
|
|||||||
@@ -138,6 +138,12 @@ function CodeEditor({ note, parentComponent, ntxId, containerRef: externalContai
|
|||||||
editor.focus();
|
editor.focus();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useTriliumEvent("focusOnDetail", ({ ntxId: eventNtxId }) => {
|
||||||
|
console.log("Focus on ", ntxId, eventNtxId)
|
||||||
|
if (eventNtxId !== ntxId) return;
|
||||||
|
codeEditorRef.current?.focus();
|
||||||
|
});
|
||||||
|
|
||||||
return <CodeMirror
|
return <CodeMirror
|
||||||
{...editorProps}
|
{...editorProps}
|
||||||
editorRef={codeEditorRef}
|
editorRef={codeEditorRef}
|
||||||
|
|||||||
@@ -69,17 +69,6 @@ export default abstract class TypeWidget extends NoteContextAwareWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
activeNoteChangedEvent() {
|
|
||||||
if (!this.isActiveNoteContext()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restore focus to the editor when switching tabs, but only if the note tree is not already focused.
|
|
||||||
if (!document.activeElement?.classList.contains("fancytree-title")) {
|
|
||||||
this.focus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user