11import { useCallback , useEffect , useLayoutEffect , useRef } from 'react' ;
2- import Quill from 'quill' ;
2+ import Quill , { Range as QuillRange } from 'quill' ;
33import { useUploadShowContentImage } from '@boolti/api' ;
44import Styled from './QuillEditor.styles' ;
55import './blot' ;
@@ -83,7 +83,7 @@ const QuillEditor: React.FC<EditorProps> = ({
8383 const editorElement = editorElementRef . current ;
8484 if ( ! editorElement ) return ;
8585
86- const quill = new Quill ( editorElement , {
86+ quillRef . current = new Quill ( editorElement , {
8787 modules : {
8888 toolbar : {
8989 container : [
@@ -96,14 +96,39 @@ const QuillEditor: React.FC<EditorProps> = ({
9696 video : videoUploadHandler ,
9797 } ,
9898 } ,
99+ keyboard : {
100+ bindings : {
101+ enter : {
102+ key : 'Enter' ,
103+ collapsed : true ,
104+ handler : ( range : QuillRange ) => {
105+ if ( ! quillRef . current || ! document . activeElement ) return
106+
107+ const selection = document . getSelection ( ) ;
108+ selection ?. removeAllRanges ( ) ;
109+ quillRef . current . setSelection ( range . index , 0 ) ;
110+
111+ /*
112+ * iOS 웹 브라우저 환경에서 발생하는 한글 IME 입력 버퍼 이슈를 해결하기 위한 코드
113+ * Selection.addRange 실행 시 일부러 에러를 발생시켜, 강제로 버퍼를 비우게 한다.
114+ * try catch 문으로 wrap하면 버퍼가 비워지지 않으니 주의할 것.
115+ */
116+ selection ?. addRange ( { } as Range ) ;
117+
118+ quillRef . current . insertText ( range . index , '\n' ) ;
119+ quillRef . current . focus ( )
120+
121+ return false ;
122+ }
123+ } ,
124+ } ,
125+ } ,
99126 } ,
100127 placeholder,
101128 readOnly,
102129 theme : 'snow' ,
103130 } ) ;
104131
105- quillRef . current = quill ;
106-
107132 if ( defaultValueRef . current ) {
108133 quillRef . current . clipboard . dangerouslyPasteHTML ( defaultValueRef . current ) ;
109134 }
@@ -119,20 +144,19 @@ const QuillEditor: React.FC<EditorProps> = ({
119144 onBlurRef . current ?.( ! text ) ;
120145 } ) ;
121146
122-
123147 // 한글 입력 IME 입력 이벤트 감지 및 강제 리렌더링
124148 quillRef . current . root . addEventListener ( 'compositionstart' , ( ) => {
125149 setTimeout ( ( ) => {
126- quill . root . classList . remove ( 'ql-blank' ) ;
150+ quillRef . current ? .root . classList . remove ( 'ql-blank' ) ;
127151 } , 0 ) ;
128152 } ) ;
129153
130154 quillRef . current . root . addEventListener ( 'input' , ( ) => {
131155 setTimeout ( ( ) => {
132156 if ( quillRef . current ?. root . innerText . trim ( ) === '' ) {
133- quill . root . classList . add ( 'ql-blank' ) ;
157+ quillRef . current . root . classList . add ( 'ql-blank' ) ;
134158 } else {
135- quill . root . classList . remove ( 'ql-blank' ) ;
159+ quillRef . current ? .root . classList . remove ( 'ql-blank' ) ;
136160 }
137161 } , 0 ) ;
138162 } ) ;
0 commit comments