Skip to content

Commit 1d64325

Browse files
jackfranklinDevtools-frontend LUCI CQ
authored andcommitted
RPP: improve keyboard experience of annotations
This CL makes it so that when you tab into the annotation, you are immediately placed into edit mode, and the cursor is put at the end of the annotation. This *sort of* worked for clicking with the mouse, but with the keyboard you have to do a bit more work to get the cursor to display at the right point. Because by default it will not appear. Fixed: 406549702 Change-Id: I328d70763a09fc60a0c4258586fc97ba3d2f5658 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6401393 Commit-Queue: Jack Franklin <[email protected]> Auto-Submit: Jack Franklin <[email protected]> Reviewed-by: Alina Varkki <[email protected]>
1 parent 9d8351c commit 1d64325

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

front_end/panels/timeline/overlays/components/EntryLabelOverlay.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ export class EntryLabelOverlay extends HTMLElement {
261261
// We do not want to create multi-line labels.
262262
// Therefore, if the new key is `Enter` key, treat it
263263
// as the end of the label input and blur the input field.
264-
if (event.key === Platform.KeyboardUtilities.ENTER_KEY || event.key === Platform.KeyboardUtilities.ESCAPE_KEY) {
264+
if ((event.key === Platform.KeyboardUtilities.ENTER_KEY || event.key === Platform.KeyboardUtilities.ESCAPE_KEY) &&
265+
this.#isLabelEditable) {
265266
// Note that we do not stop the event propagating here; this is on
266267
// purpose because we need it to bubble up into TimelineFlameChartView's
267268
// handler. That updates the state and deals with the keydown.
@@ -276,7 +277,6 @@ export class EntryLabelOverlay extends HTMLElement {
276277
// safe to call this just in case the blur() didn't actually trigger.
277278
this.#inputField.blur();
278279
this.setLabelEditabilityAndRemoveEmptyLabel(false);
279-
280280
return false;
281281
}
282282

@@ -307,19 +307,10 @@ export class EntryLabelOverlay extends HTMLElement {
307307
}
308308

309309
const pastedText = clipboardData.getData('text');
310-
311310
const newText = this.#inputField.textContent + pastedText;
312311
const trimmedText = newText.slice(0, EntryLabelOverlay.MAX_LABEL_LENGTH + 1);
313-
314312
this.#inputField.textContent = trimmedText;
315-
316-
// Reset the selection to the end
317-
const selection = window.getSelection();
318-
const range = document.createRange();
319-
range.selectNodeContents(this.#inputField);
320-
range.collapse(false);
321-
selection?.removeAllRanges();
322-
selection?.addRange(range);
313+
this.#placeCursorAtInputEnd();
323314
}
324315

325316
set entryLabelVisibleHeight(entryLabelVisibleHeight: number) {
@@ -449,8 +440,9 @@ export class EntryLabelOverlay extends HTMLElement {
449440
}
450441
this.#isLabelEditable = editable;
451442
this.#render();
452-
// If the label is editable, focus cursor on it
453-
if (editable) {
443+
// If the label is editable, focus cursor on it & put the cursor at the end
444+
if (editable && this.#inputField) {
445+
this.#placeCursorAtInputEnd();
454446
this.#focusInputBox();
455447
}
456448
// On MacOS when clearing the input box it is left with a new line, so we
@@ -463,6 +455,23 @@ export class EntryLabelOverlay extends HTMLElement {
463455
}
464456
}
465457

458+
/**
459+
* Places the user's cursor at the end of the input. We do this when the user
460+
* focuses the input with either the keyboard or mouse, and when they paste in
461+
* text, so that the cursor is placed in a useful position to edit.
462+
*/
463+
#placeCursorAtInputEnd(): void {
464+
if (!this.#inputField) {
465+
return;
466+
}
467+
const selection = window.getSelection();
468+
const range = document.createRange();
469+
range.selectNodeContents(this.#inputField);
470+
range.collapse(false);
471+
selection?.removeAllRanges();
472+
selection?.addRange(range);
473+
}
474+
466475
set callTree(callTree: Utils.AICallTree.AICallTree|null) {
467476
this.#callTree = callTree;
468477
}
@@ -706,7 +715,12 @@ export class EntryLabelOverlay extends HTMLElement {
706715
<span
707716
class="input-field"
708717
role="textbox"
709-
@dblclick=${() => this.setLabelEditabilityAndRemoveEmptyLabel(true)}
718+
@focus=${() => {
719+
this.setLabelEditabilityAndRemoveEmptyLabel(true);
720+
}}
721+
@dblclick=${() => {
722+
this.setLabelEditabilityAndRemoveEmptyLabel(true);
723+
}}
710724
@blur=${() => this.setLabelEditabilityAndRemoveEmptyLabel(false)}
711725
@keydown=${this.#handleLabelInputKeyDown}
712726
@paste=${this.#handleLabelInputPaste}
@@ -718,6 +732,7 @@ export class EntryLabelOverlay extends HTMLElement {
718732
}}
719733
contenteditable=${this.#isLabelEditable ? 'plaintext-only' : false}
720734
jslog=${VisualLogging.textField('timeline.annotations.entry-label-input').track({keydown: true, click: true})}
735+
tabindex="0"
721736
></span>
722737
${(() => {
723738
switch (this.#shouldRenderAIButton()) {

0 commit comments

Comments
 (0)