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

added code for sandpack console #4672

Merged
merged 30 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6f53fe2
added code for sandpack console
harish-sethuraman May 22, 2022
8efd337
add log
harish-sethuraman May 22, 2022
d26bf0a
added console for older bundle
harish-sethuraman May 22, 2022
68afb83
Revert "[beta] Sandpack - new bundler (#4458)"
harish-sethuraman May 22, 2022
e9c61b8
adds proper console and removes new bundle
harish-sethuraman May 22, 2022
f992e96
modify styles
harish-sethuraman May 22, 2022
8ab57d8
remove unwanted code
harish-sethuraman May 22, 2022
63a812a
nit
harish-sethuraman May 22, 2022
66eeacb
fix types (#4677)
danilowoz May 24, 2022
878755c
update console
harish-sethuraman May 24, 2022
894325c
Merge branch 'main' into try-sandpack-console
gaearon May 24, 2022
877b36a
little nits
harish-sethuraman May 25, 2022
6d2f067
remove unwanted code changes
harish-sethuraman May 25, 2022
abee2a6
update bundler URL
harish-sethuraman May 31, 2022
0f75b8e
use `message.firstLoad` for clearing console
harish-sethuraman Jun 1, 2022
f2a5fd9
use `refresh` event to clear logs as well (used when going away and c…
harish-sethuraman Jun 1, 2022
da01af3
remove padding for code blocks inside console
harish-sethuraman Jun 3, 2022
038b353
Merge branch 'main' into try-sandpack-console
harish-sethuraman Jun 3, 2022
6fbd737
small UI revamps
harish-sethuraman Jun 6, 2022
094b646
change p to div since the sandpack comes inside the p, add try catch …
harish-sethuraman Jun 6, 2022
a8fa116
tweaks
gaearon Jun 10, 2022
05db819
Fixes
gaearon Jun 10, 2022
dbf4b33
Merge branch 'main' into try-sandpack-console
gaearon Jun 10, 2022
48a0667
Reset unrelated changes
gaearon Jun 10, 2022
1a98616
tweaks
gaearon Jun 10, 2022
086b30b
fix
gaearon Jun 10, 2022
a1fc684
fixes
gaearon Jun 10, 2022
0344610
oops
gaearon Jun 10, 2022
eaa0a10
Fix
gaearon Jun 10, 2022
665e455
fix
gaearon Jun 10, 2022
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
154 changes: 154 additions & 0 deletions beta/src/components/MDX/Sandpack/Console.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import cn from 'classnames';
import * as React from 'react';
import {IconChevron} from 'components/Icon/IconChevron';

import {SandpackCodeViewer, useSandpack} from '@codesandbox/sandpack-react';
import type {SandpackMessageConsoleMethods} from '@codesandbox/sandpack-client';

const getType = (
message: SandpackMessageConsoleMethods
): 'info' | 'warning' | 'error' => {
if (message === 'log' || message === 'info') {
return 'info';
}

if (message === 'warn') {
return 'warning';
}

return 'error';
};

type ConsoleData = Array<{
data: Array<string | Record<string, string>>;
id: string;
method: SandpackMessageConsoleMethods;
}>;

const MAX_MESSAGE_COUNT = 100;

export const SandpackConsole = () => {
const {listen} = useSandpack();
const [logs, setLogs] = React.useState<ConsoleData>([]);
const wrapperRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
const unsubscribe = listen((message) => {
if (
(message.type === 'start' && message.firstLoad) ||
message.type === 'refresh'
) {
setLogs([]);
}
if (message.type === 'console' && message.codesandbox) {
setLogs((prev) => {
const messages = [...prev, ...message.log];
messages.slice(Math.max(0, messages.length - MAX_MESSAGE_COUNT));

return messages.filter(({method}) => method === 'log');
});
}
});

return unsubscribe;
}, [listen]);

const [isExpanded, setIsExpanded] = React.useState(false);

React.useEffect(() => {
if (wrapperRef.current) {
wrapperRef.current.scrollTop = wrapperRef.current.scrollHeight;
}
}, [logs]);

if (logs.length === 0) {
return null;
}

return (
<div className="absolute dark:border-gray-700 bg-white dark:bg-gray-95 border-t bottom-0 w-full dark:text-white">
<div className="flex justify-between">
<button
className="flex items-center p-1"
onClick={() => setIsExpanded(!isExpanded)}>
<IconChevron displayDirection={isExpanded ? 'down' : 'right'} />
<span className="pl-1 text-sm">Console ({logs.length})</span>
</button>
<button
className="p-1"
onClick={() => {
setLogs([]);
setIsExpanded(false);
}}>
<svg
viewBox="0 0 24 24"
width="18"
height="18"
stroke="currentColor"
strokeWidth="2"
fill="none"
strokeLinecap="round"
strokeLinejoin="round">
<circle cx="12" cy="12" r="10"></circle>
<line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line>
</svg>
</button>
</div>
{isExpanded && (
<div className="w-full h-full border-y bg-white dark:border-gray-700 dark:bg-gray-95 min-h-[28px] console">
<div className="max-h-52 h-auto overflow-auto" ref={wrapperRef}>
{logs.map(({data, id, method}) => {
return (
<div
key={id}
className={cn(
'last:border-none border-b dark:border-gray-700 text-md p-1 pl-2 leading-6 font-mono',
`console-${getType(method)}`
)}>
<span className={cn('console-message')}>
{data.map((msg, index) => {
if (typeof msg === 'string') {
return <span key={`${msg}-${index}`}>{msg}</span>;
}

let children;
if (msg != null && typeof msg['@t'] === 'string') {
// CodeSandbox wraps custom types
children = msg['@t'];
} else {
try {
children = JSON.stringify(msg, null, 2);
} catch (error) {
try {
children = Object.prototype.toString.call(msg);
} catch (err) {
children = '[' + typeof msg + ']';
}
}
}

return (
<span
className={cn('console-span')}
key={`${msg}-${index}`}>
<SandpackCodeViewer
initMode="user-visible"
// fileType="js"
code={children}
/>
</span>
);
})}
</span>
</div>
);
})}
</div>
</div>
)}
</div>
);
};
2 changes: 2 additions & 0 deletions beta/src/components/MDX/Sandpack/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as React from 'react';
import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react';
import cn from 'classnames';
import {Error} from './Error';
import {SandpackConsole} from './Console';
import type {LintDiagnostic} from './useSandpackLint';

const generateRandomId = (): string =>
Expand Down Expand Up @@ -209,6 +210,7 @@ export function Preview({
loading={!isReady && iframeComputedHeight === null}
/>
</div>
<SandpackConsole />
</div>
);
}
7 changes: 7 additions & 0 deletions beta/src/styles/sandpack.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ html.dark .sp-code-editor .cm-diagnostic {
overflow: auto !important;
padding: 18px 0 !important;
}

.console .sp-cm,
.console .sp-cm .cm-scroller,
.console .sp-cm .cm-line {
padding: 0px !important;
}

.sp-cm .cm-gutters {
background-color: var(--sp-colors-bg-default);
z-index: 1;
Expand Down