Skip to content

Commit 2fea27e

Browse files
committed
Support legacy textSize in counter elements
Existing counter elements use the textSize property with values like 'large', 'medium', 'small', 'verySmall'. Map these to the new numberSize values based on equivalent font sizes to ensure backwards compatibility in both frontend rendering and editor UI. REDMINE-21218
1 parent 550c55a commit 2fea27e

5 files changed

Lines changed: 144 additions & 7 deletions

File tree

entry_types/scrolled/package/spec/contentElements/counter/Counter-spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,50 @@ describe('Counter', () => {
6161
expect(getByText('10')).toHaveScaleCategory('counterNumber', 'lg');
6262
});
6363

64+
it('defaults to md size for number', () => {
65+
const {getByText} = renderCounter();
66+
67+
expect(getByText('10')).toHaveScaleCategory('counterNumber', 'md');
68+
});
69+
70+
it('defaults to md size for unit', () => {
71+
const {getByText} = renderCounter({unit: 'kg'});
72+
73+
expect(getByText('kg')).toHaveScaleCategory('counterUnit', 'md');
74+
});
75+
76+
it('defaults to md size for description', () => {
77+
const {getByText} = renderCounter({
78+
description: [{type: 'paragraph', children: [{text: 'Some text'}]}]
79+
});
80+
81+
expect(getByText('Some text')).toHaveScaleCategory('counterDescription', 'md');
82+
});
83+
84+
it('maps legacy textSize large to counterNumber-xxxl', () => {
85+
const {getByText} = renderCounter({textSize: 'large'});
86+
87+
expect(getByText('10')).toHaveScaleCategory('counterNumber', 'xxxl');
88+
});
89+
90+
it('maps legacy textSize medium to counterNumber-xl', () => {
91+
const {getByText} = renderCounter({textSize: 'medium'});
92+
93+
expect(getByText('10')).toHaveScaleCategory('counterNumber', 'xl');
94+
});
95+
96+
it('maps legacy textSize small to counterNumber-md', () => {
97+
const {getByText} = renderCounter({textSize: 'small'});
98+
99+
expect(getByText('10')).toHaveScaleCategory('counterNumber', 'md');
100+
});
101+
102+
it('maps legacy textSize verySmall to counterNumber-xs', () => {
103+
const {getByText} = renderCounter({textSize: 'verySmall'});
104+
105+
expect(getByText('10')).toHaveScaleCategory('counterNumber', 'xs');
106+
});
107+
64108
it('renders description with counterDescription scale category', () => {
65109
const {getByText} = renderCounter({
66110
description: [{type: 'paragraph', children: [{text: 'Some text'}]}]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {createLegacyTextSizeDelegator} from 'contentElements/counter/createLegacyTextSizeDelegator';
2+
3+
describe('createLegacyTextSizeDelegator', () => {
4+
it('returns numberSize when set', () => {
5+
const model = fakeModel({numberSize: 'lg'});
6+
const delegator = createLegacyTextSizeDelegator(model);
7+
8+
expect(delegator.get('numberSize')).toBe('lg');
9+
});
10+
11+
it('maps legacy textSize large to xxxl', () => {
12+
const model = fakeModel({textSize: 'large'});
13+
const delegator = createLegacyTextSizeDelegator(model);
14+
15+
expect(delegator.get('numberSize')).toBe('xxxl');
16+
});
17+
18+
it('maps legacy textSize medium to xl', () => {
19+
const model = fakeModel({textSize: 'medium'});
20+
const delegator = createLegacyTextSizeDelegator(model);
21+
22+
expect(delegator.get('numberSize')).toBe('xl');
23+
});
24+
25+
it('maps legacy textSize small to md', () => {
26+
const model = fakeModel({textSize: 'small'});
27+
const delegator = createLegacyTextSizeDelegator(model);
28+
29+
expect(delegator.get('numberSize')).toBe('md');
30+
});
31+
32+
it('maps legacy textSize verySmall to xs', () => {
33+
const model = fakeModel({textSize: 'verySmall'});
34+
const delegator = createLegacyTextSizeDelegator(model);
35+
36+
expect(delegator.get('numberSize')).toBe('xs');
37+
});
38+
39+
it('prefers numberSize over textSize', () => {
40+
const model = fakeModel({numberSize: 'sm', textSize: 'large'});
41+
const delegator = createLegacyTextSizeDelegator(model);
42+
43+
expect(delegator.get('numberSize')).toBe('sm');
44+
});
45+
46+
it('passes through other properties', () => {
47+
const model = fakeModel({unit: 'kg'});
48+
const delegator = createLegacyTextSizeDelegator(model);
49+
50+
expect(delegator.get('unit')).toBe('kg');
51+
});
52+
53+
function fakeModel(attributes) {
54+
return {
55+
get(name) {
56+
return attributes[name];
57+
},
58+
has(name) {
59+
return name in attributes;
60+
}
61+
};
62+
}
63+
});

entry_types/scrolled/package/src/contentElements/counter/Counter.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function Counter({configuration, contentElementId, contentElementWidth, s
106106

107107
return (
108108
<Text scaleCategory="counterUnit"
109-
typographySize={configuration.unitSize}
109+
typographySize={configuration.unitSize || 'md'}
110110
inline>
111111
<span style={configuration.unitColor ? {color: paletteColor(configuration.unitColor)} : undefined}>
112112
{configuration.unit}
@@ -133,7 +133,7 @@ export function Counter({configuration, contentElementId, contentElementWidth, s
133133
style={{'--counting-duration': `${countingDuration || 1000}ms`}}
134134
>
135135
<Text scaleCategory="counterNumber"
136-
typographySize={configuration.numberSize}
136+
typographySize={configuration.numberSize || legacyTextSizes[configuration.textSize] || 'md'}
137137
inline>
138138
<span style={{color: paletteColor(configuration.numberColor)}}>
139139
{configuration.unitPlacement === 'leading' && renderUnit()}
@@ -149,7 +149,7 @@ export function Counter({configuration, contentElementId, contentElementWidth, s
149149
onChange={description => updateConfiguration({description})}
150150
onlyParagraphs={true}
151151
scaleCategory="counterDescription"
152-
typographySize={configuration.descriptionSize}
152+
typographySize={configuration.descriptionSize || 'md'}
153153
placeholder={t('pageflow_scrolled.inline_editing.type_description')} />
154154
</div>
155155
</div>
@@ -164,6 +164,13 @@ const countingDurations = {
164164
slow: 5000
165165
}
166166

167+
const legacyTextSizes = {
168+
verySmall: 'xs',
169+
small: 'md',
170+
medium: 'xl',
171+
large: 'xxxl'
172+
};
173+
167174
function easeInOut(t) {
168175
t = t * 2;
169176
if (t < 1) return (t**2)/2;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const legacyTextSizes = {
2+
verySmall: 'xs',
3+
small: 'md',
4+
medium: 'xl',
5+
large: 'xxxl'
6+
};
7+
8+
export function createLegacyTextSizeDelegator(model) {
9+
const delegator = Object.create(model);
10+
11+
delegator.get = function(name) {
12+
const result = model.get(name);
13+
14+
if (name === 'numberSize') {
15+
return result || legacyTextSizes[model.get('textSize')];
16+
}
17+
18+
return result;
19+
};
20+
21+
return delegator;
22+
}

entry_types/scrolled/package/src/contentElements/counter/editor.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CheckBoxInputView, SelectInputView, SliderInputView, TextInputView, NumberInputView, SeparatorView
44
} from 'pageflow/ui';
55

6+
import {createLegacyTextSizeDelegator} from './createLegacyTextSizeDelegator';
67
import pictogram from './pictogram.svg';
78

89
editor.contentElementTypes.register('counter', {
@@ -13,15 +14,14 @@ editor.contentElementTypes.register('counter', {
1314

1415
defaultConfig: {
1516
targetValue: 100,
16-
countingSpeed: 'medium',
17-
numberSize: 'md',
18-
unitSize: 'md',
19-
descriptionSize: 'md'
17+
countingSpeed: 'medium'
2018
},
2119

2220
configurationEditor({entry}) {
2321
const locale = entry.metadata.get('locale');
2422
this.tab('general', function() {
23+
const modelDelegator = createLegacyTextSizeDelegator(this.model);
24+
2525
this.input('targetValue', NumberInputView, {locale});
2626
this.input('decimalPlaces', SelectInputView, {
2727
values: [0, 1, 2, 3],
@@ -43,6 +43,7 @@ editor.contentElementTypes.register('counter', {
4343
order: 'asc'
4444
});
4545
this.input('numberSize', SliderInputView, {
46+
model: modelDelegator,
4647
values: numberSizes,
4748
texts: numberTexts,
4849
defaultValue: 'md',

0 commit comments

Comments
 (0)