////////////////////////////// 템플릿폼 // 이메일 내용에서 {변수명}을 감지하여 입력 필드를 생성/제거하는 함수 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('
', '
', '