MCPcopy
hub / github.com/GrapesJS/grapesjs / CssComposer

Class CssComposer

packages/core/src/css_composer/index.ts:84–649  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82}
83
84export default class CssComposer extends ItemManagerModule<CssComposerConfig & { pStylePrefix?: string }> {
85 classes = {
86 CssRule,
87 CssRules,
88 CssRuleView,
89 CssRulesView,
90 };
91 rules: CssRules;
92 rulesView?: CssRulesView;
93 events = CssEvents;
94
95 Selectors = Selectors;
96
97 storageKey = 'styles';
98 protected _itemCache = new Map<string, CssRule>();
99 /**
100 * Initializes module. Automatically called with a new instance of the editor
101 * @param {Object} config Configurations
102 * @private
103 */
104 constructor(em: EditorModel) {
105 super(em, 'CssComposer', null, CssEvents, defConfig());
106 const { config } = this;
107
108 const ppfx = config.pStylePrefix;
109 if (ppfx) config.stylePrefix = ppfx + config.stylePrefix;
110
111 // @ts-ignore
112 config.rules = this.em.config.style || config.rules || '';
113
114 this.rules = new CssRules([], config);
115 this.all = this.rules as any;
116 this._setupCacheListeners();
117 }
118
119 protected override _setupCacheListeners() {
120 super._setupCacheListeners();
121 this.em.listenTo(this.rules, 'change:selectors change:state change:mediaText', this._onItemKeyChange.bind(this));
122 }
123
124 protected _makeCacheKey(rule: CssRule) {
125 const atRuleKey = rule.getAtRule();
126 const selectorsKey = rule.selectorsToString();
127 return `${atRuleKey}__${selectorsKey}`;
128 }
129
130 _makeCacheKeyFromProps(ruleProps: CssRuleProperties) {
131 const { atRuleType = '', mediaText = '', state = '', selectorsAdd = '', selectors = [] } = ruleProps;
132
133 const selectorsStr = selectors.map((selector) => (isString(selector) ? selector : selector.toString())).join('');
134
135 const selectorsRes = [];
136 selectorsStr && selectorsRes.push(`${selectorsStr}${state ? `:${state}` : ''}`);
137 selectorsAdd && selectorsRes.push(selectorsAdd);
138 const selectorsKey = selectorsRes.join(', ');
139
140 const typeStr = atRuleType ? `@${atRuleType}` : mediaText ? '@media' : '';
141 const atRuleKey = typeStr + (mediaText && typeStr ? ` ${mediaText}` : '');

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected