Heading.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useContext } from 'react'
  21. import { Select } from 'antd'
  22. const { Option } = Select
  23. import { OptionProps } from 'antd/lib/select'
  24. import { EditorContext } from '../context'
  25. import { HeadingElementTypes } from '../Element/constants'
  26. const Heading: React.FC = () => {
  27. const { isElementActive, toggleElement } = useContext(EditorContext)
  28. const activeValue =
  29. HeadingSelectOptions.find(({ value }) => isElementActive(value))?.value ||
  30. HeadingElementTypes.HeadingNone
  31. return (
  32. <Select
  33. value={activeValue}
  34. dropdownMatchSelectWidth={false}
  35. className="richtext-toolbar-select"
  36. style={{ width: 90 }}
  37. size="small"
  38. onChange={toggleElement}
  39. >
  40. {HeadingSelectOptions.map(({ value, label }) => (
  41. <Option key={value} value={value}>
  42. {label}
  43. </Option>
  44. ))}
  45. </Select>
  46. )
  47. }
  48. export default Heading
  49. interface HeadingOptionProps extends OptionProps {
  50. value: HeadingElementTypes
  51. }
  52. const HeadingSelectOptions: HeadingOptionProps[] = [
  53. {
  54. value: HeadingElementTypes.HeadingOne,
  55. label: <h1>一级标题</h1>
  56. },
  57. {
  58. value: HeadingElementTypes.HeadingTwo,
  59. label: <h2>二级标题</h2>
  60. },
  61. {
  62. value: HeadingElementTypes.HeadingThree,
  63. label: <h3>三级标题</h3>
  64. },
  65. {
  66. value: HeadingElementTypes.HeadingFour,
  67. label: <h4>四级标题</h4>
  68. },
  69. {
  70. value: HeadingElementTypes.HeadingFive,
  71. label: <h5>五级标题</h5>
  72. },
  73. {
  74. value: HeadingElementTypes.HeadingSix,
  75. label: <h6>六级标题</h6>
  76. },
  77. {
  78. value: HeadingElementTypes.HeadingNone,
  79. label: <span>无标题</span>
  80. }
  81. ]