Skip to content
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

x/playground: cannot restore code selected and deleted by pressing tab #41037

Closed
ProximaB opened this issue Aug 25, 2020 · 11 comments
Closed

x/playground: cannot restore code selected and deleted by pressing tab #41037

ProximaB opened this issue Aug 25, 2020 · 11 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ProximaB
Copy link

What version of Go are you using (go version)?

The playground uses the latest stable release of Go.
The current version is go1.14.7.

Web browser and operating system

The latest Chrome, Windows 10

What operating system and processor architecture are you using (go env)?

That running on https://play.golang.org/.

How to reproduce a bug?

Select any part of text and click Tab key. Code disappear, cannot undo changes.

What did you expect to do?

Got my code back.

What did you see instead?

Nothing 😅

@ALTree ALTree changed the title Bug play.golang.org. Selected code disapear when clicked Tab button. Cannot undo changes. x/playground: cannot restore code selected and deleted by pressing tab Aug 26, 2020
@gopherbot gopherbot added this to the Unreleased milestone Aug 26, 2020
@ALTree
Copy link
Member

ALTree commented Aug 26, 2020

Interesting. If you select some code and delete it by pressing backspace, canc, or any letter/number, you can get it back by pressing Ctrl+z, but if you delete it by pressing Tab, Ctrl+z doesn't work.

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 26, 2020
@ProximaB
Copy link
Author

ProximaB commented Aug 26, 2020

@ALTree I found the cause of that issue. The playground has a bug in a custom tab handle that erases selected code and replace it with "\t" programmatically in a way that doesn't support native undo feature.
Please help me to make pull request, I really would like to contribute into this project.

//playground.js
function insertTabs(n) {
    // find the selection start and end
    var start = code[0].selectionStart;
    var end   = code[0].selectionEnd;
    // split the textarea content into two, and insert n tabs
    var v = code[0].value;
    var u = v.substr(0, start);
    for (var i=0; i<n; i++) {
    u += "\t";
    }
    u += v.substr(end);
    // set revised content
    code[0].value = u; <-- this is cause of the bug
    // reset caret position after inserted tabs
    code[0].selectionStart = start+n;
    code[0].selectionEnd = start+n;
}

I write solutions and it seems to work on Chrome and Firefox.
Simply replace line code[0].value =u with the following snippet of code:

field = code[0]
let document = field.ownerDocument;

if (!document.execCommand('insertText', false, u)) {
    //firefox only
	field.setRangeText(
		u,
		start || 0,
		end || 0,
		'end'
	);

	field.dispatchEvent(new InputEvent('input', {
		data: u,
		inputType: 'insertText',
		isComposing: false
	}));
}

@ALTree
Copy link
Member

ALTree commented Sep 1, 2020

Feel free to send a PR. playground.js should be the one here: https://github.com/golang/website/tree/master/content/static. I can't comment on your solution because I don't know javascript.

@ProximaB
Copy link
Author

ProximaB commented Sep 1, 2020

@ALTree I made a pull-request golang/website#13
Thanks 😊

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 5, 2020
ProximaB added a commit to ProximaB/tools that referenced this issue Sep 16, 2020
The playground suffer from a bug that on tab click replace selected code with "\t" programmatically in a way that doesn't support native undo feature.

For golang/go#41037.
@ProximaB ProximaB changed the title x/playground: cannot restore code selected and deleted by pressing tab godoc/static: rewrite tab insertion so undo works Sep 16, 2020
@gopherbot
Copy link

Change https://golang.org/cl/255157 mentions this issue: godoc/static: rewrite tab insertion so undo works

@ProximaB ProximaB changed the title godoc/static: rewrite tab insertion so undo works x/playground: cannot restore code selected and deleted by pressing tab Sep 16, 2020
@gopherbot
Copy link

Change https://golang.org/cl/252377 mentions this issue: x/playground: Update x/playground its go.mod to pick up the newer version

ProximaB added a commit to ProximaB/playground that referenced this issue Sep 16, 2020
Update go.mod will pick up newer version containing fix golang/tools#251

For golang/go#41037
@gopherbot
Copy link

Change https://golang.org/cl/255177 mentions this issue: x/playground: Update go.mod to pick up the newer version

@distinctdan
Copy link

Just ran into this and lost some work, super obnoxious bug. Any progress on fixing this?

@ProximaB
Copy link
Author

ProximaB commented Feb 10, 2021

Just ran into this and lost some work, super obnoxious bug. Any progress on fixing this?

Use this goplay.tools

I fixed that issue, unfortunately it hasn't been merged because I've used execCommand which is described as obsolete in Mozilla documentation. I didn't find any other solutions to fix that on others browsers Blink/Webkit based without reimplementing code editor. The case here is to invoke native edit event to maintain textbook capabilities like undo, redo etc.

@olavfosse
Copy link

Bumping, just ran into this myself. very annoying

@gopherbot
Copy link

Change https://golang.org/cl/366535 mentions this issue: _content/js: make tab on selection undoable

passionSeven added a commit to passionSeven/website that referenced this issue Oct 18, 2022
People often highlight a selection and type tab, hoping to indent it.
Instead, the editor replaces it with a tab. That's fine - some editors work that way.
But the old implementation did not push that edit onto the undo stack,
so that there was no way to get the deleted selection back. Fix that.

Fixes golang/go#41037.

Change-Id: I9f2aa94ac389a3905d07411b0a156dbc66a7ec8a
Reviewed-on: https://go-review.googlesource.com/c/website/+/366535
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Website-Publish: Russ Cox <rsc@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
@golang golang locked and limited conversation to collaborators Nov 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants