|
15 | 15 | import type DraftEditor from 'DraftEditor.react'; |
16 | 16 | import type {DraftInlineStyle} from 'DraftInlineStyle'; |
17 | 17 |
|
18 | | -const BlockTree = require('BlockTree'); |
19 | 18 | const DraftModifier = require('DraftModifier'); |
20 | 19 | const EditorState = require('EditorState'); |
21 | 20 | const UserAgent = require('UserAgent'); |
22 | 21 |
|
23 | 22 | const getEntityKeyForSelection = require('getEntityKeyForSelection'); |
24 | | -const gkx = require('gkx'); |
25 | 23 | const isEventHandled = require('isEventHandled'); |
26 | 24 | const isSelectionAtLeafStart = require('isSelectionAtLeafStart'); |
27 | 25 | const nullthrows = require('nullthrows'); |
@@ -190,73 +188,60 @@ function editOnBeforeInput( |
190 | 188 | } |
191 | 189 | } |
192 | 190 | if (!mustPreventNative) { |
193 | | - if (gkx('draft_improved_decorator_fingerprint')) { |
194 | | - // Let's say we have a decorator that highlights hashtags. In many cases |
195 | | - // we need to prevent native behavior and rerender ourselves -- |
196 | | - // particularly, any case *except* where the inserted characters end up |
197 | | - // anywhere except exactly where you put them. |
198 | | - // |
199 | | - // Using [] to denote a decorated leaf, some examples: |
200 | | - // |
201 | | - // 1. 'hi #' and append 'f' |
202 | | - // desired rendering: 'hi [#f]' |
203 | | - // native rendering would be: 'hi #f' (incorrect) |
204 | | - // |
205 | | - // 2. 'x [#foo]' and insert '#' before 'f' |
206 | | - // desired rendering: 'x #[#foo]' |
207 | | - // native rendering would be: 'x [##foo]' (incorrect) |
208 | | - // |
209 | | - // 3. '[#foobar]' and insert ' ' between 'foo' and 'bar' |
210 | | - // desired rendering: '[#foo] bar' |
211 | | - // native rendering would be: '[#foo bar]' (incorrect) |
212 | | - // |
213 | | - // 4. '[#foo]' and delete '#' [won't use this beforeinput codepath though] |
214 | | - // desired rendering: 'foo' |
215 | | - // native rendering would be: '[foo]' (incorrect) |
216 | | - // |
217 | | - // 5. '[#foo]' and append 'b' |
218 | | - // desired rendering: '[#foob]' |
219 | | - // native rendering would be: '[#foob]' (native insertion is OK here) |
220 | | - // |
221 | | - // It is safe to allow native insertion if and only if the full list of |
222 | | - // decorator ranges matches what we expect native insertion to give. We |
223 | | - // don't need to compare the content because the only possible mutation |
224 | | - // to consider here is inserting plain text and decorators can't affect |
225 | | - // text content. |
226 | | - const oldBlockTree = editorState.getBlockTree(anchorKey); |
227 | | - const newBlockTree = newEditorState.getBlockTree(anchorKey); |
228 | | - mustPreventNative = |
229 | | - oldBlockTree.size !== newBlockTree.size || |
230 | | - oldBlockTree.zip(newBlockTree).some(([oldLeafSet, newLeafSet]) => { |
231 | | - // selectionStart is guaranteed to be selectionEnd here |
232 | | - const oldStart = oldLeafSet.get('start'); |
233 | | - const adjustedStart = |
234 | | - oldStart + (oldStart >= selectionStart ? chars.length : 0); |
235 | | - const oldEnd = oldLeafSet.get('end'); |
236 | | - const adjustedEnd = |
237 | | - oldEnd + (oldEnd >= selectionStart ? chars.length : 0); |
238 | | - return ( |
239 | | - // Different decorators |
240 | | - oldLeafSet.get('decoratorKey') !== newLeafSet.get('decoratorKey') || |
241 | | - // Different number of inline styles |
242 | | - oldLeafSet.get('leaves').size !== newLeafSet.get('leaves').size || |
243 | | - // Different effective decorator position |
244 | | - adjustedStart !== newLeafSet.get('start') || |
245 | | - adjustedEnd !== newLeafSet.get('end') |
246 | | - ); |
247 | | - }); |
248 | | - } else { |
249 | | - // Check the old and new "fingerprints" of the current block to determine |
250 | | - // whether this insertion requires any addition or removal of text nodes, |
251 | | - // in which case we would prevent the native character insertion. |
252 | | - const originalFingerprint = BlockTree.getFingerprint( |
253 | | - editorState.getBlockTree(anchorKey), |
254 | | - ); |
255 | | - const newFingerprint = BlockTree.getFingerprint( |
256 | | - newEditorState.getBlockTree(anchorKey), |
257 | | - ); |
258 | | - mustPreventNative = originalFingerprint !== newFingerprint; |
259 | | - } |
| 191 | + // Let's say we have a decorator that highlights hashtags. In many cases |
| 192 | + // we need to prevent native behavior and rerender ourselves -- |
| 193 | + // particularly, any case *except* where the inserted characters end up |
| 194 | + // anywhere except exactly where you put them. |
| 195 | + // |
| 196 | + // Using [] to denote a decorated leaf, some examples: |
| 197 | + // |
| 198 | + // 1. 'hi #' and append 'f' |
| 199 | + // desired rendering: 'hi [#f]' |
| 200 | + // native rendering would be: 'hi #f' (incorrect) |
| 201 | + // |
| 202 | + // 2. 'x [#foo]' and insert '#' before 'f' |
| 203 | + // desired rendering: 'x #[#foo]' |
| 204 | + // native rendering would be: 'x [##foo]' (incorrect) |
| 205 | + // |
| 206 | + // 3. '[#foobar]' and insert ' ' between 'foo' and 'bar' |
| 207 | + // desired rendering: '[#foo] bar' |
| 208 | + // native rendering would be: '[#foo bar]' (incorrect) |
| 209 | + // |
| 210 | + // 4. '[#foo]' and delete '#' [won't use this beforeinput codepath though] |
| 211 | + // desired rendering: 'foo' |
| 212 | + // native rendering would be: '[foo]' (incorrect) |
| 213 | + // |
| 214 | + // 5. '[#foo]' and append 'b' |
| 215 | + // desired rendering: '[#foob]' |
| 216 | + // native rendering would be: '[#foob]' (native insertion is OK here) |
| 217 | + // |
| 218 | + // It is safe to allow native insertion if and only if the full list of |
| 219 | + // decorator ranges matches what we expect native insertion to give. We |
| 220 | + // don't need to compare the content because the only possible mutation |
| 221 | + // to consider here is inserting plain text and decorators can't affect |
| 222 | + // text content. |
| 223 | + const oldBlockTree = editorState.getBlockTree(anchorKey); |
| 224 | + const newBlockTree = newEditorState.getBlockTree(anchorKey); |
| 225 | + mustPreventNative = |
| 226 | + oldBlockTree.size !== newBlockTree.size || |
| 227 | + oldBlockTree.zip(newBlockTree).some(([oldLeafSet, newLeafSet]) => { |
| 228 | + // selectionStart is guaranteed to be selectionEnd here |
| 229 | + const oldStart = oldLeafSet.get('start'); |
| 230 | + const adjustedStart = |
| 231 | + oldStart + (oldStart >= selectionStart ? chars.length : 0); |
| 232 | + const oldEnd = oldLeafSet.get('end'); |
| 233 | + const adjustedEnd = |
| 234 | + oldEnd + (oldEnd >= selectionStart ? chars.length : 0); |
| 235 | + return ( |
| 236 | + // Different decorators |
| 237 | + oldLeafSet.get('decoratorKey') !== newLeafSet.get('decoratorKey') || |
| 238 | + // Different number of inline styles |
| 239 | + oldLeafSet.get('leaves').size !== newLeafSet.get('leaves').size || |
| 240 | + // Different effective decorator position |
| 241 | + adjustedStart !== newLeafSet.get('start') || |
| 242 | + adjustedEnd !== newLeafSet.get('end') |
| 243 | + ); |
| 244 | + }); |
260 | 245 | } |
261 | 246 | if (!mustPreventNative) { |
262 | 247 | mustPreventNative = mustPreventDefaultForCharacter(chars); |
|
0 commit comments