Skip to content

Commit

Permalink
Rich Text: Set applied format as active in applyFormats
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 10, 2019
1 parent 4108e37 commit bea3426
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/rich-text/src/apply-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ export function applyFormat(
replace( newFormats[ endIndex ], index, format );
endIndex++;
}
// Otherwise, insert a placeholder with the format so new input appears
// with the format applied.
} else {
return {
...value,
activeFormats: [ ...activeFormats, format ],
};
}
} else {
// Determine the highest position the new format can be inserted at.
Expand All @@ -92,5 +85,12 @@ export function applyFormat(
}
}

return normaliseFormats( { ...value, formats: newFormats } );
return normaliseFormats( {
...value,
formats: newFormats,
// Always revise active formats. This serves as a placeholder for new
// inputs with the format so new input appears with the format applied,
// and ensures a format of the same type uses the latest values.
activeFormats: [ format, ...activeFormats ],
} );
}
10 changes: 10 additions & 0 deletions packages/rich-text/src/test/apply-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ em ],
formats: [ [ em ], [ em ], [ em ], [ em ] ],
};
const result = applyFormat( deepFreeze( record ), em, 0, 4 );
Expand All @@ -39,6 +40,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ em ],
formats: [ [ strong, em ], [ strong, em ], [ strong, em ], [ strong, em ] ],
};
const result = applyFormat( deepFreeze( record ), em, 0, 4 );
Expand All @@ -55,6 +57,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ em ],
formats: [ [ strong, em ], [ strong, em ], [ strong, em ], [ strong, em ] ],
};
const result = applyFormat( deepFreeze( record ), em, 0, 4 );
Expand All @@ -71,6 +74,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ strong ],
formats: [ [ strong ], [ strong, em ], [ strong, em ], [ strong ] ],
};
const result = applyFormat( deepFreeze( record ), strong, 0, 4 );
Expand All @@ -87,6 +91,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ strong ],
formats: [ [ strong ], [ strong, em ], [ strong, em ], , ],
};
const result = applyFormat( deepFreeze( record ), strong, 0, 3 );
Expand All @@ -103,6 +108,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ strong ],
formats: [ , [ strong, em ], [ strong, em ], [ strong ] ],
};
const result = applyFormat( deepFreeze( record ), strong, 1, 4 );
Expand All @@ -119,6 +125,7 @@ describe( 'applyFormat', () => {
};
const expected = {
...record,
activeFormats: [ strong ],
formats: [ , [ strong, em ], [ strong ], [ strong, em ] ],
};
const result = applyFormat( deepFreeze( record ), strong, 1, 4 );
Expand All @@ -134,6 +141,7 @@ describe( 'applyFormat', () => {
text: 'one two three',
};
const expected = {
activeFormats: [ strong ],
formats: [ , , , [ strong ], [ strong, em ], [ strong, em ], [ em ], , , , , , , ],
text: 'one two three',
};
Expand All @@ -152,6 +160,7 @@ describe( 'applyFormat', () => {
end: 6,
};
const expected = {
activeFormats: [ strong ],
formats: [ , , , [ strong ], [ strong, em ], [ strong, em ], [ em ], , , , , , , ],
text: 'one two three',
start: 3,
Expand Down Expand Up @@ -190,6 +199,7 @@ describe( 'applyFormat', () => {
end: 4,
};
const expected = {
activeFormats: [ a2 ],
formats: [ , , , , [ a2 ], [ a2 ], [ a2 ], , , , , , , ],
text: 'one two three',
start: 4,
Expand Down

0 comments on commit bea3426

Please sign in to comment.