Skip to content

Instantly share code, notes, and snippets.

View Gerhut's full-sized avatar
😷
Working from home

George Cheng Gerhut

😷
Working from home
View GitHub Profile
@Gerhut
Gerhut / codegen.yml
Created July 17, 2023 13:08
Repro of wrong formatted config.schema.json
schema: 'https://jihulab.com/api/graphql'
documents: ['graphql/**/*.graphql']
ignoreNoDocuments: true
generates:
'graphql/dist/types.ts':
plugins: ['typescript']
'graphql/dist/':
preset: 'near-operation-file'
presetConfig:
folder: 'dist'
@Gerhut
Gerhut / atomWithSyncEffects.ts
Last active January 3, 2023 07:37
recoil atom with sync effects
import {
atom,
atomFamily,
AtomFamilyOptions,
AtomOptions,
ReadWriteSelectorFamilyOptions,
ReadWriteSelectorOptions,
selector,
selectorFamily,
SerializableParam
@Gerhut
Gerhut / I18n.js
Last active August 25, 2021 07:02
Simple React I18n
import { createContext, createElement, useContext, useState, useCallback } from 'react'
/** @typedef {{ [language: string] : { [text: string]: string} }} Translations */
const I18nContext = createContext({
/** @type {string} */
language: '',
/** @param {string} language */
setLanguage(language) {},
/** @type {Translations} */
/**
* @callback Tween
* @param {number} x
* @returns {number}
*/
/**
* @callback Easing
* @param {number} x
* @returns {number}
*/

现有如下代码:

function Button1() {
  const [count, setCount] = React.useState(0);
  const handleClick = React.useCallback(function () {
    console.log(count);
    setCount(count + 1);
  }, []);
 return (
@Gerhut
Gerhut / bookmarklet.js
Last active February 11, 2024 22:18
Embed custom scripts by Bookmarklet
(function (_) {
var $ = document.createElement('SCRIPT')
$.src = _
$.onerror = function () { alert('Failed to load ' + _) }
document.body.appendChild($)
} ("data:application/javascript,alert('Hello, world.')"))

Architecture of Editdown

Main Components

Editdown is made up of 2 main components:

  • Frontend: A React component React-editdown.
  • Backend: A Koa application Koa-editdown.

React-editdown

import React, {
createContext,
useState,
useContext
} from 'react'
const AppContext = createContext({
checking: true,
setChecking (checking) {}
})
@Gerhut
Gerhut / useReducer.js
Created June 20, 2019 09:43
React Hooks
import { createRef, useState, useEffect, useContext } from 'react'
export function useReducer(reducer, initialArg, init) {
const [stateRef] = useState(() => {
if (init === undefined) {
return createRef(initialArg)
} else {
return createRef(init(initialArg)))
}
})
@Gerhut
Gerhut / bug.ts
Created June 19, 2019 06:28
TypeScript typing bug
interface Cat {
kind: "cat";
name: string;
}
interface Dog {
kind: "dog";
name: string;
}