소스 파일 : /edit/edit_embed.js (2021-01-01)     소스 설명 : (해설편집) 편집 내용 중 삽입 요소 처리
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
// (2021.1.1, 차재복, Cha Jae Bok, http://www.ktword.co.kr)

// mathjax 편집
function mathjax_edit (div, edit_btn) {

    // textarea 생성
    let textarea = document.createElement('textarea');
    textarea.style.width = '100%';
    textarea.style.whiteSpace = 'pre-wrap';

    // textarea height 조절
    textarea.setAttribute('rows',edit_btn.dataset.math.split('\n').length);

    textarea.innerHTML = edit_btn.dataset.math;

    if(edit_btn.dataset.embed=='pre_btn') textarea.disabled = true;
    
    div.appendChild(textarea);

    // button 생성
    let button = document.createElement('button');
    button.innerHTML = '서버 저장';
//    button.dataset.embed = 'extra';
    div.appendChild(button);

    button.addEventListener('click', function(evt) {
        // 변수 정리
        let mathExpr = evt.target.previousSibling.value;
        let no = edit_btn.dataset.no;
        let in_no = edit_btn.dataset.in_no;

        // 서버 송출 전 확인
        let is_confirm = confirm('no='+no+'\nin_no='+in_no+'\n'+mathExpr);
        if(!is_confirm) return;

        // ajax promise 호출
        let url = 'update.php';
        let method = 'post';
        parms ={'type' : 'math_edit', 'no' : no, 'in_no' : in_no, 'pure_str' : mathExpr};
        ajaxPromise(url, method, parms).then(
            response => {
                alert(response.msg);
                if (response.math) {
                    // 수식 화면 표출
                    let math_out = edit_btn.previousSibling;
                    math_out.innerHTML = response.math;

                    // textarea height 조절
                    textarea.setAttribute('rows',response.math.split('\n').length);
                    
                    // 출력 div에 MathJax 적용
                    MathJax.Hub.Queue(["Typeset",MathJax.Hub,math_out]);

                    // in_no 점검 (용어해설 본문에 아직 mathjax가 그대로 있는 경우) 
                    if(response.r_type=='converted'){
                        edit_btn.dataset.in_no = response.in_no;
                        edit_btn.title = response.in_no;
                        edit_btn.style.color = 'black';
                        math_out.dataset.in_no = response.in_no;
                        math_out.dataset.embed = 'core';
                        math_out.style.border = '1px gray dotted';
                        textarea.disabled = false;
                    }
                }
            },
            error => {
                alert(error);
            }
        );
    });
//    }, true);   // bubbling (기본값,false)-> capturing (true) 으로 이벤트 전파 방향 변경

}


// 이미지,링크,mathjax 등을 제거하고, {[(...)]}로 교체
function before_out(obj, str='') {

    for (let i=0; i<obj.length; i++ ) {

        // 단순 텍스트일 때, 그냥 str에 붙임
        if (obj[i].nodeType==3) {
            str += obj[i].textContent;
        // dataset.embed가 있는 경우 만, {[(...)]}로써 붙임
        } else if (obj[i].dataset.embed) {
                if (obj[i].dataset.embed == 'core') str += '{[('+obj[i].dataset.in_no+')]}';
                if (obj[i].dataset.embed == 'pre_math') str += obj[i].dataset.math;
        // sup 일 때
        } else if (obj[i].nodeName=='SUP') {
            str += '<sup>'+obj[i].textContent+'</sup>';
        // b 일 때
        } else if (obj[i].nodeName=='B') {
            str += '<b>'+obj[i].textContent+'</b>';
        // div 일 때
        } else if(obj[i].nodeName=='DIV') {
            if(!obj[i].hasChildNodes()) continue;

            let childs = obj[i].childNodes;
            for(let j=0; j<childs.length; j++) {
                if (childs[j].nodeType==3) str += childs[j].textContent + '\n';
                else if (childs[j].nodeName=='BR') str += '\n'; // br
                else if (childs[j].dataset.embed == 'core') str += '{[('+childs[j].dataset.in_no+')]}';
                else if (childs[j].dataset.embed == 'pre_math') str += childs[j].dataset.math;
                else alert(childs[j].nodeName);
            }
        }

    }

    return str;
}


Copyrightⓒ written by 차재복 (Cha Jae Bok)
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"