Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
690f693
Improvements to the shortcut keys
samejr Sep 29, 2023
2903a54
Updated code mirror theme to the new dark mode style
samejr Sep 29, 2023
c09ffb4
Removed the old test page help panel copy
samejr Sep 29, 2023
033db98
WIP new structure for the test page
samejr Sep 29, 2023
810507c
Added some of the side panel options for examples and account ID
samejr Sep 29, 2023
09bdaa9
Conditionally hide the side panel components if they’re blank
samejr Sep 29, 2023
1be7df3
Example with selected state working
matt-aitken Oct 1, 2023
a647bdd
Allow selecting the example again to overwrite any edits
matt-aitken Oct 1, 2023
793b425
Previous run payloads working
matt-aitken Oct 1, 2023
817ed79
Select a recent payload if there’s no example
matt-aitken Oct 2, 2023
b38cfd5
Created Icon and DetailCell components
matt-aitken Oct 2, 2023
88e7116
The integrations now use the DetailCell
matt-aitken Oct 2, 2023
f39577a
Use DetailCell on the integrations page
matt-aitken Oct 3, 2023
55a38ea
Added support for description to DetailCell. With proper variants now
matt-aitken Oct 3, 2023
c08c695
DateTimeAccurate and formatDateTimeAccurate weren’t displaying correc…
matt-aitken Oct 4, 2023
912c600
DetailCell now allows label and description to be React components
matt-aitken Oct 4, 2023
625b8ee
Using the DetailCell on the Test page
matt-aitken Oct 4, 2023
ebd0fb0
Styled the CodeMirror scrollbars to match elsewhere
matt-aitken Oct 4, 2023
b8cb6a3
Adding copy and clear to the JSONEditor, not working properly yet though
matt-aitken Oct 4, 2023
1764f73
Removed the copy/clear buttons from the Test route
matt-aitken Oct 4, 2023
e98a85a
Delete the light color theme
matt-aitken Oct 4, 2023
d85026f
The editor now supports copying/clearing etc
matt-aitken Oct 4, 2023
fc58e9b
Made it clear when text is copied
matt-aitken Oct 4, 2023
c267904
Changed the learn link to a tertiary button
matt-aitken Oct 4, 2023
7ed3aab
Fixed CMD shortcut keys
matt-aitken Oct 4, 2023
93d083b
Run test button now works with ⌘ Enter
matt-aitken Oct 4, 2023
6cb50ff
CodeMirror now allows ⌘Enter to escape the field
matt-aitken Oct 4, 2023
b08c22e
Added JSON linter to the test editor
matt-aitken Oct 4, 2023
9e2e968
Fix for buttons not being aligned correctly
matt-aitken Oct 4, 2023
905afe4
Copy/clear buttons now aligned
matt-aitken Oct 4, 2023
30c6817
Made the integration DetailCell text smaller
matt-aitken Oct 4, 2023
6522caf
The test recent payloads now have colored text to help identify the s…
matt-aitken Oct 4, 2023
09cc9fb
DetailCell descriptions are dimmer
matt-aitken Oct 4, 2023
23d0ff4
Added link to docs for BYOA
matt-aitken Oct 5, 2023
9d02886
Fix for buttons going full width when they shouldn’t
matt-aitken Oct 5, 2023
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
90 changes: 72 additions & 18 deletions apps/webapp/app/components/code/JSONEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { json as jsonLang } from "@codemirror/lang-json";
import type { ViewUpdate } from "@codemirror/view";
import { CheckIcon, ClipboardIcon } from "@heroicons/react/20/solid";
import type { ReactCodeMirrorProps, UseCodeMirror } from "@uiw/react-codemirror";
import { useCodeMirror } from "@uiw/react-codemirror";
import { useRef, useEffect } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { cn } from "~/utils/cn";
import { Button } from "../primitives/Buttons";
import { getEditorSetup } from "./codeMirrorSetup";
import { darkTheme } from "./codeMirrorTheme";
import { cn } from "~/utils/cn";

