Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Tag.startOpen, Tag.startClosed, Tag.getStartOpenState #2975

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/friendly-jobs-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@quri/squiggle-lang": patch
"@quri/squiggle-components": patch
---

Added Tag.startOpen, Tag.startClosed, and Tag.getStartOpenState
6 changes: 6 additions & 0 deletions packages/components/src/components/SquiggleViewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ export const shouldBeginCollapsed = (
value: SqValueWithContext,
path: SqValuePath
): boolean => {
const startOpenState = value.tags.startOpenState();
if (startOpenState === "open") {
return false;
} else if (startOpenState === "closed") {
return true;
}
const childrenValues = getChildrenValues(value);
if (path.isRoot()) {
return childrenValues.length > 30;
Expand Down
31 changes: 20 additions & 11 deletions packages/components/src/stories/SquigglePlayground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,26 @@ export const Tagged: Story = {
args: {
defaultCode: `z = 34 -> Tag.format(".1f")

@name("My favorite Dist")
@doc("This is a long description")
@format("$.2")
x = 5 to 10

@showAs(Plot.numericFn)
@name("My favorite Fn")
fn = {|e| e}
@name("My favorite Dist")
@doc("This is a long description")
@format("$.2")
x = 5 to 10

bar = [x, fn]

y = x -> Tag.getAll`,
@showAs(Plot.numericFn)
@name("My favorite Fn")
fn = {|e| e}

@hide
bar = [x, fn]

@startOpen
s = 4 to 10

@startClosed
@showAs(Plot.numericFn)
@name("My favorite Fn2")
fn2 = {|e| e}

y = x -> Tag.getAll`,
},
};
9 changes: 9 additions & 0 deletions packages/squiggle-lang/__tests__/library/tag_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ describe("Tags", () => {
testEvalToBe("3 -> Tag.hide -> Tag.getHide", "true");
});

describe("startOpenToggle", () => {
testEvalToBe("3 -> Tag.startOpen -> Tag.getStartOpenState", '"open"');
testEvalToBe("3 -> Tag.startClosed -> Tag.getStartOpenState", '"closed"');
testEvalToBe(
"3 -> Tag.startClosed -> Tag.startOpen -> Tag.getStartOpenState",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small feature that one can do "startClosed" then "startOpen", and the last one is what counts. This wouldn't be the case if we saved both separately.

'"open"'
);
});

describe("omit", () => {
testEvalToBe(
"123 -> Tag.name('myName') -> Tag.doc('myDoc') -> Tag.format('.2%') -> Tag.omit(['name', 'doc']) -> Tag.getAll",
Expand Down
45 changes: 45 additions & 0 deletions packages/squiggle-lang/src/fr/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,51 @@ example2 = {|x| x + 1}`,
}),
],
}),
maker.make({
name: "startOpen",
description: `When the value is first displayed, it will begin open in the viewer. Refresh the page to reset.`,
displaySection: "Tags",
definitions: [
makeDefinition(
[frWithTags(frAny({ genericName: "A" }))],
frWithTags(frAny({ genericName: "A" })),
([{ value, tags }]) => ({
value,
tags: tags.merge({ startOpenState: "open" }),
}),
{ isDecorator: true }
),
],
}),
maker.make({
name: "startClosed",
description: `When the value is first displayed, it will begin collapsed in the viewer. Refresh the page to reset.`,
displaySection: "Tags",
definitions: [
makeDefinition(
[frWithTags(frAny({ genericName: "A" }))],
frWithTags(frAny({ genericName: "A" })),
([{ value, tags }]) => ({
value,
tags: tags.merge({ startOpenState: "closed" }),
}),
{ isDecorator: true }
),
],
}),
maker.make({
name: "getStartOpenState",
displaySection: "Tags",
description: `Returns the startOpenState of a value, which can be "open", "closed", or "" if no startOpenState is set. Set using \`Tag.startOpen\` and \`Tag.startClosed\`.`,
definitions: [
makeDefinition(
[frWithTags(frAny())],
frString,
([{ tags }]) => tags?.value.startOpenState || "",
{ isDecorator: true }
OAGr marked this conversation as resolved.
Show resolved Hide resolved
),
],
}),
maker.make({
name: "notebook",
description: `Displays the list of values as a notebook. This means that element indices are hidden, and the values are displayed in a vertical list. Useful for displaying combinations of text and values.`,
Expand Down
4 changes: 4 additions & 0 deletions packages/squiggle-lang/src/public/SqValue/SqTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ export class SqTags {
notebook(): boolean | undefined {
return this.tags.notebook();
}

startOpenState(): "open" | "closed" | undefined {
return this.tags.startOpenState();
}
}
9 changes: 9 additions & 0 deletions packages/squiggle-lang/src/value/valueTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ValueTagsType = {
dateFormat?: string;
hidden?: boolean;
notebook?: boolean;
startOpenState?: "open" | "closed";
};

type ValueTagsTypeName = keyof ValueTagsType;
Expand All @@ -23,6 +24,7 @@ const valueTagsTypeNames: ValueTagsTypeName[] = [
"dateFormat",
"hidden",
"notebook",
"startOpenState",
];

function convertToValueTagsTypeName(
Expand Down Expand Up @@ -69,6 +71,9 @@ export class ValueTags {
if (value.notebook) {
result.push(["notebook", vBool(value.notebook)]);
}
if (value.startOpenState) {
result.push(["startOpenState", vString(value.startOpenState)]);
}
return result;
}

Expand Down Expand Up @@ -128,4 +133,8 @@ export class ValueTags {
notebook() {
return this.value.notebook;
}

startOpenState() {
return this.value.startOpenState;
}
}