Initial
This commit is contained in:
59
src/view/FileSwitcherBar.ts
Normal file
59
src/view/FileSwitcherBar.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { App, TFile } from 'obsidian';
|
||||
|
||||
export interface FileSwitcherBarOptions {
|
||||
app: App;
|
||||
currentFile: TFile;
|
||||
taskFiles: TFile[];
|
||||
inboxPath: string;
|
||||
onSwitchFile: (file: TFile) => void;
|
||||
onCreateFile: (name: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a header bar with a file switcher dropdown and a "New task file" button.
|
||||
* Returns the container element to be prepended into the view's root.
|
||||
*/
|
||||
export function buildFileSwitcherBar(opts: FileSwitcherBarOptions): HTMLElement {
|
||||
const { currentFile, taskFiles, inboxPath, onSwitchFile, onCreateFile } = opts;
|
||||
|
||||
const bar = createDiv({ cls: 'yaotp-switcher-bar' });
|
||||
|
||||
// Left side: file selector
|
||||
const selectWrap = bar.createDiv({ cls: 'yaotp-switcher-select-wrap' });
|
||||
|
||||
const select = selectWrap.createEl('select', { cls: 'yaotp-switcher-select' });
|
||||
|
||||
// Always show Inbox first if it exists in the list
|
||||
const inbox = taskFiles.find((f) => f.path === inboxPath);
|
||||
const others = taskFiles.filter((f) => f.path !== inboxPath);
|
||||
const ordered = inbox ? [inbox, ...others] : others;
|
||||
|
||||
for (const file of ordered) {
|
||||
const opt = select.createEl('option', {
|
||||
text: file.basename,
|
||||
value: file.path,
|
||||
});
|
||||
if (file.path === currentFile.path) {
|
||||
opt.selected = true;
|
||||
}
|
||||
}
|
||||
|
||||
select.addEventListener('change', () => {
|
||||
const chosen = taskFiles.find((f) => f.path === select.value);
|
||||
if (chosen) onSwitchFile(chosen);
|
||||
});
|
||||
|
||||
// Right side: new file button
|
||||
const newBtn = bar.createEl('button', {
|
||||
text: '+ New list',
|
||||
cls: 'yaotp-switcher-new-btn',
|
||||
});
|
||||
newBtn.addEventListener('click', () => {
|
||||
const name = prompt('New task file name (without .md):');
|
||||
if (name && name.trim()) {
|
||||
onCreateFile(name.trim());
|
||||
}
|
||||
});
|
||||
|
||||
return bar;
|
||||
}
|
||||
Reference in New Issue
Block a user