util.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { ElementTypes } from 'components/RichText/Element'
  21. import { RichTextNode } from 'components/RichText'
  22. import { EDITOR_DEFAULT_FONT_WEIGHT_BOLD } from './contants'
  23. import { ILayerParams } from '../../types'
  24. import {
  25. IEditorSelection,
  26. IElementFontStyles,
  27. IElementTextStyles,
  28. IEditorBoxStyle
  29. } from './types'
  30. export const buildLabelBoxStyles = (params: ILayerParams) => {
  31. const {
  32. fontWeight,
  33. paddingTop,
  34. paddingBottom,
  35. paddingLeft,
  36. paddingRight,
  37. textIndent,
  38. lineHeight
  39. } = params
  40. const boxStyles: Partial<IEditorBoxStyle> = {
  41. paddingTop: `${paddingTop}px`,
  42. paddingRight: `${paddingRight}px`,
  43. paddingBottom: `${paddingBottom}px`,
  44. paddingLeft: `${paddingLeft}px`
  45. }
  46. if (fontWeight) {
  47. boxStyles.fontWeight = fontWeight
  48. }
  49. if (textIndent) {
  50. boxStyles.textIndent = `${textIndent}px`
  51. }
  52. if (lineHeight) {
  53. boxStyles.lineHeight = `${lineHeight}px`
  54. }
  55. return {
  56. boxStyles
  57. }
  58. }
  59. export const migrationLabelRichTextStyles = (params: ILayerParams) => {
  60. const {
  61. fontWeight,
  62. fontFamily,
  63. fontColor,
  64. fontSize,
  65. textAlign,
  66. textStyle
  67. } = params
  68. const fontStyles: Partial<IElementFontStyles> = {
  69. fontSize: Number(`${fontSize}`),
  70. color: `rgba(${fontColor.join()})`
  71. }
  72. const textStyles: Partial<IElementTextStyles> = {
  73. textAlign: `${textAlign}`
  74. }
  75. fontStyles.bold = fontWeight === EDITOR_DEFAULT_FONT_WEIGHT_BOLD
  76. if (textStyle.includes('italic')) {
  77. fontStyles.italic = textStyle.indexOf('italic') > -1
  78. }
  79. if (textStyle.includes('underline')) {
  80. fontStyles.underline = textStyle.indexOf('underline') > -1
  81. }
  82. if (fontFamily) {
  83. fontStyles.fontFamily = `${fontFamily}`
  84. }
  85. return {
  86. fontStyles,
  87. textStyles
  88. }
  89. }
  90. export const migrationLabelRichTextContent = (
  91. text?: string,
  92. fontStyles?: Partial<IElementFontStyles>,
  93. textStyles?: Partial<IElementTextStyles>
  94. ) => {
  95. return {
  96. content: [
  97. {
  98. type: ElementTypes.Paragraph,
  99. children: [{ text: text || '', ...fontStyles }],
  100. ...textStyles
  101. }
  102. ]
  103. }
  104. }
  105. export const onLabelEditorStylesChange = (
  106. propPath: string[],
  107. value: string | RichTextNode[]
  108. ) => {
  109. const unitRange = {}
  110. propPath.reduce((subObj, propName, idx) => {
  111. if (idx === propPath.length - 1) {
  112. return (subObj[propName] = value)
  113. }
  114. return (subObj[propName] = {})
  115. }, unitRange)
  116. return unitRange
  117. }
  118. export const onLabelEditorSelectedRange = () => {
  119. const { anchorNode } = window.getSelection() as IEditorSelection
  120. if (!anchorNode || !anchorNode.parentNode) {
  121. return false
  122. }
  123. if (anchorNode.parentNode as Node) {
  124. return anchorNode.parentElement.hasAttribute('data-slate-string')
  125. }
  126. }