Skip to content

Commit

Permalink
CONSOLE-4407: maintain backwards compatibility with ref object
Browse files Browse the repository at this point in the history
  • Loading branch information
logonoff committed Jan 14, 2025
1 parent d5c1bd9 commit a50c157
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions frontend/packages/console-dynamic-plugin-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ This section documents notable changes in the Console provided shared modules ac
from `@patternfly/react-icons` instead. The `fa-spin` class remains but is deprecated and will be
removed in the future. Plugins should provide their own CSS to spin icons if needed.
- Removed PatternFly 4.x shared modules.
- Upgraded `monaco-editor` to version `0.36.1`.

### PatternFly dynamic modules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ export type CodeEditorProps = {
};

export type CodeEditorRef = {
getEditor: () => monaco.editor.IStandaloneCodeEditor;
getMonaco: () => typeof monaco;
editor: monaco.editor.IStandaloneCodeEditor;
monaco: typeof monaco;
};

export type ResourceYAMLEditorProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const CodeEditor = React.forwardRef<CodeEditorRef, CodeEditorProps>((props, ref)
React.useImperativeHandle(
ref,
() => ({
getEditor: () => editorRef,
getMonaco: () => monacoRef,
editor: editorRef,
monaco: monacoRef,
}),
[editorRef, monacoRef],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ const CodeEditorSidebar: React.FC<CodeEditorSidebarProps> = ({
sanitizeYamlContent,
toggleSidebar,
}) => {
const getEditor = React.useCallback(() => editorRef?.current?.getEditor(), [editorRef]);
const editor = editorRef.current?.editor;

const insertYamlContent = React.useCallback(
(id: string = 'default', yamlContent: string = '', kind) => {
const yaml = sanitizeYamlContent ? sanitizeYamlContent(id, yamlContent, kind) : yamlContent;

const selection = getEditor()?.getSelection();
const selection = editor?.getSelection();
const range = new Range(
selection.startLineNumber,
selection.startColumn,
Expand Down Expand Up @@ -64,18 +64,18 @@ const CodeEditorSidebar: React.FC<CodeEditorSidebarProps> = ({
);

const op = { range, text: indentedText, forceMoveMarkers: true };
getEditor()?.executeEdits(id, [op], [newContentSelection]);
getEditor()?.focus();
editor?.executeEdits(id, [op], [newContentSelection]);
editor?.focus();
},
[sanitizeYamlContent, getEditor],
[sanitizeYamlContent, editor],
);

const replaceYamlContent = React.useCallback(
(id: string = 'default', yamlContent: string = '', kind: string) => {
const yaml = sanitizeYamlContent ? sanitizeYamlContent(id, yamlContent, kind) : yamlContent;
getEditor()?.setValue(yaml);
editor?.setValue(yaml);
},
[sanitizeYamlContent, getEditor],
[sanitizeYamlContent, editor],
);

const downloadYamlContent = React.useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export const fold = (
*/
export const registerAutoFold = (
editor: monaco.editor.IStandaloneCodeEditor,
model: monaco.editor.ITextModel,
alreadyInUse: boolean = false,
) => {
let initialFoldingTriggered = false;
const model = editor.getModel();
const tryFolding = () => {
const document = model.getValue();
if (!initialFoldingTriggered && document !== '') {
Expand Down Expand Up @@ -131,7 +131,6 @@ export const registerYAMLinMonaco = (
}

if (!alreadyInUse) {
const model = editor.getModel();
registerAutoFold(editor, model);
registerAutoFold(editor);
}
};
6 changes: 4 additions & 2 deletions frontend/public/components/edit-yaml.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ const EditYAMLInner = (props) => {
const onCancel = 'onCancel' in props ? props.onCancel : navigateBack;

/** @return {import('monaco-editor').editor.IStandaloneCodeEditor | null} */
const getEditor = () => monacoRef.current?.getEditor();
const getEditor = () => {
return monacoRef.current?.editor;
};

const getModel = React.useCallback(
(obj) => {
Expand Down Expand Up @@ -372,7 +374,7 @@ const EditYAMLInner = (props) => {
}

const newVersion = _.get(props.obj, 'metadata.resourceVersion');
const s = displayedVersion.current !== newVersion;
const s = displayedVersion.current !== newVersion && editorMounted;
setStale(s);
handleError(props.error, success);
if (props.sampleObj) {
Expand Down

0 comments on commit a50c157

Please sign in to comment.