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

fix: Enhancement of Visual Json Schema Editor #1065

Merged
merged 39 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
06a2f4e
changed type of property function
Gmin2 Apr 5, 2024
d5104da
added ability to delete properties and added property function for ca…
Gmin2 Apr 5, 2024
3d33502
added toggle property for nested and non nested objects
Gmin2 Apr 8, 2024
c530d52
modified the UI
Gmin2 Apr 10, 2024
fa72fc6
improve the UI
Gmin2 Apr 10, 2024
3e567cb
cleanup
Gmin2 Apr 10, 2024
284832a
modified the delete property
Gmin2 Apr 10, 2024
d81d930
array of type is visible
Gmin2 Apr 12, 2024
2ceb3c6
fix for adding property type to `array<object>`
Gmin2 Apr 12, 2024
dadc00d
added all the changes requested
Gmin2 Apr 12, 2024
4334c14
cleanup
Gmin2 Apr 12, 2024
04648c8
added option to `add property` to the `array<object>` type, in the UI…
Gmin2 Apr 15, 2024
1b1fd96
cleanup
Gmin2 Apr 15, 2024
b821555
Merge branch 'master' into test-editor
princerajpoot20 Apr 16, 2024
f5329d9
Merge branch 'master' of https://github.com/utnim2/studio into test-e…
Gmin2 May 7, 2024
88694a2
Merge branch 'test-editor' of https://github.com/utnim2/studio into t…
Gmin2 May 7, 2024
9983711
added ability to change array items
Gmin2 May 13, 2024
ccd6ea3
chnage color function
Gmin2 May 13, 2024
f75170c
enter
Gmin2 May 13, 2024
3b9320d
Revert "enter"
Gmin2 May 13, 2024
a9a5c0e
enter
Gmin2 May 13, 2024
f3d2278
added the remaining changes
Gmin2 May 18, 2024
398711b
Merge branch 'master' into test-editor
KhudaDad414 May 20, 2024
d05bf8a
added some changes
Gmin2 May 21, 2024
5e9c28f
added collapsable icon for object & array
Gmin2 May 22, 2024
13bc7c8
Merge branch 'master' into test-editor
princerajpoot20 May 22, 2024
562c640
added pnpm lock file
Gmin2 May 22, 2024
a3b9654
removed unused prop in Dropdown
Gmin2 May 24, 2024
e18dedd
cleanup
Gmin2 May 24, 2024
472ae06
fixed sonarcloud error
Gmin2 May 24, 2024
b616a30
cleannnn
Gmin2 May 24, 2024
33e0b9f
added some reviewed change
Gmin2 May 27, 2024
2be5cb0
removed unused props in dropdownEditor
Gmin2 May 29, 2024
67d41dd
Revert "removed unused props in dropdownEditor"
Gmin2 May 29, 2024
7b4fb9d
added three more examples and resolved some bugs
Gmin2 May 30, 2024
a08b46d
corrected all the changes
Gmin2 Jun 2, 2024
3648dc0
required array is removed when changing types and removing a property
Gmin2 Jun 4, 2024
cf87bb9
cleanup
Gmin2 Jun 5, 2024
7707e06
when changing type the root properties is removed
Gmin2 Jun 5, 2024
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
2 changes: 2 additions & 0 deletions packages/ui/components/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface DropdownMenuProps {
items: DropdownMenuItem[]
side?: 'top' | 'right' | 'bottom' | 'left'
align?: 'start' | 'center' | 'end'
onSelect?: (options: string[]) => void
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
}

