Skip to content

Next #645

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Next #645

Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/foo-bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-editor": major
---

Editor now accepts a `nodeLoader` which handles loading nodes instead of a static lookup
6 changes: 6 additions & 0 deletions .changeset/giant-steaks-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tokens-studio/graph-editor": major
---

Removed `initialGraph` from the editor. This can now be passed through with the Frame.graph option instead

5 changes: 5 additions & 0 deletions .changeset/light-avocados-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": major
---

The engine now allows for async node loading during deserialization using a nodeLoader instead of a static lookup
16 changes: 16 additions & 0 deletions .changeset/wild-cars-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@tokens-studio/graph-editor": major
---

Removes the External Loader from the editor as this will likely be scoped per Frame

You should instead attach your external loader directly to the graph you created when creating a frame

```ts
const graph = new Graph();

graph.externaloLoader = myExternalLoader;

const Frame = new Frame({ graph , ...etc})

```
2 changes: 2 additions & 0 deletions packages/graph-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"format": "npm run format:eslint && npm run format:prettier",
"format:eslint": "eslint . --fix",
"format:prettier": "prettier \"**/*.{tsx,ts,js,md,json}\" --write",
"test": "vitest run",
"test:e2e": "cypress run",
"test:e2e:manual": "cypress open",
"lint": "npm run lint:prettier && npm run lint:eslint",
Expand Down Expand Up @@ -121,6 +122,7 @@
"cypress": "^13.9.0",
"cypress-react-selector": "^3.0.0",
"glob": "^11.0.0",
"happy-dom": "^16.3.0",
"postcss": "^8.4.47",
"postcss-cli": "^11.0.0",
"postcss-css-variables": "^0.19.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/graph-editor/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ In the example above, we use the useRef hook to create a reference to the Editor
## Development

We need to force the cypress react selector to use a version of `resq` that supports 18.2.0. This is done through resolutions and by manually adding it as a dev dependency

## Notes

