import React, { Dispatch, SetStateAction } from "react"; import * as TinyMCE from "@tinymce/tinymce-react"; import axios from "axios"; import { GrammarlyButton } from "@grammarly/editor-sdk-react"; import sioc from "socket.io-client"; import { authorize, getNewToken } from "../utils/google"; import { DarkEditorMode, GlobalEditorStyles, LightEditorMode, } from "../styles/3ditor"; import { Theme } from "../utils/types"; import { DyslexicMode } from "../styles/theme"; const socket = sioc(); const Editor = ({ documentContent, documentId, theme_, setTheme_, readOnly, submitAllowed, }: { documentContent?: string; documentId?: string; theme_?: Theme; setTheme_?: Dispatch>; readOnly?: boolean; submitAllowed?: boolean; }) => { socket.on("complete_auth", async () => { console.log(await getNewToken()); }); const handleFormSubmit = () => { if (!submitAllowed || readOnly) return; console.log("Submitting..."); const url = `${window.location.protocol}//${ window.location.hostname }/api/v1/editor/save${documentId ? "over" : ""}`; const docid = (document.getElementById("_3ditor_doc_id") as HTMLInputElement) ?.value || ""; const content = (document.getElementById("_3ditor_content") as HTMLTextAreaElement) ?.value || ""; axios .post(url, { _3ditor_doc_id: docid, _3ditor_content: content }) .then((d) => { if (window.location.pathname == "/") { window.location.replace( `${window.location.protocol}//${window.location.hostname}/document/d/${d.data.documentId}/edit` ); } }) .catch(() => { alert("Unable to dave document."); }); }; const [playing, setPlaying] = React.useState(false); const [ready, setReady] = React.useState(false); const [readyToLoad, setReadyToLoad] = React.useState(false); const [theme, setTheme] = React.useState(theme_ || "light"); // eslint-disable-next-line react-hooks/exhaustive-deps React.useEffect(() => { const t = window.localStorage.getItem("theme"); if (t) setTheme(t as Theme); if (setTheme_) setTheme_(t as Theme); }); interface SelectBoxItemSpec { text: string; value: string; lang: string; } const voices: SelectBoxItemSpec[] = []; const voice: SelectBoxItemSpec = { text: "Unknown Voice", value: "-1", lang: "en-US", }; React.useEffect(() => { window.addEventListener("load", () => { const synth = window.speechSynthesis; setInterval(handleFormSubmit, 5000); synth.addEventListener("voiceschanged", () => { const voices_ = synth.getVoices(); if (voices_.length == 0) return; for (let i = 0; i < voices_.length; i++) { const v = voices_[i]; voices.push({ text: v.name, value: i + "", lang: v.lang, }); } setRVoice( getVoiceByLang("en-US") || { text: voices[4].text, value: 4 + "", lang: "en-US", } ); const s_ = new SpeechSynthesisUtterance("test"); s_.lang = "en-US"; for (let i = 0; i < voices.length; i++) { if (voices[i].text == s_.voice?.name) { setRVoice(voices[i]); } } setRVoices(voices); setReady(true); }); }); }); const [RVoices, setRVoices] = React.useState(voices); const [RVoice, setRVoice] = React.useState(voice); const getVoiceByLang = (lang: string) => RVoices.find((v) => v.lang == lang); return ( <> {theme == "light" ? : <>} {theme == "dark" ? : <>} {theme == "light-dyslexic" ? : <>} {theme == "dark-dyslexic" ? : <>} {ready ? ( <>