Changeset 30931 in webkit


Ignore:
Timestamp:
Mar 10, 2008 9:17:38 AM (16 years ago)
Author:
Adam Roben
Message:

Refactor CSS editing code so we can share it with DOM editing

Add WebInspector.startEditing, which takes the element to be edited, a
committedCallback, a cancelledCallback, and a context parameter. This
function takes care of setting up the element for editing, and calls
either the cancelledCallback or committedCallback when editing ends.

Reviewed by Tim.

(WebInspector.StylePropertyTreeElement.editingEnded):

  • Renamed from endEditing
  • Removed code now handled by WebInspector.startEditing

(WebInspector.StylePropertyTreeElement.editingCancelled):

  • Renamed from cancelEditing
  • Changed parameters to match WebInspector.startEditing's cancelledCallback

(WebInspector.StylePropertyTreeElement.editingCommitted):

  • Renamed from commitEditing
  • Changed parameters to match WebInspector.startEditing's committedCallback
  • page/inspector/inspector.js: (WebInspector.changeFocus): Changed a call to firstParentWithClassName to firstParentOrSelfWithClassName so that if the focusable element itself is the target it will be recognized. I don't know why this change wasn't needed before this. (WebInspector.isBeingEdited): Added. (WebInspector.startEditing): Added.
