Skip to content

Commit

Permalink
feat: 支持 nodesRef.context
Browse files Browse the repository at this point in the history
  • Loading branch information
zhetengbiji committed Dec 6, 2019
1 parent 517afa3 commit 6e75e69
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 31 deletions.
3 changes: 1 addition & 2 deletions lib/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ const media = [
'saveVideoToPhotosAlbum',
'createVideoContext',
'createCameraContext',
'createLivePlayerContext',
'createEditorContext'
'createLivePlayerContext'
]

const device = [
Expand Down
3 changes: 1 addition & 2 deletions lib/modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
"uni.saveVideoToPhotosAlbum": true,
"uni.createVideoContext": true,
"uni.createCameraContext": true,
"uni.createLivePlayerContext": true,
"uni.createEditorContext": true
"uni.createLivePlayerContext": true
}
}, {
"name": "device",
Expand Down
2 changes: 1 addition & 1 deletion src/core/service/api/context/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/core/service/api/context/create-map-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,4 +43,4 @@ export function createMapContext (id, context) {
return new MapContext(id, context)
}
return new MapContext(id, getCurrentPageVm('createMapContext'))
}
}
12 changes: 6 additions & 6 deletions src/core/service/api/context/create-video-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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 = {}) {
Expand All @@ -59,4 +59,4 @@ export function createVideoContext (id, context) {
return new VideoContext(id, context)
}
return new VideoContext(id, getCurrentPageVm('createVideoContext'))
}
}
17 changes: 1 addition & 16 deletions src/core/service/api/context/editor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {
getCurrentPageId
} from '../../platform'
import {
callback
} from 'uni-shared'
Expand All @@ -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
Expand All @@ -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')
}
}
39 changes: 38 additions & 1 deletion src/core/service/api/ui/create-selector-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -109,4 +146,4 @@ export function createSelectorQuery (context) {
return new SelectorQuery(context)
}
return new SelectorQuery(getCurrentPageVm('createSelectorQuery'))
}
}
7 changes: 6 additions & 1 deletion src/core/view/bridge/subscribe/api/request-component-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -134,4 +139,4 @@ export function requestComponentInfo ({
reqId,
res: result
}, pageVm.$page.id)
}
}
15 changes: 15 additions & 0 deletions src/core/view/mixins/subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}
}
}

0 comments on commit 6e75e69

Please sign in to comment.