Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt McCray mattmccray

View GitHub Profile
@mattmccray
mattmccray / ObservableUndoController.ts
Last active Sep 6, 2020
Simple Observable Undo Controller
View ObservableUndoController.ts
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 Sep 6, 2020
Super Simple Undo Manager
View UndoManager.ts
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 Aug 12, 2020
Tilt taxonomy class
View Taxonomy.js
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 Aug 12, 2020
Tilt feed component
View feed.xml.tilt.js
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/"
View context.js
let _stack = [{}]
export function getContext(key) {
return _stack[0][key]
}
export function setContext(key, value) {
_stack[0][key] = value
}
View selectImage.ts
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 Jul 11, 2020
TypeScript class for using the CockpitCMS API
View CockpitAPI.ts
const _tokenCache = new WeakMap<CockpitAPI, string>()
export class CockpitAPI {
constructor(public readonly baseUrl: string, token: string) {
_tokenCache.set(this, token)
}
setToken(token: string) {
View useStreamedState.ts
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 Jun 6, 2020
Single-File React Component - Example (idea)
View Test.react
<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(() => (
View bulma-prefers-dark-tweaks.css
* {
scroll-behavior: smooth;
box-sizing: border-box;
}
.modal-card {
box-shadow: 0px 6px 8px rgba(0,0,0,.5);
border-radius: 6px;
}
You can’t perform that action at this time.