mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-03 04:30:06 +08:00
abf215a982
* init recommend block * init design pages * home frames * add background banner * hello, Hitu makes the animation * add media query * add images * update types * add declare * preload support * update ignore * additional ignore site Home lint * update ignore * add hover effect * adjust alt of bannber * adjust lang & card shadow * fix typo * adjust desc * hitu in english * article add link * Certainty is hitu * Meaning Hitu * Growth with Hitu * Natural Hitu * slow down of Natural * adjust speed of icons * New Meaning animation * adjust animation duration * update card link * update link * values doc * replace images * faster Growth * update values * fix lint * all slow down * adjust padding * update icons * adjust margin * update images * adjust montion * adjust by designer * update pages * update design prod * update articles * update site style * update doc * update images * update style * lint fix * adjust liner color * github text color update * adjust margin of title * mobile design * update design sub card * update style * less lint * update images * design card auto height * update img * update logo transition * adjust card style * adjust style to fix 184 * adjust nav style * category it * slow down of logo * update style * hide video link * hitu article * use img of hitu * update docs * style lint * update Hitu layout doc * update images * speed up logo * update link * clean up * clean up * update doc * adjust doc * update images
175 lines
4.2 KiB
JavaScript
175 lines
4.2 KiB
JavaScript
import flattenDeep from 'lodash/flattenDeep';
|
|
import flatten from 'lodash/flatten';
|
|
|
|
export function getMenuItems(moduleData, locale, categoryOrder, typeOrder) {
|
|
const menuMeta = moduleData.map(item => item.meta).filter(meta => !meta.skip);
|
|
|
|
const menuItems = [];
|
|
const sortFn = (a, b) => (a.order || 0) - (b.order || 0);
|
|
menuMeta.sort(sortFn).forEach(meta => {
|
|
// Format
|
|
if (meta.category) {
|
|
meta.category = meta.category[locale] || meta.category;
|
|
}
|
|
if (meta.type) {
|
|
meta.type = meta.type[locale] || meta.type;
|
|
}
|
|
if (meta.title) {
|
|
meta.title = meta.title[locale] || meta.title;
|
|
}
|
|
|
|
if (!meta.category) {
|
|
menuItems.push(meta);
|
|
return;
|
|
}
|
|
|
|
// Component
|
|
if (meta.category === 'Components' && meta.type) {
|
|
let type = menuItems.find(i => i.title === meta.type);
|
|
if (!type) {
|
|
type = {
|
|
type: 'type',
|
|
title: meta.type,
|
|
children: [],
|
|
order: typeOrder[meta.type],
|
|
};
|
|
menuItems.push(type);
|
|
}
|
|
type.children.push(meta);
|
|
return;
|
|
}
|
|
|
|
let group = menuItems.find(i => i.title === meta.category);
|
|
|
|
if (!group) {
|
|
group = {
|
|
type: 'category',
|
|
title: meta.category,
|
|
children: [],
|
|
order: categoryOrder[meta.category],
|
|
};
|
|
menuItems.push(group);
|
|
}
|
|
|
|
if (meta.type) {
|
|
let type = group.children.filter(i => i.title === meta.type)[0];
|
|
if (!type) {
|
|
type = {
|
|
type: 'type',
|
|
title: meta.type,
|
|
children: [],
|
|
order: typeOrder[meta.type],
|
|
};
|
|
group.children.push(type);
|
|
}
|
|
type.children.push(meta);
|
|
} else {
|
|
group.children.push(meta);
|
|
}
|
|
});
|
|
|
|
function nestSort(list) {
|
|
return list.sort(sortFn).map(item => {
|
|
if (item.children) {
|
|
return {
|
|
...item,
|
|
children: nestSort(item.children),
|
|
};
|
|
}
|
|
|
|
return item;
|
|
});
|
|
}
|
|
|
|
return nestSort(menuItems);
|
|
}
|
|
|
|
export function isZhCN(pathname) {
|
|
return /-cn\/?$/.test(pathname);
|
|
}
|
|
|
|
export function getLocalizedPathname(path, zhCN) {
|
|
const pathname = path.startsWith('/') ? path : `/${path}`;
|
|
if (!zhCN) {
|
|
// to enUS
|
|
return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
|
|
}
|
|
if (pathname === '/') {
|
|
return '/index-cn';
|
|
}
|
|
if (pathname.endsWith('/')) {
|
|
return pathname.replace(/\/$/, '-cn/');
|
|
}
|
|
return `${pathname}-cn`;
|
|
}
|
|
|
|
export function ping(callback) {
|
|
// eslint-disable-next-line
|
|
const url =
|
|
'https://private-a' +
|
|
'lipay' +
|
|
'objects.alip' +
|
|
'ay.com/alip' +
|
|
'ay-rmsdeploy-image/rmsportal/RKuAiriJqrUhyqW.png';
|
|
const img = new Image();
|
|
let done;
|
|
const finish = status => {
|
|
if (!done) {
|
|
done = true;
|
|
img.src = '';
|
|
callback(status);
|
|
}
|
|
};
|
|
img.onload = () => finish('responded');
|
|
img.onerror = () => finish('error');
|
|
img.src = url;
|
|
return setTimeout(() => finish('timeout'), 1500);
|
|
}
|
|
|
|
export function isLocalStorageNameSupported() {
|
|
const testKey = 'test';
|
|
const storage = window.localStorage;
|
|
try {
|
|
storage.setItem(testKey, '1');
|
|
storage.removeItem(testKey);
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function loadScript(src) {
|
|
return new Promise((resolve, reject) => {
|
|
const script = document.createElement('script');
|
|
script.type = 'text/javascript';
|
|
script.src = src;
|
|
script.onload = resolve;
|
|
script.onerror = reject;
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
export function getMetaDescription(jml) {
|
|
const COMMON_TAGS = ['h1', 'h2', 'h3', 'p', 'img', 'a', 'code', 'strong'];
|
|
if (!Array.isArray(jml)) return '';
|
|
const paragraph = flattenDeep(
|
|
jml
|
|
.filter(item => {
|
|
if (Array.isArray(item)) {
|
|
const [tag] = item;
|
|
return tag === 'p';
|
|
}
|
|
return false;
|
|
})
|
|
// ['p', ['code', 'aa'], 'bb'] => ['p', 'aabb']
|
|
.map(item => {
|
|
const [tag, ...others] = flatten(item);
|
|
const content = others
|
|
.filter(other => typeof other === 'string' && !COMMON_TAGS.includes(other))
|
|
.join('');
|
|
return [tag, content];
|
|
}),
|
|
).find(p => p && typeof p === 'string' && !COMMON_TAGS.includes(p));
|
|
return paragraph;
|
|
}
|