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

Fixed color example to get along with other inline styles #3108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 26 additions & 23 deletions examples/draft-0-10-0/color/color.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,39 @@
const {editorState} = this.state;
const selection = editorState.getSelection();

// Let's just allow one color at a time. Turn off all active colors.
const nextContentState = Object.keys(colorStyleMap)
.reduce((contentState, color) => {
return Modifier.removeInlineStyle(contentState, selection, color)
}, editorState.getCurrentContent());

let nextEditorState = EditorState.push(
editorState,
nextContentState,
'change-inline-style'
const otherColors = Object.keys(colorStyleMap).filter(color =>
color !== toggledColor
);

const currentStyle = editorState.getCurrentInlineStyle();
const nextEditorState = RichUtils
.toggleInlineStyle(editorState, toggledColor);

// Unset style override for current color.
if (selection.isCollapsed()) {
nextEditorState = currentStyle.reduce((state, color) => {
return RichUtils.toggleInlineStyle(state, color);
}, nextEditorState);
}
// remove other colors style
const nextStyle = otherColors.reduce((colors, color) =>
colors.has(color) ? colors.remove(color) : colors,
nextEditorState.getCurrentInlineStyle());

this.onChange(EditorState.setInlineStyleOverride(
nextEditorState, nextStyle
));

// If the color is being toggled on, apply it.
if (!currentStyle.has(toggledColor)) {
nextEditorState = RichUtils.toggleInlineStyle(
nextEditorState,
toggledColor
);
return;
}

this.onChange(nextEditorState);
// has selection

// Turn off other active colors.
const nextContentState = otherColors
.reduce((contentState, color) =>
Modifier.removeInlineStyle(contentState, selection, color),
nextEditorState.getCurrentContent());

this.onChange(EditorState.push(
nextEditorState,
nextContentState,
"change-inline-style"
));
}

render() {
Expand Down