Skip to content

Commit 283d1fb

Browse files
Merge pull request #96 from gridaco/staging
Dec #2 - Whole file loading
2 parents e0c619d + 937ee12 commit 283d1fb

20 files changed

Lines changed: 490 additions & 171 deletions

File tree

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
1-
import React from "react";
1+
import React, { useEffect, useRef } from "react";
22

33
export function VanillaRunner({
44
width,
55
height,
66
source,
7+
enableInspector = true,
78
}: {
89
width: string;
910
height: string;
1011
source: string;
1112
componentName: string;
13+
enableInspector?: boolean;
1214
}) {
15+
const ref = useRef<HTMLIFrameElement>();
16+
17+
useEffect(() => {
18+
if (ref.current && enableInspector) {
19+
ref.current.onload = () => {
20+
const matches = ref.current.contentDocument.querySelectorAll(
21+
"div, span, button, img, image, svg"
22+
);
23+
matches.forEach((el) => {
24+
const tint = "rgba(20, 0, 255, 0.2)";
25+
const tintl = "rgba(20, 0, 255, 0.5)";
26+
const originstyle = {
27+
//@ts-ignore
28+
...el.style,
29+
};
30+
31+
if (el.id.includes("RootWrapper")) {
32+
} else {
33+
el.addEventListener("mouseenter", (e) => {
34+
//@ts-ignore
35+
e.target.style.background = tint;
36+
//@ts-ignore
37+
e.target.style.outline = `${tintl} solid 1px`;
38+
});
39+
el.addEventListener("mouseleave", (e) => {
40+
//@ts-ignore
41+
e.target.style.background = originstyle.background;
42+
//@ts-ignore
43+
e.target.style.outline = originstyle.outline;
44+
});
45+
}
46+
});
47+
48+
ref.current.contentWindow.addEventListener("click", (e) => {
49+
console.log("click", e);
50+
});
51+
};
52+
}
53+
}, [ref.current, enableInspector]);
54+
1355
const inlinesource = source || `<div></div>`;
1456
return (
15-
<>
16-
<iframe
17-
sandbox="allow-same-origin"
18-
srcDoc={inlinesource}
19-
width={width}
20-
height={height}
21-
/>
22-
</>
57+
<iframe
58+
ref={ref}
59+
sandbox="allow-same-origin"
60+
srcDoc={inlinesource}
61+
width={width}
62+
height={height}
63+
/>
2364
);
2465
}

editor/components/editor/editor-appbar/editor-appbar-fragment-for-canvas.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function AppbarFragmentForCanvas() {
66
const [state] = useEditorState();
77
return (
88
<RootWrapperAppbarFragmentForCanvas>
9-
<Breadcrumbs>{state.design?.current?.name}</Breadcrumbs>
9+
<Breadcrumbs>{state.design?.input?.name}</Breadcrumbs>
1010
</RootWrapperAppbarFragmentForCanvas>
1111
);
1212
}

