tech-tips/.obsidian/plugins/favorite-note/main.js

298 lines
9.0 KiB
JavaScript
Raw Normal View History

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
default: () => FavoritePlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian4 = require("obsidian");
// src/constants.ts
var DEFAULT_SETTINGS = {
icon: "star",
favorites: []
};
var SETTINGS_ICON_BTN_ID = "fv-select-icon-btn";
// src/lib/utils.ts
var import_obsidian = require("obsidian");
function createFavoriteButton(isFavorite = false, icon = "star") {
const trailingButton = document.createElement("span");
trailingButton.classList.add("fav-btn");
if (isFavorite) {
trailingButton.classList.add("is-favorite");
} else {
trailingButton.classList.remove("is-favorite");
}
(0, import_obsidian.setIcon)(trailingButton, icon);
return trailingButton;
}
// src/tabs/settings-tab.ts
var import_obsidian3 = require("obsidian");
// src/modals/choose-icon-modal.ts
var import_obsidian2 = require("obsidian");
var ChooseFromIconList = class extends import_obsidian2.FuzzySuggestModal {
constructor(plugin, issub = false) {
super(plugin.app);
this.plugin = plugin;
this.issub = issub;
this.setPlaceholder("Choose an icon");
}
capitalJoin(string) {
const icon = string.split(" ");
return icon.map((icon2) => {
return icon2[0].toUpperCase() + icon2.substring(1);
}).join(" ");
}
getItems() {
return (0, import_obsidian2.getIconIds)();
}
getItemText(item) {
return this.capitalJoin(
item.replace("feather-", "").replace("remix-", "").replace("bx-", "").replace(/([A-Z])/g, " $1").trim().replace(/-/gi, " ")
);
}
renderSuggestion(icon, iconItem) {
const span = createSpan({ cls: "fv-icon-item" });
iconItem.appendChild(span);
(0, import_obsidian2.setIcon)(span, icon.item);
super.renderSuggestion(icon, iconItem);
}
async onChooseItem(item) {
this.plugin.settings.icon = item;
(0, import_obsidian2.setIcon)(
document.querySelector(
`#${SETTINGS_ICON_BTN_ID}`
),
item
);
await this.plugin.saveSettings();
this.plugin.reload();
setTimeout(() => {
dispatchEvent(new Event("print-greeting-to-console"));
}, 100);
}
};
// src/tabs/settings-tab.ts
var FavoritePluginSettingsTab = class extends import_obsidian3.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new import_obsidian3.Setting(containerEl).setName("Favorite Icon").setDesc("Choose your favorite icon").addButton((el) => {
el.setIcon(this.plugin.settings.icon);
el.onClick(async () => {
new ChooseFromIconList(this.plugin, false).open();
});
this.plugin.saveSettings();
}).controlEl.children[0].setAttr("id", SETTINGS_ICON_BTN_ID);
const donationDiv = containerEl.createEl("div", {
cls: "donate-section"
});
const donateText = createEl("p", {
text: "If you like this Plugin and are considering donating to support continued development, use the button below!"
});
donationDiv.appendChild(donateText);
donationDiv.appendChild(
this.createDonateButton("https://www.buymeacoffee.com/mahmudz")
);
}
createDonateButton(link) {
const a = createEl("a");
a.setAttribute("href", link);
a.addClass("buymeacoffee-img");
const img = createEl("img", {
attr: {
src: "https://img.buymeacoffee.com/button-api/?text=Buy me a coffee &emoji=&slug=mahmudz&button_colour=BD5FFF&font_colour=ffffff&font_family=Poppins&outline_colour=000000&coffee_colour=FFDD00"
}
});
a.appendChild(img);
return a;
}
};
// src/main.ts
var FavoritePlugin = class extends import_obsidian4.Plugin {
constructor() {
super(...arguments);
this.isEnabled = false;
this.favorites = [];
}
getFileExplorer() {
return this.app.workspace.containerEl.find(".nav-folder-children");
}
onFolderExpand(e) {
if (this.isEnabled) {
this.addFavoriteIconToFolder(e.currentTarget);
} else {
const parentElement = e.currentTarget;
this.removeFavoriteIconFromChild(parentElement);
}
}
isFavorite(filePath) {
return this.favorites.includes(filePath);
}
toggleFavorite(filePath) {
const index = this.favorites.indexOf(filePath);
if (index !== -1) {
this.favorites.splice(index, 1);
} else {
this.favorites.push(filePath);
}
this.saveSettings();
}
addFavoriteIconToItem(listItem) {
var _a;
const filePath = (_a = listItem.getAttribute("data-path")) != null ? _a : "";
const trailingButton = createFavoriteButton(
this.isFavorite(filePath),
this.settings.icon
);
trailingButton.addEventListener("click", (e) => {
const favoriteButton = e.currentTarget;
if (favoriteButton) {
const titleEl = favoriteButton.parentElement;
const filePath2 = titleEl.getAttribute("data-path");
this.toggleFavorite(filePath2);
if (this.isFavorite(filePath2)) {
favoriteButton.classList.add("is-favorite");
} else {
favoriteButton.classList.remove("is-favorite");
}
}
});
listItem.appendChild(trailingButton);
}
addFavoriteIconToFolder(folderEl) {
const listItems = folderEl.querySelectorAll(
".nav-folder, .nav-file-title"
);
listItems.forEach((listItem) => {
const isAlreadyExists = listItem.find(".fav-btn");
if (isAlreadyExists) {
return;
}
if (listItem.classList.contains("nav-file-title")) {
listItem.addClass("fav-nav-file-title");
this.addFavoriteIconToItem(listItem);
} else if (!listItem.classList.contains("is-collapsed")) {
setTimeout(() => {
this.addFavoriteIconToFolder(listItem);
}, 200);
} else {
listItem.addEventListener(
"click",
this.onFolderExpand.bind(this),
{ once: true }
);
}
});
}
addFavoriteIcons() {
this.addFavoriteIconToFolder(this.getFileExplorer());
}
removeFavoriteIconFromChild(folderEl) {
const listItems = folderEl.querySelectorAll(
".nav-folder, .nav-file-title"
);
listItems.forEach((listItem) => {
if (listItem.classList.contains("nav-file-title")) {
listItem.findAll(".fav-btn").forEach((el) => el.remove());
listItem.removeClass("fav-nav-file-title");
} else {
this.removeFavoriteIconFromChild(listItem);
}
});
}
removeFavoriteIcons() {
const fileExplorer = this.getFileExplorer();
if (fileExplorer) {
this.removeFavoriteIconFromChild(fileExplorer);
}
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
this.favorites = this.settings.favorites;
}
async saveSettings() {
await this.saveData({
...this.settings,
favorites: this.favorites
});
}
reload() {
this.removeFavoriteIcons();
this.addFavoriteIcons();
}
onFileCreate(file) {
setTimeout(() => {
const listItem = this.app.workspace.containerEl.find(
`[data-path="${file.path}"]`
);
listItem.classList.add("fav-nav-file-title");
this.addFavoriteIconToItem(listItem);
}, 100);
}
onFileDelete(file) {
const index = this.favorites.indexOf(file.path);
if (index !== -1) {
this.favorites.splice(index, 1);
this.saveSettings();
}
}
async onload() {
this.isEnabled = true;
this.app.workspace.onLayoutReady(async () => {
await this.loadSettings();
this.addFavoriteIcons();
this.getFileExplorer().findAll(".nav-file-title").forEach((el) => {
el.classList.add("fav-nav-file-title");
});
this.app.vault.on("create", this.onFileCreate.bind(this));
this.app.vault.on("delete", this.onFileDelete.bind(this));
});
this.addSettingTab(new FavoritePluginSettingsTab(this.app, this));
}
onunload() {
this.isEnabled = false;
this.getFileExplorer().findAll(".nav-file-title").forEach((el) => {
el.classList.remove("fav-nav-file-title");
});
this.app.vault.off("create", this.onFileCreate.bind(this));
this.app.vault.off("delete", this.onFileDelete.bind(this));
this.removeFavoriteIcons();
}
};