Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cycsd committed Aug 1, 2023
2 parents c8e0f99 + 87ff4fe commit 0145deb
Show file tree
Hide file tree
Showing 16 changed files with 581 additions and 409 deletions.
3 changes: 1 addition & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"id": "excalibrain",
"name": "ExcaliBrain",
"version": "0.2.5",
"version": "0.2.6",
"minAppVersion": "1.1.6",
"description": "A clean, intuitive and editable graph view for Obsidian",
"author": "Zsolt Viczian",
"authorUrl": "https://zsolt.blog",
"fundingUrl": "https://ko-fi.com/zsolt",
"isDesktopOnly": false
}
205 changes: 205 additions & 0 deletions src/Components/AddToOntologyModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import { create } from "domain";
import { App, Modal, Notice, Setting } from "obsidian";
import { createBinaryOps } from "obsidian-dataview/lib/expression/binaryop";
import ExcaliBrain from "src/excalibrain-main";
import { t } from "src/lang/helpers";

export enum Ontology {
Parent = "parent",
Child = "child",
LeftFriend = "leftFriend",
RightFriend = "rightFriend",
Previous = "previous",
Next = "next",
}

export class AddToOntologyModal extends Modal {
private ontology:Ontology|null = null;
private fieldName:string;
constructor(
app: App,
private plugin: ExcaliBrain,
) {
super(app);
}

private getCurrentOntology():Ontology|null {
const { settings } = this.plugin;
const field = this.fieldName;

if(settings.hierarchy.parents.includes(field)) {
return Ontology.Parent;
}
if(settings.hierarchy.children.includes(field)) {
return Ontology.Child;
}
if(settings.hierarchy.leftFriends.includes(field)) {
return Ontology.LeftFriend;
}
if(settings.hierarchy.rightFriends.includes(field)) {
return Ontology.RightFriend;
}
if(settings.hierarchy.previous.includes(field)) {
return Ontology.Previous;
}
if(settings.hierarchy.next.includes(field)) {
return Ontology.Next;
}

return null;
}

private async setOntology(ontology:Ontology) {
if(this.ontology === ontology) return;
const { settings } = this.plugin;
const plugin = this.plugin;

//remove from current ontology
switch(this.ontology) {
case Ontology.Parent:
settings.hierarchy.parents = settings.hierarchy.parents.filter(f=>f!==this.fieldName);
plugin.hierarchyLowerCase.parents = [];
settings.hierarchy.parents.forEach(f=>plugin.hierarchyLowerCase.parents.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.Child:
settings.hierarchy.children = settings.hierarchy.children.filter(f=>f!==this.fieldName);
plugin.hierarchyLowerCase.children = [];
settings.hierarchy.children.forEach(f=>plugin.hierarchyLowerCase.children.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.LeftFriend:
settings.hierarchy.leftFriends = settings.hierarchy.leftFriends.filter(f=>f!==this.fieldName);
plugin.hierarchyLowerCase.leftFriends = [];
settings.hierarchy.leftFriends.forEach(f=>plugin.hierarchyLowerCase.leftFriends.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.RightFriend:
settings.hierarchy.rightFriends = settings.hierarchy.rightFriends.filter(f=>f!==this.fieldName);
plugin.hierarchyLowerCase.rightFriends = [];
settings.hierarchy.rightFriends.forEach(f=>plugin.hierarchyLowerCase.rightFriends.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.Previous:
settings.hierarchy.previous = settings.hierarchy.previous.filter(f=>f!==this.fieldName);
plugin.hierarchyLowerCase.previous = [];
settings.hierarchy.previous.forEach(f=>plugin.hierarchyLowerCase.previous.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.Next:
settings.hierarchy.next = settings.hierarchy.next.filter(f=>f!==this.fieldName);
plugin.hierarchyLowerCase.next = [];
settings.hierarchy.next.forEach(f=>plugin.hierarchyLowerCase.next.push(f.toLowerCase().replaceAll(" ","-")));
break;
}

//add to new ontology
switch(ontology) {
case Ontology.Parent:
settings.hierarchy.parents.push(this.fieldName);
settings.hierarchy.parents = settings.hierarchy.parents.sort((a,b)=>a.toLowerCase()<b.toLowerCase()?-1:1);
plugin.hierarchyLowerCase.parents = [];
settings.hierarchy.parents.forEach(f=>plugin.hierarchyLowerCase.parents.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.Child:
settings.hierarchy.children.push(this.fieldName);
settings.hierarchy.children = settings.hierarchy.children.sort((a,b)=>a.toLowerCase()<b.toLowerCase()?-1:1);
plugin.hierarchyLowerCase.children = [];
settings.hierarchy.children.forEach(f=>plugin.hierarchyLowerCase.children.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.LeftFriend:
settings.hierarchy.leftFriends.push(this.fieldName);
settings.hierarchy.leftFriends = settings.hierarchy.leftFriends.sort((a,b)=>a.toLowerCase()<b.toLowerCase()?-1:1);
plugin.hierarchyLowerCase.leftFriends = [];
settings.hierarchy.leftFriends.forEach(f=>plugin.hierarchyLowerCase.leftFriends.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.RightFriend:
settings.hierarchy.rightFriends.push(this.fieldName);
settings.hierarchy.rightFriends = settings.hierarchy.rightFriends.sort((a,b)=>a.toLowerCase()<b.toLowerCase()?-1:1);
plugin.hierarchyLowerCase.rightFriends = [];
settings.hierarchy.rightFriends.forEach(f=>plugin.hierarchyLowerCase.rightFriends.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.Previous:
settings.hierarchy.previous.push(this.fieldName);
settings.hierarchy.previous = settings.hierarchy.previous.sort((a,b)=>a.toLowerCase()<b.toLowerCase()?-1:1);
plugin.hierarchyLowerCase.previous = [];
settings.hierarchy.previous.forEach(f=>plugin.hierarchyLowerCase.previous.push(f.toLowerCase().replaceAll(" ","-")));
break;
case Ontology.Next:
settings.hierarchy.next.push(this.fieldName);
settings.hierarchy.next = settings.hierarchy.next.sort((a,b)=>a.toLowerCase()<b.toLowerCase()?-1:1);
plugin.hierarchyLowerCase.next = [];
settings.hierarchy.next.forEach(f=>plugin.hierarchyLowerCase.next.push(f.toLowerCase().replaceAll(" ","-")));
break;
}
await this.plugin.saveSettings();
if (plugin.scene && !plugin.scene.terminated) {
plugin.scene.vaultFileChanged = true;
}
new Notice(`Added ${this.fieldName} as ${ontology}`);
this.fieldName = null;
this.close();
}

async show( fieldName:string ) {
await this.plugin.loadSettings();
this.fieldName = fieldName;
this.ontology = this.getCurrentOntology();
this.open();
}

public async addFieldToOntology(ontology:Ontology, fieldName:string) {
await this.plugin.loadSettings();
this.fieldName = fieldName;
this.ontology = this.getCurrentOntology();
await this.setOntology(ontology);
this.fieldName = null;
}

open () {
if(!this.fieldName) return;
const { contentEl, titleEl } = this;
titleEl.setText(this.fieldName);
contentEl.createEl("p", {text: t("ADD_TO_ONTOLOGY_MODAL_DESC")});
const setting = new Setting(contentEl)
.addButton((b) => {
b.buttonEl.style.flex = "1 0 calc(33.33% - var(--size-4-2))";
b.setButtonText(t("PARENTS_NAME"))
if(this.ontology === Ontology.Parent) b.setCta();
b.onClick(()=>this.setOntology(Ontology.Parent))
})
.addButton((b) => {
b.buttonEl.style.flex = "1 0 calc(33.33% - var(--size-4-2))";
b.setButtonText(t("CHILDREN_NAME"))
if(this.ontology === Ontology.Child) b.setCta();
b.onClick(()=>this.setOntology(Ontology.Child))
})
.addButton((b) => {
b.buttonEl.style.flex = "1 0 calc(33.33% - var(--size-4-2))";
b.setButtonText(t("LEFT_FRIENDS_NAME"))
if(this.ontology === Ontology.LeftFriend) b.setCta();
b.onClick(()=>this.setOntology(Ontology.LeftFriend))
})
.addButton((b) => {
b.buttonEl.style.flex = "1 0 calc(33.33% - var(--size-4-2))";
b.setButtonText(t("RIGHT_FRIENDS_NAME"))
if(this.ontology === Ontology.RightFriend) b.setCta();
b.onClick(()=>this.setOntology(Ontology.RightFriend))
})
.addButton((b) => {
b.buttonEl.style.flex = "1 0 calc(33.33% - var(--size-4-2))";
b.setButtonText(t("PREVIOUS_NAME"))
if(this.ontology === Ontology.Previous) b.setCta();
b.onClick(()=>this.setOntology(Ontology.Previous))
})
.addButton((b) => {
b.buttonEl.style.flex = "1 0 calc(33.33% - var(--size-4-2))";
b.setButtonText(t("NEXT_NAME"))
if(this.ontology === Ontology.Next) b.setCta();
b.onClick(()=>this.setOntology(Ontology.Next))
});
setting.controlEl.style.flexWrap = "wrap";
setting.controlEl.style.justifyContent = "space-between";
super.open();
}

onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
83 changes: 80 additions & 3 deletions src/Components/ToolsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import ExcaliBrain from "src/excalibrain-main";
import { splitFolderAndFilename } from "src/utils/fileUtils";
import { PageSuggest } from "../Suggesters/PageSuggester";
import { LinkTagFilter } from "./LinkTagFilter";
import { keepOnTop } from "src/utils/utils";
import { getIcon } from "obsidian";
import { addVerticalDivider } from "./VerticalDivider";

export class ToolsPanel {
private wrapperDiv: HTMLDivElement;
private buttons: ToggleButton[] = [];
private buttons: (ToggleButton|HTMLElement)[] = [];
public linkTagFilter: LinkTagFilter;
public searchElement: HTMLInputElement;

Expand Down Expand Up @@ -90,6 +90,62 @@ export class ToolsPanel {
},
));

addVerticalDivider(buttonsWrapperDiv);

//------------
//Navigate back
//------------
this.buttons.push(
new ToggleButton(
this.plugin,
()=>false,
(val:boolean)=>{},
buttonsWrapperDiv,
{
display: "<",
icon: getIcon("lucide-arrow-big-left").outerHTML,
tooltip: t("NAVIGATE_BACK")
},
false
)
)

//------------
//Navigate forward
//------------
this.buttons.push(
new ToggleButton(
this.plugin,
()=>false,
(val:boolean)=>{},
buttonsWrapperDiv,
{
display: ">",
icon: getIcon("lucide-arrow-big-right").outerHTML,
tooltip: t("NAVIGATE_FORWARD")
},
false
)
)

//------------
//Refresh view
//------------
this.buttons.push(
new ToggleButton(
this.plugin,
()=>false,
(val:boolean)=>this.plugin.scene.pinLeaf = val,
buttonsWrapperDiv,
{
display: "🔄",
icon: getIcon("lucide-refresh-cw").outerHTML,
tooltip: t("REFRESH_VIEW")
},
false
)
)

//------------
//Pin Leaf
//------------
Expand All @@ -108,6 +164,25 @@ export class ToolsPanel {
)
)

//------------
//Automatically open central node in leaf
//------------
this.buttons.push(
new ToggleButton(
this.plugin,
()=>this.plugin.settings.autoOpenCentralDocument,
(val:boolean)=>this.plugin.settings.autoOpenCentralDocument = val,
buttonsWrapperDiv,
{
display: "🔌",
icon: `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-unplug"><path d="m19 5 3-3"/><path d="m2 22 3-3"/><path d="M6.3 20.3a2.4 2.4 0 0 0 3.4 0L12 18l-6-6-2.3 2.3a2.4 2.4 0 0 0 0 3.4Z"/><path d="M7.5 13.5 10 11"/><path d="M10.5 16.5 13 14"/><path d="m12 6 6 6 2.3-2.3a2.4 2.4 0 0 0 0-3.4l-2.6-2.6a2.4 2.4 0 0 0-3.4 0Z"/></svg>`, //getIcon("lucide-unplug").outerHTML,
tooltip: t("AUTO_OPEN_DOCUMENT")
},
false
)
)

addVerticalDivider(buttonsWrapperDiv);
//------------
//Attachments
//------------
Expand Down Expand Up @@ -290,7 +365,9 @@ export class ToolsPanel {
}

rerender() {
this.buttons.forEach(b=>b.setColor());
this.buttons.forEach(b => {
if(b instanceof ToggleButton) b.setColor();
});
this.linkTagFilter.render();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Components/VerticalDivider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const addVerticalDivider = (containerEl: HTMLElement):HTMLElement => containerEl.createDiv({
cls: "excalibrain-toolspanel-divider"});
Loading

0 comments on commit 0145deb

Please sign in to comment.