Skip to content

Add page visibility state functionality #79

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 5 commits into from
Sep 19, 2022
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
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "ide",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ Breaking changes:

New features:
- Added `close` function for `Window` (#78 by @jmp-0x7C0)
- Added `visibilityState` for `HTMLDocument` (#79 by @garyb)

Bugfixes:

Other improvements:
- Added `purs-tidy` config and format checking (#79 by @garyb)
- `HTMLDocument`'s FFI is now implemented in terms of `EffectFn` (#79 by @garyb)

## [v4.0.0](https://github.com/purescript-web/purescript-web-html/releases/tag/v4.0.0) - 2022-04-27

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict"
"build": "eslint src && purs-tidy check --config-require src && pulp build -- --censor-lib --strict"
},
"devDependencies": {
"eslint": "^7.15.0",
"pulp": "16.0.0-0",
"purescript-psa": "^0.8.2",
"purs-tidy": "^0.9.2",
"rimraf": "^3.0.2"
}
}
16 changes: 8 additions & 8 deletions src/Web/HTML.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,42 @@ module Web.HTML
) where

import Effect (Effect)
import Web.HTML.Window (Window)
import Web.HTML.Common (PropName, AttrName, ClassName) as Exports
import Web.HTML.History (History) as Exports
import Web.HTML.Common (AttrName, ClassName, PropName) as Exports
import Web.HTML.HTMLAnchorElement (HTMLAnchorElement) as Exports
import Web.HTML.HTMLAreaElement (HTMLAreaElement) as Exports
import Web.HTML.HTMLAudioElement (HTMLAudioElement) as Exports
import Web.HTML.HTMLBRElement (HTMLBRElement) as Exports
import Web.HTML.HTMLBaseElement (HTMLBaseElement) as Exports
import Web.HTML.HTMLBodyElement (HTMLBodyElement) as Exports
import Web.HTML.HTMLBRElement (HTMLBRElement) as Exports
import Web.HTML.HTMLButtonElement (HTMLButtonElement) as Exports
import Web.HTML.HTMLCanvasElement (HTMLCanvasElement) as Exports
import Web.HTML.HTMLDListElement (HTMLDListElement) as Exports
import Web.HTML.HTMLDataElement (HTMLDataElement) as Exports
import Web.HTML.HTMLDataListElement (HTMLDataListElement) as Exports
import Web.HTML.HTMLDivElement (HTMLDivElement) as Exports
import Web.HTML.HTMLDListElement (HTMLDListElement) as Exports
import Web.HTML.HTMLDocument (HTMLDocument) as Exports
import Web.HTML.HTMLElement (HTMLElement) as Exports
import Web.HTML.HTMLEmbedElement (HTMLEmbedElement) as Exports
import Web.HTML.HTMLFieldSetElement (HTMLFieldSetElement) as Exports
import Web.HTML.HTMLFormElement (HTMLFormElement) as Exports
import Web.HTML.HTMLHRElement (HTMLHRElement) as Exports
import Web.HTML.HTMLHeadElement (HTMLHeadElement) as Exports
import Web.HTML.HTMLHeadingElement (HTMLHeadingElement) as Exports
import Web.HTML.HTMLHRElement (HTMLHRElement) as Exports
import Web.HTML.HTMLIFrameElement (HTMLIFrameElement) as Exports
import Web.HTML.HTMLImageElement (HTMLImageElement) as Exports
import Web.HTML.HTMLInputElement (HTMLInputElement) as Exports
import Web.HTML.HTMLKeygenElement (HTMLKeygenElement) as Exports
import Web.HTML.HTMLLIElement (HTMLLIElement) as Exports
import Web.HTML.HTMLLabelElement (HTMLLabelElement) as Exports
import Web.HTML.HTMLLegendElement (HTMLLegendElement) as Exports
import Web.HTML.HTMLLIElement (HTMLLIElement) as Exports
import Web.HTML.HTMLLinkElement (HTMLLinkElement) as Exports
import Web.HTML.HTMLMapElement (HTMLMapElement) as Exports
import Web.HTML.HTMLMediaElement (HTMLMediaElement) as Exports
import Web.HTML.HTMLMetaElement (HTMLMetaElement) as Exports
import Web.HTML.HTMLMeterElement (HTMLMeterElement) as Exports
import Web.HTML.HTMLModElement (HTMLModElement) as Exports
import Web.HTML.HTMLObjectElement (HTMLObjectElement) as Exports
import Web.HTML.HTMLOListElement (HTMLOListElement) as Exports
import Web.HTML.HTMLObjectElement (HTMLObjectElement) as Exports
import Web.HTML.HTMLOptGroupElement (HTMLOptGroupElement) as Exports
import Web.HTML.HTMLOptionElement (HTMLOptionElement) as Exports
import Web.HTML.HTMLOutputElement (HTMLOutputElement) as Exports
Expand Down Expand Up @@ -70,8 +68,10 @@ import Web.HTML.HTMLTitleElement (HTMLTitleElement) as Exports
import Web.HTML.HTMLTrackElement (HTMLTrackElement) as Exports
import Web.HTML.HTMLUListElement (HTMLUListElement) as Exports
import Web.HTML.HTMLVideoElement (HTMLVideoElement) as Exports
import Web.HTML.History (History) as Exports
import Web.HTML.Location (Location) as Exports
import Web.HTML.Navigator (Navigator) as Exports
import Web.HTML.Window (Window)
import Web.HTML.Window (Window) as Exports

