Skip to content

Instantly share code, notes, and snippets.

View mattmccray's full-sized avatar

Matt McCray mattmccray

View GitHub Profile
@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[] = []
@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) {
@mattmccray
mattmccray / useStreamedState.ts
Last active June 17, 2020 23:07
useStreamedState
import * as React from 'react'
import { stream, scan } from 'flyd'
import merge from 'mergerino'
interface StreamedStateConfigObject<T, A> {
state: T, actions: (updater: (value: Partial<T>) => void) => A
}
type StreamedStateConfigBuilder<T, A> = () => StreamedStateConfigObject<T, A>
@mattmccray
mattmccray / Test.react
Created June 6, 2020 19:50
Single-File React Component - Example (idea)
<script role="view">
import { store, view } from 'react-easy-state'
const counter = store({
value: 0,
increment() { counter.value += 1 },
decrement() { counter.value -= 1 }
})
export default view(() => (
@mattmccray
mattmccray / bulma-prefers-dark-tweaks.css
Created June 4, 2020 03:07
Tweaks for bulma-prefers-dark
* {
scroll-behavior: smooth;
box-sizing: border-box;
}
.modal-card {
box-shadow: 0px 6px 8px rgba(0,0,0,.5);
border-radius: 6px;
}