* 设置GPT回答区域文字 * @param text * @param isDone
(text: string, isDone: boolean = false, scrollToNewLine: boolean = true, isRecord: boolean = true,)
| 144 | * @param isDone |
| 145 | */ |
| 146 | public setText(text: string, isDone: boolean = false, scrollToNewLine: boolean = true, isRecord: boolean = true,) { |
| 147 | this.outputContainer.style.display = "" |
| 148 | const outputDiv = this.outputContainer.querySelector(".markdown-body")! as HTMLDivElement |
| 149 | outputDiv.setAttribute("pureText", text); |
| 150 | outputDiv.classList.add("streaming"); |
| 151 | let ready = () => { |
| 152 | if (outputDiv.innerHTML.trim() == "") { |
| 153 | outputDiv.innerHTML = `<p></p>` |
| 154 | } |
| 155 | } |
| 156 | ready() |
| 157 | /** |
| 158 | * 根据差异渲染,只为保全光标闪烁 |
| 159 | */ |
| 160 | let md2html = () => { |
| 161 | let result = markdown.render(text) |
| 162 | // .replace(/<mjx-assistive-mml[^>]*>.*?<\/mjx-assistive-mml>/g, "") |
| 163 | /** |
| 164 | * 监测差异,替换节点或文字 |
| 165 | * @param oldNode |
| 166 | * @param newNode |
| 167 | * @returns |
| 168 | */ |
| 169 | let diffRender = (oldNode: any, newNode: any) => { |
| 170 | if (newNode.nodeName == "svg") { |
| 171 | oldNode.parentNode.replaceChild(newNode, oldNode) |
| 172 | return |
| 173 | } |
| 174 | if (oldNode.nodeName == "#text" && newNode.nodeName == "#text") { |
| 175 | oldNode.data = newNode.data |
| 176 | return |
| 177 | } else { |
| 178 | if ( |
| 179 | oldNode.outerHTML == newNode.outerHTML && |
| 180 | oldNode.innerHTML == newNode.innerHTML |
| 181 | ) { |
| 182 | return |
| 183 | } |
| 184 | } |
| 185 | // 老的比新的多要去除 |
| 186 | [...oldNode.childNodes].slice(newNode.childNodes.length).forEach((e: any)=>e.remove()) |
| 187 | for (let i = 0; i < newNode.childNodes.length; i++) { |
| 188 | if (i < oldNode.childNodes.length) { |
| 189 | if (oldNode.childNodes[i].tagName != newNode.childNodes[i].tagName) { |
| 190 | if (oldNode.childNodes[i].tagName == "#text") { |
| 191 | oldNode.childNodes[i].remove() |
| 192 | oldNode.appendChild(newNode.childNodes[i]) |
| 193 | } else { |
| 194 | oldNode.replaceChild(newNode.childNodes[i], oldNode.childNodes[i]) |
| 195 | } |
| 196 | continue |
| 197 | } else { |
| 198 | diffRender(oldNode.childNodes[i], newNode.childNodes[i]) |
| 199 | } |
| 200 | } else { |
| 201 | oldNode.appendChild(newNode.childNodes[i]) |
| 202 | } |
| 203 | } |
no test coverage detected