lunchboxjs 0.2.1014 → 0.2.1015
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.
- package/dist/lunchboxjs.js +118 -9
- package/dist/lunchboxjs.min.js +1 -1
- package/dist/lunchboxjs.module.js +110 -2
- package/package.json +2 -4
- package/src/core/updateObjectProp.ts +9 -4
- package/src/utils/get.ts +18 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/isNumber.ts +87 -0
- package/src/utils/set.ts +14 -0
package/dist/lunchboxjs.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue'), require('three')
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'vue', 'three'
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Lunchbox = {}, global.vue, global.three
|
|
5
|
-
})(this, (function (exports, vue, THREE
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue'), require('three')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'vue', 'three'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Lunchbox = {}, global.vue, global.three));
|
|
5
|
+
})(this, (function (exports, vue, THREE) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function _interopNamespace(e) {
|
|
8
8
|
if (e && e.__esModule) return e;
|
|
@@ -44,6 +44,115 @@
|
|
|
44
44
|
return null;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
const get = (obj, path, defValue) => {
|
|
48
|
+
// If path is not defined or it has false value
|
|
49
|
+
if (!path) return undefined; // Check if path is string or array. Regex : ensure that we do not have '.' and brackets.
|
|
50
|
+
// Regex explained: https://regexr.com/58j0k
|
|
51
|
+
|
|
52
|
+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g); // Find value
|
|
53
|
+
|
|
54
|
+
const result = pathArray?.reduce((prevObj, key) => prevObj && prevObj[key], obj); // If found value is undefined return default value; otherwise return the value
|
|
55
|
+
|
|
56
|
+
return result === undefined ? defValue : result;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const buildIsNumber = () => {
|
|
60
|
+
/**
|
|
61
|
+
* lodash (Custom Build) <https://lodash.com/>
|
|
62
|
+
* Build: `lodash modularize exports="npm" -o ./`
|
|
63
|
+
* Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
|
|
64
|
+
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
65
|
+
* Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
66
|
+
* Available under MIT license <https://lodash.com/license>
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/** `Object#toString` result references. */
|
|
70
|
+
const numberTag = '[object Number]';
|
|
71
|
+
/** Used for built-in method references. */
|
|
72
|
+
|
|
73
|
+
const objectProto = Object.prototype;
|
|
74
|
+
/**
|
|
75
|
+
* Used to resolve the
|
|
76
|
+
* [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
|
77
|
+
* of values.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
const objectToString = objectProto.toString;
|
|
81
|
+
/**
|
|
82
|
+
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
|
83
|
+
* and has a `typeof` result of "object".
|
|
84
|
+
*
|
|
85
|
+
* @static
|
|
86
|
+
* @memberOf _
|
|
87
|
+
* @since 4.0.0
|
|
88
|
+
* @category Lang
|
|
89
|
+
* @param {*} value The value to check.
|
|
90
|
+
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
|
91
|
+
* @example
|
|
92
|
+
*
|
|
93
|
+
* _.isObjectLike({});
|
|
94
|
+
* // => true
|
|
95
|
+
*
|
|
96
|
+
* _.isObjectLike([1, 2, 3]);
|
|
97
|
+
* // => true
|
|
98
|
+
*
|
|
99
|
+
* _.isObjectLike(_.noop);
|
|
100
|
+
* // => false
|
|
101
|
+
*
|
|
102
|
+
* _.isObjectLike(null);
|
|
103
|
+
* // => false
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
function isObjectLike(value) {
|
|
107
|
+
return !!value && typeof value == 'object';
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Checks if `value` is classified as a `Number` primitive or object.
|
|
111
|
+
*
|
|
112
|
+
* **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are
|
|
113
|
+
* classified as numbers, use the `_.isFinite` method.
|
|
114
|
+
*
|
|
115
|
+
* @static
|
|
116
|
+
* @memberOf _
|
|
117
|
+
* @since 0.1.0
|
|
118
|
+
* @category Lang
|
|
119
|
+
* @param {*} value The value to check.
|
|
120
|
+
* @returns {boolean} Returns `true` if `value` is a number, else `false`.
|
|
121
|
+
* @example
|
|
122
|
+
*
|
|
123
|
+
* _.isNumber(3);
|
|
124
|
+
* // => true
|
|
125
|
+
*
|
|
126
|
+
* _.isNumber(Number.MIN_VALUE);
|
|
127
|
+
* // => true
|
|
128
|
+
*
|
|
129
|
+
* _.isNumber(Infinity);
|
|
130
|
+
* // => true
|
|
131
|
+
*
|
|
132
|
+
* _.isNumber('3');
|
|
133
|
+
* // => false
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
const output = function isNumber(value) {
|
|
138
|
+
return typeof value == 'number' || isObjectLike(value) && objectToString.call(value) == numberTag;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return output;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const isNumber = buildIsNumber();
|
|
145
|
+
|
|
146
|
+
const set = (obj, path, value) => {
|
|
147
|
+
// Regex explained: https://regexr.com/58j0k
|
|
148
|
+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g);
|
|
149
|
+
pathArray?.reduce((acc, key, i) => {
|
|
150
|
+
if (acc[key] === undefined) acc[key] = {};
|
|
151
|
+
if (i === pathArray.length - 1) acc[key] = value;
|
|
152
|
+
return acc[key];
|
|
153
|
+
}, obj);
|
|
154
|
+
};
|
|
155
|
+
|
|
47
156
|
/** Type check on whether target is a Lunchbox.EventKey */
|
|
48
157
|
|
|
49
158
|
const isEventKey = target => {
|
|
@@ -1321,7 +1430,7 @@
|
|
|
1321
1430
|
|
|
1322
1431
|
if (typeof value === 'string' && value.startsWith('$attached')) {
|
|
1323
1432
|
const attachedName = value.replace('$attached.', '');
|
|
1324
|
-
value =
|
|
1433
|
+
value = get(node.attached, attachedName, null);
|
|
1325
1434
|
} // save instance
|
|
1326
1435
|
|
|
1327
1436
|
|
|
@@ -1334,12 +1443,12 @@
|
|
|
1334
1443
|
for (let i = 0; i < nestedPropertiesToCheck.length && !liveProperty; i++) {
|
|
1335
1444
|
const nestedProperty = nestedPropertiesToCheck[i];
|
|
1336
1445
|
const fullPath = [nestedProperty, finalKey].filter(Boolean).join('.');
|
|
1337
|
-
liveProperty = liveProperty =
|
|
1446
|
+
liveProperty = liveProperty = get(target, fullPath);
|
|
1338
1447
|
} // change property
|
|
1339
1448
|
// first, save as array in case we need to spread it
|
|
1340
1449
|
|
|
1341
1450
|
|
|
1342
|
-
if (liveProperty &&
|
|
1451
|
+
if (liveProperty && isNumber(value) && liveProperty?.setScalar) {
|
|
1343
1452
|
// if value is a number and the property has a `setScalar` method, use that
|
|
1344
1453
|
liveProperty.setScalar(value);
|
|
1345
1454
|
} else if (liveProperty && liveProperty.set) {
|
|
@@ -1365,11 +1474,11 @@
|
|
|
1365
1474
|
// ; (parentAsLunchboxNode.instance as any)[finalKey] = result
|
|
1366
1475
|
// }
|
|
1367
1476
|
|
|
1368
|
-
} else if (
|
|
1477
|
+
} else if (get(target, finalKey, undefined) !== undefined) {
|
|
1369
1478
|
// blank strings evaluate to `true`
|
|
1370
1479
|
// <mesh castShadow receiveShadow /> will work the same as
|
|
1371
1480
|
// <mesh :castShadow="true" :receiveShadow="true" />
|
|
1372
|
-
|
|
1481
|
+
set(target, finalKey, value === '' ? true : value);
|
|
1373
1482
|
} else {
|
|
1374
1483
|
// if you see this error in production, you might need to add `finalKey`
|
|
1375
1484
|
// to `internalLunchboxVueKeys` below
|
package/dist/lunchboxjs.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue"),require("three"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","vue","three","lodash"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Lunchbox={},e.vue,e.three,e.lodash)}(this,(function(e,t,n,r){"use strict";function o(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var a=o(n);const i=e=>e?.$el&&e?.$el?.hasOwnProperty?.("instance"),s=e=>"domMeta"===e?.metaType||e?.props?.["data-lunchbox"],c=e=>"standardMeta"===e?.metaType,d=e=>e.isLunchboxRootNode;function l(t={}){const n={text:t.text??""};return new e.MiniDom.RendererCommentNode({...n,...t,metaType:"commentMeta"})}function u(t={}){const n={domElement:document.createElement(t.type??"")};return new e.MiniDom.RendererDomNode({...n,...t,metaType:"domMeta"})}function p(t={}){const n={text:t.text??""};return new e.MiniDom.RendererTextNode({...t,...n,metaType:"textMeta"})}function m(t={},n={}){const r={attached:t.attached??[],attachedArray:t.attachedArray??{},instance:t.instance??null},o=new e.MiniDom.RendererStandardNode({...t,...r,metaType:"standardMeta"});return!o.type||d(o)||o.instance||(o.instance=E({...o,props:{...o.props,...n}})),o}function h({node:e,key:t,interactables:n,value:r}){return e.eventListeners[t]||(e.eventListeners[t]=[]),e.eventListenerRemoveFunctions[t]||(e.eventListenerRemoveFunctions[t]=[]),e.eventListeners[t].push(r),f.includes(t)&&e.instance&&!n.value.includes(e)&&(n.value.push(e),e.eventListenerRemoveFunctions[t].push((()=>{const t=n.value.indexOf(e);-1!==t&&n.value.splice(t,1)}))),e}const f=["onClick","onPointerUp","onPointerDown","onPointerOver","onPointerOut","onPointerEnter","onPointerLeave","onPointerMove"],y=(e,n,r,o,a)=>{if(!n?.domElement||!r||!e)return;const i=(o=o??window.innerWidth)/(a=a??window.innerHeight);if("perspectivecamera"===e.type?.toLowerCase()){const t=e;t.aspect=i,t.updateProjectionMatrix()}else if("orthographiccamera"===e.type?.toLowerCase()){const t=e,n=a/o;t.top=10*n,t.bottom=10*-n,t.right=10,t.left=-10,t.updateProjectionMatrix()}n.setSize(o,a),r&&e&&n.render(t.toRaw(r),t.toRaw(e))},b=(e,t,n,r,o)=>{const a=e.value?.domElement;if(!a)throw new Error("missing container");const i=()=>{if("container"===o){const e=(e=>{const t=getComputedStyle(e);return{width:e.clientWidth-parseFloat(t.paddingLeft)-parseFloat(t.paddingRight),height:e.clientHeight-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom)}})(a);y(t,n,r,e.width,e.height)}else y(t,n,r)};i();let s=new ResizeObserver((()=>{i()}));return a&&s.observe(a),{dispose(){a&&s.unobserve(a)}}},v=t.defineComponent({name:"LunchboxScene",setup:(e,{slots:n})=>()=>t.createVNode(t.resolveComponent("scene"),null,{default:()=>[n.default?.()]})}),g=t.defineComponent({name:"LunchboxEventHandlers",setup(){const e=Le(),n=Pe(),r=t.ref({x:1/0,y:1/0}),o=t.ref(!1);let i=[];const s=new a.Raycaster(new a.Vector3,new a.Vector3(0,0,-1)),c=({element:e,eventKeys:t,intersection:n})=>{e&&t.forEach((t=>{e.eventListeners[t]&&e.eventListeners[t].forEach((e=>{e({intersection:n})}))}))};xe((e=>{if(!e?.domElement)return;const{domElement:t}=e;t.addEventListener("pointermove",(e=>{const o=(t.width??1)/n.dpr,a=(t.height??1)/n.dpr;r.value.x=e.offsetX/o*2-1,r.value.y=-e.offsetY/a*2+1})),t.addEventListener("pointerdown",(()=>o.value=!0)),t.addEventListener("pointerup",(()=>o.value=!1))}));const d=ve(),l=()=>{const t=d.value;if(!t)return;s.setFromCamera(r.value,t);const n=s.intersectObjects(e?.value.map((e=>e.instance))??[]);let o=[],a=[],l=[];o=i.map((e=>e.intersection)),n?.forEach((t=>{if(-1===i.findIndex((e=>e.intersection.object===t.object))){const n=e?.value.find((e=>e.instance?.uuid===t.object.uuid));n&&a.push({element:n,intersection:t})}else{const n=e?.value.find((e=>e.instance?.uuid===t.object.uuid));n&&l.push({element:n,intersection:t})}const n=o.findIndex((e=>e.object.uuid===t.object.uuid));-1!==n&&o.splice(n,1)}));const u=o.map((t=>({element:e?.value.find((e=>e.instance?.uuid===t.object.uuid)),intersection:t})));a.forEach((({element:e,intersection:t})=>{c({element:e,eventKeys:["onPointerEnter"],intersection:t})})),l.forEach((({element:e,intersection:t})=>{c({element:e,eventKeys:["onPointerOver","onPointerMove"],intersection:t})})),u.forEach((({element:e,intersection:t})=>{c({element:e,eventKeys:["onPointerLeave","onPointerOut"],intersection:t})})),i=[].concat(a,l)};re(l);t.onBeforeUnmount((()=>oe(l)));const u=["onClick","onPointerDown","onPointerUp"];return t.watch(o,(e=>{l();const t=[];i.forEach((n=>{u.forEach((r=>{const o=n.element.uuid+r;!e||"onClick"!==r&&"onPointerDown"!==r?e||"onPointerUp"!==r||t.includes(o)||(n.element.eventListeners[r]?.forEach((e=>e({intersection:n.intersection}))),t.push(o)):t.includes(o)||(n.element.eventListeners[r]?.forEach((e=>e({intersection:n.intersection}))),t.push(o))}))}))})),()=>t.createVNode(t.resolveComponent("object3D"),null,null)}}),x=e=>({position:e,top:0,right:0,bottom:0,left:0,width:"100%",height:"100%",display:"block"}),R=t.defineComponent({name:"Lunchbox",props:{background:String,cameraArgs:Array,cameraLook:Array,cameraLookAt:Array,cameraPosition:Array,dpr:Number,ortho:Boolean,orthographic:Boolean,r3f:Boolean,rendererArguments:Object,rendererProperties:Object,sizePolicy:String,shadow:[Boolean,Object],transparent:Boolean,zoom:Number,updateSource:Object},setup(e,n){const r=t.ref();let o=e.dpr??-1;const i=t.ref(),s=t.ref(),c=t.ref(),d=t.ref(),l=Pe(),u=Ce(),p=Ae(),m=t.reactive({}),h=Ee();e.r3f&&a?.ColorManagement&&(a.ColorManagement.legacyMode=!1);const f=Le();t.onMounted((async()=>{if(!r.value&&!n.slots?.renderer?.()?.length)throw new Error("missing canvas");for(n.slots?.camera?.()?.length||(e.cameraPosition&&(m.position=e.cameraPosition),(e.cameraLook||e.cameraLookAt)&&(m.lookAt=e.cameraLook||e.cameraLookAt),void 0!==e.zoom&&(m.zoom=e.zoom)),d.value?.$el?.instance&&e.background&&(d.value.$el.instance.background=new a.Color(e.background)),-1===o&&(o=window.devicePixelRatio),u?.({dpr:o});!s.value?.$el?.instance&&!s.value?.component?.ctx.$el?.instance;)await new Promise((e=>requestAnimationFrame(e)));for(;!d.value?.$el?.instance&&!d.value?.component?.ctx.$el?.instance;)await new Promise((e=>requestAnimationFrame(e)));const t=s.value?.$el?.instance??s.value?.component?.ctx.$el?.instance;t.setPixelRatio(l.dpr);const f=d.value?.$el?.instance??d.value?.component?.ctx.$el?.instance,y=c.value?.$el?.instance??c.value?.component?.ctx.$el?.instance;if(!n.slots?.renderer?.()?.length){b(i,y,t,f,e.sizePolicy),e.r3f&&(t.outputEncoding=a.sRGBEncoding,t.toneMapping=a.ACESFilmicToneMapping);const n={shadow:e.shadow};n?.shadow&&(t.shadowMap.enabled=!0,"object"==typeof n.shadow&&(t.shadowMap.type=n.shadow.type))}if(!p)throw new Error("error creating app");p.config.globalProperties.lunchbox.camera=y,p.config.globalProperties.lunchbox.renderer=t,p.config.globalProperties.lunchbox.scene=f;for(let e of h??[])e({app:p,camera:y,renderer:t,scene:f});te({app:p,camera:y,renderer:t,scene:f,updateSource:e.updateSource})})),t.onBeforeUnmount((()=>{ie(),ce()}));const y="container"===e.sizePolicy?"static":"absolute",R="container"===e.sizePolicy?"static":"fixed",w=t.computed((()=>{const e=n.slots?.camera?.().find((e=>e.type?.name));return e||e}));return t.watch(w,(async(e,t)=>{e&&e?.props?.key!==t?.props?.key&&(c.value=e)}),{immediate:!0}),()=>t.createVNode(t.Fragment,null,[n.slots?.renderer?.()?.length?s.value=n.slots?.renderer?.()[0]:t.createVNode(t.Fragment,null,[t.createVNode("div",{class:"lunchbox-container",style:x(y),ref:i,"data-lunchbox":"true"},[t.createVNode("canvas",{ref:r,class:"lunchbox-canvas",style:x(R),"data-lunchbox":"true"},null)]),r.value?.domElement&&t.createVNode(t.resolveComponent("webGLRenderer"),t.mergeProps(e.rendererProperties??{},{ref:s,args:[{alpha:e.transparent,antialias:!0,canvas:r.value?.domElement,powerPreference:e.r3f?"high-performance":"default",...e.rendererArguments??{}}]}),null)]),n.slots?.scene?.()?.length?d.value=n.slots?.scene?.()[0]:t.createVNode(v,{ref:d},{default:()=>[n.slots?.default?.()]}),n.slots?.camera?.()?.length?c.value:e.ortho||e.orthographic?t.createVNode(t.resolveComponent("orthographicCamera"),t.mergeProps({ref:c,args:e.cameraArgs??[]},m),null):t.createVNode(t.resolveComponent("perspectiveCamera"),t.mergeProps({ref:c,args:e.cameraArgs??[e.r3f?75:45,.5625,1,1e3]},m),null),f?.value.length&&t.createVNode(g,null,null)])}}),w={},P={...["mesh","instancedMesh","scene","sprite","object3D","instancedBufferGeometry","bufferGeometry","boxBufferGeometry","circleBufferGeometry","coneBufferGeometry","cylinderBufferGeometry","dodecahedronBufferGeometry","extrudeBufferGeometry","icosahedronBufferGeometry","latheBufferGeometry","octahedronBufferGeometry","parametricBufferGeometry","planeBufferGeometry","polyhedronBufferGeometry","ringBufferGeometry","shapeBufferGeometry","sphereBufferGeometry","tetrahedronBufferGeometry","textBufferGeometry","torusBufferGeometry","torusKnotBufferGeometry","tubeBufferGeometry","wireframeGeometry","parametricGeometry","tetrahedronGeometry","octahedronGeometry","icosahedronGeometry","dodecahedronGeometry","polyhedronGeometry","tubeGeometry","torusKnotGeometry","torusGeometry","sphereGeometry","ringGeometry","planeGeometry","latheGeometry","shapeGeometry","extrudeGeometry","edgesGeometry","coneGeometry","cylinderGeometry","circleGeometry","boxGeometry","material","shadowMaterial","spriteMaterial","rawShaderMaterial","shaderMaterial","pointsMaterial","meshPhysicalMaterial","meshStandardMaterial","meshPhongMaterial","meshToonMaterial","meshNormalMaterial","meshLambertMaterial","meshDepthMaterial","meshDistanceMaterial","meshBasicMaterial","meshMatcapMaterial","lineDashedMaterial","lineBasicMaterial","light","spotLightShadow","spotLight","pointLight","rectAreaLight","hemisphereLight","directionalLightShadow","directionalLight","ambientLight","lightShadow","ambientLightProbe","hemisphereLightProbe","lightProbe","texture","videoTexture","dataTexture","dataTexture3D","compressedTexture","cubeTexture","canvasTexture","depthTexture","textureLoader","group","catmullRomCurve3","points","raycaster","cameraHelper","camera","perspectiveCamera","orthographicCamera","cubeCamera","arrayCamera","webGLRenderer"].map((e=>t.defineComponent({inheritAttrs:!1,name:e,setup:(n,r)=>()=>t.h(e,r.attrs,r.slots?.default?.()||[])}))).reduce(((e,t)=>(e[t.name]=t,e)),{}),Lunchbox:R},C=e=>t.defineComponent({inheritAttrs:!1,name:e,render(){return t.h(e,this.$attrs,this.$slots?.default?.()||[])}}),A=({app:e,...t})=>{Object.keys(t).forEach((n=>{e.component(n,C(n)),w[n]=t[n]}))};function E(e){if(!e.type)return null;const t=e.type[0].toUpperCase()+e.type.slice(1),n=t.replace(/Lunchbox$/,""),r=w[e.type]||a[t]||w[n]||a[n];if(!r)throw`${t} is not part of the THREE namespace! Did you forget to extend? import {extend} from 'lunchbox'; extend({app, YourComponent, ...})`;const o=(e.props.args??[]).map((t=>function({node:e,prop:t}){const n="string"==typeof t&&t.startsWith("$attachedArray"),r=function({node:e,prop:t}){return"string"==typeof t&&t.startsWith("$attachedArray")?e.attachedArray[t.replace("$attachedArray.","")]:"string"==typeof t&&t.startsWith("$attached")?e.attached[t.replace("$attached.","")]:t}({node:e,prop:t});return Array.isArray(r)&&n?r:[r]}({node:e,prop:t})));let i=[];o.forEach((e=>{i=i.concat(e)}));return new r(...i)}var L,N=new Uint8Array(16);function M(){if(!L&&!(L="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return L(N)}var S=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function B(e){return"string"==typeof e&&S.test(e)}for(var j=[],G=0;G<256;++G)j.push((G+256).toString(16).substr(1));function T(e,t,n){var r=(e=e||{}).random||(e.rng||M)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(var o=0;o<16;++o)t[n+o]=r[o];return t}return function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=(j[e[t+0]]+j[e[t+1]]+j[e[t+2]]+j[e[t+3]]+"-"+j[e[t+4]]+j[e[t+5]]+"-"+j[e[t+6]]+j[e[t+7]]+"-"+j[e[t+8]]+j[e[t+9]]+"-"+j[e[t+10]]+j[e[t+11]]+j[e[t+12]]+j[e[t+13]]+j[e[t+14]]+j[e[t+15]]).toLowerCase();if(!B(n))throw TypeError("Stringified UUID is invalid");return n}(r)}e.MiniDom=void 0,function(e){e.BaseNode=class{constructor(e={},t){this.parentNode=e?.parentNode??t??null,this.minidomType="MinidomBaseNode",this.uuid=e?.uuid??T()}uuid;parentNode;get nextSibling(){if(!this.parentNode)return null;const e=this.parentNode.children.findIndex((e=>e.uuid===this.uuid));return-1!==e&&e<this.parentNode.children.length-1?this.parentNode.children[e+1]:null}insertBefore(e,t){e.removeAsChildFromAnyParents(),e.parentNode=this;const n=this.children.findIndex((e=>e.uuid===t?.uuid));-1!==n?this.children.splice(n,0,e):this.children.push(e)}removeChild(e){const t=this.children.findIndex((t=>t?.uuid===e?.uuid));-1!==t&&this.children.splice(t,1)}children=[];addChild(e){return e&&(e.removeAsChildFromAnyParents(),e.parentNode=this,this.insertBefore(e,null)),this}getPath(){const e=[];let t=this;for(;t;)e.unshift(t),t=t.parentNode;return e}drop(){this.removeAsChildFromAnyParents(),this.parentNode=null}walk(e){const t=[this,...this.children],n=[];let r=!0;for(;t.length&&r;){const o=t.shift();if(o){if(n.includes(o))continue;n.push(o),t.push(...o.children.filter((e=>!n.includes(e)))),r=e(o)}else r=!1}}minidomType;removeAsChildFromAnyParents(){this.parentNode?.removeChild(this)}};class t extends e.BaseNode{constructor(e={},t){super(e,t),this.minidomType="RendererNode",this.eventListeners={},this.eventListenerRemoveFunctions={},this.name=e.name??"",this.metaType=e.metaType??"standardMeta",this.props=e.props??[],this.type=e.type??""}eventListeners;eventListenerRemoveFunctions;name;metaType;props;type;drop(){super.drop(),Object.keys(this.eventListenerRemoveFunctions).forEach((e=>{this.eventListenerRemoveFunctions[e].forEach((e=>e()))}))}}e.RendererBaseNode=t;class n extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.domElement=e.domElement??document.createElement("div")}domElement;isLunchboxRootNode=!0}e.RendererRootNode=n;class r extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.text=e.text??""}text}e.RendererCommentNode=r;class o extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.domElement=e.domElement??document.createElement("div")}domElement}e.RendererDomNode=o;class a extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.text=e.text??""}text}e.RendererTextNode=a;class i extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.attached=e.attached??[],this.attachedArray=e.attachedArray??{},this.instance=e.instance??null}attached;attachedArray;instance}e.RendererStandardNode=i}(e.MiniDom||(e.MiniDom={}));const k=Symbol(),O=Symbol(),$=Symbol(),D=Symbol(),F=Symbol(),K=Symbol(),I=Symbol(),V=Symbol(),U=Symbol(),z=Symbol(),H=Symbol(),W=Symbol(),q=Symbol(),_=Symbol(),X=Symbol(),Y=Symbol(),J=Symbol(),Q=Symbol(),Z=Symbol(),ee=e=>{"number"==typeof e.app.config.globalProperties.lunchbox.frameId&&cancelAnimationFrame(e.app.config.globalProperties.lunchbox.frameId),e.app.config.globalProperties.lunchbox.frameId=requestAnimationFrame((()=>te({app:e.app,renderer:e.renderer,scene:e.scene,camera:e.camera,updateSource:e.updateSource})))},te=e=>{e.updateSource?e.app.config.globalProperties.lunchbox.watchStopHandle||(e.app.config.globalProperties.lunchbox.watchStopHandle=t.watch(e.updateSource,(()=>{ee(e)}),{deep:!0})):ee(e);const{app:n,renderer:r,scene:o}=e;n.config.globalProperties.lunchbox.beforeRender.forEach((t=>{t?.(e)})),r&&o&&e.app.config.globalProperties.lunchbox.camera&&(n.customRender?n.customRender(e):r.render(t.toRaw(o),e.app.config.globalProperties.lunchbox.camera)),n.config.globalProperties.lunchbox.afterRender.forEach((t=>{t?.(e)}))},ne=()=>({onBeforeRender:t.inject(K),offBeforeRender:t.inject(I)}),re=(e,t=1/0)=>{ne().onBeforeRender?.(e,t)},oe=e=>{ne().offBeforeRender?.(e)},ae=()=>{const e=t.inject(H);return()=>{void 0!==e&&cancelAnimationFrame(e)}},ie=()=>{ae()?.()},se=()=>{const e=t.inject(W);return()=>e?.()},ce=()=>{se()?.()};function de({node:e,key:t,interactables:n,value:o}){if((e=>["onClick","onContextMenu","onDoubleClick","onPointerUp","onPointerDown","onPointerOver","onPointerOut","onPointerEnter","onPointerLeave","onPointerMove","onWheel"].includes(e))(t))return h({node:e,key:t,interactables:n,value:o});const a=t.replace(/-/g,"."),i=le[a]||a;if(pe.includes(t)||pe.includes(i))return e;if(!c(e))return e;if("string"==typeof o&&o.startsWith("$attached")){const t=o.replace("$attached.","");o=r.get(e.attached,t,null)}const s=e.instance;if(!s)return e;let d;for(let e=0;e<ue.length&&!d;e++){const t=[ue[e],i].filter(Boolean).join(".");d=d=r.get(s,t)}if(d&&r.isNumber(o)&&d.setScalar)d.setScalar(o);else if(d&&d.set){const e=Array.isArray(o)?o:[o];s[i].set(...e)}else if("function"==typeof d)if("onbeforerender"===i.toLowerCase()||"onafterrender"===i.toLowerCase())s[i]=o;else{if(!Array.isArray(o))throw new Error('Arguments on a declarative method must be wrapped in an array.\nWorks:\n<example :methodCall="[256]" />\nDoesn\'t work:\n<example :methodCall="256" />');d.bind(e.instance)(...o)}else void 0!==r.get(s,i,void 0)?r.set(s,i,""===o||o):console.log(`No property ${i} found on ${s}`);const l=s?.texture?.type||s?.type;if("string"==typeof l){const e=l.toLowerCase();switch(!0){case e.includes("material"):s.needsUpdate=!0;break;case e.includes("camera")&&s.updateProjectionMatrix:s.updateProjectionMatrix()}}return e}const le={x:"position.x",y:"position.y",z:"position.z"},ue=["","parameters"],pe=["args","attach","attachArray","is.default","isDefault","key","onAdded","ref","src"],me=["geometry","material"],he=(e,t,n,r)=>{const o={type:e,props:r};if(s(o)){return u(o)}const a=m(o);return me.forEach((t=>{e.toLowerCase().endsWith(t)&&(a.props.attach=t)})),a},fe=(e,t,n)=>{if(!t)throw new Error("missing parent");if(t.insertBefore(e,n),"commentMeta"!==e.metaType&&"textMeta"!==e.metaType&&(s(e)&&(s(t)||d(t))&&t.domElement.appendChild(e.domElement),c(e))){let n=t.metaType;if("textMeta"===n||"commentMeta"===n){const e=t.getPath();for(let n=e.length-1;n>=0;n--)if("textMeta"!==e[n].metaType&&"commentMeta"!==e[n].metaType){t=e[n];break}}if(c(e)&&e.instance?.isObject3D&&c(t)&&t.instance?.isObject3D&&t.instance?.add?.(e.instance),e?.props?.attach&&c(t)&&t?.instance){e.type?.toLowerCase().endsWith("loader")&&e.props.src&&(e.props.attach||e.props.attachArray)?function(e,t){const n=e.instance;if(t.attached=t.attached||{},t.attachedArray=t.attachedArray||{},!e.props.attach)return;if("textureloader"===e.type?.toLowerCase()){const r=n.load(e.props.src);ye(e,t,e.props.attach,r)}else n.load(e.props.src,(n=>{ye(e,t,e.props.attach,n)}),null,(e=>{throw new Error(e)}))}(e,t):ye(e,t,e.props.attach)}e.props?.onAdded&&e.props.onAdded({instance:e.instance})}};function ye(e,t,n,r){const o=r??e.instance,a=t.instance;e.props.attach===n&&(t.attached={[n]:o,...t.attached||{}},a[n]=r??e.instance),e.props.attachArray===n&&(t.attachedArray[e.props.attachArray]||(t.attachedArray[e.props.attachArray]=[]),t.attachedArray[e.props.attachArray].push(o),a[n]=[a[n]])}const be=e=>{if(!e)return;const t=[];e.walk((e=>(t.push(e),!0))),t.forEach((e=>{if(c(e)){e.instance?.removeFromParent?.();const t="scene"!==e.type&&e.instance?.dispose;t&&t.bind(e.instance)(),e.instance=null}e.drop()}))},ve=()=>t.inject(J),ge=()=>t.inject(X),xe=e=>{const n=ge();if(n.value)return void e(n.value);let r=null;r=t.watch(ge(),(t=>{t&&(e(t),r?.())}),{immediate:!0})},Re=()=>t.inject(Y),we=()=>({setCustomRender:t.inject($),clearCustomRender:t.inject(D)}),Pe=()=>t.inject(k),Ce=()=>t.inject(O),Ae=()=>t.inject(_),Ee=()=>t.inject(Z),Le=()=>t.inject(Q);e.addEventListener=h,e.afterRenderKey=V,e.appCameraKey=J,e.appKey=_,e.appRenderersKey=X,e.appRootNodeKey=q,e.appSceneKey=Y,e.beforeRenderKey=F,e.cancelUpdate=ie,e.cancelUpdateSource=ce,e.clearCustomRender=()=>{we()?.clearCustomRender?.()},e.clearCustomRenderKey=D,e.createApp=n=>{const{nodeOps:r,interactables:o}=(()=>{const e=t.ref([]);return{nodeOps:{createElement:he,createText:e=>p({text:e}),createComment:e=>l({text:e}),insert:fe,nextSibling:e=>e.nextSibling||null,parentNode:e=>e.parentNode||null,patchProp(t,n,r,o){s(t)?"style"===n?Object.keys(o).forEach((e=>{t.domElement.style[e]=o[e]})):t.domElement.setAttribute(n,o):d(t)||n.startsWith("$")||de({node:t,key:n,interactables:e,value:o})},remove:be,setElementText(){},setText(){}},interactables:e}})(),a=t.createRenderer(r).createApp(n);a.provide(Q,o),Object.keys(P).forEach((e=>{a?.component(e,P[e])})),a.provide($,(e=>{a.setCustomRender(e)})),a.provide(D,(()=>{a.clearCustomRender()}));const i=[];a.provide(F,i),a.provide(K,((e,t=1/0)=>{t===1/0?i.push(e):i.splice(t,0,e)})),a.provide(I,(e=>{if(isFinite(e))i.splice(e,1);else{const t=i.findIndex((t=>t==e));-1!==t&&i.splice(t,1)}}));const c=[];a.provide(V,c),a.provide(U,((e,t=1/0)=>{t===1/0?c.push(e):c.splice(t,0,e)})),a.provide(z,(e=>{if(isFinite(e))c.splice(e,1);else{const t=c.findIndex((t=>t==e));-1!==t&&c.splice(t,1)}})),a.config.globalProperties.lunchbox=t.reactive({afterRender:c,beforeRender:i,camera:null,dpr:1,frameId:-1,renderer:null,scene:null,watchStopHandle:null}),a.provide(k,a.config.globalProperties.lunchbox),a.provide(O,(e=>{Object.keys(e).forEach((t=>{const n=t;a.config.globalProperties.lunchbox[n]=e[n]}))})),a.provide(H,a.config.globalProperties.lunchbox.frameId),a.provide(W,a.config.globalProperties.lunchbox.watchStopHandle);const{mount:u}=a;a.mount=(t,...n)=>{const r="string"==typeof t?document.querySelector(t):t,o=new e.MiniDom.RendererRootNode({domElement:r,isLunchboxRootNode:!0,name:"root",metaType:"rootMeta",type:"root",uuid:"LUNCHBOX_ROOT"});a.rootNode=o,a.provide(q,o);return u(o,...n)},a.extend=e=>(A({app:a,...e}),a);return a.provide(Z,[]),a.setCustomRender=e=>{a&&(a.customRender=e)},a.clearCustomRender=()=>{a&&(a.customRender=null)},a.provide(_,a),a.provide(X,t.computed((()=>a.config.globalProperties.lunchbox.renderer))),a.provide(Y,t.computed((()=>a.config.globalProperties.lunchbox.scene))),a.provide(J,t.computed((()=>a.config.globalProperties.lunchbox.camera))),a},e.createCommentNode=l,e.createDomNode=u,e.createNode=m,e.createTextNode=p,e.extend=A,e.find=function(e){return e=t.isRef(e)?e.value:e,c(e)?e?.instance:i(e)?e?.$el?.instance:t.isVNode(e)?e.el?.instance:null},e.frameIdKey=H,e.getInstance=(e,n=!0)=>t.computed((()=>{const r=e.value?.$el?.instance??e.value?.instance??null;return r&&n?t.toRaw(r):r})),e.globalsInjectionKey=k,e.instantiateThreeObject=E,e.isMinidomNode=function(e){return"RendererNode"===e?.minidomType},e.lunchboxInteractables=Q,e.nestedPropertiesToCheck=ue,e.offAfterRender=e=>{ne().offBeforeRender?.(e)},e.offAfterRenderKey=z,e.offBeforeRender=oe,e.offBeforeRenderKey=I,e.onAfterRender=(e,t=1/0)=>{ne().onBeforeRender?.(e,t)},e.onAfterRenderKey=U,e.onBeforeRender=re,e.onBeforeRenderKey=K,e.onCameraReady=e=>{const n=ve();if(n.value)return void e(n.value);let r=null;r=t.watch(ve(),(t=>{t&&(e(t),r?.())}))},e.onRendererReady=xe,e.onSceneReady=e=>{const n=Re();if(n.value)return void e(n.value);let r=null;r=t.watch(Re(),(t=>{t&&(e(t),r?.())}),{immediate:!0})},e.onStart=(e,t=1/0)=>{const n=Ee();t===1/0?n?.push(e):n?.splice(t,0,e)},e.setCustomRender=e=>{we()?.setCustomRender?.(e)},e.setCustomRenderKey=$,e.startCallbackKey=Z,e.update=te,e.updateGlobals=e=>{Ce()?.(e)},e.updateGlobalsInjectionKey=O,e.updateObjectProp=de,e.useAfterRender=()=>({onAfterRender:t.inject(K),offAfterRender:t.inject(I)}),e.useApp=Ae,e.useBeforeRender=ne,e.useCamera=ve,e.useCancelUpdate=ae,e.useCancelUpdateSource=se,e.useCustomRender=we,e.useGlobals=Pe,e.useLunchboxInteractables=Le,e.useRenderer=ge,e.useScene=Re,e.useStartCallbacks=Ee,e.useUpdateGlobals=Ce,e.watchStopHandleKey=W,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue"),require("three")):"function"==typeof define&&define.amd?define(["exports","vue","three"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Lunchbox={},e.vue,e.three)}(this,(function(e,t,r){"use strict";function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var o=n(r);const a=(e,t,r)=>{if(!t)return;const n=(Array.isArray(t)?t:t.match(/([^[.\]])+/g))?.reduce(((e,t)=>e&&e[t]),e);return void 0===n?r:n},i=(()=>{const e=Object.prototype.toString;return function(t){return"number"==typeof t||function(e){return!!e&&"object"==typeof e}(t)&&"[object Number]"==e.call(t)}})(),s=e=>e?.$el&&e?.$el?.hasOwnProperty?.("instance"),c=e=>"domMeta"===e?.metaType||e?.props?.["data-lunchbox"],d=e=>"standardMeta"===e?.metaType,l=e=>e.isLunchboxRootNode;function u(t={}){const r={text:t.text??""};return new e.MiniDom.RendererCommentNode({...r,...t,metaType:"commentMeta"})}function p(t={}){const r={domElement:document.createElement(t.type??"")};return new e.MiniDom.RendererDomNode({...r,...t,metaType:"domMeta"})}function m(t={}){const r={text:t.text??""};return new e.MiniDom.RendererTextNode({...t,...r,metaType:"textMeta"})}function h(t={},r={}){const n={attached:t.attached??[],attachedArray:t.attachedArray??{},instance:t.instance??null},o=new e.MiniDom.RendererStandardNode({...t,...n,metaType:"standardMeta"});return!o.type||l(o)||o.instance||(o.instance=L({...o,props:{...o.props,...r}})),o}function f({node:e,key:t,interactables:r,value:n}){return e.eventListeners[t]||(e.eventListeners[t]=[]),e.eventListenerRemoveFunctions[t]||(e.eventListenerRemoveFunctions[t]=[]),e.eventListeners[t].push(n),y.includes(t)&&e.instance&&!r.value.includes(e)&&(r.value.push(e),e.eventListenerRemoveFunctions[t].push((()=>{const t=r.value.indexOf(e);-1!==t&&r.value.splice(t,1)}))),e}const y=["onClick","onPointerUp","onPointerDown","onPointerOver","onPointerOut","onPointerEnter","onPointerLeave","onPointerMove"],b=(e,r,n,o,a)=>{if(!r?.domElement||!n||!e)return;const i=(o=o??window.innerWidth)/(a=a??window.innerHeight);if("perspectivecamera"===e.type?.toLowerCase()){const t=e;t.aspect=i,t.updateProjectionMatrix()}else if("orthographiccamera"===e.type?.toLowerCase()){const t=e,r=a/o;t.top=10*r,t.bottom=10*-r,t.right=10,t.left=-10,t.updateProjectionMatrix()}r.setSize(o,a),n&&e&&r.render(t.toRaw(n),t.toRaw(e))},v=(e,t,r,n,o)=>{const a=e.value?.domElement;if(!a)throw new Error("missing container");const i=()=>{if("container"===o){const e=(e=>{const t=getComputedStyle(e);return{width:e.clientWidth-parseFloat(t.paddingLeft)-parseFloat(t.paddingRight),height:e.clientHeight-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom)}})(a);b(t,r,n,e.width,e.height)}else b(t,r,n)};i();let s=new ResizeObserver((()=>{i()}));return a&&s.observe(a),{dispose(){a&&s.unobserve(a)}}},g=t.defineComponent({name:"LunchboxScene",setup:(e,{slots:r})=>()=>t.createVNode(t.resolveComponent("scene"),null,{default:()=>[r.default?.()]})}),x=t.defineComponent({name:"LunchboxEventHandlers",setup(){const e=Ne(),r=Ce(),n=t.ref({x:1/0,y:1/0}),a=t.ref(!1);let i=[];const s=new o.Raycaster(new o.Vector3,new o.Vector3(0,0,-1)),c=({element:e,eventKeys:t,intersection:r})=>{e&&t.forEach((t=>{e.eventListeners[t]&&e.eventListeners[t].forEach((e=>{e({intersection:r})}))}))};Re((e=>{if(!e?.domElement)return;const{domElement:t}=e;t.addEventListener("pointermove",(e=>{const o=(t.width??1)/r.dpr,a=(t.height??1)/r.dpr;n.value.x=e.offsetX/o*2-1,n.value.y=-e.offsetY/a*2+1})),t.addEventListener("pointerdown",(()=>a.value=!0)),t.addEventListener("pointerup",(()=>a.value=!1))}));const d=ge(),l=()=>{const t=d.value;if(!t)return;s.setFromCamera(n.value,t);const r=s.intersectObjects(e?.value.map((e=>e.instance))??[]);let o=[],a=[],l=[];o=i.map((e=>e.intersection)),r?.forEach((t=>{if(-1===i.findIndex((e=>e.intersection.object===t.object))){const r=e?.value.find((e=>e.instance?.uuid===t.object.uuid));r&&a.push({element:r,intersection:t})}else{const r=e?.value.find((e=>e.instance?.uuid===t.object.uuid));r&&l.push({element:r,intersection:t})}const r=o.findIndex((e=>e.object.uuid===t.object.uuid));-1!==r&&o.splice(r,1)}));const u=o.map((t=>({element:e?.value.find((e=>e.instance?.uuid===t.object.uuid)),intersection:t})));a.forEach((({element:e,intersection:t})=>{c({element:e,eventKeys:["onPointerEnter"],intersection:t})})),l.forEach((({element:e,intersection:t})=>{c({element:e,eventKeys:["onPointerOver","onPointerMove"],intersection:t})})),u.forEach((({element:e,intersection:t})=>{c({element:e,eventKeys:["onPointerLeave","onPointerOut"],intersection:t})})),i=[].concat(a,l)};oe(l);t.onBeforeUnmount((()=>ae(l)));const u=["onClick","onPointerDown","onPointerUp"];return t.watch(a,(e=>{l();const t=[];i.forEach((r=>{u.forEach((n=>{const o=r.element.uuid+n;!e||"onClick"!==n&&"onPointerDown"!==n?e||"onPointerUp"!==n||t.includes(o)||(r.element.eventListeners[n]?.forEach((e=>e({intersection:r.intersection}))),t.push(o)):t.includes(o)||(r.element.eventListeners[n]?.forEach((e=>e({intersection:r.intersection}))),t.push(o))}))}))})),()=>t.createVNode(t.resolveComponent("object3D"),null,null)}}),R=e=>({position:e,top:0,right:0,bottom:0,left:0,width:"100%",height:"100%",display:"block"}),w=t.defineComponent({name:"Lunchbox",props:{background:String,cameraArgs:Array,cameraLook:Array,cameraLookAt:Array,cameraPosition:Array,dpr:Number,ortho:Boolean,orthographic:Boolean,r3f:Boolean,rendererArguments:Object,rendererProperties:Object,sizePolicy:String,shadow:[Boolean,Object],transparent:Boolean,zoom:Number,updateSource:Object},setup(e,r){const n=t.ref();let a=e.dpr??-1;const i=t.ref(),s=t.ref(),c=t.ref(),d=t.ref(),l=Ce(),u=Ae(),p=Ee(),m=t.reactive({}),h=Le();e.r3f&&o?.ColorManagement&&(o.ColorManagement.legacyMode=!1);const f=Ne();t.onMounted((async()=>{if(!n.value&&!r.slots?.renderer?.()?.length)throw new Error("missing canvas");for(r.slots?.camera?.()?.length||(e.cameraPosition&&(m.position=e.cameraPosition),(e.cameraLook||e.cameraLookAt)&&(m.lookAt=e.cameraLook||e.cameraLookAt),void 0!==e.zoom&&(m.zoom=e.zoom)),d.value?.$el?.instance&&e.background&&(d.value.$el.instance.background=new o.Color(e.background)),-1===a&&(a=window.devicePixelRatio),u?.({dpr:a});!s.value?.$el?.instance&&!s.value?.component?.ctx.$el?.instance;)await new Promise((e=>requestAnimationFrame(e)));for(;!d.value?.$el?.instance&&!d.value?.component?.ctx.$el?.instance;)await new Promise((e=>requestAnimationFrame(e)));const t=s.value?.$el?.instance??s.value?.component?.ctx.$el?.instance;t.setPixelRatio(l.dpr);const f=d.value?.$el?.instance??d.value?.component?.ctx.$el?.instance,y=c.value?.$el?.instance??c.value?.component?.ctx.$el?.instance;if(!r.slots?.renderer?.()?.length){v(i,y,t,f,e.sizePolicy),e.r3f&&(t.outputEncoding=o.sRGBEncoding,t.toneMapping=o.ACESFilmicToneMapping);const r={shadow:e.shadow};r?.shadow&&(t.shadowMap.enabled=!0,"object"==typeof r.shadow&&(t.shadowMap.type=r.shadow.type))}if(!p)throw new Error("error creating app");p.config.globalProperties.lunchbox.camera=y,p.config.globalProperties.lunchbox.renderer=t,p.config.globalProperties.lunchbox.scene=f;for(let e of h??[])e({app:p,camera:y,renderer:t,scene:f});re({app:p,camera:y,renderer:t,scene:f,updateSource:e.updateSource})})),t.onBeforeUnmount((()=>{se(),de()}));const y="container"===e.sizePolicy?"static":"absolute",b="container"===e.sizePolicy?"static":"fixed",w=t.computed((()=>{const e=r.slots?.camera?.().find((e=>e.type?.name));return e||e}));return t.watch(w,(async(e,t)=>{e&&e?.props?.key!==t?.props?.key&&(c.value=e)}),{immediate:!0}),()=>t.createVNode(t.Fragment,null,[r.slots?.renderer?.()?.length?s.value=r.slots?.renderer?.()[0]:t.createVNode(t.Fragment,null,[t.createVNode("div",{class:"lunchbox-container",style:R(y),ref:i,"data-lunchbox":"true"},[t.createVNode("canvas",{ref:n,class:"lunchbox-canvas",style:R(b),"data-lunchbox":"true"},null)]),n.value?.domElement&&t.createVNode(t.resolveComponent("webGLRenderer"),t.mergeProps(e.rendererProperties??{},{ref:s,args:[{alpha:e.transparent,antialias:!0,canvas:n.value?.domElement,powerPreference:e.r3f?"high-performance":"default",...e.rendererArguments??{}}]}),null)]),r.slots?.scene?.()?.length?d.value=r.slots?.scene?.()[0]:t.createVNode(g,{ref:d},{default:()=>[r.slots?.default?.()]}),r.slots?.camera?.()?.length?c.value:e.ortho||e.orthographic?t.createVNode(t.resolveComponent("orthographicCamera"),t.mergeProps({ref:c,args:e.cameraArgs??[]},m),null):t.createVNode(t.resolveComponent("perspectiveCamera"),t.mergeProps({ref:c,args:e.cameraArgs??[e.r3f?75:45,.5625,1,1e3]},m),null),f?.value.length&&t.createVNode(x,null,null)])}}),P={},C={...["mesh","instancedMesh","scene","sprite","object3D","instancedBufferGeometry","bufferGeometry","boxBufferGeometry","circleBufferGeometry","coneBufferGeometry","cylinderBufferGeometry","dodecahedronBufferGeometry","extrudeBufferGeometry","icosahedronBufferGeometry","latheBufferGeometry","octahedronBufferGeometry","parametricBufferGeometry","planeBufferGeometry","polyhedronBufferGeometry","ringBufferGeometry","shapeBufferGeometry","sphereBufferGeometry","tetrahedronBufferGeometry","textBufferGeometry","torusBufferGeometry","torusKnotBufferGeometry","tubeBufferGeometry","wireframeGeometry","parametricGeometry","tetrahedronGeometry","octahedronGeometry","icosahedronGeometry","dodecahedronGeometry","polyhedronGeometry","tubeGeometry","torusKnotGeometry","torusGeometry","sphereGeometry","ringGeometry","planeGeometry","latheGeometry","shapeGeometry","extrudeGeometry","edgesGeometry","coneGeometry","cylinderGeometry","circleGeometry","boxGeometry","material","shadowMaterial","spriteMaterial","rawShaderMaterial","shaderMaterial","pointsMaterial","meshPhysicalMaterial","meshStandardMaterial","meshPhongMaterial","meshToonMaterial","meshNormalMaterial","meshLambertMaterial","meshDepthMaterial","meshDistanceMaterial","meshBasicMaterial","meshMatcapMaterial","lineDashedMaterial","lineBasicMaterial","light","spotLightShadow","spotLight","pointLight","rectAreaLight","hemisphereLight","directionalLightShadow","directionalLight","ambientLight","lightShadow","ambientLightProbe","hemisphereLightProbe","lightProbe","texture","videoTexture","dataTexture","dataTexture3D","compressedTexture","cubeTexture","canvasTexture","depthTexture","textureLoader","group","catmullRomCurve3","points","raycaster","cameraHelper","camera","perspectiveCamera","orthographicCamera","cubeCamera","arrayCamera","webGLRenderer"].map((e=>t.defineComponent({inheritAttrs:!1,name:e,setup:(r,n)=>()=>t.h(e,n.attrs,n.slots?.default?.()||[])}))).reduce(((e,t)=>(e[t.name]=t,e)),{}),Lunchbox:w},A=e=>t.defineComponent({inheritAttrs:!1,name:e,render(){return t.h(e,this.$attrs,this.$slots?.default?.()||[])}}),E=({app:e,...t})=>{Object.keys(t).forEach((r=>{e.component(r,A(r)),P[r]=t[r]}))};function L(e){if(!e.type)return null;const t=e.type[0].toUpperCase()+e.type.slice(1),r=t.replace(/Lunchbox$/,""),n=P[e.type]||o[t]||P[r]||o[r];if(!n)throw`${t} is not part of the THREE namespace! Did you forget to extend? import {extend} from 'lunchbox'; extend({app, YourComponent, ...})`;const a=(e.props.args??[]).map((t=>function({node:e,prop:t}){const r="string"==typeof t&&t.startsWith("$attachedArray"),n=function({node:e,prop:t}){return"string"==typeof t&&t.startsWith("$attachedArray")?e.attachedArray[t.replace("$attachedArray.","")]:"string"==typeof t&&t.startsWith("$attached")?e.attached[t.replace("$attached.","")]:t}({node:e,prop:t});return Array.isArray(n)&&r?n:[n]}({node:e,prop:t})));let i=[];a.forEach((e=>{i=i.concat(e)}));return new n(...i)}var N,M=new Uint8Array(16);function S(){if(!N&&!(N="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return N(M)}var j=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function B(e){return"string"==typeof e&&j.test(e)}for(var G=[],T=0;T<256;++T)G.push((T+256).toString(16).substr(1));function k(e,t,r){var n=(e=e||{}).random||(e.rng||S)();if(n[6]=15&n[6]|64,n[8]=63&n[8]|128,t){r=r||0;for(var o=0;o<16;++o)t[r+o]=n[o];return t}return function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=(G[e[t+0]]+G[e[t+1]]+G[e[t+2]]+G[e[t+3]]+"-"+G[e[t+4]]+G[e[t+5]]+"-"+G[e[t+6]]+G[e[t+7]]+"-"+G[e[t+8]]+G[e[t+9]]+"-"+G[e[t+10]]+G[e[t+11]]+G[e[t+12]]+G[e[t+13]]+G[e[t+14]]+G[e[t+15]]).toLowerCase();if(!B(r))throw TypeError("Stringified UUID is invalid");return r}(n)}e.MiniDom=void 0,function(e){e.BaseNode=class{constructor(e={},t){this.parentNode=e?.parentNode??t??null,this.minidomType="MinidomBaseNode",this.uuid=e?.uuid??k()}uuid;parentNode;get nextSibling(){if(!this.parentNode)return null;const e=this.parentNode.children.findIndex((e=>e.uuid===this.uuid));return-1!==e&&e<this.parentNode.children.length-1?this.parentNode.children[e+1]:null}insertBefore(e,t){e.removeAsChildFromAnyParents(),e.parentNode=this;const r=this.children.findIndex((e=>e.uuid===t?.uuid));-1!==r?this.children.splice(r,0,e):this.children.push(e)}removeChild(e){const t=this.children.findIndex((t=>t?.uuid===e?.uuid));-1!==t&&this.children.splice(t,1)}children=[];addChild(e){return e&&(e.removeAsChildFromAnyParents(),e.parentNode=this,this.insertBefore(e,null)),this}getPath(){const e=[];let t=this;for(;t;)e.unshift(t),t=t.parentNode;return e}drop(){this.removeAsChildFromAnyParents(),this.parentNode=null}walk(e){const t=[this,...this.children],r=[];let n=!0;for(;t.length&&n;){const o=t.shift();if(o){if(r.includes(o))continue;r.push(o),t.push(...o.children.filter((e=>!r.includes(e)))),n=e(o)}else n=!1}}minidomType;removeAsChildFromAnyParents(){this.parentNode?.removeChild(this)}};class t extends e.BaseNode{constructor(e={},t){super(e,t),this.minidomType="RendererNode",this.eventListeners={},this.eventListenerRemoveFunctions={},this.name=e.name??"",this.metaType=e.metaType??"standardMeta",this.props=e.props??[],this.type=e.type??""}eventListeners;eventListenerRemoveFunctions;name;metaType;props;type;drop(){super.drop(),Object.keys(this.eventListenerRemoveFunctions).forEach((e=>{this.eventListenerRemoveFunctions[e].forEach((e=>e()))}))}}e.RendererBaseNode=t;class r extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.domElement=e.domElement??document.createElement("div")}domElement;isLunchboxRootNode=!0}e.RendererRootNode=r;class n extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.text=e.text??""}text}e.RendererCommentNode=n;class o extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.domElement=e.domElement??document.createElement("div")}domElement}e.RendererDomNode=o;class a extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.text=e.text??""}text}e.RendererTextNode=a;class i extends e.RendererBaseNode{constructor(e={},t){super(e,t),this.attached=e.attached??[],this.attachedArray=e.attachedArray??{},this.instance=e.instance??null}attached;attachedArray;instance}e.RendererStandardNode=i}(e.MiniDom||(e.MiniDom={}));const O=Symbol(),$=Symbol(),D=Symbol(),F=Symbol(),K=Symbol(),I=Symbol(),V=Symbol(),U=Symbol(),z=Symbol(),H=Symbol(),W=Symbol(),q=Symbol(),_=Symbol(),X=Symbol(),Y=Symbol(),J=Symbol(),Q=Symbol(),Z=Symbol(),ee=Symbol(),te=e=>{"number"==typeof e.app.config.globalProperties.lunchbox.frameId&&cancelAnimationFrame(e.app.config.globalProperties.lunchbox.frameId),e.app.config.globalProperties.lunchbox.frameId=requestAnimationFrame((()=>re({app:e.app,renderer:e.renderer,scene:e.scene,camera:e.camera,updateSource:e.updateSource})))},re=e=>{e.updateSource?e.app.config.globalProperties.lunchbox.watchStopHandle||(e.app.config.globalProperties.lunchbox.watchStopHandle=t.watch(e.updateSource,(()=>{te(e)}),{deep:!0})):te(e);const{app:r,renderer:n,scene:o}=e;r.config.globalProperties.lunchbox.beforeRender.forEach((t=>{t?.(e)})),n&&o&&e.app.config.globalProperties.lunchbox.camera&&(r.customRender?r.customRender(e):n.render(t.toRaw(o),e.app.config.globalProperties.lunchbox.camera)),r.config.globalProperties.lunchbox.afterRender.forEach((t=>{t?.(e)}))},ne=()=>({onBeforeRender:t.inject(I),offBeforeRender:t.inject(V)}),oe=(e,t=1/0)=>{ne().onBeforeRender?.(e,t)},ae=e=>{ne().offBeforeRender?.(e)},ie=()=>{const e=t.inject(W);return()=>{void 0!==e&&cancelAnimationFrame(e)}},se=()=>{ie()?.()},ce=()=>{const e=t.inject(q);return()=>e?.()},de=()=>{ce()?.()};function le({node:e,key:t,interactables:r,value:n}){if((e=>["onClick","onContextMenu","onDoubleClick","onPointerUp","onPointerDown","onPointerOver","onPointerOut","onPointerEnter","onPointerLeave","onPointerMove","onWheel"].includes(e))(t))return f({node:e,key:t,interactables:r,value:n});const o=t.replace(/-/g,"."),s=ue[o]||o;if(me.includes(t)||me.includes(s))return e;if(!d(e))return e;if("string"==typeof n&&n.startsWith("$attached")){const t=n.replace("$attached.","");n=a(e.attached,t,null)}const c=e.instance;if(!c)return e;let l;for(let e=0;e<pe.length&&!l;e++){const t=[pe[e],s].filter(Boolean).join(".");l=l=a(c,t)}if(l&&i(n)&&l?.setScalar)l.setScalar(n);else if(l&&l.set){const e=Array.isArray(n)?n:[n];c[s].set(...e)}else if("function"==typeof l)if("onbeforerender"===s.toLowerCase()||"onafterrender"===s.toLowerCase())c[s]=n;else{if(!Array.isArray(n))throw new Error('Arguments on a declarative method must be wrapped in an array.\nWorks:\n<example :methodCall="[256]" />\nDoesn\'t work:\n<example :methodCall="256" />');l.bind(e.instance)(...n)}else void 0!==a(c,s,void 0)?((e,t,r)=>{const n=Array.isArray(t)?t:t.match(/([^[.\]])+/g);n?.reduce(((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===n.length-1&&(e[t]=r),e[t])),e)})(c,s,""===n||n):console.log(`No property ${s} found on ${c}`);const u=c?.texture?.type||c?.type;if("string"==typeof u){const e=u.toLowerCase();switch(!0){case e.includes("material"):c.needsUpdate=!0;break;case e.includes("camera")&&c.updateProjectionMatrix:c.updateProjectionMatrix()}}return e}const ue={x:"position.x",y:"position.y",z:"position.z"},pe=["","parameters"],me=["args","attach","attachArray","is.default","isDefault","key","onAdded","ref","src"],he=["geometry","material"],fe=(e,t,r,n)=>{const o={type:e,props:n};if(c(o)){return p(o)}const a=h(o);return he.forEach((t=>{e.toLowerCase().endsWith(t)&&(a.props.attach=t)})),a},ye=(e,t,r)=>{if(!t)throw new Error("missing parent");if(t.insertBefore(e,r),"commentMeta"!==e.metaType&&"textMeta"!==e.metaType&&(c(e)&&(c(t)||l(t))&&t.domElement.appendChild(e.domElement),d(e))){let r=t.metaType;if("textMeta"===r||"commentMeta"===r){const e=t.getPath();for(let r=e.length-1;r>=0;r--)if("textMeta"!==e[r].metaType&&"commentMeta"!==e[r].metaType){t=e[r];break}}if(d(e)&&e.instance?.isObject3D&&d(t)&&t.instance?.isObject3D&&t.instance?.add?.(e.instance),e?.props?.attach&&d(t)&&t?.instance){e.type?.toLowerCase().endsWith("loader")&&e.props.src&&(e.props.attach||e.props.attachArray)?function(e,t){const r=e.instance;if(t.attached=t.attached||{},t.attachedArray=t.attachedArray||{},!e.props.attach)return;if("textureloader"===e.type?.toLowerCase()){const n=r.load(e.props.src);be(e,t,e.props.attach,n)}else r.load(e.props.src,(r=>{be(e,t,e.props.attach,r)}),null,(e=>{throw new Error(e)}))}(e,t):be(e,t,e.props.attach)}e.props?.onAdded&&e.props.onAdded({instance:e.instance})}};function be(e,t,r,n){const o=n??e.instance,a=t.instance;e.props.attach===r&&(t.attached={[r]:o,...t.attached||{}},a[r]=n??e.instance),e.props.attachArray===r&&(t.attachedArray[e.props.attachArray]||(t.attachedArray[e.props.attachArray]=[]),t.attachedArray[e.props.attachArray].push(o),a[r]=[a[r]])}const ve=e=>{if(!e)return;const t=[];e.walk((e=>(t.push(e),!0))),t.forEach((e=>{if(d(e)){e.instance?.removeFromParent?.();const t="scene"!==e.type&&e.instance?.dispose;t&&t.bind(e.instance)(),e.instance=null}e.drop()}))},ge=()=>t.inject(Q),xe=()=>t.inject(Y),Re=e=>{const r=xe();if(r.value)return void e(r.value);let n=null;n=t.watch(xe(),(t=>{t&&(e(t),n?.())}),{immediate:!0})},we=()=>t.inject(J),Pe=()=>({setCustomRender:t.inject(D),clearCustomRender:t.inject(F)}),Ce=()=>t.inject(O),Ae=()=>t.inject($),Ee=()=>t.inject(X),Le=()=>t.inject(ee),Ne=()=>t.inject(Z);e.addEventListener=f,e.afterRenderKey=U,e.appCameraKey=Q,e.appKey=X,e.appRenderersKey=Y,e.appRootNodeKey=_,e.appSceneKey=J,e.beforeRenderKey=K,e.cancelUpdate=se,e.cancelUpdateSource=de,e.clearCustomRender=()=>{Pe()?.clearCustomRender?.()},e.clearCustomRenderKey=F,e.createApp=r=>{const{nodeOps:n,interactables:o}=(()=>{const e=t.ref([]);return{nodeOps:{createElement:fe,createText:e=>m({text:e}),createComment:e=>u({text:e}),insert:ye,nextSibling:e=>e.nextSibling||null,parentNode:e=>e.parentNode||null,patchProp(t,r,n,o){c(t)?"style"===r?Object.keys(o).forEach((e=>{t.domElement.style[e]=o[e]})):t.domElement.setAttribute(r,o):l(t)||r.startsWith("$")||le({node:t,key:r,interactables:e,value:o})},remove:ve,setElementText(){},setText(){}},interactables:e}})(),a=t.createRenderer(n).createApp(r);a.provide(Z,o),Object.keys(C).forEach((e=>{a?.component(e,C[e])})),a.provide(D,(e=>{a.setCustomRender(e)})),a.provide(F,(()=>{a.clearCustomRender()}));const i=[];a.provide(K,i),a.provide(I,((e,t=1/0)=>{t===1/0?i.push(e):i.splice(t,0,e)})),a.provide(V,(e=>{if(isFinite(e))i.splice(e,1);else{const t=i.findIndex((t=>t==e));-1!==t&&i.splice(t,1)}}));const s=[];a.provide(U,s),a.provide(z,((e,t=1/0)=>{t===1/0?s.push(e):s.splice(t,0,e)})),a.provide(H,(e=>{if(isFinite(e))s.splice(e,1);else{const t=s.findIndex((t=>t==e));-1!==t&&s.splice(t,1)}})),a.config.globalProperties.lunchbox=t.reactive({afterRender:s,beforeRender:i,camera:null,dpr:1,frameId:-1,renderer:null,scene:null,watchStopHandle:null}),a.provide(O,a.config.globalProperties.lunchbox),a.provide($,(e=>{Object.keys(e).forEach((t=>{const r=t;a.config.globalProperties.lunchbox[r]=e[r]}))})),a.provide(W,a.config.globalProperties.lunchbox.frameId),a.provide(q,a.config.globalProperties.lunchbox.watchStopHandle);const{mount:d}=a;a.mount=(t,...r)=>{const n="string"==typeof t?document.querySelector(t):t,o=new e.MiniDom.RendererRootNode({domElement:n,isLunchboxRootNode:!0,name:"root",metaType:"rootMeta",type:"root",uuid:"LUNCHBOX_ROOT"});a.rootNode=o,a.provide(_,o);return d(o,...r)},a.extend=e=>(E({app:a,...e}),a);return a.provide(ee,[]),a.setCustomRender=e=>{a&&(a.customRender=e)},a.clearCustomRender=()=>{a&&(a.customRender=null)},a.provide(X,a),a.provide(Y,t.computed((()=>a.config.globalProperties.lunchbox.renderer))),a.provide(J,t.computed((()=>a.config.globalProperties.lunchbox.scene))),a.provide(Q,t.computed((()=>a.config.globalProperties.lunchbox.camera))),a},e.createCommentNode=u,e.createDomNode=p,e.createNode=h,e.createTextNode=m,e.extend=E,e.find=function(e){return e=t.isRef(e)?e.value:e,d(e)?e?.instance:s(e)?e?.$el?.instance:t.isVNode(e)?e.el?.instance:null},e.frameIdKey=W,e.getInstance=(e,r=!0)=>t.computed((()=>{const n=e.value?.$el?.instance??e.value?.instance??null;return n&&r?t.toRaw(n):n})),e.globalsInjectionKey=O,e.instantiateThreeObject=L,e.isMinidomNode=function(e){return"RendererNode"===e?.minidomType},e.lunchboxInteractables=Z,e.nestedPropertiesToCheck=pe,e.offAfterRender=e=>{ne().offBeforeRender?.(e)},e.offAfterRenderKey=H,e.offBeforeRender=ae,e.offBeforeRenderKey=V,e.onAfterRender=(e,t=1/0)=>{ne().onBeforeRender?.(e,t)},e.onAfterRenderKey=z,e.onBeforeRender=oe,e.onBeforeRenderKey=I,e.onCameraReady=e=>{const r=ge();if(r.value)return void e(r.value);let n=null;n=t.watch(ge(),(t=>{t&&(e(t),n?.())}))},e.onRendererReady=Re,e.onSceneReady=e=>{const r=we();if(r.value)return void e(r.value);let n=null;n=t.watch(we(),(t=>{t&&(e(t),n?.())}),{immediate:!0})},e.onStart=(e,t=1/0)=>{const r=Le();t===1/0?r?.push(e):r?.splice(t,0,e)},e.setCustomRender=e=>{Pe()?.setCustomRender?.(e)},e.setCustomRenderKey=D,e.startCallbackKey=ee,e.update=re,e.updateGlobals=e=>{Ae()?.(e)},e.updateGlobalsInjectionKey=$,e.updateObjectProp=le,e.useAfterRender=()=>({onAfterRender:t.inject(I),offAfterRender:t.inject(V)}),e.useApp=Ee,e.useBeforeRender=ne,e.useCamera=ge,e.useCancelUpdate=ie,e.useCancelUpdateSource=ce,e.useCustomRender=Pe,e.useGlobals=Ce,e.useLunchboxInteractables=Ne,e.useRenderer=xe,e.useScene=we,e.useStartCallbacks=Le,e.useUpdateGlobals=Ae,e.watchStopHandleKey=q,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isRef, isVNode, toRaw, defineComponent, createVNode, resolveComponent, ref, onBeforeUnmount, watch, reactive, onMounted, computed, Fragment, mergeProps, h, inject, createRenderer } from 'vue';
|
|
2
2
|
import * as THREE from 'three';
|
|
3
|
-
import { get, isNumber, set } from 'lodash';
|
|
4
3
|
|
|
5
4
|
function find(target) {
|
|
6
5
|
target = isRef(target) ? target.value : target; // handle standard lunchbox node
|
|
@@ -22,6 +21,115 @@ function find(target) {
|
|
|
22
21
|
return null;
|
|
23
22
|
}
|
|
24
23
|
|
|
24
|
+
const get = (obj, path, defValue) => {
|
|
25
|
+
// If path is not defined or it has false value
|
|
26
|
+
if (!path) return undefined; // Check if path is string or array. Regex : ensure that we do not have '.' and brackets.
|
|
27
|
+
// Regex explained: https://regexr.com/58j0k
|
|
28
|
+
|
|
29
|
+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g); // Find value
|
|
30
|
+
|
|
31
|
+
const result = pathArray?.reduce((prevObj, key) => prevObj && prevObj[key], obj); // If found value is undefined return default value; otherwise return the value
|
|
32
|
+
|
|
33
|
+
return result === undefined ? defValue : result;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const buildIsNumber = () => {
|
|
37
|
+
/**
|
|
38
|
+
* lodash (Custom Build) <https://lodash.com/>
|
|
39
|
+
* Build: `lodash modularize exports="npm" -o ./`
|
|
40
|
+
* Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
|
|
41
|
+
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
42
|
+
* Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
43
|
+
* Available under MIT license <https://lodash.com/license>
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/** `Object#toString` result references. */
|
|
47
|
+
const numberTag = '[object Number]';
|
|
48
|
+
/** Used for built-in method references. */
|
|
49
|
+
|
|
50
|
+
const objectProto = Object.prototype;
|
|
51
|
+
/**
|
|
52
|
+
* Used to resolve the
|
|
53
|
+
* [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
|
54
|
+
* of values.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
const objectToString = objectProto.toString;
|
|
58
|
+
/**
|
|
59
|
+
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
|
60
|
+
* and has a `typeof` result of "object".
|
|
61
|
+
*
|
|
62
|
+
* @static
|
|
63
|
+
* @memberOf _
|
|
64
|
+
* @since 4.0.0
|
|
65
|
+
* @category Lang
|
|
66
|
+
* @param {*} value The value to check.
|
|
67
|
+
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
|
68
|
+
* @example
|
|
69
|
+
*
|
|
70
|
+
* _.isObjectLike({});
|
|
71
|
+
* // => true
|
|
72
|
+
*
|
|
73
|
+
* _.isObjectLike([1, 2, 3]);
|
|
74
|
+
* // => true
|
|
75
|
+
*
|
|
76
|
+
* _.isObjectLike(_.noop);
|
|
77
|
+
* // => false
|
|
78
|
+
*
|
|
79
|
+
* _.isObjectLike(null);
|
|
80
|
+
* // => false
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
function isObjectLike(value) {
|
|
84
|
+
return !!value && typeof value == 'object';
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Checks if `value` is classified as a `Number` primitive or object.
|
|
88
|
+
*
|
|
89
|
+
* **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are
|
|
90
|
+
* classified as numbers, use the `_.isFinite` method.
|
|
91
|
+
*
|
|
92
|
+
* @static
|
|
93
|
+
* @memberOf _
|
|
94
|
+
* @since 0.1.0
|
|
95
|
+
* @category Lang
|
|
96
|
+
* @param {*} value The value to check.
|
|
97
|
+
* @returns {boolean} Returns `true` if `value` is a number, else `false`.
|
|
98
|
+
* @example
|
|
99
|
+
*
|
|
100
|
+
* _.isNumber(3);
|
|
101
|
+
* // => true
|
|
102
|
+
*
|
|
103
|
+
* _.isNumber(Number.MIN_VALUE);
|
|
104
|
+
* // => true
|
|
105
|
+
*
|
|
106
|
+
* _.isNumber(Infinity);
|
|
107
|
+
* // => true
|
|
108
|
+
*
|
|
109
|
+
* _.isNumber('3');
|
|
110
|
+
* // => false
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
const output = function isNumber(value) {
|
|
115
|
+
return typeof value == 'number' || isObjectLike(value) && objectToString.call(value) == numberTag;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return output;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const isNumber = buildIsNumber();
|
|
122
|
+
|
|
123
|
+
const set = (obj, path, value) => {
|
|
124
|
+
// Regex explained: https://regexr.com/58j0k
|
|
125
|
+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g);
|
|
126
|
+
pathArray?.reduce((acc, key, i) => {
|
|
127
|
+
if (acc[key] === undefined) acc[key] = {};
|
|
128
|
+
if (i === pathArray.length - 1) acc[key] = value;
|
|
129
|
+
return acc[key];
|
|
130
|
+
}, obj);
|
|
131
|
+
};
|
|
132
|
+
|
|
25
133
|
/** Type check on whether target is a Lunchbox.EventKey */
|
|
26
134
|
|
|
27
135
|
const isEventKey = target => {
|
|
@@ -1317,7 +1425,7 @@ function updateObjectProp({
|
|
|
1317
1425
|
// first, save as array in case we need to spread it
|
|
1318
1426
|
|
|
1319
1427
|
|
|
1320
|
-
if (liveProperty && isNumber(value) && liveProperty
|
|
1428
|
+
if (liveProperty && isNumber(value) && liveProperty?.setScalar) {
|
|
1321
1429
|
// if value is a number and the property has a `setScalar` method, use that
|
|
1322
1430
|
liveProperty.setScalar(value);
|
|
1323
1431
|
} else if (liveProperty && liveProperty.set) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lunchboxjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1015",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "vite -c utils/vite.config.ts",
|
|
6
6
|
"build": "vue-tsc --noEmit && vite build -c utils/vite.config.ts",
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
"@rollup/plugin-babel": "5.3.1",
|
|
27
27
|
"@rollup/plugin-node-resolve": "13.0.5",
|
|
28
28
|
"@rollup/plugin-typescript": "8.3.0",
|
|
29
|
-
"@types/lodash": "4.14.175",
|
|
30
29
|
"@types/node": "18.6.3",
|
|
31
30
|
"@types/three": "0.141.0",
|
|
32
31
|
"@types/uuid": "8.3.1",
|
|
@@ -50,8 +49,7 @@
|
|
|
50
49
|
"vue-tsc": "^0.3.0"
|
|
51
50
|
},
|
|
52
51
|
"peerDependencies": {
|
|
53
|
-
"
|
|
54
|
-
"three": "^0.133.0"
|
|
52
|
+
"three": "*"
|
|
55
53
|
},
|
|
56
54
|
"files": [
|
|
57
55
|
"dist",
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
get,
|
|
3
|
+
isEventKey,
|
|
4
|
+
isLunchboxStandardNode,
|
|
5
|
+
isNumber,
|
|
6
|
+
set,
|
|
7
|
+
} from '../utils'
|
|
2
8
|
import { addEventListener } from './interaction'
|
|
3
|
-
import { get, isNumber, set } from 'lodash'
|
|
4
9
|
import type { Lunch } from '..'
|
|
5
10
|
import type { Ref } from 'vue'
|
|
6
11
|
|
|
@@ -50,7 +55,7 @@ export function updateObjectProp({
|
|
|
50
55
|
if (!target) return node
|
|
51
56
|
|
|
52
57
|
// burrow down until we get property to change
|
|
53
|
-
let liveProperty
|
|
58
|
+
let liveProperty: any
|
|
54
59
|
for (let i = 0; i < nestedPropertiesToCheck.length && !liveProperty; i++) {
|
|
55
60
|
const nestedProperty = nestedPropertiesToCheck[i]
|
|
56
61
|
const fullPath = [nestedProperty, finalKey].filter(Boolean).join('.')
|
|
@@ -59,7 +64,7 @@ export function updateObjectProp({
|
|
|
59
64
|
|
|
60
65
|
// change property
|
|
61
66
|
// first, save as array in case we need to spread it
|
|
62
|
-
if (liveProperty && isNumber(value) && liveProperty
|
|
67
|
+
if (liveProperty && isNumber(value) && liveProperty?.setScalar) {
|
|
63
68
|
// if value is a number and the property has a `setScalar` method, use that
|
|
64
69
|
liveProperty.setScalar(value)
|
|
65
70
|
} else if (liveProperty && liveProperty.set) {
|
package/src/utils/get.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const get = <T = unknown>(
|
|
2
|
+
obj: Record<string, any>,
|
|
3
|
+
path: string | string[],
|
|
4
|
+
defValue?: T
|
|
5
|
+
) => {
|
|
6
|
+
// If path is not defined or it has false value
|
|
7
|
+
if (!path) return undefined
|
|
8
|
+
// Check if path is string or array. Regex : ensure that we do not have '.' and brackets.
|
|
9
|
+
// Regex explained: https://regexr.com/58j0k
|
|
10
|
+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g)
|
|
11
|
+
// Find value
|
|
12
|
+
const result = pathArray?.reduce(
|
|
13
|
+
(prevObj: Record<string, any>, key: string) => prevObj && prevObj[key],
|
|
14
|
+
obj
|
|
15
|
+
)
|
|
16
|
+
// If found value is undefined return default value; otherwise return the value
|
|
17
|
+
return result === undefined ? defValue : result
|
|
18
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -2,6 +2,11 @@ import { Lunch } from '..'
|
|
|
2
2
|
|
|
3
3
|
export * from './find'
|
|
4
4
|
|
|
5
|
+
// replacement functions from https://youmightnotneed.com/lodash
|
|
6
|
+
export * from './get'
|
|
7
|
+
export * from './isNumber'
|
|
8
|
+
export * from './set'
|
|
9
|
+
|
|
5
10
|
// MAKE SURE THESE MATCH VALUES IN types.EventKey
|
|
6
11
|
/** Type check on whether target is a Lunchbox.EventKey */
|
|
7
12
|
export const isEventKey = (target: any): target is Lunch.EventKey => {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const buildIsNumber = () => {
|
|
2
|
+
/**
|
|
3
|
+
* lodash (Custom Build) <https://lodash.com/>
|
|
4
|
+
* Build: `lodash modularize exports="npm" -o ./`
|
|
5
|
+
* Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
|
|
6
|
+
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
7
|
+
* Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
8
|
+
* Available under MIT license <https://lodash.com/license>
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** `Object#toString` result references. */
|
|
12
|
+
const numberTag = '[object Number]'
|
|
13
|
+
|
|
14
|
+
/** Used for built-in method references. */
|
|
15
|
+
const objectProto = Object.prototype
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Used to resolve the
|
|
19
|
+
* [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
|
20
|
+
* of values.
|
|
21
|
+
*/
|
|
22
|
+
const objectToString = objectProto.toString
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
|
26
|
+
* and has a `typeof` result of "object".
|
|
27
|
+
*
|
|
28
|
+
* @static
|
|
29
|
+
* @memberOf _
|
|
30
|
+
* @since 4.0.0
|
|
31
|
+
* @category Lang
|
|
32
|
+
* @param {*} value The value to check.
|
|
33
|
+
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
|
34
|
+
* @example
|
|
35
|
+
*
|
|
36
|
+
* _.isObjectLike({});
|
|
37
|
+
* // => true
|
|
38
|
+
*
|
|
39
|
+
* _.isObjectLike([1, 2, 3]);
|
|
40
|
+
* // => true
|
|
41
|
+
*
|
|
42
|
+
* _.isObjectLike(_.noop);
|
|
43
|
+
* // => false
|
|
44
|
+
*
|
|
45
|
+
* _.isObjectLike(null);
|
|
46
|
+
* // => false
|
|
47
|
+
*/
|
|
48
|
+
function isObjectLike(value: any) {
|
|
49
|
+
return !!value && typeof value == 'object'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Checks if `value` is classified as a `Number` primitive or object.
|
|
54
|
+
*
|
|
55
|
+
* **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are
|
|
56
|
+
* classified as numbers, use the `_.isFinite` method.
|
|
57
|
+
*
|
|
58
|
+
* @static
|
|
59
|
+
* @memberOf _
|
|
60
|
+
* @since 0.1.0
|
|
61
|
+
* @category Lang
|
|
62
|
+
* @param {*} value The value to check.
|
|
63
|
+
* @returns {boolean} Returns `true` if `value` is a number, else `false`.
|
|
64
|
+
* @example
|
|
65
|
+
*
|
|
66
|
+
* _.isNumber(3);
|
|
67
|
+
* // => true
|
|
68
|
+
*
|
|
69
|
+
* _.isNumber(Number.MIN_VALUE);
|
|
70
|
+
* // => true
|
|
71
|
+
*
|
|
72
|
+
* _.isNumber(Infinity);
|
|
73
|
+
* // => true
|
|
74
|
+
*
|
|
75
|
+
* _.isNumber('3');
|
|
76
|
+
* // => false
|
|
77
|
+
*/
|
|
78
|
+
const output = function isNumber(value: any) {
|
|
79
|
+
return (
|
|
80
|
+
typeof value == 'number' ||
|
|
81
|
+
(isObjectLike(value) && objectToString.call(value) == numberTag)
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
return output
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const isNumber = buildIsNumber()
|
package/src/utils/set.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const set = (
|
|
2
|
+
obj: Record<string, any>,
|
|
3
|
+
path: string | string[],
|
|
4
|
+
value: any
|
|
5
|
+
) => {
|
|
6
|
+
// Regex explained: https://regexr.com/58j0k
|
|
7
|
+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g)
|
|
8
|
+
|
|
9
|
+
pathArray?.reduce((acc: Record<string, any>, key: string, i: number) => {
|
|
10
|
+
if (acc[key] === undefined) acc[key] = {}
|
|
11
|
+
if (i === pathArray.length - 1) acc[key] = value
|
|
12
|
+
return acc[key]
|
|
13
|
+
}, obj)
|
|
14
|
+
}
|