Skip to content

Add DataTransferItem and related API #55

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Added `items` function for `DataTransfer` and related types (`DataTransferItem`, `DataTransferItemList`) (#55 by @ptrfrncsmrph)
- Added `languages` value for `Navigator` (#59 by @toastal)
- Added `HTMLHtmlElement` module and `documentElement` function `HTMLDocument` (#60 by @toastal)
- Added `onLine` value for `Navigator` (#61 by @toastal)
Expand Down
4 changes: 4 additions & 0 deletions src/Web/HTML/Event/DataTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ exports._files = function (dataTransfer) {
return dataTransfer.files;
};

exports.items = function (dataTransfer) {
return dataTransfer.items;
};

exports.types = function (dataTransfer) {
return dataTransfer.types;
};
Expand Down
5 changes: 5 additions & 0 deletions src/Web/HTML/Event/DataTransfer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.MediaType (MediaType(..))
import Data.Nullable (Nullable, toMaybe)
import Effect (Effect)
import Web.File.FileList (FileList)
import Web.HTML.Event.DataTransfer.DataTransferItem (DataTransferItemList)

foreign import data DataTransfer :: Type

Expand All @@ -29,6 +30,10 @@ files = toMaybe <$> _files

foreign import _files :: DataTransfer -> Nullable FileList

-- | Returns a `DataTransferItemList` object which is a list of all of the drag
-- | data.
foreign import items :: DataTransfer -> DataTransferItemList
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to update the explicit exports at the top of the file so this is usable!


-- | Returns an array of data formats used in the drag operation.
-- | If the drag operation included no data, then the array is empty.
foreign import types :: DataTransfer -> Array String
Expand Down
19 changes: 19 additions & 0 deletions src/Web/HTML/Event/DataTransfer/DataTransferItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

exports.kind = function (dataTransferItem) {
return dataTransferItem.kind;
};

exports.type_ = function (dataTransferItem) {
return dataTransferItem.type;
};

exports._dataTransferItem = function (index) {
return function (dataTransferItemList) {
return dataTransferItemList[index];
};
};

exports._length = function (dataTransferItemList) {
return dataTransferItemList.length;
};
35 changes: 35 additions & 0 deletions src/Web/HTML/Event/DataTransfer/DataTransferItem.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Web.HTML.Event.DataTransfer.DataTransferItem
( DataTransferItem
, DataTransferItemList
) where

import Prelude
import Data.Maybe (Maybe)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable

-- | Returns the drag data item kind, which is either "string" or "file".
foreign import kind :: DataTransferItem -> String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative here would be to create a sum type for this return value, since the spec says that it must be one of either "string" or "file". I don't know if that would be preferable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should create a sum type if those are the two options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps something like DataTransferItemType = StringItem | FileItem? It's a bit ugly 🤷

Copy link
Contributor Author

@pete-murphy pete-murphy Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec calls them Text and File respectively.
image
image

https://html.spec.whatwg.org/#dom-datatransferitem-kind
https://html.spec.whatwg.org/#the-drag-data-item-kind

So my initial thought was

data DataTransferItemKind = Text | File

but I can understand if those are too generic and we'd prefer to suffix with *Kind or *Type (or if Text is maybe an unexpected name for what has a JS value of "string"). Plenty of decisions here 😄 I'll push what I have but am open to suggestions for the naming.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I've got this wrong actually... it can return an empty string "if the DataTransferItem object is in the disabled mode." I'll open this back up for review after I make the changes there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated here 879e724

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see suffixing them with *Kind to avoid clashes with other similar types, but I think it's also fine to assume this module will be imported qualified.


-- | A Unicode string giving the type or format of the data, generally given by
-- | a MIME type. Some values that are not MIME types are special-cased for
-- | legacy reasons. The API does not enforce the use of MIME types; other
-- | values can be used as well. In all cases, however, the values are all
-- | converted to ASCII lowercase by the API.
-- | There is a limit of one text item per item type string.
foreign import type_ :: DataTransferItem -> String

-- | Access an item in the `DataTransferItemList` by index.
dataTransferItem :: Int -> DataTransferItemList -> Maybe DataTransferItem
dataTransferItem = map Nullable.toMaybe <$> _dataTransferItem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these look like they need to be added to the explicit exports


foreign import _dataTransferItem :: Int -> DataTransferItemList -> Nullable DataTransferItem

length :: DataTransferItemList -> Int
length = _length

foreign import _length :: DataTransferItemList -> Int

foreign import data DataTransferItem :: Type

foreign import data DataTransferItemList :: Type