-
-
Notifications
You must be signed in to change notification settings - Fork 215
feat(editor): add undo redo icons to editor - I204 #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DianaLease
merged 10 commits into
accordproject:main
from
teja-pola:teja/i204/add-undo-redo-icons
Mar 18, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f2b899
fix(button): fix dark mode toggle click - I201
teja-pola 1d568d6
fix(button): fix dark mode toggle click - I201
teja-pola 122432b
fix(button): fix dark mode toggle click - I201
teja-pola 57261e5
update is checked
teja-pola bd00e3f
feat(editor): add undo redo icons to editor - I204
teja-pola 289429e
feat(editor): add undo redo icons to editor - I204
teja-pola f3a4bec
feat(editor): add undo redo icons to editor - I204
teja-pola e26ba48
feat(editor): add undo redo icons to editor - I204
teja-pola 2fafb57
feat(editor): add undo redo icons to editor - I204
teja-pola 5926053
feat(editor): add undo redo icons to editor - I204
teja-pola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { useState } from 'react'; | ||
| function useUndoRedo<T>(initialValue: T, onChange?: (value: T) => void) { | ||
| const [past, setPast] = useState<T[]>([]); | ||
| const [present, setPresent] = useState<T>(initialValue); | ||
| const [future, setFuture] = useState<T[]>([]); | ||
|
|
||
| const setValue = (newValue: T) => { | ||
| setPast((prevPast) => [...prevPast, present]); | ||
| setPresent(newValue); | ||
| setFuture([]); | ||
| if (onChange) onChange(newValue); // Ensure preview updates | ||
| }; | ||
|
|
||
| const undo = () => { | ||
| if (past.length === 0) return; | ||
| const previous = past[past.length - 1]; | ||
| setPast((prevPast) => prevPast.slice(0, -1)); | ||
| setFuture((prevFuture) => [present, ...prevFuture]); | ||
| setPresent(previous); | ||
| if (onChange) onChange(previous); | ||
| }; | ||
|
|
||
| const redo = () => { | ||
| if (future.length === 0) return; | ||
| const next = future[0]; | ||
| setFuture((prevFuture) => prevFuture.slice(1)); | ||
| setPast((prevPast) => [...prevPast, present]); | ||
| setPresent(next); | ||
| if (onChange) onChange(next); | ||
| }; | ||
|
|
||
| return { value: present, setValue, undo, redo }; | ||
| } | ||
|
|
||
| export default useUndoRedo; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,48 @@ | ||
| import MarkdownEditor from "../MarkdownEditor"; | ||
| import useAppStore from "../../store/store"; | ||
| import useUndoRedo from "../../components/useUndoRedo"; | ||
| import { useCallback } from "react"; | ||
| import { debounce } from "ts-debounce"; | ||
| import { FaUndo, FaRedo } from "react-icons/fa"; | ||
|
|
||
| function TemplateMarkdown() { | ||
| const editorValue = useAppStore((state) => state.editorValue); | ||
| const setEditorValue = useAppStore((state) => state.setEditorValue); | ||
| const setTemplateMarkdown = useAppStore((state) => state.setTemplateMarkdown); | ||
| const backgroundColor = useAppStore((state) => state.backgroundColor); | ||
| const textColor = useAppStore((state) => state.textColor); | ||
|
|
||
| const backgroundColor = useAppStore((state) => state.backgroundColor); | ||
| const setTemplateMarkdown = useAppStore((state) => state.setTemplateMarkdown); | ||
| const { value, setValue, undo, redo } = useUndoRedo( | ||
| useAppStore((state) => state.editorValue), | ||
| setTemplateMarkdown // Ensures preview updates when undo/redo happens | ||
| ); | ||
|
|
||
| const debouncedSetTemplateMarkdown = useCallback( | ||
| debounce((value: string) => { | ||
| void setTemplateMarkdown(value); | ||
| }, 500), | ||
| [] | ||
| [setTemplateMarkdown] | ||
| ); | ||
|
|
||
| const handleChange = (value: string | undefined) => { | ||
| if (value !== undefined) { | ||
| setEditorValue(value); | ||
| setValue(value); | ||
| debouncedSetTemplateMarkdown(value); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="column" style={{ backgroundColor: backgroundColor }}> | ||
| <h2 style={{ color: textColor }}>TemplateMark</h2> | ||
| <div className="column" style={{ backgroundColor }}> | ||
| <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}> | ||
| <h3 style={{ color: textColor }}>TemplateMark</h3> | ||
| <div> | ||
| <FaUndo onClick={undo} title="Undo" style={{ cursor: "pointer", color: textColor, marginRight: "8px" }} /> | ||
| <FaRedo onClick={redo} title="Redo" style={{ cursor: "pointer", color: textColor }} /> | ||
| </div> | ||
| </div> | ||
| <p style={{ color: textColor }}> | ||
| A natural language template with embedded variables, conditional | ||
| sections, and TypeScript code. | ||
| A natural language template with embedded variables, conditional sections, and TypeScript code. | ||
| </p> | ||
| <MarkdownEditor value={editorValue} onChange={handleChange} /> | ||
| <MarkdownEditor value={value} onChange={handleChange} /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default TemplateMarkdown; | ||
| export default TemplateMarkdown; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.