2026-04-08 14:01:19 +02:00
|
|
|
import type React from 'react'
|
2026-04-11 23:51:58 +02:00
|
|
|
import { useRef } from 'react'
|
2026-04-08 14:01:19 +02:00
|
|
|
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'
|
2026-04-08 14:01:19 +02:00
|
|
|
import { useEditorTheme } from '../../hooks/useTheme'
|
2026-04-08 20:17:51 +02:00
|
|
|
import { deriveEditorContentState } from './editorContentState'
|
2026-04-27 10:12:13 +02:00
|
|
|
import type { RawEditorFindRequest } from '../RawEditorFindBar'
|
2026-04-08 14:01:19 +02:00
|
|
|
|
|
|
|
|
export interface Tab {
|
|
|
|
|
entry: VaultEntry
|
|
|
|
|
content: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface EditorContentProps {
|
|
|
|
|
activeTab: Tab | null
|
2026-04-29 22:52:19 +02:00
|
|
|
activeTabPath: string | null
|
2026-04-08 14:01:19 +02:00
|
|
|
isLoadingNewTab: boolean
|
2026-04-29 22:52:19 +02:00
|
|
|
isVaultLoading?: boolean
|
2026-04-08 14:01:19 +02:00
|
|
|
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
|
2026-05-04 06:00:16 +02:00
|
|
|
showTableOfContents?: boolean
|
|
|
|
|
onToggleTableOfContents?: () => void
|
2026-04-08 14:01:19 +02:00
|
|
|
inspectorCollapsed: boolean
|
|
|
|
|
onToggleInspector: () => void
|
|
|
|
|
onNavigateWikilink: (target: string) => void
|
|
|
|
|
onEditorChange?: () => void
|
|
|
|
|
onToggleFavorite?: (path: string) => void
|
|
|
|
|
onToggleOrganized?: (path: string) => void
|
2026-05-05 15:46:18 +02:00
|
|
|
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-04-08 14:01:19 +02:00
|
|
|
onDeleteNote?: (path: string) => void
|
|
|
|
|
onArchiveNote?: (path: string) => void
|
|
|
|
|
onUnarchiveNote?: (path: string) => void
|
|
|
|
|
vaultPath?: string
|
2026-04-12 11:45:59 +02:00
|
|
|
rawModeContent?: string | null
|
2026-04-27 10:12:13 +02:00
|
|
|
findRequest?: RawEditorFindRequest | null
|
2026-04-08 14:01:19 +02:00
|
|
|
rawLatestContentRef?: React.MutableRefObject<string | null>
|
2026-04-10 17:59:21 +02:00
|
|
|
onRenameFilename?: (path: string, newFilenameStem: string) => void
|
2026-04-29 19:26:24 +02:00
|
|
|
noteWidth?: NoteWidthMode
|
|
|
|
|
onToggleNoteWidth?: () => void
|
2026-04-08 14:01:19 +02:00
|
|
|
isConflicted?: boolean
|
|
|
|
|
onKeepMine?: (path: string) => void
|
|
|
|
|
onKeepTheirs?: (path: string) => void
|
2026-04-26 19:37:21 +02:00
|
|
|
locale?: AppLocale
|
2026-04-08 14:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useEditorContentModel(props: EditorContentProps) {
|
|
|
|
|
const {
|
|
|
|
|
activeTab,
|
2026-04-29 22:52:19 +02:00
|
|
|
activeTabPath,
|
2026-04-08 14:01:19 +02:00
|
|
|
entries,
|
|
|
|
|
rawMode,
|
|
|
|
|
diffMode,
|
|
|
|
|
} = props
|
|
|
|
|
|
|
|
|
|
const { cssVars } = useEditorTheme()
|
2026-04-08 20:17:51 +02:00
|
|
|
const {
|
|
|
|
|
isArchived,
|
|
|
|
|
isDeletedPreview,
|
|
|
|
|
isNonMarkdownText,
|
|
|
|
|
effectiveRawMode,
|
|
|
|
|
showEditor: showContentEditor,
|
|
|
|
|
path,
|
|
|
|
|
wordCount,
|
|
|
|
|
} = deriveEditorContentState({
|
|
|
|
|
activeTab,
|
|
|
|
|
entries,
|
|
|
|
|
rawMode,
|
2026-04-11 23:51:58 +02:00
|
|
|
activeStatus: props.activeStatus,
|
2026-04-08 20:17:51 +02:00
|
|
|
})
|
|
|
|
|
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
|
2026-04-08 14:01:19 +02:00
|
|
|
|
|
|
|
|
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,
|
2026-04-08 14:01:19 +02:00
|
|
|
path,
|
|
|
|
|
breadcrumbBarRef,
|
|
|
|
|
wordCount,
|
|
|
|
|
}
|
|
|
|
|
}
|