Skip to content

Instantly share code, notes, and snippets.

View thanapongp's full-sized avatar

Thanapong Prathumchat thanapongp

View GitHub Profile
@thanapongp
thanapongp / isRangesOverlap.ts
Created April 4, 2024 04:42
Quick and easy dates ranges overlap check
export function isRangesOverlap(
firstRange: [Date, Date],
secondRange: [Date, Date]
): boolean {
const firstRangeStart = firstRange[0].getTime();
const secondRangeStart = secondRange[0].getTime();
const firstRangeEnd = firstRange[1].getTime();
const secondRangeEnd = secondRange[1].getTime();
@thanapongp
thanapongp / auto-facetime.scpt
Created February 26, 2021 14:38
A simple AppleScript that automatically answers any FaceTime call. Useful to make your Mac as a make-shift IP Camera
tell application "System Events"
repeat
repeat until (exists window 1 of application process "Notification Center")
delay 1
log "Wait for notification center"
end repeat
if exists button "Accept" of window 1 of application process "Notification Center" then
log "Answering"
click button "Accept" of window 1 of application process "Notification Center"
delay 3
@thanapongp
thanapongp / app.snippet.5.js
Created October 23, 2019 08:16
performEdit method
performEdit(item, editInput) {
const itemNameEl = item.getElementsByClassName('item-name')[0];
const editButtonEl = item.getElementsByClassName('edit-button')[0];
if (editInput.value.trim().length !== 0) {
itemNameEl.innerHTML = editInput.value.trim();
}
editInput.remove();
@thanapongp
thanapongp / app.snippet.4.js
Created October 23, 2019 06:57
Fixed editItem method
addNewItem(itemName) {
//...
item.getElementsByClassName('edit-button')[0].addEventListener('click', () => this.editItem(item));
}
editItem(item) {
const oldItemName = item.getElementsByClassName('item-name')[0].innerHTML.trim();
const editInput = htmlToElement(`
@thanapongp
thanapongp / app.snippet.3.js
Last active October 23, 2019 05:21
performEdit method
editItem(item, oldItemName) {
const editInput = htmlToElement(`
<input type="text" value="${oldItemName}" placeholder="Enter new item name" class="bg-gray-800 px-4 py-2 rounded block w-full text-gray-200 font-light">
`);
editInput.addEventListener('keyup', e => {
if (e.keyCode === 13) {
this.performEdit(item, editInput)
}
});
@thanapongp
thanapongp / app.snippet.2.js
Created October 23, 2019 05:10
editItem method
editItem(item, oldItemName) {
const editInput = htmlToElement(`
<input type="text" value="${oldItemName}" placeholder="Enter new item name" class="bg-gray-800 px-4 py-2 rounded block w-full text-gray-200 font-light">
`);
const itemNameEl = item.getElementsByClassName('item-name')[0];
const editButtonEl = item.getElementsByClassName('edit-button')[0];
insertElementAfter(editInput, itemNameEl);
@thanapongp
thanapongp / utils.js
Created October 23, 2019 04:53
intsertAfter function
export function insertElementAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
@thanapongp
thanapongp / app.snippet.1.js
Last active October 23, 2019 04:56
app.js with new editItem method
addNewItem(itemName) {
//...
const item = htmlToElement(`
...
<button class="edit-button">
<svg class="stroke-current text-gray-600 h-4" viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path>
</svg>
@thanapongp
thanapongp / utils.js
Created September 8, 2019 09:49
util.js with updated htmlToElement
export function htmlToElement(html) {
const template = document.createElement('template');
template.innerHTML = html.trim();
return template.content.firstElementChild;
}
@thanapongp
thanapongp / app.js
Created September 8, 2019 08:32
app.js with evaluateEmptyState
import { htmlToElement } from './utils';
export default class App {
//...
addNewItem(itemName) {
//...
const item = htmlToElement(`
<li class="flex py-4 border-b border-gray-900 item"> <!-- add class 'item' -->