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

Side by Side Diff: Source/devtools/front_end/elements/StylesSidebarPane.js

Issue 441873010: DevTools: [SSP] Implement adding new rule in user stylesheet (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: minor changes Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro 3 * Copyright (C) 2009 Joseph Pecoraro
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 29 matching lines...) Expand all
40 this._elementStateButton = document.createElement("button"); 40 this._elementStateButton = document.createElement("button");
41 this._elementStateButton.className = "pane-title-button element-state"; 41 this._elementStateButton.className = "pane-title-button element-state";
42 this._elementStateButton.title = WebInspector.UIString("Toggle Element State "); 42 this._elementStateButton.title = WebInspector.UIString("Toggle Element State ");
43 this._elementStateButton.addEventListener("click", this._toggleElementStateP ane.bind(this), false); 43 this._elementStateButton.addEventListener("click", this._toggleElementStateP ane.bind(this), false);
44 this.titleElement.appendChild(this._elementStateButton); 44 this.titleElement.appendChild(this._elementStateButton);
45 45
46 var addButton = document.createElement("button"); 46 var addButton = document.createElement("button");
47 addButton.className = "pane-title-button add"; 47 addButton.className = "pane-title-button add";
48 addButton.id = "add-style-button-test-id"; 48 addButton.id = "add-style-button-test-id";
49 addButton.title = WebInspector.UIString("New Style Rule"); 49 addButton.title = WebInspector.UIString("New Style Rule");
50 addButton.addEventListener("click", this._createNewRule.bind(this), false); 50 addButton.addEventListener("click", this._createNewRuleInViaInspectorStyleSh eet.bind(this), false);
51 this.titleElement.appendChild(addButton); 51 this.titleElement.appendChild(addButton);
52 52
53 this._computedStylePane = computedStylePane; 53 this._computedStylePane = computedStylePane;
54 computedStylePane.setHostingPane(this); 54 computedStylePane.setHostingPane(this);
55 this._setPseudoClassCallback = setPseudoClassCallback; 55 this._setPseudoClassCallback = setPseudoClassCallback;
56 this.element.addEventListener("contextmenu", this._contextMenuEventFired.bin d(this), true); 56 this.element.addEventListener("contextmenu", this._contextMenuEventFired.bin d(this), true);
57 WebInspector.settings.colorFormat.addChangeListener(this._colorFormatSetting Changed.bind(this)); 57 WebInspector.settings.colorFormat.addChangeListener(this._colorFormatSetting Changed.bind(this));
58 WebInspector.settings.showUserAgentStyles.addChangeListener(this._showUserAg entStylesSettingChanged.bind(this)); 58 WebInspector.settings.showUserAgentStyles.addChangeListener(this._showUserAg entStylesSettingChanged.bind(this));
59 59
60 this._createElementStatePane(); 60 this._createElementStatePane();
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 783
784 _colorFormatSettingChanged: function(event) 784 _colorFormatSettingChanged: function(event)
785 { 785 {
786 for (var pseudoId in this.sections) { 786 for (var pseudoId in this.sections) {
787 var sections = this.sections[pseudoId]; 787 var sections = this.sections[pseudoId];
788 for (var i = 0; i < sections.length; ++i) 788 for (var i = 0; i < sections.length; ++i)
789 sections[i].update(true); 789 sections[i].update(true);
790 } 790 }
791 }, 791 },
792 792
793 _createNewRule: function(event) 793 /**
794 * @param {?Event} event
795 */
796 _createNewRuleInViaInspectorStyleSheet: function(event)
794 { 797 {
795 event.consume(); 798 event.consume();
796 this.expand(); 799 var cssModel = this._target.cssModel;
797 this.addBlankSection().startEditingSelector(); 800 cssModel.requestViaInspectorStylesheet(this._node, viaInspectorCallback. bind(this));
801
802 /**
803 * @param {?WebInspector.CSSStyleSheetHeader} styleSheetHeader
804 * @this {WebInspector.StylesSidebarPane}
805 */
806 function viaInspectorCallback(styleSheetHeader)
807 {
808 if (!styleSheetHeader)
809 return;
810 styleSheetHeader.requestContent(onViaInspectorContent.bind(this, sty leSheetHeader.id));
811 }
812
813 /**
814 * @param {string} styleSheetId
815 * @param {string} text
816 * @this {WebInspector.StylesSidebarPane}
817 */
818 function onViaInspectorContent(styleSheetId, text)
819 {
820 var lines = text.split("\n");
821 var range = WebInspector.TextRange.createFromLocation(lines.length - 1, lines[lines.length - 1].length);
822 this._addBlankSection(this.sections[0][1], styleSheetId, range);
823 }
798 }, 824 },
799 825
800 /** 826 /**
801 * @return {!WebInspector.BlankStylePropertiesSection} 827 * @param {!WebInspector.StylePropertiesSection} insertAfterSection
828 * @param {string} styleSheetId
829 * @param {!WebInspector.TextRange} ruleLocation
802 */ 830 */
803 addBlankSection: function() 831 _addBlankSection: function(insertAfterSection, styleSheetId, ruleLocation)
804 { 832 {
805 var blankSection = new WebInspector.BlankStylePropertiesSection(this, th is._node ? WebInspector.DOMPresentationUtils.simpleSelector(this._node) : ""); 833 this.expand();
vsevik 2014/08/12 16:06:01 Why is this needed?
lushnikov 2014/08/13 09:10:48 Acknowledged.
834 var blankSection = new WebInspector.BlankStylePropertiesSection(this, th is._node ? WebInspector.DOMPresentationUtils.simpleSelector(this._node) : "", st yleSheetId, ruleLocation, insertAfterSection.rule);
806 835
807 var elementStyleSection = this.sections[0][1]; 836 this._sectionsContainer.insertBefore(blankSection.element, insertAfterSe ction.element.nextSibling);
808 this._sectionsContainer.insertBefore(blankSection.element, elementStyleS ection.element.nextSibling);
809 837
810 this.sections[0].splice(2, 0, blankSection); 838 var index = this.sections[0].indexOf(insertAfterSection);
811 839 this.sections[0].splice(index + 1, 0, blankSection);
812 return blankSection; 840 blankSection.startEditingSelector();
813 }, 841 },
814 842
815 removeSection: function(section) 843 removeSection: function(section)
816 { 844 {
817 for (var pseudoId in this.sections) { 845 for (var pseudoId in this.sections) {
818 var sections = this.sections[pseudoId]; 846 var sections = this.sections[pseudoId];
819 var index = sections.indexOf(section); 847 var index = sections.indexOf(section);
820 if (index === -1) 848 if (index === -1)
821 continue; 849 continue;
822 sections.splice(index, 1); 850 sections.splice(index, 1);
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1101 var openBrace = document.createElement("span"); 1129 var openBrace = document.createElement("span");
1102 openBrace.textContent = " {"; 1130 openBrace.textContent = " {";
1103 selectorContainer.appendChild(openBrace); 1131 selectorContainer.appendChild(openBrace);
1104 selectorContainer.addEventListener("mousedown", this._handleEmptySpaceMouseD own.bind(this), false); 1132 selectorContainer.addEventListener("mousedown", this._handleEmptySpaceMouseD own.bind(this), false);
1105 selectorContainer.addEventListener("click", this._handleSelectorContainerCli ck.bind(this), false); 1133 selectorContainer.addEventListener("click", this._handleSelectorContainerCli ck.bind(this), false);
1106 1134
1107 var closeBrace = document.createElement("div"); 1135 var closeBrace = document.createElement("div");
1108 closeBrace.textContent = "}"; 1136 closeBrace.textContent = "}";
1109 this.element.appendChild(closeBrace); 1137 this.element.appendChild(closeBrace);
1110 1138
1139 if (this.editable && this.rule) {
1140 var newRuleButton = closeBrace.createChild("div", "sidebar-pane-button-n ew-rule");
1141 newRuleButton.addEventListener("click", this._onNewRuleClick.bind(this), false);
1142 }
1143
1111 this._selectorElement.addEventListener("click", this._handleSelectorClick.bi nd(this), false); 1144 this._selectorElement.addEventListener("click", this._handleSelectorClick.bi nd(this), false);
1112 this.element.addEventListener("mousedown", this._handleEmptySpaceMouseDown.b ind(this), false); 1145 this.element.addEventListener("mousedown", this._handleEmptySpaceMouseDown.b ind(this), false);
1113 this.element.addEventListener("click", this._handleEmptySpaceClick.bind(this ), false); 1146 this.element.addEventListener("click", this._handleEmptySpaceClick.bind(this ), false);
1114 1147
1115 if (this.rule) { 1148 if (this.rule) {
1116 // Prevent editing the user agent and user rules. 1149 // Prevent editing the user agent and user rules.
1117 if (this.rule.isUserAgent || this.rule.isUser) 1150 if (this.rule.isUserAgent || this.rule.isUser)
1118 this.editable = false; 1151 this.editable = false;
1119 else { 1152 else {
1120 // Check this is a real CSSRule, not a bogus object coming from WebI nspector.BlankStylePropertiesSection. 1153 // Check this is a real CSSRule, not a bogus object coming from WebI nspector.BlankStylePropertiesSection.
(...skipping 19 matching lines...) Expand all
1140 1173
1141 if (this.navigable) 1174 if (this.navigable)
1142 this.element.classList.add("navigable"); 1175 this.element.classList.add("navigable");
1143 1176
1144 if (!this.editable) 1177 if (!this.editable)
1145 this.element.classList.add("read-only"); 1178 this.element.classList.add("read-only");
1146 } 1179 }
1147 1180
1148 WebInspector.StylePropertiesSection.prototype = { 1181 WebInspector.StylePropertiesSection.prototype = {
1149 /** 1182 /**
1183 * @param {?Event} event
1184 */
1185 _onNewRuleClick: function(event)
1186 {
1187 event.consume();
1188 var range = WebInspector.TextRange.createFromLocation(this.rule.style.ra nge.endLine, this.rule.style.range.endColumn + 1);
1189 this._parentPane._addBlankSection(this, this.rule.styleSheetId, range);
1190 },
1191
1192 /**
1150 * @param {!WebInspector.CSSRule} editedRule 1193 * @param {!WebInspector.CSSRule} editedRule
1151 * @param {!WebInspector.TextRange} oldRange 1194 * @param {!WebInspector.TextRange} oldRange
1152 * @param {!WebInspector.TextRange} newRange 1195 * @param {!WebInspector.TextRange} newRange
1153 */ 1196 */
1154 _styleSheetRuleEdited: function(editedRule, oldRange, newRange) 1197 _styleSheetRuleEdited: function(editedRule, oldRange, newRange)
1155 { 1198 {
1156 if (!this.rule || !this.rule.styleSheetId) 1199 if (!this.rule || !this.rule.styleSheetId)
1157 return; 1200 return;
1158 if (this.rule !== editedRule) 1201 if (this.rule !== editedRule)
1159 this.rule.sourceStyleSheetEdited(editedRule.styleSheetId, oldRange, newRange); 1202 this.rule.sourceStyleSheetEdited(editedRule.styleSheetId, oldRange, newRange);
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after
1833 }, 1876 },
1834 1877
1835 __proto__: WebInspector.PropertiesSection.prototype 1878 __proto__: WebInspector.PropertiesSection.prototype
1836 } 1879 }
1837 1880
1838 /** 1881 /**
1839 * @constructor 1882 * @constructor
1840 * @extends {WebInspector.StylePropertiesSection} 1883 * @extends {WebInspector.StylePropertiesSection}
1841 * @param {!WebInspector.StylesSidebarPane} stylesPane 1884 * @param {!WebInspector.StylesSidebarPane} stylesPane
1842 * @param {string} defaultSelectorText 1885 * @param {string} defaultSelectorText
1886 * @param {string} styleSheetId
1887 * @param {!WebInspector.TextRange} ruleLocation
1888 * @param {!WebInspector.CSSRule=} insertAfterRule
1843 */ 1889 */
1844 WebInspector.BlankStylePropertiesSection = function(stylesPane, defaultSelectorT ext) 1890 WebInspector.BlankStylePropertiesSection = function(stylesPane, defaultSelectorT ext, styleSheetId, ruleLocation, insertAfterRule)
1845 { 1891 {
1846 WebInspector.StylePropertiesSection.call(this, stylesPane, {selectorText: de faultSelectorText, rule: {isViaInspector: true}}, true, false); 1892 WebInspector.StylePropertiesSection.call(this, stylesPane, {selectorText: de faultSelectorText, rule: {isViaInspector: true}}, true, false);
1893 this._ruleLocation = ruleLocation;
1894 this._styleSheetId = styleSheetId;
1895 if (insertAfterRule)
1896 this._createMediaList(insertAfterRule);
1847 this.element.classList.add("blank-section"); 1897 this.element.classList.add("blank-section");
1848 } 1898 }
1849 1899
1850 WebInspector.BlankStylePropertiesSection.prototype = { 1900 WebInspector.BlankStylePropertiesSection.prototype = {
1851 get isBlank() 1901 get isBlank()
1852 { 1902 {
1853 return !this._normal; 1903 return !this._normal;
1854 }, 1904 },
1855 1905
1856 expand: function() 1906 expand: function()
1857 { 1907 {
1858 if (!this.isBlank) 1908 if (!this.isBlank)
1859 WebInspector.StylePropertiesSection.prototype.expand.call(this); 1909 WebInspector.StylePropertiesSection.prototype.expand.call(this);
1860 }, 1910 },
1861 1911
1862 editingSelectorCommitted: function(element, newContent, oldContent, context, moveDirection) 1912 editingSelectorCommitted: function(element, newContent, oldContent, context, moveDirection)
1863 { 1913 {
1864 if (!this.isBlank) { 1914 if (!this.isBlank) {
1865 WebInspector.StylePropertiesSection.prototype.editingSelectorCommitt ed.call(this, element, newContent, oldContent, context, moveDirection); 1915 WebInspector.StylePropertiesSection.prototype.editingSelectorCommitt ed.call(this, element, newContent, oldContent, context, moveDirection);
1866 return; 1916 return;
1867 } 1917 }
1868 1918
1869 /** 1919 /**
1870 * @param {!WebInspector.CSSRule} newRule 1920 * @param {!WebInspector.CSSRule} newRule
1871 * @this {WebInspector.StylePropertiesSection} 1921 * @this {WebInspector.StylePropertiesSection}
1872 */ 1922 */
1873 function successCallback(newRule) 1923 function successCallback(newRule)
1874 { 1924 {
1875 var doesSelectorAffectSelectedNode = newRule.matchingSelectors.lengt h > 0; 1925 var doesSelectorAffectSelectedNode = newRule.matchingSelectors.lengt h > 0;
1876 var styleRule = { section: this, style: newRule.style, selectorText: newRule.selectorText, sourceURL: newRule.resourceURL(), rule: newRule }; 1926 var styleRule = { media: newRule.media, section: this, style: newRul e.style, selectorText: newRule.selectorText, sourceURL: newRule.resourceURL(), r ule: newRule };
1877 this._makeNormal(styleRule); 1927 this._makeNormal(styleRule);
1878 1928
1879 if (!doesSelectorAffectSelectedNode) { 1929 if (!doesSelectorAffectSelectedNode) {
1880 this.noAffect = true; 1930 this.noAffect = true;
1881 this.element.classList.add("no-affect"); 1931 this.element.classList.add("no-affect");
1882 } 1932 }
1883 1933
1934 var ruleTextLines = ruleText.split("\n");
1935 var startLine = this._ruleLocation.startLine;
1936 var startColumn = this._ruleLocation.startColumn;
1937 var newRange = new WebInspector.TextRange(startLine, startColumn, st artLine + ruleTextLines.length - 1, startColumn + ruleTextLines[ruleTextLines.le ngth - 1].length);
1938 this._parentPane._styleSheetRuleEdited(newRule, this._ruleLocation, newRange);
1939
1884 this._updateRuleOrigin(); 1940 this._updateRuleOrigin();
1885 this.expand(); 1941 this.expand();
1886 if (this.element.parentElement) // Might have been detached already. 1942 if (this.element.parentElement) // Might have been detached already.
1887 this._moveEditorFromSelector(moveDirection); 1943 this._moveEditorFromSelector(moveDirection);
1888 1944
1889 delete this._parentPane._userOperation; 1945 delete this._parentPane._userOperation;
1890 this._editingSelectorEnded(); 1946 this._editingSelectorEnded();
1891 this._markSelectorMatches(); 1947 this._markSelectorMatches();
1892 } 1948 }
1893 1949
1894 if (newContent) 1950 if (newContent)
1895 newContent = newContent.trim(); 1951 newContent = newContent.trim();
1896 this._parentPane._userOperation = true; 1952 this._parentPane._userOperation = true;
1897 1953
1898 var cssModel = this._parentPane._target.cssModel; 1954 var cssModel = this._parentPane._target.cssModel;
1899 cssModel.requestViaInspectorStylesheet(this._parentPane._node, viaInspec torCallback.bind(this)); 1955 var rulePrefix = this._ruleLocation.startLine === 0 && this._ruleLocatio n.startColumn === 0 ? "" : "\n\n";
vsevik 2014/08/12 16:06:01 Could we be a bit smarter and make sure that we ha
lushnikov 2014/08/13 09:10:48 As per offline discussion, this is sufficient for
1900 1956 var ruleText = rulePrefix + newContent + " {}";
vsevik 2014/08/12 16:06:01 Maybe " {\n}\n" ? We could also take indent into
lushnikov 2014/08/13 09:10:48 Discussed offline.
1901 /** 1957 cssModel.addRule(this._styleSheetId, this._parentPane._node, ruleText, t his._ruleLocation, successCallback.bind(this), this.editingSelectorCancelled.bin d(this));
1902 * @this {WebInspector.BlankStylePropertiesSection}
1903 * @param {?WebInspector.CSSStyleSheetHeader} styleSheetHeader
1904 */
1905 function viaInspectorCallback(styleSheetHeader)
1906 {
1907 if (!styleSheetHeader) {
1908 this.editingSelectorCancelled();
1909 return;
1910 }
1911 cssModel.addRule(styleSheetHeader.id, this._parentPane._node, newCon tent, successCallback.bind(this), this.editingSelectorCancelled.bind(this));
1912 }
1913 }, 1958 },
1914 1959
1915 editingSelectorCancelled: function() 1960 editingSelectorCancelled: function()
1916 { 1961 {
1917 delete this._parentPane._userOperation; 1962 delete this._parentPane._userOperation;
1918 if (!this.isBlank) { 1963 if (!this.isBlank) {
1919 WebInspector.StylePropertiesSection.prototype.editingSelectorCancell ed.call(this); 1964 WebInspector.StylePropertiesSection.prototype.editingSelectorCancell ed.call(this);
1920 return; 1965 return;
1921 } 1966 }
1922 1967
(...skipping 1388 matching lines...) Expand 10 before | Expand all | Expand 10 after
3311 if (userEnteredText && (userEnteredText === userEnteredText.toUpperCase( ))) { 3356 if (userEnteredText && (userEnteredText === userEnteredText.toUpperCase( ))) {
3312 for (var i = 0; i < results.length; ++i) 3357 for (var i = 0; i < results.length; ++i)
3313 results[i] = results[i].toUpperCase(); 3358 results[i] = results[i].toUpperCase();
3314 } 3359 }
3315 var selectedIndex = this._cssCompletions.mostUsedOf(results); 3360 var selectedIndex = this._cssCompletions.mostUsedOf(results);
3316 completionsReadyCallback(results, selectedIndex); 3361 completionsReadyCallback(results, selectedIndex);
3317 }, 3362 },
3318 3363
3319 __proto__: WebInspector.TextPrompt.prototype 3364 __proto__: WebInspector.TextPrompt.prototype
3320 } 3365 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698