feat(display): add settings menu

This commit is contained in:
Kritagya Bhattarai (CalfMoon) 2025-03-08 14:34:34 +05:45
parent b99c464470
commit 8f06207d8a
4 changed files with 112 additions and 1 deletions

View file

@ -1,5 +1,5 @@
{
"name": "obsidian-sample-plugin",
"name": "obsidian-node-factor",
"version": "1.0.0",
"description": "Customize factors effecting node size in graph.",
"main": "main.js",

View file

@ -1,7 +1,26 @@
import { Plugin } from "obsidian";
import { NodeFactorSettings, DEFAULT_SETTINGS } from "./types";
import SampleSettingTab from "./settings";
export default class NodeFactor extends Plugin {
settings: NodeFactorSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerEvent(this.app.workspace.on("layout-change", () => {}));
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}

75
src/settings.ts Normal file
View file

@ -0,0 +1,75 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import NodeFactor from "./main";
export default class SampleSettingTab extends PluginSettingTab {
plugin: NodeFactor;
constructor(app: App, plugin: NodeFactor) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h1", { text: "Node Factor Settings" });
new Setting(containerEl)
.setName("Forward link weight multiplier")
.setDesc("Multiplier for forward links weight (0 to diable).")
.addSlider((slider) =>
slider
.setLimits(0, 20, 1)
.setDynamicTooltip()
.setValue(this.plugin.settings.fwdMultiplier)
.onChange(async (value) => {
this.plugin.settings.fwdMultiplier = value;
await this.plugin.saveSettings();
}),
);
new Setting(containerEl)
.setName("Travel forward tree")
.setDesc("Travel forward tree to calcuate node size.")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.fwdTree)
.onChange(async (value) => {
this.plugin.settings.fwdTree = value;
await this.plugin.saveSettings();
}),
);
new Setting(containerEl)
.setName("Backward link weight multiplier")
.setDesc("Multiplier for backward links weight (0 to diable).")
.addSlider((slider) =>
slider
.setLimits(0, 20, 1)
.setDynamicTooltip()
.setValue(this.plugin.settings.bwdMultiplier)
.onChange(async (value) => {
this.plugin.settings.bwdMultiplier = value;
await this.plugin.saveSettings();
}),
);
new Setting(containerEl)
.setName("Character per weight")
.setDesc(
"Add 1 weight to node size per no of given Character (0 to disable).",
)
.addSlider((slider) =>
slider
.setLimits(0, 5000, 100)
.setDynamicTooltip()
.setValue(this.plugin.settings.lettersPerWt)
.onChange(async (value) => {
this.plugin.settings.lettersPerWt = value;
await this.plugin.saveSettings();
}),
);
}
}

17
src/types.ts Normal file
View file

@ -0,0 +1,17 @@
export interface NodeFactorSettings {
fwdMultiplier: number;
fwdTree: boolean;
bwdMultiplier: number;
lettersPerWt: number;
}
export const DEFAULT_SETTINGS: NodeFactorSettings = {
fwdMultiplier: 1,
fwdTree: false,
bwdMultiplier: 1,
lettersPerWt: 0,
};