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

feat: add editor ref to ArticleEditor component #62

Merged
merged 1 commit into from
Jan 14, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}

.ck.ck-editor__main > .ck-editor__editable {
background-color: var(--mantine-color-body);
background-color: var(--mantine-color-dark-6);
}

.editor-container_classic-editor .editor-container__editor {
Expand Down Expand Up @@ -182,7 +182,7 @@

:root[data-mantine-color-scheme="dark"] {
/* Helper variables to avoid duplication in the colors. */
--ck-custom-background: var(--mantine-color-body);
--ck-custom-background: var(--mantine-color-dark-6);
--ck-custom-foreground: hsl(255, 3%, 18%);
--ck-custom-border: var(--mantine-color-dark-4);
--ck-color-base-border: var(--mantine-color-dark-4);
Expand Down Expand Up @@ -215,6 +215,7 @@

--ck-color-button-save: hsl(120, 100%, 46%);
--ck-color-button-cancel: hsl(15, 100%, 56%);
--ck-color-button-on-color: var(--mantine-color-blue-6);

/* -- Overrides the default .ck-dropdown class colors. -------------------------------------- */

Expand Down Expand Up @@ -281,7 +282,6 @@
--ck-color-widget-editable-focus-background: var(--ck-custom-white);

/* -- Overrides the default colors used by the ckeditor5-link package. ---------------------- */

--ck-color-link-default: hsl(190, 100%, 75%);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
"use client";
import {useRef, useMemo} from "react";
import {useMemo, type RefObject} from "react";
import {CKEditor} from "@ckeditor/ckeditor5-react";
import {ClassicEditor, EditorConfig} from "ckeditor5";
import {editorConfig} from "./editor-config";
import "ckeditor5/ckeditor5.css";
import "./article-editor.css";

export type EditorRef = CKEditor<ClassicEditor>;

type Props = {
initialData?: string;
editorRef?: RefObject<EditorRef>;
};

export function ArticleEditor({initialData}: Props) {
const editorContainerRef = useRef<any>(null);
const editorRef = useRef<any>(null);
const editorWordCountRef = useRef<any>(null);

export function ArticleEditor({initialData, editorRef}: Props) {
const config: EditorConfig = useMemo(() => {
return {
...editorConfig,
Expand All @@ -24,19 +23,12 @@ export function ArticleEditor({initialData}: Props) {

return (
<div className="main-container">
<div
className="editor-container editor-container_classic-editor editor-container_include-style editor-container_include-block-toolbar editor-container_include-word-count"
ref={editorContainerRef}
>
<div className="editor-container editor-container_classic-editor editor-container_include-style editor-container_include-block-toolbar editor-container_include-word-count">
<div className="editor-container__editor">
<div ref={editorRef}>
{config && <CKEditor editor={ClassicEditor} config={config} />}
</div>
{config && (
<CKEditor editor={ClassicEditor} config={config} ref={editorRef} />
)}
</div>
<div
className="editor_container__word-count"
ref={editorWordCountRef}
></div>
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {ArticleEditor} from "./article-editor";
export {ArticleEditor, type EditorRef} from "./article-editor";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import dynamic from "next/dynamic";
import {useRef} from "react";
import {useFormState} from "react-dom";
import {
Box,
Expand All @@ -13,10 +13,12 @@ import {
} from "@mantine/core";
import {DateTimeInput} from "@/components/date-time-input";
import {FormButton} from "@/components/form-button";
import {type EditorRef} from "@/features/articles/components/article-editor";
import {FileInput} from "./file-input";
import {IconPhotoPlus, IconMovie} from "@tabler/icons-react";
import {upsertArticleAction} from "../../actions/upsert-article";
import {isGregorianStartDateTime} from "@/lib/date-and-time";
import dynamic from "next/dynamic";

const ArticleEditor = dynamic(
async () => {
Expand All @@ -43,6 +45,7 @@ type Props = {
};

export function ArticleUpsertForm({article}: Props) {
const editorRef = useRef<EditorRef>(null);
const [state, dispatch] = useFormState(upsertArticleAction, {
success: true,
});
Expand All @@ -53,7 +56,10 @@ export function ArticleUpsertForm({article}: Props) {
: null;

const handleSubmit = async (formData: FormData) => {
formData.set("body", "");
if (Boolean(editorRef.current?.editor?.getData) === false) {
throw new Error("ArticleEditor getData is undefined");
}
formData.set("body", editorRef.current?.editor?.getData() || "");
if (article?.articleId) {
formData.set("uuid", article.articleId);
}
Expand All @@ -78,7 +84,10 @@ export function ArticleUpsertForm({article}: Props) {
/>
<Box>
<InputLabel>محتوا</InputLabel>
<ArticleEditor initialData={article?.defaultBody} />
<ArticleEditor
initialData={article?.defaultBody}
editorRef={editorRef}
/>
</Box>
<FileInput
name="cover"
Expand Down
Loading