提交 169889cf 编写于 作者: Q qiang

feat: v3-app 和 h5 支持 uni.loadFontFace

上级 00607758
export const loadFontFace = {
family: {
type: String,
required: true
},
source: {
type: String,
required: true
},
desc: {
type: Object,
required: false
},
success: {
type: Function,
required: false
},
fail: {
type: Function,
required: false
},
complete: {
type: Function,
required: false
}
}
import {
invoke
} from 'uni-core/service/bridge'
import {
getCurrentPageId
} from '../../platform'
UniServiceJSBridge.subscribe('onLoadFontFaceCallback', ({
callbackId,
data
}) => {
invoke(callbackId, data)
})
export function loadFontFace (options, callbackId) {
const pageId = getCurrentPageId()
if (!pageId) {
return {
errMsg: 'loadFontFace:fail not font page'
}
}
UniServiceJSBridge.publishHandler('loadFontFace', {
options,
callbackId
}, pageId)
}
export function loadFontFace ({
options,
callbackId
}) {
const { family, source, desc = {} } = options
const fonts = document.fonts
if (fonts) {
const fontFace = new FontFace(family, source, desc)
fontFace
.load()
.then(() => {
fonts.add(fontFace)
UniViewJSBridge.publishHandler('onLoadFontFaceCallback', {
callbackId,
data: {
errMsg: `loadFontFace:ok`
}
})
})
.catch(error => {
UniViewJSBridge.publishHandler('onLoadFontFaceCallback', {
callbackId,
data: {
errMsg: `loadFontFace:fail ${error}`
}
})
})
} else {
var style = document.createElement('style')
style.innerText = `@font-face{font-family:"${family}";src:${source};font-style:${desc.style};font-weight:${desc.weight};font-stretch:${desc.stretch};unicode-range:${desc.unicodeRange};font-variant:${desc.variant};font-feature-settings:${desc.featureSettings};}`
document.head.appendChild(style)
UniViewJSBridge.publishHandler('onLoadFontFaceCallback', {
callbackId,
data: {
errMsg: `loadFontFace:ok`
}
})
}
}
......@@ -3,6 +3,9 @@ import subscribeApis from 'uni-api-subscribe'
import {
pageScrollTo
} from './scroll'
import {
loadFontFace
} from './font'
import initPlatformSubscribe from 'uni-platform/view/bridge/subscribe'
......@@ -12,6 +15,7 @@ export default function initSubscribe (subscribe) {
})
subscribe('pageScrollTo', pageScrollTo)
subscribe('loadFontFace', loadFontFace)
initPlatformSubscribe(subscribe)
}
}
......@@ -47,7 +47,6 @@ export default [
'stopBeaconDiscovery',
'setBackgroundColor',
'setBackgroundTextStyle',
'loadFontFace',
'getProvider',
'login',
'checkSession',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册