revert: $Node.parent
This commit is contained in:
defaultkavy 2024-04-24 20:57:01 +08:00
parent ec62a580b6
commit d28953d354
5 changed files with 50 additions and 45 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
extensions
.npmignore
bun.lockb
node_modules

View File

@ -3,39 +3,41 @@ import { $Node } from "./node/$Node";
import { $Text } from "./node/$Text";
export class $NodeManager {
#container: $Container;
elementList = new Set<$Node>
$container: $Container;
$elementList = new Set<$Node>
constructor(container: $Container) {
this.#container = container;
this.$container = container;
}
add(element: $Node | string) {
if (typeof element === 'string') {
const text = new $Text(element);
this.elementList.add(text);
this.$elementList.add(text);
(text as Mutable<$Node>).parent = this.$container;
} else {
this.elementList.add(element);
this.$elementList.add(element);
(element as Mutable<$Node>).parent = this.$container;
}
}
remove(element: $Node) {
if (!this.elementList.has(element)) return this;
this.elementList.delete(element);
if (!this.$elementList.has(element)) return this;
this.$elementList.delete(element);
(element as Mutable<$Node>).parent = undefined;
return this;
}
removeAll(render = true) {
this.elementList.forEach(ele => this.remove(ele));
this.$elementList.forEach(ele => this.remove(ele));
if (render) this.render();
}
replace(target: $Node, replace: $Node) {
const array = this.array.map(node => {
if (node === target) return replace;
else return node;
})
this.elementList.clear();
array.forEach(node => this.elementList.add(node));
const array = this.array
array.splice(array.indexOf(target), 1, replace);
target.remove();
this.$elementList.clear();
array.forEach(node => this.$elementList.add(node));
return this;
}
@ -58,7 +60,7 @@ export class $NodeManager {
}
}
get array() {return [...this.elementList.values()]};
get array() {return [...this.$elementList.values()]};
get dom() {return this.#container.dom}
get dom() {return this.$container.dom}
}

View File

@ -1,5 +1,4 @@
import { $State } from "./$State";
import { $AsyncNode } from "./node/$AsyncNode";
import { $Container } from "./node/$Container";
import { $Document } from "./node/$Document";
import { $Node } from "./node/$Node";

View File

@ -5,6 +5,7 @@ export abstract class $Node<N extends Node = Node> {
abstract readonly dom: N;
readonly __hidden: boolean = false;
private domEvents: {[key: string]: Map<Function, Function>} = {};
readonly parent?: $Container;
on<K extends keyof HTMLElementEventMap>(type: K, callback: (event: HTMLElementEventMap[K], $node: this) => any, options?: AddEventListenerOptions | boolean) {
if (!this.domEvents[type]) this.domEvents[type] = new Map()
@ -30,12 +31,12 @@ export abstract class $Node<N extends Node = Node> {
}
hide(): boolean;
hide(hide?: boolean | $State<boolean>): this;
hide(hide?: boolean | $State<boolean>) { return $.fluent(this, arguments, () => this.__hidden, () => {
hide(hide?: boolean | $State<boolean>, render?: boolean): this;
hide(hide?: boolean | $State<boolean>, render = true) { return $.fluent(this, arguments, () => this.__hidden, () => {
if (hide === undefined) return;
if (hide instanceof $State) { (this as Mutable<$Node>).__hidden = hide.value; hide.use(this, 'hide')}
else (this as Mutable<$Node>).__hidden = hide;
this.parent?.children.render();
if (render) this.parent?.children.render();
return this;
})}
@ -64,8 +65,4 @@ export abstract class $Node<N extends Node = Node> {
if (this instanceof $Element) return true;
else return false;
}
get parent() {
return this.dom.parentElement?.$ as $Container | undefined;
}
}

View File

@ -1,22 +1,25 @@
{
"name": "fluentx",
"description": "Fast, fluent, simple web builder",
"version": "0.0.9",
"type": "module",
"module": "index.ts",
"author": {
"name": "defaultkavy",
"email": "defaultkavy@gmail.com",
"url": "https://github.com/defaultkavy"
},
"keywords": ["web", "front-end", "lib", "fluent", "framework"],
"homepage": "https://github.com/defaultkavy/fluentx",
"repository": {
"type": "git",
"url": "git+https://github.com/defaultkavy/fluentx.git"
},
"bugs": {
"url": "https://github.com/defaultkavy/fluentx/issues"
},
"license": "ISC"
"name": "fluentx",
"version": "0.0.9",
"author": {
"name": "defaultkavy",
"email": "defaultkavy@gmail.com",
"url": "https://github.com/defaultkavy"
},
"repository": {
"type": "git",
"url": "git+https://github.com/defaultkavy/fluentx.git"
},
"module": "index.ts",
"bugs": {
"url": "https://github.com/defaultkavy/fluentx/issues"
},
"description": "Fast, fluent, simple web builder",
"homepage": "https://github.com/defaultkavy/fluentx",
"keywords": ["web", "front-end", "lib", "fluent", "framework"],
"license": "ISC",
"type": "module",
"workspaces": [
"extensions/*"
]
}