Skip to content

Commit

Permalink
chore(website): [playground] add copy as json and simplify ast viewer (
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Apr 2, 2023
1 parent 7ca2c90 commit 0d8d9f2
Show file tree
Hide file tree
Showing 31 changed files with 1,054 additions and 1,134 deletions.
42 changes: 0 additions & 42 deletions packages/website/src/components/ASTViewerESTree.tsx

This file was deleted.

22 changes: 0 additions & 22 deletions packages/website/src/components/ASTViewerScope.tsx

This file was deleted.

71 changes: 0 additions & 71 deletions packages/website/src/components/ASTViewerTS.tsx

This file was deleted.

81 changes: 27 additions & 54 deletions packages/website/src/components/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import type { TSESTree } from '@typescript-eslint/utils';
import clsx from 'clsx';
import type * as ESQuery from 'esquery';
import type Monaco from 'monaco-editor';
import React, { useCallback, useReducer, useState } from 'react';
import React, { useCallback, useState } from 'react';
import type { SourceFile } from 'typescript';

import { useMediaQuery } from '../hooks/useMediaQuery';
import ASTViewerESTree from './ASTViewerESTree';
import ASTViewerScope from './ASTViewerScope';
import ASTViewerTS from './ASTViewerTS';
import ASTViewer from './ast/ASTViewer';
import { detailTabs } from './config';
import ConfigEslint from './config/ConfigEslint';
import ConfigTypeScript from './config/ConfigTypeScript';
Expand All @@ -20,7 +16,6 @@ import { ESQueryFilter } from './ESQueryFilter';
import useHashState from './hooks/useHashState';
import EditorTabs from './layout/EditorTabs';
import Loader from './layout/Loader';
import { shallowEqual } from './lib/shallowEqual';
import OptionsSelector from './OptionsSelector';
import styles from './Playground.module.css';
import ConditionalSplitPane from './SplitPane/ConditionalSplitPane';
Expand All @@ -32,22 +27,6 @@ import type {
TabType,
} from './types';

function rangeReducer<T extends SelectedRange | null>(
prevState: T,
action: T,
): T {
if (prevState !== action) {
if (
!prevState ||
!action ||
!shallowEqual(prevState.start, action.start) ||
!shallowEqual(prevState.end, action.end)
) {
return action;
}
}
return prevState;
}
function Playground(): JSX.Element {
const [state, setState] = useHashState({
jsx: false,
Expand All @@ -65,13 +44,12 @@ function Playground(): JSX.Element {
const [ruleNames, setRuleNames] = useState<RuleDetails[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [tsVersions, setTSVersion] = useState<readonly string[]>([]);
const [selectedRange, setSelectedRange] = useReducer(rangeReducer, null);
const [position, setPosition] = useState<Monaco.Position | null>(null);
const [selectedRange, setSelectedRange] = useState<SelectedRange>();
const [position, setPosition] = useState<number>();
const [activeTab, setTab] = useState<TabType>('code');
const [showModal, setShowModal] = useState<TabType | false>(false);
const [esQueryFilter, setEsQueryFilter] = useState<ESQuery.Selector>();
const [esQueryError, setEsQueryError] = useState<Error>();
const enableSplitPanes = useMediaQuery('(min-width: 996px)');

const updateModal = useCallback(
(config?: Partial<ConfigModel>) => {
Expand All @@ -92,6 +70,15 @@ function Playground(): JSX.Element {
[],
);

const astToShow =
state.showAST === 'ts'
? tsAst
: state.showAST === 'scope'
? scope
: state.showAST === 'es'
? esAst
: undefined;

return (
<div className={styles.codeContainer}>
{ruleNames.length > 0 && (
Expand All @@ -109,7 +96,6 @@ function Playground(): JSX.Element {
/>
<div className={styles.codeBlocks}>
<ConditionalSplitPane
render={enableSplitPanes}
split="vertical"
minSize="10%"
defaultSize="20rem"
Expand All @@ -126,7 +112,6 @@ function Playground(): JSX.Element {
/>
</div>
<ConditionalSplitPane
render={enableSplitPanes}
split="vertical"
minSize="10%"
defaultSize="50%"
Expand Down Expand Up @@ -157,7 +142,7 @@ function Playground(): JSX.Element {
onTsASTChange={setTsAST}
onScopeChange={setScope}
onMarkersChange={setMarkers}
decoration={selectedRange}
selectedRange={selectedRange}
onChange={setState}
onLoaded={onLoaded}
onSelect={setPosition}
Expand All @@ -178,33 +163,21 @@ function Playground(): JSX.Element {
)}
</div>

{(state.showAST === 'ts' && tsAst && (
<ASTViewerTS
value={tsAst}
position={position}
onSelectNode={setSelectedRange}
{(state.showAST === 'es' && esQueryError && (
<ErrorViewer
type="warning"
title="Invalid Selector"
value={esQueryError}
/>
)) ||
(state.showAST === 'scope' && scope && (
<ASTViewerScope
value={scope}
position={position}
onSelectNode={setSelectedRange}
/>
)) ||
(state.showAST === 'es' && esQueryError && (
<ErrorViewer
type="warning"
title="Invalid Selector"
value={esQueryError}
/>
)) ||
(state.showAST === 'es' && esAst && (
<ASTViewerESTree
value={esAst}
position={position}
filter={esQueryFilter}
onSelectNode={setSelectedRange}
(state.showAST && astToShow && (
<ASTViewer
key={String(state.showAST)}
filter={state.showAST === 'es' ? esQueryFilter : undefined}
value={astToShow}
enableScrolling={true}
cursorPosition={position}
onHoverNode={setSelectedRange}
/>
)) || <ErrorsViewer value={markers} />}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { useWindowSize } from '@docusaurus/theme-common';
import clsx from 'clsx';
import React from 'react';
import SplitPane, { type SplitPaneProps } from 'react-split-pane';

import splitPaneStyles from './SplitPane.module.css';

export interface ConditionalSplitPaneProps {
render: boolean;
}

function ConditionalSplitPane({
render,
children,
...props
}: ConditionalSplitPaneProps & SplitPaneProps): JSX.Element {
return render ? (
}: SplitPaneProps): JSX.Element {
const windowSize = useWindowSize();

return windowSize !== 'mobile' ? (
<SplitPane
resizerClassName={clsx(splitPaneStyles.resizer, splitPaneStyles.vertical)}
{...props}
Expand Down
24 changes: 24 additions & 0 deletions packages/website/src/components/ast/ASTViewer.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
line-height: 18px;
letter-spacing: 0;
font-feature-settings: 'liga' 0, 'calt' 0;
position: relative;
min-width: 100%;
}

.list * {
vertical-align: top;
}

.list,
Expand Down Expand Up @@ -47,6 +53,20 @@
color: var(--token-color-class-name);
}

.propNumber,
.propEmpty,
.propRegExp,
.propClass,
.propBoolean,
.propError,
.propString {
display: inline-block;
max-width: 600px;
word-wrap: break-word;
word-break: break-all;
white-space: pre-wrap;
}

.propName {
color: var(--token-color-property);
}
Expand All @@ -71,6 +91,10 @@
color: var(--token-color-boolean);
}

.propError {
color: var(--token-color-deleted);
}

.propString {
color: var(--token-color-string);
}
Expand Down

0 comments on commit 0d8d9f2

Please sign in to comment.