ios 键盘扩展:输入前处理上下文仅在关闭并重新打开键盘后执行

esbemjvw  于 2023-08-08  发布在  iOS
关注(0)|答案(1)|浏览(107)

我正在为iOS开发一个键盘扩展,在replaceDocumentText函数中处理输入之前的上下文时遇到了一个问题。该函数应该处理输入光标前后的文本,但我注意到,处理输入前上下文的循环(documentContextBeforeInput)只有在我关闭并重新打开键盘后才执行。
我正在使用KeyboardKit扩展。
`

func replaceDocumentText(textProxy: UITextDocumentProxy,       suggestionsRequired: Bool=false)  

    

  async -> (context: String, count: Int) {
    var startingOffset = 0
    var context = ""
    var forwardContextCount = 0

    //show loading only if suggestions required
    if suggestionsRequired {
        self.loadingState = .loading
    }

while let previousContext = textProxy.documentContextBeforeInput, !previousContext.isEmpty {
   /// Check for the context before the cursor
        context = previousContext + context
        startingOffset += previousContext.count
        textProxy.adjustTextPosition(byCharacterOffset: -previousContext.count)
       
 
       try? await Task.sleep(nanoseconds: 50_000_000)
    }
    
    // Adjust cursor from where it started
       textProxy.adjustTextPosition(byCharacterOffset: context.count)
    
 
        try? await Task.sleep(nanoseconds: 1000_000_000)
  
    
    
    while let nextContext = textProxy.documentContextAfterInput, !nextContext.isEmpty {
        /// Check for the context After the cursor
        forwardContextCount += 1
        context += nextContext
        textProxy.adjustTextPosition(byCharacterOffset: nextContext.count)
    
        
        try? await Task.sleep(nanoseconds: 100_000_000)
    }
    
    self.promptText = context
    self.promptCount = context.count
    
    return await withCheckedContinuation({ continuation in
        if suggestionsRequired {
            Task{
                await self.getSuggestions()

            }
        }
       
        continuation.resume(returning: (self.promptText, self.promptCount))
    })
}``

字符串
当我在textField中有文本时,我按下按钮来读取上下文,但这一行永远不会执行,我相信这是因为“.isEmpty”条件while let previousContext = textProxy.documentContextBeforeInput, !previousContext.isEmpty
但是我有文本在textField只要我关闭我的键盘扩展并重新打开它,现在按下按钮,一切都会顺利工作。请帮我这个忙。
我尝试了一些文本的情况下,但无法找出问题

bttbmeg0

bttbmeg01#

func replaceDocumentText(textProxy: UITextDocumentProxy, suggestionsRequired: Bool = false) async -> (context: String, count: Int) {
    var context = ""
    var forwardContextCount = 0

    //show loading only if suggestions required
    if suggestionsRequired {
        self.loadingState = .loading
    }

    // Combine previous and next context directly without adjusting text position
    let previousContext = textProxy.documentContextBeforeInput ?? ""
    let nextContext = textProxy.documentContextAfterInput ?? ""
    context = previousContext + nextContext

    self.promptText = context
    self.promptCount = context.count

    return await withCheckedContinuation({ continuation in
        if suggestionsRequired {
            Task {
                await self.getSuggestions()
            }
        }
       
        continuation.resume(returning: (self.promptText, self.promptCount))
    })
}

字符串

相关问题