kitchen-simulator
Version:
It is a kitchen simulator (self-contained micro-frontend).
334 lines (322 loc) • 13 kB
JavaScript
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { HDRCubeTextureLoader } from 'three/examples/jsm/loaders/HDRCubeTextureLoader.js';
import { ANIMATE_OBJECT_OPEN_DOOR_ROTATION_UNIT, ANIMATE_STEP_MAX, DECIMAL_PLACES_2, HDR_URLS, MODE_ELEVATION_VIEW } from "../constants";
import * as Three from 'three';
import { returnReplaceableDeepSearchType } from "./../components/viewer2d/utils";
import { convert } from "./convert-units-lite";
import { formatNumber } from "./math";
import AWS from 'aws-sdk';
var s3 = new AWS.S3({
accessKeyId: process.env.REACT_APP_AWS_ID,
secretAccessKey: process.env.REACT_APP_AWS_SECRET
});
var s3ParseUrl = function s3ParseUrl(url) {
var _decodedUrl = decodeURIComponent(url);
var _result = null;
// http://s3.amazonaws.com/bucket/key1/key2
var match = decodedUrl.match(/^https?:\/\/s3.amazonaws.com\/([^\/]+)\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[1],
key: _match[2],
region: ''
};
}
// http://s3-aws-region.amazonaws.com/bucket/key1/key2
match = decodedUrl.match(/^https?:\/\/s3-([^.]+).amazonaws.com\/([^\/]+)\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[2],
key: _match[3],
region: _match[1]
};
}
// http://bucket.s3.amazonaws.com/key1/key2
match = decodedUrl.match(/^https?:\/\/([^.]+).s3.amazonaws.com\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[1],
key: _match[2],
region: ''
};
}
// http://bucket.s3-aws-region.amazonaws.com/key1/key2 or,
// http://bucket.s3.aws-region.amazonaws.com/key1/key2
match = decodedUrl.match(/^https?:\/\/([^.]+).(?:s3-|s3\.)([^.]+).amazonaws.com\/?(.*?)$/);
if (_match) {
_result = {
bucket: _match[1],
key: _match[3],
region: _match[2]
};
}
return _result;
};
export var textureCube = new HDRCubeTextureLoader().setPath('/catalog/envMap/').load(HDR_URLS, function () {
textureCube.magFilter = Three.LinearFilter;
textureCube.needsUpdate = true;
});
export var getSignedUrl = function getSignedUrl(url) {
if (!url) return url;
var signedUrl = url;
try {
var _s3ParseUrl = s3ParseUrl(url),
bucket = _s3ParseUrl.bucket,
key = _s3ParseUrl.key;
signedUrl = s3.getSignedUrl('getObject', {
Bucket: bucket,
Key: key,
ResponseCacheControl: 'no-cache'
});
} catch (e) {
signedUrl = url;
}
return signedUrl;
};
export var base64Decode = function base64Decode(targetStr) {
if (!targetStr) return null;
try {
// If URL-encoded, decode first
var base64Str = targetStr.includes('%') ? decodeURIComponent(targetStr) : targetStr;
var binaryStr = atob(base64Str);
// Convert binary string to UTF-8 string
return decodeURIComponent(binaryStr.split('').map(function (_char) {
return "%".concat(_char.charCodeAt(0).toString(16).padStart(2, '0'));
}).join(''));
} catch (e) {
console.log('Invalid Base64 or UTF-8 string: ', e);
return null;
}
};
export var refineProductForSummaryPdf = function refineProductForSummaryPdf(refinedProductList, catalog, categoryData, itemSKU, suffix, item, product, measurementUnit, count) {
var _doorColor;
var review;
var doorColor = item.doorStyle;
if (doorColor.size) {
doorColor = doorColor.toJS();
}
var doorStyleName = (_doorColor = doorColor) === null || _doorColor === void 0 ? void 0 : _doorColor.name;
if (catalog !== undefined) {
var _doorColor2, _doorColor4;
if (!((_doorColor2 = doorColor) !== null && _doorColor2 !== void 0 && _doorColor2.install)) {
categoryData.data.doorStyles.items.forEach(function (category) {
category.items.forEach(function (element) {
if (element.items.filter(function (it) {
var _doorColor3;
return it.id === ((_doorColor3 = doorColor) === null || _doorColor3 === void 0 ? void 0 : _doorColor3.id);
})) {
doorColor = _objectSpread(_objectSpread({}, doorColor), {}, {
install: category.name
});
}
});
});
}
review = catalog.elements[item.type];
if (!review) {
review = catalog.elements[returnReplaceableDeepSearchType(item.type)];
}
if (((_doorColor4 = doorColor) === null || _doorColor4 === void 0 || (_doorColor4 = _doorColor4.install) === null || _doorColor4 === void 0 ? void 0 : _doorColor4.toLowerCase()) === 'pre-assembled') {
itemSKU += '-assembled';
} else {
itemSKU += '-rta';
}
var refinedData = {
thumbnail: review.img,
sku: itemSKU,
sizeInfo: {
width: Math.round(convert(review.info.sizeinfo.width).from('in').to(measurementUnit._unit) * 100) / 100,
depth: Math.round(convert(review.info.sizeinfo.depth).from('in').to(measurementUnit._unit) * 100) / 100,
height: Math.round(convert(review.info.sizeinfo.height).from('in').to(measurementUnit._unit) * 100) / 100,
_unit: measurementUnit._unit
},
doorstyle: doorStyleName,
installation: suffix,
subTotal: product && product.discounted_price ? product && Number(product.discounted_price * (count || 1)) : 0
};
var index = (refinedProductList || []).findIndex(function (row) {
return row.sku === itemSKU;
});
if (index !== -1) {
refinedProductList[index] = refinedData;
} else {
refinedProductList.push(refinedData);
}
}
};
export var refineMoldingForSummaryPdf = function refineMoldingForSummaryPdf(refinedProductList, molding, product, newSKU) {
if (molding) {
var review = {
img: molding.thumbnail,
name: molding.name,
count: molding.count
};
var subTotalPrice = product && product.discounted_price ? formatNumber(product.discounted_price * review.count, DECIMAL_PLACES_2) : 0;
var refinedData = {
id: newSKU,
thumbnail: review.img,
sku: review.name,
sizeInfo: null,
subTotal: Number(subTotalPrice)
};
var index = (refinedProductList || []).findIndex(function (row) {
return row.sku === newSKU;
});
if (index !== -1) {
refinedProductList[index] = refinedData;
} else {
refinedProductList.push(refinedData);
}
}
};
export function applyEdges(object) {
if (object && object.isMesh && !(object instanceof Three.LineSegments)) {
var parent = object.parent;
if (!isEmpty(parent)) {
if (object.name.match('interior_drawer') || object.name.match('interior') || object.name.match('soul')) {
parent.remove(object);
} else {
// Create a replacement
var edges = new Three.EdgesGeometry(object.geometry, 10);
var lineMaterial = new Three.LineBasicMaterial({
color: 0x000000,
depthTest: true,
depthFunc: Three.LessEqualDepth,
depthWrite: false
});
var line = new Three.LineSegments(edges, lineMaterial);
line.position.copy(object.position);
line.scale.copy(object.scale);
line.rotation.copy(object.rotation);
line.name = object.name;
// Replace in parent
parent.remove(object);
parent.add(line);
}
}
}
}
export function replaceMeshesWithLineSegments(object) {
if (object && object.children.length > 0) {
for (var i = object.children.length - 1; i >= 0; i--) {
var child = object.children[i];
// Recursively process children first
replaceMeshesWithLineSegments(child);
}
} else {
//
// Check if the child is a THREE.Mesh
applyEdges(object);
}
}
export var isEmpty = function isEmpty(value) {
return value === undefined || value === null || typeof value === 'array' && value.length === 0 || _typeof(value) === 'object' && Object.keys(value).length === 0 || typeof value === 'string' && value.trim().length === 0;
};
// animate the Opening or Closing Door action
export var animateDoor = function animateDoor(offsetData, doorObj, open_doors, item, isCorner, mode) {
var offset = new Three.Vector3(offsetData.x, offsetData.y, offsetData === null || offsetData === void 0 ? void 0 : offsetData.z);
doorObj.translateOnAxis(offset, 1);
var rotation = doorObj.name.match(/\d_(door[^LR1-9]*)(_[LR])?(_[1-9])?$/)[2] === '_L' ? -ANIMATE_OBJECT_OPEN_DOOR_ROTATION_UNIT : ANIMATE_OBJECT_OPEN_DOOR_ROTATION_UNIT;
var isflip = item.properties.flip_doorhandle;
rotation = isflip ? -rotation : rotation;
if (isCorner == 1) {
if (!doorObj.name.includes('base_end')) {
rotation *= 2;
}
var tmp = doorObj.children[0].children.filter(function (item) {
return item.children.length != 0;
});
if (tmp.length != 0) {
rotation = open_doors ? -rotation : rotation;
} else {
rotation = open_doors ? rotation : -rotation;
}
} else {
rotation = open_doors ? rotation : -rotation;
}
if (mode === 'Opened2D') rotation *= ANIMATE_STEP_MAX; // Check whether the object's door is opened in 2D.
doorObj.rotateY(rotation);
doorObj.translateOnAxis(offset, -1);
};
// Opening or Closing the drawer action
export var translateDrawer = function translateDrawer(item, drawerObj, open_doors, mode) {
var animate_translate_unit = convert(item.properties.depth._length).from(item.properties.depth._unit).to('cm') * (1 / 2) / ANIMATE_STEP_MAX * 0.01;
// Check whether the object's drawer is opened in 2D.
if (mode === 'Opened2D') animate_translate_unit *= ANIMATE_STEP_MAX;
drawerObj.translateZ(open_doors ? -animate_translate_unit : animate_translate_unit);
};
export var getPath = function getPath() {
return window.location.pathname;
};
export var getPathInfo = function getPathInfo(num) {
var paths = window.location.pathname.split('/');
if (paths.length >= num + 1) {
return paths[num];
} else {
return paths[num - 1 >= 0 ? num - 1 : 0];
}
};
// determine if elevation mode or not
export function isElevationView(mode) {
if (mode === MODE_ELEVATION_VIEW) return true;else return false;
}
// calculate camera rect in elevation mode
/**
*
* @param {number} width width of elevation view - canvas
* @param {number} height height of elevation view - canvas
* @param {number} ceilHeight ceiling Height
* @param {number} lineLength wall's length
* @returns {object}
*/
//** elevation view (canvas) **//
//// h /////////////////////// width //////////////////////// h ////
//// e //// **c***************lineLength*************c** //// e ////
//// i //// **e e** //// i ////
//// g //// **i i** //// g ////
//// h //// **l***************lineLength*************l** //// h ////
//// t /////////////////////// width //////////////////////// t ////
export function handleCamRect(width, height, ceilHeight, lineLength) {
var scale = Math.min(width * 0.6 / lineLength, height * 0.6 / ceilHeight); // scale is min value
var cameraRect = {
width: width / scale,
height: height / scale
};
return cameraRect;
}
/**
*
* @param {*} layer
* @param {*} ceilHeight
* @param {*} ceilUnit
* @returns true ceilingHeight is bigger than totalHeight(item's height + altitude), otherwise false
*/
export function isCeilLimitation(layer, ceilHeight, ceilUnit) {
var items = layer.getIn(['layers', layer.selectedLayer, 'items']);
return items.some(function (item) {
var height = item.properties.getIn(['height', '_length']);
var heightUnit = item.properties.getIn(['height', '_unit']);
var altitude = item.properties.getIn(['altitude', '_length']);
var altitudeUnit = item.properties.getIn(['altitude', '_unit']);
var totalHeight = convert(height).from(heightUnit).to(ceilUnit._unit) + convert(altitude).from(altitudeUnit).to(ceilUnit._unit);
return totalHeight > ceilHeight;
});
}
export function isValidNumber(n) {
return !isNaN(n) && isFinite(n);
}
export function toPlainObject(maybeImmutable) {
if (maybeImmutable && typeof maybeImmutable.toJS === 'function') {
return maybeImmutable.toJS();
}
return maybeImmutable; // already a plain JS object
}
export function isImmutable(maybeImmutable) {
return !!(maybeImmutable && typeof maybeImmutable.toJS === 'function');
}
export function debugUtil() {
return new Date();
}