Skip to content

Commit dcbef9c

Browse files
committed
Migrate to TS
1 parent bd4801c commit dcbef9c

File tree

5 files changed

+88
-12
lines changed

5 files changed

+88
-12
lines changed

package-lock.json

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"devDependencies": {
3838
"@babel/preset-typescript": "^7.18.6",
3939
"eslint": "^8.21.0",
40+
"grapesjs": "^0.19.5",
4041
"grapesjs-cli": "^3.0.1",
4142
"jest": "^28.1.3"
4243
},

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import type grapesjs from 'grapesjs';
12
import parser from './parser';
23

3-
export default (editor) => {
4+
const plugin: grapesjs.Plugin = (editor) => {
45
editor.setCustomParserCss(parser);
56
};
7+
8+
export default plugin;

src/parser.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
import postcss from 'postcss';
1+
import type grapesjs from 'grapesjs';
2+
import postcss, { Rule, AtRule, Declaration } from 'postcss';
3+
4+
export type ParsedRule = {
5+
selectors: string;
6+
style: Record<string, string>;
7+
atRule?: string;
8+
params?: string;
9+
}
10+
11+
type Editor = grapesjs.Editor;
212

313
/**
414
* Log stuff
515
* @param {Editor} editor
616
* @param {*} msg
717
*/
8-
export const log = (editor, msg) =>
18+
export const log = (editor: Editor, msg: any) =>
919
editor && editor.log(msg, { ns: 'parser-poscss' });
1020

1121
/**
1222
* Create rule from node
1323
* @param {Object} node
1424
* @return {Object}
1525
*/
16-
export const createRule = node => {
17-
const declarations = node.nodes || [];
18-
const style = {};
26+
export const createRule = (node: Rule): ParsedRule => {
27+
const declarations = (node.nodes as Declaration[]) || [];
28+
const style: Record<string, string> = {};
1929

2030
declarations.forEach(({ prop, value, important }) => {
2131
style[prop] = value + (important ? ' !important' : '');
@@ -33,28 +43,29 @@ export const createRule = node => {
3343
* @param {Array<Object>} result
3444
* @return {Object}
3545
*/
36-
export const createAtRule = (node, result) => {
46+
export const createAtRule = (node: AtRule, result: ParsedRule[]) => {
3747
const { name, params } = node;
3848
const isNested = ['media', 'keyframes'].indexOf(name) >= 0;
3949

4050
if (isNested) {
4151
node.nodes.forEach(node => {
4252
result.push({
43-
...createRule(node),
53+
...createRule(node as Rule),
4454
atRule: name,
4555
params,
4656
})
4757
});
4858
} else {
4959
result.push({
50-
...createRule(node),
60+
// @ts-ignore
61+
...createRule(node as Rule),
5162
atRule: name,
5263
})
5364
}
5465
};
5566

56-
export default (css, editor) => {
57-
const result = [];
67+
export default (css: string, editor: Editor) => {
68+
const result: ParsedRule[] = [];
5869
log(editor, ['Input CSS', css]);
5970

6071
const ast = postcss().process(css).sync().root;

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./node_modules/grapesjs-cli/src/template/tsconfig.json",
3+
"include": ["src"]
4+
}

0 commit comments

Comments
 (0)