Editor.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, useImperativeHandle, useCallback } from 'react'
  21. import classnames from 'classnames'
  22. import { Editable, withReact, Slate, RenderLeafProps } from 'slate-react'
  23. import { createEditor, Node } from 'slate'
  24. import { withHistory } from 'slate-history'
  25. import { withHtml } from './decorators'
  26. import { parseHtml } from './util'
  27. import Toolbar from './Toolbar'
  28. import { Element, withElements, TextStyles, ElementTypes } from './Element'
  29. import { Leaf } from './Leaf'
  30. import './RichText.less'
  31. interface IEditorProps {
  32. value?: Node[] | string
  33. className?: string
  34. readOnly?: boolean
  35. toolbar?: React.ReactNode
  36. onFormatText?: (text: string) => string
  37. onChange?: (newVal: Node[]) => void
  38. }
  39. const Editor: React.FC<IEditorProps> = (props, ref) => {
  40. const { value, className, readOnly, toolbar, onFormatText, onChange } = props
  41. const initialValue = useMemo(() => {
  42. let parsedValue: Node[]
  43. if (typeof value === 'string') {
  44. try {
  45. parsedValue = JSON.parse(value)
  46. } catch {
  47. parsedValue = parseHtml(value)
  48. }
  49. if (!parsedValue.length) {
  50. parsedValue.push({ text: '' })
  51. }
  52. } else {
  53. parsedValue = value
  54. }
  55. return parsedValue
  56. }, [value])
  57. const editor = useMemo(
  58. () => withElements(withHtml(withReact(withHistory(createEditor())))),
  59. []
  60. )
  61. const renderLeaf = useCallback(
  62. (props: RenderLeafProps) => {
  63. return <Leaf {...props} onFormatText={onFormatText} />
  64. },
  65. [onFormatText]
  66. )
  67. const cls = useMemo(
  68. () =>
  69. classnames({
  70. richtext: true,
  71. 'richtext-editor': !readOnly,
  72. [className]: !!className
  73. }),
  74. []
  75. )
  76. useImperativeHandle(ref, () => ({}))
  77. return (
  78. <div className={cls}>
  79. <Slate
  80. editor={editor}
  81. value={initialValue}
  82. onChange={onChange}
  83. >
  84. {toolbar === false ? null : toolbar || <Toolbar.Toolbar />}
  85. <Editable
  86. renderElement={Element}
  87. renderLeaf={renderLeaf}
  88. readOnly={readOnly}
  89. spellCheck
  90. />
  91. </Slate>
  92. </div>
  93. )
  94. }
  95. export default React.forwardRef(Editor)