Monorepo containing core modules of Zeichen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.3 KiB

  1. import * as React from 'react'
  2. import * as PropTypes from 'prop-types'
  3. import {
  4. Container,
  5. MarkupButton,
  6. LinkButton,
  7. SectionSelect,
  8. AttributeSelect,
  9. SectionButton,
  10. Editor as MobileDocEditor,
  11. } from 'react-mobiledoc-editor'
  12. import { Bold, Italic, Code, List, Link, MessageCircle, } from 'react-feather'
  13. import styled from 'styled-components'
  14. const ToolbarWrapper = styled('div')({
  15. display: 'flex',
  16. gap: '1rem',
  17. })
  18. const ToolbarItem = styled('div')({
  19. display: 'block',
  20. })
  21. const StyledMarkupButton = styled(MarkupButton)({
  22. border: 0,
  23. background: 'transparent',
  24. width: '2rem',
  25. height: '2rem',
  26. font: 'inherit',
  27. padding: 0,
  28. outline: 0,
  29. })
  30. const StyledLinkButton = styled(LinkButton)({
  31. border: 0,
  32. background: 'transparent',
  33. width: '2rem',
  34. height: '2rem',
  35. font: 'inherit',
  36. padding: 0,
  37. outline: 0,
  38. })
  39. const StyledSectionButton = styled(SectionButton)({
  40. border: 0,
  41. background: 'transparent',
  42. width: '2rem',
  43. height: '2rem',
  44. font: 'inherit',
  45. fontFamily: 'monospace',
  46. fontSize: '1.75rem',
  47. lineHeight: 0,
  48. padding: 0,
  49. outline: 0,
  50. })
  51. const propTypes = {
  52. onChange: PropTypes.func,
  53. cards: PropTypes.array,
  54. atoms: PropTypes.array,
  55. content: PropTypes.object,
  56. placeholder: PropTypes.string,
  57. autoFocus: PropTypes.bool,
  58. }
  59. type Props = PropTypes.InferProps<typeof propTypes>
  60. const Editor: React.FC<Props> = ({
  61. onChange,
  62. cards = [],
  63. atoms = [],
  64. content,
  65. placeholder,
  66. autoFocus,
  67. }) => {
  68. const [isClient, setIsClient, ] = React.useState(false)
  69. const editorRef = React.useRef(null)
  70. const handleInitialize = e => {
  71. editorRef.current = e
  72. }
  73. React.useEffect(() => {
  74. setIsClient(true)
  75. }, [])
  76. if (isClient) {
  77. return (
  78. <Container
  79. autofocus={autoFocus}
  80. onChange={onChange}
  81. cards={[
  82. ...cards,
  83. ]}
  84. atoms={[
  85. ...atoms,
  86. ]}
  87. mobiledoc={content}
  88. placeholder={placeholder}
  89. didCreateEditor={handleInitialize}
  90. >
  91. <ToolbarWrapper>
  92. <ToolbarItem>
  93. <StyledMarkupButton tag="strong">
  94. <Bold />
  95. </StyledMarkupButton>
  96. </ToolbarItem>
  97. <ToolbarItem>
  98. <StyledMarkupButton tag="em">
  99. <Italic />
  100. </StyledMarkupButton>
  101. </ToolbarItem>
  102. <ToolbarItem>
  103. <StyledMarkupButton tag="code">
  104. <Code />
  105. </StyledMarkupButton>
  106. </ToolbarItem>
  107. <ToolbarItem>
  108. <StyledLinkButton>
  109. <Link />
  110. </StyledLinkButton>
  111. </ToolbarItem>
  112. {/*<ToolbarItem><SectionSelect tags={["h1", "h2", "h3"]} /></ToolbarItem>*/}
  113. {/*<ToolbarItem><AttributeSelect attribute="text-align" values={["left", "center", "right"]} /></ToolbarItem>*/}
  114. <ToolbarItem>
  115. <StyledSectionButton tag="blockquote">
  116. <MessageCircle />
  117. </StyledSectionButton>
  118. </ToolbarItem>
  119. <ToolbarItem>
  120. <StyledSectionButton tag="ul">
  121. <List />
  122. </StyledSectionButton>
  123. </ToolbarItem>
  124. <ToolbarItem><StyledSectionButton tag="ol">1.</StyledSectionButton></ToolbarItem>
  125. </ToolbarWrapper>
  126. <MobileDocEditor />
  127. </Container>
  128. )
  129. }
  130. return null
  131. }
  132. Editor.propTypes = propTypes
  133. export default Editor