From 6e75e691c7dacaa1a8304de99bc5ef34237d7772 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 6 Dec 2019 17:07:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20nodesRef.context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apis.js | 3 +- lib/modules.json | 3 +- src/core/service/api/context/canvas.js | 2 +- .../service/api/context/create-map-context.js | 4 +- .../api/context/create-video-context.js | 12 +++--- src/core/service/api/context/editor.js | 17 +------- .../service/api/ui/create-selector-query.js | 39 ++++++++++++++++++- .../subscribe/api/request-component-info.js | 7 +++- src/core/view/mixins/subscriber.js | 15 +++++++ 9 files changed, 71 insertions(+), 31 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index 1986540bef..7859433e20 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -59,8 +59,7 @@ const media = [ 'saveVideoToPhotosAlbum', 'createVideoContext', 'createCameraContext', - 'createLivePlayerContext', - 'createEditorContext' + 'createLivePlayerContext' ] const device = [ diff --git a/lib/modules.json b/lib/modules.json index b73f80427e..2592668e20 100644 --- a/lib/modules.json +++ b/lib/modules.json @@ -66,8 +66,7 @@ "uni.saveVideoToPhotosAlbum": true, "uni.createVideoContext": true, "uni.createCameraContext": true, - "uni.createLivePlayerContext": true, - "uni.createEditorContext": true + "uni.createLivePlayerContext": true } }, { "name": "device", diff --git a/src/core/service/api/context/canvas.js b/src/core/service/api/context/canvas.js index c4f353b1f5..9adcc7a102 100644 --- a/src/core/service/api/context/canvas.js +++ b/src/core/service/api/context/canvas.js @@ -258,7 +258,7 @@ var methods3 = ['setFillStyle', 'setTextAlign', 'setStrokeStyle', 'setGlobalAlph 'setTextBaseline', 'setLineDash' ] -class CanvasContext { +export class CanvasContext { constructor (id, pageId) { this.id = id this.pageId = pageId diff --git a/src/core/service/api/context/create-map-context.js b/src/core/service/api/context/create-map-context.js index bdfb94a48d..e50a2a454e 100644 --- a/src/core/service/api/context/create-map-context.js +++ b/src/core/service/api/context/create-map-context.js @@ -7,7 +7,7 @@ function operateMapPlayer (mapId, pageVm, type, data) { invokeMethod('operateMapPlayer', mapId, pageVm, type, data) } -class MapContext { +export class MapContext { constructor (id, pageVm) { this.id = id this.pageVm = pageVm @@ -43,4 +43,4 @@ export function createMapContext (id, context) { return new MapContext(id, context) } return new MapContext(id, getCurrentPageVm('createMapContext')) -} +} diff --git a/src/core/service/api/context/create-video-context.js b/src/core/service/api/context/create-video-context.js index f238c10015..9d2d0a2824 100644 --- a/src/core/service/api/context/create-video-context.js +++ b/src/core/service/api/context/create-video-context.js @@ -9,7 +9,7 @@ function operateVideoPlayer (videoId, pageVm, type, data) { invokeMethod('operateVideoPlayer', videoId, pageVm, type, data) } -class VideoContext { +export class VideoContext { constructor (id, pageVm) { this.id = id this.pageVm = pageVm @@ -25,8 +25,8 @@ class VideoContext { operateVideoPlayer(this.id, this.pageVm, 'stop') } seek (position) { - operateVideoPlayer(this.id, this.pageVm, 'seek', { - position + operateVideoPlayer(this.id, this.pageVm, 'seek', { + position }) } sendDanmu (args) { @@ -36,8 +36,8 @@ class VideoContext { if (!~RATES.indexOf(rate)) { rate = 1.0 } - operateVideoPlayer(this.id, this.pageVm, 'playbackRate', { - rate + operateVideoPlayer(this.id, this.pageVm, 'playbackRate', { + rate }) } requestFullScreen (args = {}) { @@ -59,4 +59,4 @@ export function createVideoContext (id, context) { return new VideoContext(id, context) } return new VideoContext(id, getCurrentPageVm('createVideoContext')) -} +} diff --git a/src/core/service/api/context/editor.js b/src/core/service/api/context/editor.js index 430bde5f5c..008b8f4cd8 100644 --- a/src/core/service/api/context/editor.js +++ b/src/core/service/api/context/editor.js @@ -1,6 +1,3 @@ -import { - getCurrentPageId -} from '../../platform' import { callback } from 'uni-shared' @@ -22,7 +19,7 @@ UniServiceJSBridge.subscribe('onEditorMethodCallback', ({ const methods = ['insertDivider', 'insertImage', 'insertText', 'setContents', 'getContents', 'clear', 'removeFormat', 'undo', 'redo'] -class EditorContext { +export class EditorContext { constructor (id, pageId) { this.id = id this.pageId = pageId @@ -45,15 +42,3 @@ methods.forEach(function (method) { }) }) }) - -export function createEditorContext (id, context) { - if (context) { - return new EditorContext(id, context.$page.id) - } - const pageId = getCurrentPageId() - if (pageId) { - return new EditorContext(id, pageId) - } else { - UniServiceJSBridge.emit('onError', 'createEditorContext:fail') - } -} diff --git a/src/core/service/api/ui/create-selector-query.js b/src/core/service/api/ui/create-selector-query.js index 917bb4f3e7..b4ea789cb7 100644 --- a/src/core/service/api/ui/create-selector-query.js +++ b/src/core/service/api/ui/create-selector-query.js @@ -7,6 +7,26 @@ import { getCurrentPageVm } from '../../platform' +import { CanvasContext } from '../context/canvas' +import { MapContext } from '../context/create-map-context' +import { VideoContext } from '../context/create-video-context' +import { EditorContext } from '../context/editor' + +const ContextClasss = { + canvas: CanvasContext, + map: MapContext, + video: VideoContext, + editor: EditorContext +} + +function convertContext (result) { + if (result.context) { + const { id, name, page } = result.context + const ContextClass = ContextClasss[name] + result.context = ContextClass && new ContextClass(id, page) + } +} + class NodesRef { constructor (selectorQuery, component, selector, single) { this._selectorQuery = selectorQuery @@ -53,6 +73,18 @@ class NodesRef { ) return this._selectorQuery } + + context (callback) { + this._selectorQuery._push( + this._selector, + this._component, + this._single, { + context: true + }, + callback + ) + return this._selectorQuery + } } class SelectorQuery { @@ -66,6 +98,11 @@ class SelectorQuery { invokeMethod('requestComponentInfo', this._page, this._queue, res => { const queueCbs = this._queueCb res.forEach((result, index) => { + if (Array.isArray(result)) { + result.forEach(convertContext) + } else { + convertContext(result) + } const queueCb = queueCbs[index] if (isFn(queueCb)) { queueCb.call(this, result) @@ -109,4 +146,4 @@ export function createSelectorQuery (context) { return new SelectorQuery(context) } return new SelectorQuery(getCurrentPageVm('createSelectorQuery')) -} +} diff --git a/src/core/view/bridge/subscribe/api/request-component-info.js b/src/core/view/bridge/subscribe/api/request-component-info.js index c528c5283d..0ca86a2eaf 100644 --- a/src/core/view/bridge/subscribe/api/request-component-info.js +++ b/src/core/view/bridge/subscribe/api/request-component-info.js @@ -74,6 +74,11 @@ function getNodeInfo (el, fields) { info.scrollTop = 0 } } + if (fields.context) { + if (el.__vue__ && el.__vue__._getContextInfo) { + info.context = el.__vue__._getContextInfo() + } + } return info } @@ -134,4 +139,4 @@ export function requestComponentInfo ({ reqId, res: result }, pageVm.$page.id) -} +} diff --git a/src/core/view/mixins/subscriber.js b/src/core/view/mixins/subscriber.js index 1be0a9ca5b..f04718c09d 100644 --- a/src/core/view/mixins/subscriber.js +++ b/src/core/view/mixins/subscriber.js @@ -19,6 +19,9 @@ export default { }, beforeDestroy () { // 销毁时移除 this._toggleListeners('unsubscribe', this.id) + if (this._contextId) { + this._toggleListeners('unsubscribe', this._contextId) + } }, methods: { _toggleListeners (type, id, watch) { @@ -31,6 +34,18 @@ export default { } // 纠正VUniVideo等组件命名为Video UniViewJSBridge[type](this.$page.id + '-' + this.$options.name.replace(/VUni([A-Z])/, '$1').toLowerCase() + '-' + id, this._handleSubscribe) + }, + _getContextInfo () { + const id = `context-${this._uid}` + if (!this._contextId) { + this._toggleListeners('subscribe', id) + this._contextId = id + } + return { + name: this.$options.name.replace(/VUni([A-Z])/, '$1').toLowerCase(), + id, + page: this.$page.id + } } } } -- GitLab