Skip to content
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

Pipe as the first agument instead of the last. #571

Merged
merged 9 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
229 changes: 117 additions & 112 deletions core/source/Array.mint

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/source/Clipboard.mint
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* This module has functions for manipulating the clipboard. */
module Clipboard {
/* Sets the clipboards content to the given value. */
fun set (value : String) : Promise(Never, String) {
fun set (value : String) : Promise(String) {
`
(() => {
// Create a textarea element
Expand Down
123 changes: 58 additions & 65 deletions core/source/Dom.mint
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ module Dom {

Dom.blurActiveElement()
*/
fun blurActiveElement : Promise(Never, Void) {
fun blurActiveElement : Promise(Void) {
`document.activeElement && document.activeElement.blur()`
}

/*
Returns if the given element is in an element that matches the given
selector.

Dom.containedInSelector("body", Dom.getElementBySelector("div"))
Dom.containedInSelector(Dom.getElementBySelector("div"), "body")
*/
fun containedInSelector (selector : String, element : Dom.Element) : Bool {
fun containedInSelector (element : Dom.Element, selector : String) : Bool {
`
(() => {
for (let base of document.querySelectorAll(selector)) {
Expand All @@ -32,9 +32,9 @@ module Dom {
/*
Returns if the given base element contains the given element.

Dom.contains(div, body) == true
Dom.contains(body, div) == true
*/
fun contains (element : Dom.Element, base : Dom.Element) : Bool {
fun contains (base : Dom.Element, element : Dom.Element) : Bool {
`#{base}.contains(#{element})`
}

Expand All @@ -43,7 +43,7 @@ module Dom {

case (Dom.getElementBySelector("body")) {
Maybe::Just(body) =>
try {
{
div =
Dom.getElementBySelector("div")

Expand All @@ -54,11 +54,11 @@ module Dom {
}
*/
fun containsMaybe (
maybeElement : Maybe(Dom.Element),
base : Dom.Element
base : Dom.Element,
maybeElement : Maybe(Dom.Element)
) : Bool {
maybeElement
|> Maybe.map(Dom.contains(base))
|> Maybe.map((item : Dom.Element) { Dom.contains(base, item) })
|> Maybe.withDefault(false)
}

Expand All @@ -79,23 +79,20 @@ module Dom {
|> Dom.focus
|> Dom.getElementById()
*/
fun focus (maybeElement : Maybe(Dom.Element)) : Promise(Never, Void) {
fun focus (maybeElement : Maybe(Dom.Element)) : Promise(Void) {
case (maybeElement) {
Maybe::Just(element) =>
sequence {
{
focusWhenVisible(element)

Promise.never()
} catch {
Promise.never()
Promise.resolve(void)
}

Maybe::Nothing => Promise.never()
Maybe::Nothing => Promise.resolve(void)
}
}

/* Focuses the first focusable descendant of the given element. */
fun focusFirst (element : Dom.Element) : Promise(Never, Void) {
fun focusFirst (element : Dom.Element) : Promise(Void) {
element
|> getFocusableElements
|> Array.first
Expand All @@ -109,14 +106,14 @@ module Dom {
|> Dom.getElementById
|> Dom.focusWhenVisible()
*/
fun focusWhenVisible (element : Dom.Element) : Promise(String, Void) {
fun focusWhenVisible (element : Dom.Element) : Promise(Result(String, Void)) {
`
new Promise((resolve, reject) => {
new Promise((resolve) => {
let counter = 0

let focus = () => {
if (counter > 15) {
reject('Could not focus the element in 150ms. Is it visible?')
resolve(#{Result::Err("Could not focus the element in 150ms. Is it visible?")})
}

#{element}.focus()
Expand All @@ -125,7 +122,7 @@ module Dom {
counter++
setTimeout(focus, 10)
} else {
resolve(#{void})
resolve(#{Result::Ok(void)})
}
}

Expand Down Expand Up @@ -154,17 +151,15 @@ module Dom {
/*
If the attribute is present, it will return its value on the given element.

try {
outcome =
Dom.getElementById("my-div")
outcome =
Dom.getElementById("my-div")

case (outcome) {
Maybe::Just(element) => Dom.getAttribute("id", element) == "my-div"
Maybe::Nothing => false
}
case (outcome) {
Maybe::Just(element) => Dom.getAttribute(element, "id") == "my-div"
Maybe::Nothing => false
}
*/
fun getAttribute (name : String, element : Dom.Element) : Maybe(String) {
fun getAttribute (element : Dom.Element, name : String) : Maybe(String) {
`
(() => {
const value = #{element}.getAttribute(#{name})
Expand Down Expand Up @@ -225,14 +220,14 @@ module Dom {
const rect = #{dom}.getBoundingClientRect()

return #{{
bottom = `rect.bottom`,
height = `rect.height`,
width = `rect.width`,
right = `rect.right`,
left = `rect.left`,
top = `rect.top`,
x = `rect.x`,
y = `rect.y`
bottom: `rect.bottom`,
height: `rect.height`,
width: `rect.width`,
right: `rect.right`,
left: `rect.left`,
top: `rect.top`,
x: `rect.x`,
y: `rect.y`
}}
})()
`
Expand Down Expand Up @@ -291,9 +286,9 @@ module Dom {
const element = document.elementFromPoint(#{left}, #{top})

if (element) {
return new Just(element)
return #{Maybe::Just(`element`)}
} else {
return new Nothing()
return #{Maybe::Nothing}
}
})()
`
Expand All @@ -303,9 +298,9 @@ module Dom {
Gets all descendant elements of an element which are matching
the given selector.

Dom.getElementsBySelector("a[name]", element)
Dom.getElementsBySelector(element, "a[name]")
*/
fun getElementsBySelector (selector : String, element : Dom.Element) : Array(Dom.Element) {
fun getElementsBySelector (element : Dom.Element, selector : String) : Array(Dom.Element) {
`Array.from(#{element}.querySelectorAll(#{selector}))`
}

Expand Down Expand Up @@ -395,31 +390,29 @@ module Dom {
/*
Returns the table of contents of the given element for the given selectors.

Dom.getTableOfContents("h1, h2, h3, h4", element) == [
Dom.getTableOfContents(element, "h1, h2, h3, h4") == [
{"h1", "The title of the page", "the-title-of-the-page"},
{"h2", "A subtitle of the page", "a-subtitle-of-the-page"},
{"h3", "A sub-subtitle of the page", "a-sub-subtitle-of-the-page"}
]
*/
fun getTableOfContents (selector : String, element : Dom.Element) : Array(Tuple(String, String, String)) {
fun getTableOfContents (element : Dom.Element, selector : String) : Array(Tuple(String, String, String)) {
element
|> getElementsBySelector(selector)
|> Array.map(
(item : Dom.Element) : Tuple(String, String, String) {
try {
tag =
item
|> getTagName()
|> String.toLowerCase()
let tag =
item
|> getTagName()
|> String.toLowerCase()

text =
getTextContent(item)
let text =
getTextContent(item)

hash =
String.parameterize(text)
let hash =
String.parameterize(text)

{tag, text, hash}
}
{tag, text, hash}
})
}

Expand All @@ -446,9 +439,9 @@ module Dom {
/*
Measures the given text width with the given font using the canvas.

Dom.getTextWidth("20px sans-serif", "Hello There") = 300
Dom.getTextWidth("Hello There", "20px sans-serif") = 300
*/
fun getTextWidth (font : String, text : String) : Number {
fun getTextWidth (text : String, font : String) : Number {
`
(() => {
const canvas = document.createElement('canvas');
Expand Down Expand Up @@ -487,10 +480,10 @@ module Dom {
/*
Returns whether or not the given `Dom.Element` matches the given selector.

Dom.matches("div", Dom.createElement("div")) == true
Dom.matches("p", Dom.createElement("div")) == false
Dom.matches(Dom.createElement("div"), "div") == true
Dom.matches(Dom.createElement("div"), "p") == false
*/
fun matches (selector : String, dom : Dom.Element) : Bool {
fun matches (dom : Dom.Element, selector : String) : Bool {
`
(() => {
try {
Expand All @@ -507,7 +500,7 @@ module Dom {

Dom.scrollTo(element, 10, 10)
*/
fun scrollTo (element : Dom.Element, left : Number, top : Number) : Promise(Never, Void) {
fun scrollTo (element : Dom.Element, left : Number, top : Number) : Promise(Void) {
`#{element}.scrollTo({
left: #{left},
top: #{top}
Expand All @@ -523,9 +516,9 @@ module Dom {
|> Dom.setAttribute("name", "test")
*/
fun setAttribute (
element : Dom.Element,
attribute : String,
value : String,
element : Dom.Element
value : String
) : Dom.Element {
`#{element}.setAttribute(#{attribute}, #{value}) && element`
}
Expand All @@ -538,7 +531,7 @@ module Dom {
|> Dom.setStyle("background", "red")
|> Dom.setStyle("color", "white")
*/
fun setStyle (name : String, value : String, element : Dom.Element) : Dom.Element {
fun setStyle (element : Dom.Element, name : String, value : String) : Dom.Element {
`
(() => {
#{element}.style[#{name}] = #{value}
Expand All @@ -552,7 +545,7 @@ module Dom {

It is used to set the value of `input` fields programmatically.
*/
fun setValue (value : String, dom : Dom.Element) : Dom.Element {
fun setValue (dom : Dom.Element, value : String) : Dom.Element {
`(#{dom}.value = #{value}) && #{dom}`
}

Expand All @@ -561,7 +554,7 @@ module Dom {

Dom.smoothScrollTo(element, 10, 10)
*/
fun smoothScrollTo (element : Dom.Element, left : Number, top : Number) : Promise(Never, Void) {
fun smoothScrollTo (element : Dom.Element, left : Number, top : Number) : Promise(Void) {
`#{element}.scrollTo({
behavior: 'smooth',
left: #{left},
Expand Down
16 changes: 8 additions & 8 deletions core/source/Dom/Dimensions.mint
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ module Dom.Dimensions {
/* Returns an empty Dom.Dimensions record. */
fun empty : Dom.Dimensions {
{
bottom = 0,
height = 0,
width = 0,
right = 0,
left = 0,
top = 0,
x = 0,
y = 0
bottom: 0,
height: 0,
width: 0,
right: 0,
left: 0,
top: 0,
x: 0,
y: 0
}
}
}
Loading