Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicoptima committed Jul 24, 2023
1 parent f95d048 commit 3f4e4f6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "loom",
"name": "Loom",
"version": "1.16.0",
"version": "1.16.1",
"minAppVersion": "0.15.0",
"description": "Loom in Obsidian",
"author": "celeste",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-loom",
"version": "1.16.0",
"version": "1.16.1",
"description": "Loom in Obsidian",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ body {
display: inline-flex !important;
margin-left: -0.025em;
padding: 0.1em 0.075em;
border-radius: 0.2em;
border-radius: 0.25em;
--icon-size: 1.1rem;
}
.loom__node-button:hover {
Expand Down
118 changes: 66 additions & 52 deletions views.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoomSettings, NoteState } from "./common";
import { LoomSettings, Node, NoteState } from "./common";
import { App, ItemView, Menu, Modal, Setting, WorkspaceLeaf, setIcon } from "obsidian";
import { Range } from "@codemirror/state";
import {
Expand All @@ -11,6 +11,61 @@ import {
} from "@codemirror/view";
const dialog = require("electron").remote.dialog;

interface MenuContext {
app: App;
event: MouseEvent;
state: NoteState;
id: string;
node: Node;
deletable: boolean;
}

const showNodeMenu = ({ app, event, state, id, node, deletable }: MenuContext) => {
const menu = new Menu();

const menuItem = (name: string, icon: string, callback: () => void) =>
menu.addItem((item) => {
item.setTitle(name);
item.setIcon(icon);
item.onClick(callback);
});

const zeroArgMenuItem = (name: string, icon: string, event: string) =>
menuItem(name, icon, () => app.workspace.trigger(event));
const selfArgMenuItem = (name: string, icon: string, event: string) =>
menuItem(name, icon, () => app.workspace.trigger(event, id));

if (state.hoisted[state.hoisted.length - 1] === id)
zeroArgMenuItem("Unhoist", "arrow-down", "loom:unhoist");
else
selfArgMenuItem("Hoist", "arrow-up", "loom:hoist");

if (node.bookmarked)
selfArgMenuItem("Remove bookmark", "bookmark-minus", "loom:toggle-bookmark");
else
selfArgMenuItem("Bookmark", "bookmark", "loom:toggle-bookmark");

menu.addSeparator();
selfArgMenuItem("Create child", "plus", "loom:create-child");
selfArgMenuItem("Create sibling", "list-plus", "loom:create-sibling");

menu.addSeparator();
selfArgMenuItem("Delete all children", "x", "loom:clear-children");
selfArgMenuItem("Delete all siblings", "list-x", "loom:clear-siblings");

if (node.parentId !== null) {
menu.addSeparator();
selfArgMenuItem("Merge with parent", "arrow-up-left", "loom:merge-with-parent");
}

if (deletable) {
menu.addSeparator();
selfArgMenuItem("Delete", "trash", "loom:delete");
}

menu.showAtMouseEvent(event);
}

export class LoomView extends ItemView {
getNoteState: () => NoteState | null;
getSettings: () => LoomSettings;
Expand Down Expand Up @@ -345,62 +400,13 @@ export class LoomView extends ItemView {
this.app.workspace.trigger("loom:switch-to", id)
);

// define `showMenu`,
// which is triggered by pressing the menu button or right-clicking the node

const rootNodes = Object.entries(state.nodes)
.filter(([, node]) => node.parentId === null)
const deletable = rootNodes.length !== 1 || rootNodes[0][0] !== id;

const showMenu = (event: MouseEvent) => {
const menu = new Menu();

const menuItem = (name: string, icon: string, callback: () => void) =>
menu.addItem((item) => {
item.setTitle(name);
item.setIcon(icon);
item.onClick(callback);
});

const zeroArgMenuItem = (name: string, icon: string, event: string) =>
menuItem(name, icon, () => this.app.workspace.trigger(event));
const selfArgMenuItem = (name: string, icon: string, event: string) =>
menuItem(name, icon, () => this.app.workspace.trigger(event, id));

if (state.hoisted[state.hoisted.length - 1] === id)
zeroArgMenuItem("Unhoist", "arrow-down", "loom:unhoist");
else
selfArgMenuItem("Hoist", "arrow-up", "loom:hoist");

if (node.bookmarked)
selfArgMenuItem("Remove bookmark", "bookmark-minus", "loom:toggle-bookmark");
else
selfArgMenuItem("Bookmark", "bookmark", "loom:toggle-bookmark");

menu.addSeparator();
selfArgMenuItem("Create child", "plus", "loom:create-child");
selfArgMenuItem("Create sibling", "list-plus", "loom:create-sibling");

menu.addSeparator();
selfArgMenuItem("Delete all children", "x", "loom:clear-children");
selfArgMenuItem("Delete all siblings", "list-x", "loom:clear-siblings");

if (node.parentId !== null) {
menu.addSeparator();
selfArgMenuItem("Merge with parent", "arrow-up-left", "loom:merge-with-parent");
}

if (deletable) {
menu.addSeparator();
selfArgMenuItem("Delete", "trash", "loom:delete");
}

menu.showAtMouseEvent(event);
}

nodeContainer.addEventListener("contextmenu", (event) => {
event.preventDefault();
showMenu(event);
showNodeMenu({ app: this.app, event, state, id, node, deletable });
});

// add buttons on hover
Expand All @@ -418,7 +424,7 @@ export class LoomView extends ItemView {
button_.addEventListener("click", callback);
};

button("Show menu", "menu", showMenu);
button("Show menu", "menu", (event) => showNodeMenu({ app: this.app, event, state, id, node, deletable }));

if (state.hoisted[state.hoisted.length - 1] === id)
button("Unhoist", "arrow-down", () => this.app.workspace.trigger("loom:unhoist"));
Expand Down Expand Up @@ -545,6 +551,14 @@ export class LoomSiblingsView extends ItemView {
this.app.workspace.trigger("loom:switch-to", id)
);

const rootNodes = Object.entries(state.nodes)
.filter(([, node]) => node.parentId === null)
const deletable = rootNodes.length !== 1 || rootNodes[0][0] !== id;
nodeContainer.addEventListener("contextmenu", (event) => {
event.preventDefault();
showNodeMenu({ app: this.app, event, state, id, node, deletable });
});

if (parseInt(i) !== siblings.length - 1)
container.createEl("hr", { cls: "loom__sibling-separator" });

Expand Down

0 comments on commit 3f4e4f6

Please sign in to comment.