Skip to content

Instantly share code, notes, and snippets.

View mattmccray's full-sized avatar

Matt McCray mattmccray

View GitHub Profile
@mattmccray
mattmccray / StateManager.ts
Last active May 30, 2021 03:49
Simple state manager using react and immer.
import React from 'react'
import { Patch, produceWithPatches } from 'immer'
export interface StateManager<T = {}> {
handle<P>(signal: Signal<P>, callback: (state: T, event: P) => void): () => void
getState(): T
onStateChanged: Signal<{ state: T, changes: Patch[] }>
}
@mattmccray
mattmccray / mutt.js
Last active January 15, 2021 19:45
MUTT - Micro Unit Testing Tool (for browsers)
// MUTT - Micro Unit Testing Tool (for browsers)
let _testDepth = 0;
export function test(name, suite) {
const title = `🐶 ${name}`;
const ctx = {
count: 0,
failed: 0
};
@mattmccray
mattmccray / ObservableUndoController.ts
Last active September 6, 2020 17:04
Simple Observable Undo Controller
import { observable, computed, runInAction } from 'mobx'
import uid from './uid'
type UndoFn = () => void
type Command = () => UndoFn | Promise<UndoFn>
interface UndoContext {
id: string
label: string
command: Command
@mattmccray
mattmccray / UndoManager.ts
Last active September 6, 2020 14:35
Super Simple Undo Manager
type UndoFn = () => void
type Command = () => UndoFn | Promise<UndoFn>
interface UndoContext {
command: Command
revert: UndoFn | Promise<UndoFn>
}
export class UndoManager {
undoStack: UndoContext[] = []
/*
Scrippets:
Blocks of text formatted as a screenplay
Original Ruby script by John August:
site: http://johnaugust.com
script: http://pastie.org/257717
css: http://pastie.org/257678
JavaScript version by M@ McCray:
@mattmccray
mattmccray / Taxonomy.js
Created August 12, 2020 19:43
Tilt taxonomy class
import _ from 'lodash'
class TagSet {
constructor({ name, slug, sortBy = 'title' }) {
/** @type {string} */
this.name = name
/** @type {string} */
this.slug = slug
/** @type {string} */
this.sortBy = sortBy
@mattmccray
mattmccray / feed.xml.tilt.js
Last active August 12, 2020 19:33
Tilt feed component
import { renderMarkdown, useCollection, useFiles, usePage, useSite, xml } from "../packages/tilt/index.js";
export default function Feed(props, children) {
const site = useSite()
const posts = useCollection('posts')
const buildDate = (new Date()).toUTCString()
return xml`<?xml version="1.0" encoding="UTF-8"?>
<rss
xmlns:dc="http://purl.org/dc/elements/1.1/"
@mattmccray
mattmccray / context.js
Created August 6, 2020 15:24
Micro context
let _stack = [{}]
export function getContext(key) {
return _stack[0][key]
}
export function setContext(key, value) {
_stack[0][key] = value
}
export function selectImage(accepts: string = "image/*"): Promise<File | null> {
return new Promise((resolve, reject) => {
const input = createFileInput(accepts)
let focusCount = 0
let fileWasSelected = false
input.onchange = (e: any) => {
fileWasSelected = true
const file = e.target.files[0]
@mattmccray
mattmccray / CockpitAPI.ts
Created July 11, 2020 17:20
TypeScript class for using the CockpitCMS API
const _tokenCache = new WeakMap<CockpitAPI, string>()
export class CockpitAPI {
constructor(public readonly baseUrl: string, token: string) {
_tokenCache.set(this, token)
}
setToken(token: string) {