Skip to content

Commit

Permalink
chore: fix money migration, add avatar size migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Jun 27, 2024
1 parent 963ba47 commit 7b4f3a5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import type {Attribute} from 'parse5/dist/common/token';

import {addImportToClosestModule} from '../../../../utils/add-import-to-closest-module';
import {findElementsByTagName} from '../../../../utils/templates/elements';
import {findAttr} from '../../../../utils/templates/inputs';
import {
getTemplateFromTemplateResource,
getTemplateOffset,
} from '../../../../utils/templates/template-resource';
import type {TemplateResource} from '../../../interfaces';
import {removeAttrs} from '../utils/remove-attrs';
import {replaceSizeAttr} from './toggles/common';

const sizeMap: Record<string, string> = {
xxs: 'xs',
xs: 's',
s: 'm',
m: 'l',
l: 'xl',
xl: 'xxl',
xxl: 'xxl',
};

function addModules(
componentPath: string,
Expand Down Expand Up @@ -47,21 +59,22 @@ export function migrateAvatar({
const template = getTemplateFromTemplateResource(resource, fileSystem);
const templateOffset = getTemplateOffset(resource);

const elements = findElementsByTagName(template, 'tui-avatar');
const avatarElements = findElementsByTagName(template, 'tui-avatar');
const markerIconElements = findElementsByTagName(template, 'tui-marker-icon');

elements.forEach(({attrs, sourceCodeLocation}) => {
const avatarUrlAttr = attrs.find(
attr => attr.name === '[avatarurl]' || attr.name === 'avatarurl',
);
const fallbackAttr = attrs.find(
attr => attr.name === '[fallback]' || attr.name === 'fallback',
);
const textAttr = attrs.find(
attr => attr.name === '[text]' || attr.name === 'text',
);
const roundedAttr = attrs.find(
attr => attr.name === '[rounded]' || attr.name === 'rounded',
);
[...avatarElements, ...markerIconElements].forEach(({attrs, sourceCodeLocation}) => {
if (!sourceCodeLocation) {
return;
}

replaceSizeAttr(attrs, sourceCodeLocation, recorder, templateOffset, sizeMap);
});

avatarElements.forEach(({attrs, sourceCodeLocation}) => {
const avatarUrlAttr = findAttr(attrs, 'avatarurl');
const fallbackAttr = findAttr(attrs, 'fallback');
const textAttr = findAttr(attrs, 'text');
const roundedAttr = findAttr(attrs, 'rounded');

if ((!avatarUrlAttr && !textAttr) || !sourceCodeLocation) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function migrateMoney({

const format = JSON.stringify(
cleanObject({
decimal: decimalAttr?.value,
decimalMode: decimalAttr?.value,
precision: precisionAttr?.value,
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function replaceSizeAttr(
sourceCodeLocation: ElementLocation,
recorder: UpdateRecorder,
templateOffset: number,
map = sizeMap,
): void {
const sizeAttr = attrs.find(attr => attr.name === 'size');

Expand All @@ -43,7 +44,7 @@ export function replaceSizeAttr(
recorder.remove(templateOffset + startOffset, endOffset - startOffset);
recorder.insertRight(
templateOffset + startOffset,
`size="${sizeMap[sizeAttr.value] || sizeAttr.value}"`,
`size="${map[sizeAttr.value] || sizeAttr.value}"`,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const TEMPLATE_AFTER = `
<p class="description tui-text_body-xs">Description</p>
</section>
<tui-avatar${' '}
size="xs"
size="s"
src="tuiIconStarLarge"
class="tui-island__marker icon"
></tui-avatar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ const TEMPLATE_BEFORE = `
avatarUrl="tuiIconUser"
text="alex inkin"
[rounded]="true"
size="xxs"
></tui-avatar>
<tui-avatar
avatarUrl="tuiIconUser"
[size]="size"
></tui-avatar>
<tui-avatar
class="tui-avatar"
Expand All @@ -66,9 +68,11 @@ const TEMPLATE_AFTER = `
${''}
${''}
[round]="true"
size="xs"
></tui-avatar>
<tui-avatar [src]="'tuiIconUser'" [round]="false"
${''}
[size]="size"
></tui-avatar>
<tui-avatar [src]="avatarUrl | tuiFallbackSrc : (text | tuiInitials) | async" [round]="false"
class="tui-avatar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const TEMPLATE_AFTER = `
[style.color]="'var(--tui-error-fill)'"
></tui-badge-notification>
<tui-avatar [src]="'a b' | tuiInitials"
size="l"
size="xl"
${''}
[round]="true"
></tui-avatar>
Expand All @@ -111,7 +111,7 @@ const TEMPLATE_AFTER = `
></tui-icon>
<tui-avatar [src]="'e' | tuiInitials" [round]="false"
size="l"
size="xl"
${''}
></tui-avatar>
</tui-badged-content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const TEMPLATE_AFTER = `
<span class="money">{{ 123 | tuiAmount : "RUB" | async }}</span>
<span [tuiNumberFormat]='{"decimal":"always"}' customDirective>{{ value | tuiAmount : "RUB" | async }}</span>
<span [tuiNumberFormat]='{"decimalMode":"always"}' customDirective>{{ value | tuiAmount : "RUB" | async }}</span>
`;

Expand Down

0 comments on commit 7b4f3a5

Please sign in to comment.