-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement file tabs management
- Loading branch information
1 parent
278b2cc
commit edbd3e9
Showing
9 changed files
with
203 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import fileSystem from '@/lib/fs'; | ||
import { IDEContext, IFileTab } from '@/state/IDE.context'; | ||
import { useContext } from 'react'; | ||
|
||
const useFileTab = () => { | ||
const { fileTab, setFileTab, activeProject } = useContext(IDEContext); | ||
|
||
const syncTabSettings = async (updatedTab?: IFileTab) => { | ||
if (!activeProject) return; | ||
|
||
const defaultSetting = { | ||
tab: { | ||
items: [], | ||
active: null, | ||
}, | ||
}; | ||
|
||
try { | ||
const settingPath = `/${activeProject}/.ide/setting.json`; | ||
if (!(await fileSystem.exists(settingPath))) { | ||
await fileSystem.writeFile( | ||
settingPath, | ||
JSON.stringify(defaultSetting, null, 2), | ||
{ | ||
overwrite: true, | ||
}, | ||
); | ||
} | ||
const setting = (await fileSystem.readFile(settingPath)) as string; | ||
|
||
let parsedSetting = setting ? JSON.parse(setting) : defaultSetting; | ||
|
||
if (updatedTab) { | ||
parsedSetting.tab = updatedTab; | ||
} else { | ||
parsedSetting = { | ||
...defaultSetting, | ||
...parsedSetting, | ||
}; | ||
setFileTab(parsedSetting.tab); | ||
} | ||
|
||
await fileSystem.writeFile( | ||
settingPath, | ||
JSON.stringify(parsedSetting, null, 2), | ||
{ | ||
overwrite: true, | ||
}, | ||
); | ||
} catch (error) { | ||
console.error('Error syncing tab settings:', error); | ||
} | ||
}; | ||
|
||
const open = (name: string, path: string) => { | ||
if (fileTab.active === name) return; | ||
|
||
const existingTab = fileTab.items.find((item) => item.path === path); | ||
|
||
if (existingTab) { | ||
const updatedTab = { ...fileTab, active: path }; | ||
setFileTab(updatedTab); | ||
syncTabSettings(updatedTab); | ||
} else { | ||
const newTab = { name, path, isDirty: false }; | ||
const updatedTab = { | ||
...fileTab, | ||
items: [...fileTab.items, newTab], | ||
active: path, | ||
}; | ||
setFileTab(updatedTab); | ||
syncTabSettings(updatedTab); | ||
} | ||
}; | ||
|
||
const close = (filePath: string, closeAll: boolean = false) => { | ||
let updatedTab: IFileTab; | ||
|
||
if (closeAll) { | ||
updatedTab = { items: [], active: null }; | ||
} else { | ||
const updatedItems = fileTab.items.filter( | ||
(item) => item.path !== filePath, | ||
); | ||
|
||
let newActiveTab = fileTab.active; | ||
if (fileTab.active === filePath) { | ||
const closedTabIndex = fileTab.items.findIndex( | ||
(item) => item.path === filePath, | ||
); | ||
if (updatedItems.length > 0) { | ||
if (closedTabIndex > 0) { | ||
newActiveTab = updatedItems[closedTabIndex - 1].path; | ||
} else { | ||
newActiveTab = updatedItems[0].path; | ||
} | ||
} else { | ||
newActiveTab = null; // No more tabs open | ||
} | ||
} | ||
|
||
updatedTab = { items: updatedItems, active: newActiveTab }; | ||
} | ||
|
||
setFileTab(updatedTab); | ||
syncTabSettings(updatedTab); | ||
}; | ||
|
||
return { fileTab, open, close, syncTabSettings }; | ||
}; | ||
|
||
export default useFileTab; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useFileTab } from './fileTabs.hooks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.