Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
samlhuillier committed Nov 13, 2024
1 parent 73996b2 commit 7617aad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/components/Editor/DocumentStats.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useEffect, useState } from 'react'
import { Editor } from '@tiptap/react'

interface DocumentStatsProps {
editor: Editor | null
}

const DocumentStats: React.FC<DocumentStatsProps> = ({ editor }) => {
const [show, setShow] = useState(false)

useEffect(() => {
const initDocumentStats = async () => {
const showStats = await window.electronStore.getDocumentStats()
setShow(showStats)
}

initDocumentStats()

const handleDocStatsChange = (event: Electron.IpcRendererEvent, value: boolean) => {
setShow(value)
}

window.ipcRenderer.on('show-doc-stats-changed', handleDocStatsChange)
}, [])

if (!editor || !show) return null

return (
<div className="absolute bottom-2 right-2 flex gap-4 text-sm text-gray-500">
<div>Characters: {editor.storage.characterCount.characters()}</div>
<div>Words: {editor.storage.characterCount.words()}</div>
</div>
)
}

export default DocumentStats
49 changes: 4 additions & 45 deletions src/components/Editor/EditorManager.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { useEffect, useState } from 'react'
import { EditorContent } from '@tiptap/react'
import InEditorBacklinkSuggestionsDisplay from './BacklinkSuggestionsDisplay'
import EditorContextMenu from './EditorContextMenu'
import SearchBar from './Search/SearchBar'
import { useFileContext } from '@/contexts/FileContext'
import { useContentContext } from '@/contexts/ContentContext'
import DocumentStats from './DocumentStats'

const EditorManager: React.FC = () => {
const [showSearchBar, setShowSearchBar] = useState(false)
const [contextMenuVisible, setContextMenuVisible] = useState(false)
const [menuPosition, setMenuPosition] = useState({ x: 0, y: 0 })
const [editorFlex, setEditorFlex] = useState(true)

const { editor, suggestionsState, vaultFilesFlattened } = useFileContext()
const [showDocumentStats, setShowDocumentStats] = useState(false)
const { openContent } = useContentContext()
const { editor } = useFileContext()

const handleContextMenu = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault()
Expand All @@ -29,15 +26,6 @@ const EditorManager: React.FC = () => {
if (contextMenuVisible) setContextMenuVisible(false)
}

const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {
const { target } = event
if (target instanceof HTMLElement && target.getAttribute('data-backlink') === 'true') {
event.preventDefault()
const backlinkPath = target.textContent
if (backlinkPath) openContent(backlinkPath)
}
}

useEffect(() => {
const initEditorContentCenter = async () => {
const isCenter = await window.electronStore.getEditorFlexCenter()
Expand All @@ -52,21 +40,6 @@ const EditorManager: React.FC = () => {
window.ipcRenderer.on('editor-flex-center-changed', handleEditorChange)
}, [])

useEffect(() => {
const initDocumentStats = async () => {
const showStats = await window.electronStore.getDocumentStats()
setShowDocumentStats(showStats)
}

initDocumentStats()

const handleDocStatsChange = (event: Electron.IpcRendererEvent, value: boolean) => {
setShowDocumentStats(value)
}

window.ipcRenderer.on('show-doc-stats-changed', handleDocStatsChange)
}, [])

return (
<div
className="relative size-full cursor-text overflow-hidden bg-dark-gray-c-eleven py-4 text-slate-400 opacity-80"
Expand All @@ -82,33 +55,19 @@ const EditorManager: React.FC = () => {
/>
)}

<div
className={`relative h-full ${editorFlex ? 'flex justify-center py-4 pl-4' : ''} ${showDocumentStats ? 'pb-3' : ''}`}
>
<div className={`relative h-full ${editorFlex ? 'flex justify-center py-4 pl-4' : ''}`}>
<div className="relative size-full overflow-y-auto">
<EditorContent
className={`relative size-full bg-dark-gray-c-eleven ${editorFlex ? 'max-w-xl' : ''}`}
style={{
wordBreak: 'break-word',
}}
onContextMenu={handleContextMenu}
onClick={handleClick}
editor={editor}
/>
</div>
</div>
{suggestionsState && (
<InEditorBacklinkSuggestionsDisplay
suggestionsState={suggestionsState}
suggestions={vaultFilesFlattened.map((file) => file.relativePath)}
/>
)}
{editor && showDocumentStats && (
<div className="absolute bottom-2 right-2 flex gap-4 text-sm text-gray-500">
<div>Characters: {editor.storage.characterCount.characters()}</div>
<div>Words: {editor.storage.characterCount.words()}</div>
</div>
)}
<DocumentStats editor={editor} />
</div>
)
}
Expand Down
6 changes: 0 additions & 6 deletions src/contexts/FileContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
getNextAvailableFileNameGivenBaseName,
sortFilesAndDirectories,
} from '@/lib/file'
import { SuggestionsState } from '@/components/Editor/BacklinkSuggestionsDisplay'
import HighlightExtension, { HighlightData } from '@/components/Editor/HighlightExtension'
import { RichTextLink } from '@/components/Editor/RichTextLink'
import '@/styles/tiptap.scss'
Expand All @@ -49,15 +48,13 @@ type FileContextType = {
navigationHistory: string[]
addToNavigationHistory: (value: string) => void
openOrCreateFile: (filePath: string, optionalContentToWriteOnCreate?: string) => Promise<void>
suggestionsState: SuggestionsState | null | undefined
spellCheckEnabled: boolean
highlightData: HighlightData
noteToBeRenamed: string
setNoteToBeRenamed: React.Dispatch<React.SetStateAction<string>>
fileDirToBeRenamed: string
setFileDirToBeRenamed: React.Dispatch<React.SetStateAction<string>>
renameFile: (oldFilePath: string, newFilePath: string) => Promise<void>
setSuggestionsState: React.Dispatch<React.SetStateAction<SuggestionsState | null | undefined>>
setSpellCheckEnabled: React.Dispatch<React.SetStateAction<boolean>>
deleteFile: (path: string | undefined) => Promise<boolean>
selectedDirectory: string | null
Expand All @@ -80,7 +77,6 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
const [expandedDirectories, setExpandedDirectories] = useState<Map<string, boolean>>(new Map())
const [selectedDirectory, setSelectedDirectory] = useState<string | null>(null)
const [currentlyOpenFilePath, setCurrentlyOpenFilePath] = useState<string | null>(null)
const [suggestionsState, setSuggestionsState] = useState<SuggestionsState | null>()
const [needToWriteEditorContentToDisk, setNeedToWriteEditorContentToDisk] = useState<boolean>(false)
const [needToIndexEditorContent, setNeedToIndexEditorContent] = useState<boolean>(false)
const [spellCheckEnabled, setSpellCheckEnabled] = useState<boolean>(false)
Expand Down Expand Up @@ -369,15 +365,13 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
navigationHistory,
addToNavigationHistory,
openOrCreateFile,
suggestionsState,
spellCheckEnabled,
highlightData,
noteToBeRenamed,
setNoteToBeRenamed,
fileDirToBeRenamed,
setFileDirToBeRenamed,
renameFile,
setSuggestionsState,
setSpellCheckEnabled,
deleteFile,
selectedDirectory,
Expand Down

0 comments on commit 7617aad

Please sign in to comment.