简体   繁体   English

Flask表单具有丰富的文本就地编辑功能

[英]Flask form with rich text in-place editing

I want to let approved users edit their own Rich Text form posts. 我想让批准的用户编辑自己的Rich Text表单帖子。 I know about TinyMCE, CKEditor, X-editable , wtf_tinymce etc. but I cannot find a single consistent example of being able to post form data from an inline editable rich text field. 我知道TinyMCE,CKEditor, X-editablewtf_tinymce等等,但我找不到能够从内联可编辑富文本字段发布表单数据的单一一致示例。

This ' Flask Biography ' page comes closest I believe but it looks complex; 我相信这个' Flask Biography '页面最接近,但看起来很复杂; TinyCME also has an inline editing example but that requires a div element rather than a conventional form one, so I don't see how to get the data from that. TinyCME也有一个内联编辑示例,但需要一个div元素而不是传统的形式,所以我不知道如何从中获取数据。

How do I use a rich text editor on a Flask form field? 如何在Flask表单字段上使用富文本编辑器?

The SO Python chat room has a website, https://sopython.com/ with a wiki and some other tools. SO Python聊天室有一个网站, https://sopython.com/ ,带有wiki和其他一些工具。 We use Ace as a text field enhancement. 我们使用Ace作为文本字段增强。 Other text editors presumably work similarly. 其他文本编辑器可能工作方式类似。

Create a basic text area as normal, and mark it in some way to indicate that the editor should replace it. 像往常一样创建一个基本文本区域,并以某种方式标记它以指示编辑器应该替换它。 Use the editor's JavaScript API to create an editor, link it to the text area, and replace the text area. 使用编辑器的JavaScript API创建编辑器,将其链接到文本区域,然后替换文本区域。 Submitting the form still submits the text field, which is updated by the editor. 提交表单仍然会提交文本字段,该字段由编辑器更新。

Here's how we integrate Ace: 以下是我们整合Ace的方式:

<textarea data-editor="markdown"></textarea>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
<script type="text/javascript">
    // https://gist.github.com/duncansmart/5267653
    // find each text area marked to have an editor
    $('textarea[data-editor]').each(function() {
        var textarea = $(this);
        var mode = textarea.data('editor');
        // create the editor div
        var div = $('<div>', {
            'width': textarea.outerWidth(),
            'height': textarea.outerHeight(),
            'class': textarea.attr('class')
        }).insertBefore(textarea);
        // hide the original text area
        textarea.hide();
        // configure the editor
        var editor = ace.edit(div[0]);
        var session = editor.getSession();
        editor.setTheme("ace/theme/github");
        session.setValue(textarea.val());
        session.setMode('ace/mode/' + mode);
        session.setNewLineMode('unix');
        session.setTabSize(4);
        session.setUseSoftTabs(true);
        session.setUseWrapMode(true);
        // update the text area before submitting the form
        textarea.closest('form').submit(function() {
            textarea.val(editor.getSession().getValue());
        });
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM