Skip to content

Commit

Permalink
Fix editor
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardo-forina committed Nov 15, 2024
1 parent f74c6c8 commit d09ac40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/OpenApiEditorWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export async function getNodeSource(
return {
source:
sourceType === "yaml" ? YAML.stringify(source) : JSON.stringify(source),
type: "yaml",
type: sourceType,
};
}

Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/codeEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import {
} from "./CodeEditorMachineContext.ts";

export function CodeEditor() {
const { source } = useMachineSelector((state) => {
const { source, type } = useMachineSelector((state) => {
return {
source: state.context.source,
type: state.context.type,
};
});
const actorRef = useMachineActorRef();
return (
<SourceEditor
source={source}
type={type}
onChangeSourceType={(source, sourceType) => {
actorRef.send({
type: "CHANGE_SOURCE_TYPE",
Expand Down
29 changes: 14 additions & 15 deletions packages/ui/src/components/SourceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ import {
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { SaveIcon, UndoIcon } from "@patternfly/react-icons";
import { editor } from "monaco-editor";
import { Source, SourceType } from "../OpenApiEditorModels.ts";
import { SourceType } from "../OpenApiEditorModels.ts";
import { SectionSkeleton } from "./SectionSkeleton.tsx";
import IStandaloneCodeEditor = editor.IStandaloneCodeEditor;

export function SourceEditor({
source,
type,
onChangeSourceType,
onSave,
}: {
source?: Source;
source?: string;
type: SourceType;
onChangeSourceType: (source: string, targetSourceType: SourceType) => void;
onSave: (source: string, sourceType: SourceType) => void;
}) {
const [height, setHeight] = useState<number>(0);

const [code, setCode] = useState<string | undefined>(source?.source);
const [mode, setMode] = useState(source?.type ?? "yaml");
const [code, setCode] = useState<string | undefined>(source);

const containerRef = useRef<HTMLDivElement | null>(null);
const editorRef = useRef<IStandaloneCodeEditor | null>(null);
Expand All @@ -51,7 +52,7 @@ export function SourceEditor({
}, []);

useEffect(() => {
setCode(source?.source);
setCode(source);
}, [source]);

const onEditorDidMount: CodeEditorProps["onEditorDidMount"] = (e) => {
Expand Down Expand Up @@ -80,10 +81,10 @@ export function SourceEditor({
tooltipProps={{ content: "Save changes" }}
onClick={() => {
if (code) {
onSave(code, mode);
onSave(code, type);
}
}}
isDisabled={isDisabled || source.source === code}
isDisabled={isDisabled || source === code}
>
Save
</CodeEditorControl>,
Expand All @@ -92,12 +93,12 @@ export function SourceEditor({
icon={<UndoIcon />}
onClick={() => {
if (source) {
setCode(source.source);
setCode(source);
}
}}
aria-label={"Revert changes"}
tooltipProps={{ content: "Revert changes" }}
isDisabled={isDisabled || source.source === code}
isDisabled={isDisabled || source === code}
>
Revert
</CodeEditorControl>,
Expand All @@ -108,21 +109,19 @@ export function SourceEditor({
>
<ToggleGroupItem
text={"YAML"}
isSelected={(mode ?? "yaml") === "yaml"}
isSelected={(type ?? "yaml") === "yaml"}
onClick={() => {
if (code) {
setMode("yaml");
onChangeSourceType(code, "yaml");
}
}}
isDisabled={!source}
/>
<ToggleGroupItem
text={"JSON"}
isSelected={mode === "json"}
isSelected={type === "json"}
onClick={() => {
if (code) {
setMode("json");
onChangeSourceType(code, "json");
}
}}
Expand All @@ -134,8 +133,8 @@ export function SourceEditor({
isMinimapVisible={(code?.length ?? 0) < 1_000_000}
isLanguageLabelVisible={false}
onChange={(code) => setCode(code)}
language={source?.type === "json" ? Language.json : Language.yaml}
height={`${height - 90}px`}
language={type === "json" ? Language.json : Language.yaml}
height={`${height - 130}px`}
onEditorDidMount={onEditorDidMount}
emptyState={<SectionSkeleton count={5} />}
code={code}
Expand Down

0 comments on commit d09ac40

Please sign in to comment.