Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Flowify editOnInput.js
Browse files Browse the repository at this point in the history
Summary:
Involved fixing two sketchy null checks. Nothing huge.

NOTE: There's lots of non flow-strict code that makes checks on keys. Empty string is an invalid key. This is the behavior right now, so I left it unchanged.

I extracted it to another module because I'll be using it to check the existence of keys in other parts of the code too.

Reviewed By: kedromelon

Differential Revision: D16595680

fbshipit-source-id: 13a08e3b2477ba46ba2a2c66089afe47edccb20a
  • Loading branch information
mrkev authored and facebook-github-bot committed Aug 23, 2019
1 parent 6972278 commit 594a14f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/component/handlers/edit/editOnInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @flow strict-local
* @emails oncall+draft_js
*/

Expand All @@ -18,6 +18,7 @@ const DraftOffsetKey = require('DraftOffsetKey');
const EditorState = require('EditorState');
const UserAgent = require('UserAgent');

const {notEmptyKey} = require('draftKeyUtils');
const findAncestorOffsetKey = require('findAncestorOffsetKey');
const gkx = require('gkx');
const keyCommandPlainBackspace = require('keyCommandPlainBackspace');
Expand Down Expand Up @@ -158,8 +159,8 @@ function editOnInput(editor: DraftEditor, e: SyntheticInputEvent<>): void {
});

const entityKey = block.getEntityAt(start);
const entity = entityKey && content.getEntity(entityKey);
const entityType = entity && entity.getMutability();
const entity = notEmptyKey(entityKey) ? content.getEntity(entityKey) : null;
const entityType = entity != null ? entity.getMutability() : null;
const preserveEntity = entityType === 'MUTABLE';

// Immutable or segmented entities cannot properly be handled by the
Expand Down
22 changes: 22 additions & 0 deletions src/component/utils/draftKeyUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* Provides utilities for handling draftjs keys.
*
* @emails oncall+draft_js
* @flow strict-local
* @format
*/

'use strict';

function notEmptyKey(key: ?string): boolean %checks {
return key != null && key != '';
}

module.exports = {
notEmptyKey,
};

0 comments on commit 594a14f

Please sign in to comment.