Add Squiggle Editor
This commit is contained in:
parent
39ca9aecb5
commit
125f6097e9
|
@ -1,8 +1,8 @@
|
|||
import * as React from 'react';
|
||||
import * as _ from 'lodash';
|
||||
import _ from 'lodash';
|
||||
import type { Spec } from 'vega';
|
||||
import { run } from '@squiggle/lang';
|
||||
import type { DistPlus, SamplingInputs } from '@squiggle/lang';
|
||||
import { run } from '@quri/squiggle-lang';
|
||||
import type { DistPlus, SamplingInputs } from '@quri/squiggle-lang';
|
||||
import { createClassFromSpec } from 'react-vega';
|
||||
import * as chartSpecification from './spec-distributions.json'
|
||||
import * as percentilesSpec from './spec-pertentiles.json'
|
||||
|
@ -39,10 +39,8 @@ export const SquiggleChart : React.FC<SquiggleChartProps> = props => {
|
|||
|
||||
|
||||
let result = run(props.squiggleString, samplingInputs);
|
||||
console.log(result)
|
||||
if (result.tag === "Ok") {
|
||||
let chartResults = result.value.map(chartResult => {
|
||||
console.log(chartResult)
|
||||
if(chartResult["NAME"] === "Float"){
|
||||
return <MakeNumberShower precision={3} number={chartResult["VAL"]} />;
|
||||
}
|
||||
|
|
71
packages/components/src/SquiggleEditor.tsx
Normal file
71
packages/components/src/SquiggleEditor.tsx
Normal file
|
@ -0,0 +1,71 @@
|
|||
import * as React from 'react';
|
||||
import * as ReactDOM from 'react-dom';
|
||||
import { SquiggleChart } from './SquiggleChart'
|
||||
import { ReactCodeJar } from "react-codejar";
|
||||
|
||||
|
||||
export interface SquiggleEditorProps {
|
||||
/** The input string for squiggle */
|
||||
initialSquiggleString : string,
|
||||
|
||||
/** If the output requires monte carlo sampling, the amount of samples */
|
||||
sampleCount? : number,
|
||||
/** The amount of points returned to draw the distribution */
|
||||
outputXYPoints? : number,
|
||||
kernelWidth? : number,
|
||||
pointDistLength? : number,
|
||||
/** If the result is a function, where the function starts */
|
||||
diagramStart? : number,
|
||||
/** If the result is a function, where the function ends */
|
||||
diagramStop? : number,
|
||||
/** If the result is a function, how many points along the function it samples */
|
||||
diagramCount? : number
|
||||
}
|
||||
|
||||
const highlight = (editor: HTMLInputElement) => {
|
||||
let code = editor.textContent;
|
||||
code = code.replace(/\((\w+?)(\b)/g, '(<font color="#8a2be2">$1</font>$2');
|
||||
editor.innerHTML = code;
|
||||
};
|
||||
|
||||
export const SquiggleEditor : React.FC<SquiggleEditorProps> = props => {
|
||||
let [expression, setExpression] = React.useState(props.initialSquiggleString)
|
||||
return (
|
||||
<div>
|
||||
<ReactCodeJar
|
||||
code={expression}
|
||||
onUpdate={setExpression}
|
||||
style={{
|
||||
borderRadius: "6px",
|
||||
width: "530px",
|
||||
border: "1px solid grey",
|
||||
fontFamily: "'Source Code Pro', monospace",
|
||||
fontSize: "14px",
|
||||
fontWeight: "400",
|
||||
letterSpacing: "normal",
|
||||
lineHeight: "20px",
|
||||
padding: "10px",
|
||||
tabSize: "4"
|
||||
}}
|
||||
highlight={highlight}
|
||||
lineNumbers={false}
|
||||
/>
|
||||
<SquiggleChart
|
||||
squiggleString={expression}
|
||||
sampleCount={props.sampleCount}
|
||||
outputXYPoints={props.outputXYPoints}
|
||||
kernelWidth={props.kernelWidth}
|
||||
pointDistLength={props.pointDistLength}
|
||||
diagramStart={props.diagramStart}
|
||||
diagramStop={props.diagramStop}
|
||||
diagramCount={props.diagramCount}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function renderSquiggleEditor(props : SquiggleEditorProps) {
|
||||
let parent = document.createElement("div")
|
||||
ReactDOM.render(<SquiggleEditor {...props} /> , parent)
|
||||
return parent
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
export { SquiggleChart } from './SquiggleChart';
|
||||
export { SquiggleEditor, renderSquiggleEditor } from './SquiggleEditor';
|
||||
|
|
26
packages/components/src/stories/SquiggleEditor.stories.mdx
Normal file
26
packages/components/src/stories/SquiggleEditor.stories.mdx
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { SquiggleEditor } from '../SquiggleEditor'
|
||||
import { Canvas, Meta, Story, Props } from '@storybook/addon-docs';
|
||||
|
||||
<Meta title="Squiggle/SquiggleEditor" component={ SquiggleEditor } />
|
||||
|
||||
export const Template = SquiggleEditor
|
||||
|
||||
# Squiggle Editor
|
||||
|
||||
A Squiggle editor is simply a squiggle chart with a text editor rendered before
|
||||
it that allows the user to change the expression that is getting rendered.
|
||||
|
||||
## Usage
|
||||
|
||||
An example of a normal distribution is:
|
||||
<Canvas>
|
||||
<Story
|
||||
name="Default"
|
||||
args={{
|
||||
initialSquiggleString: "normal(5,2)"
|
||||
}}>
|
||||
{Template.bind({})}
|
||||
</Story>
|
||||
</Canvas>
|
||||
|
||||
<Props of={SquiggleEditor} />
|
Loading…
Reference in New Issue
Block a user