Skip to content

Commit 2a4a322

Browse files
committed
fix problam & move example to file
1 parent 2ca290d commit 2a4a322

15 files changed

+44
-50
lines changed
File renamed without changes.
File renamed without changes.

index.css example/index.css

File renamed without changes.

index.html example/index.html

File renamed without changes.

index.ts example/index.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { startEngine, data, createEffect, css, makeFunc } from "@bayn/miniframe"
1+
import { startEngine, data, createEffect, css, makeFunc } from "../lib/index"
22

33
data.color = 'blue'
44
data.a = 1;
@@ -18,14 +18,14 @@ startEngine(() => [`
1818
color:$color
1919
</div>
2020
<div class="$textColor">
21-
color:$textColor
21+
textColor:$textColor
2222
</div>
2323
<label>
24-
<div class="round">
24+
<div class="round">
2525
color:$textColor
2626
</div>
2727
</label>
28-
<a click="switchColor">switchColor</a>
28+
<button click="switchColor">switchColor</button>
2929
</div>`,
3030
css`
3131
.blue {
@@ -44,8 +44,5 @@ startEngine(() => [`
4444
border-radius: 50%;
4545
color: white;
4646
}
47-
a{
48-
cursor: pointer;
49-
}
5047
`]
5148
)

lib/compiler/html.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { data } from "../index"
33
const htmlCompile = (htmlString: string) => {
44
const parser = new DOMParser();
55
const replaceAttrs = ['class', 'id', 'click']
6-
// 解析HTML字符串为DOM对象
6+
// should replace frame own func
77
const dom = parser.parseFromString(htmlString, 'text/html');
8+
// have to be
89
let target = dom.childNodes[0].childNodes[1].childNodes[0]
910
const replaceParms = (target: HTMLElement) => {
10-
target.childNodes.forEach((res) => {
11+
(target.childNodes as unknown as HTMLElement[]).forEach((res) => {
1112
if (res.nodeType === 3 && res.textContent?.trim() == '') return;
1213
if (res.nodeType === 3) {
1314
(res.textContent as string | undefined) = res.textContent?.replace(/\$(.*?)\s/g, (match) => {
@@ -16,17 +17,17 @@ const htmlCompile = (htmlString: string) => {
1617
}
1718
if (res.nodeType === 1) {
1819
replaceAttrs.forEach((attr) => {
19-
if ((res as HTMLElement).getAttribute(attr)) {
20-
(res as HTMLElement).setAttribute(attr, (res as HTMLElement).getAttribute(attr)!.replace(/\$(.*?)\s/g, (match) => {
20+
if (res.getAttribute(attr)) {
21+
res.setAttribute(attr, res.getAttribute(attr)!.replace(/\$(.*?)\s/g, (match) => {
2122
return String(data[match.slice(1, match.length - 1)])
2223
}))
2324
if (attr === 'click') {
24-
(res as HTMLElement).onclick = data.func[(res as HTMLElement).getAttribute(attr)!]
25+
res.onclick = data.func[res.getAttribute(attr)!]
2526
}
2627
}
2728
})
2829
}
29-
replaceParms(res as HTMLElement)
30+
replaceParms(res)
3031
})
3132
}
3233
replaceParms(target as HTMLElement)

lib/css/compile.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import type { dataParams } from "../type/global"
12
/**
23
* @description Render css
34
* @param tokens TemplateStringsArray
45
* @param expressions (string|number)[]
56
*/
6-
function css(tokens: TemplateStringsArray, ...expressions: (string | number)[]) {
7+
function css(tokens: TemplateStringsArray, ...expressions: (dataParams)[]) {
78
const cssString = tokens.map((token, index) => {
89
return (expressions[index - 1] ?? '') + token;
910
}).join('');

lib/engine/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const startEngine = (render: () => [string, HTMLStyleElement], el?: string) => {
1515
let html = htmlCompile(dom[0])
1616
if (container!.firstElementChild) {
1717
container!.childNodes.forEach(child => child.remove())
18-
container!.firstElementChild.replaceWith(html, dom[1])
18+
container!.firstElementChild.replaceWith(html!, dom[1])
1919
} else {
20-
container!.appendChild(html)
20+
container!.appendChild(html!)
2121
container!.appendChild(dom[1])
2222
}
2323
})

lib/reactive/data.ts

-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ const propsToEffects: any = {}
22
const dirtyEffects: any = []
33
let queued = false
44
let currentEffect: Function | undefined = undefined;
5-
6-
type data = {
7-
func: {
8-
[key: string]: (this: GlobalEventHandlers, ev: MouseEvent) => any;
9-
}
10-
} & {
11-
[key: string]: string | number;
12-
};
13-
145
/**
156
* @description reative data, can refresh dom.
167
*/

lib/type/global.d.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
type clickEvent = (this: GlobalEventHandlers, ev: MouseEvent) => any
2+
3+
type data = {
4+
func: {
5+
[key: string]: (this: GlobalEventHandlers, ev: MouseEvent) => any;
6+
}
7+
} & {
8+
[key: string]: dataParams;
9+
};
10+
11+
type dataParams = string | number | Date | boolean | null | undefined | Function | (() => void)
12+
13+
214
export {
3-
clickEvent
15+
clickEvent,
16+
data,
17+
dataParams
418
}

lib/type/package.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ declare module '@bayn/miniframe' {
44
function createEffect(effect: Function);
55
function makeFunc(name: string, cb: clickEvent);
66
const data: data;
7-
}
7+
}
8+
9+
type data = {
10+
func: {
11+
[key: string]: (this: GlobalEventHandlers, ev: MouseEvent) => any;
12+
}
13+
} & {
14+
[key: string]: string | number | Date | boolean | null | undefined | Function | (() => void);
15+
};

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "@bayn/miniframe",
3-
"version": "0.0.1-beta.3",
3+
"version": "0.0.1-beta.4",
44
"repository": {
55
"type": "git",
66
"url": "[email protected]:Bayn-Web/miniframe.git"
77
},
8-
"description": "",
8+
"description": "the minimal reactive front-end frame by signal engine",
99
"type": "module",
1010
"types": "./lib/package.d.ts",
1111
"module": "./lib/index.ts",
1212
"scripts": {
13-
"dev": "vite --open",
13+
"dev": "vite --open ./example/index.html",
1414
"build": "vite build"
1515
},
1616
"keywords": [

pnpm-lock.yaml

-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"lib/**/*.ts",
2424
"lib/*.ts",
2525
"*.ts"
26-
]
26+
, "example/index.ts" ]
2727
}

vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
lib: {
66
entry: './lib/index.ts',
77
name: 'miniFrame',
8-
fileName: `miniFrame.js`,
8+
fileName: `miniFrame`,
99
},
1010
},
1111
});

0 commit comments

Comments
 (0)