index.tsx 1.3 KB

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