Skip to content

Commit

Permalink
Merge pull request #414 from lhein/308_undo_redo
Browse files Browse the repository at this point in the history
adding Undo and Redo in Source editor #308
  • Loading branch information
lhein authored Nov 24, 2023
2 parents a448f68 + febf2a8 commit ace99a6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
1 change: 0 additions & 1 deletion packages/ui-tests/cypress/support/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ declare global {
editorDeleteLine(line: number, repeatCount: number): Chainable<JQuery<Element>>;
checkCodeSpanLine(spanText: string, linesCount: number | undefined): Chainable<JQuery<Element>>;
checkCodeSpanLine(spanText: string): Chainable<JQuery<Element>>;
syncUpCodeChanges(): Chainable<JQuery<Element>>;
}
}
}
5 changes: 0 additions & 5 deletions packages/ui-tests/cypress/support/next-commands/sourceCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ Cypress.Commands.add('editorAddText', (line, text) => {
Cypress.Commands.add('uploadFixture', (fixture) => {
cy.openSourceCode();
cy.get('.pf-v5-c-code-editor__main > input').attachFile(fixture);
cy.syncUpCodeChanges();
});

Cypress.Commands.add('syncUpCodeChanges', () => {
cy.get('[data-testid="sourceCode--applyButton"]').click();
});

Cypress.Commands.add('editorDeleteLine', (line, repeatCount) => {
Expand Down
22 changes: 22 additions & 0 deletions packages/ui/src/components/SourceCode/RedoButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CodeEditorControl } from '@patternfly/react-code-editor';
import { RedoIcon } from '@patternfly/react-icons';
import { FunctionComponent } from 'react';

interface IRedoIcon {
isVisible: boolean;
onClick: () => void;
}

export const RedoButton: FunctionComponent<IRedoIcon> = (props) => {
return (
<CodeEditorControl
key="redoButton"
icon={<RedoIcon className="icon-redo" />}
aria-label="Redo change"
data-testid="sourceCode--redoButton"
onClick={props.onClick}
tooltipProps={{ content: 'Redo change', position: 'top' }}
isVisible={true}
/>
);
};
18 changes: 16 additions & 2 deletions packages/ui/src/components/SourceCode/SourceCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { setDiagnosticsOptions } from 'monaco-yaml';
import { FunctionComponent, Ref, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
import { EditorDidMount } from 'react-monaco-editor';
import './SourceCode.scss';
import { SyncButton } from './SyncButton';
import './workers/enable-workers';
import { sourceSchemaConfig, SourceSchemaType } from '../../models/camel';
import { EntitiesContext } from '../../providers/entities.provider';
import { UndoButton } from './UndoButton';
import { RedoButton } from './RedoButton';

interface SourceCodeProps {
code: string;
Expand Down Expand Up @@ -56,8 +57,21 @@ export const SourceCode: FunctionComponent<SourceCodeProps> = (props) => {
quickSuggestions: { other: true, strings: true, comments: true },
});

const undoAction = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(editorRef.current?.getModel() as any)?.undo();
};

const redoAction = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(editorRef.current?.getModel() as any)?.redo();
};

const customControls = useMemo(() => {
return [<SyncButton key="sync-button" isDirty={false} isVisible onClick={() => undefined} />];
return [
<UndoButton key="undo-button" isVisible onClick={undoAction} />,
<RedoButton key="redo-button" isVisible onClick={redoAction} />,
];
}, []);

return (
Expand Down
7 changes: 0 additions & 7 deletions packages/ui/src/components/SourceCode/SyncButton.scss

This file was deleted.

33 changes: 0 additions & 33 deletions packages/ui/src/components/SourceCode/SyncButton.tsx

This file was deleted.

22 changes: 22 additions & 0 deletions packages/ui/src/components/SourceCode/UndoButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CodeEditorControl } from '@patternfly/react-code-editor';
import { UndoIcon } from '@patternfly/react-icons';
import { FunctionComponent } from 'react';

interface IUndoIcon {
isVisible: boolean;
onClick: () => void;
}

export const UndoButton: FunctionComponent<IUndoIcon> = (props) => {
return (
<CodeEditorControl
key="undoButton"
icon={<UndoIcon className="icon-undo" />}
aria-label="Undo change"
data-testid="sourceCode--undoButton"
onClick={props.onClick}
tooltipProps={{ content: 'Undo change', position: 'top' }}
isVisible={true}
/>
);
};

0 comments on commit ace99a6

Please sign in to comment.