add ctrl-enter to execute from editor

This commit is contained in:
LenAnderson 2024-04-07 17:01:26 -04:00
parent 21d3a7dc3e
commit 63cbfda9b1
2 changed files with 43 additions and 16 deletions

View File

@ -25,6 +25,10 @@
<span>Tab size:</span> <span>Tab size:</span>
<input type="number" min="1" max="9" id="qr--modal-tabSize" class="text_pole"> <input type="number" min="1" max="9" id="qr--modal-tabSize" class="text_pole">
</label> </label>
<label class="checkbox_label">
<input type="checkbox" id="qr--modal-executeShortcut">
<span>Ctrl+Enter to execute</span>
</label>
</div> </div>
<textarea class="monospace" id="qr--modal-message"></textarea> <textarea class="monospace" id="qr--modal-message"></textarea>
</div> </div>

View File

@ -44,6 +44,11 @@ export class QuickReply {
/**@type {HTMLInputElement}*/ settingsDomLabel; /**@type {HTMLInputElement}*/ settingsDomLabel;
/**@type {HTMLTextAreaElement}*/ settingsDomMessage; /**@type {HTMLTextAreaElement}*/ settingsDomMessage;
/**@type {HTMLElement}*/ editorExecuteBtn;
/**@type {HTMLElement}*/ editorExecuteErrors;
/**@type {HTMLInputElement}*/ editorExecuteHide;
/**@type {Promise}*/ editorExecutePromise;
get hasContext() { get hasContext() {
return this.contextList && this.contextList.length > 0; return this.contextList && this.contextList.length > 0;
@ -231,6 +236,12 @@ export class QuickReply {
localStorage.setItem('qr--tabSize', JSON.stringify(Number(tabSize.value))); localStorage.setItem('qr--tabSize', JSON.stringify(Number(tabSize.value)));
updateTabSize(); updateTabSize();
}); });
/**@type {HTMLInputElement}*/
const executeShortcut = dom.querySelector('#qr--modal-executeShortcut');
executeShortcut.checked = JSON.parse(localStorage.getItem('qr--executeShortcut') ?? 'true');
executeShortcut.addEventListener('click', () => {
localStorage.setItem('qr--executeShortcut', JSON.stringify(executeShortcut.checked));
});
/**@type {HTMLTextAreaElement}*/ /**@type {HTMLTextAreaElement}*/
const message = dom.querySelector('#qr--modal-message'); const message = dom.querySelector('#qr--modal-message');
updateWrap(); updateWrap();
@ -268,6 +279,12 @@ export class QuickReply {
message.selectionStart = start - 1; message.selectionStart = start - 1;
message.selectionEnd = end - count; message.selectionEnd = end - count;
this.updateMessage(message.value); this.updateMessage(message.value);
} else if (evt.key == 'Enter' && evt.ctrlKey && !evt.shiftKey && !evt.altKey) {
evt.stopPropagation();
evt.preventDefault();
if (executeShortcut.checked) {
this.executeFromEditor();
}
} }
}); });
@ -396,27 +413,15 @@ export class QuickReply {
/**@type {HTMLElement}*/ /**@type {HTMLElement}*/
const executeErrors = dom.querySelector('#qr--modal-executeErrors'); const executeErrors = dom.querySelector('#qr--modal-executeErrors');
this.editorExecuteErrors = executeErrors;
/**@type {HTMLInputElement}*/ /**@type {HTMLInputElement}*/
const executeHide = dom.querySelector('#qr--modal-executeHide'); const executeHide = dom.querySelector('#qr--modal-executeHide');
let executePromise; this.editorExecuteHide = executeHide;
/**@type {HTMLElement}*/ /**@type {HTMLElement}*/
const executeBtn = dom.querySelector('#qr--modal-execute'); const executeBtn = dom.querySelector('#qr--modal-execute');
this.editorExecuteBtn = executeBtn;
executeBtn.addEventListener('click', async()=>{ executeBtn.addEventListener('click', async()=>{
if (executePromise) return; await this.executeFromEditor();
executeBtn.classList.add('qr--busy');
executeErrors.innerHTML = '';
if (executeHide.checked) {
document.querySelector('#shadow_popup').classList.add('qr--hide');
}
try {
executePromise = this.execute();
await executePromise;
} catch (ex) {
executeErrors.textContent = ex.message;
}
executePromise = null;
executeBtn.classList.remove('qr--busy');
document.querySelector('#shadow_popup').classList.remove('qr--hide');
}); });
await popupResult; await popupResult;
@ -425,6 +430,24 @@ export class QuickReply {
} }
} }
async executeFromEditor() {
if (this.editorExecutePromise) return;
this.editorExecuteBtn.classList.add('qr--busy');
this.editorExecuteErrors.innerHTML = '';
if (this.editorExecuteHide.checked) {
document.querySelector('#shadow_popup').classList.add('qr--hide');
}
try {
this.editorExecutePromise = this.execute();
await this.editorExecutePromise;
} catch (ex) {
this.editorExecuteErrors.textContent = ex.message;
}
this.editorExecutePromise = null;
this.editorExecuteBtn.classList.remove('qr--busy');
document.querySelector('#shadow_popup').classList.remove('qr--hide');
}