Merge pull request #2149 from Wolfsblvt/duplicate-wi-entries

Button to duplicate WI entries
This commit is contained in:
Cohee 2024-04-27 17:57:59 +03:00 committed by GitHub
commit 8e7ffab793
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 14 deletions

View File

@ -5088,7 +5088,8 @@
</div>
</div>
</div>
<i class="menu_button delete_entry_button fa-solid fa-trash-can" type="submit" value=""></i>
<i class="menu_button duplicate_entry_button fa-solid fa-paste" title="Duplicate world info entry" data-i18n="[title]Duplicate world info entry" type="submit" value=""></i>
<i class="menu_button delete_entry_button fa-solid fa-trash-can" title="Delete world info entry" data-i18n="[title]Delete world info entry" type="submit" value=""></i>
</div>
<div class="inline-drawer-content flex-container paddingBottom5px wide100p">
<div class="flex-container wide100p alignitemscenter">

View File

@ -522,7 +522,7 @@ function registerWorldInfoSlashCommands() {
return '';
}
const entry = createWorldInfoEntry(file, data, true);
const entry = createWorldInfoEntry(file, data);
if (key) {
entry.key.push(key);
@ -795,20 +795,20 @@ function displayWorldEntries(name, data, navigation = navigation_option.none) {
callback: function (/** @type {object[]} */ page) {
$('#world_popup_entries_list').empty();
const keywordHeaders = `
<div id="WIEntryHeaderTitlesPC" class="flex-container wide100p spaceBetween justifyCenter textAlignCenter" style="padding:0 2.5em;">
<div id="WIEntryHeaderTitlesPC" class="flex-container wide100p spaceBetween justifyCenter textAlignCenter" style="padding:0 4.5em;">
<small class="flex1">
Title/Memo
</small>
<small style="width: calc(3.5em + 5px)">
<small style="width: calc(3.5em + 15px)">
Status
</small>
<small style="width: calc(3.5em + 20px)">
<small style="width: calc(3.5em + 30px)">
Position
</small>
<small style="width: calc(3.5em + 15px)">
<small style="width: calc(3.5em + 20px)">
Depth
</small>
<small style="width: calc(3.5em + 15px)">
<small style="width: calc(3.5em + 20px)">
Order
</small>
<small style="width: calc(3.5em + 15px)">
@ -859,7 +859,8 @@ function displayWorldEntries(name, data, navigation = navigation_option.none) {
}
$('#world_popup_new').off('click').on('click', () => {
createWorldInfoEntry(name, data);
const entry = createWorldInfoEntry(name, data);
if (entry) updateEditor(entry.uid);
});
$('#world_popup_name_button').off('click').on('click', async () => {
@ -1571,6 +1572,18 @@ function getWorldEntry(name, data, entry) {
});
preventRecursionInput.prop('checked', entry.preventRecursion).trigger('input');
// duplicate button
const duplicateButton = template.find('.duplicate_entry_button');
duplicateButton.data('uid', entry.uid);
duplicateButton.on('click', function () {
const uid = $(this).data('uid');
const entry = duplicateWorldInfoEntry(data, uid);
if (entry) {
saveWorldInfo(name, data);
updateEditor(entry.uid);
}
});
// delete button
const deleteButton = template.find('.delete_entry_button');
deleteButton.data('uid', entry.uid);
@ -1755,7 +1768,33 @@ function createEntryInputAutocomplete(input, callback) {
});
}
async function deleteWorldInfoEntry(data, uid) {
/**
* Duplicated a WI entry by copying all of its properties and assigning a new uid
* @param {*} data - The data of the book
* @param {number} uid - The uid of the entry to copy in this book
* @returns {*} The new WI duplicated entry
*/
function duplicateWorldInfoEntry(data, uid) {
if (!data || !('entries' in data) || !data.entries[uid]) {
return;
}
// Exclude uid and gather the rest of the properties
const { uid: _, ...originalData } = data.entries[uid];
// Create new entry and copy over data
const entry = createWorldInfoEntry(data.name, data);
Object.assign(entry, originalData);
return entry;
}
/**
* Deletes a WI entry, with a user confirmation dialog
* @param {*[]} data - The data of the book
* @param {number} uid - The uid of the entry to copy in this book
*/
function deleteWorldInfoEntry(data, uid) {
if (!data || !('entries' in data)) {
return;
}
@ -1792,7 +1831,7 @@ const newEntryTemplate = {
role: 0,
};
function createWorldInfoEntry(name, data, fromSlashCommand = false) {
function createWorldInfoEntry(name, data) {
const newUid = getFreeWorldEntryUid(data);
if (!Number.isInteger(newUid)) {
@ -1803,10 +1842,6 @@ function createWorldInfoEntry(name, data, fromSlashCommand = false) {
const newEntry = { uid: newUid, ...structuredClone(newEntryTemplate) };
data.entries[newUid] = newEntry;
if (!fromSlashCommand) {
updateEditor(newUid);
}
return newEntry;
}