CSSWorkspaceBinding.js 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. export default class CSSWorkspaceBinding{constructor(targetManager,workspace){this._workspace=workspace;this._modelToInfo=new Map();this._sourceMappings=[];targetManager.observeModels(SDK.CSSModel,this);}
  2. modelAdded(cssModel){this._modelToInfo.set(cssModel,new ModelInfo(cssModel,this._workspace));}
  3. modelRemoved(cssModel){this._modelToInfo.get(cssModel)._dispose();this._modelToInfo.delete(cssModel);}
  4. updateLocations(header){this._modelToInfo.get(header.cssModel())._updateLocations(header);}
  5. createLiveLocation(rawLocation,updateDelegate,locationPool){return this._modelToInfo.get(rawLocation.cssModel())._createLiveLocation(rawLocation,updateDelegate,locationPool);}
  6. propertyUILocation(cssProperty,forName){const style=cssProperty.ownerStyle;if(!style||style.type!==SDK.CSSStyleDeclaration.Type.Regular||!style.styleSheetId){return null;}
  7. const header=style.cssModel().styleSheetHeaderForId(style.styleSheetId);if(!header){return null;}
  8. const range=forName?cssProperty.nameRange():cssProperty.valueRange();if(!range){return null;}
  9. const lineNumber=range.startLine;const columnNumber=range.startColumn;const rawLocation=new SDK.CSSLocation(header,header.lineNumberInSource(lineNumber),header.columnNumberInSource(lineNumber,columnNumber));return this.rawLocationToUILocation(rawLocation);}
  10. rawLocationToUILocation(rawLocation){for(let i=this._sourceMappings.length-1;i>=0;--i){const uiLocation=this._sourceMappings[i].rawLocationToUILocation(rawLocation);if(uiLocation){return uiLocation;}}
  11. return this._modelToInfo.get(rawLocation.cssModel())._rawLocationToUILocation(rawLocation);}
  12. uiLocationToRawLocations(uiLocation){for(let i=this._sourceMappings.length-1;i>=0;--i){const rawLocations=this._sourceMappings[i].uiLocationToRawLocations(uiLocation);if(rawLocations.length){return rawLocations;}}
  13. const rawLocations=[];for(const modelInfo of this._modelToInfo.values()){rawLocations.pushAll(modelInfo._uiLocationToRawLocations(uiLocation));}
  14. return rawLocations;}
  15. addSourceMapping(sourceMapping){this._sourceMappings.push(sourceMapping);}}
  16. class SourceMapping{rawLocationToUILocation(rawLocation){}
  17. uiLocationToRawLocations(uiLocation){}}
  18. class ModelInfo{constructor(cssModel,workspace){this._eventListeners=[cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetAdded,this._styleSheetAdded,this),cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetRemoved,this._styleSheetRemoved,this)];this._stylesSourceMapping=new Bindings.StylesSourceMapping(cssModel,workspace);const sourceMapManager=cssModel.sourceMapManager();this._sassSourceMapping=new Bindings.SASSSourceMapping(cssModel.target(),sourceMapManager,workspace);this._locations=new Platform.Multimap();this._unboundLocations=new Platform.Multimap();}
  19. _createLiveLocation(rawLocation,updateDelegate,locationPool){const location=new LiveLocation(rawLocation,this,updateDelegate,locationPool);const header=rawLocation.header();if(header){location._header=header;this._locations.set(header,location);location.update();}else{this._unboundLocations.set(rawLocation.url,location);}
  20. return location;}
  21. _disposeLocation(location){if(location._header){this._locations.delete(location._header,location);}else{this._unboundLocations.delete(location._url,location);}}
  22. _updateLocations(header){for(const location of this._locations.get(header)){location.update();}}
  23. _styleSheetAdded(event){const header=(event.data);if(!header.sourceURL){return;}
  24. for(const location of this._unboundLocations.get(header.sourceURL)){location._header=header;this._locations.set(header,location);location.update();}
  25. this._unboundLocations.deleteAll(header.sourceURL);}
  26. _styleSheetRemoved(event){const header=(event.data);for(const location of this._locations.get(header)){location._header=null;this._unboundLocations.set(location._url,location);location.update();}
  27. this._locations.deleteAll(header);}
  28. _rawLocationToUILocation(rawLocation){let uiLocation=null;uiLocation=uiLocation||this._sassSourceMapping.rawLocationToUILocation(rawLocation);uiLocation=uiLocation||this._stylesSourceMapping.rawLocationToUILocation(rawLocation);uiLocation=uiLocation||Bindings.resourceMapping.cssLocationToUILocation(rawLocation);return uiLocation;}
  29. _uiLocationToRawLocations(uiLocation){let rawLocations=this._sassSourceMapping.uiLocationToRawLocations(uiLocation);if(rawLocations.length){return rawLocations;}
  30. rawLocations=this._stylesSourceMapping.uiLocationToRawLocations(uiLocation);if(rawLocations.length){return rawLocations;}
  31. return Bindings.resourceMapping.uiLocationToCSSLocations(uiLocation);}
  32. _dispose(){Common.EventTarget.removeEventListeners(this._eventListeners);this._stylesSourceMapping.dispose();this._sassSourceMapping.dispose();}}
  33. class LiveLocation extends Bindings.LiveLocationWithPool{constructor(rawLocation,info,updateDelegate,locationPool){super(updateDelegate,locationPool);this._url=rawLocation.url;this._lineNumber=rawLocation.lineNumber;this._columnNumber=rawLocation.columnNumber;this._info=info;this._header=null;}
  34. uiLocation(){if(!this._header){return null;}
  35. const rawLocation=new SDK.CSSLocation(this._header,this._lineNumber,this._columnNumber);return Bindings.cssWorkspaceBinding.rawLocationToUILocation(rawLocation);}
  36. dispose(){super.dispose();this._info._disposeLocation(this);}
  37. isBlackboxed(){return false;}}
  38. self.Bindings=self.Bindings||{};Bindings=Bindings||{};Bindings.CSSWorkspaceBinding=CSSWorkspaceBinding;Bindings.CSSWorkspaceBinding.SourceMapping=SourceMapping;Bindings.CSSWorkspaceBinding.ModelInfo=ModelInfo;Bindings.cssWorkspaceBinding;