Skip to content

Commit

Permalink
fix styling. add extra buttons. add copy
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingislost committed Feb 16, 2022
1 parent 93d289f commit d088ab1
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 49 deletions.
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-embedded-query-control",
"name": "Embedded Query Control",
"version": "0.0.2",
"version": "0.1.0",
"minAppVersion": "0.12.5",
"description": "An experimental Obsidian plugin that adds controls to embedded queries",
"author": "NothingIsLost",
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-embedded-query-control",
"name": "Embedded Query Control",
"version": "0.0.2",
"version": "0.1.0",
"minAppVersion": "0.12.5",
"description": "An experimental Obsidian plugin that adds controls to embedded queries",
"author": "NothingIsLost",
Expand Down
143 changes: 109 additions & 34 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component, Plugin, ViewCreator, WorkspaceLeaf } from "obsidian";
import { around } from "monkey-around";
import { Component, Modal, Plugin, ViewCreator, WorkspaceLeaf } from "obsidian";
import { DEFAULT_SETTINGS, EmbeddedQueryControlSettings, SettingTab } from "./settings";
import { translate } from "./utils";

export default class EmbeddedQueryControlPlugin extends Plugin {
SearchHeaderDom: any;
SearchResultsExport: any;
settings: EmbeddedQueryControlSettings;
settingsTab: SettingTab;

Expand All @@ -29,8 +31,20 @@ export default class EmbeddedQueryControlPlugin extends Plugin {
// @ts-ignore we need a leaf before any leafs exists in the workspace, so we create one from scratch
let leaf = new WorkspaceLeaf(plugin.app);
let searchView = viewCreator(leaf);
let uninstall = around(Modal.prototype, {
open(old: any) {
return function (...args: any[]) {
plugin.SearchResultsExport = this.constructor;
return;
};
},
});
searchView.onCopyResultsClick(new MouseEvent(null));
uninstall();
plugin.SearchHeaderDom = searchView.headerDom.constructor;
});
} else {
this.getSearchExport();
}
this.register(
(uninstall = around(Component.prototype, {
Expand Down Expand Up @@ -68,10 +82,25 @@ export default class EmbeddedQueryControlPlugin extends Plugin {
}

getSearchHeader() {
let searchHeader: any | undefined = this.app.workspace.getLeavesOfType("search")?.first()?.view?.headerDom;
let searchHeader: any = this.app.workspace.getLeavesOfType("search")?.first()?.view?.headerDom;
return searchHeader?.constructor;
}

getSearchExport() {
const plugin = this;
let searchView: any = this.app.workspace.getLeavesOfType("search")?.first()?.view;
let uninstall = around(Modal.prototype, {
open(old: any) {
return function (...args: any[]) {
plugin.SearchResultsExport = this.constructor;
return;
};
},
});
searchView?.onCopyResultsClick(new MouseEvent(null));
uninstall();
}

onunload(): void {}

patchSearchView(EmbeddedQuery: any, EmbeddedQueryDOM: any) {
Expand All @@ -82,39 +111,85 @@ export default class EmbeddedQueryControlPlugin extends Plugin {
return function (...args: any[]) {
try {
if (!this.patched) {
if (!this.el?.parentElement?.parentElement?.hasClass("cm-preview-code-block")) return;
this.patched = true;
this.setCollapseAll = function (value: boolean) {
this.collapseAll = value;
this.children.forEach(child => {
child.setCollapse(value, false);
if (this.el?.closest(".internal-query")) {
let defaultHeaderEl = this.el.parentElement.querySelector(".internal-query-header");
this.patched = true;
this.setExtraContext = function (value: boolean) {
this.extraContext = value;
this.extraContextButtonEl.toggleClass("is-active", value);
this.children.forEach(child => {
child.setExtraContext(value);
});
this.infinityScroll.invalidateAll();
};
this.setTitleDisplay = function (value: boolean) {
this.showTitle = value;
this.showTitleButtonEl.toggleClass("is-active", value);
defaultHeaderEl.toggleClass("is-hidden", value);
};
this.setResultsDisplay = function (value: boolean) {
this.showResults = value;
this.showResultsButtonEl.toggleClass("is-active", value);
this.el.toggleClass("is-hidden", value);
};
this.setCollapseAll = function (value: boolean) {
this.collapseAllButtonEl.toggleClass("is-active", value);
this.collapseAll = value;
this.children.forEach(child => {
child.setCollapse(value, false);
});
this.infinityScroll.invalidateAll();
};
this.setSortOrder = (sortType: string) => {
this.sortOrder = sortType;
this.changed();
};
this.onCopyResultsClick = event => {
event.preventDefault();
new plugin.SearchResultsExport(this.app, this).open();
};
let SearchHeaderDOM = plugin.SearchHeaderDom ? plugin.SearchHeaderDom : plugin.getSearchHeader();
let headerDom = (this.headerDom = new SearchHeaderDOM(this.app, this.el.parentElement));
defaultHeaderEl.insertAdjacentElement("afterend", headerDom.navHeaderEl);
this.collapseAllButtonEl = headerDom.addNavButton(
"bullet-list",
translate("plugins.search.label-collapse-results"),
() => {
return this.setCollapseAll(!this.collapseAll);
}
);
this.extraContextButtonEl = headerDom.addNavButton(
"expand-vertically",
translate("plugins.search.label-more-context"),
() => {
return this.setExtraContext(!this.extraContext);
}
);
headerDom.addSortButton(
(sortType: string) => {
return this.setSortOrder(sortType);
},
() => {
return this.sortOrder;
}
);
this.showTitleButtonEl = headerDom.addNavButton("heading-glyph", "Hide Query Title", () => {
return this.setTitleDisplay(!this.showTitle);
});
this.showResultsButtonEl = headerDom.addNavButton("lines-of-text", "Hide Query Results", () => {
return this.setResultsDisplay(!this.showResults);
});
this.infinityScroll.invalidateAll();
};
this.setSortOrder = (sortType: string) => {
this.sortOrder = sortType;
this.changed();
};
let SearchHeaderDOM = plugin.SearchHeaderDom ? plugin.SearchHeaderDom : plugin.getSearchHeader();
let headerDom = (this.headerDom = new SearchHeaderDOM(this.app, this.el.parentElement));
this.el.parentElement.prepend(headerDom.navHeaderEl);
this.collapseAllButtonEl = headerDom.addNavButton("bullet-list", "Collapse results", () => {
return this.setCollapseAll(!this.collapseAll);
});
this.extraContextButtonEl = headerDom.addNavButton("expand-vertically", "Show more context", () => {
return this.setExtraContext(!this.extraContext);
});
headerDom.addSortButton(
(sortType: string) => {
return this.setSortOrder(sortType);
},
() => {
return this.sortOrder;
}
);
this.extraContext = plugin.settings.defaultShowContext;
this.sortOrder = plugin.settings.defaultSortOrder;
this.collapseAll = plugin.settings.defaultCollapse;
headerDom.addNavButton(
"documents",
translate("plugins.search.label-copy-search-results"),
this.onCopyResultsClick.bind(this)
);
this.setExtraContext(plugin.settings.defaultShowContext);
this.sortOrder = plugin.settings.defaultSortOrder;
this.setCollapseAll(plugin.settings.defaultCollapse);
this.setTitleDisplay(plugin.settings.defaultHideResults);
this.setResultsDisplay(plugin.settings.defaultHideResults);
}
}
} catch (err) {
console.log(err);
Expand Down
33 changes: 25 additions & 8 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import EmbeddedQueryControlPlugin from "./main";
import { translate } from "./utils";

export interface EmbeddedQueryControlSettings {
defaultCollapse: boolean;
defaultShowContext: boolean;
defaultHideTitle: boolean;
defaultHideResults: boolean;
defaultSortOrder: string;
}

export const DEFAULT_SETTINGS: EmbeddedQueryControlSettings = {
defaultCollapse: false,
defaultShowContext: false,
defaultHideTitle: false,
defaultHideResults: false,
defaultSortOrder: "alphabetical",
};

Expand Down Expand Up @@ -42,14 +47,28 @@ export class SettingTab extends PluginSettingTab {
})
);

new Setting(containerEl).setName("Hide query title by default").addToggle(toggle =>
toggle.setValue(this.plugin.settings.defaultHideTitle).onChange(value => {
this.plugin.settings.defaultHideTitle = value;
this.plugin.saveSettings();
})
);

new Setting(containerEl).setName("Hide query results by default").addToggle(toggle =>
toggle.setValue(this.plugin.settings.defaultHideResults).onChange(value => {
this.plugin.settings.defaultHideResults = value;
this.plugin.saveSettings();
})
);

new Setting(containerEl).setName("Default query result sort order").addDropdown(cb => {
cb.addOptions({
alphabetical: Translate("plugins.file-explorer.label-sort-a-to-z"),
alphabeticalReverse: Translate("plugins.file-explorer.label-sort-z-to-a"),
byModifiedTime: Translate("plugins.file-explorer.label-sort-new-to-old"),
byModifiedTimeReverse: Translate("plugins.file-explorer.label-sort-old-to-new"),
byCreatedTime: Translate("plugins.file-explorer.label-sort-created-new-to-old"),
byCreatedTimeReverse: Translate("plugins.file-explorer.label-sort-created-old-to-new"),
alphabetical: translate("plugins.file-explorer.label-sort-a-to-z"),
alphabeticalReverse: translate("plugins.file-explorer.label-sort-z-to-a"),
byModifiedTime: translate("plugins.file-explorer.label-sort-new-to-old"),
byModifiedTimeReverse: translate("plugins.file-explorer.label-sort-old-to-new"),
byCreatedTime: translate("plugins.file-explorer.label-sort-created-new-to-old"),
byCreatedTimeReverse: translate("plugins.file-explorer.label-sort-created-old-to-new"),
});
cb.setValue(this.plugin.settings.defaultSortOrder);
cb.onChange(async value => {
Expand All @@ -59,5 +78,3 @@ export class SettingTab extends PluginSettingTab {
});
}
}

const Translate = i18next.t.bind(i18next);
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const translate = i18next.t.bind(i18next);
14 changes: 9 additions & 5 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
.mod-root .workspace-leaf-content[data-type="markdown"] .cm-preview-code-block .nav-header {
margin-top: 0;
.workspace-leaf-content[data-type="markdown"] .internal-query .nav-header {
margin-top: 0; /* works around issues with minimal theme */
}

.mod-root .workspace-leaf-content[data-type="markdown"] .cm-preview-code-block .nav-header .nav-buttons-container {
left: 0;
position: absolute;
.workspace-leaf-content[data-type="markdown"] .internal-query .nav-header .nav-buttons-container {
position: unset; /* works around issues with minimal theme */
justify-content: center; /* works around issues with minimal theme */
}

.workspace-leaf-content[data-type="markdown"] .internal-query .is-hidden {
display: none;
}

0 comments on commit d088ab1

Please sign in to comment.