Files
kupshop/admin/static/llm-modal/common/textUtil.ts
2025-08-02 16:30:27 +02:00

72 lines
1.7 KiB
TypeScript

import { ResponseHistory } from '../context/ModalContext';
export function getTargetText(target: string) {
if (window.CKEDITOR !== undefined) {
const editor = CKEDITOR?.instances?.[target];
if (editor) {
return editor.getData() as string;
}
}
return ($('[name="' + target + '"]').val() ?? '') as string;
}
export function isTargetCkeditor(target: string): boolean {
if (window.CKEDITOR !== undefined) {
return !!CKEDITOR?.instances[target];
}
return false;
}
export function setTargetText(target: string, text: string) {
window.parent.postMessage({
event: 'llm-modal-submit',
payload: text,
});
if (window.CKEDITOR !== undefined) {
const editor = CKEDITOR?.instances[target];
if (editor) {
editor.setData(text);
return;
}
}
const $input = $('[name="' + target + '"]');
if ($input.length) {
$input.val(text);
$input.trigger('change');
}
}
export function getCurrentResponse(responses: ResponseHistory[], activeResponse: number) {
if (!responses.length) {
return null;
}
if (activeResponse < responses.length) {
return responses[activeResponse];
}
return responses?.[responses.length - 1];
}
export function onKeyboardEscape(e: any, callback: () => void) {
if (e.key === 'Escape') {
e.preventDefault();
callback();
}
}
export function onKeyboardShiftEnter(e: any, callback?: () => void) {
if (e.shiftKey) {
onKeyboardEnter(e, callback);
}
}
export function onKeyboardEnter(e: any, callback?: () => void) {
if (e.key === 'Enter' || e.which === 13) {
e.preventDefault();
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
callback && callback();
}
}