Skip to content

Commit afbd093

Browse files
Merge pull request #145 from gridaco/editor-console-feed
Editor console feed
2 parents 4b5052c + 5c338dd commit afbd093

File tree

50 files changed

+800
-310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+800
-310
lines changed

editor-packages/editor-debugger/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./window-console-feed";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useEffect, useState } from "react";
2+
import { Console, Hook, Unhook } from "@code-editor/console-feed";
3+
4+
export function WindowConsoleFeed({ style }: { style?: React.CSSProperties }) {
5+
const [logs, setLogs] = useState([]);
6+
7+
useEffect(() => {
8+
// run once
9+
Hook(
10+
window.console,
11+
(log) => setLogs((currLogs) => [...currLogs, log]),
12+
false
13+
);
14+
return () => {
15+
Unhook(window.console as any);
16+
};
17+
}, []);
18+
19+
return (
20+
<div style={style}>
21+
<Console logs={logs} variant="dark" />
22+
</div>
23+
);
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./tab";
2+
export * from "./tab-badge";
3+
export * from "./console-feed";
4+
export * from "./visualization";
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from "react";
2+
import styled from "@emotion/styled";
3+
4+
const bgcolortypemap = {
5+
default: "rgba(255, 255, 255, 0.1)",
6+
warning: "rgba(255, 230, 0, 0.1)",
7+
error: "rgba(255, 0, 0, 0.1)",
8+
};
9+
10+
export function TabBadge({
11+
type = "default",
12+
value,
13+
}: {
14+
type?: "default" | "warning" | "error";
15+
value: string | number;
16+
}) {
17+
const background = bgcolortypemap[type];
18+
19+
if (value === undefined || value === null) {
20+
return <></>;
21+
}
22+
23+
return (
24+
<BaseDevtoolsTabBadge background={background}>
25+
<Value>{value}</Value>
26+
</BaseDevtoolsTabBadge>
27+
);
28+
}
29+
30+
const Value = styled.span`
31+
color: rgb(151, 151, 151);
32+
text-overflow: ellipsis;
33+
font-size: 10px;
34+
font-family: Inter, sans-serif;
35+
font-weight: 400;
36+
text-align: center;
37+
`;
38+
39+
const BaseDevtoolsTabBadge = styled.div<{ background: string }>`
40+
display: flex;
41+
justify-content: center;
42+
flex-direction: column;
43+
align-items: center;
44+
flex: none;
45+
gap: 10px;
46+
border-radius: 50%;
47+
width: 18px;
48+
height: 18px;
49+
background-color: ${(p) => p.background};
50+
box-sizing: border-box;
51+
padding: 10px;
52+
`;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from "react";
2+
import styled from "@emotion/styled";
3+
import { TabBadge } from "../tab-badge";
4+
5+
export function DevtoolsTab({
6+
label,
7+
badge,
8+
selected,
9+
onTap,
10+
}: {
11+
selected?: boolean;
12+
label: string;
13+
badge?: string | number;
14+
onTap?: () => void;
15+
}) {
16+
return (
17+
<TabBase onClick={onTap}>
18+
<Label data-selected={selected}>{label}</Label>
19+
<TabBadge value={badge} />
20+
</TabBase>
21+
);
22+
}
23+
24+
const TabBase = styled.div`
25+
cursor: pointer;
26+
user-select: none;
27+
display: flex;
28+
justify-content: flex-start;
29+
flex-direction: row;
30+
align-items: center;
31+
gap: 8px;
32+
box-sizing: border-box;
33+
`;
34+
35+
const Label = styled.span`
36+
color: rgb(151, 151, 151);
37+
text-overflow: ellipsis;
38+
font-size: 12px;
39+
font-family: Inter, sans-serif;
40+
font-weight: 400;
41+
text-align: left;
42+
43+
&:hover {
44+
color: white;
45+
}
46+
47+
&[data-selected="true"] {
48+
color: white;
49+
}
50+
`;

editor-packages/editor-debugger/debugger-panel.tsx renamed to editor-packages/editor-devtools/debugger-panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from "react";
22
import styled from "@emotion/styled";
33
import { useRouter } from "next/router";
44
import { ClearRemoteDesignSessionCache } from "components/clear-remote-design-session-cache";
5-
import { WidgetTree } from "@code-editor/debugger/components/visualization/json-visualization/json-tree";
5+
import { WidgetTree } from "@code-editor/devtools/components/visualization/json-visualization/json-tree";
66
import Link from "next/link";
77

88
export const Debugger = ({
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./debugger-panel";
2+
export * from "./components";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@code-editor/devtools",
3+
"version": "0.0.0",
4+
"private": false,
5+
"dependencies": {
6+
"@code-editor/console-feed": "^3.3.1"
7+
}
8+
}

editor-packages/editor-preview-pip/lib/resizable-pip.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import React from "react";
22
import PIP from "./pip";
33
import { ResizableBox } from "react-resizable";
44
import "react-resizable/css/styles.css";
5-
import type { ResizableBoxProps as RawResizableBoxProps } from "react-resizable";
5+
import type {
6+
ResizableBoxProps as RawResizableBoxProps,
7+
ResizableProps,
8+
} from "react-resizable";
69
import styled from "@emotion/styled";
710

811
interface ResizableBoxProps
@@ -17,7 +20,7 @@ interface ResizableBoxProps
1720
* resize handle to display - a react component
1821
* @default none
1922
*/
20-
resizeHandle?: React.ReactNode;
23+
resizeHandle?: ResizableProps["handle"];
2124
/**
2225
* @default 500
2326
*/

editor-packages/editor-services-esbuild/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const bundler = async (rawCode: string, lang: Loader) => {
4444

4545
return { code: result.outputFiles[0].text, err: null };
4646
} catch (error: any) {
47-
console.error("esbuild error: ", error);
4847
return {
4948
code: null,
5049
err: { method: "error", data: [error.message], id: nanoid() },

editor/components/app-runner/vanilla-esbuild-app-runner.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useCallback, useEffect, useRef, useState } from "react";
22
import { VanillaRunner } from "components/app-runner/vanilla-app-runner";
3+
import { useDispatch } from "core/dispatch";
34

45
export function VanillaESBuildAppRunner({
56
doc,
@@ -13,6 +14,17 @@ export function VanillaESBuildAppRunner({
1314
};
1415
}) {
1516
const ref = useRef<HTMLIFrameElement>();
17+
const dispatch = useDispatch();
18+
19+
const consoleLog = useCallback(
20+
(p: { method; data }) => {
21+
dispatch({
22+
type: "devtools-console",
23+
log: p,
24+
});
25+
},
26+
[dispatch]
27+
);
1628

1729
const loadCode = useCallback(
1830
(e: HTMLIFrameElement) => {
@@ -29,6 +41,22 @@ export function VanillaESBuildAppRunner({
2941
}
3042
}, [doc?.html, doc?.css, doc?.javascript]);
3143

44+
useEffect(() => {
45+
const handler = (event: any) => {
46+
if (event.data.type === "console") {
47+
console[event.data.method](JSON.parse(event.data.data).join(" "));
48+
consoleLog({
49+
method: event.data.method,
50+
data: JSON.parse(event.data.data),
51+
});
52+
}
53+
};
54+
55+
window.addEventListener("message", handler);
56+
57+
return () => window.removeEventListener("message", handler);
58+
}, []);
59+
3260
return (
3361
<VanillaRunner
3462
ref={ref}

editor/components/canvas/controller-zoom-control.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import styled from "@emotion/styled";
3-
import RefreshSharpIcon from "@material-ui/icons/RefreshSharp";
3+
import RefreshSharpIcon from "@mui/icons-material/RefreshSharp";
44
import { colors } from "theme";
55

66
export function ZoomControl({

editor/components/code-editor/code-editor.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from "react";
22
import { MonacoEditor, MonacoEditorProps as MonacoEditorProps } from "./monaco";
3-
import { Tabs, Tab } from "@material-ui/core";
3+
import { Tabs, Tab } from "@mui/material";
44

55
export interface CodeEditorProps
66
extends Omit<MonacoEditorProps, "defaultValue" | "defaultLanguage"> {}
@@ -35,8 +35,10 @@ export function CodeEditor({
3535
<Tabs
3636
value={filekey}
3737
onChange={handleChange}
38+
indicatorColor="primary"
39+
textColor="inherit"
3840
variant="scrollable"
39-
scrollButtons="off"
41+
scrollButtons={false}
4042
style={{ color: "white" }}
4143
aria-label="scrollable prevent tabs example"
4244
>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useState } from "react";
22
import styled from "@emotion/styled";
3-
import TreeView from "@material-ui/lab/TreeView";
4-
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
5-
import ChevronRightIcon from "@material-ui/icons/ChevronRight";
6-
import TreeItem from "@material-ui/lab/TreeItem";
3+
import TreeView from "@mui/lab/TreeView";
4+
import TreeItem from "@mui/lab/TreeItem";
5+
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
6+
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
77
import { SideNavigation } from "components/side-navigation";
88

99
interface LayerTree {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import styled from "@emotion/styled";
3-
import { ArrowBack } from "@material-ui/icons";
3+
import { ArrowBack } from "@mui/icons-material";
44
import { useRouter } from "next/router";
55
import { colors } from "theme";
66
import ClientOnly from "components/client-only";

editor/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export * as figmacomp from "./figma";
22
export * as canvas from "./design-preview-as-is";
33
export * as code from "./code-editor";
44
export * as runner from "./app-runner";
5-
export * as visualization from "../../editor-packages/editor-debugger/components/visualization";
5+
export * as visualization from "../../editor-packages/editor-devtools/components/visualization";
66
export * from "./client-only";

editor/core/actions/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { FrameworkConfig } from "@designto/config";
2-
import type { EditorState, ScenePreviewData } from "core/states";
2+
import type { ConsoleLog, EditorState, ScenePreviewData } from "core/states";
33

44
export type WorkspaceAction =
55
//
@@ -20,7 +20,8 @@ export type Action =
2020
| HighlightLayerAction
2121
| CanvasModeAction
2222
| PreviewAction
23-
| CodeEditorAction;
23+
| CodeEditorAction
24+
| DevtoolsAction;
2425

2526
export type ActionType = Action["type"];
2627

@@ -74,3 +75,13 @@ export interface CodeEditorEditComponentCodeAction {
7475
componentName: string;
7576
raw: string;
7677
}
78+
79+
export type DevtoolsAction = DevtoolsConsoleAction | DevtoolsConsoleClearAction;
80+
export interface DevtoolsConsoleAction {
81+
type: "devtools-console";
82+
log: ConsoleLog;
83+
}
84+
85+
export interface DevtoolsConsoleClearAction {
86+
type: "devtools-console-clear";
87+
}

editor/core/reducers/editor-reducer.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
CanvasModeGobackAction,
99
PreviewBuildingStateUpdateAction,
1010
PreviewSetAction,
11+
DevtoolsConsoleAction,
12+
DevtoolsConsoleClearAction,
1113
} from "core/actions";
1214
import { EditorState } from "core/states";
1315
import { useRouter } from "next/router";
@@ -153,6 +155,35 @@ export function editorReducer(state: EditorState, action: Action): EditorState {
153155
draft.currentPreview = data; // set
154156
});
155157
}
158+
case "devtools-console": {
159+
const { log } = <DevtoolsConsoleAction>action;
160+
return produce(state, (draft) => {
161+
if (!draft.devtoolsConsole?.logs?.length) {
162+
draft.devtoolsConsole = { logs: [] };
163+
}
164+
165+
const logs = Array.from(state.devtoolsConsole?.logs ?? []);
166+
logs.push(log);
167+
168+
draft.devtoolsConsole.logs = logs;
169+
});
170+
break;
171+
}
172+
case "devtools-console-clear": {
173+
const {} = <DevtoolsConsoleClearAction>action;
174+
return produce(state, (draft) => {
175+
if (draft.devtoolsConsole?.logs?.length) {
176+
draft.devtoolsConsole.logs = [
177+
{
178+
id: "clear",
179+
method: "info",
180+
data: ["Console was cleared"],
181+
},
182+
];
183+
}
184+
});
185+
break;
186+
}
156187
default:
157188
throw new Error(`Unhandled action type: ${action["type"]}`);
158189
}

0 commit comments

Comments
 (0)