@amaui/style
Version:
CSS in JS styling solution
62 lines (50 loc) • 1.86 kB
JavaScript
import AmauiStyleRuleProperty from './AmauiStyleRuleProperty';
import { is } from './utils';
const optionsDefault = {
priority: 'individual'
};
function sort(amauiStyle) {
let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const options = { ...optionsDefault,
...options_
};
const method = values => {
const value = {
arguments: {
values
}
};
if (is('array', values)) {
const priority = options.priority; // Sort by grouping all rules
values.sort((a, b) => {
if (a.value instanceof AmauiStyleRuleProperty && !(b.value instanceof AmauiStyleRuleProperty)) return -1;
if (a.constructor === b.constructor || is('simple', a.value) && is('simple', b.value)) return 0;
return 1;
}); // Order by priority
if (priority !== 'original') values.sort((a, b) => {
if (!(a.value instanceof AmauiStyleRuleProperty && b.value instanceof AmauiStyleRuleProperty || is('simple', a.value) && is('simple', b.value))) return 0;
if ((a === null || a === void 0 ? void 0 : a.property) < (b === null || b === void 0 ? void 0 : b.property)) return priority === 'individual' ? -1 : 1;
if ((a === null || a === void 0 ? void 0 : a.property) > (b === null || b === void 0 ? void 0 : b.property)) return priority === 'individual' ? 1 : -1;
}); // Add sorted array to value
value.value = values;
}
return value;
}; // Add method to subscriptions
if (amauiStyle) {
amauiStyle.subscriptions.rules.sort.subscribe(method);
}
const remove = () => {
// Remove method from subscriptions
if (amauiStyle) {
amauiStyle.subscriptions.rules.sort.unsubscribe(method);
}
};
const response = {
methods: {
method
},
remove
};
return response;
}
export default sort;