@node-projects/web-component-designer-stylesheetservice-css-parser 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# web-component-designer-stylesheetservice-css-tools
|
|
2
|
+
|
|
3
|
+
## NPM Package
|
|
4
|
+
|
|
5
|
+
https://www.npmjs.com/package/@node-projects/web-component-designer-stylesheetservice-css-tools
|
|
6
|
+
|
|
7
|
+
npm i @node-projects/web-component-designer-stylesheetservice-css-tools
|
|
8
|
+
|
|
9
|
+
## Description
|
|
10
|
+
|
|
11
|
+
This is a Stylesheetparser using @adobe/css-tools
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
import { CssToolsStylesheetService } from '@node-projects/web-component-designer-stylesheetservice-css-tools';
|
|
16
|
+
serviceContainer.register("stylesheetService", designerCanvas => new CssToolsStylesheetService(designerCanvas));
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CssDeclarationAST, CssRuleAST, CssStylesheetAST } from "@node-projects/css-parser";
|
|
2
|
+
import { AbstractStylesheetService, IStyleRule, IStylesheet, IStyleDeclaration, IDesignerCanvas, IDesignItem } from "@node-projects/web-component-designer";
|
|
3
|
+
import { Specificity } from "@node-projects/web-component-designer";
|
|
4
|
+
interface IRuleWithAST extends IStyleRule {
|
|
5
|
+
ast: CssRuleAST;
|
|
6
|
+
declarations: IDeclarationWithAST[];
|
|
7
|
+
stylesheet: IStylesheet;
|
|
8
|
+
stylesheetName: string;
|
|
9
|
+
specificity: Specificity;
|
|
10
|
+
}
|
|
11
|
+
interface IDeclarationWithAST extends IStyleDeclaration {
|
|
12
|
+
ast: CssDeclarationAST;
|
|
13
|
+
}
|
|
14
|
+
export declare class CssToolsStylesheetService extends AbstractStylesheetService {
|
|
15
|
+
constructor(designerCanvas: IDesignerCanvas);
|
|
16
|
+
internalParse(style: string): Promise<CssStylesheetAST>;
|
|
17
|
+
getAppliedRules(designItem: IDesignItem): IRuleWithAST[];
|
|
18
|
+
getRules(selector: string): IStyleRule[];
|
|
19
|
+
addRule(stylesheet: IStylesheet, selector: string): Promise<IStyleRule>;
|
|
20
|
+
private getRulesFromAst;
|
|
21
|
+
getDeclarations(designItem: IDesignItem, styleName: string): IStyleDeclaration[];
|
|
22
|
+
updateDeclarationValueWithoutUndo(declaration: IDeclarationWithAST, value: string, important: boolean): void;
|
|
23
|
+
insertDeclarationIntoRule(rule: IRuleWithAST, property: string, value: string, important: boolean): boolean;
|
|
24
|
+
removeDeclarationFromRule(rule: IRuleWithAST, property: string): boolean;
|
|
25
|
+
private updateStylesheet;
|
|
26
|
+
updateCompleteStylesheet(name: string, newStyle: string): Promise<void>;
|
|
27
|
+
updateCompleteStylesheetWithoutUndo(name: string, newStyle: string, noUndo?: boolean): Promise<void>;
|
|
28
|
+
private updateCompleteStylesheetInternal;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { parse, stringify } from "@node-projects/css-parser";
|
|
2
|
+
import { AbstractStylesheetService } from "@node-projects/web-component-designer";
|
|
3
|
+
export class CssToolsStylesheetService extends AbstractStylesheetService {
|
|
4
|
+
constructor(designerCanvas) {
|
|
5
|
+
super(designerCanvas);
|
|
6
|
+
}
|
|
7
|
+
async internalParse(style) {
|
|
8
|
+
return parse(style);
|
|
9
|
+
}
|
|
10
|
+
getAppliedRules(designItem) {
|
|
11
|
+
let rules = [];
|
|
12
|
+
for (let item of this._allStylesheets.entries()) {
|
|
13
|
+
if (!item[1].ast?.stylesheet?.rules)
|
|
14
|
+
continue;
|
|
15
|
+
let rs = Array.from(this.getRulesFromAst(item[1].ast?.stylesheet?.rules, item[1].stylesheet, designItem))
|
|
16
|
+
.map(x => ({
|
|
17
|
+
selector: x[0].selectors.join(', '),
|
|
18
|
+
declarations: x[0].declarations.filter(y => y.type == 'declaration').map(y => ({
|
|
19
|
+
name: y.property,
|
|
20
|
+
value: y.value.endsWith('!important') ? y.value.substring(0, y.value.length - 10).trimEnd() : y.value,
|
|
21
|
+
important: y.value.endsWith('!important'),
|
|
22
|
+
parent: null,
|
|
23
|
+
ast: y,
|
|
24
|
+
stylesheet: item[1].stylesheet
|
|
25
|
+
})),
|
|
26
|
+
specificity: x[1],
|
|
27
|
+
stylesheetName: item[0],
|
|
28
|
+
stylesheet: item[1].stylesheet,
|
|
29
|
+
ast: x[0],
|
|
30
|
+
}));
|
|
31
|
+
rs.forEach(x => x.declarations.forEach(y => y.parent = x));
|
|
32
|
+
rules.push(...rs);
|
|
33
|
+
}
|
|
34
|
+
return rules;
|
|
35
|
+
}
|
|
36
|
+
getRules(selector) {
|
|
37
|
+
const rules = this.getAppliedRules(null);
|
|
38
|
+
return rules.filter(x => x.selector == selector);
|
|
39
|
+
}
|
|
40
|
+
async addRule(stylesheet, selector) {
|
|
41
|
+
await this.updateCompleteStylesheet(stylesheet.name, stylesheet.content + '\n' + selector + ' {}');
|
|
42
|
+
const rules = this.getRules(selector).filter(x => x.stylesheetName == stylesheet.name);
|
|
43
|
+
return rules[rules.length - 1];
|
|
44
|
+
}
|
|
45
|
+
*getRulesFromAst(cssAtRuleAst, stylesheet, designItem) {
|
|
46
|
+
for (const atRule of cssAtRuleAst) {
|
|
47
|
+
if (atRule.type == 'media') {
|
|
48
|
+
yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem);
|
|
49
|
+
}
|
|
50
|
+
else if (atRule.type == 'supports') {
|
|
51
|
+
yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem);
|
|
52
|
+
}
|
|
53
|
+
else if (atRule.type == 'rule') {
|
|
54
|
+
let spec = this.elementMatchesASelector(designItem, atRule.selectors);
|
|
55
|
+
if (spec)
|
|
56
|
+
yield [atRule, spec];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
getDeclarations(designItem, styleName) {
|
|
62
|
+
return this.getAppliedRules(designItem).flatMap(x => x.declarations).filter(x => x.name == styleName);
|
|
63
|
+
}
|
|
64
|
+
updateDeclarationValueWithoutUndo(declaration, value, important) {
|
|
65
|
+
declaration.ast.value = important ? value + ' !important' : value;
|
|
66
|
+
let ss = this._allStylesheets.get(declaration.parent.stylesheetName);
|
|
67
|
+
this.updateStylesheet(ss);
|
|
68
|
+
/*
|
|
69
|
+
declaration.ast.value = important ? value + ' !important' : value;
|
|
70
|
+
let ss = declaration.ast;
|
|
71
|
+
while (ss?.parent)
|
|
72
|
+
ss = ss?.parent;
|
|
73
|
+
let obj = { ast: ss, stylesheet: declaration.stylesheet };
|
|
74
|
+
this._allStylesheets.set(declaration.parent.stylesheetName, obj)
|
|
75
|
+
this.updateStylesheet(obj);
|
|
76
|
+
*/
|
|
77
|
+
}
|
|
78
|
+
insertDeclarationIntoRule(rule, property, value, important) {
|
|
79
|
+
rule.ast.declarations.push({
|
|
80
|
+
type: 'declaration',
|
|
81
|
+
property: property,
|
|
82
|
+
value: important ? value + ' !important' : value
|
|
83
|
+
});
|
|
84
|
+
this.updateStylesheet(this._allStylesheets.get(rule.stylesheetName));
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
removeDeclarationFromRule(rule, property) {
|
|
88
|
+
let idx = rule.ast.declarations.findIndex(x => x.property == property);
|
|
89
|
+
if (idx == -1)
|
|
90
|
+
return false;
|
|
91
|
+
rule.ast.declarations.splice(idx, 1);
|
|
92
|
+
this.updateStylesheet(this._allStylesheets.get(rule.stylesheetName));
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
updateStylesheet(ss) {
|
|
96
|
+
const old = ss.stylesheet.content;
|
|
97
|
+
ss.stylesheet.content = stringify(ss.ast, { indent: ' ', compress: false });
|
|
98
|
+
if (ss.stylesheet.designItem) {
|
|
99
|
+
ss.stylesheet.designItem.content = ss.stylesheet.content;
|
|
100
|
+
}
|
|
101
|
+
else
|
|
102
|
+
this.stylesheetChanged.emit({ name: ss.stylesheet.name, newStyle: ss.stylesheet.content, oldStyle: old, changeSource: 'styleupdate' });
|
|
103
|
+
}
|
|
104
|
+
async updateCompleteStylesheet(name, newStyle) {
|
|
105
|
+
this.updateCompleteStylesheetInternal(name, newStyle, 'styleupdate');
|
|
106
|
+
}
|
|
107
|
+
async updateCompleteStylesheetWithoutUndo(name, newStyle, noUndo = false) {
|
|
108
|
+
this.updateCompleteStylesheetInternal(name, newStyle, 'undo');
|
|
109
|
+
}
|
|
110
|
+
async updateCompleteStylesheetInternal(name, newStyle, changeSource) {
|
|
111
|
+
const ss = this._allStylesheets.get(name);
|
|
112
|
+
if (ss.stylesheet.content != newStyle) {
|
|
113
|
+
const old = ss.stylesheet.content;
|
|
114
|
+
ss.stylesheet.content = newStyle;
|
|
115
|
+
if (changeSource == 'styleupdate') {
|
|
116
|
+
ss.ast = await this.internalParse(ss.stylesheet.content);
|
|
117
|
+
}
|
|
118
|
+
if (ss.stylesheet.designItem) {
|
|
119
|
+
ss.stylesheet.designItem.content = ss.stylesheet.content;
|
|
120
|
+
}
|
|
121
|
+
else
|
|
122
|
+
this.stylesheetChanged.emit({ name: ss.stylesheet.name, newStyle: ss.stylesheet.content, oldStyle: old, changeSource });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "web-component-designer addon: Stylesheet Parser Service using @node-projects/css-parser",
|
|
3
|
+
"name": "@node-projects/web-component-designer-stylesheetservice-css-parser",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/service/stylesheetservice/CssToolsStylesheetService.js",
|
|
7
|
+
"author": "jochen.kuehner@gmx.de",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"tsc": "tsc",
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"link": "npm link",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@node-projects/css-parser": "^5.0.0",
|
|
17
|
+
"@node-projects/web-component-designer": ">=0.1.224"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/node-projects/web-component-designer.git"
|
|
22
|
+
}
|
|
23
|
+
}
|