File size: 13,527 Bytes
2b0bc5f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
// TabsExtension — организация вкладок расширений + поиск по вкладкам
// ECMAScript 2020+, A1111/Forge совместим
class TabsExtension {
/** @type {TabsExtensionConfigs} */
static #config;
/** Активные вкладки по режимам */
/** @type {Object.<string, string|null>} */
static #activeExtension = { "txt": null, "img": null };
/** Пары "чекбокс включения" <-> "кнопка вкладки" для подсветки active */
/** @type {Array.<[HTMLInputElement, HTMLButtonElement]>} */
static #enablePairs = [];
/** Таймер отложенного обновления подсветки active */
/** @type {number|null} */
static #refreshQueue = null;
/** @param {HTMLDivElement} extension @returns {HTMLInputElement|null} */
static #tryFindEnableToggle(extension) {
let temp = null;
for (const checkbox of extension.querySelectorAll('input[type=checkbox]')) {
const labelText = checkbox.parentNode?.querySelector('span')?.textContent?.toLowerCase();
if (labelText?.includes('enable')) return checkbox;
if (!temp && labelText?.includes('active')) temp = checkbox;
}
return temp;
}
/** Синхронизация active-подсветки кнопок с реальным состоянием чекбоксов */
static #refreshEnableCheckbox() {
if (this.#refreshQueue) clearTimeout(this.#refreshQueue);
this.#refreshQueue = window.setTimeout(() => {
for (const [toggle, button] of this.#enablePairs) {
if (toggle.checked) button.classList.add('active');
else button.classList.remove('active');
}
}, 250);
}
/**
* Сортировка расширений по конфигу, если включено
* @param {Object.<string, HTMLDivElement>} extensions
* @param {Object.<string, string>} configs
* @returns {Object.<string, HTMLDivElement>}
*/
static #sort_extensions(extensions, configs) {
if (!this.#config.sort) return extensions;
const sorted = {};
for (const key of Object.keys(configs)) {
if (Object.prototype.hasOwnProperty.call(extensions, key)) {
sorted[key] = extensions[key];
delete extensions[key];
}
}
for (const key of Object.keys(extensions)) sorted[key] = extensions[key];
return sorted;
}
/**
* Главная раскладка
* @param {'txt'|'img'} mode
* @param {Object.<string, HTMLDivElement>} extensions
* @param {Object.<string, string>} configs
* @returns {Object.<string, string>}
*/
static #setup_tabs(mode, extensions, configs) {
const container = {
'left': document.getElementById(`${mode}2img_script_container`),
'right': document.getElementById(`${mode}2img_results`)
};
/** @type {string} */ const mainSide = configs['tabs'];
/** @type {string} */ const oppSide = (mainSide === 'left') ? 'right' : 'left';
// Если вдруг контейнеров нет — выходим аккуратно
if (!container[mainSide] || !container[oppSide]) return configs;
// Заготовим области
const extensionContainers = {};
for (const side of ['above', 'left', 'below', 'right']) {
const div = document.createElement("div");
div.id = `tabs_ex_content-${mode}2img-${side}`;
div.style.overflow = "visible";
extensionContainers[side] = div;
}
const buttonContainer = document.createElement("div");
extensionContainers[mainSide].appendChild(buttonContainer);
buttonContainer.id = `tabs_ex_${mode}`;
container[mainSide].appendChild(extensionContainers['above']);
container[mainSide].appendChild(extensionContainers[mainSide]);
container[mainSide].appendChild(extensionContainers['below']);
container[oppSide].appendChild(extensionContainers[oppSide]);
/** Карта: ключ вкладки -> кнопка */
/** @type {Object.<string, HTMLButtonElement>} */
const allButtons = {};
// === Поиск по вкладкам (компактный) ===
const searchWrapper = document.createElement("div");
searchWrapper.style.display = "flex";
searchWrapper.style.justifyContent = "center";
searchWrapper.style.width = "100%";
searchWrapper.style.marginBottom = "6px";
const searchInput = document.createElement("input");
searchInput.type = "text";
searchInput.placeholder = "Filter extensions...";
searchInput.autocomplete = "off";
searchInput.style.width = "100%";
searchInput.style.maxWidth = "480px"; // чтобы не расползался как на скрине
searchInput.style.minWidth = "240px";
searchInput.style.padding = "6px 10px";
searchInput.style.borderRadius = "6px";
searchInput.style.border = "1px solid var(--block-border-color, #444)";
searchWrapper.appendChild(searchInput);
buttonContainer.appendChild(searchWrapper);
const self = this; // нужен доступ к #activeExtension внутри коллбэка
const applyFilter = (queryText) => {
const q = (queryText || "").trim().toLowerCase();
for (const [tabKey, btn] of Object.entries(allButtons)) {
const label = (btn.textContent || "").toLowerCase();
btn.style.display = (q.length === 0 || label.includes(q)) ? "" : "none";
}
// если активная вкладка скрыта фильтром — мягко гасим её контент
const active = self.#activeExtension[mode];
if (active && allButtons[active] && allButtons[active].style.display === "none") {
allButtons[active].classList.remove('selected');
if (extensions[active]) extensions[active].style.display = "none";
self.#activeExtension[mode] = null;
}
};
searchInput.addEventListener("input", (e) => applyFilter(e.target.value));
// Кнопки и раскладка
for (const tabKey of Object.keys(extensions)) {
if (!Object.prototype.hasOwnProperty.call(configs, tabKey)) {
configs[tabKey] = configs['default']; // новое расширение — дефолтная зона
}
const pos = configs[tabKey];
if (pos === "hide") continue;
if (pos === "above" || pos === "below") {
extensionContainers[pos].appendChild(extensions[tabKey]);
extensions[tabKey].style.display = "block";
continue;
}
// Подпись кнопки
const btnSpan = document.createElement('span');
btnSpan.className = 'tab_label';
const extensionName = (this.#config.version)
? tabKey
: extensions[tabKey].getAttribute("ext-label");
btnSpan.textContent = (!this.#config.forge)
? extensionName
: (extensionName || "").split('Integrated')[0].trim();
// Кнопка вкладки
const tabButton = document.createElement("button");
tabButton.classList.add('tab_button');
tabButton.appendChild(btnSpan);
buttonContainer.appendChild(tabButton);
allButtons[tabKey] = tabButton;
tabButton.addEventListener("click", (e) => {
// режим RMB: Ctrl/Cmd не перехватываем
if (!this.#config.rmb && (e.ctrlKey || e.metaKey)) return;
if (this.#activeExtension[mode] != null) {
allButtons[this.#activeExtension[mode]]?.classList.remove('selected');
const prev = this.#activeExtension[mode];
if (prev && extensions[prev]) extensions[prev].style.display = "none";
}
this.#activeExtension[mode] = (
(this.#config.toggle) && (this.#activeExtension[mode] === tabKey)
) ? null : tabKey;
if (this.#activeExtension[mode] != null) {
allButtons[this.#activeExtension[mode]]?.classList.add('selected');
extensions[this.#activeExtension[mode]].style.display = "block";
}
});
// Переносим содержимое вкладки в нужную сторону
extensionContainers[configs[tabKey]].appendChild(extensions[tabKey]);
// Особый случай: Scripts
if (tabKey === 'Scripts') {
const scriptsDropdown = extensions[tabKey].querySelector('input');
const observer = new MutationObserver(() => {
if (!scriptsDropdown) return;
if (scriptsDropdown.value !== 'None') allButtons['Scripts']?.classList.add('active');
else allButtons['Scripts']?.classList.remove('active');
});
observer.observe(extensions[tabKey], { childList: true, subtree: true });
if (this.#config.scripts_toggle) {
const btn = document.getElementById(`TABSEX_${mode}2img_s_toggle`);
if (btn) {
if (this.#config.rmb) {
allButtons[tabKey].addEventListener("contextmenu", (e) => {
e.preventDefault(); btn.click(); return false;
});
} else {
allButtons[tabKey].addEventListener("click", (e) => {
if (e.ctrlKey || e.metaKey) btn.click();
});
}
}
}
continue;
}
// Подсветка активных по чекбоксу Enable/Active
const enableToggle = this.#tryFindEnableToggle(extensions[tabKey]);
if (!enableToggle) continue;
if (this.#config.rmb) {
allButtons[tabKey].addEventListener("contextmenu", (e) => {
e.preventDefault(); enableToggle.click(); return false;
});
} else {
allButtons[tabKey].addEventListener("click", (e) => {
if (e.ctrlKey || e.metaKey) enableToggle.click();
});
}
if (enableToggle.checked) allButtons[tabKey].classList.add('active');
this.#enablePairs.push([enableToggle, allButtons[tabKey]]);
}
// Чистим пустые области
for (const side of ['above', 'left', 'below', 'right']) {
if (extensionContainers[side].children.length === 0) {
extensionContainers[side].remove();
}
}
if (this.#config.open) {
const firstBtn = Object.values(allButtons)[0];
if (firstBtn) firstBtn.click();
}
return configs;
}
/** Точка входа */
static init() {
this.#config = new TabsExtensionConfigs();
const configs = this.#config.parseConfigs();
const processedConfigs = {};
for (const mode of ['txt', 'img']) {
let extensions = null;
const keepExtensions = Object.keys(configs[mode])
.filter((ext) => configs[mode][ext] === "keep");
try {
const parsed = TabsExtensionParser.parse(mode, keepExtensions);
extensions = this.#sort_extensions(parsed, configs[mode]);
} catch (e) {
alert(`[TabsExtension] Something went wrong while parsing ${mode}2img extensions:\n${e}`);
continue;
}
try {
processedConfigs[mode] = this.#setup_tabs(mode, extensions, configs[mode]);
if (this.#config.container) {
const styler = document.getElementById(`${mode}2img_script_container`)
?.querySelector(".styler");
if (styler) styler.style.display = "none";
}
} catch (e) {
alert(`[TabsExtension] Something went wrong during ${mode}2img setup:\n${e}`);
continue;
}
}
try {
this.#config.saveConfigs(processedConfigs);
} catch (e) {
alert(`[TabsExtension] Something went wrong while saving configs:\n${e}`);
}
document.addEventListener("click", () => this.#refreshEnableCheckbox());
}
}
// Инициализация после загрузки UI
(function () {
onUiLoaded(() => {
const slider = document.getElementById("setting_tabs_ex_delay")
?.querySelector("input[type=range]");
const delay = parseInt(slider?.value ?? "50", 10);
setTimeout(() => {
if (typeof TabsExtension?.init === "function") TabsExtension.init();
}, Number.isNaN(delay) ? 50 : delay);
});
})();
|