diff --git a/app/portainer/components/code-editor/code-editor.controller.js b/app/portainer/components/code-editor/code-editor.controller.js index db21f3007..55a4457ba 100644 --- a/app/portainer/components/code-editor/code-editor.controller.js +++ b/app/portainer/components/code-editor/code-editor.controller.js @@ -1,8 +1,27 @@ /* @ngInject */ export default function CodeEditorController($scope) { + this.type = ''; + this.handleChange = (value) => { $scope.$evalAsync(() => { this.onChange(value); }); }; + + this.$onInit = () => { + this.type = getType({ yaml: this.yml, dockerFile: this.dockerFile, shell: this.shell }); + }; +} + +function getType({ yaml, dockerFile, shell }) { + if (yaml) { + return 'yaml'; + } + if (dockerFile) { + return 'dockerfile'; + } + if (shell) { + return 'shell'; + } + return undefined; } diff --git a/app/portainer/components/code-editor/code-editor.html b/app/portainer/components/code-editor/code-editor.html index 08ea4d41c..f7ad81fc4 100644 --- a/app/portainer/components/code-editor/code-editor.html +++ b/app/portainer/components/code-editor/code-editor.html @@ -1,9 +1,7 @@ void; value: string; @@ -62,6 +61,12 @@ const dockerFileLanguage = new LanguageSupport( ); const shellLanguage = new LanguageSupport(StreamLanguage.define(shell)); +const docTypeExtensionMap: Record = { + yaml: yamlLanguage, + dockerfile: dockerFileLanguage, + shell: shellLanguage, +}; + export function CodeEditor({ id, onChange, @@ -71,26 +76,18 @@ export function CodeEditor({ versions, onVersionChange, height = '500px', - yaml: isYaml, - dockerFile: isDockerFile, - shell: isShell, + type, 'data-cy': dataCy, }: Props) { const [isRollback, setIsRollback] = useState(false); const extensions = useMemo(() => { const extensions = []; - if (isYaml) { - extensions.push(yamlLanguage); - } - if (isDockerFile) { - extensions.push(dockerFileLanguage); - } - if (isShell) { - extensions.push(shellLanguage); + if (type && docTypeExtensionMap[type]) { + extensions.push(docTypeExtensionMap[type]); } return extensions; - }, [isYaml, isDockerFile, isShell]); + }, [type]); function handleVersionChange(version: number) { if (versions && versions.length > 1) { diff --git a/app/react/components/WebEditorForm.tsx b/app/react/components/WebEditorForm.tsx index 36f05969e..d69a32db3 100644 --- a/app/react/components/WebEditorForm.tsx +++ b/app/react/components/WebEditorForm.tsx @@ -1,16 +1,21 @@ -import { PropsWithChildren, useEffect, useMemo } from 'react'; import { useTransitionHook } from '@uirouter/react'; +import { + ComponentProps, + PropsWithChildren, + ReactNode, + useEffect, + useMemo, +} from 'react'; import { BROWSER_OS_PLATFORM } from '@/react/constants'; -import { AutomationTestingProps } from '@/types'; import { CodeEditor } from '@@/CodeEditor'; import { Tooltip } from '@@/Tip/Tooltip'; -import { FormSectionTitle } from './form-components/FormSectionTitle'; import { FormError } from './form-components/FormError'; -import { confirm } from './modals/confirm'; +import { FormSectionTitle } from './form-components/FormSectionTitle'; import { ModalType } from './modals'; +import { confirm } from './modals/confirm'; import { buildConfirmButton } from './modals/utils'; const otherEditorConfig = { @@ -52,39 +57,21 @@ export const editorConfig = { win: otherEditorConfig, } as const; -interface Props extends AutomationTestingProps { - value: string; - onChange: (value: string) => void; +type CodeEditorProps = ComponentProps; - id: string; - placeholder?: string; - yaml?: boolean; - shell?: boolean; - readonly?: boolean; - titleContent?: React.ReactNode; +interface Props extends CodeEditorProps { + titleContent?: ReactNode; hideTitle?: boolean; error?: string; - versions?: number[]; - onVersionChange?: (version: number) => void; - height?: string; } export function WebEditorForm({ id, - onChange, - placeholder, - value, titleContent = '', hideTitle, - readonly, - yaml, - shell, children, error, - versions, - onVersionChange, - height, - 'data-cy': dataCy, + ...props }: PropsWithChildren) { return (
@@ -107,16 +94,8 @@ export function WebEditorForm({
onVersionChange && onVersionChange(v)} - height={height} - data-cy={dataCy} + // eslint-disable-next-line react/jsx-props-no-spreading + {...props} />
diff --git a/app/react/edge/edge-jobs/CreateView/CreateEdgeJobForm.tsx b/app/react/edge/edge-jobs/CreateView/CreateEdgeJobForm.tsx index 549df996f..3fad4a8da 100644 --- a/app/react/edge/edge-jobs/CreateView/CreateEdgeJobForm.tsx +++ b/app/react/edge/edge-jobs/CreateView/CreateEdgeJobForm.tsx @@ -87,7 +87,7 @@ function InnerForm({ isLoading }: { isLoading: boolean }) { onChange={(value) => setFieldValue('fileContent', value)} value={values.fileContent} placeholder="Define or paste the content of your script file here" - shell + type="shell" error={errors.fileContent} /> )} diff --git a/app/react/edge/edge-jobs/ItemView/UpdateEdgeJobForm/UpdateEdgeJobForm.tsx b/app/react/edge/edge-jobs/ItemView/UpdateEdgeJobForm/UpdateEdgeJobForm.tsx index 997363331..e25684b97 100644 --- a/app/react/edge/edge-jobs/ItemView/UpdateEdgeJobForm/UpdateEdgeJobForm.tsx +++ b/app/react/edge/edge-jobs/ItemView/UpdateEdgeJobForm/UpdateEdgeJobForm.tsx @@ -83,7 +83,7 @@ function InnerForm({ isLoading }: { isLoading: boolean }) { onChange={(value) => setFieldValue('fileContent', value)} value={values.fileContent} placeholder="Define or paste the content of your script file here" - shell + type="shell" error={errors.fileContent} /> diff --git a/app/react/edge/edge-stacks/CreateView/DockerContentField.tsx b/app/react/edge/edge-stacks/CreateView/DockerContentField.tsx index 4499e6dfc..812867904 100644 --- a/app/react/edge/edge-stacks/CreateView/DockerContentField.tsx +++ b/app/react/edge/edge-stacks/CreateView/DockerContentField.tsx @@ -16,7 +16,7 @@ export function DockerContentField({ id="stack-creation-editor" value={value} onChange={onChange} - yaml + type="yaml" placeholder="Define or paste the content of your docker compose file here" error={error} readonly={readonly} diff --git a/app/react/edge/edge-stacks/CreateView/KubeManifestForm.tsx b/app/react/edge/edge-stacks/CreateView/KubeManifestForm.tsx index 8a99da7e2..0428f5859 100644 --- a/app/react/edge/edge-stacks/CreateView/KubeManifestForm.tsx +++ b/app/react/edge/edge-stacks/CreateView/KubeManifestForm.tsx @@ -72,7 +72,7 @@ export function KubeManifestForm({ id="stack-creation-editor" value={values.fileContent} onChange={(value) => handleChange({ fileContent: value })} - yaml + type="yaml" placeholder="Define or paste the content of your manifest file here" error={errors?.fileContent} data-cy="stack-creation-editor" diff --git a/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/ComposeForm.tsx b/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/ComposeForm.tsx index e31382132..118de09ac 100644 --- a/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/ComposeForm.tsx +++ b/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/ComposeForm.tsx @@ -60,7 +60,7 @@ export function ComposeForm({ handleContentChange(DeploymentType.Compose, value)} diff --git a/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/KubernetesForm.tsx b/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/KubernetesForm.tsx index 0459a1b89..e84acade5 100644 --- a/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/KubernetesForm.tsx +++ b/app/react/edge/edge-stacks/ItemView/EditEdgeStackForm/KubernetesForm.tsx @@ -35,7 +35,7 @@ export function KubernetesForm({ diff --git a/app/react/kubernetes/applications/components/EditYamlFormSection.tsx b/app/react/kubernetes/applications/components/EditYamlFormSection.tsx index 3d6c4d9a5..bb38e70d1 100644 --- a/app/react/kubernetes/applications/components/EditYamlFormSection.tsx +++ b/app/react/kubernetes/applications/components/EditYamlFormSection.tsx @@ -36,7 +36,7 @@ export function EditYamlFormSection({ onChange={(values) => onChange(values)} id={formId} placeholder="Define or paste the content of your manifest file here" - yaml + type="yaml" /> ); diff --git a/app/react/kubernetes/components/YAMLInspector.tsx b/app/react/kubernetes/components/YAMLInspector.tsx index 4808df40e..5baadf3a7 100644 --- a/app/react/kubernetes/components/YAMLInspector.tsx +++ b/app/react/kubernetes/components/YAMLInspector.tsx @@ -37,7 +37,7 @@ export function YAMLInspector({ readonly hideTitle id={identifier} - yaml + type="yaml" height={expanded ? '800px' : '500px'} onChange={() => {}} // all kube yaml inspectors in CE are read only /> diff --git a/app/react/portainer/templates/custom-templates/CreateView/InnerForm.tsx b/app/react/portainer/templates/custom-templates/CreateView/InnerForm.tsx index dc42952a2..7d7729c64 100644 --- a/app/react/portainer/templates/custom-templates/CreateView/InnerForm.tsx +++ b/app/react/portainer/templates/custom-templates/CreateView/InnerForm.tsx @@ -100,7 +100,7 @@ export function InnerForm({ id="custom-template-creation-editor" value={values.FileContent} onChange={handleChangeFileContent} - yaml + type="yaml" placeholder={texts.editor.placeholder} error={errors.FileContent} > diff --git a/app/react/portainer/templates/custom-templates/EditView/InnerForm.tsx b/app/react/portainer/templates/custom-templates/EditView/InnerForm.tsx index 3075d6ca0..9a78821f0 100644 --- a/app/react/portainer/templates/custom-templates/EditView/InnerForm.tsx +++ b/app/react/portainer/templates/custom-templates/EditView/InnerForm.tsx @@ -92,7 +92,7 @@ export function InnerForm({ id="edit-custom-template-editor" value={gitFileContent || values.FileContent} onChange={handleChangeFileContent} - yaml + type="yaml" placeholder={ gitFileContent ? 'Preview of the file from git repository' diff --git a/app/react/portainer/templates/custom-templates/ListView/StackFromCustomTemplateFormWidget/DeployForm.tsx b/app/react/portainer/templates/custom-templates/ListView/StackFromCustomTemplateFormWidget/DeployForm.tsx index 3bea73914..fb0f214cc 100644 --- a/app/react/portainer/templates/custom-templates/ListView/StackFromCustomTemplateFormWidget/DeployForm.tsx +++ b/app/react/portainer/templates/custom-templates/ListView/StackFromCustomTemplateFormWidget/DeployForm.tsx @@ -119,7 +119,7 @@ export function DeployForm({ } setFieldValue('fileContent', value); }} - yaml + type="yaml" error={errors.fileContent} placeholder="Define or paste the content of your docker compose file here" readonly={isGit}