1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2025-12-24 07:12:57 +00:00

Dynamically catch and refresh colors in tag editor

This commit is contained in:
2024-10-12 20:15:59 +04:00
parent 03512a6539
commit 57c505bee9
2 changed files with 38 additions and 31 deletions

View File

@@ -1,30 +1,11 @@
import {BaseComponent} from "$lib/components/base/BaseComponent.js";
import {getComponent} from "$lib/components/base/ComponentUtils.js";
class TagsForm extends BaseComponent {
/**
* Button to edit tags for the image.
* @type {HTMLElement|null}
*/
#editTagButton;
build() {
this.#editTagButton = document.querySelector('#edit-tags');
}
init() {
if (this.#editTagButton) {
this.#editTagButton.addEventListener('click', this.#onEditClicked.bind(this));
}
}
#onEditClicked() {
this.#refreshTagColors();
}
export class TagsForm extends BaseComponent {
/**
* Collect all the tag categories available on the page and color the tags in the editor according to them.
*/
#refreshTagColors() {
refreshTagColors() {
const tagCategories = this.#gatherTagCategories();
const editableTags = this.container.querySelectorAll('.tag');
@@ -63,8 +44,38 @@ class TagsForm extends BaseComponent {
return tagCategories;
}
}
export function initializeTagForm(container) {
new TagsForm(container).initialize();
static watchForEditors() {
document.body.addEventListener('click', event => {
const targetElement = event.target;
if (!(targetElement instanceof HTMLElement)) {
return;
}
const tagEditorWrapper = targetElement.closest('#image_tags_and_source');
if (!tagEditorWrapper) {
return;
}
const refreshTrigger = targetElement.closest('.js-taginput-show, #edit-tags')
if (!refreshTrigger) {
return;
}
const tagFormElement = tagEditorWrapper.querySelector('#tags-form');
/** @type {TagsForm|null} */
let tagEditor = getComponent(tagFormElement);
if (!tagEditor || (!tagEditor instanceof TagsForm)) {
tagEditor = new TagsForm(tagFormElement);
tagEditor.initialize();
}
tagEditor.refreshTagColors();
});
}
}