Skip to content

Commit

Permalink
Merge pull request #1243 from XiaoMi/develop
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
solarjoker authored Oct 22, 2020
2 parents 5175d1c + 8f7606a commit d16fdd1
Show file tree
Hide file tree
Showing 718 changed files with 59,672 additions and 40,533 deletions.
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
es2021: true
},
extends: ['plugin:react/recommended', 'standard', 'prettier'],
parserOptions: {
ecmaFeatures: {
jsx: true
},
ecmaVersion: 12,
sourceType: 'module'
},
plugins: ['react', 'prettier'],
rules: {
'prettier/prettier': ['error', { singleQuote: true, semi: false, printWidth: 120, trailingComma: 'none' }],
'react/prop-types': 0,
'react/no-children-prop': 0,
'react/display-name': 0
}
}
701 changes: 88 additions & 613 deletions CHANGELOG.md

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions build/prerenderSite.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ const server = createSPAServer({
onCreated: renderUrls
})

async function renderUrls () {
async function renderUrls() {
renderUrlsToString({
urls: getUrls(),
onItemRendered: render,
onFinished () {
onFinished() {
server.close()
process.exit(0)
}
})
}

async function render (content, url) {
async function render(content, url) {
const dir = getRelativePathFromUrl(url).replace(baseUrl, '')
writeFile(path.join(dist, dir), content)
}
Expand All @@ -41,44 +41,44 @@ async function render (content, url) {
* get all prerender needed urls.
* @returns {string[]}
*/
function getUrls () {
function getUrls() {
const urls = [...getComponentUrls(), ...getDesignUrls(), ...getTemplateUrls(), ...getExtraUrls()]
const zh = urls.map(v => `http://localhost:${port}${baseUrl}/zh-CN/${v}`)
const en = urls.map(v => `http://localhost:${port}${baseUrl}/en-US/${v}`)
const zh = urls.map((v) => `http://localhost:${port}${baseUrl}/zh-CN/${v}`)
const en = urls.map((v) => `http://localhost:${port}${baseUrl}/en-US/${v}`)
return [...zh, ...en]
}

/**
* get components dir pages.
* @returns {string[]}
*/
function getComponentUrls () {
function getComponentUrls() {
const compPath = path.resolve(__dirname, '../docs/zh-CN/components')
return fs.readdirSync(compPath).map(v => `docs/${v.replace('.mdx', '')}`)
return fs.readdirSync(compPath).map((v) => `docs/${v.replace('.mdx', '')}`)
}

/**
* get designs dir pages.
* @returns {string[]}
*/
function getDesignUrls () {
function getDesignUrls() {
const compPath = path.resolve(__dirname, '../docs/zh-CN/designs')
return fs.readdirSync(compPath).map(v => `designs/${v.replace('.mdx', '')}`)
return fs.readdirSync(compPath).map((v) => `designs/${v.replace('.mdx', '')}`)
}

/**
* get templates dir pages.
* @returns {string[]}
*/
function getTemplateUrls () {
function getTemplateUrls() {
const tempPath = path.resolve(__dirname, '../docs/zh-CN/templates')
return fs.readdirSync(tempPath).map(v => `templates/${v.replace('.md', '')}`)
return fs.readdirSync(tempPath).map((v) => `templates/${v.replace('.md', '')}`)
}

/**
* get non-regular pages
* @returns {string[]}
*/
function getExtraUrls () {
function getExtraUrls() {
return ['', 'docs/quick-start', 'docs/template', 'docs/theme', 'docs/i18n', 'docs/changelog']
}
4 changes: 2 additions & 2 deletions components/_util/depreactedPropsCompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const isDevelopment = /development/gi.test(process.env.NODE_ENV)
* @param {[[string, string, Function], [string, string, Function]]} compatPair
* @returns
*/
export const depreactedPropsCompat = (compatPair) => (WrappedComponent) => {
const WrapperComponent = (props) => {
export const depreactedPropsCompat = compatPair => WrappedComponent => {
const WrapperComponent = props => {
const compatProps = { ...props }
const componentName =
WrappedComponent.displayName ||
Expand Down
54 changes: 54 additions & 0 deletions components/_util/hi-request/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import axios from 'axios'

const callBackInter = new Map()

const axiosInstance = axios.create({
type: 'basics',
url: '',
responseType: 'json'
})

axiosInstance.interceptors.request.use(
(config) => {
if (callBackInter.has('beforeRequest')) {
return callBackInter.get('beforeRequest')(config)
}
return config
},
(error) => {
if (callBackInter.has('errorRequest')) {
return callBackInter.get('errorRequest')(error)
}
callBackInter.has('errorCallback') && callBackInter.get('errorCallback')(error)
return error
}
)

axiosInstance.interceptors.response.use(
(response) => {
if (callBackInter.has('beforeResponse')) {
return callBackInter.get('beforeResponse')(response)
}
return response
},
(error) => {
if (callBackInter.has('errorResponse')) {
return callBackInter.get('errorResponse')(error)
}
callBackInter.has('errorCallback') && callBackInter.get('errorCallback')(error)
return error
}
)

const axiosIns = (options) => {
const { beforeResponse, errorResponse, beforeRequest, errorRequest, data, errorCallback } = options
beforeRequest && callBackInter.set('beforeRequest', beforeRequest)
errorResponse && callBackInter.set('errorResponse', errorResponse)
beforeResponse && callBackInter.set('beforeResponse', beforeResponse)
errorRequest && callBackInter.set('errorRequest', errorRequest)
errorCallback && callBackInter.set('errorCallback', errorCallback)

return axiosInstance({ ...options })
}
export { axios }
export default axiosIns
29 changes: 29 additions & 0 deletions components/_util/hi-request/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import axiosIns from './axios'

const download = (options, host) => {
const { filename = '未命名' } = options
const url = host ? host + options.url : options.url

// 设置类型,防止出现乱码
Object.assign(options, { responseType: 'blob' })
axiosIns({ ...options, url }).then(
(res) => {
const { downloadSuccess } = options
const blob = new window.Blob([res.data])
const downloadElement = document.createElement('a')
const href = window.URL.createObjectURL(blob) // 创建下载的链接
downloadElement.href = href
downloadElement.download = filename // 下载后文件名
document.body.appendChild(downloadElement)
downloadElement.click() // 点击下载
document.body.removeChild(downloadElement) // 下载完成移除元素
window.URL.revokeObjectURL(href) // 释放blob对象
downloadSuccess && downloadSuccess(res)
},
(error) => {
const { downloadFail } = options
downloadFail && downloadFail(error)
}
)
}
export default download
65 changes: 65 additions & 0 deletions components/_util/hi-request/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import jsonp from './jsonp'
import download from './download'
import upload from './upload'
import axiosIns, { axios } from './axios'

/**
* 请求方法
* @param options
* @param baseUrl
*/

const InternalRequest = (options, host) => {
const { type = 'basics' } = options
const url = host ? host + options.url : options.url
if (type === 'jsonp' || type === 'download') {
return type === 'jsonp' ? jsonp : download
}
return axiosIns(
type === 'upload'
? {
url,
method: 'post',
...upload(options).options
}
: {
url,
type: 'basics',
...options
}
)
}
const HiRequest = (options, host) => {
return InternalRequest(options, host)
}
// 请求语法糖: reguest.get HiRequest.post ……
const METHODS = ['get', 'post', 'delete', 'put', 'patch', 'head', 'options']
METHODS.forEach((method) => {
HiRequest[method] = (url, options) => HiRequest({ ...options, method, url })
})
// 取消请求
const CANCEL = ['CancelToken', 'Cancel', 'isCancel']
CANCEL.forEach((type) => {
HiRequest[type] = axios[type]
})
// add jsonp
HiRequest.jsonp = jsonp
// download
HiRequest.download = download
// upload
HiRequest.upload = (options, host) => {
options.type = 'upload'
return HiRequest(options, host)
}

// Expose all/spread
HiRequest.all = (promises) => {
return Promise.all(promises)
}
HiRequest.spread = (callback) => {
return (arr) => {
return callback.apply(null, arr)
}
}

export default HiRequest
77 changes: 77 additions & 0 deletions components/_util/hi-request/jsonp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const defaultJsonpOptions = {
timeout: 5000,
jsonpCallback: 'callback'
}

const generateCallbackFunction = () => {
return `jsonp_${Date.now()}_${Math.ceil(Math.random() * 100000)}`
}
const clearFunction = (functionName) => {
try {
delete window[functionName]
} catch (e) {
window[functionName] = undefined
}
}

const removeScript = (scriptId) => {
const script = document.getElementById(scriptId)
if (script) {
document.getElementsByTagName('head')[0].removeChild(script)
}
}
const jsonp = (_url, options = defaultJsonpOptions) => {
const { timeout, jsonpCallback } = options
let url = _url
let timeoutId
return new Promise((resolve, reject) => {
const callbackFunction =
options && options.jsonpCallbackFunction ? options.jsonpCallbackFunction : generateCallbackFunction()

const scriptId = `${jsonpCallback}_${callbackFunction}`

window[callbackFunction] = (response) => {
resolve({
ok: true,
// keep consistent with fetch API
json: () => Promise.resolve(response)
})

if (timeoutId) clearTimeout(timeoutId)

removeScript(scriptId)

clearFunction(callbackFunction)
}

url += url.indexOf('?') === -1 ? '?' : '&'

const jsonpScript = document.createElement('script')
jsonpScript.setAttribute('src', `${url}${jsonpCallback}=${callbackFunction}`)
if (options && options.charset) {
jsonpScript.setAttribute('charset', options.charset)
}
jsonpScript.id = scriptId
document.getElementsByTagName('head')[0].appendChild(jsonpScript)

timeoutId = setTimeout(() => {
reject(new Error(`JSONP request to ${_url} timed out`))

clearFunction(callbackFunction)
removeScript(scriptId)
window[callbackFunction] = () => {
clearFunction(callbackFunction)
}
}, timeout)

// Caught if got 404/500
jsonpScript.onerror = () => {
reject(new Error(`JSONP request to ${_url} failed`))

clearFunction(callbackFunction)
removeScript(scriptId)
if (timeoutId) clearTimeout(timeoutId)
}
})
}
export default jsonp
21 changes: 21 additions & 0 deletions components/_util/hi-request/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const upload = (options) => {
const { file, name = 'file', params = {}, headers, data } = options
const formFile = new window.FormData()
if (file) {
formFile.append(name, file)
}
// 设置除file外需要带入的参数
if (params) {
Object.keys(params).forEach((key) => {
formFile.append(key, params[key])
})
}
return {
options: Object.assign({
...options,
data: data || formFile,
headers: { ...headers, 'Content-Type': 'multipart/form-data' }
})
}
}
export default upload
2 changes: 2 additions & 0 deletions components/_util/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './depreactedPropsCompat'
export { default as SwitchVersion } from './SwitchVersion'


1 change: 1 addition & 0 deletions components/_util/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export function warningOnce (condition, format, args) {
warned[format] = !condition
}
}

Loading

0 comments on commit d16fdd1

Please sign in to comment.