export interface JSONEditorProps extends Omit<ReactCodeMirrorProps, "onBlur"> {
defaultValue?: string;
Expand All @@ -14,6 +16,8 @@ export interface JSONEditorProps extends Omit<ReactCodeMirrorProps, "onBlur"> {
onChange?: (value: string) => void;
onUpdate?: (update: ViewUpdate) => void;
onBlur?: (code: string) => void;
showCopyButton?: boolean;
showClearButton?: boolean;
}

const languages = {
Expand All @@ -38,6 +42,8 @@ export function JSONEditor(opts: JSONEditorProps) {
onBlur,
basicSetup,
autoFocus,
showCopyButton = true,
showClearButton = true,
} = {
...defaultProps,
...opts,
Expand Down Expand Up @@ -65,7 +71,8 @@ export function JSONEditor(opts: JSONEditorProps) {
onChange,
onUpdate,
};
const { setContainer, state } = useCodeMirror(settings);
const { setContainer, view } = useCodeMirror(settings);
const [copied, setCopied] = useState(false);

useEffect(() => {
if (editor.current) {
Expand All @@ -75,24 +82,71 @@ export function JSONEditor(opts: JSONEditorProps) {

//if the defaultValue changes update the editor
useEffect(() => {
if (state !== undefined) {
state.update({
changes: { from: 0, to: state.doc.length, insert: defaultValue },
if (view !== undefined) {
if (view.state.doc.toString() === defaultValue) return;
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: defaultValue },
});
}
}, [defaultValue, state]);
}, [defaultValue, view]);

const clear = useCallback(() => {
if (view === undefined) return;
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: undefined },
});
onChange?.("");
}, [view]);

const copy = useCallback(() => {
if (view === undefined) return;
navigator.clipboard.writeText(view.state.doc.toString());
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 1500);
}, [view]);

return (
<div
className={cn(
"overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-slate-700",
opts.className
)}
ref={editor}
onBlur={() => {
if (!onBlur) return;
onBlur(editor.current?.textContent ?? "");
}}
/>
<div className={cn(opts.className, "relative")}>
<div
className="h-full w-full"
ref={editor}
onBlur={() => {
if (!onBlur) return;
onBlur(editor.current?.textContent ?? "");
}}
/>
<div className="absolute right-3 top-3 flex items-center gap-2">
{showClearButton && (
<Button
type="button"
variant="secondary/small"
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
clear();
}}
>
Clear
</Button>
)}
{showCopyButton && (
<Button
type="button"
variant="secondary/small"
LeadingIcon={copied ? CheckIcon : ClipboardIcon}
leadingIconClassName={copied ? "text-green-500 group-hover:text-green-500" : undefined}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
copy();
}}
>
Copy
</Button>
)}
</div>
</div>
);
}
51 changes: 25 additions & 26 deletions apps/webapp/app/components/code/codeMirrorSetup.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
import { closeBrackets } from "@codemirror/autocomplete";
import { indentWithTab } from "@codemirror/commands";
import { jsonParseLinter } from "@codemirror/lang-json";
import { bracketMatching } from "@codemirror/language";
import { lintGutter, lintKeymap, linter } from "@codemirror/lint";
import { highlightSelectionMatches } from "@codemirror/search";
import { Prec, type Extension } from "@codemirror/state";
import {
highlightSpecialChars,
drawSelection,
highlightActiveLine,
dropCursor,
lineNumbers,
highlightActiveLine,
highlightActiveLineGutter,
highlightSpecialChars,
keymap,
lineNumbers,
} from "@codemirror/view";
import type { Extension } from "@codemirror/state";
import { highlightSelectionMatches } from "@codemirror/search";
import { json as jsonLang } from "@codemirror/lang-json";
import { closeBrackets } from "@codemirror/autocomplete";
import { bracketMatching } from "@codemirror/language";
import { indentWithTab } from "@codemirror/commands";

export function getPreviewSetup(): Array<Extension> {
return [
jsonLang(),
highlightSpecialChars(),
drawSelection(),
dropCursor(),
bracketMatching(),
highlightSelectionMatches(),
lineNumbers(),
];
}

export function getViewerSetup(): Array<Extension> {
return [drawSelection(), dropCursor(), bracketMatching(), lineNumbers()];
}

export function getEditorSetup(showLineNumbers = true, showHighlights = true): Array<Extension> {
const options = [
drawSelection(),
dropCursor(),
bracketMatching(),
closeBrackets(),
keymap.of([indentWithTab]),
lintGutter(),
linter(jsonParseLinter()),
Prec.highest(
keymap.of([
{
key: "Mod-Enter",
run: () => {
console.log("Mod-Enter");
return true;
},
preventDefault: false,
},
])
),
keymap.of([indentWithTab, ...lintKeymap]),
];

if (showLineNumbers) {
Expand Down
Loading