gpt4 book ai didi

javascript - 如何从原始 DraftJS 数据中获取内容长度?

转载 作者:行者123 更新时间:2023-12-03 01:56:08 26 4
gpt4 key购买 nike

运行完 convertToRaw(editorState.getCurrentContent()) 后,我将 DraftJS 编辑器的内容作为 JSON 字符串存储在数据库中。 。

数据库中存储内容的示例:

{"blocks": [{"key": "1m9md", "data": {}, "text": "", "type": "unstyled", "depth": 0, "entityRanges": [], "inlineStyleRanges": []}, {"key": "c8gek", "data": {}, "text": "Lorem ipsum dolor sit amet, eu habitasse magna aliquam non et faucibus, aliquet et ante tempus vestibulum pellentesque, ligula curabitur. Eum torquent dapibus est, ac consectetuer integer. Luctus pellentesque ut pellentesque. Libero id lorem bibendum, sed elementum, adipiscing mollis, aenean pellentesque ligula nonummy pellentesque ornare urna, sit a sagittis wisi rhoncus tellus congue. Risus tristique blandit in nascetur, mauris quisque sed erat error integer accumsan, ante facilisi. Libero proin nunc vestibulum tempus, consectetuer aliquam sed velit morbi quis, molestie accumsan in, lacus eu, aliquam odio porta tellus ac. Praesent aliquet.", "type": "unstyled", "depth": 0, "entityRanges": [], "inlineStyleRanges": []}, {"key": "7mv98", "data": {}, "text": "", "type": "unstyled", "depth": 0, "entityRanges": [], "inlineStyleRanges": []}, {"key": "63ck4", "data": {}, "text": "", "type": "unstyled", "depth": 0, "entityRanges": [], "inlineStyleRanges": []}], "entityMap": {}}

现在,当以 HTML 形式向用户显示此内容时,我想检查他们是否在每个部分中实际在 DraftJS 编辑器中输入了任何文本。即使他们将其留空,数据库也会存储:

{"blocks": [{"key": "6n2bu", "data": {}, "text": "", "type": "unstyled", "depth": 0, "entityRanges": [], "inlineStyleRanges": []}], "entityMap": {}}

似乎没有明确的方法来以编程方式检查内容的长度(或者它是否存在)。

我正在寻找一种方法来检查内容是否存在,然后再将其转换为 HTML 并使用 React 进行渲染。

最佳答案

您可以使用ContentState.hasText()

为了查看是否为空。

否则,您可以随时调用ContentState.getPlainText()从而得到全文,然后就可以得到长度了。

例如myContent.getPlainText(' ').length

关于javascript - 如何从原始 DraftJS 数据中获取内容长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com