Files
2026-06-11 18:47:38 +09:00

156 lines
5.6 KiB
JavaScript

////////////////////////////// 템플릿폼
// 이메일 내용에서 {변수명}을 감지하여 입력 필드를 생성/제거하는 함수
function detectVariables(content) {
if (!content) return;
// [수정] 정규식의 \w가 한글을 포함하지 못하므로, [a-zA-Z0-9_가-힣]로 변경하여 한글 변수도 감지하도록 합니다.
const matches = [...new Set((content.match(/{([a-zA-Z0-9_가-힣]+)}/g) || []))];
const container = document.getElementById('variableInputs');
const existingVars = new Set();
const newVars = new Set(matches);
container.querySelectorAll('input[data-var]').forEach(input => {
existingVars.add(`{${input.dataset.var}}`);
});
// 새로 발견된 변수는 입력 필드 추가
newVars.forEach(tag => {
if (!existingVars.has(tag)) {
const varName = tag.replace(/[{}]/g, '');
const label = document.createElement('label');
label.textContent = varName;
const input = document.createElement('input');
input.type = 'text';
input.className = 'frm_input';
input.name = `variables[${varName}]`;
input.dataset.var = varName;
input.value = (typeof serverVars !== 'undefined' && serverVars[varName]) ? serverVars[varName] : '';
input.style.width = '95%';
input.placeholder = `${varName}의 기본값 입력`;
container.appendChild(label);
container.appendChild(input);
}
});
// 내용에서 삭제된 변수는 입력 필드 제거
existingVars.forEach(tag => {
if (!newVars.has(tag)) {
const varName = tag.replace(/[{}]/g, '');
const inputToRemove = container.querySelector(`input[data-var="${varName}"]`);
if (inputToRemove) {
if (inputToRemove.previousElementSibling && inputToRemove.previousElementSibling.tagName === 'LABEL') {
inputToRemove.previousElementSibling.remove();
}
inputToRemove.remove();
}
}
});
}
// '변수 새로고침' 버튼 클릭 시 실행될 함수
function refreshVariables() {
var content = '';
// 1. 스마트 에디터 2.0
if (typeof oEditors !== 'undefined' && oEditors.getById['content']) {
content = oEditors.getById['content'].getContents();
}
// 2. CHEditor5
else if (typeof ed_content !== 'undefined') {
content = ed_content.outputBodyHTML();
}
else {
alert('에디터가 로드되지 않았거나 지원하지 않는 에디터입니다.');
return;
}
detectVariables(content);
alert('변수 목록을 새로고침했습니다.');
}
// 미리보기 기능
function previewTemplate() {
let html = '';
// 1. 스마트 에디터 2.0
if (typeof oEditors !== 'undefined' && oEditors.getById['content']) {
html = oEditors.getById['content'].getContents();
}
// 2. CHEditor5
else if (typeof ed_content !== 'undefined') {
html = ed_content.outputBodyHTML();
}
else {
alert('에디터가 로드되지 않았습니다.');
return;
}
const variableInputs = document.querySelectorAll('#variableInputs input');
variableInputs.forEach(input => {
const varName = input.dataset.var;
const value = input.value;
if (value) {
const regex = new RegExp('{' + varName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '}', 'g');
html = html.replace(regex, value);
}
});
const popup = window.open('', '미리보기', 'width=800,height=600,scrollbars=yes');
popup.document.write('<html><head><title>템플릿 미리보기</title></head><body>' + html + '</body></html>');
popup.document.close();
}
// 폼 전송 시 에디터 내용 업데이트 및 유효성 검사
function form_check(f) {
// 1. 스마트 에디터 2.0
if (typeof oEditors !== 'undefined' && oEditors.getById['content']) {
oEditors.getById['content'].exec("UPDATE_CONTENTS_FIELD", []);
sanitizeEditorContent();
}
// 2. CHEditor5
else if (typeof ed_content !== 'undefined') {
f.content.value = ed_content.outputBodyHTML();
}
// 에디터 내용이 비어있는지 체크
const content = jQuery.trim(f.content.value);
const emptyPatterns = ['', '<p>&nbsp;</p>', '<p><br></p>', '<div><br></div>', '&nbsp;'];
// 태그 제거 후 공백 체크
const textContent = content.replace(/<[^>]*>?/gm, '').trim();
if (textContent === '' && emptyPatterns.includes(content.toLowerCase())) {
alert('내용을 입력해 주십시오.');
if (typeof oEditors !== 'undefined') {
oEditors.getById['content'].exec("FOCUS");
} else if (typeof ed_content !== 'undefined') {
ed_content.returnFalse();
} else {
f.content.focus();
}
return false;
}
return true;
}
// 에디터 내용 정제 - 빈 값 판별 및 공백 처리 (스마트 에디터용)
function sanitizeEditorContent() {
if (typeof oEditors === 'undefined' || !oEditors.getById['content']) return;
const contentEl = document.getElementById('content');
const html = oEditors.getById['content'].getIR();
oEditors.getById['content'].exec('UPDATE_CONTENTS_FIELD', []);
const val = contentEl.value.toLowerCase().replace(/^\s*|\s*$/g, '');
const emptyPatterns = ['&nbsp;', '<p>&nbsp;</p>', '<p><br></p>', '<div><br></div>', '<p></p>', '<br>', ''];
if (emptyPatterns.includes(val)) {
contentEl.value = '';
}
}