Chiaming Yen's Cyber Universe

  • Home
    • SMap
    • reveal
    • blog
  • About
    • KMOL
    • MDE
    • ToDo
    • Google
      • Blogger
      • Youtube
  • Python
  • Portable
    • Lua 解譯
    • Wink
      • Add Wink
  • CMSiMDE
    • 建立網頁
    • 基本操作
      • 客製化
      • 編輯網頁內容
      • 上傳與引用檔案
      • 上傳與引用圖片
      • 嵌入程式碼
      • 嵌入影片檔
      • 檢視 STL
    • 置入 disqus
    • Gitlab 同步
    • Heroku 部署
    • Fossil SCM
    • 注意事項
      • 標題選擇
      • 靜態網頁 404
      • 動態網站錯誤
    • 延伸開發
    • 已知問題
      • IPv6
      • 上傳資料引用
      • MathJax
      • html 分頁
      • 靜態搜尋
      • template
      • 重複標題頁面
    • ajax
    • black
    • summernote
重複標題頁面 << Previous Next >> black

ajax

共計有四個地方必須修改: https://mde.tw/lab/blog/2021-kmol-cmsimde-ajax-editor-save.html

第一就是 sh4tinymce 長久以來的 bug. 可以透過 tinymce.activeEditor.setDirty(true); 知會 editor, 在 onSubmitFunction 將 highlighter 引用的程式碼插入 editor 時, 讓表單中的 Save 可以點擊.

另外兩項更動, 則是將 savePage 與 ssavePage 中移除跳行的程式碼, 予以註解.

最後一項修改則是 tinymce_editor() 函式:

def tinymce_editor(menu_input=None, editor_content=None, page_order=None):
    
    """Tinymce editor scripts
    """
    
    sitecontent =file_get_contents(config_dir + "content.htm")
    editor = set_admin_css() + editorhead() + '''</head>''' + editorfoot()
    # edit all pages
    if page_order is None:
        outstring = editor + "<div class='container'><nav>" + \
                        menu_input + "</nav><section><form onsubmit='return save_all_data(this)'> \
                        <textarea class='simply-editor' name='page_content' cols='50' rows='15'>" +  \
                        editor_content + "</textarea><input type='button' onClick='save_all()' value='save'>"
        outstring +="""
        <script>
        // leave  warning when modification not saved
        window.addEventListener('beforeunload', function(e) {
        var myPageIsDirty = tinymce.activeEditor.isDirty()
        if(myPageIsDirty) {
            //following two lines will cause the browser to ask the user if they
            //want to leave. The text of this dialog is controlled by the browser.
            e.preventDefault(); //per the standard
            e.returnValue = ''; //required for Chrome
        }
        //else: user is allowed to leave without a warning dialog
        });
        
        function tempAlert(msg,duration)
            {
             var el = document.createElement("div");
             el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:lightgreen;");
             el.innerHTML = msg;
             setTimeout(function(){
              el.parentNode.removeChild(el);
             },duration);
             document.body.appendChild(el);
            }
        
        function save_all(){
            tinymce.activeEditor.execCommand('mceSave');
        }
        
        function save_all_data(form) {
                var page_content = $('textarea#page_content').val();
                
                $.ajax({
                    type: "POST",
                    url: "/savePage",
                    data: {"page_content": page_content},
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                    success: function() {
                        //document.getElementById("notice").innerHTML = "saved!";
                        parser = new DOMParser();
                        parsed = parser.parseFromString(page_content, 'text/html');
                        paragraphs = parsed.querySelectorAll('h1, h2, h3');
                        //alert(paragraphs.length)
                        //tempAlert("saved!", 700);

                        if (paragraphs.length > 1 || paragraphs.length == 0)
                        {
                            // when no title page will cause reload to error page
                            //window.location.reload();
                            document.location.href="/";
                        }
                        else
                        {
                            tempAlert("saved!", 700);
                        }
                    }
                 }); 
        }
        </script>
        """
        outstring += "</form></section></body></html>"
    else:
        # add viewpage button while single page editing
        head, level, page = parse_content()
        outstring = "<p id='notice'></p>"
        outstring  += editor + "<div class='container'><nav>" + \
                        menu_input+"</nav><section><form onsubmit='return save_data(this)'> \
                        <textarea class='simply-editor' id='page_content' name='page_content' cols='50' rows='15'>" + \
                        editor_content + "</textarea><input type='hidden'  id='page_order' name='page_order' value='" + \
                        str(page_order) + "'>"
        # add an extra collaborative save button
        outstring += """<input type="button" onClick="ssave()"  value="save">"""
        outstring += """<input type="button" onClick="cssave()"  value="csave">"""

        outstring +="""
        <script>
        // leave  warning when modification not saved
        window.addEventListener('beforeunload', function(e) {
        var myPageIsDirty = tinymce.activeEditor.isDirty()
        if(myPageIsDirty) {
            //following two lines will cause the browser to ask the user if they
            //want to leave. The text of this dialog is controlled by the browser.
            e.preventDefault(); //per the standard
            e.returnValue = ''; //required for Chrome
        }
        //else: user is allowed to leave without a warning dialog
        });
        
        function tempAlert(msg,duration)
            {
             var el = document.createElement("div");
             el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:lightgreen;");
             el.innerHTML = msg;
             setTimeout(function(){
              el.parentNode.removeChild(el);
             },duration);
             document.body.appendChild(el);
            }
            
        // default action is "save"
        var action ="save";
        
        function cssave(){
            action = "csave";
            tinymce.activeEditor.execCommand('mceSave');
        }
        
        function ssave(){
            action = "save";
            tinymce.activeEditor.execCommand('mceSave');
        }
        
        function save_data(form) {
                var page_content = $('textarea#page_content').val();
                var page_order = $('#page_order').val();
                
                $.ajax({
                    type: "POST",
                    url: "/ssavePage",
                    data: {"page_content": page_content, "page_order": page_order, "action": action},
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                    success: function() {
                        //document.getElementById("notice").innerHTML = "saved!";
                        parser = new DOMParser();
                        parsed = parser.parseFromString(page_content, 'text/html');
                        paragraphs = parsed.querySelectorAll('h1, h2, h3');
                        //alert(paragraphs.length)
                        //tempAlert("saved!", 700);

                        if (paragraphs.length > 1 || paragraphs.length == 0 )
                        {
                            //window.location.reload();
                            document.location.href="/";
                        }
                        else
                        {
                            tempAlert("saved!", 700);
                        }
                    }
                 }); 
        }
        </script>
        """
        outstring += '''<input type=button onClick="location.href='/get_page/''' + \
                    head[page_order] + \
                    ''''" value='viewpage'></form></section></body></html>'''
    return outstring
    


重複標題頁面 << Previous Next >> black

Copyright © All rights reserved | This template is made with by Colorlib