Skip to content

Commit

Permalink
enhance: improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjschlapbach committed Oct 20, 2024
1 parent 8c2dcd1 commit 0e0fdf9
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions apps/frontend-manage/src/components/common/ContentInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
import { Tooltip } from '@uzh-bf/design-system'
import isHotkey from 'is-hotkey'
import { useTranslations } from 'next-intl'
import React, { PropsWithChildren, useCallback, useMemo, useState } from 'react'
import React, {
PropsWithChildren,
ReactNode,
useCallback,
useMemo,
useState,
} from 'react'
import {
BaseEditor,
createEditor,
Expand Down Expand Up @@ -117,8 +123,11 @@ function ContentInput({

const [isImageDropzoneOpen, setIsImageDropzoneOpen] = useState(false)

const renderElement = useCallback((props: any) => <Element {...props} />, [])
const renderLeaf = useCallback((props: any) => <Leaf {...props} />, [])
const renderElement = useCallback(
(props: ElementProps) => <Element {...props} />,
[]
)
const renderLeaf = useCallback((props: LeafProps) => <Leaf {...props} />, [])
const editor = useMemo(() => withHistory(withReact(createEditor())), [])

const editorValue = useMemo(() => {
Expand All @@ -135,7 +144,6 @@ function ContentInput({
className?.root
)}
>
{/* eslint-disable-next-line react/no-children-prop */}
<Slate
editor={editor}
initialValue={editorValue}
Expand All @@ -153,9 +161,8 @@ function ContentInput({
renderElement={renderElement}
renderLeaf={renderLeaf}
onKeyDown={(event) => {
// eslint-disable-next-line no-restricted-syntax
for (const hotkey in HOTKEYS) {
if (isHotkey(hotkey, event as any)) {
if (isHotkey(hotkey, event)) {
event.preventDefault()
const mark = HOTKEYS[hotkey]
toggleMark(editor, mark)
Expand Down Expand Up @@ -250,7 +257,7 @@ function ContentInput({
active={isImageDropzoneOpen}
editor={editor}
format="paragraph"
onClick={(e: any) => {
onClick={() => {
setIsImageDropzoneOpen((prev) => !prev)
}}
>
Expand Down Expand Up @@ -428,7 +435,13 @@ const isMarkActive = (
return marks ? marks[format] === true : false
}

const Element = ({ attributes, children, element }: any) => {
interface ElementProps {
attributes: any
children: ReactNode
element: CustomElement
}

const Element = ({ attributes, children, element }: ElementProps) => {
switch (element.type) {
case 'block-quote':
return (
Expand All @@ -438,10 +451,10 @@ const Element = ({ attributes, children, element }: any) => {
)
case 'bulleted-list':
return <ul {...attributes}>{children}</ul>
case 'heading-one':
return <h1 {...attributes}>{children}</h1>
case 'heading-two':
return <h2 {...attributes}>{children}</h2>
// case 'heading-one':
// return <h1 {...attributes}>{children}</h1>
// case 'heading-two':
// return <h2 {...attributes}>{children}</h2>
case 'list-item':
return <li {...attributes}>{children}</li>
case 'numbered-list':
Expand All @@ -451,7 +464,13 @@ const Element = ({ attributes, children, element }: any) => {
}
}

const Leaf = ({ attributes, children, leaf }: any) => {
interface LeafProps {
attributes: any
children: ReactNode
leaf: CustomText
}

const Leaf = ({ attributes, children, leaf }: LeafProps) => {
let formattedChildren = children
if (leaf.bold) {
formattedChildren = <strong>{formattedChildren}</strong>
Expand Down

0 comments on commit 0e0fdf9

Please sign in to comment.