Skip to content

Commit 4c15036

Browse files
committed
[LG-5129] feat(code-editor): add undo/redo functionality #3063 (#3091)
* feat(CodeEditor): add undo and redo functionality to the editor - Implemented handleUndo and handleRedo methods to allow users to undo and redo editor actions. - Updated CodeEditorHandle interface to include undo and redo methods, enhancing the editor's capabilities. - Improved user experience by providing feedback when undo/redo actions are unavailable. * feat(CodeEditor): add undo and redo methods to context and editor - Introduced undo and redo functions in the CodeEditorContext to enhance user interaction. - Updated the CodeEditor component to integrate these new functionalities, improving the overall editing experience. * feat(Panel): integrate undo and redo functionality in the editor panel - Added handleUndoClick and handleRedoClick methods to the Panel component to utilize the new undo and redo functions from the CodeEditorContext. - Updated the onClick handlers for the Undo and Redo menu items to call the new methods, enhancing user interaction and experience. * feat(CodeEditor): implement undo and redo methods in tests and utilities - Added tests for undo and redo functionalities in the CodeEditor component, ensuring they work correctly with content changes. - Enhanced test utilities to include getContent, undo, and redo methods, providing a more comprehensive testing framework for the editor. - Updated the Panel component tests to verify the integration of undo and redo actions in the user interface, improving overall test coverage. * feat(CodeEditor): enhance imperative handle API and testing utilities - Updated the imperative handle test to expose a complete API, including methods for undo, redo, getContents, formatCode, and getEditorViewInstance. - Added a new utility function, getHandle, to retrieve the editor handle instance for testing imperative methods. - Modified the insertText function to default to inserting text at the end of the document, improving usability in tests. * feat(CodeEditor): enhance documentation and add imperative API details - Updated the README to clarify the various methods for formatting code in the CodeEditor, emphasizing the programmatic access through the CodeEditor ref. - Introduced a new section detailing the imperative API, including methods for content manipulation, formatting, and undo/redo operations. - Added examples for using the imperative methods, improving the documentation for developers. - Adjusted the formatting options description to enhance clarity and usability. * changeset * chore: remove obsolete clever-editors-undo changeset file * refactor(CodeEditor): reorganize testing utilities - Moved `codeSnippets` to a dedicated testing file for better organization and accessibility. - Updated import paths in the CodeEditor stories and specs to reflect the new location of `codeSnippets`. - Introduced comprehensive test utilities for CodeMirror extension hooks, enhancing the testing framework's capabilities. * feat(CodeEditor): add comprehensive testing utilities for Panel component - Introduced `renderPanel` function to facilitate rendering of the Panel component with necessary context for testing. - Added `PanelSelectors` for consistent access to panel elements in tests. - Implemented various interaction methods for testing user interactions with the Panel, including format and copy button actions. - Enhanced README documentation to include new testing utilities and examples for better guidance on testing Panel interactions. * refactor(CodeEditor): update import paths for testing utilities - Changed import paths in various extension spec files to reflect the new location of testing utilities, improving organization and accessibility. - Ensured consistency across all extension tests by standardizing the import structure. * refactor(CodeEditor): update module references for undo/redo functionality - Changed references from `modules` to `coreModules` in the `handleUndo` and `handleRedo` functions to align with the updated module loading structure. - Improved code clarity and maintainability by ensuring consistent usage of the core modules for command handling. * refactor(CodeEditor): update testing utilities and documentation - Renamed mock functions to default stub functions for clarity in the CodeEditor context. - Updated the README to reflect changes in the testing utilities, including new context configuration options for overriding default functions in tests. - Enhanced test cases to utilize the new stub functions, improving consistency and maintainability in testing the Panel component. * refactor(CodeEditor): update import structure in useIndentExtension tests - Reorganized import statements in the useIndentExtension.spec.ts file to improve clarity and maintainability. - Moved the IndentUnits import to align with the updated structure of testing utilities. * refactor(CodeEditor): update Panel tests to use default context functions - Removed the use of mockPanelFunctions in Panel tests, replacing them with defaultPanelContextFunctions for improved clarity and consistency. - Updated test cases to directly utilize context configuration for undo and redo actions, enhancing maintainability and readability of the tests.
1 parent f4a3021 commit 4c15036

28 files changed

+1240
-275
lines changed

.changeset/sixty-turtles-tell.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@leafygreen-ui/code-editor': minor
3+
---
4+
5+
**Feature**: Add comprehensive undo/redo functionality to CodeEditor component
6+
7+
- **Imperative API**: Added `undo()` and `redo()` methods to CodeEditor ref handle that return boolean success indicators
8+
- **Panel integration**: Existing undo/redo buttons in Panel secondary menu now perform actual operations instead of just triggering callbacks
9+
- **Context support**: Undo/redo functions available to child components via `CodeEditorContext` for custom UI implementations
10+
- **Enhanced TypeScript**: Complete `CodeEditorHandle` interface documentation with all imperative methods (`getContents`, `formatCode`, `undo`, `redo`, `getEditorViewInstance`)
11+
- **Test utilities**: New testing helpers including `editor.getContent()`, `editor.getHandle()`, `editor.interactions.undo()`, and `editor.interactions.redo()`
12+
- **Improved testing**: Functional tests verify actual content changes rather than just mock function calls
13+
- **Documentation**: New "Imperative API" section in README with comprehensive examples and reorganized structure
14+
15+
**Technical implementation**: Leverages CodeMirror's built-in `@codemirror/commands` module with proper error handling and integration into existing lazy-loading architecture. Maintains full backward compatibility while extending functionality.

0 commit comments

Comments
 (0)