如何在SLATEJS富文本编辑器中传递事件

huangapple go评论98阅读模式
英文:

How to pass events in the SLATEJS rich text editor

问题

I'm trying coding follow this doc:

https://docs.slatejs.org/walkthroughs/05-executing-commands

I just wanted to make it elegant, so I tried to separate the Toolbar Button into other files. But how do I pass the event onKeydown there?

I tried the following code, but I got the result that 'editor is undefined'

Did I overlook something?

Here is a simple DEMO:

exprot function App(){
 //...others

 const [editor] = useState(() => withReact(createEditor()));
 return (
   <Slate {...props} >
     <MyToolbar {...editor} />
     //...others
   </Slate>
 );
}
function MyToolbar(editor: ReactEditor){
   return (
      <div>
        //Icon button span
        <span {...props} onMouseDown={event => {
                            event.preventDefault();
                            CustomEditor.toggleBold(editor);
                    }}></span>
      </div>
   );
}

(Note: The code appears to have some HTML encoding issues with '<' and '>' characters, which need to be corrected to make it work.)

英文:

I'm trying coding follow this doc:

https://docs.slatejs.org/walkthroughs/05-executing-commands

I just wanted to make it elegant, so I tried to separate the Toolbar Button into other files. But how do I pass the event onKeydown there?

I tried the following code, but I got the result that 'editor is undefined'

Did I overlook something?

Here is a simple DEMO:

exprot function App(){
 //...others

 const [editor] = useState(() =&gt; withReact(createEditor()));
 return (
   &lt;Slate {...props} &gt;
     &lt;MyToolbar {...editor} /&gt;
     //...others
   &lt;/Slate&gt;
 );
}
function MyToolbar(editor: ReactEditor){
   return (
      &lt;div&gt;
        //Icon button span
        &lt;span {...props} onMouseDown={event =&gt; {
                            event.preventDefault();
                            CustomEditor.toggleBold(editor);
                    }}&gt;&lt;/span&gt;
      &lt;/div&gt;
   );
}

答案1

得分: 1

不要将editor作为一个属性传递,使用钩子useSlate()来获取它:

import { useSlate } from "slate-react";

function MyToolbar() {
  const editor = useSlate();

  return (
    <div>
      <span
        onMouseDown={event => {
          event.preventDefault();
          CustomEditor.toggleBold(editor);
        }}
      ></span>
    </div>
  );
}
英文:

Don't pass editor as a prop, use the hook useSlate() to get it:

import { useSlate } from &quot;slate-react&quot;;

function MyToolbar() {
  const editor = useSlate();

  return (
    &lt;div&gt;
      &lt;span
        onMouseDown={event =&gt; {
          event.preventDefault();
          CustomEditor.toggleBold(editor);
        }}
      &gt;&lt;/span&gt;
    &lt;/div&gt;
  );
}

huangapple
  • 本文由 发表于 2023年1月6日 11:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定