Comments (9)
For some reason, I thought the beforecopy/beforecut/beforepaste events were used as signals for whether or not to enable the corresponding context menu items. However, in my experiments, Chrome never fired beforecopy/beforecut/beforepaste events before showing a context menu, even with a selected range. So this either changed at some point, or my memory is faulty.
What actually seems to happen in Chrome when you call queryCommandEnabled('copy') is this:
- First, check if copy is supported. If copy is not supported, return false. Note that queryCommandSupported('copy') only returns true with an active user gesture (!!) so much of the time, queryCommandEnabled('copy') returns false immediately here.
- If the current selection is in a password field, return false.
- Fire a beforecopy event. The event target is determined the same way as the event target for firing a copy event: it's based on the current selection, or the element if there is no selection. If preventDefault() is called, return true.
- If there is a selected range, return true.
- Otherwise, return false.
So it actually seems pretty close to what you're proposing. In Chrome, I'm planning on taking the user gesture check out of step 1 so it always returns true. Then the rest follows pretty closely to what you proposed. Two questions:
- What do IE/FF do with the beforecopy/beforecut/beforepaste events today?
- Is it OK for queryCommandEnabled('copy') to return true when execCommand('copy') might not work, due to lack of a user gesture?
from clipboard-apis.
Apologies - I'm struggling to follow the user case for this, when / how would these events be used?
Gut instinct says than preventingDefault / returning false in an event should result in the enabledness check to return false, not true?
Regarding 2.) - I would prefer that queryCommandEnabled returns true when execCommand is going to work. Otherwise, if it returns true when execCommand fails, whats the point of the developer ever checking it? Just check queryCommandSupported and try execCommand and see what happens.
from clipboard-apis.
@gauntface well, these events are a bit .. weird. But think of it this way (or read the spec): the "default action" of the event is to run the UA's built-in logic for enabling/disabling copy/cut/paste commands. The built-in logic will likely conclude for example that "copy" should not be enabled if there is no selection. (It will also conclude that the copy action is not available to document.execCommand() if the JS thread is not user-initiated). When you override this built-in logic (by preventing the "default" outcome) the command will end up being considered enabled.
As for the use case, say you have a complex web app where one UI library handles copy/cut/DnD and another component is responsible for rendering some buttons. Rather than having a tight coupling between those components, the library handling buttons could call queryCommandEnabled('copy') and the DnD library could listen to 'beforecopy' and call preventDefault() if it wanted the button to be enabled.
from clipboard-apis.
For some reason, I thought the beforecopy/beforecut/beforepaste
events were used as signals for whether or not to enable the
corresponding context menu items.
Well, that's how they were intended to work (but AFAIK even in IE which invented these events, support has left much to be desired..).
queryCommandSupported('copy') only returns true with an active user gesture (!!)
Well, that sounds wrong :) But IMO it would be useful to just move that check to the queryCommandEnabled() code - say, instead of the "is there a selection" check. So the question "is this supported" responds true and the question "is this enabled" returns false if execCommand() would indeed fail?
(Now, while I've been pushing for this as a way to feature-detect user-initiated execCommand(copy|cut) support, I admit that it's a bit inconsistent with how queryCommandEnabled() works with other commands. Maybe the perfect API would have a tri-state return value from queryCommandEnabled() - for example false,true,'user initiated'..)
So it actually seems pretty close to what you're proposing.
Indeed.
from clipboard-apis.
+1 being able to call queryCommandSupported() and get true where is it supported and then use queryCommandEnabled() to check whether execCommand will succeed or not.
from clipboard-apis.
Well, I am just wondering how all the other browsers are returning true when queryCommandSupported() is used but Chrome always returns false. I just started trying to implement this into our CMS today, as FF announced support for it this month, only to find out that Chrome has a weird behavior that it always returns false so my copy button is never displayed in chrome, when chrome does support it, plus it has been almost 3 months since this was last commented on, is anything being done with it?
from clipboard-apis.
+1 for queryCommandSupported() to return true where is it supported
from clipboard-apis.
btw, I had to write a polyfill for document.queryCommandSupported to get this right on Chrome and Firefox
from clipboard-apis.
before(cut|copy|paste) events are now killed. Goodbye to this bug too. See #25 for details.
from clipboard-apis.
Related Issues (20)
- Why does the Clipboard interface extends EventTarget?
- Broken references in Clipboard API and events
- Web Platform tests using "clipboard-read" HOT 2
- Async Clipboard API take more time compared to DataTransfer API to read/write data in System Clipboard HOT 1
- Read Blob data for the supported formats on-demand during getType. HOT 11
- Start and End fragment tags in text/html format on Windows HOT 1
- Add method `match` (`clipboard.match(regex)`) to check clipboard for pattern matching before reading - as it is done in iOS HOT 1
- Optional data types description seems wrong HOT 1
- Feature detection of web custom format support HOT 1
- Parallelism and task sources HOT 2
- algorithms don't enforce focus requirement
- Aljbore HOT 1
- References to permissions
- Topic: ‘unsanitized’ option to allow developers to opt-out of UA-provided sanitization HOT 1
- Merge a PR only with explicit implementor consensus (followup) HOT 4
- read(formats) ambiguous
- RTF is not exposed when using navigator.clipboard.read()
- `supports` should take a dictionary instead of a DOMString as argument for readability HOT 2
- [Async Clipboard API] Use Stream APIs to provide data during write HOT 10
- No provision made for multiple clipboards, e.g. on Linux
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clipboard-apis.