简体   繁体   English

如何在DraftJS的Anchor标签中显示子组件?

[英]How to display children components in an Anchor tag in DraftJS?

Updated post with a working example in CodeSandbox 使用CodeSandbox中的工作示例更新了帖子

We're using DraftJS to insert an <a> around some selected text in an editor. 我们正在使用DraftJS在编辑器中插入一些选定文本的<a>

Original state 原始状态

It presently works by wrapping the anchor tag around the highlighted text. 它目前通过将锚标记包裹在突出显示的文本周围来工作。

Eg, for this text: Foo bar foo . 例如,对于这个文本: Foo bar foo
If the user highlights "bar", our editor will render <a href='http://...'>bar</a> 如果用户突出显示“bar”,我们的编辑器将呈现<a href='http://...'>bar</a>

The current code to alter the content state: 用于更改内容状态的当前代码:

this.applyLink = () => {
  const { editorState } = this.state;
  const selectionState = editorState.getSelection();

  const entity = Entity.create("LINK", "MUTABLE", {
    url: "http://foo.com"
  });

  const update = RichUtils.toggleLink(editorState, selectionState, entity);

  this.onChange(update);
};

The actual Link that is getting rendered: 正在渲染的实际链接:

const Link = props => {
  const { url } = props.contentState.getEntity(props.entityKey).getData();
  return (
    <a href={url} style={styles.link}>
      {props.children}
    </a>
  );
};

The custom decorator which creates the Link: 创建链接的自定义装饰器:

function findLinkEntities(contentBlock: ContentBlock, callback, contentState) {
  contentBlock.findEntityRanges(character => {
    const entityKey = character.getEntity();

    return (
      entityKey !== null &&
      contentState.getEntity(entityKey).getType() === "LINK"
    );
  }, callback);
}

Desired state 期望的状态

Our product owner has asked that the links retain any other elements which may be selected. 我们的产品所有者要求链接保留可能选择的任何其他元素。

Eg, for text with a custom entity: Foo bar <custom-entity .../> foo . 例如,对于具有自定义实体的文本: Foo bar <custom-entity .../> foo

If the user highlights "bar" and the custom entity, we want to render the anchor tag around both of them. 如果用户突出显示“bar” 自定义实体,我们希望围绕它们呈现锚标记。 Ie <a href='http://...'>bar <custom-entity .../> </a> . <a href='http://...'>bar <custom-entity .../> </a>

However, our code is stripping anything that isn't text-- the <custom-entity> disappears. 但是,我们的代码正在剥离任何非文本的内容 - <custom-entity>消失。

Update with CodeSandbox 使用CodeSandbox进行更新

I've created an example which reproduces this in CodeSandbox, available here 我已经创建了一个在CodeSandbox中重现这个的例子, 可以在这里找到

The CodeSandbox page detail the steps to reproduce. CodeSandbox页面详细介绍了重现的步骤。 In short if you add text and the Custom Entity (using the button), then highlight the content like so: 简而言之,如果您添加文本和自定义实体(使用按钮),则突出显示内容,如下所示: 点击“申请链接”之前

Then with that highlight still active, click 'Apply link', the editor renders: 然后,当该突出显示仍处于活动状态时,单击“应用链接”,编辑器将呈现:

自定义实体消失

^ Note the Custom Entity disappeared after the Link was applied. ^注意应用链接后,自定义实体消失了。

Can anyone help figure out how to keep that Custom Entity inside the anchor tag? 任何人都可以帮助弄清楚如何将自定义实体保留在锚标记内?

Draft.js does not support nested entities , but there are some options to nest decorators if you can use them instead. Draft.js不支持嵌套实体 ,但如果您可以使用它们,则有一些选项可以嵌套装饰器

In particular, take a look as this jsfiddle . 特别是,看看这个jsfiddle

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

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