fix: should not trigger by float search itself twice

This commit is contained in:
quorafind 2025-02-27 15:25:39 +08:00
parent 9b7a38f0f2
commit ab7c600b3c

View file

@ -113,6 +113,7 @@ const initSearchViewWithLeaf = async (
state: { state: {
...DEFAULT_SETTINGS.searchViewState, ...DEFAULT_SETTINGS.searchViewState,
...state, ...state,
triggerBySelf: true,
}, },
}); });
@ -179,13 +180,7 @@ export default class FloatSearchPlugin extends Plugin {
`Search obsidian in ${this.settings.defaultViewType} view`, `Search obsidian in ${this.settings.defaultViewType} view`,
() => { () => {
if (this.settings.defaultViewType === "modal") { if (this.settings.defaultViewType === "modal") {
// Check if a modal is already open to prevent duplicate modals this.initModal(this.state, true, true);
if (!this.modal) {
this.initModal(this.state, true, true);
} else {
// If a modal is already open, focus it instead of creating a new one
this.modal.contentEl.querySelector("input")?.focus();
}
} else { } else {
initSearchViewWithLeaf( initSearchViewWithLeaf(
this.app, this.app,
@ -309,17 +304,19 @@ export default class FloatSearchPlugin extends Plugin {
// 0.14.x doesn't have WorkspaceContainer; this can just be an instanceof check once 15.x is mandatory: // 0.14.x doesn't have WorkspaceContainer; this can just be an instanceof check once 15.x is mandatory:
if ( if (!requireApiVersion("0.15.0")) {
parent === self.app.workspace.rootSplit || if (
(WorkspaceContainer && parent === self.app.workspace.rootSplit ||
parent instanceof WorkspaceContainer) (WorkspaceContainer &&
) { parent instanceof WorkspaceContainer)
for (const popover of EmbeddedView.popoversForWindow( ) {
(parent as WorkspaceContainer).win for (const popover of EmbeddedView.popoversForWindow(
)) { (parent as WorkspaceContainer).win
// Use old API here for compat w/0.14.x )) {
if (old.call(this, cb, popover.rootSplit)) // Use old API here for compat w/0.14.x
return false; if (old.call(this, cb, popover.rootSplit))
return false;
}
} }
} }
return false; return false;
@ -334,11 +331,6 @@ export default class FloatSearchPlugin extends Plugin {
return old.call(this, leaf, params); return old.call(this, leaf, params);
}; };
}, },
onDragLeaf(old) {
return function (event: MouseEvent, leaf: WorkspaceLeaf) {
return old.call(this, event, leaf);
};
},
pushUndoHistory(old: any) { pushUndoHistory(old: any) {
return function ( return function (
leaf: WorkspaceLeaf, leaf: WorkspaceLeaf,
@ -707,11 +699,9 @@ export default class FloatSearchPlugin extends Plugin {
!state?.triggerBySelf !state?.triggerBySelf
) { ) {
if (self.queryLoaded) { if (self.queryLoaded) {
// Check if a modal is already open to prevent duplicate modals
if ( if (
self.settings.defaultViewType === self.settings.defaultViewType ===
"modal" && "modal"
!self.modal // Only create a new modal if one doesn't exist
) { ) {
self.initModal( self.initModal(
{ {
@ -723,10 +713,7 @@ export default class FloatSearchPlugin extends Plugin {
true, true,
false false
); );
} else if ( } else {
self.settings.defaultViewType !==
"modal"
) {
initSearchViewWithLeaf( initSearchViewWithLeaf(
self.app, self.app,
self.settings.defaultViewType, self.settings.defaultViewType,
@ -902,28 +889,11 @@ export default class FloatSearchPlugin extends Plugin {
if (!checking) { if (!checking) {
const currentFile = activeLeaf.view.file; const currentFile = activeLeaf.view.file;
const query = options.queryBuilder(currentFile); const query = options.queryBuilder(currentFile);
this.initModal(
// Check if a modal is already open to prevent duplicate modals { ...this.state, query, current: true },
if (!this.modal) { true,
this.initModal( false
{ ...this.state, query, current: true }, );
true,
false
);
} else {
// If a modal is already open, focus it and set the query
const inputEl =
this.modal.contentEl.querySelector("input");
if (inputEl) {
inputEl.value = query;
inputEl.focus();
// Trigger search with the new query
const event = new Event("input", {
bubbles: true,
});
inputEl.dispatchEvent(event);
}
}
} }
return true; return true;
} }
@ -943,49 +913,23 @@ export default class FloatSearchPlugin extends Plugin {
this.addCommand({ this.addCommand({
id: "search-obsidian-globally", id: "search-obsidian-globally",
name: "Search obsidian globally", name: "Search obsidian globally",
callback: () => { callback: () =>
// Check if a modal is already open to prevent duplicate modals this.initModal(
if (!this.modal) { { ...this.state, query: "", current: false },
this.initModal( false,
{ ...this.state, query: "", current: false }, true
false, ),
true
);
} else {
// If a modal is already open, focus it and clear the query
const inputEl = this.modal.contentEl.querySelector("input");
if (inputEl) {
inputEl.value = "";
inputEl.focus();
}
}
},
}); });
this.addCommand({ this.addCommand({
id: "search-obsidian-globally-state", id: "search-obsidian-globally-state",
name: "Search Obsidian Globally (With Last State)", name: "Search Obsidian Globally (With Last State)",
callback: () => { callback: () =>
// Check if a modal is already open to prevent duplicate modals this.initModal(
if (!this.modal) { { ...this.state, query: this.state.query, current: false },
this.initModal( true,
{ false
...this.state, ),
query: this.state.query,
current: false,
},
true,
false
);
} else {
// If a modal is already open, focus it and set the query to the last state
const inputEl = this.modal.contentEl.querySelector("input");
if (inputEl) {
inputEl.value = this.state.query || "";
inputEl.focus();
}
}
},
}); });
this.createCommand({ this.createCommand({
@ -1041,10 +985,10 @@ export default class FloatSearchPlugin extends Plugin {
isExistingLeaf.setViewState({ isExistingLeaf.setViewState({
type: "search", type: "search",
active: true, active: true,
state: this.state as Record< state: {
string, ...this.state,
unknown triggerBySelf: true,
>, } as Record<string, unknown>,
}); });
return; return;
} }
@ -1478,7 +1422,7 @@ class FloatSearchModal extends Modal {
: "preview", : "preview",
}; };
this.fileLeaf.setViewState(estate, { this.fileLeaf.setViewState(estate, {
focus: !0, focus: true,
}); });
setTimeout(() => { setTimeout(() => {
( (