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

Open
ProximaB opened this issue Aug 25, 2020 · 10 comments
Open

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

ProximaB opened this issue Aug 25, 2020 · 10 comments
Labels
Milestone

Comments

@ProximaB
Copy link

@ProximaB ProximaB commented Aug 25, 2020

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 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.

@ProximaB
Copy link
Author

@ProximaB 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 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 ProximaB commented Sep 1, 2020

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

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

@gopherbot gopherbot commented Sep 16, 2020

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

@gopherbot gopherbot commented Sep 16, 2020

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

@gopherbot gopherbot commented Sep 16, 2020

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

@distinctdan
Copy link

@distinctdan distinctdan commented Feb 10, 2021

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

@ProximaB
Copy link
Author

@ProximaB 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.

@fossegrim
Copy link

@fossegrim fossegrim commented Mar 23, 2021

Bumping, just ran into this myself. very annoying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants