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
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
if (watcher) {
watcher.close()
}
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,
})
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) => {
if (err) {
console.error('Error reading the file:', err)
@@ -132,15 +154,40 @@ function setupWatcher(filePath: string) {
// Add your event listeners
watcher
.on('add', async (path: string) => {
console.log('file added', path)
await bundleAndSend(path)
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('unlink', (path: string) => console.warn(`File ${path} has been removed`))
const watchedPaths = watcher.getWatched()
console.log(watchedPaths)
}
function prepareAndSendDir(dir: string) {
console.log('prepareAndSendDir', dir)
const files = fs.readdirSync(dir)
const dirName = path.basename(dir)
const data = [
@@ -175,6 +222,7 @@ ipcMain.on('dropped-file', (event, arg) => {
if (fs.statSync(pathToCheck).isDirectory()) {
// If it's a directory, get the list of files
prepareAndSendDir(pathToCheck)
setupWatcher(pathToCheck)
} else {
setupWatcher(pathToCheck)
}

View File

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

View File

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