@isthatuzii/create-nano-app
Version:
Desktop application scaffolding tool for the Nano Framework
131 lines (115 loc) • 2.88 kB
JavaScript
export const createMainWorkspaceFunctions = (
state,
setState,
props
) => {
const setActiveTab = (tabId) => {
setState(prev => ({
...prev,
activeTab: tabId
}));
if (props.onTabChange) {
props.onTabChange(tabId);
}
};
const closeTab = (tabId) => {
setState(prev => {
const newTabs = prev.tabs.filter(tab => tab.id !== tabId);
let newActiveTab = prev.activeTab;
// If we're closing the active tab, switch to another
if (prev.activeTab === tabId) {
if (newTabs.length > 0) {
const closingIndex = prev.tabs.findIndex(tab => tab.id === tabId);
const nextIndex = Math.min(closingIndex, newTabs.length - 1);
newActiveTab = newTabs[nextIndex].id;
} else {
newActiveTab = null;
}
}
return {
...prev,
tabs: newTabs,
activeTab: newActiveTab
};
});
if (props.onTabClose) {
props.onTabClose(tabId);
}
};
const addNewTab = () => {
const newTabId = `tab_${Date.now()}`;
const newTab = {
id: newTabId,
name: 'New Tab',
type: 'untitled',
modified: false,
content: {
name: 'Untitled',
type: 'Empty',
guid: '',
path: ''
}
};
setState(prev => ({
...prev,
tabs: [...prev.tabs, newTab],
activeTab: newTabId
}));
if (props.onTabAdd) {
props.onTabAdd(newTab);
}
};
const openTab = (item) => {
// Check if tab is already open
const existingTab = state().tabs.find(tab => tab.id === item.id);
if (existingTab) {
setActiveTab(item.id);
return;
}
// Create new tab
const newTab = {
id: item.id,
name: item.name,
type: item.type,
modified: false,
content: {
name: item.name,
type: item.type,
guid: item.guid || `${item.id}-guid`,
path: item.path || `path/to/${item.name}.xml`
}
};
setState(prev => ({
...prev,
tabs: [...prev.tabs, newTab],
activeTab: item.id
}));
};
const markTabModified = (tabId, modified = true) => {
setState(prev => ({
...prev,
tabs: prev.tabs.map(tab =>
tab.id === tabId ? { ...tab, modified } : tab
)
}));
};
const initialize = () => {
console.log("🗂️ MainWorkspace initialized");
// Listen for item selections from DataBrowser
if (props.onItemSelect) {
props.onItemSelect = (item) => {
if (item.type === 'EntityClassDefinition') {
openTab(item);
}
};
}
};
return {
setActiveTab,
closeTab,
addNewTab,
openTab,
markTabModified,
initialize
};
};