-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Insight): CKEditor and various stability fixes
- Loading branch information
1 parent
f2124f9
commit 5b3bc6a
Showing
13 changed files
with
9,843 additions
and
4,318 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import axios from "axios"; | ||
import { useRef, useEffect, useState } from "react"; | ||
import LoadingSpinner from "../LoadingSpinner"; | ||
import { CKEditor } from "@ckeditor/ckeditor5-react"; | ||
import InsightEditor from "@libretexts/insight-ckeditor5-build"; | ||
import Editor from "@libretexts/insight-ckeditor5-build"; | ||
import { Plugin } from "@ckeditor/ckeditor5-core"; | ||
|
||
interface KBCKEditorProps extends React.HTMLAttributes<HTMLDivElement> { | ||
data: string | undefined; | ||
onDataChange: (newData: string) => void; | ||
pageUUID: string; | ||
} | ||
|
||
const KBCKEditor: React.FC<KBCKEditorProps> = ({ | ||
data, | ||
onDataChange, | ||
pageUUID, | ||
...rest | ||
}) => { | ||
const [loading, setLoading] = useState(false); | ||
|
||
class ImageUploadAdapterPlugin { | ||
private loader: any; | ||
constructor(loader: any) { | ||
this.loader = loader; | ||
} | ||
|
||
upload() { | ||
return this.loader.file.then( | ||
(file: any) => | ||
new Promise((resolve, reject) => { | ||
const myReader = new FileReader(); | ||
myReader.addEventListener("loadend", (e: any) => { | ||
this.sendRequest(file).then((res) => { | ||
if (!res || typeof res !== "string") reject("Invalid response"); | ||
resolve({ default: res }); | ||
}); | ||
}); | ||
myReader.readAsDataURL(file); | ||
}) | ||
); | ||
} | ||
|
||
async sendRequest(file: File) { | ||
const formData = new FormData(); | ||
formData.append("file", file); | ||
|
||
const uploadRes = await axios.post( | ||
`/kb/page/${pageUUID}/files`, | ||
formData, | ||
{ | ||
headers: { "Content-Type": "multipart/form-data" }, | ||
} | ||
); | ||
if (uploadRes.data.err || !uploadRes.data.url) { | ||
throw new Error(uploadRes.data.errMsg); | ||
} | ||
return uploadRes.data.url; | ||
} | ||
} | ||
|
||
return ( | ||
<div aria-busy={loading} {...rest}> | ||
{loading && <LoadingSpinner />} | ||
<CKEditor | ||
//@ts-expect-error | ||
editor={InsightEditor} | ||
data={data} | ||
onReady={(editor) => { | ||
editor.plugins.get("FileRepository").createUploadAdapter = ( | ||
loader | ||
) => { | ||
return new ImageUploadAdapterPlugin(loader); | ||
}; | ||
}} | ||
onChange={(event, editor) => { | ||
const data = editor.data.get(); | ||
onDataChange(data); | ||
}} | ||
/> | ||
<p className="text-xs text-gray-500 italic ml-1"> | ||
Caution: When using Source mode, ensure you toggle back to WYSIWYG mode | ||
before saving. Otherwise, your changes may be lost. CKEditor will ignore | ||
invalid HTML when toggling back to WYSIWYG mode. | ||
</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default KBCKEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.