Textarea编辑器不工作,但Textarea自身工作

2024-04-28 21:07:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我从here得到了一个所见即所得文本编辑器,但它对我不起作用。我不知道我是否做错了什么,但是当我尝试在没有文本编辑器的情况下使用文本区域编辑文本时,它会起作用。以下是我的工作HTML:

<center>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class="flashes">
{% for message in messages %}
    <li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</center>

这是我的烧瓶应用程序:

if 'loggedin' in session:
if request.method == "POST" and 'loggedin' in session and 'title' in request.form and 'message' in request.form:
    title = request.form['title']
    message = request.form['message']
    author = session['username']

    if title == '':
        flash('Your post must have a title!')
        redirect(url_for('post'))
    elif message == '':
        flash("You didn't write anything")
        redirect(url_for('post'))
    elif len(title) > 50:
        flash('The title can contain up to 50 characters!')
        redirect(url_for('post'))
    elif len(title) < 3:
        flash('The title must be over 3 characters!')
        redirect(url_for('post'))
    elif len(message) < 3:
        flash('The message must be over 3 characters!')
    else:
        # Napravi cursor
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)

        # Izvrši
        cursor.execute("INSERT INTO posts_announcements(title, message, author) VALUES (%s, %s, %s)", (title, message, author))
        mysql.connection.commit()

        # Zatvori 
        cursor.close()
        flash("Your message has been posted!")
        return render_template('announcements.html', username=session['username'])

其他: 返回重定向(url_用于('announces')) 返回呈现模板('post.html')

我真的很想使用这个文本编辑器,它改变了游戏规则。Javascript配置正确,我可以编辑文本,但不能发布任何内容。我经常收到一条短信:“你什么都没写。”

