get-selected-text-range.js 766 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import createCallbacks from 'uni-helpers/callbacks'
import {
  getCurrentPageId
} from '../../platform'
import {
  invoke
} from '../../bridge'

const getSelectedTextRangeEventCallbacks = createCallbacks('getSelectedTextRangeEvent')

UniServiceJSBridge.subscribe('onGetSelectedTextRange', ({
  callbackId,
  data
}) => {
  console.log('onGetSelectedTextRange')
  const callback = getSelectedTextRangeEventCallbacks.pop(callbackId)
  if (callback) {
    callback(data)
  }
})

export function getSelectedTextRange (_, callbackId) {
  const pageId = getCurrentPageId()
  UniServiceJSBridge.publishHandler('getSelectedTextRange', {
    pageId,
    callbackId: getSelectedTextRangeEventCallbacks.push(function (res) {
      invoke(callbackId, res)
    })
  }, pageId)
}