foreign import window :: Effect Window
1 change: 1 addition & 0 deletions src/Web/HTML/Event/DataTransfer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ foreign import _setDragImage :: DataTransfer -> Element -> Int -> Int -> Effect
-- | The x and y coordinates define where the image appears relative to the mouse.
setDragImage :: DataTransfer -> Element -> Int -> Int -> Effect Unit
setDragImage = _setDragImage

foreign import _dropEffect :: DataTransfer -> Effect String

data DropEffect = Copy | Link | Move | None
Expand Down
2 changes: 1 addition & 1 deletion src/Web/HTML/HTMLAnchorElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Web.DOM (ChildNode, Element, Node, NonDocumentTypeChildNode, ParentNode)
import Web.DOM.DOMTokenList (DOMTokenList)
import Web.Event.EventTarget (EventTarget)
import Web.HTML.HTMLElement (HTMLElement)
import Web.Internal.FFI (unsafeReadProtoTagged)
import Web.HTML.HTMLHyperlinkElementUtils (HTMLHyperlinkElementUtils)
import Web.Internal.FFI (unsafeReadProtoTagged)

foreign import data HTMLAnchorElement :: Type

Expand Down
1 change: 0 additions & 1 deletion src/Web/HTML/HTMLBaseElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ toParentNode = unsafeCoerce
toEventTarget :: HTMLBaseElement -> EventTarget
toEventTarget = unsafeCoerce


foreign import href :: HTMLBaseElement -> Effect String
foreign import setHref :: String -> HTMLBaseElement -> Effect Unit

Expand Down
1 change: 0 additions & 1 deletion src/Web/HTML/HTMLCanvasElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ toParentNode = unsafeCoerce
toEventTarget :: HTMLCanvasElement -> EventTarget
toEventTarget = unsafeCoerce


-- typedef (CanvasRenderingContext2D or WebGLRenderingContext) RenderingContext;

foreign import width :: HTMLCanvasElement -> Effect Int
Expand Down
1 change: 0 additions & 1 deletion src/Web/HTML/HTMLDataElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ toParentNode = unsafeCoerce
toEventTarget :: HTMLDataElement -> EventTarget
toEventTarget = unsafeCoerce


foreign import value :: HTMLDataElement -> Effect String
foreign import setValue :: String -> HTMLDataElement -> Effect Unit
1 change: 0 additions & 1 deletion src/Web/HTML/HTMLDataListElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ toParentNode = unsafeCoerce
toEventTarget :: HTMLDataListElement -> EventTarget
toEventTarget = unsafeCoerce


foreign import options :: HTMLDataListElement -> Effect HTMLCollection
48 changes: 16 additions & 32 deletions src/Web/HTML/HTMLDocument.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
export function _documentElement(doc) {
return function () {
return doc.documentElement;
};
return doc.documentElement;
}

