Skip to content

Instantly share code, notes, and snippets.

View loilo's full-sized avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / contenteditable-caret-position.js
Last active December 17, 2024 07:33
Some utilities for detecting the caret position inside a contenteditable element
/**
* Get the number of characters in an element
*
* @param {Element} element
* @return {number}
*/
function getTextLength(element) {
let range = element.ownerDocument.createRange()
range.selectNodeContents(element)
@loilo
loilo / parse-filesize.php
Created February 13, 2021 10:01
PHP Filesize Parser
<?php
/**
* Parse a filesize to its number of bytes
*
* @param string|int|float $input
* @param int $base
* @return int
*
* @see https://github.com/patrickkettner/filesize-parser, the original JavaScript implementation of this function
@loilo
loilo / bitmask-to-array.mjs
Created November 15, 2020 14:09
Create Array From Bitmask
/**
* Convert a bitmask to an array of its individual bits
*
* @param {number} bitmask The bitmask to dissect
* @return {number[]}
*
* @example
* assert.equal(bitmaskToArray(0b101), [0b100, 0b001])
*/
export function bitmaskToArray(bitmask) {
@loilo
loilo / use-terminal-size.ts
Last active September 25, 2020 10:15
useTerminalSize() React Hook
import { useEffect, useState } from 'react'
/**
* React hook which provides the size of the terminal, based on https://usehooks.com/useWindowSize/
* Great for usage with Ink (https://npmjs.com/package/ink)
*/
export function useTerminalSize() {
const [terminalSize, setTerminalSize] = useState([
process.stdout.columns,
process.stdout.rows
@loilo
loilo / readme.md
Last active December 10, 2024 17:42
Async Computed Values for Vue 3

Async Computed Values for Vue 3

This gist provides a useAsyncComputed function which allows to create asynchronous dependencies on reactive values (using Vue 3's Composition API).

Requires at least Vue 3.0 and TypeScript 4.0.

Usage

Basic Usage

@loilo
loilo / readme.md
Last active May 9, 2025 22:23
Sass Dark/Light Theme Mixin

Sass Dark/Light Theme Mixin

This is a Sass mixin to handle a 3-way dark mode. It relies on a data-theme attribute on your <html> element with a value of light or dark. If data-theme is absent (i.e. it's neither light nor dark), the system's preferred mode is used.

body {
  // matches data-theme="light" or data-theme="auto" with system instructing light mode
  @include light {
    background: white;
 color: black;
@loilo
loilo / redirect-to-english.js
Last active October 12, 2023 21:23
Userscript: Redirect Website to its English Version
// This userscript redirects you to the English version of a website if it's denoted in the source code.
// Insert any URLs of websites below (after @match), for example https://developer.mozilla.org/* or https://www.php.net/*
// Use multiple @match clauses to enable the script on several domains.
// ==UserScript==
// @name Redirect to English
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Redirect websites to their English version
// @author Florian Reuschel <[email protected]>
@loilo
loilo / manipulate_html.php
Last active November 28, 2024 08:01
Modify HTML Using PHP
<?php
function walk_dom(DOMNode $domNode, callable $callback): void
{
foreach ($domNode->childNodes as $node) {
$callback($node);
if ($node->hasChildNodes()) {
walk_dom($node, $callback);
}
@loilo
loilo / buffer.php
Created April 11, 2020 20:49
Output Buffering Directive for Laravel Blade Templates
<?php
// In your AppServiceProvider's boot() method, put this:
Blade::directive('buffer', function () {
return '<?php ob_start(); ?>';
});
Blade::directive('endbuffer', function (string $name) {
if ($name === '') {
return '<?php ob_end_clean(); ?>';