This project uses [CSS modules](https://github.com/css-modules/css-modules) to handle encapsulating css. We distribute this in the dist with imports to `*.module.css` files still remaining without transformation to the css files. We assume that the implementing system will handle this transformation and export of the mangled css classnames if needed.
33 changes: 19 additions & 14 deletions packages/graph-editor/src/components/commandPalette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import { observer } from 'mobx-react-lite';
import { showNodesCmdPaletteSelector } from '@/redux/selectors/ui.js';
import { useDispatch, useSelector } from 'react-redux';
import { useSelectAddedNodes } from '@/hooks/useSelectAddedNodes.js';
import React from 'react';
import React, { useCallback } from 'react';
import Search from '@tokens-studio/icons/Search.js';
import styles from './index.module.css';

export interface ICommandMenu {
items: DropPanelStore;
handleSelectNewNodeType: (node: NodeRequest) =>
handleSelectNewNodeType: (node: NodeRequest) => Promise<
| {
graphNode: Node;
flowNode: ReactFlowNode;
}
| undefined;
| undefined
>;
}

const CommandItem = observer(
Expand Down Expand Up @@ -80,7 +81,10 @@ const CommandMenuGroup = observer(
},
);

const CommandMenu = ({ items, handleSelectNewNodeType }: ICommandMenu) => {
export const CommandMenu = ({
items,
handleSelectNewNodeType,
}: ICommandMenu) => {
const showNodesCmdPalette = useSelector(showNodesCmdPaletteSelector);
const dispatch = useDispatch();
const cursorPositionRef = React.useRef<{ x: number; y: number }>({
Expand All @@ -91,8 +95,8 @@ const CommandMenu = ({ items, handleSelectNewNodeType }: ICommandMenu) => {
const reactflow = useReactFlow();
const selectAddedNodes = useSelectAddedNodes();

const handleSelectItem = (item) => {
const newNode = handleSelectNewNodeType({
const handleSelectItem = async (item) => {
const newNode = await handleSelectNewNodeType({
position: reactflow.screenToFlowPosition(cursorPositionRef.current),
...item,
});
Expand Down Expand Up @@ -132,13 +136,16 @@ const CommandMenu = ({ items, handleSelectNewNodeType }: ICommandMenu) => {
}, [dispatch.ui, showNodesCmdPalette]);

// Close the menu when Escape key is pressed inside the input
const handleKeyDown = (e) => {
if (e.key === 'Escape') {
e.preventDefault();
const handleKeyDown = useCallback(
(e) => {
if (e.key === 'Escape') {
e.preventDefault();

dispatch.ui.setShowNodesCmdPalette(false);
}
};
dispatch.ui.setShowNodesCmdPalette(false);
}
},
[dispatch.ui],
);

return (
<Command.Dialog
Expand Down Expand Up @@ -216,5 +223,3 @@ function NodePreview({ title, description, docs }) {
</Stack>
);
}

export { CommandMenu };
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ContextMenuItem } from './ContextMenuStyles.js';
import { Menu, Separator } from 'react-contexify';
import { clear } from '../../editor/actions/clear.js';
import { showGrid, snapGrid } from '@/redux/selectors/settings.js';
import { useAction } from '@/editor/actions/provider.js';
import { useAutoLayout } from '../../editor/hooks/useAutolayout.js';
import { useDispatch } from '@/hooks/index.js';
import { useFrame } from '@/system/frame/hook.js';
import { useLocalGraph } from '@/context/graph.js';
import { useReactFlow } from 'reactflow';
import { useSelector } from 'react-redux';
import React, { useCallback } from 'react';

export interface IPaneContextMenu<T = unknown> {
Expand All @@ -17,8 +16,8 @@ export interface IPaneContextMenu<T = unknown> {

export const PaneContextMenu = <T = unknown,>({ id }: IPaneContextMenu<T>) => {
const reactFlowInstance = useReactFlow();
const showGridValue = useSelector(showGrid);
const snapGridValue = useSelector(snapGrid);

const frame = useFrame();
const dispatch = useDispatch();
const graph = useLocalGraph();
const createNode = useAction('createNode');
Expand Down Expand Up @@ -51,12 +50,12 @@ export const PaneContextMenu = <T = unknown,>({ id }: IPaneContextMenu<T>) => {
}, [reactFlowInstance]);

const setShowGrid = useCallback(() => {
dispatch.settings.setShowGrid(!showGridValue);
}, [dispatch.settings, showGridValue]);
frame.settings.setShowGrid(!frame.settings.showGrid);
}, [frame.settings]);

const setSnapGrid = useCallback(() => {
dispatch.settings.setSnapGrid(!snapGridValue);
}, [dispatch.settings, snapGridValue]);
frame.settings.setSnapGrid(!frame.settings.snapGrid);
}, [frame.settings]);

const clearCallback = useCallback(() => {
clear(reactFlowInstance, graph);
Expand All @@ -70,7 +69,7 @@ export const PaneContextMenu = <T = unknown,>({ id }: IPaneContextMenu<T>) => {
<Separator />
<ContextMenuItem onClick={layout}>Apply Layout</ContextMenuItem>
<ContextMenuItem onClick={setShowGrid}>
{showGridValue ? 'Hide' : 'Show'} Grid
{frame.settings.showGrid ? 'Hide' : 'Show'} Grid
</ContextMenuItem>
<ContextMenuItem onClick={recenter}>Recenter</ContextMenuItem>
<Separator />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const SelectionContextMenu = ({ id, nodes }: INodeContextMenuProps) => {
});
}, [nodes, reactFlowInstance, selectedNodeIds, store]);

const onCreateSubgraph = useCallback(() => {
const onCreateSubgraph = useCallback(async () => {
//We need to work out which nodes do not have parents in the selection

const lookup = new Set(selectedNodeIds);
Expand All @@ -96,7 +96,7 @@ export const SelectionContextMenu = ({ id, nodes }: INodeContextMenuProps) => {
y: position.y / selectedNodes.length,
};

const nodes = createNode({
const nodes = await createNode({
type: 'studio.tokens.generic.subgraph',
position: finalPosition,
});
Expand Down
19 changes: 8 additions & 11 deletions packages/graph-editor/src/components/controls/array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import {
import { ColorPickerPopover } from '../colorPicker/index.js';
import { IField } from './interface.js';
import { JSONTree } from 'react-json-tree';
import { delayedUpdateSelector } from '@/redux/selectors/index.js';
import { observer } from 'mobx-react-lite';
import { toJS } from 'mobx';
import { useSelector } from 'react-redux';
import FloppyDisk from '@tokens-studio/icons/FloppyDisk.js';
import Minus from '@tokens-studio/icons/Minus.js';
import Plus from '@tokens-studio/icons/Plus.js';
Expand All @@ -40,7 +38,7 @@ const NEW_ITEM_DEFAULTS = {
},
};

export const ArrayField = observer(({ port, readOnly }: IField) => {
export const ArrayField = observer(({ port, readOnly, settings }: IField) => {
const [value, setValue] = React.useState(port.value);
const [selectItemsType, setSelectItemsType] = React.useState(
port.type.items.$id,
Expand All @@ -49,7 +47,6 @@ export const ArrayField = observer(({ port, readOnly }: IField) => {
const [autofocusIndex, setAutofocusIndex] = React.useState<number | null>(
null,
);
const useDelayed = useSelector(delayedUpdateSelector);

React.useEffect(() => {
setValue(port.value);
Expand Down Expand Up @@ -82,13 +79,13 @@ export const ArrayField = observer(({ port, readOnly }: IField) => {

setValue(newValueArray);

if (useDelayed) {
if (settings.delayedUpdate) {
return;
}

(port as Input).setValue(newValueArray);
},
[port, useDelayed, value],
[port, settings.delayedUpdate, value],
);

const onColorChange = useCallback(
Expand All @@ -106,12 +103,12 @@ export const ArrayField = observer(({ port, readOnly }: IField) => {

setAutofocusIndex(newValueArray.length - 1);

if (useDelayed) {
if (settings.delayedUpdate) {
return;
}

(port as Input).setValue(newValueArray);
}, [itemsType, port, useDelayed, value]);
}, [itemsType, port, settings.delayedUpdate, value]);

const removeItem = useCallback(
(index: number) => {
Expand All @@ -121,13 +118,13 @@ export const ArrayField = observer(({ port, readOnly }: IField) => {
setValue(newValueArray);
setAutofocusIndex(null);

if (useDelayed) {
if (settings.delayedUpdate) {
return;
}

(port as Input).setValue(newValueArray);
},
[port, useDelayed, value],
[port, settings.delayedUpdate, value],
);

const itemList = React.useMemo(() => {
Expand Down Expand Up @@ -255,7 +252,7 @@ export const ArrayField = observer(({ port, readOnly }: IField) => {
onClick={addItem}
/>
</Stack>
{useDelayed && (
{settings.delayedUpdate && (
<Stack justify="end">
<IconButton
icon={<FloppyDisk />}
Expand Down
11 changes: 4 additions & 7 deletions packages/graph-editor/src/components/controls/color.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { ColorPickerPopover } from '../colorPicker/index.js';
import { IField } from './interface.js';
import { IconButton, Stack, Text } from '@tokens-studio/ui';
import { Input, hexToColor, toColor, toHex } from '@tokens-studio/graph-engine';
import { delayedUpdateSelector } from '@/redux/selectors/index.js';
import { observer } from 'mobx-react-lite';
import { useSelector } from 'react-redux';
import FloppyDisk from '@tokens-studio/icons/FloppyDisk.js';
import React, { useCallback } from 'react';
import styles from './color.module.css';

export const ColorField = observer(({ port, readOnly }: IField) => {
const useDelayed = useSelector(delayedUpdateSelector);
export const ColorField = observer(({ port, readOnly, settings }: IField) => {
const [val, setVal] = React.useState('');

React.useEffect(() => {
Expand All @@ -33,14 +30,14 @@ export const ColorField = observer(({ port, readOnly }: IField) => {
col = e.target.value;
}
setVal(col);
if (useDelayed) {
if (settings.delayedUpdate) {
return;
}

//We need to convert from hex
(port as Input).setValue(hexToColor(col));
},
[port, useDelayed],
[port, settings.delayedUpdate],
);

if (readOnly) {
Expand All @@ -62,7 +59,7 @@ export const ColorField = observer(({ port, readOnly }: IField) => {
<Stack direction="row" justify="between" align="center" gap={2}>
<ColorPickerPopover value={val} onChange={onChange} />
<Text muted>{val}</Text>
{useDelayed && (
{settings.delayedUpdate && (
<IconButton
icon={<FloppyDisk />}
onClick={() => (port as Input).setValue(val)}
Expand Down
38 changes: 20 additions & 18 deletions packages/graph-editor/src/components/controls/curve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import { Input } from '@tokens-studio/graph-engine';
import { observer } from 'mobx-react-lite';
import React from 'react';

export const CurveField = observer(({ port, readOnly }: IField) => {
const onChange = (index: number, value: number[]) => {
if (!readOnly) {
const points = [...port.value.curves[0].points];
points[index] = value;
(port as Input).setValue({
curves: [
{
points,
},
],
});
}
};
return (
<CurveEditor points={port.value.curves[0].points} onChange={onChange} />
);
});
export const CurveField = observer(
({ port, readOnly }: Omit<IField, 'settings'>) => {
const onChange = (index: number, value: number[]) => {
if (!readOnly) {
const points = [...port.value.curves[0].points];
points[index] = value;
(port as Input).setValue({
curves: [
{
points,
},
],
});
}
};
return (
<CurveEditor points={port.value.curves[0].points} onChange={onChange} />
);
},
);
2 changes: 2 additions & 0 deletions packages/graph-editor/src/components/controls/interface.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Port } from '@tokens-studio/graph-engine';
import { SystemSettings } from '@/system/frame/settings.js';
export interface IField {
port: Port;
readOnly?: boolean;
settings: SystemSettings;
}
Loading
Loading