index.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Slate, Editable } from 'slate-react';
  2. import Leaf from './Leaf';
  3. import { useTextBlock } from './TextBlock.hooks';
  4. import BlockHorizontalToolbar from '../BlockHorizontalToolbar';
  5. import React from 'react';
  6. import { BlockType, NestedBlock } from '$app/interfaces/document';
  7. import NodeChildren from '$app/components/document/Node/NodeChildren';
  8. function TextBlock({
  9. node,
  10. childIds,
  11. placeholder,
  12. ...props
  13. }: {
  14. node: NestedBlock;
  15. childIds?: string[];
  16. placeholder?: string;
  17. } & React.HTMLAttributes<HTMLDivElement>) {
  18. const { editor, value, onChange, onKeyDown, onDOMBeforeInput } = useTextBlock(node.id);
  19. const className = props.className !== undefined ? ` ${props.className}` : '';
  20. return (
  21. <>
  22. <div {...props} className={`px-1 py-[2px]${className}`}>
  23. <Slate editor={editor} onChange={onChange} value={value}>
  24. <BlockHorizontalToolbar id={node.id} />
  25. <Editable
  26. onKeyDown={onKeyDown}
  27. onDOMBeforeInput={onDOMBeforeInput}
  28. renderLeaf={(leafProps) => <Leaf {...leafProps} />}
  29. placeholder={placeholder || 'Please enter some text...'}
  30. />
  31. </Slate>
  32. </div>
  33. <NodeChildren className='pl-[1.5em]' childIds={childIds} />
  34. </>
  35. );
  36. }
  37. export default React.memo(TextBlock);