Showing posts with label CKEditor5. Show all posts
Showing posts with label CKEditor5. Show all posts

Thursday, October 22, 2020

Menggunakan CKEDITOR5 di NextJS

 Pertama-tama mari kita install next.js dengan perintah 

npx create-next-app



Stelah itu kita jalanin next.js-nya buat mastiin semuanya okie

cd next-ckedt

npm run dev

Hasilnya



Siiipsss semuanya okie.... setelah itu kita beralih ke CKEditor 5 yang dokumnetasinya bisa dilihat disini:
https://ckeditor.com/docs/ckeditor5/latest/index.html

Untuk integrasi ke next.js, kita bisa lihat contoh integrasi buat react.js

https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/react.html

So pertama-tama install dl packagenya

npm install --save @ckeditor/ckeditor5-react @ckeditor/ckeditor5-build-classic


Setelah itu kita apus semua kode di index.js seperti terlihat dibawah:

 
Jadi skrg pagenya udah kosong kecuali titlenya hahhaa...


Naah... abis itu kita masukin source  code dibawah untuk index.js

import Head from 'next/head'
import styles from '../styles/Home.module.css'

import React, { useState, useEffect, useRef } from 'react'

export default function Home() {
  const editorCKRef = useRef()
  const [editorLoaded, setEditorLoaded] = useState(false)
  const { CKEditor, ClassicEditor } = editorCKRef.current || {}

  useEffect(() => {
    editorCKRef.current = {
      CKEditor: require('@ckeditor/ckeditor5-react'),
      ClassicEditor: require('@ckeditor/ckeditor5-build-classic')
    }
    setEditorLoaded(true)
  }, [])

  return (
    <div className={styles.container}>
      <Head>
        <title>My CKEditor 5</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <h2>Using CKEditor 5 build in Next JS</h2>
      {editorLoaded &&
        <CKEditor
          editor={ ClassicEditor }
          data="<p>Hello from CKEditor 5!</p>"
          onInit={ editor => {
              // You can store the "editor" and use when it is needed.
              console.log( 'Editor is ready to use!', editor );
          } }
          onChange={ ( event, editor ) => {
              const data = editor.getData();
              console.log('ON CHANGE')
              console.log(data)
              // console.log( { event, editor, data } );
          } }
          onBlur={ ( event, editor ) => {
              console.log( 'Blur.', editor );
          } }
          onFocus={ ( event, editor ) => {
              console.log( 'Focus.', editor );
          } }
        />
      }
      
    </div>
  )
}

Daaan hasilnya seperti ini:


Naah contentnya bisa dicapture di onChange, jadi difungsi itu tar bisa dikonekin ke useState trs disimpen di database lewat API. Contohnya seperti ini

Siiipss segini dl ajjah :D