Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(ocr): move viewing extracted text outside note actions
Elian Dorane435657deef7
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex be93ffd013..59281c850c 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -307,6 +307,10 @@ export type CommandMappings = {lastTab: CommandData;showNoteSource: CommandData;showNoteOCRText: CommandData;+ showOcrTextDialog: CommandData & {+ textUrl: string;+ processUrl: string;+ };showSQLConsole: CommandData;showBackendLog: CommandData;showCheatsheet: CommandData;diff --git a/apps/client/src/components/root_command_executor.ts b/apps/client/src/components/root_command_executor.tsindex 4560eafce6..1ae82a6923 100644--- a/apps/client/src/components/root_command_executor.ts+++ b/apps/client/src/components/root_command_executor.ts@@ -148,15 +148,12 @@ export default class RootCommandExecutor extends Component {}}- async showNoteOCRTextCommand() {- const notePath = appContext.tabManager.getActiveContextNotePath();-- if (notePath) {- await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {- activate: true,- viewScope: {- viewMode: "ocr"- }+ showNoteOCRTextCommand() {+ const noteId = appContext.tabManager.getActiveContextNoteId();+ if (noteId) {+ appContext.triggerCommand("showOcrTextDialog", {+ textUrl: `ocr/notes/${noteId}/text`,+ processUrl: `ocr/process-note/${noteId}`});}}diff --git a/apps/client/src/layouts/layout_commons.tsx b/apps/client/src/layouts/layout_commons.tsxindex c1d908f5d6..f56377d1c7 100644--- a/apps/client/src/layouts/layout_commons.tsx+++ b/apps/client/src/layouts/layout_commons.tsx@@ -23,6 +23,7 @@ import DeleteNotesDialog from "../widgets/dialogs/delete_notes.js";import InfoDialog from "../widgets/dialogs/info.js";import IncorrectCpuArchDialog from "../widgets/dialogs/incorrect_cpu_arch.js";import CallToActionDialog from "../widgets/dialogs/call_to_action.jsx";+import OcrTextDialog from "../widgets/dialogs/ocr_text.js";import PopupEditorDialog from "../widgets/dialogs/PopupEditor.jsx";import PrintPreviewDialog from "../widgets/dialogs/print_preview.jsx";import ToastContainer from "../widgets/Toast.jsx";@@ -54,5 +55,6 @@ export function applyModals(rootContainer: RootContainer) {.child(<IncorrectCpuArchDialog />).child(<PopupEditorDialog />).child(<CallToActionDialog />)+ .child(<OcrTextDialog />).child(<ToastContainer />);}diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.tsindex 1e375a994a..5938735413 100644--- a/apps/client/src/services/link.ts+++ b/apps/client/src/services/link.ts@@ -30,7 +30,7 @@ async function getLinkIcon(noteId: string, viewMode: ViewMode | undefined) {return icon;}-export type ViewMode = "default" | "source" | "attachments" | "contextual-help" | "note-map" | "ocr";+export type ViewMode = "default" | "source" | "attachments" | "contextual-help" | "note-map";export interface ViewScope {/**diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsxindex 1d7b75b59a..0a6c87303b 100644--- a/apps/client/src/widgets/NoteDetail.tsx+++ b/apps/client/src/widgets/NoteDetail.tsx@@ -325,8 +325,6 @@ export async function getExtendedWidgetType(note: FNote | null | undefined, noteif (noteContext?.viewScope?.viewMode === "source") {resultingType = "readOnlyCode";- } else if (noteContext.viewScope?.viewMode === "ocr") {- resultingType = "readOnlyOCRText";} else if (noteContext.viewScope?.viewMode === "attachments") {resultingType = noteContext.viewScope.attachmentId ? "attachmentDetail" : "attachmentList";} else if (noteContext.viewScope?.viewMode === "note-map") {diff --git a/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.css b/apps/client/src/widgets/dialogs/ocr_text.csssimilarity index 77%rename from apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.cssrename to apps/client/src/widgets/dialogs/ocr_text.cssindex 724db970bf..923fe00581 100644--- a/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.css+++ b/apps/client/src/widgets/dialogs/ocr_text.css@@ -1,16 +1,3 @@-.text-representation {- padding: 10px;-}--.text-representation-header {- margin-bottom: 10px;- padding: 8px 12px;- background-color: var(--main-background-color);- border: 1px solid var(--main-border-color);- border-radius: 4px;- font-weight: 500;-}-.text-representation-loading {text-align: center;padding: 30px;diff --git a/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsx b/apps/client/src/widgets/dialogs/ocr_text.tsxsimilarity index 84%rename from apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsxrename to apps/client/src/widgets/dialogs/ocr_text.tsxindex 03c5a3120d..da3fab684c 100644--- a/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsx+++ b/apps/client/src/widgets/dialogs/ocr_text.tsx@@ -1,4 +1,4 @@-import "./ReadOnlyTextRepresentation.css";+import "./ocr_text.css";import type { OCRProcessResponse, TextRepresentationResponse } from "@triliumnext/commons";import { useEffect, useState } from "preact/hooks";@@ -8,7 +8,8 @@ import { t } from "../../services/i18n";import server from "../../services/server";import toast from "../../services/toast";import { randomString } from "../../services/utils";-import { TypeWidgetProps } from "./type_widget";+import { useTriliumEvent } from "../react/hooks";+import Modal from "../react/Modal";type State =| { kind: "loading" }@@ -16,19 +17,35 @@ type State =| { kind: "empty" }| { kind: "error"; message: string };-interface TextRepresentationProps {+export interface TextRepresentationProps {/** The API path to fetch OCR text from (e.g. `ocr/notes/{id}/text`). */textUrl: string;/** The API path to trigger OCR processing (e.g. `ocr/process-note/{id}`). */processUrl: string;}-export default function ReadOnlyTextRepresentation({ note }: TypeWidgetProps) {+export default function OcrTextDialog() {+ const [ shown, setShown ] = useState(false);+ const [ textUrl, setTextUrl ] = useState("");+ const [ processUrl, setProcessUrl ] = useState("");++ useTriliumEvent("showOcrTextDialog", ({ textUrl, processUrl }) => {+ setTextUrl(textUrl);+ setProcessUrl(processUrl);+ setShown(true);+ });+return (- <TextRepresentation- textUrl={`ocr/notes/${note.noteId}/text`}- processUrl={`ocr/process-note/${note.noteId}`}- />+ <Modal+ className="ocr-text-modal"+ title={t("ocr.extracted_text_title")}+ show={shown}+ onHidden={() => setShown(false)}+ size="lg"+ scrollable+ >+ {shown && <TextRepresentation textUrl={textUrl} processUrl={processUrl} />}+ </Modal>);}@@ -112,11 +129,7 @@ export function TextRepresentation({ textUrl, processUrl }: TextRepresentationPr}return (- <div className="text-representation note-detail-printable">- <div className="text-representation-header">- <span className="bx bx-text" />{" "}{t("ocr.extracted_text_title")}- </div>-+ <div className="text-representation">{state.kind === "loading" && (<div className="text-representation-loading"><span className="bx bx-loader-alt bx-spin" />{" "}{t("ocr.loading_text")}@@ -124,11 +137,9 @@ export function TextRepresentation({ textUrl, processUrl }: TextRepresentationPr)}{state.kind === "loaded" && (- <>- <div className="text-representation-content">- {state.text}- </div>- </>+ <div className="text-representation-content">+ {state.text}+ </div>)}{state.kind === "empty" && (diff --git a/apps/client/src/widgets/note_types.tsx b/apps/client/src/widgets/note_types.tsxindex 67ec56a618..ce8c487b45 100644--- a/apps/client/src/widgets/note_types.tsx+++ b/apps/client/src/widgets/note_types.tsx@@ -12,7 +12,7 @@ import { TypeWidgetProps } from "./type_widgets/type_widget";* A `NoteType` altered by the note detail widget, taking into consideration whether the note is editable or not and adding special note types such as an empty one,* for protected session or attachment information.*/-export type ExtendedNoteType = Exclude<NoteType, "launcher" | "text" | "code" | "llmChat"> | "empty" | "readOnlyCode" | "readOnlyText" | "readOnlyOCRText" | "editableText" | "editableCode" | "attachmentDetail" | "attachmentList" | "protectedSession" | "sqlConsole" | "markdown" | "llmChat";+export type ExtendedNoteType = Exclude<NoteType, "launcher" | "text" | "code" | "llmChat"> | "empty" | "readOnlyCode" | "readOnlyText" | "editableText" | "editableCode" | "attachmentDetail" | "attachmentList" | "protectedSession" | "sqlConsole" | "markdown" | "llmChat";export type TypeWidget = ((props: TypeWidgetProps) => VNode | JSX.Element | undefined);type NoteTypeView = () => (Promise<{ default: TypeWidget } | TypeWidget> | TypeWidget);@@ -78,11 +78,6 @@ export const TYPE_MAPPINGS: Record<ExtendedNoteType, NoteTypeMapping> = {className: "note-detail-readonly-code",printable: true},- readOnlyOCRText: {- view: () => import("./type_widgets/ReadOnlyTextRepresentation"),- className: "note-detail-ocr-text",- printable: true- },editableCode: {view: async () => (await import("./type_widgets/code/Code")).EditableCode,className: "note-detail-code",diff --git a/apps/client/src/widgets/ribbon/NoteActions.tsx b/apps/client/src/widgets/ribbon/NoteActions.tsxindex de4a87953b..651f4a8b54 100644--- a/apps/client/src/widgets/ribbon/NoteActions.tsx+++ b/apps/client/src/widgets/ribbon/NoteActions.tsx@@ -1,8 +1,7 @@import { ConvertToAttachmentResponse } from "@triliumnext/commons";import { Dropdown as BootstrapDropdown } from "bootstrap";import { ComponentChildren, RefObject } from "preact";-import { createPortal } from "preact/compat";-import { useContext, useEffect, useRef, useState } from "preact/hooks";+import { useContext, useEffect, useRef } from "preact/hooks";import appContext, { CommandNames } from "../../components/app_context";import Component from "../../components/component";@@ -24,9 +23,7 @@ import ActionButton from "../react/ActionButton";import Dropdown from "../react/Dropdown";import { FormDropdownDivider, FormDropdownSubmenu, FormListHeader, FormListItem, FormListToggleableItem } from "../react/FormList";import { useIsNoteReadOnly, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteLabelOptionalBool, useNoteProperty, useSyncedRef, useTriliumEvent, useTriliumOption } from "../react/hooks";-import Modal from "../react/Modal";import { ParentComponent } from "../react/react_utils";-import { TextRepresentation } from "../type_widgets/ReadOnlyTextRepresentation";import { NoteTypeDropdownContent, useNoteBookmarkState, useShareState } from "./BasicPropertiesTab";import NoteActionsCustom from "./NoteActionsCustom";@@ -98,9 +95,6 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNoteconst { isReadOnly, enableEditing } = useIsNoteReadOnly(note, noteContext);const isNormalViewMode = noteContext?.viewScope?.viewMode === "default";const itemToFocusRef = useRef<ItemToFocus>(null);- const supportsOcr = ["image", "file"].includes(noteType);- const [ ocrModalShown, setOcrModalShown ] = useState(false);-// Keyboard shortcuts.useTriliumEvent("toggleRibbonTabBasicProperties", () => {if (!isNewLayout) return;@@ -175,7 +169,7 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNote<CommandItem command="openNoteExternally" icon="bx bx-file-find" disabled={isSearchOrBook || !isElectron} text={t("note_actions.open_note_externally")} title={t("note_actions.open_note_externally_title")} /><CommandItem command="openNoteCustom" icon="bx bx-customize" disabled={isSearchOrBook || isMac || !isElectron} text={t("note_actions.open_note_custom")} /><CommandItem command="showNoteSource" icon="bx bx-code" disabled={!hasSource} text={t("note_actions.note_source")} />- <CommandItem command={() => setOcrModalShown(true)} icon="bx bx-text" disabled={!supportsOcr} text={t("note_actions.view_ocr_text")} />+ <CommandItem command="showNoteOCRText" icon="bx bx-text" disabled={!["image", "file"].includes(noteType)} text={t("note_actions.view_ocr_text")} />{(syncServerHost && isElectron) &&<CommandItem command="openNoteOnServer" icon="bx bx-world" disabled={!syncServerHost} text={t("note_actions.open_note_on_server")} />}@@ -190,23 +184,6 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNotecommand={() => branches.deleteNotes([note.getParentBranches()[0].branchId])}/></Dropdown>-- {supportsOcr && createPortal(- <Modal- className="ocr-text-modal"- title={t("ocr.extracted_text_title")}- show={ocrModalShown}- onHidden={() => setOcrModalShown(false)}- size="lg"- scrollable- >- <TextRepresentation- textUrl={`ocr/notes/${note.noteId}/text`}- processUrl={`ocr/process-note/${note.noteId}`}- />- </Modal>,- document.body- )}</>);}diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex 685242887b..06111bb8bd 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -2,7 +2,6 @@ import "./Attachment.css";import { ConvertAttachmentToNoteResponse } from "@triliumnext/commons";import { t } from "i18next";-import { createPortal } from "preact/compat";import { useContext, useEffect, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context";@@ -27,12 +26,10 @@ import { FormDropdownDivider, FormListItem } from "../react/FormList";import HelpButton from "../react/HelpButton";import { useTriliumEvent } from "../react/hooks";import Icon from "../react/Icon";-import Modal from "../react/Modal";import NoItems from "../react/NoItems";import NoteLink from "../react/NoteLink";import { ParentComponent, refToJQuerySelector } from "../react/react_utils";import { TextPreview } from "./File";-import { TextRepresentation } from "./ReadOnlyTextRepresentation";import { TypeWidgetProps } from "./type_widget";/**@@ -142,7 +139,6 @@ export function AttachmentDetail({ note, viewScope }: TypeWidgetProps) {function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment, isFullDetail?: boolean }) {const contentWrapper = useRef<HTMLDivElement>(null);- const [ ocrModalShown, setOcrModalShown ] = useState(false);const [ title, setTitle ] = useState(attachment.title);const [ textContent, setTextContent ] = useState<string | null>(null);const supportsOcr = attachment.role === "image" || attachment.role === "file";@@ -195,7 +191,10 @@ function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment,<AttachmentActionsattachment={attachment}copyAttachmentLinkToClipboard={copyAttachmentLinkToClipboard}- onShowOcr={supportsOcr ? () => setOcrModalShown(true) : undefined}+ onShowOcr={supportsOcr ? () => appContext.triggerCommand("showOcrTextDialog", {+ textUrl: `ocr/attachments/${attachment.attachmentId}/text`,+ processUrl: `ocr/process-attachment/${attachment.attachmentId}`+ }) : undefined}/><h4 className="attachment-title">{!isFullDetail ? (@@ -224,22 +223,6 @@ function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment,<div ref={contentWrapper} className="attachment-content-wrapper" /></div>- {supportsOcr && createPortal(- <Modal- className="ocr-text-modal"- title={t("ocr.extracted_text_title")}- show={ocrModalShown}- onHidden={() => setOcrModalShown(false)}- size="lg"- scrollable- >- <TextRepresentation- textUrl={`ocr/attachments/${attachment.attachmentId}/text`}- processUrl={`ocr/process-attachment/${attachment.attachmentId}`}- />- </Modal>,- document.body- )}</div>);}