Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/ui/TextPrompt.js

Issue 2439223002: DevTools: Use grey hint text for applied suggestion in TextPrompt (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/http/tests/inspector-unit/text-prompt-hint-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 27 matching lines...) Expand all
38 { 38 {
39 /** 39 /**
40 * @type {!Element|undefined} 40 * @type {!Element|undefined}
41 */ 41 */
42 this._proxyElement; 42 this._proxyElement;
43 this._proxyElementDisplay = "inline-block"; 43 this._proxyElementDisplay = "inline-block";
44 this._loadCompletions = completions; 44 this._loadCompletions = completions;
45 this._completionStopCharacters = stopCharacters || " =:[({;,!+-*/&|^<>."; 45 this._completionStopCharacters = stopCharacters || " =:[({;,!+-*/&|^<>.";
46 this._autocompletionTimeout = WebInspector.TextPrompt.DefaultAutocompletionT imeout; 46 this._autocompletionTimeout = WebInspector.TextPrompt.DefaultAutocompletionT imeout;
47 this._title = ""; 47 this._title = "";
48 this._userEnteredRange = null;
48 this._previousText = ""; 49 this._previousText = "";
49 this._currentHintText = ""; 50 this._currentHintText = "";
50 this._completionRequestId = 0; 51 this._completionRequestId = 0;
52 this._autocompleteElement = createElementWithClass("span", "auto-complete-te xt");
51 } 53 }
52 54
53 WebInspector.TextPrompt.DefaultAutocompletionTimeout = 250; 55 WebInspector.TextPrompt.DefaultAutocompletionTimeout = 250;
54 56
55 /** @enum {symbol} */ 57 /** @enum {symbol} */
56 WebInspector.TextPrompt.Events = { 58 WebInspector.TextPrompt.Events = {
57 ItemApplied: Symbol("text-prompt-item-applied"), 59 ItemApplied: Symbol("text-prompt-item-applied"),
58 ItemAccepted: Symbol("text-prompt-item-accepted") 60 ItemAccepted: Symbol("text-prompt-item-accepted")
59 }; 61 };
60 62
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 this._proxyElement.appendChild(element); 135 this._proxyElement.appendChild(element);
134 this._element.classList.add("text-prompt"); 136 this._element.classList.add("text-prompt");
135 this._element.addEventListener("keydown", this._boundOnKeyDown, false); 137 this._element.addEventListener("keydown", this._boundOnKeyDown, false);
136 this._element.addEventListener("input", this._boundOnInput, false); 138 this._element.addEventListener("input", this._boundOnInput, false);
137 this._element.addEventListener("mousewheel", this._boundOnMouseWheel, fa lse); 139 this._element.addEventListener("mousewheel", this._boundOnMouseWheel, fa lse);
138 this._element.addEventListener("selectstart", this._boundSelectStart, fa lse); 140 this._element.addEventListener("selectstart", this._boundSelectStart, fa lse);
139 this._element.addEventListener("blur", this._boundClearAutocomplete, fal se); 141 this._element.addEventListener("blur", this._boundClearAutocomplete, fal se);
140 this._element.ownerDocument.defaultView.addEventListener("resize", this. _boundClearAutocomplete, false); 142 this._element.ownerDocument.defaultView.addEventListener("resize", this. _boundClearAutocomplete, false);
141 143
142 if (this._suggestBoxEnabled) 144 if (this._suggestBoxEnabled)
143 this._suggestBox = new WebInspector.SuggestBox(this); 145 this._suggestBox = new WebInspector.SuggestBox(this, 20, true);
144 146
145 if (this._title) 147 if (this._title)
146 this._proxyElement.title = this._title; 148 this._proxyElement.title = this._title;
147 149
148 return this._proxyElement; 150 return this._proxyElement;
149 }, 151 },
150 152
151 detach: function() 153 detach: function()
152 { 154 {
153 this._removeFromElement(); 155 this._removeFromElement();
(...skipping 11 matching lines...) Expand all
165 { 167 {
166 return this._element.textContent; 168 return this._element.textContent;
167 }, 169 },
168 170
169 /** 171 /**
170 * @return {string} 172 * @return {string}
171 */ 173 */
172 userEnteredText: function() 174 userEnteredText: function()
173 { 175 {
174 var text = this.text(); 176 var text = this.text();
175 if (this._autocompleteElement) { 177 if (this._autocompleteElement.parentNode) {
176 var addition = this._autocompleteElement.textContent; 178 var addition = this._autocompleteElement.textContent;
177 text = text.substring(0, text.length - addition.length); 179 text = text.substring(0, text.length - addition.length);
178 } 180 }
179 return text; 181 return text;
180 }, 182 },
181 183
182 /** 184 /**
183 * @param {string} x 185 * @param {string} x
184 */ 186 */
185 setText: function(x) 187 setText: function(x)
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 delete this._selectionTimeout; 275 delete this._selectionTimeout;
274 if (!this.isCaretInsidePrompt() && this._element.isComponentSelectio nCollapsed()) { 276 if (!this.isCaretInsidePrompt() && this._element.isComponentSelectio nCollapsed()) {
275 this.moveCaretToEndOfPrompt(); 277 this.moveCaretToEndOfPrompt();
276 this.autoCompleteSoon(); 278 this.autoCompleteSoon();
277 } 279 }
278 } 280 }
279 281
280 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100); 282 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100);
281 }, 283 },
282 284
283
284 /** 285 /**
285 * @param {!Event} event 286 * @param {!Event} event
286 */ 287 */
287 onMouseWheel: function(event) 288 onMouseWheel: function(event)
288 { 289 {
289 // Subclasses can implement. 290 // Subclasses can implement.
290 }, 291 },
291 292
292 /** 293 /**
293 * @param {!Event} event 294 * @param {!Event} event
294 */ 295 */
295 onKeyDown: function(event) 296 onKeyDown: function(event)
296 { 297 {
297 if (isEnterKey(event))
298 return;
299
300 var handled = false; 298 var handled = false;
301 299
302 switch (event.key) { 300 switch (event.key) {
303 case "Tab": 301 case "Tab":
304 handled = this.tabKeyPressed(event); 302 handled = this.tabKeyPressed(event);
305 break; 303 break;
306 case "ArrowLeft": 304 case "ArrowLeft":
307 case "Home": 305 case "Home":
308 this.clearAutocomplete(); 306 this.clearAutocomplete();
309 break; 307 break;
(...skipping 29 matching lines...) Expand all
339 if (handled) 337 if (handled)
340 event.consume(true); 338 event.consume(true);
341 }, 339 },
342 340
343 /** 341 /**
344 * @param {!Event} event 342 * @param {!Event} event
345 */ 343 */
346 onInput: function(event) 344 onInput: function(event)
347 { 345 {
348 var text = this.userEnteredText(); 346 var text = this.userEnteredText();
349 var hasCommonPrefix = text.startsWith(this._previousText) || this._previ ousText.startsWith(text); 347 if (text.startsWith(this._previousText) || this._previousText.startsWith (text))
350 if (this._autocompleteElement && hasCommonPrefix)
351 this._autocompleteElement.textContent = this._currentHintText.substr ing(text.length); 348 this._autocompleteElement.textContent = this._currentHintText.substr ing(text.length);
einbinder 2016/10/21 23:52:20 broken
352 else 349 else
353 this._clearAutocompleteElement(); 350 this._clearAutocompleteElement();
354 this._previousText = text; 351 this._previousText = text;
355 352
356 this.autoCompleteSoon(); 353 this.autoCompleteSoon();
357 }, 354 },
358 355
359 /** 356 /**
360 * @return {boolean} 357 * @return {boolean}
361 */ 358 */
(...skipping 12 matching lines...) Expand all
374 { 371 {
375 if (this.isSuggestBoxVisible()) 372 if (this.isSuggestBoxVisible())
376 this._suggestBox.hide(); 373 this._suggestBox.hide();
377 this._clearAutocompleteElement(); 374 this._clearAutocompleteElement();
378 }, 375 },
379 376
380 _clearAutocompleteElement: function() 377 _clearAutocompleteElement: function()
381 { 378 {
382 this._clearAutocompleteTimeout(); 379 this._clearAutocompleteTimeout();
383 380
384 if (!this._autocompleteElement) 381 this._autocompleteElement.remove();
385 return; 382 this._userEnteredRange = null;
383 },
386 384
387 this._autocompleteElement.remove(); 385 /**
388 delete this._autocompleteElement; 386 * @param {string} text
389 delete this._userEnteredRange; 387 */
390 delete this._userEnteredText; 388 _setAutocompleteText: function(text)
389 {
390 if (this.isCaretAtEndOfPrompt()) {
391 this._autocompleteElement.textContent = text.substring(this._userEnt eredRange.toString().length);
392 this._element.appendChild(this._autocompleteElement);
393 }
394 this._currentHintText = text;
391 }, 395 },
392 396
393 _clearAutocompleteTimeout: function() 397 _clearAutocompleteTimeout: function()
394 { 398 {
395 if (this._completeTimeout) { 399 if (this._completeTimeout) {
396 clearTimeout(this._completeTimeout); 400 clearTimeout(this._completeTimeout);
397 delete this._completeTimeout; 401 delete this._completeTimeout;
398 } 402 }
399 this._completionRequestId++; 403 this._completionRequestId++;
400 }, 404 },
(...skipping 22 matching lines...) Expand all
423 427
424 var shouldExit; 428 var shouldExit;
425 429
426 if (!force && !this.isCaretAtEndOfPrompt() && !this.isSuggestBoxVisible( )) 430 if (!force && !this.isCaretAtEndOfPrompt() && !this.isSuggestBoxVisible( ))
427 shouldExit = true; 431 shouldExit = true;
428 else if (!selection.isCollapsed) 432 else if (!selection.isCollapsed)
429 shouldExit = true; 433 shouldExit = true;
430 else if (!force) { 434 else if (!force) {
431 // BUG72018: Do not show suggest box if caret is followed by a non-s top character. 435 // BUG72018: Do not show suggest box if caret is followed by a non-s top character.
432 var wordSuffixRange = selectionRange.startContainer.rangeOfWord(sele ctionRange.endOffset, this._completionStopCharacters, this._element, "forward"); 436 var wordSuffixRange = selectionRange.startContainer.rangeOfWord(sele ctionRange.endOffset, this._completionStopCharacters, this._element, "forward");
433 var autocompleteTextLength = (this._autocompleteElement && this._aut ocompleteElement.parentNode) ? this._autocompleteElement.textContent.length : 0; 437 var autocompleteTextLength = (this._autocompleteElement.parentNode) ? this._autocompleteElement.textContent.length : 0;
434 if (wordSuffixRange.toString().length !== autocompleteTextLength) 438 if (wordSuffixRange.toString().length !== autocompleteTextLength)
435 shouldExit = true; 439 shouldExit = true;
436 } 440 }
437 if (shouldExit) { 441 if (shouldExit) {
438 this.clearAutocomplete(); 442 this.clearAutocomplete();
439 return; 443 return;
440 } 444 }
441 445
442 var wordPrefixRange = selectionRange.startContainer.rangeOfWord(selectio nRange.startOffset, this._completionStopCharacters, this._element, "backward"); 446 var wordPrefixRange = selectionRange.startContainer.rangeOfWord(selectio nRange.startOffset, this._completionStopCharacters, this._element, "backward");
443 this._loadCompletions(/** @type {!Element} */ (this._proxyElement), word PrefixRange, force || false, this._completionsReady.bind(this, ++this._completio nRequestId, selection, wordPrefixRange, !!reverse, !!force)); 447 this._loadCompletions(/** @type {!Element} */ (this._proxyElement), word PrefixRange, force || false, this._completionsReady.bind(this, ++this._completio nRequestId, selection, wordPrefixRange, !!reverse, !!force));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 524
521 var fullWordRange = this._createRange(); 525 var fullWordRange = this._createRange();
522 fullWordRange.setStart(originalWordPrefixRange.startContainer, originalW ordPrefixRange.startOffset); 526 fullWordRange.setStart(originalWordPrefixRange.startContainer, originalW ordPrefixRange.startOffset);
523 fullWordRange.setEnd(selectionRange.endContainer, selectionRange.endOffs et); 527 fullWordRange.setEnd(selectionRange.endContainer, selectionRange.endOffs et);
524 528
525 if (prefix + selectionRange.toString() !== fullWordRange.toString()) 529 if (prefix + selectionRange.toString() !== fullWordRange.toString())
526 return; 530 return;
527 531
528 selectedIndex = (this._disableDefaultSuggestionForEmptyInput && !this.te xt()) ? -1 : (selectedIndex || 0); 532 selectedIndex = (this._disableDefaultSuggestionForEmptyInput && !this.te xt()) ? -1 : (selectedIndex || 0);
529 533
530 this._userEnteredRange = fullWordRange;
531 this._userEnteredText = fullWordRange.toString();
532
533 if (this._suggestBox) 534 if (this._suggestBox)
534 this._suggestBox.updateSuggestions(this._boxForAnchorAtStart(selecti on, fullWordRange), annotatedCompletions, selectedIndex, !this.isCaretAtEndOfPro mpt(), this._userEnteredText); 535 this._suggestBox.updateSuggestions(this._boxForAnchorAtStart(selecti on, fullWordRange), annotatedCompletions, selectedIndex, !this.isCaretAtEndOfPro mpt(), this.userEnteredText());
535 536
536 if (selectedIndex === -1) 537 if (selectedIndex === -1)
537 return; 538 return;
538 539 this._userEnteredRange = fullWordRange;
539 var wordPrefixLength = originalWordPrefixRange.toString().length; 540 this.applySuggestion(annotatedCompletions[selectedIndex].title, true);
540
541 if (this.isCaretAtEndOfPrompt()) {
542 var completionText = annotatedCompletions[selectedIndex].title;
543 var prefixText = this._userEnteredRange.toString();
544 var suffixText = completionText.substring(wordPrefixLength);
545 this._userEnteredRange.deleteContents();
546 this._element.normalize();
547 var finalSelectionRange = this._createRange();
548
549 var prefixTextNode = createTextNode(prefixText);
550 fullWordRange.insertNode(prefixTextNode);
551
552 if (!this._autocompleteElement)
553 this._autocompleteElement = createElementWithClass("span", "auto -complete-text");
554 this._autocompleteElement.textContent = suffixText;
555 this._currentHintText = completionText;
556
557 prefixTextNode.parentNode.insertBefore(this._autocompleteElement, pr efixTextNode.nextSibling);
558
559 finalSelectionRange.setStart(prefixTextNode, wordPrefixLength);
560 finalSelectionRange.setEnd(prefixTextNode, wordPrefixLength);
561 selection.removeAllRanges();
562 selection.addRange(finalSelectionRange);
563 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemApp lied);
564 }
565 }, 541 },
566 542
567 /** 543 /**
568 * @override 544 * @override
569 * @param {string} completionText 545 * @param {string} completionText
570 * @param {boolean=} isIntermediateSuggestion 546 * @param {boolean=} isIntermediateSuggestion
571 */ 547 */
572 applySuggestion: function(completionText, isIntermediateSuggestion) 548 applySuggestion: function(completionText, isIntermediateSuggestion)
573 { 549 {
574 this._applySuggestion(completionText, isIntermediateSuggestion); 550 if (!this._userEnteredRange)
551 return;
552 this._setAutocompleteText(completionText);
553 if (isIntermediateSuggestion)
554 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemApp lied);
575 }, 555 },
576 556
577 /** 557 /**
578 * @param {string} completionText
579 * @param {boolean=} isIntermediateSuggestion
580 */
581 _applySuggestion: function(completionText, isIntermediateSuggestion)
582 {
583 if (!this._userEnteredRange) {
584 // We could have already cleared autocompletion range by the time th is is called. (crbug.com/587683)
585 return;
586 }
587
588 var wordPrefixLength = this._userEnteredText ? this._userEnteredText.len gth : 0;
589
590 this._userEnteredRange.deleteContents();
591 this._element.normalize();
592 var finalSelectionRange = this._createRange();
593 var completionTextNode = createTextNode(completionText);
594 this._userEnteredRange.insertNode(completionTextNode);
595 if (this._autocompleteElement) {
596 this._autocompleteElement.remove();
597 delete this._autocompleteElement;
598 }
599
600 if (isIntermediateSuggestion)
601 finalSelectionRange.setStart(completionTextNode, wordPrefixLength);
602 else
603 finalSelectionRange.setStart(completionTextNode, completionText.leng th);
604
605 finalSelectionRange.setEnd(completionTextNode, completionText.length);
606
607 var selection = this._element.getComponentSelection();
608 selection.removeAllRanges();
609 selection.addRange(finalSelectionRange);
610 if (isIntermediateSuggestion)
611 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemApp lied, { itemText: completionText });
612 },
613
614 /**
615 * @override 558 * @override
616 */ 559 */
617 acceptSuggestion: function() 560 acceptSuggestion: function()
618 { 561 {
619 this._acceptSuggestionInternal(); 562 this._acceptSuggestionInternal();
620 }, 563 },
621 564
622 /** 565 /**
623 * @return {boolean} 566 * @return {boolean}
624 */ 567 */
625 _acceptSuggestionInternal: function() 568 _acceptSuggestionInternal: function()
626 { 569 {
627 if (!this._autocompleteElement || !this._autocompleteElement.parentNode) 570 if (!this._userEnteredRange)
628 return false; 571 return false;
629 572
630 var text = this._autocompleteElement.textContent; 573 var text = this._currentHintText;
631 var textNode = createTextNode(text); 574 var textNode = createTextNode(text);
632 this._autocompleteElement.parentNode.replaceChild(textNode, this._autoco mpleteElement); 575 this._autocompleteElement.remove();
633 delete this._autocompleteElement; 576 this._userEnteredRange.deleteContents();
634 577 this._userEnteredRange.insertNode(textNode);
635 var finalSelectionRange = this._createRange(); 578 var finalSelectionRange = this._createRange();
636 finalSelectionRange.setStart(textNode, text.length); 579 finalSelectionRange.setStart(textNode, text.length);
637 finalSelectionRange.setEnd(textNode, text.length); 580 finalSelectionRange.setEnd(textNode, text.length);
638 581
639 var selection = this._element.getComponentSelection(); 582 var selection = this._element.getComponentSelection();
640 selection.removeAllRanges(); 583 selection.removeAllRanges();
641 selection.addRange(finalSelectionRange); 584 selection.addRange(finalSelectionRange);
642 585
643 this.clearAutocomplete(); 586 this.clearAutocomplete();
644 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemAccepte d); 587 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemAccepte d);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 var node = selectionRange.startContainer; 623 var node = selectionRange.startContainer;
681 if (!node.isSelfOrDescendant(this._element)) 624 if (!node.isSelfOrDescendant(this._element))
682 return false; 625 return false;
683 626
684 if (node.nodeType === Node.TEXT_NODE && selectionRange.startOffset < nod e.nodeValue.length) 627 if (node.nodeType === Node.TEXT_NODE && selectionRange.startOffset < nod e.nodeValue.length)
685 return false; 628 return false;
686 629
687 var foundNextText = false; 630 var foundNextText = false;
688 while (node) { 631 while (node) {
689 if (node.nodeType === Node.TEXT_NODE && node.nodeValue.length) { 632 if (node.nodeType === Node.TEXT_NODE && node.nodeValue.length) {
690 if (foundNextText && (!this._autocompleteElement || !this._autoc ompleteElement.isAncestor(node))) 633 if (foundNextText && !this._autocompleteElement.isAncestor(node) )
691 return false; 634 return false;
692 foundNextText = true; 635 foundNextText = true;
693 } 636 }
694 637
695 node = node.traverseNextNode(this._element); 638 node = node.traverseNextNode(this._element);
696 } 639 }
697 640
698 return true; 641 return true;
699 }, 642 },
700 643
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 }, 939 },
997 940
998 /** 941 /**
999 * @return {string|undefined} 942 * @return {string|undefined}
1000 */ 943 */
1001 _currentHistoryItem: function() 944 _currentHistoryItem: function()
1002 { 945 {
1003 return this._data[this._data.length - this._historyOffset]; 946 return this._data[this._data.length - this._historyOffset];
1004 } 947 }
1005 }; 948 };
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/http/tests/inspector-unit/text-prompt-hint-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698