Changeset 223336 in webkit


Ignore:
Timestamp:
Oct 15, 2017 11:25:54 PM (7 years ago)
Author:
Nikita Vasilyev
Message:

Web Inspector: Modify CSS number values with up key and down key
https://bugs.webkit.org/show_bug.cgi?id=170779
<rdar://problem/33170633>

Reviewed by Matt Baker.

Source/WebInspectorUI:

Up key increments a number, Down key decrements it.

Holding modifier keys changes the step value:

  • Option modifies the value by 0.1
  • Shift modifies the value by 10
  • Command modifies the value by 100
  • UserInterface/Test.html:
  • UserInterface/Views/EditingSupport.js:

(WI.incrementElementValue):
Abstract away incrementElementValue into a public method.

  • UserInterface/Views/SpreadsheetTextField.js:

(WI.SpreadsheetTextField.prototype._handleKeyDown):

LayoutTests:

Add tests for WI.incrementElementValue defined in WebInspectorUI/UserInterface/Views/EditingSupport.js.

  • inspector/unit-tests/editing-support-expected.txt: Added.
  • inspector/unit-tests/editing-support.html: Added.
Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r223328 r223336  
     12017-10-15  Nikita Vasilyev  <nvasilyev@apple.com>
     2
     3        Web Inspector: Modify CSS number values with up key and down key
     4        https://bugs.webkit.org/show_bug.cgi?id=170779
     5        <rdar://problem/33170633>
     6
     7        Reviewed by Matt Baker.
     8
     9        Add tests for WI.incrementElementValue defined in WebInspectorUI/UserInterface/Views/EditingSupport.js.
     10
     11        * inspector/unit-tests/editing-support-expected.txt: Added.
     12        * inspector/unit-tests/editing-support.html: Added.
     13
    1142017-10-14  Antoine Quint  <graouts@apple.com>
    215
  • trunk/Source/WebInspectorUI/ChangeLog

    r223335 r223336  
     12017-10-15  Nikita Vasilyev  <nvasilyev@apple.com>
     2
     3        Web Inspector: Modify CSS number values with up key and down key
     4        https://bugs.webkit.org/show_bug.cgi?id=170779
     5        <rdar://problem/33170633>
     6
     7        Reviewed by Matt Baker.
     8
     9        Up key increments a number, Down key decrements it.
     10
     11        Holding modifier keys changes the step value:
     12        - Option modifies the value by 0.1
     13        - Shift modifies the value by 10
     14        - Command modifies the value by 100
     15
     16        * UserInterface/Test.html:
     17        * UserInterface/Views/EditingSupport.js:
     18        (WI.incrementElementValue):
     19        Abstract away incrementElementValue into a public method.
     20
     21        * UserInterface/Views/SpreadsheetTextField.js:
     22        (WI.SpreadsheetTextField.prototype._handleKeyDown):
     23
    1242017-10-15  Devin Rousso  <webkit@devinrousso.com>
    225
  • trunk/Source/WebInspectorUI/UserInterface/Test.html

    r223058 r223336  
    217217    <script src="Views/CodeMirrorAdditions.js"></script>
    218218    <script src="Views/CodeMirrorFormatters.js"></script>
     219    <script src="Views/EditingSupport.js"></script>
    219220    <script src="Views/View.js"></script>
    220221
  • trunk/Source/WebInspectorUI/UserInterface/Views/EditingSupport.js

    r222678 r223336  
    229229        } else if (result && result.startsWith("modify-")) {
    230230            let direction = result.substring(7);
    231             let modifyValue = direction.startsWith("up") ? 1 : -1;
     231            let delta = direction.startsWith("up") ? 1 : -1;
    232232            if (direction.endsWith("big"))
    233                 modifyValue *= 10;
     233                delta *= 10;
    234234
    235235            if (event.shiftKey)
    236                 modifyValue *= 10;
     236                delta *= 10;
    237237            else if (event.ctrlKey)
    238                 modifyValue /= 10;
    239 
    240             let selection = element.ownerDocument.defaultView.getSelection();
    241             if (!selection.rangeCount)
     238                delta /= 10;
     239
     240            let modified = WI.incrementElementValue(element, delta);
     241            if (!modified)
    242242                return;
    243 
    244             let range = selection.getRangeAt(0);
    245             if (!range.commonAncestorContainer.isSelfOrDescendant(element))
    246                 return false;
    247 
    248             let wordRange = range.startContainer.rangeOfWord(range.startOffset, WI.EditingSupport.StyleValueDelimiters, element);
    249             let word = wordRange.toString();
    250             let wordPrefix = "";
    251             let wordSuffix = "";
    252             let nonNumberInWord = /[^\d-\.]+/.exec(word);
    253             if (nonNumberInWord) {
    254                 let nonNumberEndOffset = nonNumberInWord.index + nonNumberInWord[0].length;
    255                 if (range.startOffset > wordRange.startOffset + nonNumberInWord.index && nonNumberEndOffset < word.length && range.startOffset !== wordRange.startOffset) {
    256                     wordPrefix = word.substring(0, nonNumberEndOffset);
    257                     word = word.substring(nonNumberEndOffset);
    258                 } else {
    259                     wordSuffix = word.substring(nonNumberInWord.index);
    260                     word = word.substring(0, nonNumberInWord.index);
    261                 }
    262             }
    263 
    264             let matches = WI.EditingSupport.CSSNumberRegex.exec(word);
    265             if (!matches || matches.length !== 4)
    266                 return;
    267 
    268             let replacement = matches[1] + (Math.round((parseFloat(matches[2]) + modifyValue) * 100) / 100) + matches[3];
    269 
    270             selection.removeAllRanges();
    271             selection.addRange(wordRange);
    272             document.execCommand("insertText", false, wordPrefix + replacement + wordSuffix);
    273 
    274             let container = range.commonAncestorContainer;
    275             let startOffset = range.startOffset;
    276             // This check is for the situation when the cursor is in the space between the
    277             // opening quote of the attribute and the first character. In that spot, the
    278             // commonAncestorContainer is actually the entire attribute node since `="` is
    279             // added as a simple text node. Since the opening quote is immediately before
    280             // the attribute, the node for that attribute must be the next sibling and the
    281             // text of the attribute's value must be the first child of that sibling.
    282             if (container.parentNode.classList.contains("editing")) {
    283                 container = container.nextSibling.firstChild;
    284                 startOffset = 0;
    285             }
    286             startOffset += wordPrefix.length;
    287 
    288             if (!container)
    289                 return;
    290 
    291             let replacementSelectionRange = document.createRange();
    292             replacementSelectionRange.setStart(container, startOffset);
    293             replacementSelectionRange.setEnd(container, startOffset + replacement.length);
    294 
    295             selection.removeAllRanges();
    296             selection.addRange(replacementSelectionRange);
    297243
    298244            if (typeof config.numberCommitHandler === "function")
     
    329275};
    330276
     277WI.incrementElementValue = function(element, delta)
     278{
     279    let selection = element.ownerDocument.defaultView.getSelection();
     280    if (!selection.rangeCount)
     281        return false;
     282
     283    let range = selection.getRangeAt(0);
     284    if (!range.commonAncestorContainer.isSelfOrDescendant(element))
     285        return false;
     286
     287    let wordRange = range.startContainer.rangeOfWord(range.startOffset, WI.EditingSupport.StyleValueDelimiters, element);
     288    let word = wordRange.toString();
     289    let wordPrefix = "";
     290    let wordSuffix = "";
     291    let nonNumberInWord = /[^\d-\.]+/.exec(word);
     292    if (nonNumberInWord) {
     293        let nonNumberEndOffset = nonNumberInWord.index + nonNumberInWord[0].length;
     294        if (range.startOffset > wordRange.startOffset + nonNumberInWord.index && nonNumberEndOffset < word.length && range.startOffset !== wordRange.startOffset) {
     295            wordPrefix = word.substring(0, nonNumberEndOffset);
     296            word = word.substring(nonNumberEndOffset);
     297        } else {
     298            wordSuffix = word.substring(nonNumberInWord.index);
     299            word = word.substring(0, nonNumberInWord.index);
     300        }
     301    }
     302
     303    let matches = WI.EditingSupport.CSSNumberRegex.exec(word);
     304    if (!matches || matches.length !== 4)
     305        return false;
     306
     307    let replacement = matches[1] + (Math.round((parseFloat(matches[2]) + delta) * 100) / 100) + matches[3];
     308
     309    selection.removeAllRanges();
     310    selection.addRange(wordRange);
     311    document.execCommand("insertText", false, wordPrefix + replacement + wordSuffix);
     312
     313    let container = range.commonAncestorContainer;
     314    let startOffset = range.startOffset;
     315    // This check is for the situation when the cursor is in the space between the
     316    // opening quote of the attribute and the first character. In that spot, the
     317    // commonAncestorContainer is actually the entire attribute node since `="` is
     318    // added as a simple text node. Since the opening quote is immediately before
     319    // the attribute, the node for that attribute must be the next sibling and the
     320    // text of the attribute's value must be the first child of that sibling.
     321    if (container.parentNode.classList.contains("editing") && container.nextSibling) {
     322        container = container.nextSibling.firstChild;
     323        startOffset = 0;
     324    }
     325    startOffset += wordPrefix.length;
     326
     327    if (!container)
     328        return false;
     329
     330    let replacementSelectionRange = document.createRange();
     331    replacementSelectionRange.setStart(container, startOffset);
     332    replacementSelectionRange.setEnd(container, startOffset + replacement.length);
     333
     334    selection.removeAllRanges();
     335    selection.addRange(replacementSelectionRange);
     336
     337    return true;
     338};
     339
    331340WI.EditingSupport = {
    332341    StyleValueDelimiters: " \xA0\t\n\"':;,/()",
  • trunk/Source/WebInspectorUI/UserInterface/Views/SpreadsheetTextField.js

    r223283 r223336  
    226226        }
    227227
     228        if (event.key === "ArrowUp" || event.key === "ArrowDown") {
     229            let delta = 1;
     230            if (event.metaKey)
     231                delta = 100;
     232            else if (event.shiftKey)
     233                delta = 10;
     234            else if (event.altKey)
     235                delta = 0.1;
     236
     237            if (event.key === "ArrowDown")
     238                delta = -delta;
     239
     240            let didModify = WI.incrementElementValue(this._element, delta);
     241            if (!didModify)
     242                return;
     243
     244            event.stop();
     245
     246            if (this._delegate && typeof this._delegate.spreadsheetTextFieldDidChange === "function")
     247                this._delegate.spreadsheetTextFieldDidChange(this);
     248        }
     249
    228250        if (event.key === "Escape") {
    229251            event.stop();
Note: See TracChangeset for help on using the changeset viewer.