首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何改变SlateJS节点元素onChange的属性?

如何改变SlateJS节点元素onChange的属性?
EN

Stack Overflow用户
提问于 2022-02-17 05:21:09
回答 1查看 1.5K关注 0票数 2

如何在SlateJS OnChange方法中更改节点元素属性?

我有一个像以前一样的初始元素,注意到'id‘属性了吗?在OnChange中,我希望动态设置id属性。请参阅下面的实现,或多或少只是SlateJs的基本反应设置

代码语言:javascript
复制
const initialValue: Descendant[] = [
    {
        type: 'paragraph',
        id: '',
        children: [
            {
                text:
                    'This is editable plain text, just like a <textarea>!',
            },
        ],
    },
]

板岩反应组分

代码语言:javascript
复制
        <Slate
            editor={editor}
            value={textContent}
            onChange={currTextContent => {
                // Logic for newlines
                if (currTextContent.length > textContent.length) {
                    // Same example from slatejs on how to save content
                    const isAstChange = editor.operations.some(
                        op => 'set_selection' !== op.type,
                    )

                    if (isAstChange) {
                        // Set id property of new line
                        currTextContent[0].id = 'test'
                    }
                }

                settextContent(currTextContent)
            }}>
            <Editable
                placeholder="Enter some rich text…"
                spellCheck
                autoFocus
            />
        </Slate>

但是,它说.id属性是只读的。如果我尝试设置整个对象,也会发生同样的情况。这是只读的。通过currTextContent.newID添加新属性也会导致错误,对象是不可扩展的。

代码语言:javascript
复制
                    currTextContent[0] = {
                        type: 'paragraph',
                        id: '',
                        children: [
                            {
                                text:
                                    'This is editable plain text, just like a <textarea>!',
                            },
                        ],
                    }

如何在SlateJS方法中更改(或添加) onChange节点的元素属性?在Editor类中有这样做的函数吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-19 00:41:16

因此,您实际上不能更改onChange中的节点,因为它们是只读的。相反,您需要使用Slate转换,以插入或添加如下所示的新属性。

代码语言:javascript
复制
    Transforms.setNodes(
        editor,
        {
            id: '123',
        },
        { at: [0] },
    )

这将将id = 123的属性插入节点。

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

https://stackoverflow.com/questions/71152875

复制
相关文章

相似问题

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