Skip to content

Instantly share code, notes, and snippets.

View andreasvirkus's full-sized avatar
🙊
made you look

andreas andreasvirkus

🙊
made you look
View GitHub Profile
@andreasvirkus
andreasvirkus / serializeWithStyles.js
Created July 19, 2018 07:14
Get the whole DOM of an element with its styles (defaults excluded) inlined.
/**
* Usage:
*
* document.getElementById('app').serializeWithStyles()
*
* Credits go to https://stackoverflow.com/a/6310120/2803743
*/
Element.prototype.serializeWithStyles = (function () {
// Mapping between tag names and css default values lookup tables. This allows to exclude default values in the result.
function resizeImage(el, width, height, quality) {
var canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
var context = canvas.getContext('2d')
context.drawImage(el, 0, 0, width, height)
try {
return canvas.toDataURL('image/jpeg', quality)
} catch (e) {
@andreasvirkus
andreasvirkus / Modal.vue
Last active September 10, 2022 22:03
Simple Vue modal component
<template>
<transition-fade :duration="200">
<div
v-if="show"
:class="$style.backdrop"
@click="$emit('close')"
@keydown.stop
@keyup.esc="handleEscape"
>
<dialog v-show="show" ref="dialog" :open="show" :class="$style.modal" tabindex="-1">
const roundingStops = [5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000]
export const roundToScale = (count, scale = roundingStops) => {
if (count === 0) return 0
const nearestIdx = scale.findIndex((s) => s > count)
let nearest = scale[nearestIdx]
if (nearestIdx !== 0 && nearest / 2 > count) nearest = scale[nearestIdx - 1] || scale[0]
return nearest || count
const prettier = require("prettier");
const pluginName = "HandleChunkLoadFailurePlugin";
/**
* This Webpack plugin calls `window._chunkHandler.handleVersionChange()` when loading a chunk fails
*/
class HandleChunkLoadFailurePlugin {
constructor(options = {}) {
this.options = Object.assign({}, options);
export const strip = (html: string) => {
const doc = new DOMParser().parseFromString(html, 'text/html')
return (doc.body.textContent || '').trim()
}
@andreasvirkus
andreasvirkus / Editor.vue
Last active January 20, 2022 15:58
Tiptap emoji plugin for Klausapp
<template>
<div ref="editor" class="notranslate">
<div v-show="showSuggestions" ref="suggestions">
<template v-if="(filteredSuggestions || []).length">
<div
v-for="(item, index) in filteredSuggestions.slice(0, 10)"
:key="index"
:class="[$style.suggestion, navigatedSuggestionIndex === index && $style.selected]"
@click="selectSuggestion(item)"
>
@andreasvirkus
andreasvirkus / debounce.js
Created June 12, 2021 17:12
A modern twist to an oldie, but goldie.
export function debounce(func, timeout = 300){
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}
@andreasvirkus
andreasvirkus / jump.js
Last active April 19, 2021 14:33
A vanilla scrollTop implementation (includes throttle and scrollTo utility functions)
/**
* Vanilla scrollTo implementation (smoothly scroll/"jump" to an element)
* @author github.com/andreasvirkus
*
* Default easing is:
* Robert Penner's easeInOutQuad - http://robertpenner.com/easing/
*
* @param {String} target selector to scroll to
* @param {Object} options Optionally defined duration, offset, callback and easing
*
@andreasvirkus
andreasvirkus / style.css
Last active March 25, 2021 10:04
Vanilla JS Toast component
.toaster {
display: block;
position: fixed;
left: 50%;
transform: translateX(-50%);
bottom: 16px;
width: 100%;
height: 0;
z-index: 11;
}