提交 59e02a82 编写于 作者: D DCloud_LXH

chore: editor use invokeViewMethod

上级 406330a7
import { callOptions } from '@dcloudio/uni-shared'
let eventReady = false
// let eventReady = false
let index = 0
let optionsCache: Record<string, any> = {}
function operateEditor(
......@@ -9,33 +9,30 @@ function operateEditor(
type: string,
options: any
) {
const data: { callbackId?: string; options?: any } = {}
if (
const data: { callbackId?: string; options?: any } = { options }
const needCallOptions =
options &&
('success' in options || 'fail' in options || 'complete' in options)
) {
if (needCallOptions) {
const callbackId = String(index++)
data.callbackId = callbackId
optionsCache[callbackId] = options
if (!eventReady) {
UniServiceJSBridge.subscribe(
'onEditorMethodCallback',
({ callbackId, data }: { callbackId: string; data: any }) => {
callOptions(optionsCache[callbackId], data)
delete optionsCache[callbackId]
}
)
eventReady = true
}
}
data.options = options
UniServiceJSBridge.publishHandler(
'editor.' + componentId,
UniServiceJSBridge.invokeViewMethod<{}, { callbackId: string; data: any }>(
`editor.${componentId}`,
{
componentId,
type,
data,
},
({ callbackId, data }) => {
if (needCallOptions) {
callOptions(optionsCache[callbackId], data)
delete optionsCache[callbackId]
}
},
pageId
)
}
......
......@@ -6,8 +6,9 @@ import QuillClass, {
RangeStatic,
StringMap,
} from 'quill'
import { useContextInfo, useSubscribe } from '@dcloudio/uni-components'
import { useContextInfo } from '@dcloudio/uni-components'
import { getRealPath } from '@dcloudio/uni-platform'
import { getCurrentPageId, registerViewMethod } from '@dcloudio/uni-core'
import { CustomEventTrigger } from '../../../helpers/useEvent'
import HTMLParser from '../../../helpers/html-parser'
import loadScript from './loadScript'
......@@ -55,6 +56,7 @@ interface WindowExt extends Window {
export function useQuill(
props: {
id: string
readOnly?: any
placeholder?: any
showImgSize?: any
......@@ -255,9 +257,13 @@ export function useQuill(
}
})
})
const id = useContextInfo()
useSubscribe(
(type: string, data: any) => {
registerViewMethod<
{
type: string
data: { callbackId?: string; options: any }
},
{ callbackId: string; data: any }
>(getCurrentPageId(), `editor.${props.id}`, ({ type, data }, resolve) => {
const { options, callbackId } = data
let res
let range: RangeStatic | undefined
......@@ -408,15 +414,12 @@ export function useQuill(
errMsg = 'not ready'
}
if (callbackId) {
UniViewJSBridge.publishHandler('onEditorMethodCallback', {
resolve({
callbackId,
data: extend({}, res, {
errMsg: `${type}:${errMsg ? 'fail ' + errMsg : 'ok'}`,
}),
})
}
},
id,
true
)
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册