首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在draft-js上做多行加亮?

问如何在draft-js上做多行加亮?
EN

Stack Overflow用户
提问于 2016-08-11 18:30:20
回答 2查看 2K关注 0票数 1

现在我做了高亮标记。

高亮内联并不是那么困难。我使用CompositeDecorator重写文本。https://facebook.github.io/draft-js/docs/advanced-topics-decorators.html

但是我不能使用多行语法(例如,codeblock)。默认情况下,换行符将成为下一个块,装饰符将逐个块地处理。

下图是我的实现示例。我不能修饰代码块语法。

​

​

如何在draft-js上创建多行高亮显示?

EN

回答 2

Stack Overflow用户

发布于 2016-08-12 12:44:55

这取决于你想要的“高亮”风格。大多数情况下,如果你只想要一些颜色或字体大小,内联样式就足够了。查看color示例。

而对于块样式,您需要一个自定义的CSS类并将块映射到您的类,请参阅this。

票数 0
EN

Stack Overflow用户

发布于 2016-08-15 21:06:23

我在附近找到了工作。在blockRendererFn上通过dand检测标记码块。

代码语言:javascript
复制
// use blockRedererFn
<Editor
  blockRendererFn={(block) => blockRenderer(block, this.state.editorState)}
  editorState={this.state.editorState}
/>;

// detect code block and add fontFamily: monospace
// Example
//
// ```
// here
// ```
function blockRenderer(contentBlock, editorState) {
  const type = contentBlock.getType();
  if (type === "unstyled") {
    const allText = editorState.getCurrentContent().getPlainText();
    const allCount = countCodeBlockHeader(allText);
    if (allCount > 0 && allCount % 2 === 0) {
      const contentText = contentBlock.getText();
      const [before, after] = allText.split(contentText);
      const beforeCount = countCodeBlockHeader(before);
      const afterCount = countCodeBlockHeader(after);
      if (beforeCount % 2 === 1) {
        if (afterCount % 2 === 1) {
          return {
            component: (_props) => {
              return <code style={{
                fontFamily: "monospace",
                direction: "ltr",
                unicodeBidi: "bidi-override",
              }}>{contentText}</code>;
            },
            editable: true
          };
        }
      }
    }
  }
}

function countCodeBlockHeader(text) {
  return text
  .split("\n")
  .filter(l => l.match(new RegExp("^(```)")))
  .length;
}

但它是脏的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38893815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档