handle deleting

This commit is contained in:
LenAnderson
2023-12-20 17:56:08 +00:00
parent 41a88e165c
commit ac09fa6019
5 changed files with 96 additions and 82 deletions

View File

@ -102,7 +102,8 @@ const loadSettings = async () => {
}
try {
settings = QuickReplySettings.from(extension_settings.quickReplyV2);
} catch {
} catch (ex) {
debugger;
settings = QuickReplySettings.from(defaultSettings);
}
};

View File

@ -16,7 +16,7 @@ export class QuickReplyConfig {
static from(props) {
props.setList = props.setList?.map(it=>QuickReplySetLink.from(it)) ?? [];
props.setList = props.setList?.map(it=>QuickReplySetLink.from(it))?.filter(it=>it.set) ?? [];
const instance = Object.assign(new this(), props);
instance.init();
return instance;
@ -50,7 +50,7 @@ export class QuickReplyConfig {
delay: getSortableDelay(),
stop: ()=>this.onSetListSort(),
});
this.setList.forEach((qrl,idx)=>setList.append(qrl.renderSettings(idx)));
this.setList.filter(it=>!it.set.isDeleted).forEach((qrl,idx)=>setList.append(qrl.renderSettings(idx)));
}

View File

@ -26,17 +26,16 @@ export class QuickReplySet {
/**@type {String}*/ name;
/**@type {Boolean}*/ disableSend = false;
/**@type {Boolean}*/ placeBeforeInput = false;
/**@type {Boolean}*/ injectInput = false;
/**@type {QuickReply[]}*/ qrList = [];
/**@type {Number}*/ idIndex = 0;
/**@type {Function}*/ save;
/**@type {Boolean}*/ isDeleted = false;
/**@type {Function}*/ save;
/**@type {HTMLElement}*/ dom;
/**@type {HTMLElement}*/ settingsDom;
@ -201,6 +200,7 @@ export class QuickReplySet {
this.unrender();
const idx = QuickReplySet.list.indexOf(this);
QuickReplySet.list.splice(idx, 1);
this.isDeleted = true;
} else {
warn(`Failed to delete Quick Reply Set: ${this.name}`);
}

View File

@ -1,9 +1,14 @@
import { QuickReplySet } from './QuickReplySet.js';
export class QuickReplySetLink {
static from(props) {
props.set = QuickReplySet.get(props.set);
return Object.assign(new this(), props);
/**@type {QuickReplySetLink}*/
const instance = Object.assign(new this(), props);
return instance;
}
@ -24,7 +29,6 @@ export class QuickReplySetLink {
renderSettings(idx) {
if (!this.settingsDom || idx != this.index) {
this.index = idx;
const item = document.createElement('div'); {
this.settingsDom = item;
@ -86,7 +90,6 @@ export class QuickReplySetLink {
item.append(del);
}
}
}
return this.settingsDom;
}
unrenderSettings() {

View File

@ -79,17 +79,26 @@ export class SettingsUi {
}
prepareGlobalSetList() {
this.settings.config.renderSettingsInto(this.dom.querySelector('#qr--global'));
const dom = this.template.querySelector('#qr--global');
const clone = dom.cloneNode(true);
// @ts-ignore
this.settings.config.renderSettingsInto(clone);
this.dom.querySelector('#qr--global').replaceWith(clone);
}
prepareChatSetList() {
const dom = this.template.querySelector('#qr--chat');
const clone = dom.cloneNode(true);
if (this.settings.chatConfig) {
this.settings.chatConfig.renderSettingsInto(this.dom.querySelector('#qr--chat'));
// @ts-ignore
this.settings.chatConfig.renderSettingsInto(clone);
} else {
const info = document.createElement('div'); {
info.textContent = 'No active chat.';
// @ts-ignore
clone.append(info);
}
this.dom.querySelector('#qr--chat').append(info);
}
this.dom.querySelector('#qr--chat').replaceWith(clone);
}
prepareQrEditor() {
@ -201,19 +210,8 @@ export class SettingsUi {
async deleteQrSet() {
const confirmed = await callPopup(`Are you sure you want to delete the Quick Reply Set "${this.currentQrSet.name}"? This cannot be undone.`, 'confirm');
if (confirmed) {
const idx = QuickReplySet.list.indexOf(this.currentQrSet);
await this.currentQrSet.delete();
this.currentSet.children[idx].remove();
[
...Array.from(this.globalSetList.querySelectorAll('.qr--item > .qr--set')),
...Array.from(this.chatSetList.querySelectorAll('.qr--item > .qr--set')),
]
// @ts-ignore
.filter(it=>it.value == this.currentQrSet.name)
// @ts-ignore
.forEach(it=>it.closest('.qr--item').querySelector('.qr--del').click())
;
this.onQrSetChange();
this.rerender();
}
}
@ -230,20 +228,32 @@ export class SettingsUi {
QuickReplySet.list[QuickReplySet.list.indexOf(oldQrs)] = qrs;
this.currentSet.value = name;
this.onQrSetChange();
this.prepareGlobalSetList();
this.prepareChatSetList();
}
} else {
const qrs = new QuickReplySet();
qrs.name = name;
qrs.addQuickReply();
const idx = QuickReplySet.list.findIndex(it=>it.name.localeCompare(name) == 1);
if (idx > -1) {
QuickReplySet.list.splice(idx, 0, qrs);
} else {
QuickReplySet.list.push(qrs);
}
const opt = document.createElement('option'); {
opt.value = qrs.name;
opt.textContent = qrs.name;
if (idx > -1) {
this.currentSet.children[idx].insertAdjacentElement('beforebegin', opt);
} else {
this.currentSet.append(opt);
}
}
this.currentSet.value = name;
this.onQrSetChange();
this.prepareGlobalSetList();
this.prepareChatSetList();
}
}
}