All files / modules/10-common/components/ShellScriptMonaco ShellScriptMonaco.tsx

62.5% Statements 25/40
38.46% Branches 10/26
33.33% Functions 4/12
64.1% Lines 25/39

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              101x   101x 101x 101x 101x   101x 101x 101x 101x         101x                                 101x   101x 68x 68x 68x 68x   68x 19x   19x                                                                 19x 16x         68x                                               2x                         68x                                     101x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import type { MonacoEditorProps } from 'react-monaco-editor'
import { Dialog, Classes } from '@blueprintjs/core'
import { FormikProps, connect } from 'formik'
import { get } from 'lodash-es'
import { Button } from '@wings-software/uicore'
import type { languages, IDisposable } from 'monaco-editor/esm/vs/editor/editor.api'
import { useStrings } from 'framework/strings'
import MonacoEditor from '@common/components/MonacoEditor/MonacoEditor'
import { useDeepCompareEffect } from '@common/hooks'
import css from './ShellScriptMonaco.module.scss'
 
export type ScriptType = 'Bash' | 'PowerShell'
type Languages = typeof languages
 
const langMap: Record<ScriptType, string> = {
  Bash: 'shell',
  PowerShell: 'powershell'
}
 
export interface ShellScriptMonacoProps {
  title?: string
  scriptType: ScriptType
  name: string
  disabled?: boolean
  expressions?: string[]
}
 
export interface ConnectedShellScriptMonacoProps extends ShellScriptMonacoProps {
  formik: FormikProps<unknown>
}
 
const VAR_REGEX = /.*<\+.*?/
 
export function ShellScriptMonaco(props: ConnectedShellScriptMonacoProps): React.ReactElement {
  const { scriptType, formik, name, disabled, expressions, title } = props
  const [isFullScreen, setFullScreen] = React.useState(false)
  const { getString } = useStrings()
  const value = get(formik.values, name) || ''
 
  useDeepCompareEffect(() => {
    const disposables: IDisposable[] = []
 
    Iif (Array.isArray(expressions) && expressions.length > 0) {
      const suggestions: Array<Partial<languages.CompletionItem>> = expressions
        .filter(label => label)
        .map(label => ({
          label,
          insertText: label + '>',
          kind: 13
        }))
 
      Object.values(langMap).forEach(lang => {
        const disposable = (monaco?.languages as Languages)?.registerCompletionItemProvider(lang, {
          triggerCharacters: ['+', '.'],
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          provideCompletionItems(model, position): any {
            const prevText = model.getValueInRange({
              startLineNumber: position.lineNumber,
              startColumn: 0,
              endLineNumber: position.lineNumber,
              endColumn: position.column
            })
 
            if (VAR_REGEX.test(prevText)) {
              return { suggestions }
            }
 
            return { suggestions: [] }
          }
        })
 
        disposables.push(disposable)
      })
    }
 
    return () => {
      disposables.forEach(disposable => disposable.dispose())
    }
  }, [expressions])
 
  const editor = (
    <div
      className={css.monacoWrapper}
      onKeyDown={event => {
        if (event.key === 'Enter') {
          event.stopPropagation()
        }
      }}
    >
      <MonacoEditor
        height={isFullScreen ? '70vh' : 300}
        value={value}
        name={name}
        language={langMap[scriptType] as string}
        options={
          {
            fontFamily: "'Roboto Mono', monospace",
            fontSize: 13,
            minimap: {
              enabled: false
            },
            readOnly: disabled,
            scrollBeyondLastLine: false
          } as MonacoEditorProps['options']
        }
        onChange={txt => formik.setFieldValue(name, txt)}
      />
      {isFullScreen ? null : (
        <Button
          className={css.expandBtn}
          icon="fullscreen"
          small
          onClick={() => setFullScreen(true)}
          iconProps={{ size: 10 }}
        />
      )}
    </div>
  )
  return (
    <React.Fragment>
      {isFullScreen ? <div className={css.monacoWrapper} /> : editor}
      <Dialog
        lazy
        enforceFocus={false}
        isOpen={isFullScreen}
        isCloseButtonShown
        canOutsideClickClose={false}
        onClose={() => setFullScreen(false)}
        title={title ? title : `${getString('script')} (${scriptType})`}
        className={css.monacoDialog}
      >
        <div className={Classes.DIALOG_BODY}>{editor}</div>
      </Dialog>
    </React.Fragment>
  )
}
 
export const ShellScriptMonacoField = connect<ShellScriptMonacoProps>(ShellScriptMonaco)