在此链接中,您可以找到 read_more/read_less 功能,但在 slatejs 中,我们无法直接访问文本。那么,如何以 slatejs 的方式在 saltejs 中创建该功能。我试图创建使用 ref
const dots = React.useRef<any>();
const moreText = React.useRef<any>();
const btnText = React.useRef<any>();
然后我以这种方式实现
<span ref={dots}>...</span>
<span ref={moreText}>
<Editable
readOnly={readOnly}
renderElement={renderElement}
renderLeaf={renderLeaf}
placeholder="Enter some rich text…"
spellCheck
autoFocus
onKeyDown={onKeyDown}
/>
</span>
<button
onClick={() => {
if (dots.current.style.display === "none") {
dots.current.style.display = "inline";
btnText.current.innerHTML = "Read less";
moreText.current.style.display = "inline";
} else {
dots.current.style.display = "none";
btnText.current.innerHTML = "Read more";
moreText.current.style.display = "none";
}
}}
ref={btnText}
>
Read more
</button>
但它隐藏了所有文本。但是,我想我需要像这样moreText.current.lenghth(300).appendSpan(span of ref={moreText})