feat: support toggle file path visibility

This commit is contained in:
Quorafind 2024-04-09 15:13:48 +08:00
parent 196e3711be
commit e5480ead33
2 changed files with 914 additions and 797 deletions

View file

@ -4,7 +4,7 @@ import {
Editor, ExtraButtonComponent,
Menu, MenuItem,
Modal, OpenViewState, PaneType,
Plugin, SearchView, setIcon,
Plugin, SearchView, setIcon, Setting,
TAbstractFile,
TFile, ViewStateResult,
Workspace,
@ -41,6 +41,7 @@ interface searchState {
interface FloatSearchSettings {
searchViewState: searchState;
showFilePath: boolean;
}
@ -52,8 +53,9 @@ const DEFAULT_SETTINGS: FloatSearchSettings = {
matchingCase: false,
query: "",
sortOrder: "alphabetical",
}
}
},
showFilePath: false,
};
const allViews: viewType[] = [{
type: "modal",
@ -84,7 +86,7 @@ const initSearchViewWithLeaf = async (app: App, type: PaneType | 'sidebar', stat
const inputEl = leaf.containerEl.getElementsByTagName("input")[0];
inputEl.focus();
}, 0);
}
};
export default class FloatSearchPlugin extends Plugin {
settings: FloatSearchSettings;
@ -94,6 +96,8 @@ export default class FloatSearchPlugin extends Plugin {
private applyStateDebounceTimer = 0;
private applySettingsDebounceTimer = 0;
patchedDomChildren = false;
public applySettingsUpdate() {
clearTimeout(this.applySettingsDebounceTimer);
@ -120,6 +124,7 @@ export default class FloatSearchPlugin extends Plugin {
this.patchWorkspace();
this.patchWorkspaceLeaf();
this.patchSearchView();
this.patchVchildren();
this.registerObsidianURIHandler();
this.registerObsidianCommands();
@ -127,6 +132,7 @@ export default class FloatSearchPlugin extends Plugin {
this.registerContextMenuHandler();
this.addRibbonIcon('search', 'Search Obsidian In Modal', () => this.initModal(this.state, true, true));
this.updateFilePathVisibility();
}
onunload() {
@ -134,8 +140,20 @@ export default class FloatSearchPlugin extends Plugin {
this.modal?.close();
}
updateFilePathVisibility() {
const {showFilePath} = this.settings;
console.log(showFilePath);
document.body.toggleClass("show-file-path", showFilePath);
}
changeFilePathVisibility() {
this.settings.showFilePath = !this.settings.showFilePath;
this.updateFilePathVisibility();
this.applySettingsUpdate();
}
registerIcons() {
addIcon('panel-left-inactive', `<path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 4.999687 3 L 19.000312 3 C 20.104688 3 21 3.895312 21 4.999687 L 21 19.000312 C 21 20.104688 20.104688 21 19.000312 21 L 4.999687 21 C 3.895312 21 3 20.104688 3 19.000312 L 3 4.999687 C 3 3.895312 3.895312 3 4.999687 3 Z M 4.999687 3 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 13.999688 L 9 15 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 19.000312 L 9 21 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 3 L 9 4.999687 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 9 L 9 10.000312 " transform="matrix(4.166667,0,0,4.166667,0,0)"/>`)
addIcon('panel-left-inactive', `<path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 4.999687 3 L 19.000312 3 C 20.104688 3 21 3.895312 21 4.999687 L 21 19.000312 C 21 20.104688 20.104688 21 19.000312 21 L 4.999687 21 C 3.895312 21 3 20.104688 3 19.000312 L 3 4.999687 C 3 3.895312 3.895312 3 4.999687 3 Z M 4.999687 3 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 13.999688 L 9 15 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 19.000312 L 9 21 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 3 L 9 4.999687 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 9 9 L 9 10.000312 " transform="matrix(4.166667,0,0,4.166667,0,0)"/>`);
addIcon('app-window', `<path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 4.000312 4.000312 L 19.999688 4.000312 C 21.105 4.000312 22.000312 4.895625 22.000312 6 L 22.000312 18 C 22.000312 19.104375 21.105 19.999688 19.999688 19.999688 L 4.000312 19.999688 C 2.895 19.999688 1.999687 19.104375 1.999687 18 L 1.999687 6 C 1.999687 4.895625 2.895 4.000312 4.000312 4.000312 Z M 4.000312 4.000312 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 10.000312 4.000312 L 10.000312 7.999687 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 1.999687 7.999687 L 22.000312 7.999687 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 6 4.000312 L 6 7.999687 " transform="matrix(4.166667,0,0,4.166667,0,0)"/>`);
addIcon('panel-top', `<path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 4.999687 3 L 19.000312 3 C 20.104688 3 21 3.895312 21 4.999687 L 21 19.000312 C 21 20.104688 20.104688 21 19.000312 21 L 4.999687 21 C 3.895312 21 3 20.104688 3 19.000312 L 3 4.999687 C 3 3.895312 3.895312 3 4.999687 3 Z M 4.999687 3 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 3 9 L 21 9 " transform="matrix(4.166667,0,0,4.166667,0,0)"/>`);
addIcon('square-equal', `<path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 4.999687 3 L 19.000312 3 C 20.104688 3 21 3.895312 21 4.999687 L 21 19.000312 C 21 20.104688 20.104688 21 19.000312 21 L 4.999687 21 C 3.895312 21 3 20.104688 3 19.000312 L 3 4.999687 C 3 3.895312 3.895312 3 4.999687 3 Z M 4.999687 3 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 7.000312 10.000312 L 16.999688 10.000312 " transform="matrix(4.166667,0,0,4.166667,0,0)"/><path style="fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke:currentColor;stroke-opacity:1;stroke-miterlimit:4;" d="M 7.000312 13.999688 L 16.999688 13.999688 " transform="matrix(4.166667,0,0,4.166667,0,0)"/>`);
@ -242,7 +260,7 @@ export default class FloatSearchPlugin extends Plugin {
return function (pinned: boolean) {
old.call(this, pinned);
if (isEmebeddedLeaf(this) && !pinned) this.setPinned(true);
}
};
},
openFile(old) {
return function (file: TFile, openState?: OpenViewState) {
@ -296,7 +314,7 @@ export default class FloatSearchPlugin extends Plugin {
}, 1);
return view;
}
};
},
}),
);
@ -307,7 +325,7 @@ export default class FloatSearchPlugin extends Plugin {
this.state = state;
this.settings.searchViewState = state as searchState;
this.applySettingsUpdate();
}
};
const checkCurrentViewType = (leaf: WorkspaceLeaf) => {
const isModal = document.querySelector('.float-search-modal') !== null;
@ -315,7 +333,7 @@ export default class FloatSearchPlugin extends Plugin {
if (currentLeafRoot?.side && (currentLeafRoot?.side === "left" || currentLeafRoot?.side === 'right')) return "sidebar";
if (leaf.getContainer()?.type === "window") return "window";
return isModal ? "modal" : "split";
}
};
const initViewMenu = (menu: Menu, current: searchType, originLeaf?: WorkspaceLeaf) => {
menu.dom.toggleClass("float-search-view-menu", true);
@ -356,7 +374,7 @@ export default class FloatSearchPlugin extends Plugin {
});
}
return menu;
}
};
const patchSearch = () => {
const searchView = this.app.workspace.getLeavesOfType("search")[0]?.view as any;
@ -364,6 +382,7 @@ export default class FloatSearchPlugin extends Plugin {
if (!searchView) return false;
const searchViewConstructor = searchView.constructor;
const self = this;
this.register(
around(searchViewConstructor.prototype, {
@ -381,7 +400,18 @@ export default class FloatSearchPlugin extends Plugin {
layoutMenu.showAtPosition({x: viewSwitchButtonPos.x, y: viewSwitchButtonPos.y + 30});
});
targetEl.parentElement.insertBefore(viewSwitchEl, targetEl);
if (!this.hidePathToggle) {
this.hidePathToggle = new Setting(this.searchParamsContainerEl).setName('Show file path').addToggle((toggle) => {
toggle.toggleEl.toggleClass('mod-small', true);
toggle.setValue(self.settings.showFilePath).onChange(async (value) => {
self.settings.showFilePath = !value;
self.changeFilePathVisibility();
self.applySettingsUpdate();
});
});
}
};
},
startSearch(old) {
return function () {
@ -391,7 +421,7 @@ export default class FloatSearchPlugin extends Plugin {
...viewState,
query: this.searchComponent.getValue(),
});
}
};
}
})
);
@ -409,6 +439,52 @@ export default class FloatSearchPlugin extends Plugin {
});
}
patchVchildren() {
const patchSearchDom = () => {
const searchView = this.app.workspace.getLeavesOfType("search")[0]?.view as any;
if (!searchView) return false;
const dom = searchView.dom.constructor;
const self = this;
this.register(
around(dom.prototype, {
stopLoader(old) {
return function () {
old.call(this);
// console.log(this?.vChildren?.children);
this?.vChildren?.children?.forEach((child: any) => {
if (child?.file && !child?.pathEl) {
const path = (child?.file.parent?.path) || '/';
const pathEl = createDiv({cls: "search-result-file-path"});
const pathIconEl = pathEl.createDiv({cls: "search-result-file-path-icon"});
setIcon(pathIconEl, "folder");
const pathTextEl = pathEl.createDiv({
cls: "search-result-file-path-text",
text: path
});
child.pathEl = pathEl;
const titleEl = child.containerEl.find('.search-result-file-title');
titleEl.prepend(pathEl);
}
});
};
}
})
);
return true;
};
this.app.workspace.onLayoutReady(() => {
if (!patchSearchDom()) {
const evt = this.app.workspace.on("layout-change", () => {
patchSearchDom() && this.app.workspace.offref(evt);
});
this.registerEvent(evt);
}
});
}
registerObsidianURIHandler() {
this.registerObsidianProtocolHandler("fs", (path) => this.initModal({
...this.state,
@ -443,23 +519,32 @@ export default class FloatSearchPlugin extends Plugin {
}
registerObsidianCommands() {
this.addCommand({
id: 'show-or-hide-file-path',
name: 'Show/hide file path',
callback: () => {
this.changeFilePathVisibility();
}
});
this.addCommand({
id: 'search-obsidian-globally',
name: 'Search Obsidian Globally',
name: 'Search obsidian globally',
callback: () => this.initModal({...this.state, query: "", current: false}, false, true)
});
this.addCommand({
id: 'search-obsidian-globally-state',
name: 'Search Obsidian Globally (With Last State)',
name: 'Search obsidian globally (with last state)',
callback: () => this.initModal({...this.state, query: "", current: false}, true, false)
});
this.createCommand({
id: 'search-in-backlink',
name: 'Search In Backlink Of Current File',
name: 'Search in backlink Of current file',
queryBuilder: (file) => {
return " /\\[\\[" + (file.extension === "canvas" ? file.name : file.basename) + "(\\|[^\\]]*)?\\]\\]/";
}
@ -467,7 +552,7 @@ export default class FloatSearchPlugin extends Plugin {
this.createCommand({
id: 'search-in-current-file',
name: 'Search In Current File',
name: 'Search in current file',
queryBuilder: (file) => {
return " path:" + `"${file.path}"`;
}
@ -476,7 +561,7 @@ export default class FloatSearchPlugin extends Plugin {
for (const type of ['split', 'tab', 'window'] as PaneType[]) {
this.addCommand({
id: `open-search-view-${type}`,
name: `Open Search View (${type})`,
name: `Open search view (${type})`,
callback: async () => initSearchViewWithLeaf(this.app, type)
});
}
@ -503,9 +588,9 @@ export default class FloatSearchPlugin extends Plugin {
menu.addItem((item) => {
// Add sub menu
item.setTitle('Search "' + searchWord + '"' + " in Float Search").setIcon("search")
.onClick(() => this.initModal({...this.state, query: selection, current: false}, true, false))
})
}))
.onClick(() => this.initModal({...this.state, query: selection, current: false}, true, false));
});
}));
}
registerContextMenuHandler() {
@ -771,7 +856,7 @@ class FloatSearchModal extends Modal {
break;
}
}
};
}
initContent() {
@ -825,7 +910,7 @@ class FloatSearchModal extends Modal {
return;
}
}
};
}
async initFileView(file: TFile, state: any) {
@ -870,7 +955,7 @@ class FloatSearchModal extends Modal {
(this.searchLeaf.view as SearchView).searchComponent.inputEl.focus();
}
}
};
if (!this.fileEl) return;

View file

@ -111,3 +111,35 @@ If your plugin does not need CSS, delete this file.
stroke-linecap: round;
stroke-linejoin: round;
}
body:not(.show-file-path) .search-result-file-title .search-result-file-path {
display: none;
}
.show-file-path .search-result-file-title .search-result-file-path {
display: flex;
justify-content: center;
align-items: center;
color: var(--text-muted);
font-size: var(--font-ui-smaller);
font-weight: var(--font-bolder);
margin-right: var(--size-2-2);
padding: 1px;
padding-left: var(--size-4-2);
background-color: var(--background-secondary-alt);
padding-right: var(--size-4-2);
border-radius: var(--radius-s);
--icon-size: 12px;
}
.show-file-path .search-result-file-title .search-result-file-path-icon {
display: flex;
align-items: center;
justify-content: center;
margin-right: var(--size-2-2);
}
.show-file-path .search-result-file-title .search-result-file-path:hover {
background-color: var(--background-secondary);
}