Font.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { TextProperties } from '../Element/constants'
  25. import { EditorContext } from '../context'
  26. const Font: React.FC = () => {
  27. const { isTextPropertyActive, toggleTextProperty } = useContext(EditorContext)
  28. const fontFamily =
  29. FontSelectOptions.find(({ value }) =>
  30. isTextPropertyActive(TextProperties.FontFamily, value)
  31. )?.value || ''
  32. const fontSize = +isTextPropertyActive(TextProperties.FontSize) || ''
  33. const fontFamilyChange = (value: string) =>
  34. toggleTextProperty(TextProperties.FontFamily, value)
  35. const fontSizeChange = (value: number) =>
  36. toggleTextProperty(TextProperties.FontSize, value)
  37. return (
  38. <>
  39. <Select
  40. value={fontFamily}
  41. dropdownMatchSelectWidth={false}
  42. className="richtext-toolbar-select"
  43. style={{ width: 100 }}
  44. size="small"
  45. onChange={fontFamilyChange}
  46. >
  47. {FontSelectOptions.map(({ value, label }) => (
  48. <Option key={value} value={value}>
  49. <span style={{ fontFamily: value }}>{label}</span>
  50. </Option>
  51. ))}
  52. </Select>
  53. <Select
  54. value={fontSize}
  55. dropdownMatchSelectWidth={false}
  56. className="richtext-toolbar-select"
  57. size="small"
  58. onChange={fontSizeChange}
  59. >
  60. <Option value="">默认</Option>
  61. {FontSizeSelectOptions.map(({ value, label }) => (
  62. <Option key={value} value={value}>
  63. <span
  64. style={{
  65. fontSize: value,
  66. lineHeight: `${value}px`
  67. }}
  68. >
  69. {label}
  70. </span>
  71. </Option>
  72. ))}
  73. </Select>
  74. </>
  75. )
  76. }
  77. export default Font
  78. interface FontOptionProps extends OptionProps {
  79. value: string
  80. label: string
  81. }
  82. const FontSelectOptions: FontOptionProps[] = [
  83. { value: '', label: '默认字体' },
  84. { value: "Microsoft Yahei", label: "微软雅黑"},
  85. { value: "SimSun", label: "宋体"},
  86. { value: "Heiti", label: "黑体"},
  87. { value: "STXihei", label: "华文细黑"},
  88. { value: "Verdana", label: "Verdana"},
  89. { value: "Arial", label: "Arial"},
  90. { value: "Times New Roman", label: "Times New Roman"},
  91. { value: "Times", label: "Times"},
  92. { value: "MS Sans Serif", label: "MS Sans Serif"},
  93. { value: 'Sans Serif', label: 'Sans Serif' },
  94. { value: 'Serif', label: 'Serif' },
  95. { value: 'Monospace', label: 'Monospace' },
  96. ]
  97. const FontSizeSelectOptions: OptionProps[] = [
  98. 10,
  99. 12,
  100. 13,
  101. 14,
  102. 15,
  103. 16,
  104. 18,
  105. 20,
  106. 24,
  107. 28,
  108. 32,
  109. 36,
  110. 40,
  111. 48,
  112. 56,
  113. 64,
  114. 72,
  115. 96,
  116. 128
  117. ].map((size) => ({
  118. value: size,
  119. label: `${size}px`
  120. }))