Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(ckeditor5-react): prevent ckeditor from loading on server side #1513

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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