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

fix: the problem 're-render all content when japanese / chinese input' #3032

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/module-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module.exports = Object.assign(
{
immutable: 'immutable',
React: 'react',
react: 'react',
ReactDOM: 'react-dom',
ReactDOMComet: 'react-dom',
'object-assign': 'object-assign',
Expand All @@ -17,5 +17,5 @@ module.exports = Object.assign(
reactComponentExpect: 'react-dom/lib/reactComponentExpect',
},
require('fbjs/module-map'),
require('fbjs-scripts/third-party-module-map')
require('fbjs-scripts/third-party-module-map'),
);
33 changes: 30 additions & 3 deletions src/component/base/DraftEditor.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {BlockMap} from 'BlockMap';
import type {DraftEditorModes} from 'DraftEditorModes';
import type {DraftEditorDefaultProps, DraftEditorProps} from 'DraftEditorProps';
import type {DraftScrollPosition} from 'DraftScrollPosition';
import type {BlockKeyMap} from 'BlockKeyMap';

const DefaultDraftBlockRenderMap = require('DefaultDraftBlockRenderMap');
const DefaultDraftInlineStyle = require('DefaultDraftInlineStyle');
Expand All @@ -40,6 +41,7 @@ const invariant = require('invariant');
const isHTMLElement = require('isHTMLElement');
const nullthrows = require('nullthrows');
const React = require('react');
const {Map} = require('immutable');

const isIE = UserAgent.isBrowser('IE');

Expand All @@ -57,7 +59,7 @@ const handlerMap = {
render: null,
};

type State = {contentsKey: number};
type State = {contentsKey: number, blockKeyMap: BlockKeyMap};

let didInitODS = false;

Expand Down Expand Up @@ -191,6 +193,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
setMode: (mode: DraftEditorModes) => void;
exitCurrentMode: () => void;
restoreEditorDOM: (scrollPosition?: DraftScrollPosition) => void;
restoreBlockDOM: (key: string, scrollPosition?: DraftScrollPosition) => void;
setClipboard: (clipboard: ?BlockMap) => void;
getClipboard: () => ?BlockMap;
getEditorKey: () => string;
Expand All @@ -199,7 +202,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
onDragLeave: () => void;

// See `restoreEditorDOM()`.
state: State = {contentsKey: 0};
state: State = {contentsKey: 0, blockKeyMap: new Map({})};

constructor(props: DraftEditorProps) {
super(props);
Expand Down Expand Up @@ -349,7 +352,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
textAlignment,
textDirectionality,
} = this.props;

const {contentsKey, blockKeyMap} = this.state;
const rootClass = cx({
'DraftEditor/root': true,
'DraftEditor/alignLeft': textAlignment === 'left',
Expand Down Expand Up @@ -388,6 +391,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
editorState,
preventScroll,
textDirectionality,
blockKeyMap,
};

const contentClassName =
Expand Down Expand Up @@ -598,6 +602,29 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
this._handler = handler[mode];
};

/**
* Used via `this.restoreBlockDOM()`.
* Force a complete re-render of the DraftEditorBlock in DraftEditorContents
* Search for a block with the specified block key and re-render it.
*/
restoreBlockDOM: (
key: string,
scrollPosition?: DraftScrollPosition,
) => void = (key: string, scrollPosition?: DraftScrollPosition): void => {
const {blockKeyMap} = this.state;
this.setState(
{
blockKeyMap: blockKeyMap.set(
key,
blockKeyMap.has(key) ? blockKeyMap.get(key) + 1 : 1,
),
},
() => {
this.focus(scrollPosition);
},
);
};

// eslint-disable-next-line fb-www/extra-arrow-initializer
exitCurrentMode: () => void = (): void => {
this.setMode('edit');
Expand Down
14 changes: 13 additions & 1 deletion src/component/contents/DraftEditorContents-core.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {DraftBlockRenderMap} from 'DraftBlockRenderMap';
import type {DraftInlineStyle} from 'DraftInlineStyle';
import type EditorState from 'EditorState';
import type {BidiDirection} from 'UnicodeBidiDirection';
import type {BlockKeyMap} from 'BlockKeyMap';

const DraftEditorBlock = require('DraftEditorBlock.react');
const DraftOffsetKey = require('DraftOffsetKey');
Expand All @@ -38,6 +39,7 @@ type Props = {
editorState: EditorState,
preventScroll?: boolean,
textDirectionality?: BidiDirection,
blockKeyMap: BlockKeyMap,
...
};

Expand Down Expand Up @@ -85,11 +87,19 @@ class DraftEditorContents extends React.Component<Props> {
const prevDirectionMap = prevEditorState.getDirectionMap();
const nextDirectionMap = nextEditorState.getDirectionMap();

const prevBlockKeyMap = this.props.blockKeyMap;
const nextBlockKeyMap = nextProps.blockKeyMap;

// Text direction has changed for one or more blocks. We must re-render.
if (prevDirectionMap !== nextDirectionMap) {
return true;
}

// blockKeyMap has chaged. We must re-render on block level.
if (prevBlockKeyMap !== nextBlockKeyMap) {
return true;
}

const didHaveFocus = prevEditorState.getSelection().getHasFocus();
const nowHasFocus = nextEditorState.getSelection().getHasFocus();

Expand Down Expand Up @@ -136,6 +146,7 @@ class DraftEditorContents extends React.Component<Props> {
editorKey,
preventScroll,
textDirectionality,
blockKeyMap,
} = this.props;

const content = editorState.getCurrentContent();
Expand Down Expand Up @@ -181,6 +192,7 @@ class DraftEditorContents extends React.Component<Props> {
preventScroll,
selection,
tree: editorState.getBlockTree(key),
key: `${key}-${blockKeyMap.get(key) || '0'}`,
};

const configForType =
Expand Down Expand Up @@ -231,7 +243,7 @@ class DraftEditorContents extends React.Component<Props> {
const child = React.createElement(
Element,
childProps,
<Component {...componentProps} key={key} />,
<Component {...componentProps} />,
);

processedBlocks.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const DraftEditorCompositionHandler = {
);
const compositionEndSelectionState = documentSelection.selectionState;

editor.restoreEditorDOM();
// editor.restoreEditorDOM();

// See:
// - https://github.com/facebook/draft-js/issues/2093
Expand All @@ -250,6 +250,13 @@ const DraftEditorCompositionHandler = {
? EditorState.forceSelection(editorState, compositionEndSelectionState)
: EditorState.acceptSelection(editorState, compositionEndSelectionState);

const anchorKey = compositionEndSelectionState.getAnchorKey();
const focusKey = compositionEndSelectionState.getFocusKey();

anchorKey === focusKey
? editor.restoreBlockDOM(anchorKey)
: editor.restoreEditorDOM();

editor.update(
EditorState.push(
editorStateWithUpdatedSelection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ beforeEach(() => {
setMode: jest.fn(),
restoreEditorDOM: jest.fn(),
exitCurrentMode: jest.fn(),
restoreBlockDOM: jest.fn(),
update: jest.fn(state => (editor._latestEditorState = state)),
};
});
Expand Down
14 changes: 14 additions & 0 deletions src/model/immutable/BlockKeyMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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.
*
* @format
* @flow strict-local
* @emails oncall+draft_js
*/

'use strict';

export type BlockKeyMap = Map<string, number>;