Skip to content

Instantly share code, notes, and snippets.

View kmelve's full-sized avatar
💬
is typing

Knut Melvær kmelve

💬
is typing
View GitHub Profile
@kmelve
kmelve / preview-schema.js
Created February 13, 2020 21:52
Preview conf with referenced doc
export default createSchema({
// We name our schema
name: 'default',
// Then proceed to concatenate our document type
// to the ones provided by any plugins that are installed
types: schemaTypes.concat([
{
name: 'frontpage',
type: 'document',
title: 'Frontpage',
@kmelve
kmelve / confitionalFields.js
Created February 7, 2020 06:13
Tim’s conditional fields for Sanity.io
import PropTypes from 'prop-types'
import React from 'react'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import {setIfMissing} from 'part:@sanity/form-builder/patch-event'
// FormBuilderInput automatically generates fields from a schema
import {FormBuilderInput} from 'part:@sanity/form-builder'
// a Higher Order Component that passes document values as props
import {withDocument} from 'part:@sanity/form-builder'
class confitionalFields extends React.PureComponent {
static propTypes = {
const { isFuture, parseISO } = require('date-fns')
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const { format } = require('date-fns')
async function createBlogPostPages (graphql, actions, reporter) {
@kmelve
kmelve / WebcamSource.js
Created November 26, 2019 22:19
Barebones Webcam Image Select Asset Source Component for Sanity Studio
import React from 'react'
import Dialog from 'part:@sanity/components/dialogs/fullscreen'
import Cam from 'react-webcam'
export default class Webcam extends React.Component {
static defaultProps = {
selectedAssets: undefined
}
state = {
@kmelve
kmelve / deleteDocsWithoutSchema.js
Last active March 16, 2021 21:10
Delete documents without corrensponding schema definition in a Sanity Studio folder
/**
* This script can be placed in the root of your studio folder,
* and be run with `sanity exec deleteDocsWithoutSchema.js --with-user-credentials
*/
import client from 'part:@sanity/base/client'
const getSanitySchema = require('./node_modules/@sanity/core/lib/actions/graphql/getSanitySchema')
const Schema = getSanitySchema(process.cwd())
const types = Schema._original.types.map(({name}) => name)
@kmelve
kmelve / MyTool.js
Created November 25, 2019 22:25
Proof of Concept Kanban tool for Sanity Studio
import React from 'react'
import client from 'part:@sanity/base/client'
import ReactKanban from 'react-kanban-dnd';
const columns = [
]
const query = `{
"draft": *[_type == "post" && editorial == "draft"],
@kmelve
kmelve / gatsby-config.js
Last active November 14, 2019 21:29
gatsby rss feed + sanity
const PortableText = require('@sanity/block-content-to-html')
const imageUrlBuilder = require('@sanity/image-url')
const { isFuture, parseISO } = require('date-fns')
const clientConfig = require('./client-config')
const {
getBlogUrl,
filterOutDocsPublishedInTheFuture
} = require('./nodeHelpers.js')
@kmelve
kmelve / [slug].svelte
Last active October 18, 2019 10:23
Adding images to svelte using https://github.com/sanity-io/image-url
<script context="module">
import blocksToHtml from '@sanity/block-content-to-html'
import client from '../../sanityClient'
import urlBuilder from '@sanity/image-url'
import serializers from '../../components/serializers'
const urlFor = source => urlBuilder(client).image(source)
export async function preload({ params }) {
// the `slug` parameter is available because
// this file is called [slug].html
<script context="module">
import blocksToHtml from '@sanity/block-content-to-html'
import client from '../../sanityClient'
import serializers from '../../components/serializers'
export function preload({ params, query }) {
const projection = `{
...,
excerpt[]{
...
}
@kmelve
kmelve / Image.css
Created October 11, 2019 15:07
Lazyload Images from Sanity.io
.root {
display: block;
position: relative;
}
.lqip {
image-rendering: pixelated;
width: 100%;
opacity: 1;
transition: opacity 50ms 100ms ease-out;