[英]How can I keep the line breaks (formatting) of an element appended to a list via a contenteditable div input?
I've a bit of a problem.我有点问题。 I'm currently programming a list, where users can enter some hints/messages.我目前正在编写一个列表,用户可以在其中输入一些提示/消息。 This works quite well but there is a problem I've found and I don't like it.这很有效,但我发现了一个问题,我不喜欢它。
To prevent any HTML input, I've implemented some stuff inside a paste
function that gives me any plain text in case there is HTML.为了防止任何 HTML 输入,我在paste
函数中实现了一些东西,如果有 HTML,它会给我任何纯文本。 When I now copy some code that has multiple lines like:当我现在复制一些具有多行的代码时,例如:
$( "button" ).click( function () {
let template = $( "#template" ).clone();
template.removeAttr("id");
template.html( input.html() );
input.empty();
$( "#wrapper" ).append( template );
} );
and paste it into my contenteditable div
, the line formatting is still there.并将其粘贴到我的contenteditable div
,行格式仍然存在。 If I now press my button to append it to the list, the formatting is completely gone and everything is in one single line.如果我现在按下按钮将其附加到列表中,格式将完全消失,所有内容都在一行中。
I'm looking now for a way to fix this.我现在正在寻找解决此问题的方法。 Does anyone has an idea how I prevent loosing any of the line breaks and formatting?有谁知道我如何防止丢失任何换行符和格式?
jQuery(document).ready(function($) { let input = $("#input"); $("button").click(function() { let template = $("#template").clone(); template.removeAttr("id"); template.html(input.html()); input.empty(); $("#wrapper").append(template); }); input.on("paste", function(e) { e.preventDefault(); let clipboardText = e.originalEvent.clipboardData.getData('text'); document.execCommand("insertHTML", false, clipboardText.replace(/\\t/g, " ")); }); });
[contenteditable=true] { border: 1px solid #aaaaaa; padding: 8px; border-radius: 12px; margin-bottom: 20px; white-space: pre-wrap; word-wrap: break-word; } [contenteditable=true]:empty:before { content: attr(placeholder); display: block; color: #aaaaaa; } #wrapper { border: 1px solid; height: 350px; overflow-y: scroll; overflow-x: hidden; margin-bottom: 10px; padding: 15px; } .entry { display: flex; background-color: gray; margin-bottom: 8px; padding: 4px; } #template { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="wrapper"> <span id="template" class="entry"></span> </div> <div id="input" placeholder="Write a message..." contenteditable="true" spellcheck="true"></div> <button>Add to list</button>
Edit编辑
I'm already replacing tabs by whitespaces to move around a possible issue here.我已经用空格替换制表符来解决这里可能出现的问题。
You can just add white-space: pre;
你可以只添加white-space: pre;
to your css .entry
class, like so:到您的 css .entry
类,如下所示:
jQuery(document).ready(function($) { let input = $("#input"); $("button").click(function() { let template = $("#template").clone(); template.removeAttr("id"); template.html(input.html()); input.empty(); $("#wrapper").append(template); }); input.on("paste", function(e) { e.preventDefault(); let clipboardText = e.originalEvent.clipboardData.getData('text'); document.execCommand("insertHTML", false, clipboardText.replace(/\\t/g, " ")); }); });
[contenteditable=true] { border: 1px solid #aaaaaa; padding: 8px; border-radius: 12px; margin-bottom: 20px; white-space: pre-wrap; word-wrap: break-word; } [contenteditable=true]:empty:before { content: attr(placeholder); display: block; color: #aaaaaa; } #wrapper { border: 1px solid; height: 350px; overflow-y: scroll; overflow-x: hidden; margin-bottom: 10px; padding: 15px; } .entry { display: flex; white-space: pre; background-color: gray; margin-bottom: 8px; padding: 4px; } #template { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="wrapper"> <span id="template" class="entry"></span> </div> <div id="input" placeholder="Write a message..." contenteditable="true" spellcheck="true"></div> <button>Add to list</button>
Using white-space: pre
will tell the browser to preserve white space, and text will only wrap on line breaks.使用white-space: pre
会告诉浏览器保留空白,文本只会在换行符处换行。 Acts like the <pre>
tag in HTML.就像 HTML 中的<pre>
标签一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.