From d16833b3dae77ccf13e3af7f5e42c8131b0d1d2c Mon Sep 17 00:00:00 2001 From: Ian Jones Date: Mon, 20 Nov 2017 00:13:02 -0800 Subject: [PATCH] Draft.js - Stop links after pasting at end of block Summary: Currently in Draft when you paste a linkt the link continues and you have to manually unset it. Reviewed By: sophiebits Differential Revision: D5273910 fbshipit-source-id: 7fcda63f542a538692d016eb49395bb27fad73c9 --- .../getEntityKeyForSelection-test.js.snap | 2 ++ .../__tests__/getEntityKeyForSelection-test.js | 14 ++++++++++++++ src/model/entity/getEntityKeyForSelection.js | 3 +++ 3 files changed, 19 insertions(+) diff --git a/src/model/entity/__tests__/__snapshots__/getEntityKeyForSelection-test.js.snap b/src/model/entity/__tests__/__snapshots__/getEntityKeyForSelection-test.js.snap index 838288aa5e..067619bd96 100644 --- a/src/model/entity/__tests__/__snapshots__/getEntityKeyForSelection-test.js.snap +++ b/src/model/entity/__tests__/__snapshots__/getEntityKeyForSelection-test.js.snap @@ -4,6 +4,8 @@ exports[`must not return key if immutable 1`] = `null`; exports[`must not return key if immutable with collapsed selection 1`] = `null`; +exports[`must not return key if mutable with collapsed selection at end of an entity 1`] = `null`; + exports[`must not return key if segmented 1`] = `null`; exports[`must not return key if segmented with collapsed selection 1`] = `null`; diff --git a/src/model/entity/__tests__/getEntityKeyForSelection-test.js b/src/model/entity/__tests__/getEntityKeyForSelection-test.js index 4410fccbd5..fbf3c9edf6 100644 --- a/src/model/entity/__tests__/getEntityKeyForSelection-test.js +++ b/src/model/entity/__tests__/getEntityKeyForSelection-test.js @@ -27,6 +27,11 @@ const COLLAPSED_SELECTION = initialSelectionState.merge({ focusOffset: 2, }); +const COLLAPSED_SELECTION_ENTITY_END = initialSelectionState.merge({ + anchorOffset: 5, + focusOffset: 5, +}); + const NON_COLLAPSED_SELECTION = initialSelectionState.merge({ anchorOffset: 2, focusKey: 'c', @@ -50,6 +55,15 @@ test('must return key if mutable with collapsed selection', () => { expect(key).toMatchSnapshot(); }); +test('must not return key if mutable with collapsed selection at end of an entity', () => { + setEntityMutability('MUTABLE'); + const key = getEntityKeyForSelection( + contentState, + COLLAPSED_SELECTION_ENTITY_END, + ); + expect(key).toMatchSnapshot(); +}); + test('must not return key if immutable with collapsed selection', () => { setEntityMutability('IMMUTABLE'); const key = getEntityKeyForSelection(contentState, COLLAPSED_SELECTION); diff --git a/src/model/entity/getEntityKeyForSelection.js b/src/model/entity/getEntityKeyForSelection.js index 4cca5c2857..72b937f933 100644 --- a/src/model/entity/getEntityKeyForSelection.js +++ b/src/model/entity/getEntityKeyForSelection.js @@ -33,6 +33,9 @@ function getEntityKeyForSelection( var offset = targetSelection.getAnchorOffset(); if (offset > 0) { entityKey = contentState.getBlockForKey(key).getEntityAt(offset - 1); + if (entityKey !== contentState.getBlockForKey(key).getEntityAt(offset)) { + return null; + } return filterKey(contentState.getEntityMap(), entityKey); } return null;