简体   繁体   English

如何根据draft js中的运行时数据动态应用内联styles

[英]How to dynamically apply inline styles based on runtime data in draft js

In my application, I have a backend server where I send all the text entered in the draft js editor and the server returns tokens for the text.在我的应用程序中,我有一个后端服务器,我发送在草稿 js 编辑器中输入的所有文本,服务器返回文本的标记。 Based on these tokens I want to style the text.基于这些标记,我想为文本设置样式。 How do I do that in draft js?我如何在草稿 js 中做到这一点?

Approach 1: I tried using decorators where I have a map that I fill up based on the token types and text and the decorator function uses this map to apply styles to all the text. Approach 1: I tried using decorators where I have a map that I fill up based on the token types and text and the decorator function uses this map to apply styles to all the text. However, the decorator is always one step behind as it is working with previous onChange's data.然而,装饰器总是落后一步,因为它正在处理以前的 onChange 数据。 There is no way to manually trigger a decorate().无法手动触发 decorate()。

Approach 2: If I want to use Richutils, it automatically uses the current selection to toggle inline styles.方法2:如果我想使用Richutils,它会自动使用当前选择来切换内联styles。 In my case there is no selection, so that won't work.在我的情况下,没有选择,所以这是行不通的。

I am just looking for a simple function that can apply inline styles based on character index/offset.我只是在寻找一个简单的 function 可以基于字符索引/偏移量应用内联 styles。 something like就像是

editorState = Richutils.applyInlineStyle(editorState, indexstart, length (or indexend), style);

Is there any other way I can achieve this.有没有其他方法可以实现这一点。 It seems a perfectly doable and reasonable thing to have supported, but artificially restricted.支持,但人为限制,似乎是一件完全可行和合理的事情。

https://draftjs.org/docs/api-reference-modifier/#applyinlinestyle is excatly the function you need. https://draftjs.org/docs/api-reference-modifier/#applyinlinestyle正是您需要的 function。

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

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