From 169889cf33806707f96155facf09c52ac4f1b9a8 Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 25 Nov 2019 20:34:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20v3-app=20=E5=92=8C=20h5=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20uni.loadFontFace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../helpers/protocol/ui/load-font-face.js | 26 +++++++++++++ src/core/service/api/ui/load-font-face.js | 27 +++++++++++++ src/core/view/bridge/subscribe/font.js | 39 +++++++++++++++++++ src/core/view/bridge/subscribe/index.js | 6 ++- src/platforms/h5/helpers/todo-api.js | 1 - 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/core/helpers/protocol/ui/load-font-face.js create mode 100644 src/core/service/api/ui/load-font-face.js create mode 100644 src/core/view/bridge/subscribe/font.js 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 000000000..6c6fb7e9a --- /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 000000000..fa2cf580a --- /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 000000000..267534775 --- /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 e5ad516d9..3fbaede98 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 b2c26b7ff..4294ebab2 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', -- GitLab