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.

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