简体   繁体   English

如何在中型编辑器中将类添加到当前活动段落?

[英]How can I add a class to current active paragraph in Medium Editor?

I'm currently using the Medium Editor JS Clone on a project where I can edit articles. 我目前在可以编辑文章的项目上使用中型编辑器JS克隆

The editor is working great, I can format elements and the code it's clean but I'm trying now to find a way for add photos while editing. 该编辑器工作得很好,我可以格式化元素和干净的代码,但是我现在正在尝试寻找一种在编辑时添加照片的方法。

A great solution would be adding a side button to the active paragraph in absolute mode, but I haven't found any way to add a class to the current active paragraph. 一个很好的解决方案是在绝对模式下向活动段落添加一个侧面按钮,但是我还没有找到将类添加到当前活动段落的任何方法。

This is what it generates: 这是它生成的:

<div class="editable" contenteditable="true" spellcheck="true" data-medium-editor-element="true" role="textbox" aria-multiline="true" data-medium-editor-editor-index="1" medium-editor-index="574f8260-cdfd-bf53-e5e4-3fff9da2aa19" data-placeholder="Scrivi il testo..." data-medium-focused="true">
    <p>Hello this is an editor test.</p>
    <p>This is an another paragraph.</p>
</div>

Here's what I want to have: 这就是我想要的:

图像问题描述

So, knowing which p inside the editor's div is the current active, will allow me to set the top position of the side button and on click append the just uploaded photo to the paragraph. 因此,知道编辑器div中的哪个p是当前激活状态,将使我能够设置侧面按钮的顶部位置,然后单击将刚刚上传的照片追加到段落中。

您可以使用getSelectedParentElement函数找到当前处于活动状态的元素(选择所在的位置)

editor.getSelectedParentElement()

I believe for most cases just using getSelectedParentElement() should work fine, but there can be cases where calling editor.getSelectedParentElement() might not give you the top-level block container element you're looking for. 我相信在大多数情况下,仅使用getSelectedParentElement()正常工作,但在某些情况下,调用editor.getSelectedParentElement()可能无法为您提供所需的顶级块容器元素。 There can be some types of formatting where elements can be nested inside each other (ie a <pre> within a <p> or a <p> within a <blockquote> ) 可能存在某些类型的格式化,其中元素可以彼此嵌套(即<p><pre> <p><blockquote><p> <blockquote>

There is another getTopBlockContainer(element) helper method exposed via MediumEditor.util.getTopBlockContainer(element) which will take in an element and return the top level block element container which is a direct child of the root <div> of the editor. 通过MediumEditor.util.getTopBlockContainer(element)公开了另一个getTopBlockContainer(element)帮助器方法,该方法将接受一个元素并返回顶级块元素容器,该容器是编辑器根<div>的直接子级。

So, if you're looking to always get the block element that is a direct child of the root editor <div> , you can use a combination of getSelectedParentElement() and getTopBlockContainer() like this: 因此,如果您希望始终获取作为根编辑器<div>的直接子元素的block元素,则可以使用getSelectedParentElement()getTopBlockContainer()的组合,如下所示:

var topBlock = MediumEditor.util.getTopBlockContainer(editor.getSelectedParentElement());

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

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