编辑:这是文本编辑器代码

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Helvetica Neue', 'Helvetica', arial, sans-serif;
}
.editor {
width: 40rem;
min-height: 18rem;
box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.3);
border-top: 6px solid #4a4a4a;
border-radius: 3px;
margin: 2rem;
}
.editor .toolbar {
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}
.editor .toolbar .line {
display: flex;
border-bottom: 1px solid #e2e2e2;
}
.editor .toolbar .line:last-child {
border-bottom: none;
}
.editor .toolbar .line .box {
display: flex;
border-left: 1px solid #e2e2e2;
}
.editor .toolbar .line .box .btn {
display: block;
display: flex;
align-items: center;
justify-content: center;
position: relative;
transition: 0.2s ease all;
}
.editor .toolbar .line .box .btn:hover, .editor .toolbar .line .box 
.btn.active {
background-color: #e1e1e1;
cursor: pointer;
}
.editor .toolbar .line .box .btn.icon img {
width: 15px;
padding: 10px;
}
.editor .toolbar .line .box .btn.icon.smaller img {
width: 12px;
}
.editor .toolbar .line .box .btn.has-submenu {
width: 20px;
padding: 0 10px;
}
.editor .toolbar .line .box .btn.has-submenu::after {
content: '';
width: 6px;
height: 6px;
position: absolute;
background-image: url(https://image.flaticon.com/icons/svg/25/25243.svg);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
right: 4px;
}
.editor .toolbar .line .box .btn.has-submenu .submenu {
display: none;
position: absolute;
top: 36px;
left: -1px;
z-index: 10;
background-color: #fff;
border: 1px solid #b5b5b5;
border-top: none;
}
.editor .toolbar .line .box .btn.has-submenu .submenu .btn {
width: 39px;
}
.editor .toolbar .line .box .btn.has-submenu:hover .submenu {
display: block;
}
.editor .content-area {
padding: 15px 12px;
line-height: 1.5;
}
.editor .content-area .visuell-view {
outline: none;
}
.editor .content-area .visuell-view p {
margin: 12px 0;
}
.editor .content-area .html-view {
outline: none;
display: none;
width: 100%;
height: 200px;
border: none;
resize: none;
}

</style>
</head>

<body>
<center>
        {% with messages = get_flashed_messages() %}
        {% if messages %}
        <ul class="flashes">
        {% for message in messages %}
            <li>{{ message }}</li>
        {% endfor %}
        </ul>
        {% endif %}
        {% endwith %}
        </center>
    </div>
<form id="message" name="message" method="post" autocomplete="off"> 
<input type="text" name="title" placeholder="Title">
<div class="editor">
  <div class="toolbar">
    <div class="line">
      
      <div class="box">
        <span class="btn icon smaller" data-action="bold" title="Bold">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25432.svg">
        </span>
        <span class="btn icon smaller" data-action="italic" 
title="Italic">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25392.svg">
        </span>
        <span class="btn icon smaller" data-action="underline" 
title="Underline">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25433.svg">
        </span>
        <span class="btn icon smaller" data-action="strikeThrough" 
title="Strike through">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25626.svg">
        </span>
      </div>
      
      <div class="box">
        <span class="btn icon has-submenu">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25351.svg">
          <div class="submenu">
            <span class="btn icon" data-action="justifyLeft" 
title="Justify left">
              <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25351.svg">  
            </span>
            <span class="btn icon" data-action="justifyCenter" 
title="Justify center">
              <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25440.svg">  
            </span>
            <span class="btn icon" data-action="justifyRight" 
title="Justify right">
              <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25288.svg">  
            </span>
            <span class="btn icon" data-action="formatBlock" 
title="Justify block">
              <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25181.svg">  
            </span>
          </div>
        </span>
        <span class="btn icon" data-action="insertOrderedList" 
title="Insert ordered list">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25242.svg">  
        </span>
        <span class="btn icon" data-action="insertUnorderedList" 
title="Insert unordered list">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25648.svg">  
        </span>
        <span class="btn icon" data-action="outdent" title="Outdent">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25410.svg">  
        </span>
        <span class="btn icon" data-action="indent" title="Indent">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25233.svg">  
        </span>
        
      </div>
      <div class="box">
        <span class="btn icon" data-action="insertHorizontalRule" 
title="Insert horizontal rule">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25232.svg">  
        </span>
      </div>
      
    </div>
    <div class="line">
      
      <div class="box">
        <span class="btn icon smaller" data-action="undo" title="Undo">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25249.svg">
        </span>
        <span class="btn icon" data-action="removeFormat" title="Remove 
format">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25454.svg">  
        </span>
      </div>
      
      <div class="box">
        <span class="btn icon smaller" data-action="createLink" 
title="Insert Link">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25385.svg">
        </span>
        <span class="btn icon smaller" data-action="unlink" 
title="Unlink">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25341.svg">
        </span>
      </div>
      <div class="box">
        <span class="btn icon" data-action="code" title="Show HTML-Code">
          <img loading="lazy" 
src="https://image.flaticon.com/icons/svg/25/25185.svg">
        </span>
      </div>
      
    </div>
  </div>
  <div class="content-area">
    <div class="visuell-view" contenteditable>
    </div>
    <textarea form="message" name="message" class="html-view" 
spellcheck="false"></textarea>
  </div>
</div>
<input type="submit" name="post" value="Post">
</form>
<script>
const editor = document.getElementsByClassName('editor')[0];
const toolbar = editor.getElementsByClassName('toolbar')[0];
const buttons = toolbar.querySelectorAll('.btn:not(.has-submenu)');
for(let i = 0; i < buttons.length; i++) {
let button = buttons[i];

button.addEventListener('click', function(e) {
let action = this.dataset.action;

switch(action) {
  case 'code':
    execCodeAction(this, editor);
    break;
  case 'createLink':
    execLinkAction();
    break;
  default:
    execDefaultAction(action);
}

});
}
function execCodeAction(button, editor) {
const contentArea = editor.getElementsByClassName('content-area')[0];
const visuellView = contentArea.getElementsByClassName('visuell-view')[0];
const htmlView = contentArea.getElementsByClassName('html-view')[0];
if(button.classList.contains('active')) { // show visuell view
visuellView.innerHTML = htmlView.value;
htmlView.style.display = 'none';
visuellView.style.display = 'block';
button.classList.remove('active');     
} else {  // show html view
htmlView.innerText = visuellView.innerHTML;
visuellView.style.display = 'none';
htmlView.style.display = 'block';
button.classList.add('active'); 
}
}
function execLinkAction() {
let linkValue = prompt('Link (e.g. https://webdeasy.de/)');
document.execCommand('createLink', false, linkValue);
}
function execDefaultAction(action) {
document.execCommand(action, false);
}
</script>

</body>
</html>

Tags: httpssvgimagedivcomimgtitleaction