Skip to content

Commit

Permalink
Update migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
alizedebray committed Apr 5, 2024
1 parent 3012053 commit cfea152
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 98 deletions.
46 changes: 23 additions & 23 deletions packages/migrations/src/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,107 @@
"migration-general-classes-bg-opacity": {
"version": "5.0.0",
"description": "Migrates general bg-opacity classes.",
"factory": "./migrations/general/classes/bg-opacity"
"factory": "./migrations/v5/general/classes/bg-opacity"
},
"migration-general-classes-secondary": {
"version": "5.0.0",
"description": "Migrates general bg-secondary, border-secondary and text-secondary classes.",
"factory": "./migrations/general/classes/secondary"
"factory": "./migrations/v5/general/classes/secondary"
},
"migration-general-text-auto": {
"version": "5.0.0",
"description": "Migrates general text-auto classes.",
"factory": "./migrations/general/classes/text-auto"
"factory": "./migrations/v5/general/classes/text-auto"
},
"migration-general-rtl": {
"version": "5.0.0",
"description": "Migrates general rtl helper classes (https://getbootstrap.com/docs/5.0/migration/#rtl).",
"factory": "./migrations/general/classes/rtl"
"factory": "./migrations/v5/general/classes/rtl"
},
"migration-general-sr-only": {
"version": "5.0.0",
"description": "Migrates general sr-only helper classes (https://getbootstrap.com/docs/5.0/migration/#helpers).",
"factory": "./migrations/general/classes/sr-only"
"factory": "./migrations/v5/general/classes/sr-only"
},
"migration-bootstrap-badge": {
"version": "5.0.0",
"description": "Migrates bootstrap badge component.",
"factory": "./migrations/bootstrap/badge"
"factory": "./migrations/v5/bootstrap/badge"
},
"migration-bootstrap-blockquote": {
"version": "5.0.0",
"description": "Migrates bootstrap blockquote component.",
"factory": "./migrations/bootstrap/blockquote"
"factory": "./migrations/v5/bootstrap/blockquote"
},
"migration-bootstrap-button": {
"version": "5.0.0",
"description": "Migrates bootstrap button component.",
"factory": "./migrations/bootstrap/button"
"factory": "./migrations/v5/bootstrap/button"
},
"migration-bootstrap-button-close": {
"version": "5.0.0",
"description": "Migrates bootstrap close-button component.",
"factory": "./migrations/bootstrap/button-close"
"factory": "./migrations/v5/bootstrap/button-close"
},
"migration-bootstrap-form-control": {
"version": "5.0.0",
"description": "Migrates bootstrap form-control component.",
"factory": "./migrations/bootstrap/form-control"
"factory": "./migrations/v5/bootstrap/form-control"
},
"migration-bootstrap-form-select": {
"version": "5.0.0",
"description": "Migrates bootstrap form-select component.",
"factory": "./migrations/bootstrap/form-select"
"factory": "./migrations/v5/bootstrap/form-select"
},
"migration-bootstrap-textarea": {
"version": "5.0.0",
"description": "Migrates bootstrap textarea component.",
"factory": "./migrations/bootstrap/textarea"
"factory": "./migrations/v5/bootstrap/textarea"
},
"migration-bootstrap-form-checkbox": {
"version": "5.0.0",
"description": "Migrates bootstrap form-checkbox component.",
"factory": "./migrations/bootstrap/form-checkbox"
"factory": "./migrations/v5/bootstrap/form-checkbox"
},
"migration-bootstrap-form-radio": {
"version": "5.0.0",
"description": "Migrates bootstrap form-radio component.",
"factory": "./migrations/bootstrap/form-radio"
"factory": "./migrations/v5/bootstrap/form-radio"
},
"migration-bootstrap-form-switch": {
"version": "5.0.0",
"description": "Migrates bootstrap form-switch component.",
"factory": "./migrations/bootstrap/form-switch"
"factory": "./migrations/v5/bootstrap/form-switch"
},
"migration-ng-buttons": {
"version": "5.0.0",
"description": "Deprecates ng-bootstrap button component.",
"factory": "./migrations/ngbootstrap/buttons"
"factory": "./migrations/v5/ngbootstrap/buttons"
},
"migration-post-custom-select": {
"version": "5.0.0",
"description": "Migrates post custom-select component.",
"factory": "./migrations/post/custom-select"
"factory": "./migrations/v5/post/custom-select"
},
"migration-post-subnavigation": {
"version": "5.0.0",
"description": "Migrates post subnavigation component.",
"factory": "./migrations/post/subnavigation"
"factory": "./migrations/v5/post/subnavigation"
},
"migration-post-topic-teaser": {
"version": "5.0.0",
"description": "Migrates post topic-teaser component.",
"factory": "./migrations/post/topic-teaser"
"factory": "./migrations/v5/post/topic-teaser"
},
"migration-post-stepper": {
"version": "6.0.0",
"description": "Improves the post stepper accessibility.",
"factory": "./migrations/post/stepper"
"factory": "./migrations/v6/stepper"
},
"migration-bootstrap-chip": {
"migration-badge-to-chip-or-tag": {
"version": "7.0.0",
"description": "Renames badges to chips.",
"factory": "./migrations/bootstrap/chip"
"description": "Migrates badges to chip or tag depending on the content.",
"factory": "./migrations/v7/badge"
}
}
}
75 changes: 0 additions & 75 deletions packages/migrations/src/migrations/bootstrap/chip/index.ts

