jodit-react
Version:
Jodit is awesome and usefully wysiwyg editor with filebrowser
124 lines (120 loc) • 3.73 kB
JavaScript
// src/JoditEditor.tsx
import { useEffect, useRef, forwardRef } from "react";
// src/include.jodit.ts
import { Jodit as JoditES5 } from "jodit/esm/index.js";
import "jodit/es2021/jodit.min.css";
import "jodit/esm/plugins/all.js";
var Jodit = JoditES5;
// src/JoditEditor.tsx
import { jsx } from "react/jsx-runtime";
function usePrevious(value) {
const ref = useRef("");
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
var JoditEditor = forwardRef(
({
JoditConstructor = Jodit,
className,
config,
id,
name,
onBlur,
onChange,
tabIndex,
value,
editorRef
}, ref) => {
const textAreaRef = useRef(null);
const joditRef = useRef(null);
useEffect(() => {
const element = textAreaRef.current;
const jodit = JoditConstructor.make(element, config);
joditRef.current = jodit;
if (typeof editorRef === "function") {
editorRef(jodit);
}
return () => {
if (jodit.isReady) {
jodit.destruct();
} else {
jodit.waitForReady().then((joditInstance) => joditInstance.destruct());
}
};
}, [JoditConstructor, config, editorRef]);
useEffect(() => {
if (ref) {
if (typeof ref === "function") {
ref(joditRef.current);
} else {
ref.current = joditRef.current;
}
}
}, [textAreaRef, ref, joditRef]);
const preClassName = usePrevious(className != null ? className : "");
useEffect(() => {
var _a, _b;
const classList = (_b = (_a = joditRef.current) == null ? void 0 : _a.container) == null ? void 0 : _b.classList;
if (preClassName !== className && typeof preClassName === "string") {
preClassName.split(/\s+/).filter(Boolean).forEach((cl) => classList == null ? void 0 : classList.remove(cl));
}
if (className && typeof className === "string") {
className.split(/\s+/).filter(Boolean).forEach((cl) => classList == null ? void 0 : classList.add(cl));
}
}, [className, preClassName]);
useEffect(() => {
var _a;
if ((_a = joditRef.current) == null ? void 0 : _a.workplace) {
joditRef.current.workplace.tabIndex = tabIndex || -1;
}
}, [tabIndex]);
useEffect(() => {
const jodit = joditRef.current;
if (!(jodit == null ? void 0 : jodit.events) || !(onBlur || onChange)) {
return;
}
const onBlurHandler = (event) => {
var _a, _b;
return onBlur && onBlur((_b = (_a = joditRef == null ? void 0 : joditRef.current) == null ? void 0 : _a.value) != null ? _b : "", event);
};
const onChangeHandler = (value2) => onChange && onChange(value2);
jodit.events.on("blur", onBlurHandler).on("change", onChangeHandler);
return () => {
var _a;
(_a = jodit.events) == null ? void 0 : _a.off("blur", onBlurHandler).off("change", onChangeHandler);
};
}, [onBlur, onChange]);
useEffect(() => {
const jodit = joditRef.current;
const updateValue = () => {
if (jodit && value !== void 0 && jodit.value !== value) {
jodit.value = value;
}
};
if (jodit) {
if (jodit.isReady) {
updateValue();
} else {
jodit.waitForReady().then(updateValue);
}
}
}, [value]);
return /* @__PURE__ */ jsx("div", { className: "jodit-react-container", children: /* @__PURE__ */ jsx(
"textarea",
{
defaultValue: value,
name,
id,
ref: textAreaRef
}
) });
}
);
JoditEditor.displayName = "JoditEditor";
var JoditEditor_default = JoditEditor;
export {
Jodit,
JoditEditor_default
};