-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(editor): restore cursor position after tag/env-var completion in code editors #3406
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
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b3d3312
fix(editor): restore cursor position after tag/env-var completion in …
waleedlatif1 3983524
lint
waleedlatif1 a47d64e
refactor(editor): extract restoreCursorAfterInsertion helper, fix wea…
waleedlatif1 0983950
updated
waleedlatif1 b9882c7
fix(editor): replace useEffect with direct ref assignment for editorV…
waleedlatif1 a651923
fix(editor): guard cursor restoration behind preview/readOnly check
waleedlatif1 f37f92b
fix(editor): escape blockId in CSS selector with CSS.escape()
waleedlatif1 167f64c
perf(editor): use ref for cursor fallback to stabilize useCallback
waleedlatif1 a73bd6c
refactor(editor): pass cursor position explicitly from dropdowns
waleedlatif1 d3e7186
docs(editor): fix JSDoc terminology — macrotask not microtask
waleedlatif1 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
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
43 changes: 43 additions & 0 deletions
43
...im/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/utils.ts
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,43 @@ | ||
| /** | ||
| * Type of dropdown insertion that determines the delimiter pair used | ||
| * for cursor position computation. | ||
| */ | ||
| export type InsertionType = 'tag' | 'envVar' | ||
|
|
||
| /** | ||
| * Restores the cursor position in a textarea after a tag or env-var | ||
| * dropdown insertion. Computes where the inserted token ends in the | ||
| * new value and places the cursor right after it. | ||
| * | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| * @param textarea - The textarea element to restore cursor in | ||
| * @param liveValue - The textarea value before the insertion | ||
| * @param liveCursor - The cursor position before the insertion | ||
| * @param newValue - The full new value after the insertion | ||
| * @param type - The type of insertion ('tag' for `<>`, 'envVar' for `{{}}`) | ||
| */ | ||
| export function restoreCursorAfterInsertion( | ||
| textarea: HTMLTextAreaElement | null, | ||
| liveValue: string, | ||
| liveCursor: number, | ||
| newValue: string, | ||
| type: InsertionType | ||
| ): void { | ||
| const [openDelim, closeDelim, closeLen] = | ||
| type === 'tag' ? (['<', '>', 1] as const) : (['{{', '}}', 2] as const) | ||
|
|
||
| // insertPos indexes into liveValue, but is reused to search newValue. | ||
| // This is valid because text before the trigger character is identical | ||
| // in both strings — the insertion only mutates text at/after the delimiter. | ||
| const insertPos = liveValue.slice(0, liveCursor).lastIndexOf(openDelim) | ||
|
waleedlatif1 marked this conversation as resolved.
Outdated
|
||
| const searchFrom = insertPos !== -1 ? insertPos : liveCursor | ||
| const closingPos = newValue.indexOf(closeDelim, searchFrom) | ||
|
waleedlatif1 marked this conversation as resolved.
Outdated
|
||
| const newCursorPos = closingPos !== -1 ? closingPos + closeLen : newValue.length | ||
|
|
||
| setTimeout(() => { | ||
| if (textarea) { | ||
| textarea.focus() | ||
| textarea.selectionStart = newCursorPos | ||
| textarea.selectionEnd = newCursorPos | ||
| } | ||
| }, 0) | ||
| } | ||
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.