Files
tolaria/src/components/editor-content/useEditorContentModel.ts

110 lines
3.1 KiB
TypeScript
Raw Normal View History

import type React from 'react'
import { useRef } from 'react'
import type { useCreateBlockNote } from '@blocknote/react'
2026-04-26 19:37:21 +02:00
import type { AppLocale } from '../../lib/i18n'
2026-04-29 19:26:24 +02:00
import type { NoteWidthMode, NoteStatus, VaultEntry } from '../../types'
import { useEditorTheme } from '../../hooks/useTheme'
import { deriveEditorContentState } from './editorContentState'
2026-04-27 10:12:13 +02:00
import type { RawEditorFindRequest } from '../RawEditorFindBar'
export interface Tab {
entry: VaultEntry
content: string
}
export interface EditorContentProps {
activeTab: Tab | null
2026-04-29 22:52:19 +02:00
activeTabPath: string | null
isLoadingNewTab: boolean
2026-04-29 22:52:19 +02:00
isVaultLoading?: boolean
entries: VaultEntry[]
editor: ReturnType<typeof useCreateBlockNote>
diffMode: boolean
diffContent: string | null
diffLoading: boolean
onToggleDiff: () => void
rawMode: boolean
onToggleRaw: () => void
onRawContentChange?: (path: string, content: string) => void
onSave?: () => void
activeStatus: NoteStatus
showDiffToggle: boolean
showAIChat?: boolean
onToggleAIChat?: () => void
showTableOfContents?: boolean
onToggleTableOfContents?: () => void
inspectorCollapsed: boolean
onToggleInspector: () => void
onNavigateWikilink: (target: string) => void
onEditorChange?: () => void
onToggleFavorite?: (path: string) => void
onToggleOrganized?: (path: string) => void
onEnterNeighborhood?: (entry: VaultEntry) => void
2026-04-27 01:45:16 +02:00
onRevealFile?: (path: string) => void
onCopyFilePath?: (path: string) => void
2026-05-27 14:36:36 +02:00
onCopyDeepLink?: (entry: VaultEntry) => void
2026-06-06 14:24:51 +02:00
onCopyGitUrl?: (entry: VaultEntry) => void
2026-05-29 19:15:07 +02:00
onExportPdf?: () => void
onDeleteNote?: (path: string) => void
onArchiveNote?: (path: string) => void
onUnarchiveNote?: (path: string) => void
vaultPath?: string
rawModeContent?: string | null
2026-04-27 10:12:13 +02:00
findRequest?: RawEditorFindRequest | null
rawLatestContentRef?: React.MutableRefObject<string | null>
onRenameFilename?: (path: string, newFilenameStem: string) => void
2026-04-29 19:26:24 +02:00
noteWidth?: NoteWidthMode
onToggleNoteWidth?: () => void
isConflicted?: boolean
onKeepMine?: (path: string) => void
onKeepTheirs?: (path: string) => void
2026-04-26 19:37:21 +02:00
locale?: AppLocale
}
export function useEditorContentModel(props: EditorContentProps) {
const {
activeTab,
2026-04-29 22:52:19 +02:00
activeTabPath,
entries,
rawMode,
diffMode,
} = props
const { cssVars } = useEditorTheme()
const {
isArchived,
isDeletedPreview,
isNonMarkdownText,
effectiveRawMode,
showEditor: showContentEditor,
path,
wordCount,
} = deriveEditorContentState({
activeTab,
entries,
rawMode,
activeStatus: props.activeStatus,
})
const showEditor = !diffMode && showContentEditor
2026-04-29 22:52:19 +02:00
const loadingEntry = !activeTab && activeTabPath
? entries.find((entry) => entry.path === activeTabPath) ?? null
: null
const loadingTab = loadingEntry ? { entry: loadingEntry, content: '' } : null
const breadcrumbBarRef = useRef<HTMLDivElement | null>(null)
return {
...props,
cssVars,
isArchived,
isDeletedPreview,
effectiveRawMode,
forceRawMode: isNonMarkdownText || isDeletedPreview,
showEditor,
2026-04-29 22:52:19 +02:00
loadingTab,
path,
breadcrumbBarRef,
wordCount,
}
}