Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Model: Support "no color" when set color #1514

Merged
merged 3 commits into from
Jan 19, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import { IExperimentalContentModelEditor } from '../../publicTypes/IExperimental
/**
* Set background color
* @param editor The editor to operate on
* @param backgroundColor The color to set
* @param backgroundColor The color to set. Pass null to remove existing color.
*/
export default function setBackgroundColor(
editor: IExperimentalContentModelEditor,
backgroundColor: string
backgroundColor: string | null
) {
formatSegmentWithContentModel(editor, 'setBackgroundColor', format => {
format.backgroundColor = backgroundColor;
});
formatSegmentWithContentModel(
editor,
'setBackgroundColor',
backgroundColor === null
? format => {
delete format.backgroundColor;
}
: format => {
format.backgroundColor = backgroundColor;
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import { IExperimentalContentModelEditor } from '../../publicTypes/IExperimental
/**
* Set text color
* @param editor The editor to operate on
* @param textColor The text color to set
* @param textColor The text color to set. Pass null to remove existing color.
*/
export default function setTextColor(editor: IExperimentalContentModelEditor, textColor: string) {
export default function setTextColor(
editor: IExperimentalContentModelEditor,
textColor: string | null
) {
formatSegmentWithContentModel(
editor,
'setTextColor',
format => {
format.textColor = textColor;
},
textColor === null
? format => {
delete format.textColor;
}
: format => {
format.textColor = textColor;
},
undefined /* segmentHasStyleCallback*/,
true /*includingFormatHandler*/
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ describe('setBackgroundColor', () => {
function runTest(
model: ContentModelDocument,
result: ContentModelDocument,
calledTimes: number
calledTimes: number,
newColor: string | null = 'red'
) {
segmentTestCommon(
'setBackgroundColor',
editor => setBackgroundColor(editor, 'red'),
editor => setBackgroundColor(editor, newColor),
model,
result,
calledTimes
Expand Down Expand Up @@ -318,4 +319,45 @@ describe('setBackgroundColor', () => {
1
);
});

it('Remove existing color', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segments: [
{
segmentType: 'Text',
text: 'test',
format: { backgroundColor: 'green' },
isSelected: true,
},
],
},
],
},
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
isSelected: true,
},
],
},
],
},
1,
null
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ describe('setTextColor', () => {
function runTest(
model: ContentModelDocument,
result: ContentModelDocument,
calledTimes: number
calledTimes: number,
newColor: string | null = 'red'
) {
segmentTestCommon(
'setTextColor',
editor => setTextColor(editor, 'red'),
editor => setTextColor(editor, newColor),
model,
result,
calledTimes
Expand Down Expand Up @@ -318,4 +319,45 @@ describe('setTextColor', () => {
1
);
});

it('Remove existing color', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segments: [
{
segmentType: 'Text',
text: 'test',
format: { textColor: 'green' },
isSelected: true,
},
],
},
],
},
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
isSelected: true,
},
],
},
],
},
1,
null
);
});
});