Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 */ | 33 */ |
| 34 Bindings.StylesSourceMapping = class { | 34 Bindings.StylesSourceMapping = class { |
| 35 /** | 35 /** |
| 36 * @param {!SDK.CSSModel} cssModel | 36 * @param {!SDK.CSSModel} cssModel |
| 37 * @param {!Workspace.Workspace} workspace | 37 * @param {!Workspace.Workspace} workspace |
| 38 */ | 38 */ |
| 39 constructor(cssModel, workspace) { | 39 constructor(cssModel, workspace) { |
| 40 this._cssModel = cssModel; | 40 this._cssModel = cssModel; |
| 41 this._workspace = workspace; | 41 this._workspace = workspace; |
| 42 | 42 |
| 43 /** @type {!Map<string, !Map<string, !Map<string, !SDK.CSSStyleSheetHeader>> >} */ | 43 /** @type {!Map<string, !Bindings.ContentProviderBasedProject>} */ |
| 44 this._urlToHeadersByFrameId = new Map(); | 44 this._projects = new Map(); |
| 45 | |
| 45 /** @type {!Map.<!Workspace.UISourceCode, !Bindings.StyleFile>} */ | 46 /** @type {!Map.<!Workspace.UISourceCode, !Bindings.StyleFile>} */ |
| 46 this._styleFiles = new Map(); | 47 this._styleFiles = new Map(); |
| 47 | 48 |
| 48 this._eventListeners = [ | 49 this._eventListeners = [ |
| 49 this._workspace.addEventListener(Workspace.Workspace.Events.ProjectRemoved , this._projectRemoved, this), | |
| 50 this._workspace.addEventListener( | |
| 51 Workspace.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedT oWorkspace, this), | |
| 52 this._workspace.addEventListener(Workspace.Workspace.Events.UISourceCodeRe moved, this._uiSourceCodeRemoved, this), | |
| 53 this._cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetAdded, this. _styleSheetAdded, this), | 50 this._cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetAdded, this. _styleSheetAdded, this), |
| 54 this._cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetRemoved, thi s._styleSheetRemoved, this), | 51 this._cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetRemoved, thi s._styleSheetRemoved, this), |
| 55 this._cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetChanged, thi s._styleSheetChanged, this), | 52 this._cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetChanged, thi s._styleSheetChanged, this) |
| 56 SDK.ResourceTreeModel.fromTarget(cssModel.target()) | |
| 57 .addEventListener(SDK.ResourceTreeModel.Events.MainFrameNavigated, thi s._unbindAllUISourceCodes, this) | |
| 58 ]; | 53 ]; |
| 59 } | 54 } |
| 60 | 55 |
| 61 /** | 56 /** |
| 57 * @param {!SDK.Target} target | |
| 58 * @param {?SDK.ResourceTreeFrame} frame | |
| 59 * @return {string} | |
| 60 */ | |
| 61 static _projectId(target, frame) { | |
| 62 return 'styles:' + target.id() + ':' + (frame ? frame.id : ''); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * @param {!SDK.CSSStyleSheetHeader} header | |
| 67 * @return {!Bindings.ContentProviderBasedProject} | |
| 68 */ | |
| 69 _createProject(header) { | |
| 70 var target = header.cssModel().target(); | |
| 71 var frame = SDK.ResourceTreeFrame.fromStyleSheet(header); | |
| 72 var projectId = Bindings.StylesSourceMapping._projectId(target, frame); | |
| 73 var project = new Bindings.ContentProviderBasedProject( | |
| 74 this._workspace, projectId, Workspace.projectTypes.Network, '', false /* isServiceProject */); | |
| 75 Bindings.NetworkProject.annotateProjectWithTargetAndFrame(project, target, f rame); | |
| 76 this._projects.set(projectId, project); | |
| 77 return project; | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * @param {!SDK.CSSStyleSheetHeader} header | |
| 82 * @return {?Bindings.ContentProviderBasedProject} | |
| 83 */ | |
| 84 _getProject(header) { | |
| 85 var target = header.cssModel().target(); | |
| 86 var frame = SDK.ResourceTreeFrame.fromStyleSheet(header); | |
| 87 return this._projects.get(Bindings.StylesSourceMapping._projectId(target, fr ame)) || null; | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * @param {!SDK.CSSStyleSheetHeader} header | |
| 92 * @return {!Workspace.UISourceCode} | |
| 93 */ | |
| 94 _createUISourceCode(header) { | |
| 95 var project = this._getProject(header); | |
| 96 if (!project) | |
| 97 project = this._createProject(header); | |
| 98 | |
| 99 var originalContentProvider = header.originalContentProvider(); | |
| 100 var url = originalContentProvider.contentURL(); | |
| 101 var uiSourceCode = project.createUISourceCode(url, originalContentProvider.c ontentType()); | |
| 102 Bindings.NetworkProject.useExplicitMimeType(uiSourceCode); | |
| 103 var resource = SDK.ResourceTreeModel.resourceForURL(url); | |
|
dgozman
2017/02/15 19:57:11
- header.target().model(SDK.ResourceTreeModel).res
lushnikov
2017/02/23 02:02:35
Done along this lines.
| |
| 104 var metadata = Bindings.NetworkProject.resourceMetadata(resource); | |
| 105 project.addUISourceCodeWithProvider(uiSourceCode, originalContentProvider, m etadata); | |
| 106 return uiSourceCode; | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * @param {!SDK.CSSStyleSheetHeader} header | |
| 111 * @return {?Workspace.UISourceCode} | |
| 112 */ | |
| 113 _getUISourceCode(header) { | |
| 114 var project = this._getProject(header); | |
| 115 return project ? project.uiSourceCodeForURL(header.contentURL()) : null; | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * @param {!Workspace.UISourceCode} uiSourceCode | |
| 120 */ | |
| 121 _removeUISourceCode(uiSourceCode) { | |
| 122 var project = /** @type {!Bindings.ContentProviderBasedProject} */ (uiSource Code.project()); | |
| 123 project.removeFile(uiSourceCode.url()); | |
| 124 if (project.isEmpty()) { | |
| 125 project.removeProject(); | |
| 126 this._projects.delete(project.id()); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 /** | |
| 62 * @param {!SDK.CSSLocation} rawLocation | 131 * @param {!SDK.CSSLocation} rawLocation |
| 63 * @return {?Workspace.UILocation} | 132 * @return {?Workspace.UILocation} |
| 64 */ | 133 */ |
| 65 rawLocationToUILocation(rawLocation) { | 134 rawLocationToUILocation(rawLocation) { |
| 66 var uiSourceCode = | 135 var uiSourceCode = this._getUISourceCode(rawLocation.header()); |
| 67 Bindings.NetworkProject.uiSourceCodeForStyleURL(this._workspace, rawLoca tion.url, rawLocation.header()); | |
| 68 if (!uiSourceCode) | 136 if (!uiSourceCode) |
| 69 return null; | 137 return null; |
| 70 var lineNumber = rawLocation.lineNumber; | 138 var lineNumber = rawLocation.lineNumber; |
| 71 var columnNumber = rawLocation.columnNumber; | 139 var columnNumber = rawLocation.columnNumber; |
| 72 var header = this._cssModel.styleSheetHeaderForId(rawLocation.styleSheetId); | 140 var header = rawLocation.header(); |
| 73 if (header && header.isInline && header.hasSourceURL) { | 141 if (header && header.isInline && header.hasSourceURL) { |
| 74 lineNumber -= header.lineNumberInSource(0); | 142 lineNumber -= header.lineNumberInSource(0); |
| 75 columnNumber -= header.columnNumberInSource(lineNumber, 0); | 143 columnNumber -= header.columnNumberInSource(lineNumber, 0); |
| 76 } | 144 } |
| 77 return uiSourceCode.uiLocation(lineNumber, columnNumber); | 145 return uiSourceCode.uiLocation(lineNumber, columnNumber); |
| 78 } | 146 } |
| 79 | 147 |
| 80 /** | 148 /** |
| 81 * @param {!Common.Event} event | 149 * @param {!Common.Event} event |
| 82 */ | 150 */ |
| 83 _styleSheetAdded(event) { | 151 _styleSheetAdded(event) { |
| 84 var header = /** @type {!SDK.CSSStyleSheetHeader} */ (event.data); | 152 var header = /** @type {!SDK.CSSStyleSheetHeader} */ (event.data); |
| 153 if (header.isInline && !header.hasSourceURL && header.origin !== 'inspector' ) | |
| 154 return; | |
| 155 | |
| 85 var url = header.resourceURL(); | 156 var url = header.resourceURL(); |
| 86 if (!url) | 157 if (!url) |
| 87 return; | 158 return; |
| 88 | 159 |
| 89 var map = this._urlToHeadersByFrameId.get(url); | 160 var uiSourceCode = this._getUISourceCode(header); |
| 90 if (!map) { | 161 if (!uiSourceCode) { |
| 91 map = /** @type {!Map.<string, !Map.<string, !SDK.CSSStyleSheetHeader>>} * / (new Map()); | 162 uiSourceCode = this._createUISourceCode(header); |
| 92 this._urlToHeadersByFrameId.set(url, map); | 163 this._styleFiles.set(uiSourceCode, new Bindings.StyleFile(this._cssModel, uiSourceCode)); |
| 93 } | 164 } |
| 94 var headersById = map.get(header.frameId); | 165 var styleFile = this._styleFiles.get(uiSourceCode); |
| 95 if (!headersById) { | 166 styleFile.addHeader(header); |
| 96 headersById = /** @type {!Map.<string, !SDK.CSSStyleSheetHeader>} */ (new Map()); | 167 |
| 97 map.set(header.frameId, headersById); | 168 Bindings.cssWorkspaceBinding.updateLocations(header); |
| 98 } | |
| 99 headersById.set(header.id, header); | |
| 100 var uiSourceCode = Bindings.NetworkProject.uiSourceCodeForStyleURL(this._wor kspace, url, header); | |
| 101 if (uiSourceCode) | |
| 102 this._bindUISourceCode(uiSourceCode, header); | |
| 103 } | 169 } |
| 104 | 170 |
| 105 /** | 171 /** |
| 106 * @param {!Common.Event} event | 172 * @param {!Common.Event} event |
| 107 */ | 173 */ |
| 108 _styleSheetRemoved(event) { | 174 _styleSheetRemoved(event) { |
| 109 var header = /** @type {!SDK.CSSStyleSheetHeader} */ (event.data); | 175 var header = /** @type {!SDK.CSSStyleSheetHeader} */ (event.data); |
| 110 var url = header.resourceURL(); | 176 var uiSourceCode = this._getUISourceCode(header); |
| 111 if (!url) | 177 if (!uiSourceCode) |
| 112 return; | 178 return; |
| 113 | |
| 114 var map = this._urlToHeadersByFrameId.get(url); | |
| 115 console.assert(map); | |
| 116 var headersById = map.get(header.frameId); | |
| 117 console.assert(headersById); | |
| 118 headersById.delete(header.id); | |
| 119 | |
| 120 if (!headersById.size) { | |
| 121 map.delete(header.frameId); | |
| 122 if (!map.size) { | |
| 123 this._urlToHeadersByFrameId.delete(url); | |
| 124 var uiSourceCode = Bindings.NetworkProject.uiSourceCodeForStyleURL(this. _workspace, url, header); | |
| 125 if (uiSourceCode) | |
| 126 this._unbindUISourceCode(uiSourceCode); | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 /** | |
| 132 * @param {!Workspace.UISourceCode} uiSourceCode | |
| 133 */ | |
| 134 _unbindUISourceCode(uiSourceCode) { | |
| 135 var styleFile = this._styleFiles.get(uiSourceCode); | 179 var styleFile = this._styleFiles.get(uiSourceCode); |
| 136 if (!styleFile) | 180 styleFile.removeHeader(header); |
| 181 if (styleFile.hasHeaders()) | |
| 137 return; | 182 return; |
| 138 styleFile.dispose(); | 183 styleFile.dispose(); |
| 139 this._styleFiles.delete(uiSourceCode); | 184 this._styleFiles.delete(uiSourceCode); |
| 140 } | 185 this._removeUISourceCode(uiSourceCode); |
| 141 | |
| 142 /** | |
| 143 * @param {!Common.Event} event | |
| 144 */ | |
| 145 _unbindAllUISourceCodes(event) { | |
| 146 if (event.data.target() !== this._cssModel.target()) | |
| 147 return; | |
| 148 for (var styleFile of this._styleFiles.values()) | |
| 149 styleFile.dispose(); | |
| 150 this._styleFiles.clear(); | |
| 151 this._urlToHeadersByFrameId = new Map(); | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * @param {!Common.Event} event | |
| 156 */ | |
| 157 _uiSourceCodeAddedToWorkspace(event) { | |
| 158 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data); | |
| 159 if (!this._urlToHeadersByFrameId.has(uiSourceCode.url())) | |
| 160 return; | |
| 161 this._bindUISourceCode( | |
| 162 uiSourceCode, this._urlToHeadersByFrameId.get(uiSourceCode.url()).values Array()[0].valuesArray()[0]); | |
| 163 } | |
| 164 | |
| 165 /** | |
| 166 * @param {!Workspace.UISourceCode} uiSourceCode | |
| 167 * @param {!SDK.CSSStyleSheetHeader} header | |
| 168 */ | |
| 169 _bindUISourceCode(uiSourceCode, header) { | |
| 170 if (this._styleFiles.get(uiSourceCode) || (header.isInline && !header.hasSou rceURL)) | |
| 171 return; | |
| 172 this._styleFiles.set(uiSourceCode, new Bindings.StyleFile(uiSourceCode, this )); | |
| 173 Bindings.cssWorkspaceBinding.updateLocations(header); | |
| 174 } | |
| 175 | |
| 176 /** | |
| 177 * @param {!Common.Event} event | |
| 178 */ | |
| 179 _projectRemoved(event) { | |
| 180 var project = /** @type {!Workspace.Project} */ (event.data); | |
| 181 var uiSourceCodes = project.uiSourceCodes(); | |
| 182 for (var i = 0; i < uiSourceCodes.length; ++i) | |
| 183 this._unbindUISourceCode(uiSourceCodes[i]); | |
| 184 } | |
| 185 | |
| 186 /** | |
| 187 * @param {!Common.Event} event | |
| 188 */ | |
| 189 _uiSourceCodeRemoved(event) { | |
| 190 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data); | |
| 191 this._unbindUISourceCode(uiSourceCode); | |
| 192 } | |
| 193 | |
| 194 /** | |
| 195 * @param {!Workspace.UISourceCode} uiSourceCode | |
| 196 * @param {string} content | |
| 197 * @param {boolean} majorChange | |
| 198 * @return {!Promise<?string>} | |
| 199 */ | |
| 200 _setStyleContent(uiSourceCode, content, majorChange) { | |
| 201 var styleSheetIds = this._cssModel.styleSheetIdsForURL(uiSourceCode.url()); | |
| 202 if (!styleSheetIds.length) | |
| 203 return Promise.resolve(/** @type {?string} */ ('No stylesheet found: ' + u iSourceCode.url())); | |
| 204 | |
| 205 this._isSettingContent = true; | |
| 206 | |
| 207 /** | |
| 208 * @param {?string} error | |
| 209 * @this {Bindings.StylesSourceMapping} | |
| 210 * @return {?string} | |
| 211 */ | |
| 212 function callback(error) { | |
| 213 delete this._isSettingContent; | |
| 214 return error || null; | |
| 215 } | |
| 216 | |
| 217 var promises = []; | |
| 218 for (var i = 0; i < styleSheetIds.length; ++i) | |
| 219 promises.push(this._cssModel.setStyleSheetText(styleSheetIds[i], content, majorChange)); | |
| 220 | |
| 221 return Promise.all(promises).spread(callback.bind(this)); | |
| 222 } | 186 } |
| 223 | 187 |
| 224 /** | 188 /** |
| 225 * @param {!Common.Event} event | 189 * @param {!Common.Event} event |
| 226 */ | 190 */ |
| 227 _styleSheetChanged(event) { | 191 _styleSheetChanged(event) { |
| 228 if (this._isSettingContent) | 192 var styleSheetId = event.data.styleSheetId; |
|
dgozman
2017/02/15 19:57:11
@type ?
lushnikov
2017/02/23 02:02:35
Done.
| |
| 229 return; | |
| 230 | |
| 231 this._updateStyleSheetTextSoon(event.data.styleSheetId); | |
| 232 } | |
| 233 | |
| 234 /** | |
| 235 * @param {!Protocol.CSS.StyleSheetId} styleSheetId | |
| 236 */ | |
| 237 _updateStyleSheetTextSoon(styleSheetId) { | |
| 238 if (this._updateStyleSheetTextTimer) | |
| 239 clearTimeout(this._updateStyleSheetTextTimer); | |
| 240 | |
| 241 this._updateStyleSheetTextTimer = setTimeout( | |
| 242 this._updateStyleSheetText.bind(this, styleSheetId), Bindings.StylesSour ceMapping.ChangeUpdateTimeoutMs); | |
| 243 } | |
| 244 | |
| 245 /** | |
| 246 * @param {!Protocol.CSS.StyleSheetId} styleSheetId | |
| 247 */ | |
| 248 _updateStyleSheetText(styleSheetId) { | |
| 249 if (this._updateStyleSheetTextTimer) { | |
| 250 clearTimeout(this._updateStyleSheetTextTimer); | |
| 251 delete this._updateStyleSheetTextTimer; | |
| 252 } | |
| 253 | |
| 254 var header = this._cssModel.styleSheetHeaderForId(styleSheetId); | 193 var header = this._cssModel.styleSheetHeaderForId(styleSheetId); |
|
dgozman
2017/02/15 19:57:11
Let's pass a header in an event instead?
lushnikov
2017/02/23 02:02:35
I'll postpone this change - this CL already has a
| |
| 255 if (!header) | 194 if (!header) |
| 256 return; | 195 return; |
| 257 var styleSheetURL = header.resourceURL(); | 196 var uiSourceCode = this._getUISourceCode(header); |
| 258 if (!styleSheetURL) | 197 var styleFile = uiSourceCode ? this._styleFiles.get(uiSourceCode) : null; |
|
dgozman
2017/02/15 19:57:11
if (!uiSourceCode) return;
lushnikov
2017/02/23 02:02:35
Done.
| |
| 259 return; | 198 if (styleFile) |
| 260 var uiSourceCode = Bindings.NetworkProject.uiSourceCodeForStyleURL(this._wor kspace, styleSheetURL, header); | 199 styleFile.styleSheetChanged(header); |
| 261 if (!uiSourceCode) | |
| 262 return; | |
| 263 header.requestContent().then(callback.bind(this, uiSourceCode)); | |
| 264 | |
| 265 /** | |
| 266 * @param {!Workspace.UISourceCode} uiSourceCode | |
| 267 * @param {?string} content | |
| 268 * @this {Bindings.StylesSourceMapping} | |
| 269 */ | |
| 270 function callback(uiSourceCode, content) { | |
| 271 var styleFile = this._styleFiles.get(uiSourceCode); | |
| 272 if (styleFile) | |
| 273 styleFile.addRevision(content || ''); | |
| 274 } | |
| 275 } | 200 } |
| 276 | 201 |
| 277 dispose() { | 202 dispose() { |
| 203 for (var uiSourceCode of this._styleFiles.keys()) { | |
| 204 this._removeUISourceCode(uiSourceCode); | |
| 205 var styleFile = this._styleFiles.get(uiSourceCode); | |
| 206 styleFile.dispose(); | |
| 207 } | |
| 208 this._styleFiles.clear(); | |
| 278 Common.EventTarget.removeEventListeners(this._eventListeners); | 209 Common.EventTarget.removeEventListeners(this._eventListeners); |
| 279 } | 210 } |
| 280 }; | 211 }; |
| 281 | 212 |
| 282 Bindings.StylesSourceMapping.ChangeUpdateTimeoutMs = 200; | |
| 283 | |
| 284 /** | 213 /** |
| 285 * @unrestricted | 214 * @unrestricted |
| 286 */ | 215 */ |
| 287 Bindings.StyleFile = class { | 216 Bindings.StyleFile = class { |
| 288 /** | 217 /** |
| 218 * @param {!SDK.CSSModel} cssModel | |
| 289 * @param {!Workspace.UISourceCode} uiSourceCode | 219 * @param {!Workspace.UISourceCode} uiSourceCode |
| 290 * @param {!Bindings.StylesSourceMapping} mapping | |
| 291 */ | 220 */ |
| 292 constructor(uiSourceCode, mapping) { | 221 constructor(cssModel, uiSourceCode) { |
| 222 this._cssModel = cssModel; | |
| 293 this._uiSourceCode = uiSourceCode; | 223 this._uiSourceCode = uiSourceCode; |
| 294 this._mapping = mapping; | 224 /** @type {!Set<!SDK.CSSStyleSheetHeader>} */ |
| 225 this._headers = new Set(); | |
| 295 this._eventListeners = [ | 226 this._eventListeners = [ |
| 296 this._uiSourceCode.addEventListener( | 227 this._uiSourceCode.addEventListener( |
| 297 Workspace.UISourceCode.Events.WorkingCopyChanged, this._workingCopyCha nged, this), | 228 Workspace.UISourceCode.Events.WorkingCopyChanged, this._workingCopyCha nged, this), |
| 298 this._uiSourceCode.addEventListener( | 229 this._uiSourceCode.addEventListener( |
| 299 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyC ommitted, this) | 230 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyC ommitted, this) |
| 300 ]; | 231 ]; |
| 301 this._commitThrottler = new Common.Throttler(Bindings.StyleFile.updateTimeou t); | 232 this._commitThrottler = new Common.Throttler(Bindings.StyleFile.updateTimeou t); |
| 302 this._terminated = false; | 233 this._terminated = false; |
| 303 } | 234 } |
| 304 | 235 |
| 305 /** | 236 /** |
| 237 * @param {!SDK.CSSStyleSheetHeader} header | |
| 238 */ | |
| 239 addHeader(header) { | |
| 240 this._headers.add(header); | |
| 241 } | |
| 242 | |
| 243 /** | |
| 244 * @param {!SDK.CSSStyleSheetHeader} header | |
| 245 */ | |
| 246 removeHeader(header) { | |
| 247 this._headers.delete(header); | |
| 248 } | |
| 249 | |
| 250 /** | |
| 251 * @return {boolean} | |
| 252 */ | |
| 253 hasHeaders() { | |
| 254 return !!this._headers.size; | |
| 255 } | |
| 256 | |
| 257 /** | |
| 258 * @param {!SDK.CSSStyleSheetHeader} header | |
| 259 */ | |
| 260 styleSheetChanged(header) { | |
| 261 if (this._isSettingStyleSheetContents) | |
| 262 return; | |
| 263 | |
| 264 this._commitThrottler.schedule(this._syncStyleSheetChange.bind(this, header) , false); | |
| 265 } | |
| 266 | |
| 267 /** | |
| 306 * @param {!Common.Event} event | 268 * @param {!Common.Event} event |
| 307 */ | 269 */ |
| 308 _workingCopyCommitted(event) { | 270 _workingCopyCommitted(event) { |
| 309 if (this._isAddingRevision) | 271 if (this._isAddingRevision) |
| 310 return; | 272 return; |
| 311 | 273 |
| 312 this._isMajorChangePending = true; | 274 this._isMajorChangePending = true; |
| 313 this._commitThrottler.schedule(this._commitIncrementalEdit.bind(this), true) ; | 275 this._commitThrottler.schedule(this._syncUISourceCodeChange.bind(this), true ); |
| 314 } | 276 } |
| 315 | 277 |
| 316 /** | 278 /** |
| 317 * @param {!Common.Event} event | 279 * @param {!Common.Event} event |
| 318 */ | 280 */ |
| 319 _workingCopyChanged(event) { | 281 _workingCopyChanged(event) { |
| 320 if (this._isAddingRevision) | 282 if (this._isAddingRevision) |
| 321 return; | 283 return; |
| 322 | 284 |
| 323 this._commitThrottler.schedule(this._commitIncrementalEdit.bind(this), false ); | 285 this._commitThrottler.schedule(this._syncUISourceCodeChange.bind(this), fals e); |
| 324 } | 286 } |
| 325 | 287 |
| 326 _commitIncrementalEdit() { | 288 /** |
| 289 * @param {!SDK.CSSStyleSheetHeader} header | |
| 290 * @return {!Promise} | |
| 291 */ | |
| 292 _syncStyleSheetChange(header) { | |
| 293 if (this._terminated || !this._headers.has(header)) | |
| 294 return Promise.resolve(); | |
| 295 | |
| 296 return header.requestContent().then(onHeaderContent.bind(this)); | |
| 297 | |
| 298 /** | |
| 299 * @param {?string} content | |
| 300 * @return {!Promise} | |
| 301 * @this {Bindings.StyleFile} | |
| 302 */ | |
| 303 function onHeaderContent(content) { | |
| 304 if (this._terminated || content === null) | |
| 305 return Promise.resolve(); | |
| 306 this._isAddingRevision = true; | |
| 307 this._uiSourceCode.addRevision(content); | |
| 308 delete this._isAddingRevision; | |
| 309 return this._setStyleSheetContents(content, header, true); | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 /** | |
| 314 * @return {!Promise} | |
| 315 */ | |
| 316 _syncUISourceCodeChange() { | |
| 327 if (this._terminated) | 317 if (this._terminated) |
| 328 return; | 318 return Promise.resolve(); |
| 329 var promise = | 319 var promise = this._setStyleSheetContents(this._uiSourceCode.workingCopy(), null, this._isMajorChangePending); |
| 330 this._mapping._setStyleContent(this._uiSourceCode, this._uiSourceCode.wo rkingCopy(), this._isMajorChangePending) | |
| 331 .then(this._styleContentSet.bind(this)); | |
| 332 this._isMajorChangePending = false; | 320 this._isMajorChangePending = false; |
| 333 return promise; | 321 return promise; |
| 334 } | 322 } |
| 335 | 323 |
| 336 /** | 324 /** |
| 337 * @param {?string} error | 325 * @param {string} content |
| 326 * @param {?SDK.CSSStyleSheetHeader} skipStyleSheet | |
| 327 * @param {boolean} majorChange | |
| 328 * @return {!Promise} | |
| 338 */ | 329 */ |
| 339 _styleContentSet(error) { | 330 _setStyleSheetContents(content, skipStyleSheet, majorChange) { |
| 340 if (error) | 331 this._isSettingStyleSheetContents = true; |
| 341 console.error(error); | 332 var promises = []; |
| 333 for (var header of this._headers) { | |
| 334 if (header === skipStyleSheet) | |
| 335 continue; | |
| 336 promises.push(this._cssModel.setStyleSheetText(header.id, content, majorCh ange)); | |
| 337 } | |
| 338 | |
| 339 return Promise.all(promises).then(onStyleSheetContentsUpdated.bind(this)); | |
| 340 | |
| 341 /** | |
| 342 * @param {!Array<?Protocol.Error>} errors | |
| 343 * @this {Bindings.StyleFile} | |
| 344 */ | |
| 345 function onStyleSheetContentsUpdated(errors) { | |
| 346 delete this._isSettingStyleSheetContents; | |
| 347 errors = errors.filter(error => !!error); | |
|
dgozman
2017/02/15 19:57:11
errors.filter(error => !!error).map(console.error)
lushnikov
2017/02/23 02:02:35
Done.
| |
| 348 for (var error of errors) | |
| 349 console.error(error); | |
| 350 this._styleFileSyncedForTest(); | |
| 351 } | |
| 342 } | 352 } |
| 343 | 353 |
| 344 /** | 354 _styleFileSyncedForTest() { |
| 345 * @param {string} content | |
| 346 */ | |
| 347 addRevision(content) { | |
| 348 this._isAddingRevision = true; | |
| 349 this._uiSourceCode.addRevision(content); | |
| 350 delete this._isAddingRevision; | |
| 351 } | 355 } |
| 352 | 356 |
| 353 dispose() { | 357 dispose() { |
| 354 if (this._terminated) | 358 if (this._terminated) |
| 355 return; | 359 return; |
| 356 this._terminated = true; | 360 this._terminated = true; |
| 357 Common.EventTarget.removeEventListeners(this._eventListeners); | 361 Common.EventTarget.removeEventListeners(this._eventListeners); |
| 358 } | 362 } |
| 359 }; | 363 }; |
| 360 | 364 |
| 361 Bindings.StyleFile.updateTimeout = 200; | 365 Bindings.StyleFile.updateTimeout = 200; |
| OLD | NEW |