Location:
trunk/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r30930 r30931  
     12008-03-09  Adam Roben  <aroben@apple.com>
     2
     3        Refactor CSS editing code so we can share it with DOM editing
     4
     5        Add WebInspector.startEditing, which takes the element to be edited, a
     6        committedCallback, a cancelledCallback, and a context parameter. This
     7        function takes care of setting up the element for editing, and calls
     8        either the cancelledCallback or committedCallback when editing ends.
     9
     10        Reviewed by Tim.
     11
     12        * page/inspector/StylesSidebarPane.js:
     13        (WebInspector.StylePropertyTreeElement.startEditing):
     14          - Call WebInspector.isBeingEdited and WebInspector.startEditing
     15          - Removed code now handled by WebInspector.startEditing
     16        (WebInspector.StylePropertyTreeElement.editingEnded):
     17          - Renamed from endEditing
     18          - Removed code now handled by WebInspector.startEditing
     19        (WebInspector.StylePropertyTreeElement.editingCancelled):
     20          - Renamed from cancelEditing
     21          - Changed parameters to match WebInspector.startEditing's
     22            cancelledCallback
     23        (WebInspector.StylePropertyTreeElement.editingCommitted):
     24          - Renamed from commitEditing
     25          - Changed parameters to match WebInspector.startEditing's
     26            committedCallback
     27        * page/inspector/inspector.js:
     28        (WebInspector.changeFocus): Changed a call to firstParentWithClassName
     29        to firstParentOrSelfWithClassName so that if the focusable element
     30        itself is the target it will be recognized. I don't know why this
     31        change wasn't needed before this.
     32        (WebInspector.isBeingEdited): Added.
     33        (WebInspector.startEditing): Added.
     34
    1352008-03-10  Adam Roben  <aroben@apple.com>
    236
  • trunk/WebCore/page/inspector/StylesSidebarPane.js

    r30654 r30931  
    542542            return;
    543543
    544         if (this.editing || (this.treeOutline.section && !this.treeOutline.section.editable))
    545             return;
    546 
    547         this.editing = true;
    548         this.previousTextContent = this.listItemElement.textContent;
    549 
    550         this.listItemElement.addStyleClass("focusable");
    551         this.listItemElement.addStyleClass("editing");
    552         this.wasExpanded = this.expanded;
     544        if (WebInspector.isBeingEdited(this.listItemElement) || (this.treeOutline.section && !this.treeOutline.section.editable))
     545            return;
     546
     547        var wasExpanded = this.expanded;
    553548        this.collapse();
    554549        // Lie about out children to prevent toggling on click.
     
    560555        window.getSelection().setBaseAndExtent(selectElement, 0, selectElement, 1);
    561556
    562         var treeElement = this;
    563         this.listItemElement.blurred = function() { treeElement.commitEditing() };
    564         this.listItemElement.handleKeyEvent = function(event) {
    565             if (event.keyIdentifier === "Enter") {
    566                 treeElement.commitEditing();
    567                 event.preventDefault();
    568             } else if (event.keyCode === 27) { // Escape key
    569                 treeElement.cancelEditing();
    570                 event.preventDefault();
    571             }
    572         };
    573 
    574         this.previousFocusElement = WebInspector.currentFocusElement;
    575         WebInspector.currentFocusElement = this.listItemElement;
    576     },
    577 
    578     endEditing: function()
    579     {
    580         // Revert the changes done in startEditing().
    581         delete this.listItemElement.blurred;
    582         delete this.listItemElement.handleKeyEvent;
    583 
    584         WebInspector.currentFocusElement = this.previousFocusElement;
    585         delete this.previousFocusElement;
    586 
    587         delete this.previousTextContent;
    588         delete this.editing;
    589 
    590         this.listItemElement.removeStyleClass("focusable");
    591         this.listItemElement.removeStyleClass("editing");
     557        WebInspector.startEditing(this.listItemElement, this.editingCommitted.bind(this), this.editingCancelled.bind(this), wasExpanded);
     558    },
     559
     560    editingEnded: function(wasExpanded)
     561    {
    592562        this.hasChildren = (this.children.length ? true : false);
    593         if (this.wasExpanded) {
    594             delete this.wasExpanded;
     563        if (wasExpanded)
    595564            this.expand();
    596         }
    597     },
    598 
    599     cancelEditing: function()
    600     {
    601         this.endEditing();
     565    },
     566
     567    editingCancelled: function(e, wasExpanded)
     568    {
     569        this.editingEnded(wasExpanded);
    602570        this.updateTitle();
    603571    },
    604572
    605     commitEditing: function()
    606     {
    607         var previousContent = this.previousTextContent;
    608 
    609         this.endEditing();
    610 
    611         var userInput = this.listItemElement.textContent;
     573    editingCommitted: function(e, userInput, previousContent, wasExpanded)
     574    {
     575        this.editingEnded();
     576
    612577        if (userInput === previousContent)
    613578            return; // nothing changed, so do nothing else
  • trunk/WebCore/page/inspector/inspector.js

    r30607 r30931  
    321321
    322322    if (!nextFocusElement)
    323         nextFocusElement = event.target.firstParentWithClass("focusable");
     323        nextFocusElement = event.target.firstParentOrSelfWithClass("focusable");
    324324
    325325    this.currentFocusElement = nextFocusElement;
     
    10521052
    10531053    return String.vsprintf(string, Array.prototype.slice.call(arguments, 1));
     1054}
     1055
     1056WebInspector.isBeingEdited = function(element)
     1057{
     1058    return element.__editing;
     1059}
     1060
     1061WebInspector.startEditing = function(element, committedCallback, cancelledCallback, context)
     1062{
     1063    if (element.__editing)
     1064        return;
     1065    element.__editing = true;
     1066
     1067    var oldText = element.textContent;
     1068    var handleKeyEvent = element.handleKeyEvent;
     1069    var blurred = element.blurred;
     1070
     1071    element.addStyleClass("editing");
     1072    element.addStyleClass("focusable");
     1073
     1074    var previousFocusElement = WebInspector.currentFocusElement;
     1075
     1076    function cleanUpAfterEditing() {
     1077        delete this.__editing;
     1078
     1079        this.removeStyleClass("editing");
     1080        this.removeStyleClass("focusable");
     1081
     1082        this.handleKeyEvent = handleKeyEvent;
     1083        this.blurred = blurred;
     1084
     1085        WebInspector.currentFocusElement = previousFocusElement;
     1086    }
     1087
     1088    function editingCancelled() {
     1089        this.innerText = oldText;
     1090
     1091        cleanUpAfterEditing.call(this);
     1092
     1093        cancelledCallback(this, context);
     1094    }
     1095
     1096    function editingCommitted() {
     1097        cleanUpAfterEditing.call(this);
     1098
     1099        committedCallback(this, this.textContent, oldText, context);
     1100    }
     1101
     1102    element.handleKeyEvent = function(event) {
     1103        if (event.keyIdentifier === "Enter") {
     1104            editingCommitted.call(element);
     1105            event.preventDefault();
     1106        } else if (event.keyCode === 27) { // Escape key
     1107            editingCancelled.call(element);
     1108            event.preventDefault();
     1109        }
     1110    }
     1111
     1112    element.blurred = function() { editingCancelled.call(element); }
     1113
     1114    WebInspector.currentFocusElement = element;
    10541115}
    10551116
Note: See TracChangeset for help on using the changeset viewer.