wip: working a bit

This commit is contained in:
2023-12-07 15:56:06 +01:00
parent dcfbe2c519
commit be7d302ef8
3 changed files with 54 additions and 7 deletions

View File

@@ -96,18 +96,40 @@ let watcher: any = null
let currentContent: any = null let currentContent: any = null
function setupWatcher(filePath: string) { function setupWatcher(filePath: string) {
store.set('lastFilePath', filePath) const isDir = fs.statSync(filePath).isDirectory()
store.set(isDir ? 'lastOpenDir' : 'lastFilePath', filePath)
// Close the existing watcher if it exists // Close the existing watcher if it exists
if (watcher) { if (watcher) {
watcher.close() watcher.close()
} }
watcher = chokidar.watch(filePath, { watcher = chokidar.watch(filePath, {
ignored: /(^|[/\\])\../, // ignore dotfiles ignored: path => {
if (path.includes('node_modules')) {
return true
}
// Ignore if it's not a directory and does not end with .mdx
return !path.endsWith('.mdx') && !fs.lstatSync(path).isDirectory()
},
persistent: true, persistent: true,
}) })
const bundleAndSend = async (path: string) => { const bundleAndSend = async (path: string) => {
if (isDir) {
const lastOpenDir = store.get('lastOpenDir') as string | undefined
if (!lastOpenDir) {
return
}
prepareAndSendDir(lastOpenDir)
return
}
console.log('change', path)
fs.readFile(path, 'utf8', async (err, content) => { fs.readFile(path, 'utf8', async (err, content) => {
if (err) { if (err) {
console.error('Error reading the file:', err) console.error('Error reading the file:', err)
@@ -132,15 +154,40 @@ function setupWatcher(filePath: string) {
// Add your event listeners // Add your event listeners
watcher watcher
.on('add', async (path: string) => { .on('add', async (path: string) => {
console.log('file added', path)
await bundleAndSend(path) await bundleAndSend(path)
console.warn(`File ${path} has been added`) console.warn(`File ${path} has been added`)
}) })
.on('addDir', async () => {
console.log('add dir')
const lastOpenDir = store.get('lastOpenDir') as string | undefined
if (!lastOpenDir) {
return
}
prepareAndSendDir(lastOpenDir)
})
.on('unlinkDir', async () => {
const lastOpenDir = store.get('lastOpenDir') as string | undefined
if (!lastOpenDir) {
return
}
prepareAndSendDir(lastOpenDir)
})
.on('change', bundleAndSend) .on('change', bundleAndSend)
.on('unlink', (path: string) => console.warn(`File ${path} has been removed`)) .on('unlink', (path: string) => console.warn(`File ${path} has been removed`))
const watchedPaths = watcher.getWatched()
console.log(watchedPaths)
} }
function prepareAndSendDir(dir: string) { function prepareAndSendDir(dir: string) {
console.log('prepareAndSendDir', dir)
const files = fs.readdirSync(dir) const files = fs.readdirSync(dir)
const dirName = path.basename(dir) const dirName = path.basename(dir)
const data = [ const data = [
@@ -175,6 +222,7 @@ ipcMain.on('dropped-file', (event, arg) => {
if (fs.statSync(pathToCheck).isDirectory()) { if (fs.statSync(pathToCheck).isDirectory()) {
// If it's a directory, get the list of files // If it's a directory, get the list of files
prepareAndSendDir(pathToCheck) prepareAndSendDir(pathToCheck)
setupWatcher(pathToCheck)
} else { } else {
setupWatcher(pathToCheck) setupWatcher(pathToCheck)
} }

View File

@@ -20,6 +20,8 @@ type Props = {
setSelectedMdx: (s: string) => void setSelectedMdx: (s: string) => void
} }
export const MdxFileSelector = ({ data, selectedMdx, setSelectedMdx }: Props) => { export const MdxFileSelector = ({ data, selectedMdx, setSelectedMdx }: Props) => {
console.log(data)
return ( return (
<div className={s.container}> <div className={s.container}>
<FileTree> <FileTree>

View File

@@ -38,6 +38,7 @@ export const View = () => {
} }
const directoryContentsListener: IpcRendererListener = (_event, content) => { const directoryContentsListener: IpcRendererListener = (_event, content) => {
console.log('directoryContentsListener', content)
setDirectoryContents(content) setDirectoryContents(content)
} }
@@ -54,10 +55,6 @@ export const View = () => {
} }
}, []) }, [])
if (!code) {
return null
}
return ( return (
<div className={s.page}> <div className={s.page}>
<ImagePreview onClose={() => setSrcPreview('')} open={!!srcPreview} src={srcPreview} /> <ImagePreview onClose={() => setSrcPreview('')} open={!!srcPreview} src={srcPreview} />
@@ -72,7 +69,7 @@ export const View = () => {
)} )}
</div> </div>
<Prose as={'article'} className={s.root}> <Prose as={'article'} className={s.root}>
<MdxComponent code={code} onImageClick={setSrcPreview} /> {code && <MdxComponent code={code} onImageClick={setSrcPreview} />}
</Prose> </Prose>
<div className={s.tocContainer}> <div className={s.tocContainer}>
<TableOfContents tocMap={toc?.map} /> <TableOfContents tocMap={toc?.map} />