Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import "@toast-ui/editor/dist/toastui-editor.css"; import ToastUIEditor from "@toast-ui/editor"; import { ToolbarItem } from "@toast-ui/editor/types/ui"; import classNames from "classnames"; import { INSERT_IMAGE } from "components/MarkdownInput/constants"; import UploadImage from "components/MarkdownInput/UploadImage"; import { useEffect, useRef, useState } from "react"; import { useController } from "react-hook-form"; import makeStyles from "utils/makeStyles"; import { MarkdownInputProps } from "./MarkdownInput"; const useStyles = makeStyles((theme) => ({ root: { "& .toastui-editor-contents": { fontSize: theme.typography.fontSize, fontFamily: theme.typography.fontFamily, "& h1, & h2, & h3, & h4, & h5, & h6": { borderBottom: "none", paddingBottom: 0, marginBottom: 0, marginTop: theme.spacing(2), }, "& h1": { ...theme.typography.h1, }, "& h2": { ...theme.typography.h2, }, "& h3": theme.typography.h3, "& h4": theme.typography.h4, "& h5": theme.typography.h5, "& h6": theme.typography.h6, "& p": theme.typography.body1, "& ol": theme.typography.body1, "& ul": theme.typography.body1, "& blockquote": theme.typography.body1, "& a": { color: theme.palette.primary.main, }, "& img": { width: "100%", maxWidth: "400px", }, }, }, errorState: { "& .toastui-editor-defaultUI": { border: "2px solid red", }, }, })); export default function MarkdownInput({ control, defaultValue, id, resetInputRef, labelId, name, imageUpload = false, autofocus = false, required, }: MarkdownInputProps) { const classes = useStyles(); const { field, fieldState } = useController({ name, control, defaultValue: defaultValue ?? "", rules: { required, }, }); const [imageDialogOpen, setImageDialogOpen] = useState(false); const initialDefaultValue = useRef(defaultValue); const fieldRef = useRef<ToastUIEditor | null>(null); // Separate ref for the editor const rootEl = useRef<HTMLDivElement>(null); // Workaround to keep the function identities of onBlur/onChange stable to // prevent the effect below from re-running and destroying the editor instance const fieldOnBlur = useRef<typeof field.onBlur>(field.onBlur); const fieldOnChange = useRef<typeof field.onChange>(field.onChange); useEffect(() => { const uploadButton = imageUpload ? document.createElement("button") : null; const openDialog = () => { setImageDialogOpen(true); }; if (imageUpload) { uploadButton!.type = "button"; //class stolen from tui source code uploadButton!.className = "toastui-editor-toolbar-icons image"; uploadButton!.setAttribute("aria-label", INSERT_IMAGE); uploadButton!.style.margin = "0"; uploadButton!.addEventListener("click", openDialog); } const toolbarItems: ToolbarItem[] = [ ["heading", "bold", "italic"], ["hr", "quote", "ul", "ol"], ["link"], ]; if (imageUpload) { toolbarItems.push([ { name: "image", tooltip: INSERT_IMAGE, el: uploadButton!, }, ]); } fieldRef.current = new ToastUIEditor({ el: rootEl.current!, events: { blur: () => fieldOnBlur.current(), change: () => fieldOnChange.current( (fieldRef.current as ToastUIEditor).getMarkdown() ), }, initialEditType: "wysiwyg", initialValue: initialDefaultValue.current ?? "", usageStatistics: false, toolbarItems, autofocus, extendedAutolinks: true, }); Iif (resetInputRef) { resetInputRef.current = fieldRef.current.reset.bind(fieldRef.current); } const editBox = document.querySelector(`#${id} [contenteditable=true]`); if (editBox) { editBox.setAttribute("aria-labelledby", labelId); editBox.setAttribute("aria-multiline", "true"); editBox.setAttribute("role", "textbox"); } else E{ console.warn( "Couldn't locate the markdown input area for accessibility tags" ); } return () => { Iif (resetInputRef) { resetInputRef.current = null; } if (imageUpload) uploadButton!.removeEventListener("click", openDialog); (fieldRef.current as ToastUIEditor).destroy(); }; }, [autofocus, fieldRef, resetInputRef, id, labelId, imageUpload]); return ( <> <div className={classNames(classes.root, { [classes.errorState]: fieldState.invalid, })} ref={rootEl} id={id} /> {imageUpload && ( <UploadImage open={imageDialogOpen} onClose={() => setImageDialogOpen(false)} emitter={ (fieldRef.current as ToastUIEditor | undefined)?.eventEmitter } /> )} </> ); } |