WYSIWYG μλν° - CKEditor
β’
μμ§μ
μλν°λ?
β’
React μ CKEditor μ€μΉ
β’
μνμ½λ
β¦
import
β¦
μ»΄ν¬λνΈ
μμ§μ μλν°λ?
WYSIWYG (What You See Is What You Get)
: μμ±νλ λ΄μ©μ κ·Έλλ‘ λ³΄λ©΄μ νΈμ§ν μ μλ μλν°
μμ§μ
(What You See Is What You Get) μλν°λ μ¬μ©μκ° κ²°κ³Όλ¬Όμ 보면μ μ½ν
μΈ λ₯Ό νΈμ§ν μ μλ λꡬμ
λλ€. μ¬μ©μλ ν
μ€νΈ νμ, μ΄λ―Έμ§, ν λ±μ μ§μ μ‘°μνμ¬ μΉ μ½ν
μΈ λ₯Ό λ§λ€ μ μμ΅λλ€. μ΄λ¬ν μλν°λ μ¬μ©μκ° HTMLμ΄λ CSS κ°μ λ§ν¬μ
μΈμ΄λ₯Ό λͺ°λΌλ μΉ μ½ν
μΈ λ₯Ό μ½κ² λ§λ€ μ μκ² ν΄μ€λλ€.
μΉμμλ HTML νμμΌλ‘ 컨ν
μΈ κ° λ³΄μ¬μ§λλ°, μΌλ° ν
μ€νΈλ₯Ό μμ±νλ©΄, μ λͺ©, λ¬Έλ¨, μ΄λ―Έμ§ λ±μ κ·Έλλ‘ λ³Ό μκ° μλ€. κ·Έλ°λ° μμ§μ
μλν°λ₯Ό μ΄μ©νλ©΄, μμ±νλ 컨ν
μΈ λ₯Ό κ·Έλλ‘ HTML λ‘ λ³ννμ¬ λ³΄μ¬μ£ΌκΈ° λλ¬Έμ, μλ κ·Έλλ‘ μμ±νλ©΄μ λ³Ό μ μλ€.
React μ CKEditor μ€μΉ
npm install --save @ckeditor/ckeditor5-react @ckeditor/ckeditor5-build-classic
Bash
볡μ¬
npm install --save @ckeditor/ckeditor5-alignment
Bash
볡μ¬
μνμ½λ
import
// ckeditor5
import { CKEditor } from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
JavaScript
볡μ¬
μ»΄ν¬λνΈ
<CKEditor
editor={ ClassicEditor }
config={{
placeholder: "λ΄μ©μ μ
λ ₯νμΈμ.",
toolbar: {
items: [
'undo', 'redo',
'|', 'heading',
'|', 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor',
'|', 'bold', 'italic', 'strikethrough', 'subscript', 'superscript', 'code',
'|', 'bulletedList', 'numberedList', 'todoList', 'outdent', 'indent',
'|', 'link', 'uploadImage', 'blockQuote', 'codeBlock',
'|', 'mediaEmbed',
],
shouldNotGroupWhenFull: false
},
editorConfig: {
height: 500, // Set the desired height in pixels
},
alignment: {
options: ['left', 'center', 'right', 'justify'],
},
// extraPlugins: [uploadPlugin] // μ
λ‘λ νλ¬κ·ΈμΈ
}}
data="" // β κΈ°μ‘΄ 컨ν
μΈ λ΄μ© μ
λ ₯ (HTML)
onReady={ 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( { event, editor, data } );
setContent(data);
} }
onBlur={ ( event, editor ) => {
console.log( 'Blur.', editor );
} }
onFocus={ ( event, editor ) => {
console.log( 'Focus.', editor );
} }
/>
JavaScript
볡μ¬
μλν° μ¬μ©
β’
κ²μκΈ λ±λ‘
β’
κ²μκΈ μμ
β’
κ²μκΈ μ‘°ν
κ²μκΈ λ±λ‘
<CKEditor
editor={ ClassicEditor }
config={{
placeholder: "λ΄μ©μ μ
λ ₯νμΈμ.",
toolbar: {
items: [
'undo', 'redo',
'|', 'heading',
'|', 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor',
'|', 'bold', 'italic', 'strikethrough', 'subscript', 'superscript', 'code',
'|', 'bulletedList', 'numberedList', 'todoList', 'outdent', 'indent',
'|', 'link', 'uploadImage', 'blockQuote', 'codeBlock',
'|', 'mediaEmbed',
],
shouldNotGroupWhenFull: false
},
editorConfig: {
height: 500, // Set the desired height in pixels
},
alignment: {
options: ['left', 'center', 'right', 'justify'],
},
// extraPlugins: [uploadPlugin] // μ
λ‘λ νλ¬κ·ΈμΈ
}}
data="" // β κΈ°μ‘΄ 컨ν
μΈ λ΄μ© μ
λ ₯ (HTML)
onReady={ 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( { event, editor, data } );
setContent(data);
} }
onBlur={ ( event, editor ) => {
console.log( 'Blur.', editor );
} }
onFocus={ ( event, editor ) => {
console.log( 'Focus.', editor );
} }
/>
JavaScript
볡μ¬
κ²μκΈ μμ
<CKEditor
editor={ ClassicEditor }
config={{
placeholder: "λ΄μ©μ μ
λ ₯νμΈμ.",
toolbar: {
items: [
'undo', 'redo',
'|', 'heading',
'|', 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor',
'|', 'bold', 'italic', 'strikethrough', 'subscript', 'superscript', 'code',
'|', 'bulletedList', 'numberedList', 'todoList', 'outdent', 'indent',
'|', 'link', 'uploadImage', 'blockQuote', 'codeBlock',
'|', 'mediaEmbed',
],
shouldNotGroupWhenFull: false
},
editorConfig: {
height: 500, // Set the desired height in pixels
},
alignment: {
options: ['left', 'center', 'right', 'justify'],
},
// extraPlugins: [uploadPlugin] // μ
λ‘λ νλ¬κ·ΈμΈ
}}
data={content} // β κΈ°μ‘΄ 컨ν
μΈ λ΄μ© μ
λ ₯ (HTML)
onReady={ 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( { event, editor, data } );
setContent(data);
} }
onBlur={ ( event, editor ) => {
console.log( 'Blur.', editor );
} }
onFocus={ ( event, editor ) => {
console.log( 'Focus.', editor );
} }
/>
JavaScript
볡μ¬
κ²μκΈ μ‘°ν
<CKEditor editor={ ClassicEditor }
data={ content } // μ‘°νν λ°μ΄ν° 컨ν
μΈ
disabled={true}
config={{
toolbar: [],
}}
/>
JavaScript
볡μ¬
μλν°μμ νμΌ μ λ‘λ μ μ©
β’
uploadPlugin() ν¨μ μ μ
β’
customUploadAdapter() ν¨μ μ μ
β’
extraPlugins: [uploadPlugin]
uploadPlugin() ν¨μ μ μ
function uploadPlugin(editor) {
editor.plugins.get("FileRepository").createUploadAdapter = (loader) => {
return customUploadAdapter(loader);
};
}
JavaScript
볡μ¬
customUploadAdapter() ν¨μ μ μ
const customUploadAdapter = (loader) => {
return {
upload() {
return new Promise( (resolve, reject) => {
const formData = new FormData();
loader.file.then( async (file) => {
console.log(file);
formData.append("parentTable", 'editor');
formData.append("file", file);
const headers = {
headers: {
'Content-Type' : 'multipart/form-data',
},
};
let response = await files.upload(formData, headers);
let data = await response.data;
console.log(`data : ${data}`);
let newFile = data;
let newFileNo = newFile.no
// μ΄λ―Έμ§ λ λλ§
await resolve({
default: `http://localhost:8080/files/img/${newFileNo}`
})
});
});
},
};
};
JavaScript
볡μ¬
extraPlugins: [uploadPlugin] μ€μ
extraPlugins: [uploadPlugin]
<CKEditor
editor={ ClassicEditor }
config={{
placeholder: "λ΄μ©μ μ
λ ₯νμΈμ.",
toolbar: {
items: [
'undo', 'redo',
'|', 'heading',
'|', 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor',
'|', 'bold', 'italic', 'strikethrough', 'subscript', 'superscript', 'code',
'|', 'bulletedList', 'numberedList', 'todoList', 'outdent', 'indent',
'|', 'link', 'uploadImage', 'blockQuote', 'codeBlock',
'|', 'mediaEmbed',
],
shouldNotGroupWhenFull: false
},
editorConfig: {
height: 500, // Set the desired height in pixels
},
alignment: {
options: ['left', 'center', 'right', 'justify'],
},
extraPlugins: [uploadPlugin] // μ
λ‘λ νλ¬κ·ΈμΈ
}}
data="" // β κΈ°μ‘΄ 컨ν
μΈ λ΄μ© μ
λ ₯ (HTML)
onReady={ 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( { event, editor, data } );
setContent(data);
} }
onBlur={ ( event, editor ) => {
console.log( 'Blur.', editor );
} }
onFocus={ ( event, editor ) => {
console.log( 'Focus.', editor );
} }
/>
JavaScript
볡μ¬