Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Wysiwys toolbar customization (#163)

PickleNik56fcef10f721
diff --git a/src/app/create/page.tsx b/src/app/create/page.tsx
index db58968d..cfa33e58 100644
--- a/src/app/create/page.tsx
+++ b/src/app/create/page.tsx
@@ -145,6 +145,7 @@ export default function CreateChallenge() {
<Markdown>{description}</Markdown>
</div>
) : (
+ // TODO: move out to customized component
<MDEditor
height="100%"
className="flex-1"
diff --git a/src/components/challenge/solutions/solution-editor.tsx b/src/components/challenge/solutions/solution-editor.tsx
index 488da2d5..1e2ec5e0 100644
--- a/src/components/challenge/solutions/solution-editor.tsx
+++ b/src/components/challenge/solutions/solution-editor.tsx
@@ -1,5 +1,4 @@
import { zodResolver } from '@hookform/resolvers/zod';
-import MDEditor from '@uiw/react-md-editor';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { Button } from '~/components/ui/button';
@@ -11,6 +10,58 @@ import { type ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteD
import { useSession } from 'next-auth/react';
import { useTheme } from 'next-themes';
import { useRouter } from 'next/navigation';
+import { useContext } from 'react';
+
+import MDEditor, { commands, type ICommand, EditorContext } from '@uiw/react-md-editor';
+
+const PreviewToggle = () => {
+ console.warn(commands);
+ const { preview, dispatch } = useContext(EditorContext);
+ const click = () => {
+ dispatch?.({
+ preview: preview === 'edit' ? 'preview' : 'edit',
+ });
+ };
+ if (preview === 'edit') {
+ return (
+ // TODO: styles don't work for some reason
+ <button
+ className="mr-2 w-10 px-2 font-bold"
+ style={{
+ width: '4rem !important',
+ padding: '0.25rem 1rem !important',
+ fontWeight: 'bold !important',
+ }}
+ type="button"
+ onClick={click}
+ >
+ Preview
+ </button>
+ );
+ }
+ return (
+ // TODO: styles don't work for some reason
+ <button
+ className="mr-2 w-10 px-2 font-bold"
+ style={{
+ width: '4rem !important',
+ padding: '0.25rem 1rem !important',
+ fontWeight: 'bold !important',
+ }}
+ type="button"
+ onClick={click}
+ >
+ Edit
+ </button>
+ );
+};
+
+const codePreview: ICommand = {
+ name: 'preview',
+ keyCommand: 'preview',
+ value: 'preview',
+ icon: <PreviewToggle />,
+};
const getDefaultMarkdown = (solution: string) => `
## Thoughts
@@ -81,80 +132,97 @@ export function SolutionEditor({ setOpen, challenge }: Props) {
: document.documentElement.setAttribute('data-color-mode', 'light');
return (
- <>
- <Form {...form}>
- {/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
- <form className="flex h-full flex-col" onSubmit={form.handleSubmit(onSubmit)}>
- <div className="sticky right-0 top-[41px] z-10 flex items-center justify-between gap-2 border-b border-zinc-300 bg-background/90 bg-opacity-20 p-1 pr-2 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">
- <div className="flex-1">
- <FormField
- control={form.control}
- name="title"
- render={({ field }) => (
- <FormItem>
- <FormControl>
- <Input
- className="rounded-xl border-zinc-300 bg-zinc-100 focus-visible:ring-0 focus-visible:ring-offset-0 dark:border-zinc-700 dark:bg-zinc-900"
- {...field}
- placeholder="Enter a title for your solution."
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
- <Button
- className="h-8 rounded-lg bg-white px-3 py-2 text-black hover:bg-zinc-200 focus-visible:bg-zinc-200 focus-visible:ring-offset-0 dark:bg-zinc-800 dark:text-zinc-300 dark:hover:bg-zinc-700 dark:focus-visible:bg-zinc-700"
- type="button"
- onClick={() => setOpen(false)}
- >
- Cancel
- </Button>
- <Button className="h-8 rounded-lg px-3 py-2" type="submit">
- Post
- </Button>
- </div>
+ <Form {...form}>
+ {/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
+ <form className="flex h-full flex-col" onSubmit={form.handleSubmit(onSubmit)}>
+ <div className="sticky right-0 top-[41px] z-10 flex items-center justify-between gap-2 border-b border-zinc-300 bg-background/90 bg-opacity-20 p-1 pr-2 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">
<div className="flex-1">
<FormField
control={form.control}
- name="content"
+ name="title"
render={({ field }) => (
- // @ts-ignore
- <MDEditor
- height="100%"
- value={field.value}
- // non-split-screen by default
- preview="edit"
- // removes resize handle on bottom right
- visibleDragbar={false}
- // @ts-ignore
- onChange={field.onChange}
- // TODO: customize toolbar GH #131
- // components={{
- // toolbar: (command, disabled, executeCommand) => {
- // if (command.keyCommand === 'code') {
- // return (
- // <button
- // aria-label="Insert code"
- // disabled={disabled}
- // onClick={(evn) => {
- // evn.stopPropagation();
- // executeCommand(command, command.groupName);
- // }}
- // >
- // Code
- // </button>
- // );
- // }
- // },
- // }}
- />
+ <FormItem>
+ <FormControl>
+ <Input
+ className="rounded-xl border-zinc-300 bg-zinc-100 focus-visible:ring-0 focus-visible:ring-offset-0 dark:border-zinc-700 dark:bg-zinc-900"
+ {...field}
+ placeholder="Enter a title for your solution."
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
)}
/>
</div>
- </form>
- </Form>
- </>
+ <Button
+ className="h-8 rounded-lg bg-white px-3 py-2 text-black hover:bg-zinc-200 focus-visible:bg-zinc-200 focus-visible:ring-offset-0 dark:bg-zinc-800 dark:text-zinc-300 dark:hover:bg-zinc-700 dark:focus-visible:bg-zinc-700"
+ type="button"
+ onClick={() => setOpen(false)}
+ >
+ Cancel
+ </Button>
+ <Button className="h-8 rounded-lg px-3 py-2" type="submit">
+ Post
+ </Button>
+ </div>
+ <div className="flex-1">
+ <FormField
+ control={form.control}
+ name="content"
+ render={({ field }) => (
+ // @ts-ignore
+ <MDEditor
+ height="100%"
+ value={field.value}
+ // non-split-screen by default
+ preview="edit"
+ // TODO: might be a better way to select certain buttons insetad of re-rendering to <></>
+ // commands={[commands.codeEdit, commands.codePreview]}
+ // removes resize handle on bottom right
+ visibleDragbar={false}
+ extraCommands={[codePreview, commands.fullscreen]}
+ // @ts-ignore
+ onChange={field.onChange}
+ components={{
+ toolbar: (command) => {
+ // toolbar: (command, disabled, executeCommand) => {
+ // re-render these to nothing
+ if (
+ command.keyCommand === 'hr' ||
+ command.keyCommand === 'link' ||
+ command.keyCommand === 'quote' ||
+ command.keyCommand === 'image' ||
+ command.keyCommand === 'comment' ||
+ command.keyCommand === 'list' ||
+ // TODO: these aren't under command.keyCommand?
+ command.keyCommand === 'unordered-list' ||
+ command.keyCommand === 'ordered-list' ||
+ command.keyCommand === 'checked-list'
+ ) {
+ return <></>;
+ }
+ // custom code button option
+ if (command.keyCommand === 'code') {
+ // return (
+ // <button
+ // aria-label="Insert code"
+ // disabled={disabled}
+ // onClick={(evn) => {
+ // evn.stopPropagation();
+ // executeCommand(command, command.groupName);
+ // }}
+ // >
+ // a
+ // </button>
+ // );
+ }
+ },
+ }}
+ />
+ )}
+ />
+ </div>
+ </form>
+ </Form>
);
}
diff --git a/src/components/ui/navigation.tsx b/src/components/ui/navigation.tsx
index 48940155..5c604c68 100644
--- a/src/components/ui/navigation.tsx
+++ b/src/components/ui/navigation.tsx
@@ -7,15 +7,7 @@ import { LogIn, Plus, Settings, User, Bell, Moon, Sun, Loader2 } from 'lucide-re
import { useEffect, useState } from 'react';
import { useRouter, usePathname } from 'next/navigation';
import { signIn, signOut } from 'next-auth/react';
-import {
- NavigationMenu,
- NavigationMenuContent,
- NavigationMenuItem,
- NavigationMenuLink,
- NavigationMenuList,
- NavigationMenuTrigger,
- navigationMenuTriggerStyle,
-} from './navigation-menu';
+import { navigationMenuTriggerStyle } from './navigation-menu';
import { Button } from './button';
import {
DropdownMenu,
@@ -78,7 +70,7 @@ export function Navigation() {
<User className="h-5 w-5" />
</button>
</DropdownMenuTrigger>
- <DropdownMenuContent className="w-56">
+ <DropdownMenuContent className="mr-4 w-56 rounded-xl">
<Link className="block" href="/create">
<DropdownMenuItem className="rounded-lg p-2 duration-300 focus:bg-accent focus:outline-none">
<Plus className="mr-2 h-4 w-4" />
@@ -100,9 +92,13 @@ export function Navigation() {
</Link>
)}
<DropdownMenuSeparator />
- <DropdownMenuItem onClick={() => void handleSignOut()}>
- <span>Log out</span>
- </DropdownMenuItem>
+ <Button
+ variant="ghost"
+ className="h-8 w-full justify-start rounded-b-lg rounded-t-sm px-2 text-red-500 hover:bg-red-500/20 hover:text-red-500"
+ onClick={() => void handleSignOut()}
+ >
+ <span className="text-red-500">Log out</span>
+ </Button>
</DropdownMenuContent>
</DropdownMenu>
</>
@@ -132,7 +128,7 @@ export function Navigation() {
}`}
>
<div className="flex w-full items-center justify-between">
- <div className="relative flex">
+ <div className="relative flex gap-3">
<a className="flex items-center space-x-2 duration-300" href="/">
<svg
className="h-6 w-6 rounded-md bg-[#3178C6] p-[2px]"
@@ -170,25 +166,11 @@ export function Navigation() {
</a>
{!isProd() && (
- <NavigationMenu className="pl-6">
- <NavigationMenuList>
- <>
- <NavigationMenuItem>
- <Link href="/explore" legacyBehavior passHref>
- <NavigationMenuLink className={navigationMenuTriggerStyle()}>
- Explore
- </NavigationMenuLink>
- </Link>
- </NavigationMenuItem>
- <NavigationMenuItem className="hidden sm:block">
- <NavigationMenuTrigger>Something Nice</NavigationMenuTrigger>
- <NavigationMenuContent>
- <NavigationMenuLink>Nice</NavigationMenuLink>
- </NavigationMenuContent>
- </NavigationMenuItem>
- </>
- </NavigationMenuList>
- </NavigationMenu>
+ <Link href="/explore" legacyBehavior passHref>
+ <Button variant="ghost" className={navigationMenuTriggerStyle()}>
+ Explore
+ </Button>
+ </Link>
)}
</div>
<div className="flex">
diff --git a/src/styles/globals.css b/src/styles/globals.css
index 76058f4f..a925d848 100644
--- a/src/styles/globals.css
+++ b/src/styles/globals.css
@@ -105,6 +105,7 @@ main {
.w-md-editor-toolbar-divider {
@apply bg-zinc-300 dark:bg-zinc-700;
+ /* TODO: not aligned center vertically */
height: 1.5rem !important;
}
.w-md-editor-toolbar ul {
@@ -114,7 +115,7 @@ main {
@apply text-black dark:text-white;
}
.w-md-editor-toolbar ul li button {
- @apply flex h-8 w-8 items-center justify-center rounded-md p-1 text-neutral-500 hover:bg-neutral-200 hover:text-black dark:hover:bg-zinc-700 dark:hover:text-white;
+ @apply flex h-8 w-auto min-w-[2rem] items-center justify-center rounded-md p-1 text-neutral-500 hover:bg-neutral-200 hover:text-black dark:hover:bg-zinc-700 dark:hover:text-white;
}
.w-md-editor-toolbar ul li button[disabled] {
@apply cursor-default text-neutral-700 hover:bg-transparent hover:text-neutral-700;
@@ -127,7 +128,7 @@ main {
}
.w-md-editor-preview {
- padding: 0.5rem 1rem !important;
+ padding: 1rem !important;
box-shadow: inset 1px 0 0 0 rgb(212 212 216) !important;
}
.dark .w-md-editor-preview {