|
| 1 | +/* External dependencies */ |
| 2 | +import React, { |
| 3 | + createContext, |
| 4 | + forwardRef, |
| 5 | + useMemo, |
| 6 | + useEffect, |
| 7 | + useRef, |
| 8 | + useCallback, |
| 9 | + useImperativeHandle, |
| 10 | +} from 'react' |
| 11 | +import { DOMParser, ResolvedPos } from 'prosemirror-model' |
| 12 | +import { isEmpty, noop } from 'lodash-es' |
| 13 | +import 'prosemirror-view/style/prosemirror.css' |
| 14 | + |
| 15 | +/* Internal dependencies */ |
| 16 | +import useMergeRefs from '../../hooks/useMergeRefs' |
| 17 | +import EditorBuilder from './utils/EditorBuilder' |
| 18 | +import { blocksToPMNodes, pmNodesToBlocks } from './utils/Parsers' |
| 19 | +import { ReactNodeViewProvider } from './utils/ReactNodeView' |
| 20 | +import isFill from './utils/isFill' |
| 21 | +import { EditorProps, EditorRef } from './Editor.types' |
| 22 | +import { StyledEditorInput } from './Editor.styled' |
| 23 | + |
| 24 | +export const BuilderContext = createContext(new EditorBuilder()) |
| 25 | + |
| 26 | +const emptyBlocks = [] |
| 27 | + |
| 28 | +function Editor( |
| 29 | + { |
| 30 | + initialBlocks = emptyBlocks, |
| 31 | + noStringEscape = false, |
| 32 | + onChange = noop, |
| 33 | + onClear = noop, |
| 34 | + children, |
| 35 | + }: EditorProps, |
| 36 | + forwardedRef: React.Ref<EditorRef>, |
| 37 | +) { |
| 38 | + const reactNodeViewProvider = useMemo(() => new ReactNodeViewProvider(), []) |
| 39 | + const editorBuilder: EditorBuilder = useMemo(() => new EditorBuilder(), []) |
| 40 | + |
| 41 | + const editorRef = useRef<HTMLDivElement | null>(null) |
| 42 | + const mergedRef = useMergeRefs<HTMLDivElement | EditorRef>(editorRef, forwardedRef) |
| 43 | + |
| 44 | + const getContents = useCallback(() => { |
| 45 | + const editorState = editorBuilder.editor?.state |
| 46 | + if (!editorState || !editorState.doc) { |
| 47 | + return undefined |
| 48 | + } |
| 49 | + |
| 50 | + const blocks = isFill(editorState) ? pmNodesToBlocks(editorState.doc, noStringEscape) : [] |
| 51 | + |
| 52 | + return blocks |
| 53 | + }, [ |
| 54 | + editorBuilder, |
| 55 | + noStringEscape, |
| 56 | + ]) |
| 57 | + |
| 58 | + const clearContents = useCallback(() => { |
| 59 | + editorBuilder.reset() |
| 60 | + onClear() |
| 61 | + }, [ |
| 62 | + editorBuilder, |
| 63 | + onClear, |
| 64 | + ]) |
| 65 | + |
| 66 | + useImperativeHandle(forwardedRef, () => ({ |
| 67 | + getContents, |
| 68 | + clearContents, |
| 69 | + }), [ |
| 70 | + getContents, |
| 71 | + clearContents, |
| 72 | + ]) |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + const schema = editorBuilder.createSchema() |
| 76 | + const doc = (() => { |
| 77 | + if (initialBlocks) { |
| 78 | + const content = blocksToPMNodes(initialBlocks, schema) |
| 79 | + if (content.length > 0) { |
| 80 | + return schema.node('doc', undefined, content) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return undefined |
| 85 | + })() |
| 86 | + |
| 87 | + editorBuilder.build(editorRef.current!, reactNodeViewProvider, schema, { |
| 88 | + dispatchTransaction: (transaction) => { |
| 89 | + const prevState = editorBuilder.editor!.state |
| 90 | + const newState = editorBuilder.editor!.state.apply(transaction) |
| 91 | + editorBuilder.updateState(newState) |
| 92 | + |
| 93 | + // selection 만 바뀌는 경우에도 transaction 이 발생하지만, 이 경우에는 onChange 는 불필요하므로 |
| 94 | + // doc 이 분명하게 바꾼 경우에만 onChange 발생 |
| 95 | + if (prevState.doc !== newState.doc) { |
| 96 | + onChange(newState) |
| 97 | + } |
| 98 | + }, |
| 99 | + handleDOMEvents: { |
| 100 | + // blur: () => { |
| 101 | + // onBlurRef.current() |
| 102 | + // return false |
| 103 | + // }, |
| 104 | + // focus: () => { |
| 105 | + // onFocusRef.current() |
| 106 | + // return false |
| 107 | + // }, |
| 108 | + }, |
| 109 | + clipboardTextParser: (pastedText: string, context: ResolvedPos) => { |
| 110 | + /** |
| 111 | + * ClipBoard 로 부터 Text 를 파싱할 경우, |
| 112 | + * ProseMirror 기본 동작은 newline 을 모두 무시함. |
| 113 | + * 따라서 newline 을 지켜주는 parser 를 새로 구성함. |
| 114 | + * 참조: https://prosemirror.net/docs/ref/#view.EditorProps.clipboardTextParser |
| 115 | + */ |
| 116 | + const dom = window.document.createElement('div') |
| 117 | + |
| 118 | + pastedText |
| 119 | + .trim() |
| 120 | + .split('\n') |
| 121 | + .forEach(block => { |
| 122 | + if (isEmpty(block)) { |
| 123 | + dom.appendChild(document.createElement('p')) |
| 124 | + } else { |
| 125 | + dom.appendChild(document.createElement('p')).textContent = block |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + return DOMParser |
| 130 | + .fromSchema(editorBuilder.editor!.state.schema) |
| 131 | + .parseSlice(dom, { |
| 132 | + preserveWhitespace: true, |
| 133 | + context, |
| 134 | + }) |
| 135 | + }, |
| 136 | + // clipboardTextSerializer: nodeToText, |
| 137 | + }, doc) |
| 138 | + |
| 139 | + return () => { |
| 140 | + reactNodeViewProvider.clear() |
| 141 | + } |
| 142 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 143 | + }, []) |
| 144 | + |
| 145 | + return ( |
| 146 | + <BuilderContext.Provider value={editorBuilder}> |
| 147 | + <StyledEditorInput ref={mergedRef as React.Ref<HTMLDivElement>}/> |
| 148 | + { children } |
| 149 | + </BuilderContext.Provider> |
| 150 | + ) |
| 151 | +} |
| 152 | + |
| 153 | +export default forwardRef(Editor) |
0 commit comments