Notes/src/public/app/widgets/note_icon.ts

224 lines
7.1 KiB
TypeScript
Raw Normal View History

import { t } from "../services/i18n.js";
2021-05-22 12:35:41 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
2021-02-14 23:27:28 +01:00
import attributeService from "../services/attributes.js";
import server from "../services/server.js";
2025-01-18 01:14:47 +02:00
import type FNote from "../entities/fnote.js";
import type { EventData } from "../components/app_context.js";
import type { Icon } from "./icon_list.js";
2021-02-13 20:07:08 +01:00
const TPL = `
2021-06-13 22:55:31 +02:00
<div class="note-icon-widget dropdown">
2021-02-13 20:07:08 +01:00
<style>
2021-06-13 22:55:31 +02:00
.note-icon-widget {
2021-02-13 20:07:08 +01:00
padding-top: 3px;
padding-left: 7px;
margin-right: 0;
2021-06-13 22:55:31 +02:00
width: 50px;
height: 50px;
2021-02-13 20:07:08 +01:00
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget button.note-icon {
2021-02-13 21:52:31 +01:00
font-size: 180%;
background-color: transparent;
2021-02-14 23:27:28 +01:00
border: 1px solid transparent;
cursor: pointer;
padding: 6px;
2021-03-08 22:06:26 +01:00
color: var(--main-text-color);
2021-02-14 23:27:28 +01:00
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget button.note-icon:hover {
2021-02-14 23:27:28 +01:00
border: 1px solid var(--main-border-color);
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget .dropdown-menu {
2021-02-15 22:11:38 +01:00
border-radius: 10px;
border-width: 2px;
box-shadow: 10px 10px 93px -25px black;
padding: 10px 15px 10px 15px !important;
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget .filter-row {
2021-02-15 22:11:38 +01:00
padding-top: 10px;
padding-bottom: 10px;
padding-right: 20px;
2025-01-18 01:14:47 +02:00
display: flex;
2021-02-15 22:11:38 +01:00
align-items: baseline;
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget .filter-row span {
2021-02-15 22:11:38 +01:00
display: block;
padding-left: 15px;
padding-right: 15px;
font-weight: bold;
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget .icon-list {
2021-02-14 23:27:28 +01:00
height: 500px;
overflow: auto;
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget .icon-list span {
2021-02-14 23:27:28 +01:00
display: inline-block;
padding: 10px;
cursor: pointer;
border: 1px solid transparent;
font-size: 180%;
2021-02-14 23:27:28 +01:00
}
2025-01-18 01:14:47 +02:00
2021-06-13 22:55:31 +02:00
.note-icon-widget .icon-list span:hover {
2021-02-14 23:27:28 +01:00
border: 1px solid var(--main-border-color);
2021-02-13 20:07:08 +01:00
}
</style>
2025-01-18 01:14:47 +02:00
<button class="btn dropdown-toggle note-icon" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="${t("note_icon.change_note_icon")}"></button>
2021-02-15 22:11:38 +01:00
<div class="dropdown-menu" aria-labelledby="note-path-list-button" style="width: 610px;">
<div class="filter-row">
<span>${t("note_icon.category")}</span> <select name="icon-category" class="form-select"></select>
2025-01-18 01:14:47 +02:00
2024-09-13 22:17:22 +03:00
<span>${t("note_icon.search")}</span> <input type="text" name="icon-search" class="form-control" />
2021-02-15 22:11:38 +01:00
</div>
2025-01-18 01:14:47 +02:00
2021-02-14 23:27:28 +01:00
<div class="icon-list"></div>
</div>
2021-02-13 20:07:08 +01:00
</div>`;
2025-01-18 01:14:47 +02:00
interface IconToCountCache {
iconClassToCountMap: Record<string, number>;
}
2021-05-22 12:35:41 +02:00
export default class NoteIconWidget extends NoteContextAwareWidget {
2025-01-18 01:14:47 +02:00
private $icon!: JQuery<HTMLElement>;
private $iconList!: JQuery<HTMLElement>;
private $iconCategory!: JQuery<HTMLElement>;
private $iconSearch!: JQuery<HTMLElement>;
private iconToCountCache!: Promise<IconToCountCache | null> | null;
2021-02-13 20:07:08 +01:00
doRender() {
this.$widget = $(TPL);
2025-01-09 18:07:02 +02:00
this.$icon = this.$widget.find("button.note-icon");
this.$iconList = this.$widget.find(".icon-list");
this.$iconList.on("click", "span", async (e) => {
const clazz = $(e.target).attr("class");
2025-01-18 01:14:47 +02:00
if (this.noteId && this.note) {
await attributeService.setLabel(this.noteId, this.note.hasOwnedLabel("workspace") ? "workspaceIconClass" : "iconClass", clazz);
}
2021-02-14 23:27:28 +01:00
});
2021-02-15 22:11:38 +01:00
this.$iconCategory = this.$widget.find("select[name='icon-category']");
2025-01-09 18:07:02 +02:00
this.$iconCategory.on("change", () => this.renderDropdown());
this.$iconCategory.on("click", (e) => e.stopPropagation());
2021-02-15 22:11:38 +01:00
this.$iconSearch = this.$widget.find("input[name='icon-search']");
2025-01-09 18:07:02 +02:00
this.$iconSearch.on("input", () => this.renderDropdown());
2021-02-15 22:11:38 +01:00
2025-01-09 18:07:02 +02:00
this.$widget.on("show.bs.dropdown", async () => {
const { categories } = (await import("./icon_list.js")).default;
2021-02-15 22:11:38 +01:00
this.$iconCategory.empty();
for (const category of categories) {
2025-01-09 18:07:02 +02:00
this.$iconCategory.append($("<option>").text(category.name).attr("value", category.id));
2021-02-15 22:11:38 +01:00
}
2025-01-09 18:07:02 +02:00
this.$iconSearch.val("");
2021-02-15 22:11:38 +01:00
this.renderDropdown();
});
2021-02-13 20:07:08 +01:00
}
2025-01-18 01:14:47 +02:00
async refreshWithNote(note: FNote) {
this.$icon.removeClass().addClass(`${note.getIcon()} note-icon`);
2021-02-13 20:07:08 +01:00
}
2021-02-14 23:27:28 +01:00
2025-01-18 01:14:47 +02:00
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (this.noteId && loadResults.isNoteReloaded(this.noteId)) {
this.refresh();
return;
}
for (const attr of loadResults.getAttributeRows()) {
2025-01-18 01:14:47 +02:00
if (attr.type === "label" && ["iconClass", "workspaceIconClass"].includes(attr.name ?? "") && attributeService.isAffecting(attr, this.note)) {
2021-02-14 23:27:28 +01:00
this.refresh();
break;
}
}
}
async renderDropdown() {
const iconToCount = await this.getIconToCountMap();
2025-01-09 18:07:02 +02:00
const { icons } = (await import("./icon_list.js")).default;
2021-02-14 23:27:28 +01:00
this.$iconList.empty();
if (this.getIconLabels().length > 0) {
this.$iconList.append(
2025-01-09 18:07:02 +02:00
$(`<div style="text-align: center">`).append(
$(`<button class="btn btn-sm">${t("note_icon.reset-default")}</button>`).on("click", () =>
2025-01-18 01:14:47 +02:00
this.getIconLabels().forEach((label) => {
if (this.noteId) {
attributeService.removeAttributeById(this.noteId, label.attributeId);
}
})
)
2025-01-09 18:07:02 +02:00
)
);
}
2025-01-18 01:14:47 +02:00
const categoryId = parseInt(String(this.$iconCategory.find("option:selected")?.val()));
const search = String(this.$iconSearch.val())?.trim()?.toLowerCase();
2025-01-09 18:07:02 +02:00
const filteredIcons = icons.filter((icon) => {
2021-02-15 22:11:38 +01:00
if (categoryId && icon.category_id !== categoryId) {
return false;
2021-02-15 22:11:38 +01:00
}
if (search) {
2025-01-09 18:07:02 +02:00
if (!icon.name.includes(search) && !icon.term?.find((t) => t.includes(search))) {
return false;
}
2021-02-15 22:11:38 +01:00
}
return true;
});
2025-01-18 01:14:47 +02:00
if (iconToCount) {
filteredIcons.sort((a, b) => {
const countA = iconToCount[a.className ?? ""] || 0;
const countB = iconToCount[b.className ?? ""] || 0;
2025-01-18 01:14:47 +02:00
return countB - countA;
});
}
for (const icon of filteredIcons) {
this.$iconList.append(this.renderIcon(icon));
2021-02-14 23:27:28 +01:00
}
this.$iconSearch.focus();
2021-02-14 23:27:28 +01:00
}
async getIconToCountMap() {
if (!this.iconToCountCache) {
2025-01-18 01:14:47 +02:00
this.iconToCountCache = server.get<typeof this.iconToCountCache>("other/icon-usage");
2025-01-09 18:07:02 +02:00
setTimeout(() => (this.iconToCountCache = null), 20000); // invalidate cache after 20 seconds
}
2025-01-18 01:14:47 +02:00
return (await this.iconToCountCache)?.iconClassToCountMap;
}
2025-01-18 01:14:47 +02:00
renderIcon(icon: Icon) {
2025-01-09 18:07:02 +02:00
return $("<span>")
.addClass("bx " + icon.className)
.attr("title", icon.name);
}
getIconLabels() {
2025-01-18 01:14:47 +02:00
if (!this.note) {
return [];
}
2025-01-09 18:07:02 +02:00
return this.note.getOwnedLabels().filter((label) => ["workspaceIconClass", "iconClass"].includes(label.name));
}
2021-02-13 20:07:08 +01:00
}