Skip to content

Commit

Permalink
Merge pull request #1513 from p3ol/fix/ckeditor-ssr-error
Browse files Browse the repository at this point in the history
🐛 fix(ckeditor5-react): prevent ckeditor from loading on server side
  • Loading branch information
dackmin authored Oct 25, 2024
2 parents 141ce33 + 2d1fe1c commit b67bccd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
26 changes: 24 additions & 2 deletions packages/addon-ckeditor5-react/lib/Field/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { EditorConfig } from '@ckeditor/ckeditor5-core';
import type { EventInfo } from '@ckeditor/ckeditor5-utils';
import type ClassicEditor from '@oakjs/ckeditor5-build-custom';
import type { Editor } from '@oakjs/ckeditor5-build-custom';
import { type ComponentPropsWithoutRef, useCallback } from 'react';
import { type ComponentPropsWithoutRef, useCallback, useEffect, useState } from 'react';
import { CKEditor } from '@ckeditor/ckeditor5-react';
import {
type FieldContent,
Expand All @@ -23,15 +24,36 @@ const CKEditorField = ({
value,
onChange,
}: CKEditorFieldProps) => {
const [defaultEditor, setDefaultEditor] = useState<{
classicEditor: typeof ClassicEditor;
}>();

const loadEditor = useCallback(async () => {
setDefaultEditor({
classicEditor: (await import('@oakjs/ckeditor5-build-custom'))
?.default,
});
}, []);

useEffect(() => {
if (!editor && !defaultEditor) {
loadEditor();
}
}, [editor, defaultEditor, loadEditor]);

const onChange_ = useCallback((_: EventInfo, ed: Editor) => {
onChange?.({ value: ed.getData() });
}, []);

if (!editor && !defaultEditor) {
return null;
}

return (
<div className={classNames('ckeditor-field', className)}>
<CKEditor
// @ts-ignore CK editor is weird anyway
editor={editor}
editor={editor || defaultEditor?.classicEditor}
config={config}
data={value}
onChange={onChange_}
Expand Down
3 changes: 1 addition & 2 deletions packages/addon-ckeditor5-react/lib/addons.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ReactFieldObject, AddonObject } from '@oakjs/react';
import { omit } from '@oakjs/react';
import ClassicEditor from '@oakjs/ckeditor5-build-custom';

import Field, { CKEditorFieldProps } from './Field';

Expand All @@ -13,7 +12,7 @@ export const ckeditorField = ({
render: Field,
...omit(props, ['onChange']),
props: {
editor: editor || ClassicEditor,
editor,
config: {
...config,
link: {
Expand Down

0 comments on commit b67bccd

Please sign in to comment.