UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 4.97 kB
import*as TextUtils from"../../models/text_utils/text_utils.js";import*as Common from"../common/common.js";import*as HostModule from"../host/host.js";import*as Platform from"../platform/platform.js";import{cssMetadata,GridAreaRowRegex}from"./CSSMetadata.js";export class CSSProperty{ownerStyle;index;name;value;important;disabled;parsedOk;implicit;text;range;#t;#e;#n;#r;#i=[];constructor(t,e,n,r,i,s,a,o,l,h,d){if(this.ownerStyle=t,this.index=e,this.name=n,this.value=r,this.important=i,this.disabled=s,this.parsedOk=a,this.implicit=o,this.text=l,this.range=h?TextUtils.TextRange.TextRange.fromObject(h):null,this.#t=!0,this.#e=null,this.#n=null,d&&d.length>0)for(const n of d)this.#i.push(new CSSProperty(t,++e,n.name,n.value,i,s,a,!0));else{const r=cssMetadata().getLonghands(n);for(const n of r||[])this.#i.push(new CSSProperty(t,++e,n,"",i,s,a,!0))}}static parsePayload(t,e,n){return new CSSProperty(t,e,n.name,n.value,n.important||!1,n.disabled||!1,!("parsedOk"in n)||Boolean(n.parsedOk),Boolean(n.implicit),n.text,n.range,n.longhandProperties)}ensureRanges(){if(this.#e&&this.#n)return;const t=this.range,e=this.text?new TextUtils.Text.Text(this.text):null;if(!t||!e)return;const n=e.value().indexOf(this.name),r=e.value().lastIndexOf(this.value);if(-1===n||-1===r||n>r)return;const i=new TextUtils.TextRange.SourceRange(n,this.name.length),s=new TextUtils.TextRange.SourceRange(r,this.value.length);function a(t,e,n){return 0===t.startLine&&(t.startColumn+=n,t.endColumn+=n),t.startLine+=e,t.endLine+=e,t}this.#e=a(e.toTextRange(i),t.startLine,t.startColumn),this.#n=a(e.toTextRange(s),t.startLine,t.startColumn)}nameRange(){return this.ensureRanges(),this.#e}valueRange(){return this.ensureRanges(),this.#n}rebase(t){this.ownerStyle.styleSheetId===t.styleSheetId&&this.range&&(this.range=this.range.rebaseAfterTextEdit(t.oldRange,t.newRange))}setActive(t){this.#t=t}get propertyText(){return void 0!==this.text?this.text:""===this.name?"":this.name+": "+this.value+(this.important?" !important":"")+";"}activeInStyle(){return this.#t}trimmedValueWithoutImportant(){const t="!important";return this.value.endsWith(t)?this.value.slice(0,-10).trim():this.value.trim()}async setText(t,e,n){if(!this.ownerStyle)throw new Error("No ownerStyle for property");if(!this.ownerStyle.styleSheetId)throw new Error("No owner style id");if(!this.range||!this.ownerStyle.range)throw new Error("Style not editable");if(e&&(HostModule.userMetrics.actionTaken(HostModule.UserMetrics.Action.StyleRuleEdited),this.ownerStyle.parentRule?.isKeyframeRule()&&HostModule.userMetrics.actionTaken(HostModule.UserMetrics.Action.StylePropertyInsideKeyframeEdited),this.name.startsWith("--")&&HostModule.userMetrics.actionTaken(HostModule.UserMetrics.Action.CustomPropertyEdited)),n&&t===this.propertyText)return this.ownerStyle.cssModel().domModel().markUndoableState(!e),!0;const r=this.range.relativeTo(this.ownerStyle.range.startLine,this.ownerStyle.range.startColumn),i=this.ownerStyle.cssText?this.detectIndentation(this.ownerStyle.cssText):Common.Settings.Settings.instance().moduleSetting("textEditorIndent").get(),s=this.ownerStyle.cssText?i.substring(0,this.ownerStyle.range.endColumn):"",a=new TextUtils.Text.Text(this.ownerStyle.cssText||"").replaceRange(r,Platform.StringUtilities.sprintf(";%s;",t)),o=await CSSProperty.formatStyle(a,i,s);return this.ownerStyle.setText(o,e)}static async formatStyle(t,e,n){const r=e.substring(n.length)+e;e&&(e="\n"+e);let i="",s="",a="",o=!1,l=!1;const h=TextUtils.CodeMirrorUtils.createCssTokenizer();return await h("*{"+t+"}",(function(t,n){if(!o){const r=n?.includes("comment")&&function(t){const e=t.indexOf(":");if(-1===e)return!1;const n=t.substring(2,e).trim();return cssMetadata().isCSSPropertyName(n)}(t),s=n?.includes("string")||n?.includes("meta")||n?.includes("property")||n?.includes("variableName")&&"variableName.function"!==n;return r?i=i.trimEnd()+e+t:s?(o=!0,a=t):(";"!==t||l)&&(i+=t,t.trim()&&!n?.includes("comment")&&(l=";"!==t)),void("{"!==t||n||(l=!1))}if("}"===t||";"===t){const n=a.trim();return i=i.trimEnd()+e+n+(n.endsWith(":")?" ":"")+t,l=!1,o=!1,void(s="")}if(cssMetadata().isGridAreaDefiningProperty(s)){const e=GridAreaRowRegex.exec(t);e&&0===e.index&&!a.trimEnd().endsWith("]")&&(a=a.trimEnd()+"\n"+r)}s||":"!==t||(s=a);a+=t})),o&&(i+=a),i=i.substring(2,i.length-1).trimEnd(),i+(e?"\n"+n:"")}detectIndentation(t){const e=t.split("\n");return e.length<2?"":TextUtils.TextUtils.Utils.lineIndent(e[1])}setValue(t,e,n,r){const i=this.name+": "+t+(this.important?" !important":"")+";";this.setText(i,e,n).then(r)}async setDisabled(t){if(!this.ownerStyle)return!1;if(t===this.disabled)return!0;if(!this.text)return!0;const e=this.text.trim(),n=t=>t+(t.endsWith(";")?"":";");let r;return r=t?"/* "+n(e)+" */":n(this.text.substring(2,e.length-2).trim()),this.setText(r,!0,!0)}setDisplayedStringForInvalidProperty(t){this.#r=t}getInvalidStringForInvalidProperty(){return this.#r}getLonghandProperties(){return this.#i}}