Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed May 4, 2024
1 parent 94133a7 commit 6fd9830
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function addChild(
}
const isObject = typeof child === 'object';
if (isObject && $nodes in child) {
child[$nodes].forEach((node, i) => {
child[$nodes].forEach((node: Node, i: number) => {
addChild(element, node, destructors, index + i);
});
} else if (isPrimitive(child)) {
Expand Down Expand Up @@ -352,7 +352,7 @@ function _DOM(
ctx: any,
): Node {
NODE_COUNTER++;
const element = api.element(tag, '', ctx, tagProps);
const element = api.element(tag);
if (IS_DEV_MODE) {
$DEBUG_REACTIVE_CONTEXTS.push(`${tag}`);
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/renderers/dom/dom-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export const api = {
fragment() {
return $doc.createDocumentFragment();
},
element(tagName = '', namespace = '', ctx: any = null, props: Props = [[],[],[]]): HTMLElement {
// @ts-expect-error
element(tagName = '', namespace?: string, ctx?: any, props?: Props): HTMLElement {
return $doc.createElement(tagName);
},
append(
Expand Down
26 changes: 20 additions & 6 deletions src/utils/renderers/tres/tres-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import { catalogue } from './catalogue'
import { Props } from '@/utils/types'
import { isFn } from '@/utils/shared'

function noop(fn: string): any {
// eslint-disable-next-line no-unused-expressions
fn
}

let scene: TresScene | null = null

const { logError } = {
logError() {
console.log(...arguments);
logError(msg: string) {
console.log(msg);
}
}

Expand All @@ -32,11 +28,14 @@ export const api = {
let props = {};
let args = _props[1];
args.forEach((arg) => {
// @ts-expect-error
props[arg[0]] = arg[1];
});
if (!props) { props = {} }

// @ts-expect-error
if (!props.args) {
// @ts-expect-error
props.args = []
}
if (tag === 'template') { return null }
Expand All @@ -45,9 +44,12 @@ export const api = {
let instance

if (tag === 'primitive') {
// @ts-expect-error
if (props?.object === undefined) { logError('Tres primitives need a prop \'object\'') }
// @ts-expect-error
const object = props.object as TresObject
name = object.type
// @ts-expect-error
instance = Object.assign(object, { type: name, attach: props.attach, primitive: true })
}
else {
Expand All @@ -56,18 +58,22 @@ export const api = {
logError(`${name} is not defined on the THREE namespace. Use extend to add it to the catalog.`)
}
// eslint-disable-next-line new-cap
// @ts-expect-error
instance = new target(...props.args)
}

if (instance.isCamera) {
// @ts-expect-error
if (!props?.position) {
instance.position.set(3, 3, 3)
}
// @ts-expect-error
if (!props?.lookAt) {
instance.lookAt(0, 0, 0)
}
}

// @ts-expect-error
if (props?.attach === undefined) {
if (instance.isMaterial) { instance.attach = 'material' }
else if (instance.isBufferGeometry) { instance.attach = 'geometry' }
Expand All @@ -77,7 +83,9 @@ export const api = {
// prevent it's disposal when node is removed later in it's lifecycle

if (instance.isObject3D) {
// @ts-expect-error
if (props?.material?.isMaterial) { (instance as TresObject3D).userData.tres__materialViaProp = true }
// @ts-expect-error
if (props?.geometry?.isBufferGeometry) { (instance as TresObject3D).userData.tres__geometryViaProp = true }
}

Expand All @@ -90,6 +98,7 @@ export const api = {

return instance
},
// @ts-expect-error
append(parent, child) {
if (parent && parent.isScene) { scene = parent as unknown as TresScene }

Expand Down Expand Up @@ -125,6 +134,7 @@ export const api = {
}
}
},
// @ts-expect-error
destroy(node) {
if (!node) { return }
// remove is only called on the node being removed and not on child nodes.
Expand Down Expand Up @@ -184,6 +194,7 @@ export const api = {

node.dispose?.()
},
// @ts-expect-error
prop(node, prop, nextValue) {
if (node) {
let root = node
Expand Down Expand Up @@ -222,9 +233,11 @@ export const api = {
// Traverse pierced props (e.g. foo-bar=value => foo.bar = value)
if (key.includes('-') && target === undefined) {
const chain = key.split('-')
// @ts-expect-error
target = chain.reduce((acc, key) => acc[kebabToCamel(key)], root)
key = chain.pop() as string
finalKey = key.toLowerCase()
// @ts-expect-error
if (!target?.set) { root = chain.reduce((acc, key) => acc[kebabToCamel(key)], root) }
}
let value = nextValue
Expand All @@ -245,6 +258,7 @@ export const api = {
else { target.set(value) }
}
},
// @ts-expect-error
parentNode(node) {
return node?.parent || null
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/renderers/tres/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export function createHighlightMesh(object: Object3D): Mesh {
})
// Clone the geometry of the object. You might need a more complex approach
// if the object's geometry is not straightforward.
// @ts-expect-error
const highlightMesh = new HightlightMesh(object.geometry.clone(), highlightMaterial)

return highlightMesh
Expand Down

0 comments on commit 6fd9830

Please sign in to comment.