Skip to content

Commit

Permalink
feat: add append prepend before after insertBefore method to Node
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Mar 5, 2025
1 parent f181b3d commit 6c0c2f0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
20 changes: 13 additions & 7 deletions docs/src/testure3.ts → docs/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { fonts } from 'modern-font'
import {
Engine,
Node,
Timeline,
} from '../../src'

const engine = new Engine({
autoStart: true,
autoResize: true,
backgroundColor: '#F6F7F9',
timeline: Timeline.from([0, 5000], true),
})

document.body.append(engine.view!)

async function init(): Promise<void> {
fonts.fallbackFont = await fonts.load({ family: 'fallbackFont', src: '/fonts/AaHouDiHei.woff' })

const root = engine.root
const node1 = new Node()
const node2 = new Node()
const node3 = new Node()
engine.root.appendChild(node1)
const node4 = new Node()
const node5 = new Node()
const node6 = new Node()
const node7 = new Node()
const node8 = new Node()
root.appendChild(node1)
node1.addSibling(node2)
node1.addSibling(node3)
root.append(node4)
root.insertBefore(node5, node4)
node5.before(node6)
node5.after(node7)
root.prepend(node8)

console.warn(engine.root.getChildren().map(v => v.name))
}
Expand Down
2 changes: 1 addition & 1 deletion src/scene/2d/Node2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class Node2D extends CanvasItem {
}

protected override _process(delta: number): void {
super._process(delta)
const parent = this.getParent<Node2D>()
if (this._parentTransformDirtyId !== parent?.globalTransform?.dirtyId) {
this.requestRelayout()
}
super._process(delta)
}
}
11 changes: 11 additions & 0 deletions src/scene/main/CanvasItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ export class CanvasItem extends TimelineNode {
})
}

protected override _process(delta: number): void {
super._process(delta)
const parent = this.getParent<CanvasItem>()
if (this._parentGlobalVisible !== parent?.globalVisible) {
this.requestUpdate()
}
if (this._parentGlobalOpacity !== parent?.globalOpacity) {
this.requestUpdate()
}
}

protected override _update(): void {
const parent = this.getParent<CanvasItem>()

Expand Down
57 changes: 55 additions & 2 deletions src/scene/main/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,23 @@ export class Node extends CoreObject {
return this
}

append(nodes: Node[]): void
append(...nodes: Node[]): void
prepend<T extends Node>(nodes: T[]): void
prepend<T extends Node>(...nodes: T[]): void
prepend(...nodes: any[]): void {
let _nodes
if (Array.isArray(nodes[0])) {
_nodes = nodes[0]
}
else {
_nodes = nodes
}
_nodes.forEach((node) => {
this.moveChild(node, 0)
})
}

append<T extends Node>(nodes: T[]): void
append<T extends Node>(...nodes: T[]): void
append(...nodes: any[]): void {
let _nodes
if (Array.isArray(nodes[0])) {
Expand All @@ -369,6 +384,44 @@ export class Node extends CoreObject {
})
}

before<T extends Node>(nodes: T[]): void
before<T extends Node>(...nodes: T[]): void
before(...nodes: any[]): void {
let _nodes
if (Array.isArray(nodes[0])) {
_nodes = nodes[0]
}
else {
_nodes = nodes
}
_nodes.forEach((node) => {
this._parent?.moveChild(node, this.getIndex(true))
})
}

after<T extends Node>(nodes: T[]): void
after<T extends Node>(...nodes: T[]): void
after(...nodes: any[]): void {
let _nodes
if (Array.isArray(nodes[0])) {
_nodes = nodes[0]
}
else {
_nodes = nodes
}
_nodes.forEach((node) => {
this._parent?.moveChild(node, this.getIndex(true) + 1)
})
}

insertBefore<T extends Node>(node: T, child: Node): T {
if (!child.hasParent() || !this.is(child.parent)) {
return node
}
this.moveChild(node, child.getIndex(true))
return node
}

appendChild<T extends Node>(node: T, internalMode = node.internalMode): T {
if (this.is(node) || node.hasParent()) {
return node
Expand Down

0 comments on commit 6c0c2f0

Please sign in to comment.