|
|
|
@ -1,14 +1,13 @@
|
|
|
|
|
import { forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useRef } from "react";
|
|
|
|
|
import TinyUndo from "tiny-undo";
|
|
|
|
|
import toastHelper from "../Toast";
|
|
|
|
|
import appContext from "../../stores/appContext";
|
|
|
|
|
import resourceService from "../../services/resourceService";
|
|
|
|
|
import { storage } from "../../helpers/storage";
|
|
|
|
|
import useRefresh from "../../hooks/useRefresh";
|
|
|
|
|
import Only from "../common/OnlyWhen";
|
|
|
|
|
import "../../less/editor.less";
|
|
|
|
|
|
|
|
|
|
export interface EditorRefActions {
|
|
|
|
|
element: HTMLTextAreaElement;
|
|
|
|
|
focus: FunctionType;
|
|
|
|
|
insertText: (text: string) => void;
|
|
|
|
|
setContent: (text: string) => void;
|
|
|
|
@ -56,42 +55,6 @@ const Editor = forwardRef((props: Props, ref: React.ForwardedRef<EditorRefAction
|
|
|
|
|
editorRef.current.value = initialContent;
|
|
|
|
|
refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handlePasteEvent = async (event: ClipboardEvent) => {
|
|
|
|
|
if (event.clipboardData && event.clipboardData.files.length > 0) {
|
|
|
|
|
const file = event.clipboardData.files[0];
|
|
|
|
|
const { type } = file;
|
|
|
|
|
|
|
|
|
|
if (!type.startsWith("image")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!editorRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const image = await resourceService.upload(file);
|
|
|
|
|
const url = `/r/${image.id}/${image.filename}`;
|
|
|
|
|
|
|
|
|
|
const prevValue = editorRef.current.value;
|
|
|
|
|
editorRef.current.value =
|
|
|
|
|
prevValue.slice(0, editorRef.current.selectionStart) + url + prevValue.slice(editorRef.current.selectionStart);
|
|
|
|
|
handleContentChangeCallback(editorRef.current.value);
|
|
|
|
|
refresh();
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
toastHelper.error(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
editorRef.current.addEventListener("paste", handlePasteEvent);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
editorRef.current?.removeEventListener("paste", handlePasteEvent);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
@ -135,6 +98,7 @@ const Editor = forwardRef((props: Props, ref: React.ForwardedRef<EditorRefAction
|
|
|
|
|
useImperativeHandle(
|
|
|
|
|
ref,
|
|
|
|
|
() => ({
|
|
|
|
|
element: editorRef.current as HTMLTextAreaElement,
|
|
|
|
|
focus: () => {
|
|
|
|
|
editorRef.current?.focus();
|
|
|
|
|
},
|
|
|
|
|