index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, { useMemo, useCallback } from 'react'
  21. import { RichTextNode } from 'components/RichText'
  22. import { IChartProps } from '..'
  23. import { decodeMetricName } from '../../util'
  24. import { getFormattedValue } from '../../Config/Format'
  25. import { FieldBoundaries, FieldRegx } from './constants'
  26. import Editor from './Editor'
  27. import Preview from './Preview'
  28. const ChartRichText: React.FC<IChartProps> = (props) => {
  29. const {
  30. editing,
  31. data,
  32. cols,
  33. rows,
  34. metrics,
  35. chartStyles,
  36. onChartStylesChange
  37. } = props
  38. const { content } = chartStyles.richText
  39. const mapFields = useMemo(() => {
  40. let map = cols.concat(rows).reduce((obj, field) => {
  41. obj[field.name] = {
  42. name: field.name,
  43. field: field.field,
  44. format: field.format
  45. }
  46. return obj
  47. }, {})
  48. map = metrics.reduce((obj, field) => {
  49. const name = `${field.agg}(${decodeMetricName(field.name)})`
  50. obj[name] = {
  51. name,
  52. field: field.field,
  53. format: field.format
  54. }
  55. return obj
  56. }, map)
  57. return map
  58. }, [cols, rows, metrics])
  59. const formatText = useCallback(
  60. (text: string) => {
  61. if (!text.length) {
  62. return text
  63. }
  64. const formattedText = text.replace(FieldRegx, (_, p1: string) => {
  65. if (!data.length || data[0][p1] === null) {
  66. return ''
  67. }
  68. let text = data.map((item) => item[p1])
  69. const config = mapFields[p1]
  70. if (config) {
  71. text = text.map((item) => getFormattedValue(item, config.format))
  72. }
  73. return text.join(', ')
  74. })
  75. return formattedText
  76. },
  77. [data, mapFields]
  78. )
  79. const editorChange = useCallback(
  80. (updatedContent: RichTextNode[]) => {
  81. if (updatedContent === content) {
  82. return
  83. }
  84. onChartStylesChange(['richText', 'content'], updatedContent)
  85. },
  86. [content, onChartStylesChange]
  87. )
  88. return (
  89. <>
  90. {!editing ? (
  91. <Preview key="preview" content={content} onFormatText={formatText} />
  92. ) : (
  93. <Editor
  94. key="editor"
  95. content={content}
  96. mapFields={mapFields}
  97. fieldBoundaries={FieldBoundaries}
  98. onChange={editorChange}
  99. onFormatText={formatText}
  100. />
  101. )}
  102. </>
  103. )
  104. }
  105. export default ChartRichText