export function _head(doc) {
return function () {
return doc.head;
};
return doc.head;
}

export function _body(doc) {
return function () {
return doc.body;
};
return doc.body;
}

export function _readyState(doc) {
return function () {
return doc.readyState;
};
return doc.readyState;
}

export function _visibilityState(doc) {
return doc.readyState;
}

export function _activeElement(doc) {
return function () {
return doc.activeElement;
};
return doc.activeElement;
}

export function _currentScript(doc) {
return function () {
return doc.currentScript;
};
return doc.currentScript;
}

export function referrer(doc) {
return function () {
return doc.referrer;
};
export function _referrer(doc) {
return doc.referrer;
}

export function title(doc) {
return function () {
return doc.title;
};
export function _title(doc) {
return doc.title;
}

export function setTitle(title) {
return function (doc) {
return function () {
doc.title = title;
};
};
export function _setTitle(title, doc) {
doc.title = title;
}
49 changes: 34 additions & 15 deletions src/Web/HTML/HTMLDocument.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Web.HTML.HTMLDocument
, head
, body
, readyState
, visibilityState
, activeElement
, currentScript
, referrer
Expand All @@ -26,6 +27,7 @@ import Prelude
import Data.Maybe (Maybe, fromMaybe)
import Data.Nullable (Nullable, toMaybe)
import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
import Unsafe.Coerce (unsafeCoerce)
import Web.DOM.Document (Document)
import Web.DOM.Internal.Types (Node)
Expand All @@ -34,6 +36,8 @@ import Web.DOM.ParentNode (ParentNode)
import Web.Event.EventTarget (EventTarget)
import Web.HTML.HTMLDocument.ReadyState (ReadyState)
import Web.HTML.HTMLDocument.ReadyState as ReadyState
import Web.HTML.HTMLDocument.VisibilityState (VisibilityState)
import Web.HTML.HTMLDocument.VisibilityState as VisibilityState
import Web.HTML.HTMLElement (HTMLElement)
import Web.HTML.HTMLHtmlElement (HTMLHtmlElement)
import Web.HTML.HTMLScriptElement (HTMLScriptElement)
Expand Down Expand Up @@ -71,37 +75,52 @@ toNonElementParentNode = unsafeCoerce
toEventTarget :: HTMLDocument -> EventTarget
toEventTarget = unsafeCoerce

foreign import _documentElement :: HTMLDocument -> Effect (Nullable HTMLHtmlElement)
foreign import _documentElement :: EffectFn1 HTMLDocument (Nullable HTMLHtmlElement)

documentElement :: HTMLDocument -> Effect (Maybe HTMLHtmlElement)
documentElement = map toMaybe <<< _documentElement
documentElement doc = toMaybe <$> runEffectFn1 _documentElement doc

foreign import _head :: HTMLDocument -> Effect (Nullable HTMLElement)
foreign import _head :: EffectFn1 HTMLDocument (Nullable HTMLElement)

head :: HTMLDocument -> Effect (Maybe HTMLElement)
head = map toMaybe <<< _head
head doc = toMaybe <$> runEffectFn1 _head doc

foreign import _body :: HTMLDocument -> Effect (Nullable HTMLElement)
foreign import _body :: EffectFn1 HTMLDocument (Nullable HTMLElement)

body :: HTMLDocument -> Effect (Maybe HTMLElement)
body = map toMaybe <<< _body
body doc = toMaybe <$> runEffectFn1 _body doc

foreign import _readyState :: HTMLDocument -> Effect String
foreign import _readyState :: EffectFn1 HTMLDocument String

readyState :: HTMLDocument -> Effect ReadyState
readyState = map (fromMaybe ReadyState.Loading <<< ReadyState.parse) <<< _readyState
readyState doc = (fromMaybe ReadyState.Loading <<< ReadyState.parse) <$> (runEffectFn1 _readyState doc)

foreign import _activeElement :: HTMLDocument -> Effect (Nullable HTMLElement)
foreign import _visibilityState :: EffectFn1 HTMLDocument String

visibilityState :: HTMLDocument -> Effect VisibilityState
visibilityState doc = (fromMaybe VisibilityState.Visible <<< VisibilityState.parse) <$> (runEffectFn1 _visibilityState doc)

foreign import _activeElement :: EffectFn1 HTMLDocument (Nullable HTMLElement)

activeElement :: HTMLDocument -> Effect (Maybe HTMLElement)
activeElement = map toMaybe <<< _activeElement
activeElement doc = toMaybe <$> (runEffectFn1 _activeElement doc)

foreign import _currentScript :: HTMLDocument -> Effect (Nullable HTMLScriptElement)
foreign import _currentScript :: EffectFn1 HTMLDocument (Nullable HTMLScriptElement)

currentScript :: HTMLDocument -> Effect (Maybe HTMLScriptElement)
currentScript = map toMaybe <<< _currentScript
currentScript doc = toMaybe <$> (runEffectFn1 _currentScript doc)

foreign import _referrer :: EffectFn1 HTMLDocument String

referrer :: HTMLDocument -> Effect String
referrer doc = runEffectFn1 _referrer doc

foreign import _title :: EffectFn1 HTMLDocument String

title :: HTMLDocument -> Effect String
title doc = runEffectFn1 _title doc

foreign import referrer :: HTMLDocument -> Effect String
foreign import _setTitle :: EffectFn2 String HTMLDocument Unit

foreign import title :: HTMLDocument -> Effect String
foreign import setTitle :: String -> HTMLDocument -> Effect Unit
setTitle :: String -> HTMLDocument -> Effect Unit
setTitle newTitle doc = runEffectFn2 _setTitle newTitle doc
1 change: 1 addition & 0 deletions src/Web/HTML/HTMLDocument/ReadyState.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Web.HTML.HTMLDocument.ReadyState where

import Prelude

import Data.Maybe (Maybe(..))

data ReadyState
Expand Down
28 changes: 28 additions & 0 deletions src/Web/HTML/HTMLDocument/VisibilityState.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Web.HTML.HTMLDocument.VisibilityState where

import Prelude

import Data.Maybe (Maybe(..))

data VisibilityState
= Visible
| Hidden

derive instance eqVisibilityState :: Eq VisibilityState
derive instance ordVisibilityState :: Ord VisibilityState

instance showVisibilityState :: Show VisibilityState where
show = case _ of
Visible -> "Visible"
Hidden -> "Hidden"

print :: VisibilityState -> String
print = case _ of
Visible -> "visible"
Hidden -> "hidden"

parse :: String -> Maybe VisibilityState
parse = case _ of
"visible" -> Just Visible
"hidden" -> Just Hidden
_ -> Nothing
1 change: 0 additions & 1 deletion src/Web/HTML/HTMLEmbedElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ toParentNode = unsafeCoerce
toEventTarget :: HTMLEmbedElement -> EventTarget
toEventTarget = unsafeCoerce


foreign import src :: HTMLEmbedElement -> Effect String
foreign import setSrc :: String -> HTMLEmbedElement -> Effect Unit

Expand Down
2 changes: 1 addition & 1 deletion src/Web/HTML/HTMLImageElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ module Web.HTML.HTMLImageElement
, complete
) where

import Data.Maybe (Maybe, fromMaybe)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Data.Maybe (Maybe, fromMaybe)
import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
import Prelude (Unit, map, (<<<), (<=<))
Expand Down
2 changes: 1 addition & 1 deletion src/Web/HTML/HTMLImageElement/CORSMode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Prelude (class Eq, class Ord, class Show)

data CORSMode
= Anonymous
| UseCredentials
| UseCredentials

derive instance eqCORSMode :: Eq CORSMode
derive instance ordCORSMode :: Ord CORSMode
Expand Down
2 changes: 1 addition & 1 deletion src/Web/HTML/HTMLImageElement/DecodingHint.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Prelude (class Eq, class Ord, class Show)

data DecodingHint
= Sync
| Async
| Async
| Auto

derive instance eqDecodingHint :: Eq DecodingHint
Expand Down
Loading