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

Rich Text: Set applied format as active in applyFormats #15573

Merged
merged 7 commits into from
May 13, 2019
23 changes: 13 additions & 10 deletions packages/rich-text/src/apply-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/

import { find } from 'lodash';
import { find, reject } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,7 +34,7 @@ export function applyFormat(
startIndex = value.start,
endIndex = value.end
) {
const { formats, activeFormats = [] } = value;
const { formats, activeFormats } = value;
const newFormats = formats.slice();

// The selection is collapsed.
Expand All @@ -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,15 @@ 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: [
...reject( activeFormats, { type: format.type } ),
format,
],
} );
}
11 changes: 5 additions & 6 deletions packages/rich-text/src/remove-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ export function removeFormat(
filterFormats( newFormats, endIndex, formatType );
endIndex++;
}
} else {
return {
...value,
activeFormats: reject( activeFormats, { type: formatType } ),
};
}
} else {
for ( let i = startIndex; i < endIndex; i++ ) {
Expand All @@ -62,7 +57,11 @@ export function removeFormat(
}
}

return normaliseFormats( { ...value, formats: newFormats } );
return normaliseFormats( {
...value,
formats: newFormats,
activeFormats: reject( activeFormats, { type: formatType } ),
} );
}

function filterFormats( formats, index, formatType ) {
Expand Down
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
2 changes: 2 additions & 0 deletions packages/rich-text/src/test/remove-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe( 'removeFormat', () => {
};
const expected = {
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ],
activeFormats: [],
text: 'one two three',
};
const result = removeFormat( deepFreeze( record ), 'strong', 3, 6 );
Expand All @@ -37,6 +38,7 @@ describe( 'removeFormat', () => {
};
const expected = {
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ],
activeFormats: [],
text: 'one two three',
};
const result = removeFormat( deepFreeze( record ), 'strong', 4, 4 );
Expand Down
2 changes: 2 additions & 0 deletions packages/rich-text/src/test/toggle-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe( 'toggleFormat', () => {
};
const expected = {
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ],
activeFormats: [],
text: 'one two three',
start: 3,
end: 6,
Expand All @@ -43,6 +44,7 @@ describe( 'toggleFormat', () => {
};
const expected = {
formats: [ , , , [ strong ], [ strong, em ], [ strong, em ], [ em ], , , , , , , ],
activeFormats: [ strong ],
text: 'one two three',
start: 3,
end: 6,
Expand Down