editor/components/editor/editor-layer-hierarchy/editor-layer-hierarchy-tree.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ import { useDispatch } from "core/dispatch";
1212
export function EditorLayerHierarchy() {
1313
const [state] = useEditorState();
1414
const dispatch = useDispatch();
15-
const root = state.design?.current?.entry;
16-
const layers = root ? flatten(root, ["origin"]) : [];
15+
const root = state.selectedPage
16+
? state.design.pages.find((p) => p.id == state.selectedPage).children
17+
: [state.design?.input?.entry];
18+
19+
const layers: FlattenedNode[][] = root
20+
? root.filter((l) => !!l).map((layer) => flatten(layer))
21+
: [];
1722

1823
const renderItem = useCallback(
1924
({ id, name, depth, type }) => {
@@ -48,14 +53,14 @@ export function EditorLayerHierarchy() {
4853

4954
const haschildren = useCallback(
5055
(id: string) => {
51-
return layers.some((layer) => layer.parent === id);
56+
return layers.some((l) => l.some((layer) => layer.parent === id));
5257
},
53-
[layers.length]
58+
[layers]
5459
);
5560

5661
return (
5762
<TreeView.Root
58-
data={layers}
63+
data={layers.flat()}
5964
keyExtractor={useCallback((item: any) => item.id, [])}
6065
renderItem={renderItem}
6166
/>
@@ -79,11 +84,14 @@ interface FlattenedNode {
7984

8085
const flatten = <T extends ITreeNode>(
8186
tree: T,
82-
properties?: string[],
8387
parent?: string,
8488
depth: number = 0
8589
): FlattenedNode[] => {
86-
const convert = (node: T, properties, depth: number, parent?: string) => {
90+
const convert = (node: T, depth: number, parent?: string) => {
91+
if (!node) {
92+
return;
93+
}
94+
8795
const result: FlattenedNode = {
8896
id: node.id,
8997
name: node.name,
@@ -92,17 +100,13 @@ const flatten = <T extends ITreeNode>(
92100
parent,
93101
};
94102

95-
properties?.forEach((property) => {
96-
(result as object)[property] = node[property];
97-
});
98-
99103
return result;
100104
};
101105

102106
const final = [];
103-
final.push(convert(tree, properties, depth, parent));
104-
for (const child of tree.children || []) {
105-
final.push(...flatten(child, null, tree.id, depth + 1));
107+
final.push(convert(tree, depth, parent));
108+
for (const child of tree?.children || []) {
109+
final.push(...flatten(child, tree.id, depth + 1));
106110
}
107111
return final;
108112
};
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
export const dummy = "";
1+
import React from "react";
2+
import { FileIcon } from "@radix-ui/react-icons";
3+
import styled from "@emotion/styled";
4+
import { useTheme } from "@emotion/react";
5+
import { TreeView } from "@editor-ui/hierarchy";
6+
7+
export function EditorPageItem({
8+
id,
9+
name,
10+
selected,
11+
onPress,
12+
}: {
13+
id: string;
14+
name: string;
15+
selected: boolean;
16+
onPress: () => void;
17+
}) {
18+
const { icon: iconColor, iconSelected: iconSelectedColor } =
19+
useTheme().colors;
20+
21+
return (
22+
<TreeView.Row
23+
depth={0}
24+
id={id}
25+
key={id}
26+
sortable={false}
27+
selected={selected}
28+
onPress={onPress}
29+
onDoubleClick={() => {}}
30+
icon={<FileIcon color={selected ? iconSelectedColor : iconColor} />}
31+
>
32+
{name}
33+
</TreeView.Row>
34+
);
35+
}
Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
export const dummy = "";
1+
import React, { useCallback } from "react";
2+
import styled from "@emotion/styled";
3+
import { TreeView } from "@editor-ui/hierarchy";
4+
import { EditorPageItem } from "./editor-page-item";
5+
import { useEditorState } from "core/states";
6+
import { useDispatch } from "core/dispatch";
7+
8+
const Container = styled.div<{ expanded: boolean }>(({ theme, expanded }) => ({
9+
...(expanded ? { height: "200px" } : { flex: "0 0 auto" }),
10+
display: "flex",
11+
flexDirection: "column",
12+
}));
13+
14+
type PageInfo = {
15+
id: string;
16+
name: string;
17+
type: "design" | "components" | "styles" | "assets";
18+
};
19+
20+
export function EditorPagesList() {
21+
const [state] = useEditorState();
22+
const dispatch = useDispatch();
23+
const pages = state.design?.pages ?? [];
24+
25+
return (
26+
<Container expanded={true}>
27+
<TreeView.Root
28+
sortable={false}
29+
scrollable={false}
30+
data={pages}
31+
keyExtractor={useCallback((item: PageInfo) => item.id, [])}
32+
renderItem={useCallback(
33+
(page: PageInfo, index) => {
34+
const selected = page.id === state.selectedPage;
35+
return (
36+
<EditorPageItem
37+
key={page.id}
38+
selected={selected}
39+
id={page.id}
40+
name={page.name}
41+
onPress={() => {
42+
dispatch({
43+
type: "select-page",
44+
page: page.id,
45+
});
46+
}}
47+
/>
48+
);
49+
},
50+
[state.selectedPage]
51+
)}
52+
/>
53+
</Container>
54+
);
55+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const dummy = "";
1+
export { EditorPagesList } from "./editor-pages-list";
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import { SideNavigation } from "components/side-navigation";
21
import React from "react";
2+
import styled from "@emotion/styled";
3+
import { SideNavigation } from "components/side-navigation";
34
import { EditorAppbarFragments } from "../editor-appbar";
45
import { EditorLayerHierarchy } from "../editor-layer-hierarchy";
6+
import { EditorPagesList } from "../editor-pages-list";
57

68
export function EditorSidebar() {
79
return (
810
<SideNavigation>
9-
{/* <EditorAppbarFragments.Sidebar /> */}
10-
<EditorLayerHierarchy />
11+
<EditorAppbarFragments.Sidebar />
12+
<SidebarSegment flex={"none"}>
13+
<EditorPagesList />
14+
</SidebarSegment>
15+
<SidebarSegment flex={1}>
16+
<EditorLayerHierarchy />
17+
</SidebarSegment>
1118
</SideNavigation>
1219
);
1320
}
21+
22+
const SidebarSegment = styled.div<{ flex: number | "none" }>`
23+
flex: ${(p) => p.flex};
24+
overflow-y: scroll;
25+
`;

editor/components/side-navigation/side-navigation.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ const Wrapper = styled.div`
1515
align-items: stretch;
1616
flex-direction: column;
1717
min-height: 100%;
18+
height: 100vh;
19+
overflow-y: hidden;
1820
`;

editor/core/reducers/editor-reducer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import produce from "immer";
2-
import { Action, SelectNodeAction } from "core/actions";
2+
import { Action, SelectNodeAction, SelectPageAction } from "core/actions";
33
import { EditorState } from "core/states";
44

55
export function editorReducer(state: EditorState, action: Action): EditorState {
@@ -12,8 +12,13 @@ export function editorReducer(state: EditorState, action: Action): EditorState {
1212
});
1313
}
1414
case "select-page": {
15-
// return pageReducer(state, action);
15+
const { page } = <SelectPageAction>action;
16+
return produce(state, (draft) => {
17+
draft.selectedPage = page;
18+
});
1619
}
20+
default:
21+
throw new Error(`Unhandled action type: ${action["type"]}`);
1722
}
1823

1924
return state;

editor/core/states/editor-state.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ export interface EditorState {
55
selectedPage: string;
66
selectedNodes: string[];
77
selectedLayersOnPreview: string[];
8-
design: ReflectRepository;
8+
design: FigmaReflectRepository;
99
}
1010

1111
export interface EditorSnapshot {
1212
selectedPage: string;
1313
selectedNodes: string[];
1414
selectedLayersOnPreview: string[];
15-
design: ReflectRepository;
15+
design: FigmaReflectRepository;
1616
}
1717

18-
interface ReflectRepository {
18+
interface FigmaReflectRepository {
1919
/**
2020
* fileid; filekey
2121
*/
2222
key: string;
2323

2424
// TODO:
25-
pages: [];
26-
current: DesignInput;
25+
pages: { id: string; name: string; children: ReflectSceneNode[] }[];
26+
input: DesignInput;
2727
}

0 commit comments

Comments
 (0)