Skip to content

Syntax Changes #503

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
Jan 25, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
79 changes: 36 additions & 43 deletions core/source/Dom.mint
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Dom {

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

Expand Down Expand Up @@ -43,7 +43,7 @@ module Dom {

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

Expand All @@ -58,7 +58,7 @@ module Dom {
base : Dom.Element
) : Bool {
maybeElement
|> Maybe.map(Dom.contains(base))
|> Maybe.map((item : Dom.Element) { Dom.contains(item, base) })
|> 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,14 +151,12 @@ 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("id", element) == "my-div"
Maybe::Nothing => false
}
*/
fun getAttribute (name : String, element : Dom.Element) : Maybe(String) {
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 @@ -406,20 +401,18 @@ module Dom {
|> 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 Down Expand Up @@ -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 Down Expand Up @@ -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
}
}
}
100 changes: 42 additions & 58 deletions core/source/File.mint
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@ module File {
/*
Prompts a dialog for the saving the given file.

sequence {
file =
File.select(*)
file = await File.select(*)

File.download(file)
}
File.download(file)
*/
fun download (file : File) : Promise(Never, Void) {
sequence {
url =
Url.createObjectUrlFromFile(file)

`
(() => {
const anchor = document.createElement('a')
anchor.download = #{file}.name
anchor.href = #{url}
anchor.click()
})()
`

Url.revokeObjectUrl(url)
}
fun download (file : File) : Void {
let url =
Url.createObjectUrlFromFile(file)

`
(() => {
const anchor = document.createElement('a')
anchor.download = #{file}.name
anchor.href = #{url}
anchor.click()
})()
`

Url.revokeObjectUrl(url)
}

/*
Expand Down Expand Up @@ -60,14 +55,12 @@ module File {
/*
Reads the contents of the given file as a String.

sequence {
file =
File.create("Some content...", "test.txt", "text/plain")
file =
File.create("Some content...", "test.txt", "text/plain")

File.readAsArrayBuffer(file)
}
File.readAsArrayBuffer(file)
*/
fun readAsArrayBuffer (file : File) : Promise(Never, ArrayBuffer) {
fun readAsArrayBuffer (file : File) : Promise(ArrayBuffer) {
`
(() => {
const reader = new FileReader()
Expand All @@ -83,17 +76,15 @@ module File {
/*
Reads the contents of the given file as a Data URL.

sequence {
file =
File.fromString("Some content...", "test.txt", "text/plain")
files =
await File.select("text/plain")

url =
File.readAsDataURL(file)
url =
File.readAsDataURL(file)

url == "data:text/plain;...."
}
url == "data:text/plain;...."
*/
fun readAsDataURL (file : File) : Promise(Never, String) {
fun readAsDataURL (file : File) : Promise(String) {
`
(() => {
const reader = new FileReader()
Expand All @@ -109,17 +100,15 @@ module File {
/*
Reads the contents of the given file as a String.

sequence {
file =
File.create("Some content...", "test.txt", "text/plain")
file =
File.create("Some content...", "test.txt", "text/plain")

url =
File.readAsString(file)
url =
await File.readAsString(file)

url == "Some content..."
}
url == "Some content..."
*/
fun readAsString (file : File) : Promise(Never, String) {
fun readAsString (file : File) : Promise(String) {
`
(() => {
const reader = new FileReader()
Expand All @@ -138,14 +127,12 @@ module File {
* The mime type can be restricted to the given one.
* It might not resolve if the user cancels the dialog.

sequence {
file =
File.select("application/json")
file =
await File.select("application/json")

Debug.log(file)
}
Debug.log(file)
*/
fun select (accept : String) : Promise(Never, File) {
fun select (accept : String) : Promise(File) {
`
(() => {
const input = document.createElement('input')
Expand All @@ -161,7 +148,7 @@ module File {

document.body.appendChild(input)

return new Promise((resolve, reject) => {
return new Promise((resolve) => {
input.addEventListener('change', () => {
resolve(input.files[0])
})
Expand All @@ -178,14 +165,12 @@ module File {
* The mime type can be restricted to the given one.
* It might not resolve if the user cancels the dialog.

sequence {
files =
File.selectMultiple("application/json")
files =
await File.selectMultiple("application/json")

Debug.log(files)
}
Debug.log(files)
*/
fun selectMultiple (accept : String) : Promise(Never, Array(File)) {
fun selectMultiple (accept : String) : Promise(Array(File)) {
`
(() => {
const input = document.createElement('input')
Expand All @@ -200,12 +185,11 @@ module File {
input.multiple = true
input.type = 'file'

document.body.appendChild(input)
Copy link
Member

Choose a reason for hiding this comment

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

@gdotdesign Was that removed intentionally?
I'm asking since there's still document.body.removeChild(input) left beneath 🤔


return new Promise((resolve, reject) => {
input.addEventListener('change', () => {
resolve(Array.from(input.files))
})

input.click()
document.body.removeChild(input)
})
Expand Down
Loading