React์์ suneditor-react
Classic ๋ชจ๋๋ก ์ฌ์ฉํ๋๋ฐ ํด๋ฐ ์์น๋ฅผ ์๋ํฐ ์
๋ ฅํ๋ ์ปจํ
์ธ ๋ถ๋ถ๊ณผ ๋จ์ด๋จ๋ ค์ ํ์ด์ง ์๋จ์ ๊ณ ์ ํ๊ณ ์ถ์๋ฐ
์ด์ฒ๋ผ ํด๋ฐ์ ๋ํ ์คํ์ผ์ ์ง์ ํ ์ ์๋ ์ต์
์ด ๋ฐ๋ก ์์๊น์?
toolbarContainer ์ต์
์ ์ฌ์ฉํ๋ฉด ํด๋น element ์์ ํด๋ฐ๊ฐ ์์ฑ ๋ฉ๋๋ค.
element ์ ํด๋์ค ์ด๋ฆ์ 'sun-editor` ๊ฐ ํฌํจ๋์ด ์์ด์ผ ํฉ๋๋ค.
<div id="id" class="sun-editor"></div>
{toolbarContainer: '#id' or document.querySelector('#id')}
toolbarContainer์ต์ ์ ์ฌ์ฉํ๋ฉด ํด๋นelement์์ ํด๋ฐ๊ฐ ์์ฑ ๋ฉ๋๋ค.
element์ ํด๋์ค ์ด๋ฆ์ 'sun-editor` ๊ฐ ํฌํจ๋์ด ์์ด์ผ ํฉ๋๋ค.<div id="id" class="sun-editor"></div>{toolbarContainer: '#id' or document.querySelector('#id')}
์ ๋ง ๊ฐ์ฌํฉ๋๋ค. ๋๋ถ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
ํน์ ์ถ๊ฐ๋ก ์ง๋ฌธ๋๋ ค๋ ๋ ๊น์?
1.ํด๋ฐ๋ 1๊ฐ๋ง ์ฐ๊ณ ์ปจํ ์ธ ๋ 2๊ฐ์ด์ ์ฐ๊ณ ์ถ์๋ฐ ๋ฐฉ๋ฒ์ด ์์๊น์? ์ฆ, ํด๋ฐ 1๊ฐ๋ก ๊ฐ๊ฐ์ ์ปจํ ์ธ ์ ํจ๊ณผ๋ฅผ ์ฃผ๋๋ฐฉ์.
1๋ฒ์ ํ์ฌ ๊ตฌ์กฐ๋ก๋ ๋ถ๊ฐ๋ฅ ํฉ๋๋ค.
ํฌ์ปค์ค ์ด๋ฒคํธ๋ ๊ฐ๊ฐ์ ํด๋ฐ๋ฅผ ์จ๊ธฐ๊ฑฐ๋ (toolbar.[])
๋๋ ํฌ์ปค์ค ์ด๋ฒคํธ๋ toolbarContainer ๋ฅผ ์จ๊ธฐ๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ผ ์ ์๊ฒ ๋ค์.
1๋ฒ์ ํ์ฌ ๊ตฌ์กฐ๋ก๋ ๋ถ๊ฐ๋ฅ ํฉ๋๋ค.
ํฌ์ปค์ค ์ด๋ฒคํธ๋ ๊ฐ๊ฐ์ ํด๋ฐ๋ฅผ ์จ๊ธฐ๊ฑฐ๋ (toolbar.[])
๋๋ ํฌ์ปค์ค ์ด๋ฒคํธ๋toolbarContainer๋ฅผ ์จ๊ธฐ๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ผ ์ ์๊ฒ ๋ค์.
๊ฐ๊ฐ์ ํด๋ฐ๋ฅผ ์จ๊ธฐ๊ณ ํฌ์ปค์ค์ค์ธ ์๋ํฐ์ ํด๋ฐ๋ฅผ ๋
ธ์ถ์ํค๋๊ฑด ์ฑ๊ณตํ์ต๋๋ค.
๊ทผ๋ฐ <SunEditor/><SunEditor/> ์ด๋ ๊ฒ 2๊ฐ์ ์ฌ์๋ํฐ๊ฐ ์์ ๊ฒฝ์ฐ ์ฒซ๋ฒ์งธ ์ฌ์๋ํฐ์์ ์ ์ง๋๊ณ ์๋
์คํ์ผ ๊ฐ(ํด๋ฐ)์ ๋ด์ฉ์ ๋๋ฒ์งธ ์ฌ์๋ํฐ์ defaultStyle ์์ฑ์ผ๋ก ์ ์ฉํ๊ณ ์ถ์๋ฐ ๊ฐ๋ฅํ ์ด๋ฒคํธ๊ฐ ์์๊น์?
์ต์
ํ์ด์ง๋ฅผ ์ฐธ๊ณ ์ค์ธ๋ฐ ๋ชป์ฐพ์์ ๋์์ด ํ์ํฉ๋๋ค~! ๊ฐ์ฌํฉ๋๋ค.
defaultStyle ์์ฑ์ setDefaultStyle ํจ์๋ก ๋ฐ๊ฟ์ ์์ต๋๋ค.
defaultStyle์์ฑ์ setDefaultStyle ํจ์๋ก ๋ฐ๊ฟ์ ์์ต๋๋ค.
๋ค ํด๋น์ต์
์ ์๊ณ ์๋๋ฐ, 2๊ฐ์ ์๋ํฐ๊ฐ ์๋๊ฒฝ์ฐ 1๋ฒ ์๋ํฐ์ ์คํ์ผ ์์ฑ์ ๊ฐ์ ธ์ฌ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๊น์?
1๋ฒ ์๋ํฐ์์ Bold, fontSize:15 ๋ก ์์ฑ์ค์ด๋ค๊ฐ 2๋ฒ ์๋ํฐ์๋ ์์ฑ์ ์์ํ ๋ ๋๊ฐ์ ์คํ์ผ์ ์ ์งํ๊ณ ์ถ์ด์์!
๋ง์ฝ ๋ง์ง๋ง์ ์ปค์๊ฐ ์๋ ๋ผ์ธ์ ์คํ์ผ์ ์ ์ง ํ๋ ค๋ฉด, ํ์ฌ ๋ผ์ธ์ ๋ ธ๋์ ์คํ์ผ ์ ๋ณด๋ฅผ ๋ค ํ์ธํ ํ์ 2๋ฒ์๋ํฐ์ ์ด๊ธฐ HTML์ ์ธํ ํด์ฃผ๋ ๋ฐฉ๋ฒ๋ฐ์๋ ์์ต๋๋ค.
let currentNode = core.getSelectionNode() // ๊ฑฐ์ ํ
์คํธ ๋
ธ๋๊ฐ ์ค์ง๋ง ํฌ๋งท์๋ฆฌ๋จผํธ๊ฐ ์ฌ์๋ ์์ต๋๋ค.
const tags = [core.util.createElement('br')]
while (!core.util.isFormatElement(currentNode)) {
if (currentNode.nodeType === 1) {
tags.push(currentNode.cloneNode(false))
}
currentNode = currentNode.parentElement
}
tags.push(currentNode.cloneNode(false))
const init = tags.pop();
while (tags.length > 0) {
init.appendChild(tags.pop())
}
editor2.setContents(init.outerHTML)
ํ
์คํธ ํด๋ณด์ง ์์์ง๋ง, ์ด๋ฐ์์ผ๋ก.. ํ์ฌ ์์น์์ ํฌ๋งท๊น์ง ๋
ธ๋๋ฅผ ๋ณต์ฌํ์ฌ setContents ํจ์๋ก ๋ฃ์ด ์ฃผ์ธ์.
getSelectionNode
์ ๋ง ๋น ๋ฅธ ํผ๋๋ฐฑ์ผ๋ก ๋์์ ์ฃผ์
์ ๋๋ฌด ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ๋๋ถ์ ํฐ ๋์์ ์ป์์ต๋๋ค.
๋ง์ง๋ง์ผ๋ก ์ง๋ฌธ๋๋ ค๋ ๋ ๊น์?(์ฃ์กํฉ๋๋ค)
์๋ํฐ์ ๊ธ์์ ๊ฐ์, ํ์ผ์ ํฌ๊ธฐ์ ์๊ด์์ด A4 ์ฉ์ง์ ํฌ๊ธฐ์ ๋ง๊ฒ๋ง ์ ๋ ฅ์ ๋ฐ๊ณ ์ถ์๋ฐ A4์ฉ์ง์ height๊ฐ 250mm๋ผ๊ณ ํ์์ ๊ฒฝ์ฐ char, byte, html ํฌ๊ธฐ ์ ํ์ด ์๋ ๋ผ์ธ์ ํน์ height ์ ํ์ ๊ฑธ์ด์ ๋ฑ A4์ฉ์ง ์ด์์ ์์ฑํ ์ ์๋๋ก ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๊น์? ํ ์คํธ์ ๊ทธ๋ฆผ์ ์ฒจ๋ถํ์ฌ๋ A4์ฉ์ง ์ด์์ผ๋ก ๋์ด๊ฐ์ง ์๋๋ก...(์๋ค๋ฉด ์ถ์ฒํ์๋ ๋ฐฉ์์ด ์์ผ์ ์ง..)
๋ฐ์์ ๋ฐ ์๊ฐ๋ด์ฃผ์ ์ ์ ๋ง ๊ฐ์ฌํฉ๋๋ค.
๋์์ด ๋๋ค๋ ๋คํ์ด๋ค์^^
ํ์ฌ๋ change ๋ input ์ด๋ฒคํธ๋๋ง๋ค ์๋ํฐ์ ๋์ด๋ฅผ ํ์ธํด์ ๊ธฐ์ค์ ๋์ด๊ฐ๋ฉด ๊ฒฝ๊ณ ์ฐฝ์ ๋์ฐ๊ฑฐ๋ ํ ์ ์๊ณ ..
๊ฒฝ๊ณ ์ฐฝ: noticeOpen('message'), noticeClose()
ํนํ ํ๊ธ์ ์ ๋ ฅ์ด ๋ ํ์ keyDown ์ด๋ฒคํธ๊ฐ ๋ฐ์ํด์ ํฌ๊ธฐ ์ ํ ์์๋ ์ ๋ ฅ๋ ๋ฌธ์๋ฅผ ์ง์์ฃผ๋ ์์ผ๋ก ๊ตฌํํ๊ธฐ ๋๋ฌธ์ ์์ ํ๊ฒ ๊ตฌํํ๊ธฐ๋ ์ด๋ ค์ธ๊ฒ ๊ฐ์ต๋๋ค.
editor.onInput = (e, core) {
const data = (e.data === null ? '' : e.data === undefined ? ' ' : e.data) || ''
core._getCharLength(data, 'char') // ํ์ฌ ๊ธ์์
core.context.element.wysiwygFrame.offsetHeight // ์๋ํฐ ๋์ด
}
์ค๋ฒ๋ ๊ธ์๋ฅผ ์ง์ฐ๊ณ ์ถ์ผ๋ฉด ์ฌ๊ธฐ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์. https://github.com/JiHong88/SunEditor/blob/master/src/lib/core.js#L4298
์๋ํฐ์์ ํฌ๊ธฐ์ ํ์ ๋ฒ์ด๋ ๋ฌธ์๋ฅผ ์ญ์ ํ๋ ์ฝ๋ ์
๋๋ค.
๋์์ด ๋๋ค๋ ๋คํ์ด๋ค์^^
ํ์ฌ๋ change ๋ input ์ด๋ฒคํธ๋๋ง๋ค ์๋ํฐ์ ๋์ด๋ฅผ ํ์ธํด์ ๊ธฐ์ค์ ๋์ด๊ฐ๋ฉด ๊ฒฝ๊ณ ์ฐฝ์ ๋์ฐ๊ฑฐ๋ ํ ์ ์๊ณ ..
๊ฒฝ๊ณ ์ฐฝ:noticeOpen('message'),noticeClose()ํนํ ํ๊ธ์ ์ ๋ ฅ์ด ๋ ํ์ keyDown ์ด๋ฒคํธ๊ฐ ๋ฐ์ํด์ ํฌ๊ธฐ ์ ํ ์์๋ ์ ๋ ฅ๋ ๋ฌธ์๋ฅผ ์ง์์ฃผ๋ ์์ผ๋ก ๊ตฌํํ๊ธฐ ๋๋ฌธ์ ์์ ํ๊ฒ ๊ตฌํํ๊ธฐ๋ ์ด๋ ค์ธ๊ฒ ๊ฐ์ต๋๋ค.
editor.onInput = (e, core) { const data = (e.data === null ? '' : e.data === undefined ? ' ' : e.data) || '' core._getCharLength(data, 'char') // ํ์ฌ ๊ธ์์ core.context.element.wysiwygFrame.offsetHeight // ์๋ํฐ ๋์ด }์ค๋ฒ๋ ๊ธ์๋ฅผ ์ง์ฐ๊ณ ์ถ์ผ๋ฉด ์ฌ๊ธฐ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์. https://github.com/JiHong88/SunEditor/blob/master/src/lib/core.js#L4298
์๋ํฐ์์ ํฌ๊ธฐ์ ํ์ ๋ฒ์ด๋ ๋ฌธ์๋ฅผ ์ญ์ ํ๋ ์ฝ๋ ์ ๋๋ค.
์๋ํฐ ๋์ด๊ตฌํ๋ ๋ฐฉ์์ผ๋ก ํ๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค!
๊ทผ๋ฐ ๋ฆฌ์กํธ์์ onInput ์ด๋ฒคํธ์ e ์ด๋ฒคํธ ๊ฐ์ฒด๋ง ๋ค์ด์ค๊ณ core๋ผ๋ ๊ฐ์ ์๋๋ฐ
์ด๋ป๊ฒ ์ ์ฉํ ์ ์๋๊ฑด๊ฐ์?
์.. ์ฝ๋๋ฅผ ๋ณด๋๊น ์ผ๋ถ๋ฌ ๋๊ธฐ์ง ์๋ ๊ฒ ๊ฐ๋ค์.
suneditor-react ๋ ์ ๊ฐ ๊ด๋ฆฌํ์ง ์์์ใ
core ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์๋์ง, https://github.com/mkhstar/suneditor-react ์ฌ๊ธฐ ์ด์๋ฅผ ๋จ๊ฒจ์ฃผ์ธ์.
์.. ์ฝ๋๋ฅผ ๋ณด๋๊น ์ผ๋ถ๋ฌ ๋๊ธฐ์ง ์๋ ๊ฒ ๊ฐ๋ค์.
suneditor-react ๋ ์ ๊ฐ ๊ด๋ฆฌํ์ง ์์์ใ
core ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์๋์ง, https://github.com/mkhstar/suneditor-react ์ฌ๊ธฐ ์ด์๋ฅผ ๋จ๊ฒจ์ฃผ์ธ์.
๋ค ์ด์ ๋จ๊ฒจ๋์๋๋ฐ, ์ ๊ฐ node_modules ๋ด๋ถ์์ core ๊ฐ์ฒด๋ฅผ ๋๊ธฐ๋๋ก ์ปค์คํ ํด์ ์ฌ์ฉํด๋ ๊ด์ฐฎ์๊ฑด๊ฐ์ ํน์?
์..๊ทธ๋ ๊ฒ ํ๋ฉด ๋ฐฐํฌํ ๋๋ ์ ์ฉ์ด ์๋์ง ์์๊น์.
์ฐจ๋ผ๋ฆฌ ์ด ํจํค์ง๋ฅผ react ํ๋ก์ ํธ์ ์ ์ฉํ๋๊ฒ ์ข์๊ฒ ๊ฐ์ต๋๋ค.
์..๊ทธ๋ ๊ฒ ํ๋ฉด ๋ฐฐํฌํ ๋๋ ์ ์ฉ์ด ์๋์ง ์์๊น์.
์ฐจ๋ผ๋ฆฌ ์ด ํจํค์ง๋ฅผ react ํ๋ก์ ํธ์ ์ ์ฉํ๋๊ฒ ์ข์๊ฒ ๊ฐ์ต๋๋ค.
์ผ๋จ ๋จ๊ฒจ๋์ ์ด์์ ๋ต๋ณ์ด ๋ฌ๋ฆฐ ํ์ core๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ๋ชปํ๋ค๋ฉด ํด๋น ๋ฐฉ๋ฒ๋ ๊ณ ๋ คํด๋ณด๊ฒ ์ต๋๋ค!
https://choyongjoon.com/patch-package/
ํน์, ์ ๋งํฌ์ ๋ฐฉ๋ฒ์ด ๋ฌธ์ ๊ฐ ์๋๋ค๋ฉด ์ ๋ด์ฉ์ผ๋ก ์๋ํด๋ณด๊ณ , ๋ผ์ด์ผ์ค๋ฑ์ ๋ฌธ์ ๊ฐ ๋ ๊ฒฝ์ฐ์
๋ง์ํ์ ๊ฒ์ฒ๋ผ ์ด ํจํค์ง๋ฅผ ์ ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
์ด๋ฐ ๋ฐฉ๋ฒ์ด ์์๊ตฐ์.
๊ฐ๋จํ๊ฒ ๋๊ฒ ๋ค์.
์๋ณธ์ด MIT ๋ผ์ด์ ์ค์ด๋ ๋ฌธ์ ๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์ด๋ฐ ๋ฐฉ๋ฒ์ด ์์๊ตฐ์.
๊ฐ๋จํ๊ฒ ๋๊ฒ ๋ค์.
์๋ณธ์ด MIT ๋ผ์ด์ ์ค์ด๋ ๋ฌธ์ ๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
๋ค ์ฐ์ , ์ปค์คํ
ํ์ง์๊ณ core๋ฅผ ์ธ์์๋ ๋ฐฉ๋ฒ์ด ๊ฐ์ฅ์ข์ผ๋ ์ด์์ ๋ํ ๋ต๋ณ์ ๊ธฐ๋ค๋ ค๋ณด๊ฒ ์ต๋๋ค!
์ ๋ง ๊ฐ์ฌํฉ๋๋ค.
Most helpful comment
์..๊ทธ๋ ๊ฒ ํ๋ฉด ๋ฐฐํฌํ ๋๋ ์ ์ฉ์ด ์๋์ง ์์๊น์.
์ฐจ๋ผ๋ฆฌ ์ด ํจํค์ง๋ฅผ react ํ๋ก์ ํธ์ ์ ์ฉํ๋๊ฒ ์ข์๊ฒ ๊ฐ์ต๋๋ค.