HarmonyOS 鸿蒙Next RichEditor模块中使用controller.addTextSpan()时gesture事件获取的spanPosition为何总是上一次被点击的span位置数据?
HarmonyOS 鸿蒙Next RichEditor模块中使用controller.addTextSpan()时gesture事件获取的spanPosition为何总是上一次被点击的span位置数据?
RichEditor模块里使用controller.addTextSpan()时使用gesture事件,获取到的spanPosition总是是上一次被点击的span位置数据,当上一次点击的是不同的span,则返回的是上次span的位置,不是当前被点击的span的位置。代码如下:
RichEditor(this.options)
.width(‘100%’)
.height(“80%”)
.clip(true)
.expandSafeArea([SafeAreaType.KEYBOARD])
.onReady(()=>{
let defaultStyle = this.controller.getTypingStyle()
console.info(’##DigitalLife::HttpHelper’,JSON.stringify(defaultStyle));
this.controller.addTextSpan(“0123456789\n”,{
style: {
fontColor: Color.Pink,
fontSize: “32”,
},
paragraphStyle: {
textAlign: TextAlign.Start,
leadingMargin: 16
},
gesture:
{
onClick: () => {
let richEditorSelection = this.controller.getSelection()
if (richEditorSelection) {
let spans = richEditorSelection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
let span = item as RichEditorTextSpanResult
this.start = span.spanPosition.spanRange[0]
this.end = span.spanPosition.spanRange[1]
console.info(’##DigitalLife::UI’,‘header click 0’,this.start,this.end);
})
}
}
}
})
this.controller.addTextSpan(“012345678901234567890123456789012345678901234567890123456789\n”,{
style: this.fontTextStyle,
paragraphStyle: {
textAlign: TextAlign.Start,
leadingMargin: 16
},
gesture:
{
onClick: () => {
let richEditorSelection = this.controller.getSelection()
if (richEditorSelection) {
let spans = richEditorSelection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
let span = item as RichEditorTextSpanResult
this.start = span.spanPosition.spanRange[0]
this.end = span.spanPosition.spanRange[1]
console.info(’##DigitalLife::UI’,‘content click 1’,this.start,this.end);
})
}
}
}
})
this.controller.addTextSpan(“xxxx12345678901234567890123456789012345678901234567890123456789\n”,{
style: this.fontTextStyle,
paragraphStyle: {
textAlign: TextAlign.Start,
leadingMargin: 16
},
gesture:
{
onClick: () => {
let richEditorSelection = this.controller.getSelection()
if (richEditorSelection) {
let spans = richEditorSelection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
let span = item as RichEditorTextSpanResult
this.start = span.spanPosition.spanRange[0]
this.end = span.spanPosition.spanRange[1]
console.info(’##DigitalLife::UI’,‘content click 2’,this.start,this.end);
})
}
}
}
})
})
RichEditor(this.options)
.width(‘100%’)
.height(“80%”)
.clip(true)
.expandSafeArea([SafeAreaType.KEYBOARD])
.onReady(()=>{
let defaultStyle = this.controller.getTypingStyle()
console.info(’##DigitalLife::HttpHelper’,JSON.stringify(defaultStyle));
this.controller.addTextSpan(“0123456789\n”,{
style: {
fontColor: Color.Pink,
fontSize: “32”,
},
paragraphStyle: {
textAlign: TextAlign.Start,
leadingMargin: 16
},
gesture:
{
onClick: () => {
let richEditorSelection = this.controller.getSelection()
if (richEditorSelection) {
let spans = richEditorSelection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
let span = item as RichEditorTextSpanResult
this.start = span.spanPosition.spanRange[0]
this.end = span.spanPosition.spanRange[1]
console.info(’##DigitalLife::UI’,‘header click 0’,this.start,this.end);
})
}
}
}
})
this.controller.addTextSpan(“012345678901234567890123456789012345678901234567890123456789\n”,{
style: this.fontTextStyle,
paragraphStyle: {
textAlign: TextAlign.Start,
leadingMargin: 16
},
gesture:
{
onClick: () => {
let richEditorSelection = this.controller.getSelection()
if (richEditorSelection) {
let spans = richEditorSelection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
let span = item as RichEditorTextSpanResult
this.start = span.spanPosition.spanRange[0]
this.end = span.spanPosition.spanRange[1]
console.info(’##DigitalLife::UI’,‘content click 1’,this.start,this.end);
})
}
}
}
})
this.controller.addTextSpan(“xxxx12345678901234567890123456789012345678901234567890123456789\n”,{
style: this.fontTextStyle,
paragraphStyle: {
textAlign: TextAlign.Start,
leadingMargin: 16
},
gesture:
{
onClick: () => {
let richEditorSelection = this.controller.getSelection()
if (richEditorSelection) {
let spans = richEditorSelection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
let span = item as RichEditorTextSpanResult
this.start = span.spanPosition.spanRange[0]
this.end = span.spanPosition.spanRange[1]
console.info(’##DigitalLife::UI’,‘content click 2’,this.start,this.end);
})
}
}
}
})
})
2 回复
在HarmonyOS鸿蒙Next RichEditor模块中,如果在使用controller.addTextSpan()
时遇到gesture事件获取的spanPosition
总是上一次被点击的span位置数据的问题,这通常是由于事件处理机制或状态更新延迟所导致的。
可能的原因包括:
- 事件处理异步性:事件处理可能不是在UI更新后立即进行的,导致获取的位置信息与实际显示不符。
- 状态管理问题:如果RichEditor的状态管理不当,可能会导致上一次的状态被错误地保留和使用。
- 渲染延迟:UI渲染的延迟可能导致事件处理时使用的是旧的UI状态。
为了解决这一问题,可以尝试以下方法:
- 确保在添加新文本跨度后,相关UI状态得到及时更新。
- 检查事件处理逻辑,确保在正确的时机获取位置信息。
- 如果使用了状态管理框架(如MVVM或MVC),请确保状态更新是同步且及时的。
此外,还可以考虑在事件处理代码中添加日志,以跟踪和调试状态更新的时机和顺序。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html