From 97bc0c312a5c81a1ef44895659e1160e9d58c2ac Mon Sep 17 00:00:00 2001 From: Alexopus Date: Sun, 20 Jul 2025 01:14:13 +0200 Subject: [PATCH] Refactor AppendMenu to reduce duplication --- js/components/AppendMenu.js | 102 +++++++++++++++++------------------- 1 file changed, 48 insertions(+), 54 deletions(-) diff --git a/js/components/AppendMenu.js b/js/components/AppendMenu.js index 16aa586..4c79345 100644 --- a/js/components/AppendMenu.js +++ b/js/components/AppendMenu.js @@ -82,76 +82,70 @@ export class AppendMenu { this.submenu.style.display = 'none'; document.removeEventListener('click', this.closeMenuHandler); - switch(type) { - case 'raw': - this.appendLorasList(); - break; - case 'tag': - this.appendLorasTags(); - break; - case 'meta': - this.appendLorasWithMetadata(); - break; - } + this.appendLoras(type); } - async appendLorasList() { + async appendLoras(formatType) { try { - const loras = await NotesApi.getLorasList(); + let formattedText; - // Normalize backslashes to forward slashes - const normalizedLoras = loras.map(lora => lora.replace(/\\/g, '/')); - const lorasText = normalizedLoras.join('\n'); + switch(formatType) { + case 'raw': + formattedText = await this.formatLorasRaw(); + break; + case 'tag': + formattedText = await this.formatLorasTags(); + break; + case 'meta': + formattedText = await this.formatLorasWithMetadata(); + break; + default: + throw new Error(`Unknown format type: ${formatType}`); + } - this.insertTextIntoTextarea(lorasText); + this.insertTextIntoTextarea(formattedText); } catch (error) { console.error('Error appending LoRAs:', error); - alert('Failed to append LoRAs list'); + alert(`Failed to append LoRAs ${formatType === 'raw' ? 'list' : formatType === 'tag' ? 'tags' : 'with metadata'}`); } } - async appendLorasTags() { - try { - const loras = await NotesApi.getLorasList(); + async formatLorasRaw() { + const loras = await NotesApi.getLorasList(); - // Format each LoRA as , stripping .safetensors extension and normalizing slashes - const lorasTags = loras.map(lora => { - const nameWithoutExt = lora.replace(/\.safetensors$/i, ''); - const normalizedPath = nameWithoutExt.replace(/\\/g, '/'); - return ``; - }); - const lorasText = lorasTags.join('\n'); - - this.insertTextIntoTextarea(lorasText); - } catch (error) { - console.error('Error appending LoRAs tags:', error); - alert('Failed to append LoRAs tags'); - } + // Normalize backslashes to forward slashes + const normalizedLoras = loras.map(lora => lora.replace(/\\/g, '/')); + return normalizedLoras.join('\n'); } - async appendLorasWithMetadata() { - try { - const loras = await NotesApi.getLorasWithMetadata(); + async formatLorasTags() { + const loras = await NotesApi.getLorasList(); - // Format each LoRA with metadata as , metadata - const lorasWithMeta = loras.map(lora => { - const nameWithoutExt = lora.name.replace(/\.safetensors$/i, ''); - const normalizedPath = nameWithoutExt.replace(/\\/g, '/'); - const baseTag = ``; + // Format each LoRA as , stripping .safetensors extension and normalizing slashes + const lorasTags = loras.map(lora => { + const nameWithoutExt = lora.replace(/\.safetensors$/i, ''); + const normalizedPath = nameWithoutExt.replace(/\\/g, '/'); + return ``; + }); + return lorasTags.join('\n'); + } - if (lora.metadata) { - return `${baseTag}, ${lora.metadata}`; - } else { - return baseTag; - } - }); - const lorasText = lorasWithMeta.join('\n'); + async formatLorasWithMetadata() { + const loras = await NotesApi.getLorasWithMetadata(); - this.insertTextIntoTextarea(lorasText); - } catch (error) { - console.error('Error appending LoRAs with metadata:', error); - alert('Failed to append LoRAs with metadata'); - } + // Format each LoRA with metadata as , metadata + const lorasWithMeta = loras.map(lora => { + const nameWithoutExt = lora.name.replace(/\.safetensors$/i, ''); + const normalizedPath = nameWithoutExt.replace(/\\/g, '/'); + const baseTag = ``; + + if (lora.metadata) { + return `${baseTag}, ${lora.metadata}`; + } else { + return baseTag; + } + }); + return lorasWithMeta.join('\n'); } insertTextIntoTextarea(text) {