Skip to content

Commit

Permalink
fix input problem for popups + improve js test server
Browse files Browse the repository at this point in the history
  • Loading branch information
Welpike committed Jul 4, 2024
1 parent 6de84fd commit b9fcfa4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
30 changes: 6 additions & 24 deletions src/utils/popups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,25 @@ export class Popup {

/**
* Nota (for prompts):
* - the input only handles characters, "Space" and "Backspace" yet. So the user can't use controls to copy/paste
* strings, etc...
* - if the input value length doesn't belong to the defined interval ([minLength; maxLength]), the button will be
* disabled (by default, the interval is [0;255]).
*/
async getResponse(): Promise<any> {
this.popupElement.style.display = "flex"

if(this.data.type === "show") {
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
if(this.data.type === "show") {
document.querySelector('#cazan-popup-confirm')?.addEventListener(
'click',
() => {
resolve(true)
}
)
})
} else if(this.data.type === "prompt") {
return new Promise((resolve, reject) => {
} else if(this.data.type === "prompt") {
let inputElement: HTMLInputElement = document.querySelector("#cazan-popup-input")!
let confirmBtnElement: HTMLButtonElement = document.querySelector("#cazan-popup-confirm")!

inputElement.addEventListener('keydown', (event: KeyboardEvent) => {
event.preventDefault()

if(event.key === "Backspace") {
inputElement.value = inputElement.value.slice(0, -1)
} else if(event.key === "Space") {
inputElement.value += " "
} else if(event.key.length === 1) {
inputElement.value += event.key
}

if(
inputElement.value.length >= this.data.minLength!
&& inputElement.value.length <= this.data.maxLength!
Expand All @@ -108,9 +94,7 @@ export class Popup {
}
}
)
})
} else if(this.data.type === "confirm") {
return new Promise((resolve, reject) => {
} else if(this.data.type === "confirm") {
document.querySelector("#cazan-popup-yes")?.addEventListener(
"click",
() => {
Expand All @@ -123,10 +107,8 @@ export class Popup {
resolve(false)
}
)
})
}

return new Promise((resolve, reject) => reject("Type undefined"))
}
})
}

removePopup() {
Expand Down
16 changes: 10 additions & 6 deletions tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ http.createServer(function (req, res) {
resContentType = 'text/plain'
}

if(PATHS[url]) {
resContent = fs.readFileSync(PATHS[url])
} else {
try {
try {
if(PATHS[url]) {
resContent = fs.readFileSync(PATHS[url])
} else {
resContent = fs.readFileSync(`.${url}`)
} catch (e) {} // file not found
}
}
} catch (e) {} // file not found

if (resContent === '') {
res.writeHead(404, {'Content-Type': 'text/plain'})
res.write('404 Not Found')

if(url === "/cazan.js" || url === "/cazan.min.js") {
console.error("Error: you didn't build Cazan or you didn't build it in /dist/lib. Please execute `yarn run build` or `yarn run release` and relaunch this server.")
}
} else {
res.writeHead(200, {'Content-Type': resContentType})
res.write(resContent)
Expand Down

0 comments on commit b9fcfa4

Please sign in to comment.