Skip to content

Commit

Permalink
feat: support disable navigate or create float
Browse files Browse the repository at this point in the history
  • Loading branch information
Quorafind committed Jan 18, 2024
1 parent f6b2396 commit 0928590
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 78 deletions.
69 changes: 44 additions & 25 deletions src/canvasMindMap.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Canvas, CanvasEdge, CanvasNode, ItemView, Plugin, requireApiVersion, SettingTab, TFile } from 'obsidian';
import { around } from "monkey-around";
import { addEdge, addNode, buildTrees, createChildFileNode, random } from "./utils";
import { DEFAULT_SETTINGS, MindMapSettings } from "./mindMapSettings";
import { DEFAULT_SETTINGS, MindMapSettings, MindMapSettingTab } from "./mindMapSettings";
import { CanvasEdgeData } from "obsidian/canvas";


export default class CanvasMindMap extends Plugin {
settings: MindMapSettings;
settingTab: MindMapSettingTab;


async onload() {
await this.registerSettings();
this.registerCommands();
this.patchCanvas();
this.patchMarkdownFileInfo();
Expand All @@ -20,6 +22,12 @@ export default class CanvasMindMap extends Plugin {

}

async registerSettings() {
this.settingTab = new MindMapSettingTab(this.app, this);
this.addSettingTab(this.settingTab);
await this.loadSettings();
}

registerCommands() {
this.addCommand({
id: 'split-heading-into-mindmap',
Expand Down Expand Up @@ -131,6 +139,9 @@ export default class CanvasMindMap extends Plugin {
const currentSelection = canvas.selection;
if (currentSelection.size !== 1) return;

// Check if the selected node is editing
if (currentSelection.values().next().value.isEditing) return;

const selectedItem = currentSelection.values().next().value as CanvasNode;
const viewportNodes = canvas.getViewportNodes();
const {x, y, width, height} = selectedItem;
Expand Down Expand Up @@ -164,6 +175,8 @@ export default class CanvasMindMap extends Plugin {
let selection = canvas.selection;

if (selection.size !== 1) return;
// Check if the selected node is editing
if (selection.values().next().value.isEditing) return;

let node = selection.values().next().value;
let x = direction === "left" ? node.x - node.width - 50 : direction === "right" ? node.x + node.width + 50 : node.x;
Expand Down Expand Up @@ -294,6 +307,8 @@ export default class CanvasMindMap extends Plugin {

const patchCanvas = () => {

const self = this;

const canvasView = this.app.workspace.getLeavesOfType("canvas").first()?.view;
// @ts-ignore
const canvas = canvasView?.canvas;
Expand All @@ -304,31 +319,35 @@ export default class CanvasMindMap extends Plugin {
const canvasViewunistaller = around(canvasView.constructor.prototype, {
onOpen: (next) =>
async function () {
this.scope.register(["Mod"], "ArrowUp", () => {
createFloatingNode(this.canvas, "top");
});
this.scope.register(["Mod"], "ArrowDown", () => {
createFloatingNode(this.canvas, "bottom");
});
this.scope.register(["Mod"], "ArrowLeft", () => {
createFloatingNode(this.canvas, "left");
});
this.scope.register(["Mod"], "ArrowRight", () => {
createFloatingNode(this.canvas, "right");
});
if (self.settings.create.createFloat) {
this.scope.register(["Mod"], "ArrowUp", () => {
createFloatingNode(this.canvas, "top");
});
this.scope.register(["Mod"], "ArrowDown", () => {
createFloatingNode(this.canvas, "bottom");
});
this.scope.register(["Mod"], "ArrowLeft", () => {
createFloatingNode(this.canvas, "left");
});
this.scope.register(["Mod"], "ArrowRight", () => {
createFloatingNode(this.canvas, "right");
});
}

this.scope.register(["Alt"], "ArrowUp", () => {
navigate(this.canvas, "top");
});
this.scope.register(["Alt"], "ArrowDown", () => {
navigate(this.canvas, "bottom");
});
this.scope.register(["Alt"], "ArrowLeft", () => {
navigate(this.canvas, "left");
});
this.scope.register(["Alt"], "ArrowRight", () => {
navigate(this.canvas, "right");
});
if (self.settings.navigate.useNavigate) {
this.scope.register(["Alt"], "ArrowUp", () => {
navigate(this.canvas, "top");
});
this.scope.register(["Alt"], "ArrowDown", () => {
navigate(this.canvas, "bottom");
});
this.scope.register(["Alt"], "ArrowLeft", () => {
navigate(this.canvas, "left");
});
this.scope.register(["Alt"], "ArrowRight", () => {
navigate(this.canvas, "right");
});
}

this.scope.register([], "Enter", async () => {
const node = await createSiblingNode(this.canvas);
Expand Down
119 changes: 66 additions & 53 deletions src/mindMapSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface MindMapSettings {
modifierKey: string[];
};
create: {
createFloat: boolean;
childDirection: string;
siblingWidth: number;
siblingHeight: number;
Expand All @@ -37,6 +38,7 @@ export const DEFAULT_SETTINGS: MindMapSettings = {
modifierKey: ['Alt'],
},
create: {
createFloat: true,
childDirection: 'right',
siblingWidth: 200,
siblingHeight: 100,
Expand All @@ -52,7 +54,7 @@ export const DEFAULT_SETTINGS: MindMapSettings = {
}
};

class MindMapSettingTab extends PluginSettingTab {
export class MindMapSettingTab extends PluginSettingTab {
plugin: CanvasMindMap;

updateSettings(key: any, value: any): void {
Expand Down Expand Up @@ -89,45 +91,8 @@ class MindMapSettingTab extends PluginSettingTab {

containerEl.createEl('h2', {text: 'Canvas MindMap'});

// let rowText: HTMLDivElement;
// new Setting(containerEl)
// .setName('Navigate')
// .setDesc('The number of rows in the table')
// .addSlider((slider) =>
// slider
// .setLimits(2, 12, 1)
// .setValue(this.plugin.settings.rowCount)
// .onChange(async (value) => {
// rowText.innerText = ` ${value.toString()}`;
// this.plugin.settings.rowCount = value;
// }),
// )
// .settingEl.createDiv("", (el) => {
// rowText = el;
// el.className = "table-generator-setting-text";
// el.innerText = ` ${this.plugin.settings.rowCount.toString()}`;
// });
//
// let columnText: HTMLDivElement;
// new Setting(containerEl)
// .setName('Columns Count')
// .setDesc('The number of columns in the table')
// .addSlider((slider) =>
// slider
// .setLimits(2, 12, 1)
// .setValue(this.plugin.settings.columnCount)
// .onChange(async (value) => {
// columnText.innerText = ` ${value.toString()}`;
// this.plugin.settings.columnCount = value;
// }),
// )
// .settingEl.createDiv("", (el) => {
// columnText = el;
// el.className = "table-generator-setting-text";
// el.innerText = ` ${this.plugin.settings.columnCount.toString()}`;
// });

this.containerEl.createEl('h2', {text: 'Say Thank You'});
this.useNavigateHotkeySetting(containerEl, this.plugin.settings);
this.createHotkeySetting(containerEl, this.plugin.settings);

new Setting(containerEl)
.setName('Donate')
Expand All @@ -152,19 +117,67 @@ class MindMapSettingTab extends PluginSettingTab {
});
});

if (setting.navigate.useNavigate) {
new Setting(containerEl)
.setName('Modifier Key')
.setDesc('The modifier key to use with the hotkey')
.addDropdown((dropdown) => {
const mods = supportModifierKey();
dropdown.addOption('None', 'None');
dropdown.setValue(setting.navigate.modifierKey[0]);
dropdown.onChange((value) => {
this.updateSettings('navigate.modifierKey.0', value);
});
});
}
// if (setting.navigate.useNavigate) {
// new Setting(containerEl)
// .setName('Modifier Key')
// .setDesc('The modifier key to use with the hotkey')
// .addDropdown((dropdown) => {
// const mods = supportModifierKey();
// dropdown.addOption('None', 'None');
// dropdown.setValue(setting.navigate.modifierKey[0]);
// dropdown.onChange((value) => {
// this.updateSettings('navigate.modifierKey.0', value);
// });
// });
// }

}

private createHotkeySetting(containerEl: HTMLElement, setting: MindMapSettings) {
new Setting(containerEl)
.setName('Create Float')
.setDesc('Create a float node')
.addToggle((toggle) => {
toggle.setValue(setting.create.createFloat);
toggle.onChange((value) => {
this.updateSettings('create.createFloat', value);
});
});
//
// new Setting(containerEl)
// .setName('Child Direction')
// .setDesc('The direction of the child node')
// .addDropdown((dropdown) => {
// dropdown.addOption('Right', 'right');
// dropdown.addOption('Left', 'left');
// dropdown.addOption('Up', 'up');
// dropdown.addOption('Down', 'down');
// dropdown.setValue(setting.create.childDirection);
// dropdown.onChange((value) => {
// this.updateSettings('create.childDirection', value);
// });
// });
//
// new Setting(containerEl)
// .setName('Sibling Width')
// .setDesc('The width of the sibling node')
// .addSlider((slider) => {
// slider.setLimits(100, 500, 10);
// slider.setValue(setting.create.siblingWidth);
// slider.onChange((value) => {
// this.updateSettings('create.siblingWidth', value);
// });
// });
//
// new Setting(containerEl)
// .setName('Sibling Height')
// .setDesc('The height of the sibling node')
// .addSlider((slider) => {
// slider.setLimits(50, 300, 10);
// slider.setValue(setting.create.siblingHeight);
// slider.onChange((value) => {
// this.updateSettings('create.siblingHeight', value);
// });
// });
}
}

0 comments on commit 0928590

Please sign in to comment.