|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 |
| -import { getClientArea, getTopLeftOffset } from '../../../../base/browser/dom.js'; |
| 6 | +import { getClientArea, getTopLeftOffset, isHTMLDivElement, isHTMLTextAreaElement } from '../../../../base/browser/dom.js'; |
7 | 7 | import { mainWindow } from '../../../../base/browser/window.js';
|
8 | 8 | import { coalesce } from '../../../../base/common/arrays.js';
|
9 | 9 | import { language, locale } from '../../../../base/common/platform.js';
|
@@ -133,18 +133,54 @@ export class BrowserWindowDriver implements IWindowDriver {
|
133 | 133 | if (!element) {
|
134 | 134 | throw new Error(`Editor not found: ${selector}`);
|
135 | 135 | }
|
| 136 | + if (isHTMLDivElement(element)) { |
| 137 | + // Edit context is enabled |
| 138 | + const editContext = element.editContext; |
| 139 | + if (!editContext) { |
| 140 | + throw new Error(`Edit context not found: ${selector}`); |
| 141 | + } |
| 142 | + const selectionStart = editContext.selectionStart; |
| 143 | + const selectionEnd = editContext.selectionEnd; |
| 144 | + const event = new TextUpdateEvent('textupdate', { |
| 145 | + updateRangeStart: selectionStart, |
| 146 | + updateRangeEnd: selectionEnd, |
| 147 | + text, |
| 148 | + selectionStart: selectionStart + text.length, |
| 149 | + selectionEnd: selectionStart + text.length, |
| 150 | + compositionStart: 0, |
| 151 | + compositionEnd: 0 |
| 152 | + }); |
| 153 | + editContext.dispatchEvent(event); |
| 154 | + } else if (isHTMLTextAreaElement(element)) { |
| 155 | + const start = element.selectionStart; |
| 156 | + const newStart = start + text.length; |
| 157 | + const value = element.value; |
| 158 | + const newValue = value.substr(0, start) + text + value.substr(start); |
| 159 | + |
| 160 | + element.value = newValue; |
| 161 | + element.setSelectionRange(newStart, newStart); |
| 162 | + |
| 163 | + const event = new Event('input', { 'bubbles': true, 'cancelable': true }); |
| 164 | + element.dispatchEvent(event); |
| 165 | + } |
| 166 | + } |
136 | 167 |
|
137 |
| - const textarea = element as HTMLTextAreaElement; |
138 |
| - const start = textarea.selectionStart; |
139 |
| - const newStart = start + text.length; |
140 |
| - const value = textarea.value; |
141 |
| - const newValue = value.substr(0, start) + text + value.substr(start); |
142 |
| - |
143 |
| - textarea.value = newValue; |
144 |
| - textarea.setSelectionRange(newStart, newStart); |
145 |
| - |
146 |
| - const event = new Event('input', { 'bubbles': true, 'cancelable': true }); |
147 |
| - textarea.dispatchEvent(event); |
| 168 | + async getEditorSelection(selector: string): Promise<{ selectionStart: number; selectionEnd: number }> { |
| 169 | + const element = mainWindow.document.querySelector(selector); |
| 170 | + if (!element) { |
| 171 | + throw new Error(`Editor not found: ${selector}`); |
| 172 | + } |
| 173 | + if (isHTMLDivElement(element)) { |
| 174 | + const editContext = element.editContext; |
| 175 | + if (!editContext) { |
| 176 | + throw new Error(`Edit context not found: ${selector}`); |
| 177 | + } |
| 178 | + return { selectionStart: editContext.selectionStart, selectionEnd: editContext.selectionEnd }; |
| 179 | + } else if (isHTMLTextAreaElement(element)) { |
| 180 | + return { selectionStart: element.selectionStart, selectionEnd: element.selectionEnd }; |
| 181 | + } else { |
| 182 | + throw new Error(`Unknown type of element: ${selector}`); |
| 183 | + } |
148 | 184 | }
|
149 | 185 |
|
150 | 186 | async getTerminalBuffer(selector: string): Promise<string[]> {
|
|
0 commit comments