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 all 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)

Bugfixes:

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
6 changes: 6 additions & 0 deletions src/Web/HTML/Event/DataTransfer.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Web.HTML.Event.DataTransfer
( DataTransfer
, files
, items
, types
, getData
, setData
Expand All @@ -18,6 +19,7 @@ import Data.Nullable (Nullable, toMaybe)
import Effect (Effect)
import Web.DOM.Element (Element)
import Web.File.FileList (FileList)
import Web.HTML.Event.DataTransfer.DataTransferItem (DataTransferItemList)

foreign import data DataTransfer :: Type

Expand All @@ -31,6 +33,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
25 changes: 25 additions & 0 deletions src/Web/HTML/Event/DataTransfer/DataTransferItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

exports._kind = function (nothing, just, text, file, dataTransferItem) {
if (dataTransferItem.kind === "string") {
return just(text);
} else if (dataTransferItem.kind === "file") {
return just(file);
} else {
return nothing;
}
};

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

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

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

import Prelude

import Data.Function.Uncurried (Fn5)
import Data.Function.Uncurried as Uncurried
import Data.Maybe (Maybe(..))
import Data.Nullable (Nullable)
import Data.Nullable as Nullable

data DataTransferItemKind = Text | File

derive instance Eq DataTransferItemKind
derive instance Ord DataTransferItemKind

instance Show DataTransferItemKind where
show = case _ of
Text -> "Text"
File -> "File"

foreign import _kind
:: Fn5 (forall x. Maybe x)
(forall x. x -> Maybe x)
DataTransferItemKind
DataTransferItemKind
DataTransferItem
(Maybe DataTransferItemKind)

-- | Returns the drag data item kind of the `DataTransferItem`. In the case
-- | where the `DataTransferItem` object is in _disabled mode_, `Nothing` is
-- | returned.
kind :: DataTransferItem -> Maybe DataTransferItemKind
kind = Uncurried.runFn5 _kind Nothing Just Text File

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

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

-- | 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 _length :: DataTransferItemList -> Int

length :: DataTransferItemList -> Int
length = _length

foreign import data DataTransferItem :: Type

foreign import data DataTransferItemList :: Type