fix(composer): make toolbar formatting undoable with Ctrl+Z - #178
Open
palmoni5 wants to merge 1 commit into
Open
fix(composer): make toolbar formatting undoable with Ctrl+Z#178palmoni5 wants to merge 1 commit into
palmoni5 wants to merge 1 commit into
Conversation
The formatting helpers (insertIntoTextarea, replaceSelectionInTextareaWith,
wrapSelectionInTextareaWith) replaced the whole textarea value via
$textarea.val(), which bypasses the browser's undo stack. Clicking a toolbar
button (bold, code, link...) could therefore not be reverted with Ctrl/Cmd+Z,
and in some browsers also wiped the undo history of the text typed before it.
Route all three through a shared replaceRange() helper that uses
document.execCommand('insertText') so the change is recorded as a regular
edit, with setRangeText / direct value assignment as fallbacks.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
None of the composer toolbar actions (bold, italic, code, link, list, …) can be undone with Ctrl/Cmd+Z. The three text-insertion helpers in
static/lib/composer/controls.jsreplace the entire textarea value via$textarea.val(...), which is not recorded in the browser's undo stack. In Chromium-based browsers it also discards the undo history of everything typed before the click, so the user has to remove the inserted markup by hand.Fix
Add a small
replaceRange(textarea, start, end, text)helper and routeinsertIntoTextarea,replaceSelectionInTextareaWithandwrapSelectionInTextareaWiththrough it. It selects the target range and usesdocument.execCommand('insertText'), which browsers treat as a regular user edit and push onto the undo stack. If the command is unavailable or reports failure it falls back tosetRangeText, and finally to a plain value assignment, so behaviour is unchanged where native insertion is not supported.The
action:composer.*hooks and the return value ofwrapSelectionInTextareaWithare untouched, so callers in core and nodebb-plugin-markdown need no changes.Testing
Verified the new helper in isolation in Chromium: wrap a selection in backticks, then insert
**at the start, thenundotwice — the textarea returns to its exact original value at each step (hello world→hello `world`→**hello `world`→ back tohello `world`→ back tohello world). ESLint passes.