CTRL+Z/undo in composer does not undo image data paste
-
- copy image data to clipboard
- paste in composer and wait for image upload to complete
- when upload is complete, use cmd/ctrl+z to undo
expected: image link/embedded image disappears
actual: nothing happensThis is supported by the fact that copy/pasting and undoing the paste works just fine if text is pasted.
-
-
Applicable:
-
It looks like using
document.execCommand('insertText', false, 'text to insert');
for any JS that needs to insert text into the composer should allow the browser's undo/redo history to be preserved and work as expected.So, when intercepting the paste, the placeholder markdown would be inserted using
document.execCommand
, and then when the image upload is completed, the script would need to set the textarea's selection start and end to select the placeholder text and calldocument.execCommand
to replace it with the image's URL.
-
@anotherusername I wonder if it interferes with progress reporting (the percentages that appear before upload completes).
-
@gąska all of that would need to be reproduced using
document.execCommand
.
-
@anotherusername (Control-Z)
99% uploaded...(Control-Z)
98% uploaded...(Control-Z)
97% uploaded...
-
@greybeard TBH I've really only ever noticed it saying "100%".
-
@anotherusername That's because you're not on @ben_lubar's connection
-
@anotherusername said in CTRL+Z/undo in composer does not undo image data paste:
@greybeard TBH I've really only ever noticed it saying "100%".
ISTR it updates every second until the upload is complete, so if you're on a particularly slow connection or the servers are being dodgy it could go more.
Edit: Though I suppose there's nothing inherently stopping you from exec-ing an undo and then just re-splatting the text in.
Problem being what about people who continue to try typing while the upload s in progress (whether by accident or intentionally), as that would necessarily break that...
-
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
Problem being what about people who continue to try typing while the upload s in progress (whether by accident or intentionally), as that would necessarily break that...
Yeah, that.
Having an undo history for every change, including the ones that were just updates to the percent, is the most logical way to do it.
-
@greybeard said in CTRL+Z/undo in composer does not undo image data paste:
@anotherusername (Control-Z)
99% uploaded...(Control-Z)
98% uploaded...(Control-Z)
97% uploaded...It's almost as if the % uploaded notification shouldn't be in the user-input textbox, where it constantly steals focus and moves the cursor.
-
BTW: This is how Discourse would do it:
- On CTRL-Z
- If last action was an upload
- Remove the text from the input box
- Delete the file on the server
- Identify the file based on the text that was undone, as sent by the client
- Input checking what is input checking?
- User has sent a CTRL-Z and apparently their input is "/etc/passwd"
- 503 OK
-
@lorne-kates Is that actually how Discourse does it, or are you just making up a bad way of doing it and slapping 'Discourse' on it?
-
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
@lorne-kates Is that actually how Discourse does it, or are you just making up a bad way of doing it and slapping 'Discourse' on it?
The fact that you can't tell the difference says a lot about Discourse.
-
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
@lorne-kates Is that actually how Discourse does it, or are you just making up a bad way of doing it and slapping 'Discourse' on it?
It's actually much worse. Discourse regularly scans all posts for links, and if it doesn't find one that corresponds to an upload then it deletes the file.
-
@lorne-kates said in CTRL+Z/undo in composer does not undo image data paste:
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
@lorne-kates Is that actually how Discourse does it, or are you just making up a bad way of doing it and slapping 'Discourse' on it?
The fact that you can't tell the difference says a lot about Discourse.
Oh, I can. I'm asking if you can. What it says a lot about is the sort of posts I expect from you.
-
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
Oh, I can. I'm asking if you can. What it says a lot about is the sort of posts I expect from you.
-
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
@lorne-kates Is that actually how Discourse does it, or are you just making up a bad way of doing it and slapping 'Discourse' on it?
It's actually much worse. Discourse regularly scans all posts for links, and if it doesn't find one that corresponds to an upload then it deletes the file.
Hahaha, good one. You always manage to top my "Discouse is stupid" posts.
...
wait.
-
Thanks for putting the legwork in @anotherusername -- I spent quite awhile looking into doing this but came up empty, so every place we modify the textarea value pretty much clobbers the undo/redo functionality.
I'd be happy to take a look at
execCommand
again today though I vaguely recall I did look into doing this before to no avail.... *
-
@julianlam it appears that it's pretty widely supported, and you can check for support with
document.queryCommandSupported && document.queryCommandSupported('insertText')
-- it should betrue
. If not, you'd probably have to fall back to just modifying the textarea value directly.edit: you'd need to ensure that the textarea has focus before calling
execCommand
... I don't think it'll do anything if something else has focus. Something likevar old_element = document.activeElement; textarea.focus(); document.execCommand('insertText', false, text); if (old_element != textarea) setTimeout(function () { old_element.focus(); }, 0);
...I'm not totally sure that calling
old_element.focus()
has to be wrapped inside that timeout, but it seems safer to assume that it does...
-
@anotherusername said in CTRL+Z/undo in composer does not undo image data paste:
execCommand
Um...
https://i.imgur.com/wAx4QaP.png
nm. Proceed.
-
@lorne-kates said in CTRL+Z/undo in composer does not undo image data paste:
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
@lorne-kates Is that actually how Discourse does it, or are you just making up a bad way of doing it and slapping 'Discourse' on it?
It's actually much worse. Discourse regularly scans all posts for links, and if it doesn't find one that corresponds to an upload then it deletes the file.
Hahaha, good one. You always manage to top my "Discouse is stupid" posts.
...
wait.
Oh god I was actually right?!?!?
Dammit, I'm thinking like a DiscoDev, someone please send help!
-
Shit. Maybe
execCommand
won't work. Apparently, textareas do not support theinsertText
command? You'd need to replace it with acontenteditable
element...which, IIRC, wouldn't natively support undo/redo at all...
-
When an HTML document has been switched to designMode, the document object exposes the execCommand method which allows one to run commands to manipulate the contents of the editable region.
-- via Document.execCommand, MDNLies! designMode doesn't need to be switched on! This changes everything.
In any case unless I'm misunderstanding something, this proof-of-concept pen shows that it does work at least...
I also tried it in console on community.nodebb.org and it worked fine, so... interesting, will do some hacking around and see what I come up with
Edit: caniuse says Opera Mini won't support it, so I'll use the fallback anyway. I wonder how prevalent Opera Mini is in the wild...
Edit 2: Ah, it does not seem to work in Firefox.
More resources:
-
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
Oh god I was actually right?!?!?
Wait you were guessing?!?
-
@lorne-kates said in CTRL+Z/undo in composer does not undo image data paste:
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
Oh god I was actually right?!?!?
Wait you were guessing?!?
I thought I was! Now I'm not so sure!!!
-
While we're at it, let's make it so you can also Ctrl-Z format button inserts or autocompletes of usernames or emojis.
-
@pie_flavor said in CTRL+Z/undo in composer does not undo image data paste:
While we're at it, let's make it so you can also Ctrl-Z format button inserts or autocompletes of usernames or emojis.
And while you're at thatitalicised text** you might as well keep format button state too so you can dutifully ignore the activation thereof and just skip forward one or two places.
-
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
thatitalicised text**
@boomzilla your alts are leaking again! that is @Lorne-Kates 's thing
-
@luhmann said in CTRL+Z/undo in composer does not undo image data paste:
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
thatitalicised text**
@boomzilla your alts are leaking again! that is @Lorne-Kates 's thing
Yes, did I channel it well?
-
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
Yes, did I channel it well?
It's not perfect until @pie_flavor rolls his eyes
-
@luhmann said in CTRL+Z/undo in composer does not undo image data paste:
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
Yes, did I channel it well?
It's not perfect until @pie_flavor rolls his eyes
-
What's this **bolded text thing you guys have got going on? Are we messing up somewhere?
-
@julianlam No, it's a particular foul-mouthed, change-resistant canadian being himself. It's become a meme. It's done by **bold text (view the raw).
As a side note--inserting an emoji into the
abbr
tag breaks it. CF this is broken--it should have a in anabbr
tag, but it doesn't
-
@julianlam said in CTRL+Z/undo in composer does not undo image data paste:
What's this **bolded text thing you guys have got going on? Are we messing up somewhere?
Yes, you are. It's why I've been doing thisbolded text**** since day one.
Pressing CTRL-B (or clicking the BOLD button) which inside bolded text just inserts a second
**bolded text**
instead of exiting bold mode.I am typing
I am typing
**bolded text**
I am typing
**something in bold because that's where the highlight was**
I am typing
**something in bold because that's where the highlight was**bolded text****
and I just pressed CTRL-B to close my bold section, oops it just did that instead so I'll press END to go to where the cursor SHOULD be and keep typing.
-
@julianlam said in CTRL+Z/undo in composer does not undo image data paste:
What's this **bolded text thing you guys have got going on? Are we messing up somewhere?
- Type stuff
- Click "bold" (or ctrl-b)
- Type stuff that should be bold
- Click "bold" again (or ctrl-b)
- Type more stuff
Expected result:
Bold turns off and "more stuff" isn't bold.Actual result:
New "bold" markup is added inside the existing bold markup.Simplest solution would probably be to make the bold button skip forward two characters if the cursor is followed by exactly two
*
s. Similar for italics (if the cursor is followed by exactly one*
).
-
Ah, thanks! Let me look at that now. :smiling_face_with_open_mouth_cold_sweat:
-
@julianlam said in CTRL+Z/undo in composer does not undo image data paste:
Ah, thanks! Let me look at that now. :smiling_face_with_open_mouth_cold_sweat:
Can we ban @julianlam ? I miss the stubborn "You're " diatribes enough that I think a short-ish "disciplinary" period enforced by the means of a temporary ban is needed, so Julian can reflect on his lack of providing material for my entertainment.
-
Don't worry, I'm sure you'll get enough material out of my half-baked fixes anyhow.
Filed Under: Don't mix bold and italics, people.
-
@julianlam But how is @pie_flavor going to get his eye exercise now? D:
-
@heterodox said in CTRL+Z/undo in composer does not undo image data paste:
@julianlam But how is @pie_flavor going to get his eye exercise now? D:
I'm sure someoneitalicised text will trigger hime some wae or another.
-
@tsaukpaetra said in CTRL+Z/undo in composer does not undo image data paste:
@heterodox said in CTRL+Z/undo in composer does not undo image data paste:
@julianlam But how is @pie_flavor going to get his eye exercise now? D:
I'm sure someoneitalicised text will trigger
himehis mom some wae or another.LKTFY