Text editing method and device, electronic equipment and storage medium
By constructing a delayed state object and a real-time priority strategy, the problem of user intent changes during AI asynchronous response was solved, thereby improving the stability and accuracy of the intelligent editor and enhancing the user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- IFLYTEK CO LTD
- Filing Date
- 2026-01-22
- Publication Date
- 2026-05-15
AI Technical Summary
Existing technologies, when processing AI-generated asynchronous content, suffer from inaccurate replacement target locations or conflicts with real-time user intents due to changes in user interaction intent or loss of original state during response delays. They lack persistent text range management mechanisms, multi-priority intelligent replacement strategies, range validity checks and multi-level rollback mechanisms, and automatic format recognition and secure conversion of AI-generated content for AI asynchronous response scenarios.
By constructing a delayed state object and combining it with a priority strategy for real-time state selection, the target editing range is determined. A multi-level rollback mechanism and a visual highlight synchronization mechanism are introduced to ensure accurate judgment of user intent and stability of the editor during AI asynchronous response.
It enables intelligent editing during AI asynchronous response, is compatible with user change intentions, prevents the loss of original selections, improves the robustness of the editor and user experience, and ensures the safety and reliability of replacement operations.
Smart Images

Figure CN122047178A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a text editing method, apparatus, electronic device, and storage medium. Background Technology
[0002] With the development of Large Language Model (LLM) technology, using Artificial Intelligence (AI) in editors to polish, continue, or rewrite text selected by users has become a common way to assist writing.
[0003] To achieve the above functions, existing technologies typically employ an asynchronous interaction mode. The editor responds to user selections by sending requests to a large cloud model. While waiting for the model to generate the inference content, the user interface remains in a waiting state. Upon receiving the generated content from the model, the editor calls a replacement interface to write the generated content into the document. In specific implementations, some existing technologies employ a locking strategy, directly replacing the content based on the coordinates recorded when the request was sent; or an immediate strategy, directly replacing the content using the editor's current active cursor position at the moment the generated content is returned.
[0004] However, the aforementioned existing technologies have limitations when handling long-duration asynchronous response scenarios. Since the generation process of large models typically involves delays of several seconds to tens of seconds, the user's interactive intent and the editor's state may dynamically change during this period. If a locking strategy is adopted, when the user generates a new editing intent and actively selects another text area during the waiting period, the system will still forcibly replace the text in the original position, violating the user's latest intent. If an instant strategy is adopted, when the user only performs a viewing operation during the waiting period, causing the cursor to move or canceling the text selection, the system often cannot correctly trace back to the target that originally needed to be replaced, resulting in incorrect insertion of generated content or operation failure, thus causing a logical conflict between asynchronous data return and the user's real-time operation state. Summary of the Invention
[0005] This invention provides a text editing method, apparatus, electronic device, and storage medium to address the shortcomings of existing technologies in processing AI-generated asynchronous content. These shortcomings arise from changes in user interaction intent or loss of the original state during response delays, leading to inaccurate replacement target locations or conflicts with real-time user intent. The invention achieves intelligent decision-making and precise updates to the editor's target replacement range.
[0006] This invention provides a text editing method, comprising: In response to a smart editing command triggered in the editor, a deferred state object is constructed and a generation request is sent to the content generation model; the deferred state object includes the currently selected initial text content and the position information of the initial text content; Receive the generated content output by the content generation model based on the generation request; The current real-time selection state of the editor is obtained, and the target editing range is determined by selecting one between the area corresponding to the real-time selection state and the area corresponding to the delayed state object based on a preset priority strategy. The text content within the target editing range is updated based on the generated content.
[0007] According to a text editing method provided by the present invention, the step of determining a target editing range between the region corresponding to the real-time selection state and the region corresponding to the delayed state object based on a preset priority strategy includes: Detect whether the real-time selection state is a non-empty selection; If the real-time selection state is detected to be a non-empty selection, the target editing range is determined based on the real-time text content corresponding to the real-time selection state. If the real-time selection state is detected as an empty selection, the position information of the initial text content in the delayed state object is used to determine the target editing range.
[0008] According to a text editing method provided by the present invention, before updating the text within the target editing range based on the generated content, the method further includes: Get the total length value of the document in the editor; Analyze the start and end coordinates of the target editing area; Determine whether the starting position coordinate is greater than or equal to zero, whether the ending position coordinate is less than or equal to the total length value, and whether the starting position coordinate is less than or equal to the ending position coordinate, and obtain the determination result; If all the judgment results are determined to be yes, then the update processing steps are executed.
[0009] According to a text editing method provided by the present invention, after determining that all the judgment results are yes, the method further includes: Read the text content of the editor within the target editing area; Perform the process of removing leading and trailing whitespace characters on the text content and the initial text content stored in the delayed state object respectively, and compare whether the processed text content is consistent with the processed initial text content to obtain the comparison result; If the comparison results are consistent, then the update process is executed.
[0010] According to a text editing method provided by the present invention, after constructing the delayed state object, the method further includes: Create a highlight decoration object based on the position information of the initial text content; The highlighted decorative object is rendered to the view layer of the editor so that a preset visual marker is displayed in the editor for the area corresponding to the position information of the initial text content.
[0011] According to a text editing method provided by the present invention, after rendering the highlighted decoration object to the view layer of the editor, the method further includes: In response to a detected document content change operation in the editor, the location mapping relationship associated with the document content change operation is obtained; The new position coordinates of the highlighted decoration object in the editor are calculated using the position mapping relationship; The rendering position of the highlighted decorative object in the view layer is updated based on the new position coordinates.
[0012] According to a text editing method provided by the present invention, the step of updating the text within the target editing range based on the generated content includes: Detect whether the generated content contains one or more of the following: code block markers, heading markers, or list markers; If so, the generated content will be converted into HTML format data; The HTML formatted data is rendered into the target editing area.
[0013] According to a text editing method provided by the present invention, rendering the HTML format data into the target editing area includes: Iterate through the HTML data, removing script tags, event handling attributes, and non-whitelisted URLs to generate cleaned HTML data. The purified HTML format data is rendered into the target editing area.
[0014] According to a text editing method provided by the present invention, the step of updating the text within the target editing range based on the generated content includes: Perform a first replacement operation, insert the generated content into the target editing area in rich text format, and monitor the execution result of the first replacement operation; If the execution result is detected to be abnormal or failed, a second replacement operation is triggered, which includes inserting the generated content into the target editing range in plain text format.
[0015] According to a text editing method provided by the present invention, after updating the text content within the target editing range based on the generated content, the method further includes: The delayed state object is reset to null, and the visual markers corresponding to the initial text range in the editor are cleared.
[0016] The present invention also provides a text editing device, comprising the following modules: The request processing module, in response to a smart editing command triggered in the editor, constructs a deferred state object and sends a generation request to the content generation model; the deferred state object includes the currently selected initial text content and the position information of the initial text content; The content receiving module receives the generated content output by the content generation model based on the generation request; The range determination module obtains the current real-time selection state of the editor and, based on a preset priority strategy, determines the target editing range between the area corresponding to the real-time selection state and the area corresponding to the delayed state object. The update processing module updates the text content within the target editing range based on the generated content.
[0017] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement any of the text editing methods described above.
[0018] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the text editing method as described above.
[0019] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the text editing method as described above.
[0020] The text editing method, apparatus, electronic device, and storage medium provided by this invention introduce persistent delayed state objects as historical backups and combine them with real-time cursor states for multi-level priority adjudication. This effectively addresses the time difference problem of asynchronous response of artificial intelligence models, ensuring compatibility with user-generated change intentions during the waiting period and preventing the loss of the original selection area. This significantly improves the robustness of intelligent editing and user experience. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0022] Figure 1 This is one of the flowcharts illustrating the text editing method provided by the present invention.
[0023] Figure 2 This is a schematic diagram of the data structure of the delayed state object provided by the present invention.
[0024] Figure 3 This is the second flowchart of the text editing method provided by the present invention.
[0025] Figure 4 This is a schematic diagram of the execution flow of the range validity check and multi-level rollback provided by the present invention.
[0026] Figure 5 This is a flowchart illustrating the visual highlighting and document change synchronization mechanism provided by the present invention.
[0027] Figure 6 This is a flowchart illustrating the three-priority intelligent replacement decision-making process provided by the present invention.
[0028] Figure 7 This is a schematic diagram of the structure of the text editing device provided by the present invention.
[0029] Figure 8 This is a schematic diagram of the architecture of the text editing system provided by the present invention.
[0030] Figure 9 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0031] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0032] It should be noted that, in the description of this invention, the terms "comprising," "including," or any other variations thereof are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element. Those skilled in the art will understand the specific meaning of the above terms in this invention according to the specific circumstances.
[0033] Before detailing the text editing method, apparatus, electronic device, and storage medium provided by this invention, some terms that may be mentioned in subsequent embodiments are defined in advance, as shown in Table 1: Table 1. List of Terminology Definitions
[0034] With the rapid development of large language model technology, AI-assisted writing and intelligent editing have become important trends in office software and content creation. Currently, various technical solutions have emerged in the industry that integrate AI functions into rich text editors (RTEs) to form AI editors (hereinafter referred to as editors): Existing technology one: Real-time text replacement solution. Current mainstream AI editors (such as Notion AI and Google Docs AI) typically use a real-time replacement mode. When a user selects text, the AI generates content, and the content is immediately replaced after generation. This type of solution has the following problems: (1) When the AI response time is long (usually 3-30 seconds), the user may have changed the selection or edited other content, causing the original selection range to become invalid; (2) It lacks an adaptation mechanism for changes in user behavior during AI response and cannot distinguish between the text that the user is selecting and the text captured in the past. (3) When the document content changes, the original text coordinates (from / to) may go out of bounds or point to the wrong location.
[0035] Existing technology two: a simple cached replacement solution. Some editors attempt to cache the user's selection range, but the implementation is relatively simple: (1) Only the text coordinates are cached, and the original text content is not saved, which makes it impossible to verify whether the replacement target is correct; (2) Lack of range validity verification. When the document content changes, directly using cached coordinates may lead to incorrect replacement positions or program abnormalities. (3) The diversity of formats of AI-generated content (generally including plain text, Markdown, HTML, etc.) is not taken into account, and there is a lack of automatic format conversion capability.
[0036] Existing technology three, a single-priority replacement scheme, means that existing editors typically employ a single strategy for text replacement: (1) It only uses the currently selected range or only uses the cached range, lacking intelligent priority judgment; (2) When the user's intention is unclear, it cannot provide friendly interactive feedback; (3) Lack of fault tolerance for replacement failure scenarios can easily lead to user data loss or interface abnormalities.
[0037] Existing technology four: AI content security processing solutions. Existing technologies have shortcomings in the secure processing of AI-generated content: (1) The Markdown content generated by AI was directly converted into HTML format data and inserted into the editor without XSS security cleanup; (2) Lack of automatic detection of AI-generated content format, requiring users to manually specify content type; (3) The lack of a rollback mechanism when content conversion or insertion fails may cause users to lose AI-generated content.
[0038] In summary, existing technologies in the field of AI-generated content editor integration have the following core problems: lack of text range persistence management mechanism for AI asynchronous response scenarios, lack of multi-priority intelligent replacement strategy, lack of range validity verification and multi-level rollback mechanism, lack of automatic format recognition and secure conversion of AI-generated content, and lack of state management and forced cleanup guarantee for the replacement process.
[0039] To address the aforementioned shortcomings of existing technologies, this invention provides a text editing method, apparatus, electronic device, and storage medium. It aims to offer a method for secure replacement and intelligent range management of AI-generated content in an editor, capable of solving or to some extent satisfying one or more of the following user needs during AI-assisted text editing: (1) Provide a delayed replacement state mechanism that can persistently save the user's text content selection during the AI asynchronous response, and solve the problem of loss of selection range caused by response time difference.
[0040] (2) A three-priority intelligent replacement strategy is provided, which can intelligently determine the replacement target based on the user's real-time behavior and improve the user experience.
[0041] (3) Provides a text range validity check and multi-level rollback mechanism to ensure the security and reliability of replacement operations in case of document changes or abnormalities.
[0042] (4) Provides an automatic format recognition and secure conversion method for AI-generated content, supporting automatic Markdown detection, HTML conversion and XSS protection.
[0043] (5) Provide a mandatory cleanup guarantee mechanism for replacement states to prevent state freezing caused by abnormal situations.
[0044] (6) Provides a visual highlighting and metadata synchronization mechanism to provide users with continuous visual feedback during AI response.
[0045] The following is combined Figures 1-9 As shown, the specific details of the text editing method, apparatus, electronic device and storage medium provided by the present invention are described in detail.
[0046] This invention provides a text editing method that can be applied to electronic devices such as personal computers, laptops, smartphones, and tablets. The specific execution entity can be text editing software installed in the aforementioned electronic devices, browser plugins, or text editing devices integrated into cloud office platforms.
[0047] Figure 1 This is one of the flowcharts illustrating the text editing method provided by the present invention, such as... Figure 1 As shown, including but not limited to the following steps: Step 11: In response to a smart editing command triggered in the editor, construct a deferred state object and send a generation request to the content generation model.
[0048] In this embodiment, the editor can be an editing tool that supports rich text formatting, such as an online document editor based on web technology, or an editing window of local office software. When using the editor to write documents, users can trigger intelligent editing commands through preset interactive methods.
[0049] The specific triggering methods can be keyboard shortcuts, such as "Cmd+L" or "Ctrl+L", clicking the AI assistant icon in the editor toolbar, or activating it through voice commands.
[0050] When the system detects that the aforementioned intelligent editing command has been triggered, it first captures the initial text content currently selected by the user in the document. To address the potential time lag issue when the AI-generated content is returned, the system immediately constructs a delayed state object in memory while sending the generation request to the content generation model.
[0051] This deferred state object can be a data structure used to persistently store temporary states, at least storing the position information of the initial text content currently selected by the user. It should be noted that the position information can be character index coordinates describing the start and end positions of the initial text content in the document (e.g., {from: 156, to: 189}), or it can be path information of a Document Object Model (DOM) node or other identifiers that can uniquely locate a document region. In an optional implementation, for subsequent verification purposes, the deferred state object can also store the string of the initial text content selected by the user (i.e., the actual text).
[0052] In this embodiment, after constructing and storing the aforementioned delayed state object, or synchronously with the operation of constructing the delayed state object, the system encapsulates the selected initial text content into a prompt word and sends a generation request to the background content generation model (such as a large language model) through a network interface, so that the content generation model can polish, continue, or rewrite the initial text content.
[0053] like Figure 2 As shown, this delayed state object can be represented as pendingReplacement in the code implementation. It adopts a structured data storage method and mainly contains two core fields: First, there's the text content field (text), used to store the original text content string selected by the user, for example... Figure 2 The first field shows "This is a text selected by the user"; the second is the range information field, used to store the position information of the initial text content. For example... Figure 2 As shown, this location information can specifically include a start position (from, for example, 156) and an end position (to, for example, 203). Here, from represents the starting character index of the selected area in the document, usually counting from 0; to represents the ending character position of the selected area in the document, and usually does not include the character at that position.
[0054] Through this structured storage method, the system can accurately freeze and save the context state at the moment the user triggers the command. No matter how the cursor moves in the editor afterward, the object can provide a traceable anchor point.
[0055] Step 12: Receive the generated content output by the content generation model based on the generation request.
[0056] Because the inference and network transmission of the content generation model typically take some time (e.g., 3 to 30 seconds), this is an asynchronous waiting process. During this time, the editor interface remains responsive, allowing users to view, scroll, and even edit other areas. Once the content generation model has finished processing, the system will receive the generated content returned by the model either through a callback function or asynchronously.
[0057] The generated content can be a string, which may be plain text or contain Markdown tags or HTML code, depending on the output settings of the content generation model.
[0058] Step 13: Obtain the current real-time selection state of the editor, and based on a preset priority strategy, determine the target editing range between the area corresponding to the real-time selection state and the area corresponding to the delayed state object.
[0059] When the content generated by the content generation model returns, the user's focus, i.e., the real-time selection state, may have changed or may remain unchanged compared to the initial state when the request was sent. To intelligently adapt to the user's true editing intent, the system needs to introduce a decision-making mechanism, namely a preset priority strategy, to choose between the current focus (real-time selection state) and the "historical focus (delayed state object)," two potential editing targets.
[0060] Specifically, the system first calls the editor's API to obtain the current real-time selection state. This state reflects the current position of the user's cursor or the selected text area. Simultaneously, the system reads the deferred state object constructed and stored in step 11, and parses the positional information of the initial text content contained within it.
[0061] The system then selects one option based on a preset priority strategy. The core logic of this priority strategy is to determine whether the user's current real-time behavior demonstrates a new and clear editing intention.
[0062] As an optional implementation, the method of determining the target editing range based on a preset priority strategy, choosing between the region corresponding to the real-time selection state and the region corresponding to the delayed state object, may include, but is not limited to: Detect whether the real-time selection state is a non-empty selection; If the real-time selection state is detected to be a non-empty selection, the target editing range is determined based on the real-time text content corresponding to the real-time selection state. If the real-time selection state is detected as an empty selection, the position information of the initial text content in the delayed state object is used to determine the target editing range.
[0063] Specifically, the system can determine the target editing scope to be replaced based on the following logic: First, the system checks whether the real-time selection state is a non-empty selection. A non-empty selection means that the user has explicitly selected a non-empty text region in the editor; that is, the cursor is not just blinking, but covering specific characters. If the detection result is a non-empty selection, the system determines that the user's intent has changed. Specifically, the user's intent may be to replace the currently selected text with content generated by the content generation model. Therefore, the system directly determines the real-time text range corresponding to the real-time selection state as the target editing range. This approach reflects the principle of prioritizing the user's real-time intent.
[0064] Secondly, if the real-time selection state is detected as empty (meaning the user has only placed the cursor without selecting any content), or if the `selection.empty` property is true, the system determines that the user is in a waiting state, still intending to replace the initially selected text content. In this case, the system will backtrack and call the delayed state object stored in step 11 to extract the position information of the initial text content. Then, the system uses the document area pointed to by this position information to determine the target editing area.
[0065] Step 14: Update the text content within the target editing range based on the generated content.
[0066] After determining the precise target editing range, the system performs specific editing operations, using the generated content received in step 12 to update the existing text within that range.
[0067] The update process here can be a direct text replacement operation, which involves deleting the old text within the target range and inserting the newly generated text, or it can be an insertion, appending, or formatting rewriting operation performed according to business needs.
[0068] Before performing an update, in order to ensure the security of the operation, the system usually performs necessary preprocessing on the generated content, such as format conversion and security cleaning, and re-verifies the legality of the target editing range to ensure that the replacement operation will not cause the editor to crash or data to be corrupted.
[0069] To more intuitively illustrate the complete execution logic of this technical solution in practical applications, the following will combine... Figure 3 Provide a detailed description.
[0070] Figure 3 This is the second flowchart illustrating the text editing method provided by the present invention, as shown below. Figure 3 As shown, a complete process is provided from the user triggering the intelligent editing command to the final completion of the target editing range replacement, which includes, but is not limited to, the following implementation steps: First, the user selects a piece of text in the editor interface and presses the shortcut key "Cmd+L" or "Ctrl+L". The system executes three atomic operations simultaneously: (1) capturing the selection range {from, to}, adding visual highlight (Decoration), and storing the range information in the pending state object (pendingReplacement).
[0071] The system then sends a generation request (also known as an AI request) to the content generation model and enters a waiting state (usually lasting 0-60 seconds). During this time, the system continues to monitor the document's status.
[0072] Upon receiving the generated content from the content generation model, the system first performs format recognition. For example... Figure 3 As shown in the decision box, if the content is identified as Markdown, then the "Markdown→HTML conversion" and XSS security cleanup (DOMPurify) operations will be performed to ensure content security; if the content is plain text, the conversion will be skipped.
[0073] Furthermore, the system performs a three-priority replacement check to determine the target editing range in real time. Subsequently, the system also performs a range validity check (from / to / docSize) to ensure that the replacement coordinates do not exceed the limits.
[0074] If the verification passes, the system attempts to execute the main replacement process. If the main replacement process fails, the system will initiate a multi-level fallback replacement mechanism (Level 1 → Level 2 → Level 3), for example, downgrading to plain text insertion.
[0075] It should be noted that regardless of whether the replacement is successful or not, the system will eventually perform a forced cleanup (executed in the finally block), setting the pendingReplacement object to null and clearing the visual highlight, thus completing the editing task.
[0076] The text editing method provided by this invention cleverly utilizes delayed state objects to achieve state memory across time periods, and combines it with priority relationship strategies to intelligently solve the problem that user intent may change during asynchronous responses. It can flexibly adapt to new choices when users change their minds, and accurately fill in historical choices when users are waiting. It effectively avoids replacement misalignment or target loss caused by AI response delays, and significantly improves the fluency and reliability of intelligent assisted writing.
[0077] To further enhance the robustness of text editing and prevent program crashes caused by out-of-bounds replacement locations due to drastic changes in document content (such as when a user deletes a large amount of text while waiting), this embodiment introduces a strict boundary check mechanism after determining the target editing range and before performing update processing.
[0078] Figure 4 This is a schematic diagram of the execution flow of range validity verification and multi-level rollback provided in an embodiment of the present invention, as shown below. Figure 4 As shown, before updating the text within the target editing range based on the generated content, this embodiment may further include the following steps: Step 21: Obtain the total length value of the document in the editor.
[0079] At the moment the system is about to perform a replacement operation, it first retrieves the latest state of the current document. For example... Figure 4 As shown in the flowchart, the system can obtain the total length value (docSize) of the current document by calling the interface provided by the editor (such as editor.state.doc.content.size). The total length value represents the total number of characters or the total length of nodes contained in the document, and is a benchmark parameter for determining whether the target editing scope is valid.
[0080] Step 22: parse the start and end coordinates of the target editing range. That is, the system parses two key parameters from the already determined target editing range, which may be the range of real-time text content corresponding to the real-time selection state, or the position information of the initial text content recovered from the delayed state object: the start coordinate (from) and the end coordinate (to). These two coordinate values define the boundary of the target editing range.
[0081] Step 23: Determine whether the starting position coordinate is greater than or equal to zero, whether the ending position coordinate is less than or equal to the total length value, and whether the starting position coordinate is less than or equal to the ending position coordinate, and obtain the determination result.
[0082] like Figure 4 As shown, the system can perform three logical checks sequentially: Verification 1 (from>=0) checks whether the starting coordinates of the target editing range are non-negative. This is to prevent illegal indexes caused by calculation errors.
[0083] Verification 2 (to <= docSize) checks if the end position coordinates exceed the total length of the current document. If the user deletes content at the end of the document while waiting, causing the originally recorded to coordinates to become larger than the current docSize, direct manipulation without intervention will cause an error in the editor.
[0084] Verification 3 (from<=to): Checks if the starting position is less than or equal to the ending position to ensure the logical correctness of the target editing range and that there is no reverse selection.
[0085] The system will summarize the check results for the above three conditions, such as Figure 4 As shown, if any condition is not met (i.e., the process is judged as a failure), the process will switch to the scope invalidation handling branch, record a warning log (such as console.warn), and terminate the replacement operation, thereby protecting the integrity of the document.
[0086] If all the judgment results are determined to be yes, then the update processing steps are executed.
[0087] like Figure 4 As shown in the process, the system only considers the current target editing range safe and valid when all the above verification nodes pass. After all range verifications pass, the system continues to execute a multi-level rollback update strategy to ensure the final success rate of the operation: First, the system preferentially enters the "Level 1: Standard Replacement" process. In this process, a complete transaction chain is constructed, which includes focusing, deleting the old range, and inserting new content, and the transaction is attempted to be executed.
[0088] If Level 1 executes successfully, the process ends and enters the "finally" cleanup phase. If Level 1 fails, for example, due to parsing errors caused by complex HTML structures, the system catches the exception and automatically reverts to the Level 2: Simplified Insertion process. In this process, the system abandons complex rich text operations and only attempts to write the generated content to the document by calling the basic insert interface (insertContent) in plain text form, ensuring that the user can at least obtain the generated content.
[0089] If "Level 2" still fails, the system will enter the final "Level 3: Protection Mode", which will log errors and stop any modifications to prevent damage to the original content of the document.
[0090] Through this progressive execution strategy, the present invention ensures to the greatest extent possible that the content generated by the content generation model can be securely written into the document.
[0091] The text editing method provided by this invention constructs a security barrier before performing core editing operations, effectively intercepting all possible illegal operations and greatly enhancing the stability of the editor in complex interactive scenarios.
[0092] To further ensure the accuracy of text replacement and prevent extreme cases where the location coordinates are valid but the text content at that location has been completely modified by the user, this embodiment adds a content-based secondary verification mechanism on top of the range validity checks provided in the above embodiments. Specifically, after determining that all the judgment results are yes, the following steps are performed: Step 31: Read the text content of the editor within the target editing range.
[0093] Once the system confirms that the starting and ending coordinates (from / to) of the target editing range legally exist in the document, the system can further call the editor's data reading interface, such as editor.state.doc.text, Between(from, to), to extract the text content currently contained in the target editing range from the document.
[0094] Step 32: Perform the process of removing leading and trailing whitespace characters on the text content and the initial text content stored in the delayed state object, and compare whether the processed text content is consistent with the processed initial text content to obtain the comparison result.
[0095] The system can compare the read current text content with the initial text content stored when constructing the delayed state object in step 11 of the above embodiment.
[0096] Considering that users may unintentionally input spaces or newlines before or after the selected text while waiting, or that the selection itself may contain extra whitespace, a direct exact match might fail due to minor formatting differences. Therefore, this embodiment first removes leading and trailing whitespace from both text segments separately. In programming implementation, this can usually be done by calling the trim() function.
[0097] After removing leading and trailing whitespace, the system compares the two cleaned text contents to see if they are identical. If the text contents are the same, or satisfy a preset inclusion relationship (e.g., the current content contains the initial content), the content comparison result is determined to be consistent.
[0098] Step 33: If the comparison results are consistent, then perform the update process.
[0099] Only when the content comparison results are consistent can the system determine that the current cursor position determined by the user is not only valid in terms of coordinates, but also is indeed the text content that the user originally wanted to modify, and only then will the system continue to execute the subsequent update process.
[0100] If the comparison results are inconsistent, for example, if the user rewrites a completely different sentence in the original text content, the system will terminate the automatic replacement or prompt the user with a pop-up window: "The original content has been changed, please confirm whether to continue," thereby avoiding incorrect modifications.
[0101] The text editing method provided by this invention introduces this content-level deep verification, adding semantic anchoring on top of position anchoring, forming a double insurance and completely eliminating the risk of misoperation in asynchronous editing scenarios.
[0102] Based on the above embodiments, in order to further optimize the user experience, alleviate user anxiety during AI response waiting, and clearly identify the target editing area that will be modified by AI, this embodiment provides real-time visual feedback on the front-end interface while the system processes data in the background. Specifically, after constructing the delayed state object, this embodiment may further include: Step 41: Create a highlight decoration object based on the position information of the initial text content; Step 42: Render the highlighted decoration object to the view layer of the editor so that a preset visual marker is displayed in the editor for the area corresponding to the position information of the initial text content.
[0103] Figure 5 This is a flowchart illustrating the visual highlighting and document change synchronization mechanism provided by the present invention, as shown below. Figure 5 As shown, in state 1, the initial highlight creation phase, when the user presses a shortcut key, such as "Cmd+L" to trigger the smart editing command, the system not only constructs a deferred state object in the background, but also immediately intervenes in the editor plugin layer. Using the captured position information of the initial text content (e.g., from:22, to:32), it creates an inline highlight decoration object. This highlight decoration object can contain position attributes (from / to) and style attributes.
[0104] In the implementation, style properties can be defined as a specific CSS class name (such as class:"ai-chat-highlight") or a direct inline style (such as background-color:#FFF3CD, i.e., a semi-transparent yellow background). The created highlight decoration object is added to the editor's DecorationSet, and the ViewLayer is notified to redraw.
[0105] like Figure 5As shown in the interface, the user will immediately see the selected text area, such as "This is the text that needs to be replaced," which has been highlighted with a prominent background color or border. This preset visual marker acts as a placeholder, clearly informing the user that the system has locked this text and is processing it.
[0106] During the lengthy AI response time, users may continue editing other parts of the document. To ensure that the aforementioned visual markers always accurately cover the target text, and are not misaligned due to additions or deletions of document content (e.g., the highlight remains in place while the text has been moved), this embodiment introduces a dynamic mapping mechanism. Figure 5 As shown, in the automatic synchronization phase after document changes in state 2, after rendering the highlighted decoration object to the view layer of the editor, it may further include: In response to a detected document content change operation in the editor, the location mapping relationship associated with the document content change operation is obtained; The new position coordinates of the highlighted decoration object in the editor are calculated using the position mapping relationship; The rendering position of the highlighted decorative object in the view layer is updated based on the new position coordinates.
[0107] Specifically, the system starts listening to the editor's transaction flow. Whenever the user enters characters, deletes content, or pastes text in the editor, the editor kernel generates a document content change operation, which is usually encapsulated in a Transaction object.
[0108] Suppose a user inserts the text "
New Insert
[0109] The system calls a mapping method (such as the `map` function), taking the original coordinates of the highlighted decoration object (from: 22, to: 32) as input for calculation in the position mapping relationship, such as... Figure 5 As shown, the calculation results show that the new coordinates have been adjusted to from: 27, to: 37 (i.e., automatically +5).
[0110] The system updates the decoration set using the calculated new coordinates and refreshes the view layer. In this way, although the text position changes, the highlighted background remains precisely positioned over the words "This is the text that needs to be replaced," achieving perfect synchronization between the visuals and the content.
[0111] The visual highlighting and metadata synchronization mechanism adopted in this invention ensures that, throughout the entire asynchronous waiting period, regardless of how the document changes, the user can always intuitively see the locked operation object, greatly improving the certainty and smoothness of the interaction.
[0112] To enhance the editor's adaptability to diverse AI-generated content and achieve a rich text editing experience, this embodiment introduces an intelligent format recognition and conversion mechanism in the update processing stage. Optionally, the update processing of text within the target editing range based on the generated content specifically includes the following steps: Detect whether the generated content contains one or more of the following: code block markers, heading markers, or list markers; If so, the generated content will be converted into HTML format data; The HTML formatted data is rendered into the target editing area.
[0113] When the system receives the generated content returned by the content generation model, it does not directly insert it as plain text, but first performs format detection.
[0114] The system can use preset regular expressions or string matching algorithms to scan the generated content for the presence of Markdown syntax features.
[0115] Specifically, the system focuses on detecting the following types of features: (1) Code block markers: for example, three backticks "```"; (2) Title markers: such as hash symbols "#", "##", etc.; (3) List markers: such as hyphen "-", asterisk " Or a number followed by a period "1.", etc.; In addition, bold text can also be included (" Other Markdown features include ">", quotation marks (">", etc.
[0116] If any of the above-mentioned characteristic symbols are detected, the system can determine that the generated content is Markdown formatted text. At this point, by calling a built-in parser (such as the underlying algorithm of parsing libraries like markdownit and marked), the system parses the syntax structure of the generated content and converts it into HTML formatted data, for example, by converting "..." Key Points " converted to " <strong> Key Points< / strong> , convert "- list item" to " List item )".
[0117] If the above-mentioned characteristic symbols are not detected, the system will default the content to plain text and skip the conversion step.
[0118] After completing the above format conversion, the system calls the editor's rich text insertion interface (such as insertContent) to parse and render the generated HTML data into the target editing area of the document. Thus, what the user sees in the editor is no longer source code with various symbols, but rather beautifully formatted and clearly presented rich text content.
[0119] This invention achieves a seamless transition from the original AI output to the rich text display in the editor through the aforementioned format conversion, eliminating the need for users to manually adjust the format and significantly improving editing efficiency and document aesthetics.
[0120] In specific rich text editing scenarios, directly rendering externally input HTML data poses a high security risk, especially against cross-site scripting attacks. To ensure the security of the editor and prevent malicious code from being hidden in the generated content, this embodiment introduces a strict cleanup mechanism before rendering HTML data.
[0121] Specifically, rendering the HTML format data into the target editing area includes: Iterate through the HTML data, removing script tags, event handling attributes, and non-whitelisted URLs to generate cleaned HTML data. The purified HTML format data is rendered into the target editing area.
[0122] In this embodiment, after converting Markdown data to HTML data, the system calls a security purification module (e.g., an algorithm built based on security libraries such as DOMPurify) to perform deep cleaning on the HTML data.
[0123] Specifically, the system can parse and traverse the DOM tree structure of HTML formatted data and perform the following filtering operations: (1) Remove the script tags, including completely deleting all of them. <script>标签及其内部代码,防止恶意JavaScript脚本自动执行。
[0124] (2)移除事件处理属性,如剥离所有非安全的HTML属性,特别是以on开头的事件处理器(如onclick、onload、onerror 等),防止用户点击或加载内容时触发恶意行为。
[0125] (3)移除非白名单URL,如检查标签的href属性或标签的src属性,过滤掉javascript:等危险协议,仅保留http: / / 、https: / / 等白名单内的安全链接。
[0126] 经过上述处理,就可以得到一份完全净化后的HTML格式数据,该数据只包含安全的展示型标签(如p、strong、ul 等)。
[0127] 最后,系统将这份经过净化的HTML格式数据传递给编辑器内核,安全地将其渲染到文档的目标编辑范围内。
[0128] 本发明在实现富文本渲染便利性的同时,构建了坚固的安全防线,确保用户在享受AI辅助写作时,不会因引入恶意代码而导致隐私泄露或系统遭受攻击。
[0129] 图6是本发明提供的三优先级智能替换决策的流程示意图,如图6所示,系统会通过执行三优先级智能替换决策流程,精准锁定了待替换的目标编辑范围,具体实施流程包括:首先,系统执行优先级1检查,包括读取编辑器当前的实时选择状态。如果检测到用户当前选中了一段具体的文本内容(即选区非空,selection.empty 为false),则判定用户的最新意图是针对这段新选中的文本内容进行操作。因此,系统直接获取实时选择状态对应的实时文本内容对应的位置信息(from / to),将其确定为目标编辑范围。例如,用户在等待期间改变主意,手动选中了另一段文字内容,系统将优先将这段文字内容替换初始文本内容。
[0130] 若优先级1不满足,例如用户当前光标仅闪烁但未选中内容,系统进入优先级2检查。系统会检查之前所构建的延迟状态对象(pendingReplacement)是否存在。如果该延迟状态对象存在且包含有效的位置信息,系统则判定用户处于等待状态,意图仍是替换初始文本内容。因此,系统提取延迟状态对象中存储的范围信息(range),将其确定为目标编辑范围。
[0131] 若优先级2也不满足,例如延迟状态对象因异常丢失,系统则进入优先级3检查。此时系统无法明确目标编辑范围,为了安全起见,不执行任何替换操作,而是通过弹窗或气泡(Toast)向用户显示提示信息:"请先在编辑器中选中要替换的文本内容”。
[0132] 通过这套"实时优先优先级大于历史兜底优先级,历史兜底优先级又大于异常提示优先级的三级策略,系统能够智能适配用户的各种操作场景,确保替换行为始终符合用户的真实期望。
[0133] 在通过上述行三优先级智能替换决策确定了目标编辑范围之后,在实际的软件运行环境中,复杂的富文本操作可能会因为各种不可预见的原因,如浏览器兼容性问题、HTML结构嵌套过深、编辑器插件冲突等而失败。为了确保用户能够获取带AI的生成内容,本实施例进一步提供了一套多级回退容错机制。
[0134] 具体来说,所述基于所述生成内容对所述目标编辑范围内的文本进行更新处理,可以包括:执行第一替换操作,将所述生成内容以富文本格式插入所述目标编辑范围,并监测所述第一替换操作的执行结果;若监测到所述执行结果为异常或失败,则触发第二替换操作,所述第二替换操作包括将所述生成内容以纯文本格式插入所述目标编辑范围。
[0135] 系统首先尝试以最优效果进行更新,即执行第一替换操作,包括调用编辑器的富文本处理接口,如editor.chain().insertContentAt( ),尝试将AI生成的、保留了格式(如加粗、链接、列表)的文本内容插入到目标编辑范围中。
[0136] 在执行过程中,系统利用程序的异常处理机制(如try catch 语句)或事务回调状态,实时监测该操作的执行结果。若操作顺利完成,文档更新成功,则流程终止。
[0137] 如图4中从"Level 1”指向"Level 2:简化插入”的路径所示,一旦系统捕获到第一替换操作抛出错误,例如因HTML标签嵌套错误或插件冲突导致),系统立即启动容错机制,触发第二替换操作。
[0138] 在此阶段,系统自动降级处理标准,放弃富文本格式,而是将AI生成内容视为纯字符串,则会调用编辑器最底层的纯文本插入接口,将该内容写入目标范围。这确保了用户在任何异常情况下,至少能获得AI生成的文字信息,避免了因技术故障导致的数据丢失,极大提升了系统的健壮性和用户信任度。
[0139] 为了保障编辑器状态机的健康流转,防止因单次操作的异常残留导致后续交互功能失效,例如死锁或高亮无法消除,本实施例引入了生命周期管理的强制清理机制。
[0140] 本实施例提供的文本编辑方法,在基于所述生成内容对所述目标编辑范围内的文本内容进行更新处理之后,还可以包括以下步骤:在基于所述生成内容对所述目标编辑范围内的文本进行更新处理之后,将所述延迟状态对象重置为空值,并清除所述编辑器中与所述初始文本范围对应的视觉标记。
[0141] 如图4底部的"finally:强制清理”模块所示,无论上述的更新处理(包括Level 1和Level 2)是成功完成还是最终失败,系统都会进入一个必然执行的Finally Block代码块。
[0142] 在这一实施例中,系统会通过将全局存储的延迟状态对象pendingReplacement显式地赋值为null或初始空状态,以使得本次AI辅助编辑任务的生命周期彻底结束。如果忽略此步,下一次用户触发AI辅助编辑任务时,系统可能会错误地读取到上一次残留的旧状态对象,导致逻辑错乱。
[0143] 同步地,系统会向编辑器的视图层发送一个清理指令,例如action: 'clearHighlight',该指令通知装饰器插件移除之前为本次任务创建的高亮装饰对象。
[0144] 通过执行这一步骤,编辑器界面上的黄色高亮背景或边框会立即消失,界面恢复到洁净状态,等待用户的下一次操作。这不仅保证了界面的整洁,也释放了内存资源,确保了编辑器长期运行的性能和稳定性。
[0145] 图7是本发明提供的文本编辑装置的结构示意图,如图7所示,本发明还提供一种文本编辑装置,主要包括但不限于:请求处理模块1,响应于在编辑器中触发的智能编辑指令,构建延迟状态对象并向内容生成模型发送生成请求;所述延迟状态对象包括当前选中的初始文本内容以及所述初始文本内容的位置信息;内容接收模块2,接收所述内容生成模型基于所述生成请求输出的生成内容;范围确定模块3,获取所述编辑器当前的实时选择状态,基于预设的优先级策略,在所述实时选择状态对应的区域与所述延迟状态对象对应的区域之间择一确定目标编辑范围;更新处理模块4,基于所述生成内容对所述目标编辑范围内的文本内容进行更新处理。
[0146] 需要说明的是,本发明提供的文本编辑装置,在具体运行时,可以执行上述任一实施例所述的文本编辑方法,对此本实施例不作赘述。
[0147] 图8是本发明提供的文本编辑系统的架构示意图,为了更清晰地说明这些模块在实际系统中的协同关系,下面结合图8所示的系统架构进行详细阐述。
[0148] 请求处理模块1对应图8中用户交互层(User Interaction Layer)与核心逻辑层(Core Logic Layer)的交互部分,负责监听用户的快捷键操作(如"Cmd+L”),触发文本捕获模块构建并存储延迟状态对象,在图8中体现为Zustand 状态管理库中的editorStore,同时向AI服务发起请求。
[0149] 内容接收模块2对应图8中内容处理层(Content Processing Layer)的入口,主要负责接收内容生成模型返回的异步数据流。如图8所示,内容接收模块2还集成了格式识别模块和安全转换模块,能够自动检测Markdown特征并调用DOMPurify进行XSS清洗,为后续处理准备好安全的数据。
[0150] 范围确定模块3对应图8核心逻辑层中的三优先级智能替换决策模块,用于实时读取编辑器的状态(Selection)和存储的延迟状态对象(pendingReplacement),执行如图6所示的三优先级智能替换决策(记作P1 / P2 / P3),精准判定出目标编辑范围。同时,范围确定模块3还集成了范围校验模块,对判定出的目标编辑范围进行边界合法性检查。
[0151] 更新处理模块4对应图8核心逻辑层中的多级回退模块以及最底层的事务管理,主要用于接收来自范围确定模块的目标坐标和来自内容接收模块的处理后内容,构建编辑器事务(Transaction)。如图8所示,其支持从主流程到简化替换的多级回退策略,并通过ProseMirror等编辑器内核的API最终完成对文档内容的原子性更新。
[0152] 此外,如图8所示,本发明提供的文本编辑装置还可以包含视觉反馈模块,其横跨逻辑层与交互层利用装饰集(DecorationSet)和映射机制(Mapping),在整个处理过程中维护用户可见的高亮状态,并在完成更新处理后自动响应清理指令。
[0153] 通过这种分层解耦、模块协同的架构设计,本发明提供的文本编辑装置实现了高内聚、低耦合的智能文本编辑能力,具备极强的扩展性和稳定性。
[0154] 为了验证本发明实施例所提供的文本编辑方法及装置在实际应用场景中的有效性和优越性,下面结合具体的应用场景实施例、对比测试数据及有益效果分析进行进一步说明。
[0155] 场景一,基于智能编辑指令触发的文本更新完整流程。假设用户在编辑器中编辑文档。用户选中一段初始文本内容,例如:"这个功能的实现方式比较复杂...”,并触发智能编辑指令,文本编辑装置依次执行以下流程:(1)响应智能编辑指令,捕获该区域(假设位置信息为坐标156-189),并在视图层创建高亮装饰对象,同时将初始文本内容及其位置信息存储至延迟状态对象中。
[0156] (2)向内容生成模型发送生成请求,即在等待期间,高亮装饰对象会随文档视图保持显示。
[0157] (3)接收到模型返回的生成内容(例如Markdown格式文本),自动识别格式并将其转换为HTML数据,同时执行XSS安全净化。
[0158] (4)获取当前的实时选择状态,检测到其实时选择为空(即用户光标未选中新内容)。根据预设的优先级关系,调用延迟状态对象中的位置信息确定目标编辑范围。经范围有效性校验通过后,执行更新处理,将净化后的HTML数据插入该范围。
[0159] 场景二,用户操作意图变更场景下的优先级策略执行。假设用户选中初始文本内容A并触发智能编辑指令。在等待模型响应期间,用户变更操作意图,在文档另一处选中了文本内容B。
[0160] 当文本编辑装置接收到生成内容时,执行优先级判断:(1)检测到实时选择状态为非空选择(对应文本B的选区)。
[0161] (2)根据实时选择状态优先于延迟状态对象的策略,装置判定用户的最新意图为针对文本内容B进行编辑。
[0162] (3)将实时选择状态对应的区域确定为目标编辑范围,并利用生成内容对文本内容B进行更新处理,而非覆盖初始文本内容A。
[0163] 场景三,文档内容变更导致范围失效的保护处理。假设用户选中位于文档末尾的文本触发指令。在等待期间,用户执行了删除操作,导致文档总长度小于延迟状态对象中记录的结束位置坐标。
[0164] 当装置接收到生成内容时,执行范围有效性校验:(1)获取当前文档的总长度值。
[0165] (2)判断延迟状态对象中的结束位置坐标大于当前文档总长度值,判定目标编辑范围无效。
[0166] (3)终止更新处理,并触发异常提示,同时将延迟状态对象重置为空值。
[0167] 为了验证本发明实施例技术方案的效果,进一步构建了测试环境进行对比测试。测试对象包括采用本发明提供的文本编辑装置(实验组)与未采用延迟状态管理的传统装置(对照组)。
[0168] 1)更新处理准确性测试。
[0169] 在模拟内容生成模型响应延迟1-60秒的条件下,进行500次测试。
[0170] 实验组:利用持久化的延迟状态对象,更新处理的准确率保持在 100%。
[0171] 对照组:随着延迟时间增加,因实时选择状态丢失导致的更新失败率显著上升。结论:本发明有效解决了异步响应期间位置信息丢失的问题。
[0172] 2)意图匹配度测试。
[0173] 模拟用户在等待期间变更实时选择状态的场景。
[0174] 实验组:通过优先级关系判定,系统对用户操作意图的匹配准确率达到 95.6%。结论:本发明能够智能适配用户的动态操作意图。
[0175] 3)系统稳定性测试模拟目标编辑范围越界及长时间连续操作场景。
[0176] 实验组:无效范围拦截率 100%。引入强制状态清理机制后,系统长时间运行的稳定性从95%提升至 99.9%。
[0177] 综上所述,与现有技术相比,本发明提供的文本编辑方法、装置、电子设备及存储介质具有以下有益效果:(1)解决异步响应时差问题:通过构建持久化的延迟状态对象,即使在内容生成模型响应延迟较长的情况下,装置仍能准确回溯并定位初始文本内容的位置信息,消除了传统方案因时间差导致的目标丢失风险。
[0178] (2)智能适配用户意图:采用基于实时选择状态与延迟状态对象的优先级关系判定策略,能够灵活应对用户在等待期间的操作变化,确保编辑行为始终符合用户的最新意图。
[0179] (3)全方位的安全保障:引入了从范围有效性校验到内容一致性比对,再到XSS内容净化等多重安全机制,确保了更新处理的准确性与安全性。
[0180] (4)健壮的容错机制:通过多级回退策略(富文本至纯文本的降级处理)以及强制状态清理机制,确保装置在面对复杂格式或异常情况时保持稳定运行。
[0181] (5)优化的视觉交互体验:利用高亮装饰对象及其位置映射机制,实现了视觉标记随文档内容变更的自动同步,提升了用户体验。
[0182] 图9是本发明提供的电子设备的结构示意图,如图9所示,该电子设备可以包括:处理器(processor)910、通信接口(Communications Interface)920、存储器(memory)930和通信总线940,其中,处理器910,通信接口920,存储器930通过通信总线940完成相互间的通信。处理器910可以调用存储器930中的逻辑指令,以执行文本编辑方法,该方法包括:响应于在编辑器中触发的智能编辑指令,构建延迟状态对象并向内容生成模型发送生成请求;所述延迟状态对象包括当前选中的初始文本内容以及所述初始文本内容的位置信息;接收所述内容生成模型基于所述生成请求输出的生成内容;获取所述编辑器当前的实时选择状态,基于预设的优先级策略,在所述实时选择状态对应的区域与所述延迟状态对象对应的区域之间择一确定目标编辑范围;基于所述生成内容对所述目标编辑范围内的文本内容进行更新处理。
[0183] 此外,上述的存储器930中的逻辑指令可以通过软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本发明各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、磁碟或者光盘等各种可以存储程序代码的介质。
[0184] 另一方面,本发明还提供一种计算机程序产品,所述计算机程序产品包括存储在非暂态计算机可读存储介质上的计算机程序,所述计算机程序包括程序指令,当所述程序指令被计算机执行时,计算机能够执行上述各实施例所提供的文本编辑方法,该方法包括:响应于在编辑器中触发的智能编辑指令,构建延迟状态对象并向内容生成模型发送生成请求;所述延迟状态对象包括当前选中的初始文本内容以及所述初始文本内容的位置信息;接收所述内容生成模型基于所述生成请求输出的生成内容;获取所述编辑器当前的实时选择状态,基于预设的优先级策略,在所述实时选择状态对应的区域与所述延迟状态对象对应的区域之间择一确定目标编辑范围;基于所述生成内容对所述目标编辑范围内的文本内容进行更新处理。
[0185] 又一方面,本发明还提供一种非暂态计算机可读存储介质,其上存储有计算机程序,该计算机程序被处理器执行时实现以执行上述各实施例提供的文本编辑方法,该方法包括:响应于在编辑器中触发的智能编辑指令,构建延迟状态对象并向内容生成模型发送生成请求;所述延迟状态对象包括当前选中的初始文本内容以及所述初始文本内容的位置信息;接收所述内容生成模型基于所述生成请求输出的生成内容;获取所述编辑器当前的实时选择状态,基于预设的优先级策略,在所述实时选择状态对应的区域与所述延迟状态对象对应的区域之间择一确定目标编辑范围;基于所述生成内容对所述目标编辑范围内的文本内容进行更新处理。
[0186] 以上所描述的装置实施例仅仅是示意性的,其中所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部模块来实现本实施例方案的目的。本领域普通技术人员在不付出创造性的劳动的情况下,即可以理解并实施。
[0187] 通过以上的实施方式的描述,本领域的技术人员可以清楚地了解到各实施方式可借助软件加必需的通用硬件平台的方式来实现,当然也可以通过硬件。基于这样的理解,上述技术方案本质上或者说对现有技术做出贡献的部分可以以软件产品的形式体现出来,该计算机软件产品可以存储在计算机可读存储介质中,如ROM / RAM、磁碟、光盘等,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行各个实施例或者实施例的某些部分所述的方法。
[0188] 最后应说明的是:以上实施例仅用以说明本发明的技术方案,而非对其限制;尽管参照前述实施例对本发明进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本发明各实施例技术方案的精神和范围。< / script>
Claims
1. A text editing method, characterized in that, include: In response to smart editing commands triggered in the editor, construct a deferred state object and send a generation request to the content generation model; The delayed state object includes the currently selected initial text content and the position information of the initial text content; Receive the generated content output by the content generation model based on the generation request; The current real-time selection state of the editor is obtained, and the target editing range is determined by selecting one between the area corresponding to the real-time selection state and the area corresponding to the delayed state object based on a preset priority strategy. The text content within the target editing range is updated based on the generated content.
2. The text editing method according to claim 1, characterized in that, The method of determining the target editing range based on a preset priority strategy, choosing between the region corresponding to the real-time selection state and the region corresponding to the delayed state object, includes: Detect whether the real-time selection state is a non-empty selection; If the real-time selection state is detected to be a non-empty selection, the target editing range is determined based on the real-time text content corresponding to the real-time selection state. If the real-time selection state is detected as an empty selection, the position information of the initial text content in the delayed state object is used to determine the target editing range.
3. The text editing method according to claim 1, characterized in that, Before updating the text within the target editing range based on the generated content, the process further includes: Get the total length value of the document in the editor; Analyze the start and end coordinates of the target editing area; Determine whether the starting position coordinate is greater than or equal to zero, whether the ending position coordinate is less than or equal to the total length value, and whether the starting position coordinate is less than or equal to the ending position coordinate, and obtain the determination result; If all the judgment results are determined to be yes, then the update processing steps are executed.
4. The text editing method according to claim 3, characterized in that, After determining that all the judgment results are yes, the process also includes: Read the text content of the editor within the target editing area; Perform the process of removing leading and trailing whitespace characters on the text content and the initial text content stored in the delayed state object respectively, and compare whether the processed text content is consistent with the processed initial text content to obtain the comparison result; If the comparison results are consistent, then the update process is executed.
5. The text editing method according to claim 1, characterized in that, After constructing the delayed state object, the following is also included: Create a highlight decoration object based on the position information of the initial text content; The highlighted decorative object is rendered to the view layer of the editor so that a preset visual marker is displayed in the editor for the area corresponding to the position information of the initial text content.
6. The text editing method according to claim 5, characterized in that, After rendering the highlighted decoration object to the view layer of the editor, the process also includes: In response to a detected document content change operation in the editor, the location mapping relationship associated with the document content change operation is obtained; The new position coordinates of the highlighted decoration object in the editor are calculated using the position mapping relationship; The rendering position of the highlighted decorative object in the view layer is updated based on the new position coordinates.
7. The text editing method according to claim 1, characterized in that, The step of updating the text within the target editing range based on the generated content includes: Detect whether the generated content contains one or more of the following: code block markers, heading markers, or list markers; If so, the generated content will be converted into HTML format data; The HTML formatted data is rendered into the target editing area.
8. The text editing method according to claim 7, characterized in that, The step of rendering the HTML format data to the target editing area includes: Iterate through the HTML data, removing script tags, event handling attributes, and non-whitelisted URLs to generate cleaned HTML data. The purified HTML format data is rendered into the target editing area.
9. The text editing method according to claim 1, characterized in that, The step of updating the text within the target editing range based on the generated content includes: Perform a first replacement operation, insert the generated content into the target editing area in rich text format, and monitor the execution result of the first replacement operation; If the execution result is detected to be abnormal or failed, a second replacement operation is triggered, which includes inserting the generated content into the target editing range in plain text format.
10. The text editing method according to claim 1, characterized in that, After updating the text content within the target editing range based on the generated content, the process further includes: The delayed state object is reset to null, and the visual markers corresponding to the initial text range in the editor are cleared.
11. A text editing device, characterized in that, include: The request processing module, in response to smart editing commands triggered in the editor, constructs a deferred state object and sends a generation request to the content generation model; The delayed state object includes the currently selected initial text content and the position information of the initial text content; The content receiving module receives the generated content output by the content generation model based on the generation request; The range determination module obtains the current real-time selection state of the editor and, based on a preset priority strategy, determines the target editing range between the area corresponding to the real-time selection state and the area corresponding to the delayed state object. The update processing module updates the text content within the target editing range based on the generated content.
12. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the text editing method as described in any one of claims 1 to 10.
13. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the text editing method as described in any one of claims 1 to 10.
14. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the text editing method as described in any one of claims 1 to 10.