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

Add 'Copy to Clipboard' option for 'Unsaved Draft' #2368

Closed
wants to merge 5 commits into from
Closed
Changes from all 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
86 changes: 86 additions & 0 deletions packages/components/src/components/SquiggleEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { FC, useMemo, useRef } from "react";
import { useClipboard } from 'react-use';

import { useUncontrolledCode } from "../lib/hooks/index.js";
import { useSquiggle } from "../lib/hooks/useSquiggle.js";
import { getErrors } from "../lib/utility.js";
import { CodeEditor, CodeEditorHandle } from "./CodeEditor.js";
import { DynamicSquiggleViewer } from "./DynamicSquiggleViewer.js";
import { PartialPlaygroundSettings } from "./PlaygroundSettings.js";
import { useRunnerState } from "../lib/hooks/useRunnerState.js";
import { SquiggleCodeProps } from "./types.js";

export type SquiggleEditorProps = SquiggleCodeProps & {
hideViewer?: boolean;
localSettingsEnabled?: boolean;
// environment comes from SquiggleCodeProps
} & Omit<PartialPlaygroundSettings, "environment">;

export const SquiggleEditor: FC<SquiggleEditorProps> = ({import { FC, useMemo, useRef } from "react";
import { useClipboard } from 'react-use';

import { useUncontrolledCode } from "../lib/hooks/index.js";
import { useSquiggle } from "../lib/hooks/useSquiggle.js";
Expand All @@ -16,6 +35,62 @@ export type SquiggleEditorProps = SquiggleCodeProps & {
} & Omit<PartialPlaygroundSettings, "environment">;

export const SquiggleEditor: FC<SquiggleEditorProps> = ({
const { code, setCode, defaultCode } = useUncontrolledCode({
defaultCode: propsDefaultCode,
onCodeChange,
});

const [, copyToClipboard] = useClipboard(code);

// Rest of the component code...

const runnerState = useRunnerState(code);

const [squiggleOutput, { project, isRunning }] = useSquiggle({
code: runnerState.renderedCode,
executionId: runnerState.executionId,
...(propsProject ? { project: propsProject, continues } : { environment }),
});

const errors = useMemo(() => {
if (!squiggleOutput) {
return [];
}
return getErrors(squiggleOutput.output);
}, [squiggleOutput]);

const editorRef = useRef<CodeEditorHandle>(null);

return (
<div>
<div
className="border border-grey-200 p-2 m-4"
data-testid="squiggle-editor"
>
<CodeEditor
defaultValue={defaultCode ?? ""}
onChange={setCode}
showGutter={false}
errors={errors}
project={project}
ref={editorRef}
onSubmit={() => runnerState.run()}
/>
<button onClick={copyToClipboard} disabled={!code}>Copy to Clipboard</button>
</div>
{hideViewer || !squiggleOutput?.code ? null : (
<DynamicSquiggleViewer
squiggleOutput={squiggleOutput}
isRunning={isRunning}
localSettingsEnabled={localSettingsEnabled}
editor={editorRef.current ?? undefined}
environment={environment}
{...settings}
/>
)}
</div>
);
};
defaultCode: propsDefaultCode,
onCodeChange,
project: propsProject,
Expand All @@ -29,6 +104,15 @@ export const SquiggleEditor: FC<SquiggleEditorProps> = ({
defaultCode: propsDefaultCode,
onCodeChange,
});
=======
const { code, setCode, defaultCode } = useUncontrolledCode({
defaultCode: propsDefaultCode,
onCodeChange,
});

const [, copyToClipboard] = useClipboard(code);

// Rest of the component code...

const runnerState = useRunnerState(code);

Expand All @@ -53,6 +137,8 @@ export const SquiggleEditor: FC<SquiggleEditorProps> = ({
className="border border-grey-200 p-2 m-4"
data-testid="squiggle-editor"
>
<label>Unsaved Draft</label>
<button onClick={copyToClipboard} disabled={!code}>Copy to Clipboard</button>
<CodeEditor
defaultValue={defaultCode ?? ""}
onChange={setCode}
Expand Down
Loading