|
1 | 1 | import * as React from "react"; |
2 | 2 | import * as ReactDOM from "react-dom"; |
3 | | -import { Map } from "immutable"; |
4 | 3 |
|
5 | 4 | import { |
6 | 5 | ContentBlock, |
@@ -48,154 +47,141 @@ class HandleSpan extends React.Component { |
48 | 47 | } |
49 | 48 |
|
50 | 49 | class RichEditorExample extends React.Component<{}, { editorState: EditorState }> { |
51 | | - constructor() { |
52 | | - super({}); |
53 | | - |
54 | | - const sampleMarkup = |
55 | | - '<b>Bold text</b>, <i>Italic text</i><br/ ><br />' + |
56 | | - '<a href="http://www.facebook.com">Example link</a><br /><br/ >' + |
57 | | - '<img src="image.png" height="112" width="200" />'; |
58 | | - const blocksFromHTML = convertFromHTML(sampleMarkup); |
59 | | - const state = ContentState.createFromBlockArray( |
60 | | - blocksFromHTML.contentBlocks, |
61 | | - blocksFromHTML.entityMap, |
62 | | - ); |
63 | | - const decorator = new CompositeDecorator([{ |
64 | | - strategy: ( |
65 | | - block: ContentBlock, |
66 | | - callback: (start: number, end: number) => void, |
67 | | - contentState: ContentState |
68 | | - ) => { |
69 | | - const text = block.getText(); |
70 | | - let matchArr, start; |
71 | | - while ((matchArr = HANDLE_REGEX.exec(text)) !== null) { |
72 | | - start = matchArr.index; |
73 | | - callback(start, start + matchArr[0].length); |
| 50 | + static initState() { |
| 51 | + const sampleMarkup = |
| 52 | + '<b>Bold text</b>, <i>Italic text</i><br/ ><br />' + |
| 53 | + '<a href="http://www.facebook.com">Example link</a><br /><br/ >' + |
| 54 | + '<img src="image.png" height="112" width="200" />'; |
| 55 | + const blocksFromHTML = convertFromHTML(sampleMarkup); |
| 56 | + const state = ContentState.createFromBlockArray(blocksFromHTML.contentBlocks, blocksFromHTML.entityMap); |
| 57 | + const decorator = new CompositeDecorator([ |
| 58 | + { |
| 59 | + strategy: ( |
| 60 | + block: ContentBlock, |
| 61 | + callback: (start: number, end: number) => void, |
| 62 | + contentState: ContentState |
| 63 | + ) => { |
| 64 | + const text = block.getText(); |
| 65 | + let matchArr, start; |
| 66 | + while ((matchArr = HANDLE_REGEX.exec(text)) !== null) { |
| 67 | + start = matchArr.index; |
| 68 | + callback(start, start + matchArr[0].length); |
| 69 | + } |
| 70 | + }, |
| 71 | + component: HandleSpan |
| 72 | + } |
| 73 | + ]); |
| 74 | + return { editorState: EditorState.createWithContent(state, decorator) }; |
| 75 | + } |
| 76 | + state = RichEditorExample.initState() |
| 77 | + |
| 78 | + onChange: (editorState: EditorState) => void = (editorState: EditorState) => this.setState({ editorState }); |
| 79 | + |
| 80 | + keyBindingFn(e: SyntheticKeyboardEvent): string { |
| 81 | + if (e.keyCode === KEYCODES.ENTER) { |
| 82 | + const { editorState } = this.state; |
| 83 | + const contentState = editorState.getCurrentContent(); |
| 84 | + const selectionState = editorState.getSelection(); |
| 85 | + |
| 86 | + // only split headers into header and unstyled if we press 'Enter' |
| 87 | + // at the end of a header (without text selected) |
| 88 | + if (selectionState.isCollapsed()) { |
| 89 | + const endKey = selectionState.getEndKey(); |
| 90 | + const endOffset = selectionState.getEndOffset(); |
| 91 | + const endBlock = contentState.getBlockForKey(endKey); |
| 92 | + if (isHeaderBlock(endBlock) && endOffset === endBlock.getText().length) { |
| 93 | + return SPLIT_HEADER_BLOCK; |
| 94 | + } |
| 95 | + } |
74 | 96 | } |
75 | | - }, |
76 | | - component: HandleSpan, |
77 | | - }]); |
78 | | - this.state = { editorState: EditorState.createWithContent(state, decorator) }; |
79 | | - } |
80 | 97 |
|
81 | | - onChange: (editorState: EditorState) => void = (editorState: EditorState) => this.setState({ editorState }); |
82 | | - |
83 | | - keyBindingFn(e: SyntheticKeyboardEvent): string { |
84 | | - if (e.keyCode === KEYCODES.ENTER) { |
85 | | - const { editorState } = this.state; |
86 | | - const contentState = editorState.getCurrentContent(); |
87 | | - const selectionState = editorState.getSelection(); |
88 | | - |
89 | | - // only split headers into header and unstyled if we press 'Enter' |
90 | | - // at the end of a header (without text selected) |
91 | | - if (selectionState.isCollapsed()) { |
92 | | - const endKey = selectionState.getEndKey(); |
93 | | - const endOffset = selectionState.getEndOffset(); |
94 | | - const endBlock = contentState.getBlockForKey(endKey); |
95 | | - if (isHeaderBlock(endBlock) && endOffset === endBlock.getText().length) { |
96 | | - return SPLIT_HEADER_BLOCK; |
97 | | - } |
98 | | - } |
| 98 | + return getDefaultKeyBinding(e); |
99 | 99 | } |
100 | 100 |
|
101 | | - return getDefaultKeyBinding(e); |
102 | | - } |
| 101 | + handleKeyCommand = (command: string, editorState: EditorState) => { |
| 102 | + if (command === SPLIT_HEADER_BLOCK) { |
| 103 | + this.onChange(this.splitHeaderToNewBlock()); |
| 104 | + return 'handled'; |
| 105 | + } |
103 | 106 |
|
104 | | - handleKeyCommand = (command: string, editorState: EditorState) => { |
105 | | - if (command === SPLIT_HEADER_BLOCK) { |
106 | | - this.onChange(this.splitHeaderToNewBlock()); |
107 | | - return 'handled'; |
108 | | - } |
| 107 | + const newState = RichUtils.handleKeyCommand(editorState, command); |
109 | 108 |
|
110 | | - const newState = RichUtils.handleKeyCommand(editorState, command); |
| 109 | + if (newState) { |
| 110 | + this.onChange(newState); |
| 111 | + return 'handled'; |
| 112 | + } |
111 | 113 |
|
112 | | - if (newState) { |
113 | | - this.onChange(newState); |
114 | | - return "handled"; |
115 | | - } |
| 114 | + return 'not-handled'; |
| 115 | + }; |
116 | 116 |
|
117 | | - return "not-handled"; |
118 | | - } |
| 117 | + toggleBlockType: (blockType: string) => void = (blockType: string) => { |
| 118 | + this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType)); |
| 119 | + }; |
119 | 120 |
|
120 | | - toggleBlockType: (blockType: string) => void = (blockType: string) => { |
121 | | - this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType)); |
122 | | - } |
| 121 | + toggleInlineStyle: (inlineStyle: string) => void = (inlineStyle: string) => { |
| 122 | + this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle)); |
| 123 | + }; |
123 | 124 |
|
124 | | - toggleInlineStyle: (inlineStyle: string) => void = (inlineStyle: string) => { |
125 | | - this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle)); |
126 | | - } |
| 125 | + splitHeaderToNewBlock(): EditorState { |
| 126 | + const { editorState } = this.state; |
| 127 | + const selection = editorState.getSelection(); |
127 | 128 |
|
128 | | - splitHeaderToNewBlock(): EditorState { |
129 | | - const { editorState } = this.state; |
130 | | - const selection = editorState.getSelection(); |
131 | | - |
132 | | - // Add a new block after the cursor |
133 | | - const contentWithBlock = Modifier.splitBlock( |
134 | | - editorState.getCurrentContent(), |
135 | | - selection, |
136 | | - ); |
137 | | - |
138 | | - // Change the new block type to be normal 'unstyled' text, |
139 | | - const newBlock = contentWithBlock.getBlockAfter(selection.getEndKey()); |
140 | | - const contentWithUnstyledBlock = Modifier.setBlockType( |
141 | | - contentWithBlock, |
142 | | - SelectionState.createEmpty(newBlock.getKey()), |
143 | | - 'unstyled', |
144 | | - ); |
145 | | - |
146 | | - // push the new state with 'insert-characters' to preserve the undo/redo stack |
147 | | - const stateWithNewline = EditorState.push( |
148 | | - editorState, |
149 | | - contentWithUnstyledBlock, |
150 | | - 'insert-characters' |
151 | | - ); |
152 | | - |
153 | | - // manually move the cursor to the next line (as expected) |
154 | | - const nextState = EditorState.forceSelection( |
155 | | - stateWithNewline, |
156 | | - SelectionState.createEmpty(newBlock.getKey()), |
157 | | - ); |
158 | | - |
159 | | - return nextState; |
160 | | - } |
| 129 | + // Add a new block after the cursor |
| 130 | + const contentWithBlock = Modifier.splitBlock(editorState.getCurrentContent(), selection); |
161 | 131 |
|
162 | | - render(): React.ReactElement<{}> { |
163 | | - // If the user changes block type before entering any text, we can |
164 | | - // either style the placeholder or hide it. Let's just hide it now. |
165 | | - let className = 'RichEditor-editor'; |
166 | | - var contentState = this.state.editorState.getCurrentContent(); |
167 | | - if (!contentState.hasText()) { |
168 | | - if (contentState.getBlockMap().first().getType() !== 'unstyled') { |
169 | | - className += ' RichEditor-hidePlaceholder'; |
170 | | - } |
| 132 | + // Change the new block type to be normal 'unstyled' text, |
| 133 | + const newBlock = contentWithBlock.getBlockAfter(selection.getEndKey()); |
| 134 | + const contentWithUnstyledBlock = Modifier.setBlockType( |
| 135 | + contentWithBlock, |
| 136 | + SelectionState.createEmpty(newBlock.getKey()), |
| 137 | + 'unstyled' |
| 138 | + ); |
| 139 | + |
| 140 | + // push the new state with 'insert-characters' to preserve the undo/redo stack |
| 141 | + const stateWithNewline = EditorState.push(editorState, contentWithUnstyledBlock, 'insert-characters'); |
| 142 | + |
| 143 | + // manually move the cursor to the next line (as expected) |
| 144 | + const nextState = EditorState.forceSelection(stateWithNewline, SelectionState.createEmpty(newBlock.getKey())); |
| 145 | + |
| 146 | + return nextState; |
171 | 147 | } |
172 | 148 |
|
173 | | - return ( |
174 | | - <div className="RichEditor-root"> |
175 | | - <BlockStyleControls |
176 | | - editorState={this.state.editorState} |
177 | | - onToggle={this.toggleBlockType} |
178 | | - /> |
179 | | - <InlineStyleControls |
180 | | - editorState={this.state.editorState} |
181 | | - onToggle={this.toggleInlineStyle} |
182 | | - /> |
183 | | - <div className={className}> |
184 | | - <Editor |
185 | | - blockStyleFn={getBlockStyle} |
186 | | - customStyleMap={styleMap} |
187 | | - editorState={this.state.editorState} |
188 | | - keyBindingFn={this.keyBindingFn} |
189 | | - handleKeyCommand={this.handleKeyCommand} |
190 | | - onChange={this.onChange} |
191 | | - placeholder="Tell a story..." |
192 | | - ref="editor" |
193 | | - spellCheck={true} |
194 | | - /> |
195 | | - </div> |
196 | | - </div> |
197 | | - ); |
| 149 | + render(): React.ReactElement<{}> { |
| 150 | + // If the user changes block type before entering any text, we can |
| 151 | + // either style the placeholder or hide it. Let's just hide it now. |
| 152 | + let className = 'RichEditor-editor'; |
| 153 | + var contentState = this.state.editorState.getCurrentContent(); |
| 154 | + if (!contentState.hasText()) { |
| 155 | + if ( |
| 156 | + contentState |
| 157 | + .getBlockMap() |
| 158 | + .first() |
| 159 | + .getType() !== 'unstyled' |
| 160 | + ) { |
| 161 | + className += ' RichEditor-hidePlaceholder'; |
| 162 | + } |
198 | 163 | } |
| 164 | + |
| 165 | + return ( |
| 166 | + <div className="RichEditor-root"> |
| 167 | + <BlockStyleControls editorState={this.state.editorState} onToggle={this.toggleBlockType} /> |
| 168 | + <InlineStyleControls editorState={this.state.editorState} onToggle={this.toggleInlineStyle} /> |
| 169 | + <div className={className}> |
| 170 | + <Editor |
| 171 | + blockStyleFn={getBlockStyle} |
| 172 | + customStyleMap={styleMap} |
| 173 | + editorState={this.state.editorState} |
| 174 | + keyBindingFn={this.keyBindingFn} |
| 175 | + handleKeyCommand={this.handleKeyCommand} |
| 176 | + onChange={this.onChange} |
| 177 | + placeholder="Tell a story..." |
| 178 | + ref="editor" |
| 179 | + spellCheck={true} |
| 180 | + /> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + ); |
| 184 | + } |
199 | 185 | } |
200 | 186 |
|
201 | 187 | // Custom overrides for "code" style. |
|
0 commit comments