File size: 3,386 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useState } from "react";
import ReactMde from "react-mde";
import { useDispatch } from "react-redux";
import { getDefaultToolbarCommands } from "react-mde";
import { Box } from "@chakra-ui/react";
import { nanoid } from "@reduxjs/toolkit";
import { uploadImage } from "../lib/api";
import converter from "../helper/converter";
import MDEToolbarImgIcon from "../utils/MDEToolbarImgIcon";
import { setMDEValueToStore } from "../store/post/postData";
import "react-mde/lib/styles/css/react-mde-all.css";
import "../styles/customizeMDE.scss";
import { BsCodeSquare } from "react-icons/bs";

const customToolbarCommands = () => {
  const commands = getDefaultToolbarCommands();
  commands[1].splice(3, 1, "code-block", "img_url");
  return commands;
};

const codeBlock = {
  name: "code-block",
  icon: () => <BsCodeSquare size={18} style={{ position: "absolute" }} />,
  execute: (opts) => {
    opts.textApi.replaceSelection("```\n Enter code here... \n```");
  },
};

const MDE = ({ MDEValue, setMDEValue, isSubmitting, setUploadingImg }) => {
  const [value, setValue] = useState(MDEValue || "");
  const [selectedTab, setSelectedTab] = useState("write");

  const dispatch = useDispatch();

  useEffect(() => {
    if (setMDEValue) {
      setMDEValue(value); //for comment
    } else {
      dispatch(setMDEValueToStore(value)); //for postData to publish or edit
    }
  }, [value, dispatch, setMDEValue]);

  useEffect(() => {
    if (!MDEValue) {
      setValue(MDEValue);
    } // setting MDEValue to useState doesn't trigger again after initial render so I set empty string to value if it's empty
  }, [MDEValue]);

  useEffect(() => {
    if (isSubmitting) {
      document.querySelector(".mde-text").disabled = true;
    } else {
      document.querySelector(".mde-text").disabled = false;
    }
  }, [isSubmitting]);

  const mdeImgUploadHandler = (e) => {
    const image = e.target.files[0];

    if (image) {
      document.querySelector(".mde-text").disabled = true;
      setUploadingImg(true);
      setValue((prevVal) =>
        prevVal.replace("![](img_url)", "![](uploading...)")
      );

      const selectedImgPath = `images/${img.name}${nanoid()}`;

      uploadImage(image, selectedImgPath)
        .then((url) => {
          document.querySelector(".mde-text").disabled = false;

          setValue((prevVal) =>
            prevVal.replace("![](uploading...)", `![](${url})`)
          );

          setUploadingImg(false);
        })
        .catch((err) => console.log(err));

      e.target.value = ""; // otherwise input event doesn't trigger again when user add the same file
    }
  };

  const img = {
    name: "image_url",
    icon: () => <MDEToolbarImgIcon onChange={mdeImgUploadHandler} />,
    execute: (opts) => {
      opts.textApi.replaceSelection("![](img_url)");
    },
  };

  return (
    <Box w="100%" fontFamily="monospace">
      <ReactMde
        commands={{
          "code-block": codeBlock,
          img_url: img,
        }}
        loadingPreview="loading..."
        toolbarCommands={customToolbarCommands()}
        value={value}
        onChange={setValue}
        selectedTab={selectedTab}
        onTabChange={setSelectedTab}
        generateMarkdownPreview={(markdown) =>
          Promise.resolve(converter().makeHtml(markdown))
        }
      />
    </Box>
  );
};

export default React.memo(MDE);