Skip to content

Instantly share code, notes, and snippets.

View AnalyzePlatypus's full-sized avatar
🐏
Bah.

Michoel Samuels AnalyzePlatypus

🐏
Bah.
View GitHub Profile
@AnalyzePlatypus
AnalyzePlatypus / vue-click-outside.md
Last active December 1, 2023 05:45
Vue.js 2.7: Detect clicks outside an element (Close modals, popups, etc.)

Detecting outside clicks in Vue.js

See this StackOverflow thread

First off, include the directive at the end of this gist.

  1. On your open button, make sure to use @click.stop to prevent the open click event from closing your modal.
  2. On your modal, add the v-click-outside directive and points it at a function to call when clicked outside.

Example:

@AnalyzePlatypus
AnalyzePlatypus / getImageKitModifiers.js
Created September 5, 2019 10:18
Generate image editing modifiers for ImageKit.io
/*
Usage:
import getImageKitModifierQueryString from "getImageKitModifiers.js";
const IMAGEKIT_CDN_ROOT = "https://ik.imagekit.io/my-app/";
const myImageKey = "image.jpg";
const modifierQueryString = getImageKitModifierQueryString({
width: 45,
@AnalyzePlatypus
AnalyzePlatypus / jsPDF_line_wrap.md
Last active January 24, 2024 13:38
Helper function for line-wrapping in jsPDF
@AnalyzePlatypus
AnalyzePlatypus / jsPDF_convert_points_to_other_units.js
Last active May 15, 2022 06:47
jsPDF helper function: Convert points to other units
function convertPointsToUnit(points, unit) {
// Unit table from https://github.com/MrRio/jsPDF/blob/ddbfc0f0250ca908f8061a72fa057116b7613e78/jspdf.js#L791
var multiplier;
switch(unit) {
case 'pt': multiplier = 1; break;
case 'mm': multiplier = 72 / 25.4; break;
case 'cm': multiplier = 72 / 2.54; break;
case 'in': multiplier = 72; break;
case 'px': multiplier = 96 / 72; break;
case 'pc': multiplier = 12; break;

Stripe Checkout: Multiple Checkout buttons on the same page

Suppose you have a Stripe Checkout page setup to sell a product.

To link to your payment page, you must generate a Checkout button HTML snippet for display on your site. However, the default snippet only supports one Checkout button. You cannot have several Checkout buttons for the same product on the same page.

This gist modifies Stripe's generated snippet to support several buttons.

Vue.js: Use Sentry.io without increasing initial bundle size

There's no questions about it: in the age of "fat" frontend clients, less bloat is better. A useful technique is to split off large libraries into separate Webpack chunks so the they don't take up space in your main bundle, delaying the initial load of your webapp. I like doing this with what I call the "installer pattern". Every split-out library gets its own "installer", an async function that loads the Webpack chunk, performs any initializations or configuration necessary, and then informs the relevant parts of your app that loading has completed.

Here's what that pattern looks like for installing the Sentry.io error reporting SDK:

First, add Sentry to your project:

@AnalyzePlatypus
AnalyzePlatypus / lambda-gmail-compose.md
Created May 13, 2020 08:11
Use serverless function to send low-volume emails without 3rd party mail services.

Sending email with serverless functions

You can deploy this function on any of the serverless platforms - AWS Lambda, Google Cloud Functions, Azure Functions, Netlify, Cloudflare Workers, etc.

  1. Create a new function and paste in the following code.
  2. You will need to add nodemailer to your package.json (npm i nodemailer), and follow your platform's instructions on bundling dependencies.
  3. Obtain Gmail API credentials for your account. You will need clientID, clientSecret, and refreshToken. Follow this YouTube tutorial
  4. Expose these credentials as the follwing environment variables:
GMAIL_EMAIL_ADDRESS
@AnalyzePlatypus
AnalyzePlatypus / using-sheets-api.md
Last active February 1, 2024 14:29
Node.js - Accessing a spreadsheet using the Google Sheets API

Using the Sheets API

To access the Sheets API, you will need to enable the Sheets API, obtain credentials, and grant access to your Sheet.

Based on these instructions

1. Enabling the Sheets API

  1. Open the Google Developers Console
  2. Select your project or create a new one (and then select it)

AWS Lambda: Upload code from Command Line

Last update: June 2020

AWS is notorious for it's incredibly complicated security model. This guide will walk you through setting up a fully-featured build & upload script for your Lambda functions.

You will need:

  • An AWS account

AWS Lambda SAM cli: Using local env vars

To use local env vars, you must follow the following steps:

  1. Declare all env vars in your template.yml, like so:
Resources:
  MyFunction:
 Type: AWS::Serverless::Function