fix: dropdown singleton

This commit is contained in:
Aleix 2025-05-21 18:47:46 +02:00
parent 39af634a9f
commit ef0f2a65ea
3 changed files with 23 additions and 24 deletions

View file

@ -1,4 +1,4 @@
import { MarkdownView, Editor, Notice, TFile } from 'obsidian';
import { MarkdownView, Editor, Notice, TFile, App } from 'obsidian';
import { DropdownUI } from './dropdown-ui';
import { DropdownOptions, DropdownDependencies } from './types';
import { createDummyTarget } from './dropdown-position';
@ -9,12 +9,15 @@ import { NoteStatusSettings } from 'models/types';
* High-level manager for status dropdown interactions
*/
export class DropdownManager {
private app: any;
private app: App;
private settings: NoteStatusSettings;
private statusService: StatusService;
private currentStatuses: string[] = ['unknown'];
private toolbarButton?: HTMLElement;
private dropdownUI: DropdownUI;
private dropdownUI: DropdownUI;
// Singleton para gestionar todos los dropdowns
private static activeInstance: DropdownManager | null = null;
constructor(app: any, settings: NoteStatusSettings, statusService: StatusService) {
this.app = app;
@ -35,7 +38,6 @@ export class DropdownManager {
this.dropdownUI.setOnStatusChange((statuses) => {
this.currentStatuses = [...statuses];
this.updateToolbarButton();
// this.statusService.notifyStatusChanged(statuses);
});
this.dropdownUI.setOnRemoveStatusHandler(async (status, targetFile) => {
@ -99,6 +101,7 @@ export class DropdownManager {
private updateToolbarButton(): void {
if (!this.toolbarButton) return;
DropdownManager.activeInstance?.dropdownUI.close();
this.toolbarButton.empty();
const hasValidStatus = this.currentStatuses.length > 0 &&
@ -158,7 +161,8 @@ export class DropdownManager {
/**
* Updates the dropdown UI based on current statuses
*/
public update(currentStatuses: string[] | string): void {
public update(currentStatuses: string[] | string, file?: TFile): void {
this.currentStatuses = Array.isArray(currentStatuses) ?
[...currentStatuses] : [currentStatuses];
@ -240,18 +244,14 @@ export class DropdownManager {
* Universal function to open the status dropdown
*/
public openStatusDropdown(options: DropdownOptions): void {
if (this.dropdownUI.isOpen) {
this.dropdownUI.close();
setTimeout(() => this._openStatusDropdown(options), 50);
} else {
this._openStatusDropdown(options);
// Cerrar dropdown activo antes de abrir uno nuevo
if (DropdownManager.activeInstance && DropdownManager.activeInstance !== this) {
console.log("close this:, ",DropdownManager.activeInstance)
this.resetDropdownState();
DropdownManager.activeInstance.dropdownUI.close();
}
}
DropdownManager.activeInstance = this;
/**
* Internal method to open dropdown
*/
private _openStatusDropdown(options: DropdownOptions): void {
const files = options.files || [this.app.workspace.getActiveFile()].filter(Boolean);
if (!files.length) {
new Notice('No files selected');
@ -277,6 +277,7 @@ export class DropdownManager {
this.positionAndOpenDropdown(options);
}
/**
* Reset dropdown state before opening

View file

@ -184,12 +184,10 @@ export class DropdownUI {
this.isOpen = false;
setTimeout(() => {
if (this.dropdownElement) {
this.dropdownElement.remove();
this.dropdownElement = null;
}
}, this.animationDuration);
if (this.dropdownElement) {
this.dropdownElement.remove();
this.dropdownElement = null;
}
}
/**

View file

@ -62,7 +62,7 @@ export class WorkspaceIntegration {
this.toolbarIntegration.addToolbarButtonToActiveLeaf();
// Actualiza estado
this.propagateNoteStatusChange();
this.propagateNoteStatusChange(file);
}
/**
@ -91,7 +91,7 @@ export class WorkspaceIntegration {
/**
* Verifica y propaga el estado de la nota activa
*/
private propagateNoteStatusChange(): void {
private propagateNoteStatusChange(file: TFile): void {
try {
const activeFile = this.app.workspace.getActiveFile();
let fileStatuses: string[] = [];
@ -100,7 +100,7 @@ export class WorkspaceIntegration {
}
// Dispara evento para que otros componentes se actualicen
window.dispatchEvent(new CustomEvent('note-status:status-changed', {
detail: { statuses: fileStatuses }
detail: { statuses: fileStatuses, file: file }
}));
} catch (error) {
console.error('Error checking note status:', error);