简体   繁体   English

如何通过 contenteditable div 输入保留附加到列表的元素的换行符(格式)?

[英]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.

相关问题 从 contenteditable div 中去除 HTML 格式,但在粘贴时保留换行符? - Strip HTML formatting from contenteditable div but preserve line breaks on paste? 我可以在可自动换行的div标签中用换行符得到“换行符”吗? - Can I get the 'line breaks' in a contenteditable div-tag that's word-wrapping? 如何知道用户是否删除了 contenteditable div 中的元素? - How can I know if an user deleted an element in a contenteditable div? 如何将文件输入中的图像插入到可信的div中? - How can I insert an image from a file input into a contenteditable div? 如何获得一个可疑的html元素的光标行号? - How can i get the cursor line number of a contenteditable html element? div content中的脚本插入换行符 - Script insert line breaks in div contentEditable 如何在contentEditable或其他地方找到通过换行创建的换行符? - How can I find the line-breaks created by word-wrapping in contentEditable or elsewhere? 在textarea中保持输入连续2个换行符(提交前预览div) - keep input to 2 line breaks consecutively in textarea (preview div before submit) 设置contenteditable和覆盖换行符 <br /> 代替 <div> 在元素的末尾不起作用 - Setting contenteditable and overwriting line-breaks to <br /> instead of <div> does not work at the end of the element 如何在换行符上自定义contentEditable的插入div? - How to customize inserted divs of contentEditable on line breaks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM