简体   繁体   English

在 tinymce 所见即所得编辑器中保留空白换行符

[英]Preserve white space line breaks in tinymce wysiwyg editor

I have a tinymce editor implemented in my react project installed through the package我在通过包安装的 react 项目中实现了一个 tinymce 编辑器

"@tinymce/tinymce-react": "^3.12.6". 

I am trying to preserve the white space line breaks of the data which comes from external source (excel file) when being edited in the wysiwyg editor but I have not been able to do so.在所见即所得编辑器中编辑时,我试图保留来自外部源(excel文件)的数据的空白换行符,但我无法这样做。 I have tried the options such as force_br_newlines and convert_newlines_to_brs but it does not seem to help我尝试了诸如 force_br_newlines 和 convert_newlines_to_brs 之类的选项,但似乎没有帮助

Scenario explanation in detail:场景详解:

I have an excel file which has multiline text which gets imported to the app.我有一个 excel 文件,其中包含导入到应用程序中的多行文本。 The multiline text is preserved in the database and I get the text displayed in multi lines when I log it to the console.多行文本保存在数据库中,当我将它记录到控制台时,我得到多行显示的文本。 (The console does not output new line characters line \n, \r,etc and just white space line breaks like in the original text). (控制台不会像原始文本一样输出换行符 line \n、\r 等,而只输出空格换行符)。 But, when I edit the same data with tinymce editor, the tinymce editor puts all the data in one line.但是,当我使用 tinymce 编辑器编辑相同的数据时,tinymce 编辑器会将所有数据放在一行中。

The original text is not html text and we cannot expect the end user to type HTML tags inside the excel file such as <p>...</p> or <br />原始文本不是 html 文本,我们不能期望最终用户在 excel 文件中键入 HTML 标记,例如<p>...</p> or <br />

Example data in the excel file: excel文件中的示例数据:

This is line one
This is line two
This is line three

Data when it gets displayed in the editor:数据在编辑器中显示时:

This is line one This is line two This is line three

I would like the editor to preserve the line breaks.我希望编辑器保留换行符。 Is that possible?那可能吗? How can it be achieved?如何实现? Your help would be really appreciated.您的帮助将不胜感激。 Thanks in advance.提前致谢。

TinyMCE is an HTML editor so when you pass it a string of plain text it is converting it to HTML. TinyMCE 是一个 HTML 编辑器,因此当您将纯文本字符串传递给它时,它会将其转换为 HTML。 As the newline characters etc are not valid HTML they are simply removed.由于换行符等不是有效的 HTML,它们被简单地删除。

If you have newlines in your content you could convert them to some sort of valid HTML.如果您的内容中有换行符,您可以将它们转换为某种有效的 HTML。

When you extract content from TinyMCE you have an option to get plain text and it will convert paragraphs ( <p> tags) to 2 newlines ( \n\n ) and line breaks ( <br> tags) to one newline ( \n ).当您从 TinyMCE 提取内容时,您可以选择获取纯文本,它将段落( <p>标签)转换为 2 个换行符( \n\n )和换行符( <br>标签)转换为一个换行符( \n ) . You could do the reverse with your text file to create HTML that represents the line breaks appropriately.您可以对文本文件执行相反的操作,以创建适当地表示换行符的 HTML。

As far as I know, there's now way to achieve that with TinyMCE, for the reasons outlined by @micheal-fromin.据我所知,由于@micheal-fromin 概述的原因,现在有办法使用 TinyMCE 实现这一目标。

The best you can do is search for new lines ( \n and/or \r ) in the source text and replace them with HTML line breaks ( </br> ).您可以做的最好的事情是在源文本中搜索新行( \n和/或\r )并将它们替换为 HTML 换行符( </br> )。

PHP solution PHP解决方案

PHP has a very handy function that does exactly that: nl2br() . PHP 有一个非常方便的函数可以做到这一点: nl2br()

The problem with nl2br is that it will also replace newlines within HTML tags. nl2br的问题在于它还会替换 HTML 标记中的换行符。 If that's an issue for you, I recommend replacing blocks of text with paragraph tags ( <p> ) using either autop() (pure PHP) or wpautop() (in WordPress).如果这对您来说是个问题,我建议使用autop() (纯 PHP)或wpautop() (在 WordPress 中)用段落标签 ( <p> ) 替换文本块。

Javascript solution Javascript 解决方案

In Javascript, nl2br translates to:在 Javascript 中, nl2br转换为:

function nl2br (str, is_xhtml) {
  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display

  return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

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

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