This file was deleted.

106 changes: 106 additions & 0 deletions packages/migrations/src/migrations/v7/badge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Rule } from '@angular-devkit/schematics';
import type { AnyNode, Cheerio, CheerioAPI } from 'cheerio';
import { DomUpdate, getDomMigrationRule } from '../../../utils/dom-migration';
import { themeColors } from '../../../utils/constants';

export default function (): Rule {
return getDomMigrationRule(
new BadgeCheckToChipFilterUpdate(),
new BadgeToChipDismissibleUpdate(),
new BadgeToTagUpdate(),
);
}

class BadgeCheckToChipFilterUpdate implements DomUpdate {
selector = '.badge-check';

update($elements: Cheerio<AnyNode>, $: CheerioAPI) {
$elements.each((_i, element) => {
const $element = $(element);

$element.removeClass('badge-check').addClass('chip-filter');

const $input = $element.children('.badge-check-input');
if ($input) $input.removeClass('badge-check-input').addClass('chip-filter-input');

const $label = $element.children('.badge-check-label');
if ($label) $label.removeClass('badge-check-label').addClass('chip-filter-label');

addChipTextClass($label);
});
}
}

class BadgeToChipDismissibleUpdate implements DomUpdate {
selector = '.badge';

update($elements: Cheerio<AnyNode>, $: CheerioAPI) {
$elements.each((_i, element) => {
const $element = $(element);

// only update badge with close button
const $closeBtn = $element.children('.btn-close');
if (!$closeBtn.length) {
return;
}

addChipTextClass($element);

$closeBtn.removeAttr('class').addClass('chip chip-dismissible');

const ariaLabel = $closeBtn.attr('aria-label');
if (ariaLabel) {
$closeBtn.removeAttr('aria-label');
$closeBtn.append(`<span class="visually-hidden">${ariaLabel}</span>`);
}

$closeBtn.append($element.children()).data($element.data());

$element.replaceWith($closeBtn);
});
}
}

class BadgeToTagUpdate implements DomUpdate {
selector = '.badge';

bgClassRegex: RegExp = new RegExp(`^bg-(${themeColors.join('|')})$`);

update($elements: Cheerio<AnyNode>, $: CheerioAPI) {
$elements.each((_i, element) => {
const $element = $(element);

// do not update nested badges
const $parent = $element.parent();
if ($parent.hasClass('tag')) {
return;
}

$element.removeClass('badge').addClass('tag');

if ($element.hasClass('badge-sm')) $element.removeClass('badge-sm').addClass('tag-sm');

$element
.attr('class')
?.split(' ')
.forEach(cssClass => {
const [_, bgColor] = cssClass.match(this.bgClassRegex) ?? [];

if (!bgColor) return;

if (bgColor === 'active' || bgColor === 'yellow') $element.addClass('tag-yellow');
if (bgColor === 'white') $element.addClass('tag-white');
if (bgColor === 'info') $element.addClass('tag-info');
if (bgColor === 'success') $element.addClass('tag-success');
if (bgColor === 'danger') $element.addClass('tag-danger');
if (bgColor === 'warning') $element.addClass('tag-warning');

$element.removeClass(cssClass);
});
});
}
}

function addChipTextClass($element: Cheerio<AnyNode>) {
$element.children('span:not(.badge)').addClass('chip-text');
}

0 comments on commit cfea152

Please sign in to comment.