interface DropdownMenuItemComponentProps {
Expand All @@ -44,6 +45,7 @@ export const DropdownMenu: FunctionComponent<DropdownMenuProps> = ({
items,
side,
align,
onSelect
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
}) => {
return (
<RadixDropdownMenu.Root>
Expand Down
163 changes: 114 additions & 49 deletions packages/ui/components/VisualEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, { useState, useEffect } from 'react';
import SchemaObject from './VisualEditor/SchemaObject';
import _ from 'lodash';
import { getColorForType } from './VisualEditor/SchemaProperty';
import { DropdownMenu, DropdownMenuItem } from './DropdownMenu';
import { IoIosArrowDropdown } from 'react-icons/io';
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

interface VisualEditorProps {
schema: string;
Expand All @@ -15,14 +18,7 @@ interface SchemaObjectInterface {
}

export const VisualEditor: React.FC<VisualEditorProps> = ({ schema, onSchemaChange }) => {
const selectStyle = {
backgroundColor: '#0F172A',
color: 'white',
borderRadius: '3px',
fontSize: '12px',
fontFamily: 'Inter, sans-serif'
};


const [schemaObject, setSchemaObject] = useState<SchemaObjectInterface>({});

useEffect(() => {
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -43,57 +39,126 @@ export const VisualEditor: React.FC<VisualEditorProps> = ({ schema, onSchemaChan
onSchemaChange(newSchemaString);
};

const renderRootTypeSelector = () => (
<div>
<select
value={schemaObject.type || ''}
onChange={(e) => handleSchemaChange({ type: e.target.value })}
style={selectStyle}
>
<option value="">Select root type</option>
<option value="object">Object</option>
<option value="array">Array</option>
<option value="string">String</option>
<option value="number">Number</option>
<option value="boolean">Boolean</option>
</select>
</div>
);
const handleRootTypeDropdownSelect = (selectedOption: string) => {
if (selectedOption === 'array') {
handleSchemaChange({ type: 'array', items: { type: 'string' } });
} else {
handleSchemaChange({ type: selectedOption });
}
};

const renderArrayItemTypeSelector = () => {
if (schemaObject.type === 'array') {
const handleArrayItemTypeDropdownSelect = (selectedOption: string) => {
handleSchemaChange({ items: schemaObject.items ? { ...schemaObject.items, type: selectedOption } : { type: selectedOption } });
};

const rootTypeOptions: DropdownMenuItem[] = [
{ title: 'Select Root Type',onSelect: () => {}},
{ type: 'separator'},
{ type: 'regular', title: 'Object', onSelect: () => handleRootTypeDropdownSelect('object') },
{ type: 'regular', title: 'Array', onSelect: () => handleRootTypeDropdownSelect('array') },
{ type: 'regular', title: 'String', onSelect: () => handleRootTypeDropdownSelect('string') },
{ type: 'regular', title: 'Number', onSelect: () => handleRootTypeDropdownSelect('number') },
{ type: 'regular', title: 'Boolean', onSelect: () => handleRootTypeDropdownSelect('boolean') },
];

const itemTypeOptions: DropdownMenuItem[] = [
{ title: 'Select Items Type',onSelect: () => {}},
{ type: 'separator'},
{ type: 'regular', title: 'String', onSelect: () => handleArrayItemTypeDropdownSelect('string') },
{ type: 'regular', title: 'Number', onSelect: () => handleArrayItemTypeDropdownSelect('number') },
{ type: 'regular', title: 'Boolean', onSelect: () => handleArrayItemTypeDropdownSelect('boolean') },
{ type: 'regular', title: 'Object', onSelect: () => handleArrayItemTypeDropdownSelect('object') },
];

const renderRootTypeDisplay = () => {
if(schemaObject.type === 'array') {
return null;
}
const rootType = schemaObject.type || '';
const displayRootType = rootType.charAt(0).toUpperCase() + rootType.slice(1);
return (
<div className="flex items-center">
<span
style={{
color: getColorForType(rootType),
borderRadius: '3px',
padding: '2px 4px',
fontSize: '14px',
fontFamily: 'Inter, Helvetica',
}}
>
{displayRootType}
</span>
</div>
);
};

const renderArrayItemTypeDisplay = () => {
if (schemaObject.type === 'array' && schemaObject.items) {
const itemType = schemaObject.items?.type || '';
return (
<div>
<strong>Array Item Type:</strong>
<select
value={schemaObject.items?.type || ''}
onChange={(e) => handleSchemaChange({ items: { ...schemaObject.items, type: e.target.value } })}
style={selectStyle}
<div className="flex items-center">
<span
style={{
color: getColorForType('array', itemType),
borderRadius: '3px',
padding: '2px 4px',
fontSize: '14px',
fontFamily: 'Inter, Helvetica',
}}
>
<option value="">Select item type</option>
<option value="string">String</option>
<option value="number">Number</option>
<option value="boolean">Boolean</option>
<option value="object">Object</option>
<option value="array">Array</option>
</select>
{`Array<${itemType}>`}
</span>
</div>
);
}
return null;
};

return (
<div className="visual-editor" style={{ width: '45vw', minWidth: '550px', background: '#0F172A', color: '#CBD5E1', fontFamily: 'Inter, sans-serif', padding: '20px'}}>
{renderRootTypeSelector()}
{renderArrayItemTypeSelector()}

<SchemaObject
schema={schemaObject.type === 'array' ? schemaObject.items : schemaObject}
onSchemaChange={(newSchema: any) => handleSchemaChange(newSchema)}
path={schemaObject.type === 'array' ? 'items' : ''}
level={0}
/>
<div
className="visual-editor"
style={{
width: "45vw",
minWidth: "550px",
background: "#0F172A",
color: "#CBD5E1",
fontFamily: "Inter, sans-serif",
padding: "20px",
}}
>
<div className="flex items-center gap-2">
{renderRootTypeDisplay()}
{renderArrayItemTypeDisplay()}
<DropdownMenu
trigger={
<button>
<IoIosArrowDropdown />
</button>
}
items={rootTypeOptions}
/>
{schemaObject.type === "array" && (
<DropdownMenu
trigger={
<button>
<IoIosArrowDropdown />
</button>
}
items={itemTypeOptions}
/>
)}
</div>
{(schemaObject.type === "object" || schemaObject.type === "array") && (
<SchemaObject
schema={
schemaObject.type === "array" ? schemaObject.items : schemaObject
}
onSchemaChange={(newSchema: any) => handleSchemaChange(newSchema)}
path={schemaObject.type === "array" ? "items" : ""}
level={0}
/>
)}
</div>
);
};
Expand Down
3 changes: 1 addition & 2 deletions packages/ui/components/VisualEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface CodeEditorProps {
}

export const CodeEditor: React.FC<CodeEditorProps> = ({ schema, onSchemaChange }) => {
const [value, setValue] = useState<string>(schema);
const [value, setValue] = useState(schema);
const [error, setError] = useState<string>('');

useEffect(() => {
Expand Down Expand Up @@ -37,7 +37,6 @@ export const CodeEditor: React.FC<CodeEditorProps> = ({ schema, onSchemaChange }

return (
<div className="code-editor">
<h2>Code Editor</h2>
{error && <p style={{ color: 'red' }}>Error: {error}</p>}
<textarea
value={value}
Expand Down
Loading
Loading