diff --git a/src/core/helpers/protocol/ui/load-font-face.js b/src/core/helpers/protocol/ui/load-font-face.js new file mode 100644 index 0000000000000000000000000000000000000000..6c6fb7e9a8462c1204c9488a2ee2729f2daea01e --- /dev/null +++ b/src/core/helpers/protocol/ui/load-font-face.js @@ -0,0 +1,26 @@ +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 + } +} diff --git a/src/core/service/api/ui/load-font-face.js b/src/core/service/api/ui/load-font-face.js new file mode 100644 index 0000000000000000000000000000000000000000..fa2cf580ac31d32950ffed800116c0f330530077 --- /dev/null +++ b/src/core/service/api/ui/load-font-face.js @@ -0,0 +1,27 @@ +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) +} diff --git a/src/core/view/bridge/subscribe/font.js b/src/core/view/bridge/subscribe/font.js new file mode 100644 index 0000000000000000000000000000000000000000..26753477591dfdbd18ba8a9a417479cc7e4f0732 --- /dev/null +++ b/src/core/view/bridge/subscribe/font.js @@ -0,0 +1,39 @@ +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` + } + }) + } +} diff --git a/src/core/view/bridge/subscribe/index.js b/src/core/view/bridge/subscribe/index.js index e5ad516d983582fd3fc9affb028a1754a01d86f3..3fbaede98958288d08769cb0d6f05ddab97b7209 100644 --- a/src/core/view/bridge/subscribe/index.js +++ b/src/core/view/bridge/subscribe/index.js @@ -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) -} +} diff --git a/src/platforms/h5/helpers/todo-api.js b/src/platforms/h5/helpers/todo-api.js index b2c26b7ff92ee2ee4aff7a518ce1d768b0d71d55..4294ebab27f7109b6feb728f4ecc6986a58bc2b4 100644 --- a/src/platforms/h5/helpers/todo-api.js +++ b/src/platforms/h5/helpers/todo-api.js @@ -47,7 +47,6 @@ export default [ 'stopBeaconDiscovery', 'setBackgroundColor', 'setBackgroundTextStyle', - 'loadFontFace', 'getProvider', 'login', 'checkSession',