【问题标题】:Add style to text after using paste_as_text in TinyMCE在 TinyMCE 中使用 paste_as_text 后为文本添加样式
【发布时间】:2019-06-07 17:16:25
【问题描述】:

使用 TinyMCE 4,我已经为 paste 插件配置了 paste_as_text 选项,如 here 所述。

它按预期工作,例如:

<p>&nbsp;</p>
<h1 dir="ltr" style="line-height: 1.38; margin-top: 20pt; margin-bottom: 6pt;"><span style="font-size: 20pt; font-family: Arial; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">Requirements</span></h1>
<h3 dir="ltr" style="line-height: 1.38; margin-top: 16pt; margin-bottom: 4pt;"><span style="font-size: 14pt; font-family: Arial; color: #434343; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">Nav Bar/Header</span></h3>
<ul style="margin-top: 0pt; margin-bottom: 0pt;">
<li dir="ltr" style="list-style-type: disc; font-size: 11pt; font-family: Arial; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre;">
<p dir="ltr" style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">Change EAT | DRINK to DISCOVER but keep this as the existing dropdown everywhere</span></p>
</li>
<li dir="ltr" style="list-style-type: disc; font-size: 11pt; font-family: Arial; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre;">
<p dir="ltr" style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">Change WATCH | READ to WATCH but keep this as the existing dropdown everywhere</span></p>
</li>
<li dir="ltr" style="list-style-type: disc; font-size: 11pt; font-family: Arial; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre;">
<p dir="ltr" style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; color: #000000; font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">Keep EXPERIENCE as the existing dropdown everywhere</span></p>
</li>
</ul>

变成:

<p>Requirements<br />Nav Bar/Header<br />Change EAT | DRINK to DISCOVER but keep this as the existing dropdown everywhere<br />Change WATCH | READ to WATCH but keep this as the existing dropdown everywhere<br />Keep EXPERIENCE as the existing dropdown everywhere</p>

但由于所有行都在单个&lt;p&gt; 内,当我尝试设置第一行的样式时,例如将其更改为Heading 1,所有文本都会被格式化。

我怎样才能避免这种情况?

【问题讨论】:

    标签: tinymce tinymce-4 tinymce-plugins


    【解决方案1】:

    基于您选择粘贴为文本这一事实,您无法真正避免这种情况。

    Paste As Text 将删除所有 HMTL 标签并仅返回“文本”,正如功能名称所暗示的那样。

    当您随后对标题 1 进行格式化之类的操作时,TinyMCE 会将光标所在的块转换为 &lt;h1&gt;

    这按设计工作。

    如果您只想将部分内容转换为标题,则需要先将其放在自己的块中。通常,按 Enter 在 TinyMCE 中启动一个新块(段落),因此在您想要的文本之前和之后执行此操作会将其移动到自己的块中。

    【讨论】:

    • 对,但是不能配置成使用倍数&lt;p&gt;而不是&lt;br/&gt;吗?也许在处理设置上有些东西
    • 如果你想要多个 P 标签,我就不会使用 paste_as_text。您引用的原始 HTML 已经存在于多个段落中。如果您继续使用paste_as_text,您可以在将内容插入编辑器之前对其进行处理,并以您想要的任何方式转换内容,包括将每个&lt;br&gt; 之间的文本转换为段落。
    • 我想删除所有样式但保留不同的段落,以便用户可以轻松设置新格式。我想我会研究其他方法。
    • API 允许您在粘贴时处理内容,但在将其插入编辑器之前。 tiny.cloud/docs/plugins/paste/#paste_postprocess 使用该钩子,您可以剥离所有属性但保留语义元素。
    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多