/** * @license * Copyright 2020 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ /** * This kinda turned into something like storybook.js and similar. Maybe we * should just use another library. */ import {themeNames} from './themes.js'; import {tokens} from './highlight-tokens.js'; interface BaseKnob { id: Id; label: string; section: string; default: T; cssProperty?: string; formatCss?: (value: T) => string; htmlAttribute?: string; } interface CheckboxKnob extends BaseKnob { type: 'checkbox'; } interface SliderKnob extends BaseKnob { type: 'slider'; min: number; max: number; } interface ColorKnob extends BaseKnob { type: 'color'; unsetLabel?: string; // Gross hack. When we load a new theme, we change color knob defaults. But if // we switch back to the default theme, this is how we know what the original // default was. originalDefault?: string; } interface SelectKnob extends BaseKnob { type: 'select'; options: ReadonlyArray; } interface InputKnob extends BaseKnob { type: 'input'; placeholder?: string; inputType?: string; } function checkbox( def: Omit, 'type'> ): CheckboxKnob { return {type: 'checkbox', ...def}; } function slider( def: Omit, 'type'> ): SliderKnob { return {type: 'slider', ...def}; } function color( def: Omit, 'type'> ): ColorKnob { return {type: 'color', ...def}; } function select( def: Omit, 'type'> ): SelectKnob { return {type: 'select', ...def}; } function input( def: Omit, 'type'> ): InputKnob { return {type: 'input', ...def}; } const pixels = (value: number) => value + 'px'; export const knobs = [ // code style select({ id: 'theme', label: 'Theme', section: 'code', options: ['default', ...themeNames], default: 'default', }), slider({ id: 'fontSize', label: 'Font size', section: 'code', cssProperty: '--playground-code-font-size', formatCss: pixels, min: 1, max: 30, default: 14, }), select({ id: 'fontFamily', label: 'Font', section: 'code', cssProperty: '--playground-code-font-family', options: ['monospace', 'Roboto Mono', 'Source Code Pro', 'Ubuntu Mono'], formatCss: (value: string | number | boolean) => { if (value !== 'monospace') { return `${value}, monospace`; } return value; }, default: 'monospace', }), ...tokens.map((token) => { const {id, label, cssProperty, defaultValue} = token; return color({ id, label, cssProperty, default: defaultValue, originalDefault: defaultValue, section: 'code', }); }), // features input({ id: 'cdnBaseUrl', label: 'CDN Base URL', section: 'features', default: '', placeholder: 'Resolves to https://unpkg.com', htmlAttribute: 'cdn-base-url', inputType: 'text', }), checkbox({ id: 'resizable', label: 'Resizable', section: 'features', default: false, htmlAttribute: 'resizable', }), checkbox({ id: 'editableFileSystem', label: 'Editable filesystem', section: 'features', default: false, htmlAttribute: 'editable-file-system', }), checkbox({ id: 'lineNumbers', label: 'Line numbers', section: 'features', default: false, htmlAttribute: 'line-numbers', }), checkbox({ id: 'lineWrapping', label: 'Line wrapping', section: 'features', default: false, htmlAttribute: 'line-wrapping', }), checkbox({ id: 'noCompletions', label: 'No completions', section: 'features', default: false, htmlAttribute: 'no-completions', }), // general appearance color({ id: 'highlight', label: 'Highlight', cssProperty: '--playground-highlight-color', default: '#6200ee', section: 'general appearance', }), color({ id: 'pageBackground', label: 'Page background', default: '#cccccc', originalDefault: '#cccccc', section: 'general appearance', }), slider({ id: 'radius', label: 'Radius', cssProperty: 'border-radius', formatCss: pixels, min: 0, max: 30, default: 0, section: 'general appearance', }), checkbox({ id: 'borders', label: 'Borders', cssProperty: '--playground-border', formatCss: (value: string | number | boolean) => value ? '1px solid #ddd' : 'none', default: true, section: 'general appearance', }), // tab bar color({ id: 'tabBarBackground', label: 'Background', cssProperty: '--playground-tab-bar-background', default: '#eaeaea', section: 'tab bar', }), color({ id: 'tabBarForeground', label: 'Foreground', cssProperty: '--playground-tab-bar-foreground-color', default: '#000000', section: 'tab bar', }), slider({ id: 'barHeight', label: 'Bar height', cssProperty: '--playground-bar-height', formatCss: pixels, min: 10, max: 100, default: 40, section: 'tab bar', }), // preview color({ id: 'previewToolbarBackground', label: 'Background', cssProperty: '--playground-preview-toolbar-background', default: '#ffffff', section: 'preview', }), color({ id: 'previewToolbarForeground', label: 'Foreground', cssProperty: '--playground-preview-toolbar-foreground-color', default: '#444444', section: 'preview', }), slider({ id: 'previewWidth', label: 'Preview width', cssProperty: '--playground-preview-width', formatCss: (val) => `${val}%`, min: 0, max: 100, default: 30, section: 'preview', }), ] as const; export type Knob = (typeof knobs)[number]; export type KnobId = Knob['id']; type KnobsById = {[K in Knob as K['id']]: K}; export const knobsById = {} as KnobsById; export const knobsBySection = {} as {[section: string]: Knob[]}; for (const knob of knobs) { // Cast to an arbitrary specific Knob type here because TypeScript isn't quite // clever enough to know that the id will match the type. knobsById[(knob as (typeof knobs)[0]).id] = knob as (typeof knobs)[0]; let catArr = knobsBySection[knob.section]; if (catArr === undefined) { catArr = knobsBySection[knob.section] = []; } catArr.push(knob); } export const knobIds = Object.keys(knobsById) as KnobId[]; export const knobSectionNames = Object.keys(knobsBySection); export type KnobsOfType = Exclude< { [K in Knob as K['id']]: K extends {type: T} ? K : never; }[KnobId], never >; export type KnobValueType = KnobsById[T]['default']; export class KnobValues { private values = new Map(); getValue(id: T): KnobValueType { if (this.values.has(id)) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return this.values.get(id) as any; } // eslint-disable-next-line @typescript-eslint/no-explicit-any return knobsById[id].default as any; } setValue(id: T, value: KnobValueType) { this.values.set(id, value); } }