Progress on move entry * Fixed header alignment * Building HTML with browser api instead of string. * jqueryElement.data(key, value) usages converted to jqueryElement.attr(data-key, value) * Logs simplified * Removed success toastry message

bc08d42d0e85db81e5b858267af02121bf63a100

bmen25124 <bmen25124@gmail.com>

2 files changed, +46 -41Showing whitespace changes
public/scripts/templates/worldInfoKeywordHeaders.html+1 -1
@@ -1,4 +1,4 @@
11<div id="WIEntryHeaderTitlesPC" class="flex-container wide100p spaceBetween justifyCenter textAlignCenter" style="padding:0 47.5em0em;">
22 <small class="flex1" data-i18n="Title/Memo">Title/Memo</small>
33 <small style="width: calc(3.5em + 10px)" data-i18n="Strategy">Strategy</small>
44 <small style="width: calc(3.5em + 20px)" data-i18n="Position">Position</small>
public/scripts/world-info.js+45 -40
@@ -2208,7 +2208,7 @@ function verifyWorldInfoSearchSortRule() {
22082208 * Use `originalWIDataKeyMap` to find the correct value to be set.
22092209 *
22102210 * @param {object} data - The data object containing the original data entries.
22112211 * @param {stringnumber} uid - The unique identifier of the data entry.
22122212 * @param {string} key - The key of the value to be set.
22132213 * @param {any} value - The value to be set.
22142214 */
@@ -3141,21 +3141,38 @@ export async function getWorldEntry(name, data, entry) {
31413141
31423142 // move button
31433143 const moveButton = template.find('.move_entry_button');
31443144 moveButton.dataattr('data-uid', entry.uid);
31453145 moveButton.dataattr('data-current-world', name);
31463146 moveButton.on('click', async function (e) {
31473147 e.stopPropagation();
31483148 const sourceUid = $(this).data('uid');
31493149 const sourceWorld = $(this).data('current-world');
3150- // Loading world info is bad, do we have cache variable?
3150+ const sourceWorldInfo = await loadWorldInfo(sourceWorld);
3151- const sourceName = (await loadWorldInfo(sourceWorld)).entries[sourceUid].comment;
3151+ if (!sourceWorldInfo) {
3152+ return;
3153+ }
3154+ const sourceName = sourceWorldInfo.entries[sourceUid]?.comment;
3155+ if (sourceName === undefined) {
3156+ return;
3157+ }
3158+
3159+ const select = document.createElement('select');
3160+ select.id = 'move_entry_target_select';
3161+ select.classList.add('text_pole', 'wide100p', 'margin-top');
3162+
3163+ const defaultOption = document.createElement('option');
3164+ defaultOption.value = '';
3165+ defaultOption.textContent = `-- ${t`Select Target Lorebook`} --`;
3166+ select.appendChild(defaultOption);
31523167
3153- let optionsHtml = `<option value="">-- ${t`Select Target Lorebook`} --</option>`;
31543168 let selectableWorldCount = 0;
31553169 world_names.forEach(worldName => {
31563170 if (worldName !== sourceWorld) { // Exclude the current world
3157- optionsHtml += `<option value="${world_names.indexOf(worldName)}">${worldName}</option>`;
3171+ const option = document.createElement('option');
3158- selectableWorldCount += 1;
3172+ option.value = world_names.indexOf(worldName).toString();
3173+ option.textContent = worldName;
3174+ select.appendChild(option);
3175+ selectableWorldCount++;
31593176 }
31603177 });
31613178
@@ -3164,27 +3181,24 @@ export async function getWorldEntry(name, data, entry) {
31643181 return;
31653182 }
31663183
3167- const content = `
3184+ // Create wrapper div
3168- <div>${t`Move ${sourceName} to:`}</div>
3185+ const wrapper = document.createElement('div');
3169- <select id="move_entry_target_select" class="text_pole wide100p margin-top">
3186+ wrapper.textContent = t`Move ${sourceName} to:`;
3170- ${optionsHtml}
3171- </select>
3172- `;
31733187
3174- const popupPromise = callGenericPopup(content, POPUP_TYPE.CONFIRM, '', {
3188+ // Create container and append elements
3175- okButton: t`Move`,
3189+ const container = document.createElement('div');
3176- cancelButton: t`Cancel`,
3190+ container.appendChild(wrapper);
31773191 }container.appendChild(select);
31783192
31793193 let selectedWorldIndex = -1;
31803194 $('#move_entry_target_select')select.onaddEventListener('change', function () {
3181- /** @type {string} */
3195+ selectedWorldIndex = this.value === '' ? -1 : Number(this.value);
3182- // @ts-ignore
3183- const value = $(this).val();
3184- selectedWorldIndex = value === '' ? -1 : Number(value);
31853196 });
31863197
31873198 const popupConfirm = await popupPromise;callGenericPopup(container, POPUP_TYPE.CONFIRM, '', {
3199+ okButton: t`Move`,
3200+ cancelButton: t`Cancel`,
3201+ });
31883202 if (!popupConfirm) {
31893203 return;
31903204 }
@@ -5337,19 +5351,11 @@ jQuery(() => {
53375351 *
53385352 * @param {string} sourceName - The name of the source lorebook file.
53395353 * @param {string} targetName - The name of the target lorebook file.
53405354 * @param {number|string} uid - The UID of the entry to move from the source lorebook.
53415355 * @returns {Promise<boolean>} True if the move was successful, false otherwise.
53425356 */
53435357export async function moveWorldInfoEntry(sourceName, targetName, uid) {
5344- console.log(`[WI] Attempting to move entry UID ${uid} from '${sourceName}' to '${targetName}'`);
5345-
5346- if (!sourceName || !targetName || uid === undefined || uid === null) {
5347- console.error('[WI Move] Missing required arguments.');
5348- return false;
5349- }
5350-
53515358 if (sourceName === targetName) {
5352- toastr.warning(t`Source and target lorebooks cannot be the same.`);
53535359 return false;
53545360 }
53555361
@@ -5407,20 +5413,19 @@ export async function moveWorldInfoEntry(sourceName, targetName, uid) {
54075413 targetData.entries[newUid] = entryToMove;
54085414
54095415 delete sourceData.entries[entryUidString];
54105416 // Remove from originalData if it exists, using the original UID
54115417 deleteWIOriginalDataValue(sourceData, entryUidString);
5418+ // TODO: setWIOriginalDataValue
54125419 console.debug(`[WI Move] Removed entry UID ${entryUidString} from source '${sourceName}'.`);
54135420
54145421
5415- // Save immediately to reduce chances of inconsistency if the browser is closed
5422+ await saveWorldInfo(targetName, targetData);
5416- // Note: This is not truly atomic. If one save fails, state could be inconsistent.
5417- await saveWorldInfo(targetName, targetData, true);
54185423 console.debug(`[WI Move] Saved target lorebook '${targetName}'.`);
54195424 await saveWorldInfo(sourceName, sourceData, true);
54205425 console.debug(`[WI Move] Saved source lorebook '${sourceName}'.`);
54215426
54225427
54235428 toastrconsole.successlog(t`[WI Move] ${entryToMove.comment} moved successfully! to '${targetName}'.`);
54245429
54255430 // Check if the currently viewed book in the editor is the source or target and reload it
54265431 const currentEditorBookIndex = Number($('#world_editor_select').val());