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

sort plugin #129

Merged
merged 2 commits into from
Apr 2, 2024
Merged
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
6 changes: 6 additions & 0 deletions example/empty-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { Plugin, PluginKey } from "prosemirror-state";
function createPluginState() {
return {
doSomething() {},
apple: "apple",
cherry: "cherry",
dog: "dog",
elephant: "elephant",
frog: "frog",
banana: "banana",
};
}

Expand Down
31 changes: 31 additions & 0 deletions src/components/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { ReactNode, MouseEventHandler } from "react";
import { css } from "@compiled/react";
import theme from "../theme";

type ButtonProps = {
onClick: MouseEventHandler<HTMLButtonElement>;
children: ReactNode;
};

const buttonStyles = css({
color: theme.softerMain,
marginTop: "4px",
marginBottom: "4px",
fontWeight: 400,
fontSize: "12px",
background: "transparent",
border: "none",
"&:hover": {
background: theme.white10,
},
});

const Button = ({ onClick, children }: ButtonProps) => {
return (
<button css={buttonStyles} onClick={onClick}>
{children}
</button>
);
};

export default Button;
1 change: 1 addition & 0 deletions src/components/json-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type JSONTreeProps = {
valueRenderer?: (value: any) => string | React.ReactNode;
labelRenderer?: (value: any) => string | React.ReactNode;
isCustomNode?: (node: any) => boolean;
sortObjectKeys?: boolean | ((...args: any[]) => any);
};
export default function JSONTreeWrapper(props: JSONTreeProps) {
return (
Expand Down
66 changes: 66 additions & 0 deletions src/components/search-bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useCallback, useState } from "react";
import theme from "../theme";
import { css } from "@compiled/react";

interface SearchBarProps {
onSearch: (query: string) => void;
}

const buttonStyles = css({
color: theme.softerMain,
padding: 6,
fontWeight: 400,
fontSize: "12px",
background: "transparent",
border: "none",
"&:hover": {
background: theme.white10,
},
});

const inputStyles = css({
background: "transparent",
border: "1px solid " + theme.softerMain,
outline: "none",
color: theme.softerMain,
"&::placeholder": { color: theme.white20 },
});

const searchBarWrapperStyles = css({
display: "flex",
gap: "4px",
margin: 4,
});

const SearchBar: React.FC<SearchBarProps> = ({ onSearch }) => {
const [query, setQuery] = useState("");

const handleInputChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value);
onSearch(event.target.value);
},
[onSearch]
);

const handleSearch = useCallback(() => {
onSearch(query);
}, [query, onSearch]);

return (
<div css={searchBarWrapperStyles}>
<input
type="text"
placeholder={"Search here"}
value={query}
onChange={handleInputChange}
css={inputStyles}
/>
<button css={buttonStyles} onClick={handleSearch}>
SEARCH
</button>
</div>
);
};

export default SearchBar;
93 changes: 75 additions & 18 deletions src/tabs/plugins.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from "react";
import React, { useCallback, useState } from "react";
import type { Plugin } from "prosemirror-state";
import { InfoPanel } from "../components/info-panel";
import { Heading } from "../components/heading";
import JSONTree from "../components/json-tree";
import { List } from "../components/list";
import { SplitView, SplitViewCol } from "../components/split-view";
import { atom, useAtom, useAtomValue } from "jotai";
import { useAtomValue } from "jotai";
import { editorStateAtom } from "../state/editor-state";
import SearchBar from "../components/search-bar";
import Button from "../components/button";

export function valueRenderer(raw: string, ...rest: Array<string>) {
if (typeof rest[0] === "function") {
Expand All @@ -19,40 +21,95 @@ export function PluginState(props: { pluginState: any }) {
return (
<div>
<Heading>Plugin State</Heading>
<JSONTree data={props.pluginState} valueRenderer={valueRenderer} />
<JSONTree
data={props.pluginState}
valueRenderer={valueRenderer}
sortObjectKeys={true}
/>
</div>
);
}

const selectedPluginIndexAtom = atom(0);

// TODO: replace isDimmed with useCallback once EditorStateContainer is decomposed
export default function PluginsTab() {
const [selectedIndex, setSelected] = useAtom(selectedPluginIndexAtom);
const isSelected = React.useCallback(
(_plugin, index) => selectedIndex === index,
[selectedIndex]
);
const state = useAtomValue(editorStateAtom);
if (!state) return null;

const [selectedPlugin, setSelectedPlugin] = useState(state.plugins[0]);
const [pluginsLocal, setPluginsLocal] = useState(state.plugins);
const [sortAsc, setSortOrder] = useState(true);

const handleOnListItemClick = React.useCallback(
(_plugin, index) => setSelected(index),
(_plugin) => setSelectedPlugin(_plugin),
[]
);
const state = useAtomValue(editorStateAtom);

if (!state) return null;

const plugins = state.plugins as any as Plugin<any>[];
const selectedPlugin = plugins[selectedIndex];
const selectedPluginState = selectedPlugin.getState(state);

const handleSearch = useCallback(
(input: string) => {
const filteredPlugins = (state.plugins as any as Plugin<any>[]).filter(
(plugin) => {
return (plugin as any).key
.toLowerCase()
.includes(input.toLowerCase());
}
);
setPluginsLocal(filteredPlugins);
},
[state.plugins]
);

const handleClickSort = () => {
setSortOrder(!sortAsc);
};

const handleSortAsc = (plugins: any) => {
return [...plugins].sort((a, b) => {
if ((a as any).key < (b as any).key) {
return -1;
}
if ((a as any).key > (b as any).key) {
return 1;
}
return 0;
});
};
const handleSortDes = (plugins: any) => {
return [...plugins].sort((a, b) => {
if ((a as any).key < (b as any).key) {
return 1;
}
if ((a as any).key > (b as any).key) {
return -1;
}
return 0;
});
};

return (
<SplitView testId="__prosemirror_devtools_tabs_plugins__">
<SplitViewCol noPaddings>
<div
style={{
display: "flex",
marginLeft: 6,
marginRight: 6,
marginBottom: 3,
marginTop: 3,
}}
>
<SearchBar onSearch={handleSearch} />
<Button onClick={handleClickSort}>
SORT {sortAsc ? "DES" : "ASC"}
</Button>
</div>
<List
items={plugins}
items={
sortAsc ? handleSortAsc(pluginsLocal) : handleSortDes(pluginsLocal)
}
getKey={(plugin: Plugin) => (plugin as any).key}
title={(plugin: Plugin) => (plugin as any).key}
isSelected={isSelected}
isDimmed={(plugin: Plugin) => !plugin.getState(state)}
onListItemClick={handleOnListItemClick}
/>
Expand Down
Loading