canvasengine 2.0.0-beta.1 → 2.0.0-beta.2

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.
@@ -1,180 +0,0 @@
1
- {
2
- function generateError(message, location) {
3
- const { start, end } = location;
4
- const errorMessage = `${message}\n` +
5
- `at line ${start.line}, column ${start.column} to line ${end.line}, column ${end.column}`;
6
- throw new Error(errorMessage);
7
- }
8
- }
9
-
10
- start
11
- = _ elements:(element)* _ {
12
- if (elements.length === 1) {
13
- return elements[0];
14
- }
15
- return `[${elements.join(',')}]`;
16
- }
17
-
18
- element
19
- = forLoop
20
- / ifCondition
21
- / selfClosingElement
22
- / openCloseElement
23
- / comment
24
-
25
- selfClosingElement
26
- = _ "<" _ tagName:tagName _ attributes:attributes _ "/>" _ {
27
- const attrs = attributes.length > 0 ? `{ ${attributes.join(', ')} }` : null;
28
- return attrs ? `h(${tagName}, ${attrs})` : `h(${tagName})`;
29
- }
30
-
31
- openCloseElement
32
- = "<" _ tagName:tagName _ attributes:attributes _ ">" _ content:content _ "</" _ closingTagName:tagName _ ">" {
33
- if (tagName !== closingTagName) {
34
- error("Mismatched opening and closing tags");
35
- }
36
- const attrs = attributes.length > 0 ? `{ ${attributes.join(', ')} }` : null;
37
- const children = content ? content : null;
38
- if (attrs && children) {
39
- return `h(${tagName}, ${attrs}, ${children})`;
40
- } else if (attrs) {
41
- return `h(${tagName}, ${attrs})`;
42
- } else if (children) {
43
- return `h(${tagName}, null, ${children})`;
44
- } else {
45
- return `h(${tagName})`;
46
- }
47
- }
48
-
49
- / "<" _ tagName:tagName _ attributes:attributes _ {
50
- generateError("Syntaxe d'élément invalide", location());
51
- }
52
-
53
- attributes
54
- = attrs:(attribute (_ attribute)*)? {
55
- return attrs
56
- ? [attrs[0]].concat(attrs[1].map(a => a[1]))
57
- : [];
58
- }
59
-
60
- attribute
61
- = staticAttribute
62
- / dynamicAttribute
63
- / eventHandler
64
-
65
- eventHandler
66
- = "@" eventName:identifier _ "=" _ "{" _ handlerName:attributeValue _ "}" {
67
- return `${eventName}: ${handlerName}`;
68
- }
69
- / "@" eventName:attributeName _ {
70
- return eventName;
71
- }
72
-
73
- dynamicAttribute
74
- = attributeName:attributeName _ "=" _ "{" _ attributeValue:attributeValue _ "}" {
75
- if (attributeValue.trim().match(/^[a-zA-Z_]\w*$/)) {
76
- return `${attributeName}: ${attributeValue}`;
77
- } else {
78
- return `${attributeName}: computed(() => ${attributeValue.replace(/@?[a-zA-Z_][a-zA-Z0-9_]*(?!:)/g, (match) => {
79
- if (match.startsWith('@')) {
80
- return match.substring(1);
81
- }
82
- return `${match}()`;
83
- })})`;
84
- }
85
- }
86
- / attributeName:attributeName _ {
87
- return attributeName;
88
- }
89
-
90
- attributeValue
91
- = $([^{}]* ("{" [^{}]* "}" [^{}]*)*) {
92
- const t = text().trim()
93
- if (t.startsWith("{") && t.endsWith("}")) {
94
- return `(${t})`;
95
- }
96
- return t
97
- }
98
-
99
- staticAttribute
100
- = attributeName:attributeName _ "=" _ "\"" attributeValue:staticValue "\"" {
101
- return `${attributeName}: ${attributeValue}`;
102
- }
103
-
104
- eventAttribute
105
- = "(" _ eventName:eventName _ ")" _ "=" _ "\"" eventAction:eventAction "\"" {
106
- return `${eventName}: () => { ${eventAction} }`;
107
- }
108
-
109
- staticValue
110
- = [^"]+ {
111
- var val = text();
112
- return isNaN(val) ? `'${val}'` : val;
113
- }
114
-
115
- content
116
- = elements:(element)* {
117
- const filteredElements = elements.filter(el => el !== null);
118
- if (filteredElements.length === 0) return null;
119
- if (filteredElements.length === 1) return filteredElements[0];
120
- return `[${filteredElements.join(', ')}]`;
121
- }
122
-
123
- textNode
124
- = text:$([^<]+) {
125
- const trimmed = text.trim();
126
- return trimmed ? `'${trimmed}'` : null;
127
- }
128
-
129
- textElement
130
- = text:[^<>]+ {
131
- const trimmed = text.join('').trim();
132
- return trimmed ? JSON.stringify(trimmed) : null;
133
- }
134
-
135
- forLoop
136
- = _ "@for" _ "(" _ variableName:identifier _ "of" _ iterable:identifier _ ")" _ "{" _ content:content _ "}" _ {
137
- return `loop(${iterable}, (${variableName}) => ${content})`;
138
- }
139
-
140
- ifCondition
141
- = _ "@if" _ "(" _ condition:condition _ ")" _ "{" _ content:content _ "}" _ {
142
- return `cond(${condition}, () => ${content})`;
143
- }
144
-
145
- tagName
146
- = [a-zA-Z][a-zA-Z0-9]* { return text(); }
147
-
148
- attributeName
149
- = [a-zA-Z][a-zA-Z0-9-]* { return text(); }
150
-
151
- eventName
152
- = [a-zA-Z][a-zA-Z0-9-]* { return text(); }
153
-
154
- variableName
155
- = [a-zA-Z_][a-zA-Z0-9_]* { return text(); }
156
-
157
- iterable
158
- = [a-zA-Z_][a-zA-Z0-9_]* { return text(); }
159
-
160
- condition
161
- = $([^)]*) { return text().trim(); }
162
-
163
- eventAction
164
- = [^"]* { return text(); }
165
-
166
- _ 'whitespace'
167
- = [ \t\n\r]*
168
-
169
- identifier
170
- = [a-zA-Z_][a-zA-Z0-9_]* { return text(); }
171
-
172
- comment
173
- = singleComment+ {
174
- return null
175
- }
176
-
177
- singleComment
178
- = "<!--" _ content:((!("-->") .)* "-->") _ {
179
- return null;
180
- }
@@ -1,166 +0,0 @@
1
- import { createFilter } from "vite";
2
- import { parse } from "acorn";
3
- import fs from "fs";
4
- import pkg from "peggy";
5
- import path from "path";
6
- import * as ts from "typescript"; // Import TypeScript package
7
- import { fileURLToPath } from 'url';
8
-
9
- const { generate } = pkg;
10
-
11
- const DEV_SRC = "../../src"
12
-
13
- export default function canvasengine() {
14
- const filter = createFilter("**/*.ce");
15
-
16
- // Convert import.meta.url to a file path
17
- const __filename = fileURLToPath(import.meta.url);
18
- const __dirname = path.dirname(__filename);
19
-
20
- const grammar = fs.readFileSync(
21
- path.join(__dirname, "grammar.pegjs").replace("dist/compiler/grammar.pegjs", "src/compiler/grammar.pegjs"),
22
- "utf8");
23
- const parser = generate(grammar);
24
- const isDev = process.env.NODE_ENV === "development";
25
- const FLAG_COMMENT = "/*--[TPL]--*/";
26
-
27
- const PRIMITIVE_COMPONENTS = [
28
- "Canvas",
29
- "Sprite",
30
- "Text",
31
- "Joystick",
32
- "Viewport",
33
- "Graphics",
34
- "Container",
35
- "ImageMap",
36
- "NineSliceSprite",
37
- "Rect",
38
- "Circle",
39
- "svg"
40
- ];
41
-
42
- return {
43
- name: "vite-plugin-ce",
44
- transform(code: string, id: string) {
45
- if (!filter(id)) return;
46
-
47
- // Extract the script content
48
- const scriptMatch = code.match(/<script>([\s\S]*?)<\/script>/);
49
- let scriptContent = scriptMatch ? scriptMatch[1].trim() : "";
50
-
51
- // Transform SVG tags to Svg components
52
- let template = code.replace(/<script>[\s\S]*?<\/script>/, "")
53
- .replace(/^\s+|\s+$/g, '');
54
-
55
- // Add SVG transformation
56
- template = template.replace(/<svg>([\s\S]*?)<\/svg>/g, (match, content) => {
57
- return `<Svg content="${content.trim()}" />`;
58
- });
59
-
60
- const parsedTemplate = parser.parse(template);
61
-
62
- // trick to avoid typescript remove imports in scriptContent
63
- scriptContent += FLAG_COMMENT + parsedTemplate
64
-
65
- let transpiledCode = ts.transpileModule(scriptContent, {
66
- compilerOptions: {
67
- module: ts.ModuleKind.Preserve,
68
- },
69
- }).outputText;
70
-
71
- // remove code after /*---*/
72
- transpiledCode = transpiledCode.split(FLAG_COMMENT)[0]
73
-
74
- // Use Acorn to parse the script content
75
- const parsed = parse(transpiledCode, {
76
- sourceType: "module",
77
- ecmaVersion: 2020,
78
- });
79
-
80
- // Extract imports
81
- const imports = parsed.body.filter(
82
- (node) => node.type === "ImportDeclaration"
83
- );
84
-
85
- // Extract non-import statements from scriptContent
86
- const nonImportCode = parsed.body
87
- .filter((node) => node.type !== "ImportDeclaration")
88
- .map((node) => transpiledCode.slice(node.start, node.end))
89
- .join("\n");
90
-
91
- let importsCode = imports
92
- .map((imp) => {
93
- let importCode = transpiledCode.slice(imp.start, imp.end);
94
- if (isDev && importCode.includes("from 'canvasengine'")) {
95
- importCode = importCode.replace(
96
- "from 'canvasengine'",
97
- `from '${DEV_SRC}'`
98
- );
99
- }
100
- return importCode;
101
- })
102
- .join("\n");
103
-
104
- // Define an array for required imports
105
- const requiredImports = ["h", "computed", "cond", "loop"];
106
-
107
- // Check for missing imports
108
- const missingImports = requiredImports.filter(
109
- (importName) =>
110
- !imports.some(
111
- (imp) =>
112
- imp.specifiers &&
113
- imp.specifiers.some(
114
- (spec) =>
115
- spec.type === "ImportSpecifier" &&
116
- spec.imported &&
117
- 'name' in spec.imported &&
118
- spec.imported.name === importName
119
- )
120
- )
121
- );
122
-
123
- // Add missing imports
124
- if (missingImports.length > 0) {
125
- const additionalImportCode = `import { ${missingImports.join(
126
- ", "
127
- )} } from ${isDev ? `'${DEV_SRC}'` : "'canvasengine'"};`;
128
- importsCode = `${additionalImportCode}\n${importsCode}`;
129
- }
130
-
131
- // Check for primitive components in parsedTemplate
132
- const primitiveImports = PRIMITIVE_COMPONENTS.filter((component) =>
133
- parsedTemplate.includes(`h(${component}`)
134
- );
135
-
136
- // Add missing imports for primitive components
137
- primitiveImports.forEach((component) => {
138
- const importStatement = `import { ${component} } from ${
139
- isDev ? `'${DEV_SRC}'` : "'canvasengine'"
140
- };`;
141
- if (!importsCode.includes(importStatement)) {
142
- importsCode = `${importStatement}\n${importsCode}`;
143
- }
144
- });
145
-
146
- // Generate the output
147
- const output = String.raw`
148
- ${importsCode}
149
- import { useProps, useDefineProps } from ${isDev ? `'${DEV_SRC}'` : "'canvasengine'"}
150
-
151
- export default function component($$props) {
152
- const $props = useProps($$props)
153
- const defineProps = useDefineProps($$props)
154
- ${nonImportCode}
155
- let $this = ${parsedTemplate}
156
- return $this
157
- }
158
- `;
159
-
160
- return {
161
- code: output,
162
- map: null,
163
- };
164
- },
165
- };
166
- }
@@ -1,89 +0,0 @@
1
- import { Graphics } from "../components";
2
- import { h } from "../engine/signal";
3
- import * as PIXI from "pixi.js";
4
- import { useProps } from "../hooks/useProps";
5
-
6
- interface BarProps {
7
- backgroundColor?: string;
8
- foregroundColor?: string;
9
- value: number;
10
- maxValue: number;
11
- width: number;
12
- height: number;
13
- }
14
-
15
- function componentToHex(c) {
16
- var hex = c.toString(16);
17
- return hex.length == 1 ? "0" + hex : hex;
18
- }
19
-
20
- function rgbToHex(r, g, b) {
21
- return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
22
- }
23
-
24
- export function Bar(opts: BarProps) {
25
- const {
26
- width,
27
- height,
28
- value,
29
- maxValue,
30
- backgroundColor,
31
- foregroundColor,
32
- border,
33
- innerMargin,
34
- borderRadius,
35
- } = useProps(opts, {
36
- backgroundColor: "#000000",
37
- foregroundColor: "#FFFFFF",
38
- innerMargin: 0,
39
- borderRadius: 0,
40
- });
41
-
42
- return h(
43
- Graphics,
44
- {
45
- ...opts,
46
- width,
47
- height,
48
- draw(g: PIXI.Graphics) {
49
- if (borderRadius()) {
50
- g.roundRect(0, 0, width(), height(), borderRadius());
51
- } else {
52
- g.rect(0, 0, width(), height());
53
- }
54
- if (border) {
55
- g.stroke(border);
56
- }
57
- g.fill(backgroundColor());
58
- },
59
- },
60
- h(Graphics, {
61
- width,
62
- height,
63
- draw(g: PIXI.Graphics) {
64
- const margin = innerMargin();
65
- const _borderRadius = borderRadius();
66
- const w = Math.max(
67
- 0,
68
- Math.min(
69
- width() - 2 * margin,
70
- (value() / maxValue()) * (width() - 2 * margin)
71
- )
72
- );
73
- const h = height() - 2 * margin;
74
- if (borderRadius) {
75
- g.roundRect(margin, margin, w, h, _borderRadius);
76
- } else {
77
- g.rect(margin, margin, w, h);
78
- }
79
- const color = foregroundColor();
80
- if (color.startsWith("rgba")) {
81
- const [r, g, b, a] = color.match(/\d+(\.\d+)?/g).map(Number);
82
- g.fill({ color: rgbToHex(r, g, b), alpha: a });
83
- } else {
84
- g.fill(color);
85
- }
86
- },
87
- })
88
- );
89
- }
File without changes