Refactor AppendMenu to reduce duplication

This commit is contained in:
Alexopus
2025-07-20 01:14:13 +02:00
parent b72bc5ca18
commit 97bc0c312a
+48 -54
View File
@@ -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 <lora:name:1.0>, stripping .safetensors extension and normalizing slashes
const lorasTags = loras.map(lora => {
const nameWithoutExt = lora.replace(/\.safetensors$/i, '');
const normalizedPath = nameWithoutExt.replace(/\\/g, '/');
return `<lora:${normalizedPath}>`;
});
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 <lora:name:1.0>, metadata
const lorasWithMeta = loras.map(lora => {
const nameWithoutExt = lora.name.replace(/\.safetensors$/i, '');
const normalizedPath = nameWithoutExt.replace(/\\/g, '/');
const baseTag = `<lora:${normalizedPath}>`;
// Format each LoRA as <lora:name>, stripping .safetensors extension and normalizing slashes
const lorasTags = loras.map(lora => {
const nameWithoutExt = lora.replace(/\.safetensors$/i, '');
const normalizedPath = nameWithoutExt.replace(/\\/g, '/');
return `<lora:${normalizedPath}>`;
});
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 <lora:name>, metadata
const lorasWithMeta = loras.map(lora => {
const nameWithoutExt = lora.name.replace(/\.safetensors$/i, '');
const normalizedPath = nameWithoutExt.replace(/\\/g, '/');
const baseTag = `<lora:${normalizedPath}>`;
if (lora.metadata) {
return `${baseTag}, ${lora.metadata}`;
} else {
return baseTag;
}
});
return lorasWithMeta.join('\n');
}
insertTextIntoTextarea(text) {