# All files / src/TextEditor TextEditor.tsx

66.66% Statements 34/51
63.88% Branches 23/36
30% Functions 3/10
65.3% Lines 32/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    1547x
    7x
    7x
    7x
    7x

    7x

    7x

    7x

    7x

    7x
    11x
    11x
    11x
    11x
    11x
    11x
    11x

    11x
    11x

    11x
    11x
    11x

    11x

    11x

    11x
    4x

    11x
    4x

    11x

    11x

    11x

    7x
     import { Editor } from '@tinymce/tinymce-react'
    import React, { useState } from 'react'
    import { useI18nContext } from '../_hooks/I18n'
    import { useZIndexContext } from '../_hooks/ZIndex'
    import {
      defaultPlugins,
      generateExternalPlugins,
      generateLanguageUrl,
      getValidLookupLocale,
      tinyMCEConfig,
      tinyMCESource,
      tinyMCETabMarkup,
      tinyMCEVersion,
    } from '../_utils/TinyMCE'
    import { GlobalStyle } from './TextEditor.styles'
    import { TextEditorProps, TextEditorProviderProps } from './TextEditor.types'

    const errorRed = '#E61920'

    function noop() {}

    const TextEditorContext = React.createContext<
      Omit<TextEditorProviderProps, 'children'>
    >({
      features: {
        tabAsNavigation: undefined,
        stickyToolbar: undefined,
      },
    })

    export function TextEditorProvider({
      children,
      features,
    }: TextEditorProviderProps) {
      return (
        <TextEditorContext.Provider value={{ features }}>
          {children}
        </TextEditorContext.Provider>
      )
    }

    /**
     * editor.addShortcut does not notify on Tab key presses
     */
    function setupShortcuts(editor: any) {
      editor.on('keydown', function checkTabPress(event: React.KeyboardEvent) {
        Iif (event.altKey && event.key === 'Tab') {
          editor.execCommand('mceInsertContent', false, tinyMCETabMarkup)
          event.preventDefault()
          event.stopPropagation()
          return false
        }
      })
    }

    /**

     We use text editors to allow users to enter multiple lines of formatted text.

     If users need to add unformatted text, see Text Area.

     @since 10.19.0

     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-texteditor--demo)

     @see [Design Guidelines](https://design.procore.com/text-editor)

     */
    export function TextEditor({
      error,
      init: initOverrides,
      locale,
      onChange = noop,
      onInit: _onInit,
      plugins = defaultPlugins,
      ...props
    }: TextEditorProps) {
      const { locale: contextLocale } = useI18nContext()
      const { value: zIndex } = useZIndexContext()

      const { features } = React.useContext(TextEditorContext)
      const stickyToolbar = features?.stickyToolbar ? { toolbar_sticky: true } : {}
      const tabControls = features?.tabAsNavigation
        ? {
            external_plugins: generateExternalPlugins(
              plugins,
              defaultPlugins.filter((p) => p !== 'nonbreaking')
            ),
            nonbreaking_force_tab: false,
            setup: setupShortcuts,
          }
        : {}

      const onEditorChange: React.ComponentProps<
        typeof Editor
      >['onEditorChange'] = (_: any, editor: any) => {
        onChange(editor.getContent(), editor.isDirty())
      }

      const ref = React.useRef<HTMLElement>()

      const updateError = () => {
        Iif (ref.current) {
          if (error) {
            ref.current.style.borderColor = errorRed
          } else {
            ref.current.style.borderColor = ''
          }
        }
      }

      React.useEffect(() => {
        updateError()
      }, [error])

      const onInit: React.ComponentProps<typeof Editor>['onInit'] = (
        event,
        editor
      ) => {
        ref.current = editor.editorContainer
        updateError()
        _onInit && _onInit(event, editor)
      }

      const [derivedInitial] = useState(props.initialValue ?? props.value)

      // zIndex + 1 because on first render zIndex of Portal(Modal) is higher
      return (
        <>
          <GlobalStyle $zIndex={zIndex + 1} />
          <Editor
            init={{
              ...tinyMCEConfig,
              external_plugins: generateExternalPlugins(plugins),
              language_url: generateLanguageUrl(locale || contextLocale),
              language: getValidLookupLocale(locale || contextLocale),
              content_style: `p { margin: 0; }`,
              ...stickyToolbar,
              ...tabControls,
              ...initOverrides,
            }}
            tinymceScriptSrc={`${tinyMCESource}/${tinyMCEVersion}/tinymce.min.js`}
            onInit={onInit}
            onEditorChange={onEditorChange}
            {...props}
            value={props.value ?? props.initialValue}
            initialValue={derivedInitial}
          />
        </>
      )
    }

    TextEditor.displayName = 'TextEditor'