# All files / src/_utils TinyMCE.ts

90.47% Statements 19/21
71.42% Branches 5/7
85.71% Functions 6/7
90.47% Lines 19/21

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
    154
    155
    156
    157

    9x
    9x
    9x

    9x

    9x

    9x

    9x

    9x

    9x
    22x

    11x

    9x
    11x
    11x

    58x

    9x

    28x

    14x

    70x

     import { Editor } from '@tinymce/tinymce-react'
    import { Locale } from '../_hooks/I18n'

    export const scriptId = 'tiny-mce-core-source'
    export const tinyMCESource = 'https://s3.amazonaws.com/procore-core/tinymce'
    export const tinyMCEVersion = '5.8.1'

    type TextEditorLocales = { [key in Locale]: string }

    // TinyMCE Supported Languages: https://www.tiny.cloud/docs/configure/localization/#supportedlanguages
    const textEditorLocales: TextEditorLocales = {
      en: 'en',
      'en-US': 'en',
      'en-CA': 'en',
      'fr-CA': 'fr_FR',
      'fr-FR': 'fr_FR',
      es: 'es',
      'es-ES': 'es',
      'en-AE': 'en',
      'en-AU': 'en',
      'en-GB': 'en',
      'en-SG': 'en',
      ko: 'ko_KR',
      'th-TH': 'th_TH',
      pt: 'pt_BR',
      'is-IS': 'en',
      'de-DE': 'de',
      'zh-SG': 'zh_CN',
      pseudo: 'en',
    }

    export const defaultPlugins = [
      'autolink',
      'autoresize',
      'lists',
      'nonbreaking',
      'paste',
    ]

    export const plugins = [
      'advlist',
      'anchor',
      'autolink',
      'autoresize',
      'autosave',
      'bbcode',
      'charmap',
      'code',
      'codesample',
      'colorpicker',
      'contextmenu',
      'directionality',
      'emoticons',
      'fullpage',
      'fullscreen',
      'help',
      'hr',
      'image',
      'imagetools',
      'importcss',
      'insertdatetime',
      'legacyoutput',
      'link',
      'lists',
      'media',
      'nonbreaking',
      'noneditable',
      'pagebreak',
      'paste',
      'preview',
      'print',
      'save',
      'searchreplace',
      'spellchecker',
      'tabfocus',
      'table',
      'template',
      'textpattern',
      'toc',
      'visualblocks',
      'visualchars',
      'wordcount',
    ]

    /**
     *  Historically, `nonbreaking_force_tab` has been set to `true`
     *  and this takes the `Tab` keypress as input to add to the content.
     *  e.g indent the start of the text block like a paragraph.
     *  This is different from the TinyMCE tool and command 'Indent', which
     *  indents the entire block. e.g. indent this entire text block and the
     *  wrapped lines.
     *  This is the HTML it outputs when `nonbreaking_force_tab: true`
     */
    export const tinyMCETabMarkup = `<span class=\"mce-nbsp-wrap\" contenteditable=\"false\">&nbsp;&nbsp;&nbsp;</span>`

    export const tinyMCEConfig: any & Editor = {
      autoresize_overflow_padding: 12,
      autoresize_bottom_margin: 0,
      browser_spellcheck: true,
      convert_url: false,
      content_css: `${tinyMCESource}/${tinyMCEVersion}/skins/content/default/content.min.css`,
      extended_valid_elements:
        'table[class=mce-table|border:1px double #000|cellpadding: 3px|style=border-collapse: collapse; border-color: #000]',
      min_height: 96,
      invalid_elements: 'form,script',
      menubar: false,
      nonbreaking_force_tab: true,
      paste_preprocess: (_: any, args: any) => {
        args.content = args.content.replace(/<pre>/gi, '<p>')
        args.content = args.content.replace(/<\/pre>/gi, '</p>')
      },
      paste_remove_styles_if_webkit: true,
      paste_retain_style_properties: 'font-size,text-decoration',
      paste_word_valid_elements:
        '@[style],pre,-strong/b,-em/i,-span,-p,-ol,-ul,-li,-table,-tr,-td[colspan|rowspan],-th,-thead,-tfoot,-tbody,-a[href|name],sub,sup,strike,br,u',
      relative_urls: false,
      remove_script_host: false,
      statusbar: false,
      skin_url: `${tinyMCESource}/${tinyMCEVersion}/skins/ui/oxide`,
      toolbar:
        'bold italic underline | alignleft aligncenter alignright | bullist numlist | outdent indent | cut paste pastetext | fontsizeselect | forecolor backcolor | link table image | undo redo',
      toolbar_mode: 'floating',
    }

    export function getValidLookupLocale(locale: Locale): string {
      return textEditorLocales[locale] || textEditorLocales.en
    }

    function usesBuiltInText(locale: string) {
      return locale === textEditorLocales.en
    }

    export function generateLanguageUrl(locale: Locale) {
      const lookupLocale = getValidLookupLocale(locale)
      return usesBuiltInText(lookupLocale)
        ? ''
        : `${tinyMCESource}/${tinyMCEVersion}/langs/${lookupLocale}.js`
    }

    function generatePluginUrl(plugin: string) {
      return `${tinyMCESource}/${tinyMCEVersion}/plugins/${plugin}/plugin.min.js`
    }

    export function generateExternalPlugins(
      plugins: Array<string>,
      basePlugins = defaultPlugins
    ) {
      return Array.from(new Set(plugins.concat(basePlugins))).reduce(
        (acc, plugin) => {
          return plugins.includes(plugin)
            ? Object.assign(acc, { [plugin]: generatePluginUrl(plugin) })
            : acc
        },
        {}
      )
    }