kitchen-simulator
Version:
It is a kitchen simulator (self-contained micro-frontend).
245 lines (244 loc) • 9.44 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SNAP_SEGMENT = exports.SNAP_POINT = exports.SNAP_MASK = exports.SNAP_LINE = exports.SNAP_GUIDE = exports.SNAP_GRID = void 0;
exports.addGridSnap = addGridSnap;
exports.addLineSegmentSnap = addLineSegmentSnap;
exports.addLineSnap = addLineSnap;
exports.addPointSnap = addPointSnap;
exports.nearestSnap = nearestSnap;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _immutable = require("immutable");
var Geometry = _interopRequireWildcard(require("./geometry"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2["default"])(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; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var SNAP_POINT = exports.SNAP_POINT = 'SNAP_POINT';
var SNAP_LINE = exports.SNAP_LINE = 'SNAP_LINE';
var SNAP_SEGMENT = exports.SNAP_SEGMENT = 'SNAP_SEGMENT';
var SNAP_GRID = exports.SNAP_GRID = 'SNAP_GRID';
var SNAP_GUIDE = exports.SNAP_GUIDE = 'SNAP_GUIDE';
var SNAP_MASK = exports.SNAP_MASK = new _immutable.Map({
SNAP_POINT: true,
SNAP_LINE: true,
SNAP_SEGMENT: true,
SNAP_GRID: false,
SNAP_GUIDE: true
});
var PointSnap = /*#__PURE__*/function (_Record) {
function PointSnap() {
(0, _classCallCheck2["default"])(this, PointSnap);
return _callSuper(this, PointSnap, arguments);
}
(0, _inherits2["default"])(PointSnap, _Record);
return (0, _createClass2["default"])(PointSnap, [{
key: "nearestPoint",
value: function nearestPoint(x, y) {
return {
x: this.x,
y: this.y,
distance: Geometry.pointsDistance(this.x, this.y, x, y)
};
}
}, {
key: "isNear",
value: function isNear(x, y, distance) {
return ~(this.x - x) + 1 < distance && ~(this.y - y) + 1 < distance;
}
}]);
}((0, _immutable.Record)({
type: 'point',
x: -1,
y: -1,
radius: 1,
priority: 1,
related: new _immutable.List()
}));
var LineSnap = /*#__PURE__*/function (_Record2) {
function LineSnap() {
(0, _classCallCheck2["default"])(this, LineSnap);
return _callSuper(this, LineSnap, arguments);
}
(0, _inherits2["default"])(LineSnap, _Record2);
return (0, _createClass2["default"])(LineSnap, [{
key: "nearestPoint",
value: function nearestPoint(x, y) {
return _objectSpread(_objectSpread({}, Geometry.closestPointFromLine(this.a, this.b, this.c, x, y)), {}, {
distance: Geometry.distancePointFromLine(this.a, this.b, this.c, x, y)
});
}
}, {
key: "isNear",
value: function isNear(x, y, distance) {
return true;
}
}]);
}((0, _immutable.Record)({
type: 'line',
a: -1,
b: -1,
c: -1,
radius: 1,
priority: 1,
related: new _immutable.List()
}));
var LineSegmentSnap = /*#__PURE__*/function (_Record3) {
function LineSegmentSnap() {
(0, _classCallCheck2["default"])(this, LineSegmentSnap);
return _callSuper(this, LineSegmentSnap, arguments);
}
(0, _inherits2["default"])(LineSegmentSnap, _Record3);
return (0, _createClass2["default"])(LineSegmentSnap, [{
key: "nearestPoint",
value: function nearestPoint(x, y) {
return _objectSpread(_objectSpread({}, Geometry.closestPointFromLineSegment(this.x1, this.y1, this.x2, this.y2, x, y)), {}, {
distance: Geometry.distancePointFromLineSegment({
x: this.x1,
y: this.y1
}, {
x: this.x2,
y: this.y2
}, x, y)
});
}
}, {
key: "isNear",
value: function isNear(x, y, distance) {
return true;
}
}]);
}((0, _immutable.Record)({
type: 'line-segment',
x1: -1,
y1: -1,
x2: -1,
y2: -1,
radius: 1,
priority: 1,
related: new _immutable.List()
}));
var GridSnap = /*#__PURE__*/function (_Record4) {
function GridSnap() {
(0, _classCallCheck2["default"])(this, GridSnap);
return _callSuper(this, GridSnap, arguments);
}
(0, _inherits2["default"])(GridSnap, _Record4);
return (0, _createClass2["default"])(GridSnap, [{
key: "nearestPoint",
value: function nearestPoint(x, y) {
return {
x: this.x,
y: this.y,
distance: Geometry.pointsDistance(this.x, this.y, x, y)
};
}
}, {
key: "isNear",
value: function isNear(x, y, distance) {
return ~(this.x - x) + 1 < distance && ~(this.y - y) + 1 < distance;
}
}]);
}((0, _immutable.Record)({
type: 'grid',
x: -1,
y: -1,
radius: 1,
priority: 1,
related: new _immutable.List()
}));
function nearestSnap(snapElements, x, y, snapMask) {
var filter = {
point: snapMask.get(SNAP_POINT),
line: snapMask.get(SNAP_LINE),
'line-segment': snapMask.get(SNAP_SEGMENT),
grid: snapMask.get(SNAP_GRID)
};
return snapElements.valueSeq().filter(function (el) {
return filter[el.type] && el.isNear(x, y, el.radius);
}).map(function (snap) {
return {
snap: snap,
point: snap.nearestPoint(x, y)
};
}).filter(function (_ref) {
var radius = _ref.snap.radius,
distance = _ref.point.distance;
return distance < radius;
}).min(function (_ref2, _ref3) {
var p1 = _ref2.snap.priority,
d1 = _ref2.point.distance;
var p2 = _ref3.snap.priority,
d2 = _ref3.point.distance;
return p1 === p2 ? d1 < d2 ? -1 : 1 : p1 > p2 ? -1 : 1;
});
}
function addPointSnap(snapElements, x, y, radius, priority, related) {
related = new _immutable.List([related]);
return snapElements.push(new PointSnap({
x: x,
y: y,
radius: radius,
priority: priority,
related: related
}));
}
function addLineSnap(snapElements, a, b, c, radius, priority, related) {
related = new _immutable.List([related]);
return snapElements.withMutations(function (snapElements) {
var alreadyPresent = snapElements.some(function (lineSnap) {
return lineSnap.type === 'line' && a === lineSnap.a && b === lineSnap.b && c === lineSnap.c;
});
if (alreadyPresent) return snapElements;
var intersections = snapElements.valueSeq().filter(function (snap) {
return snap.type === 'line';
}).map(function (snap) {
return Geometry.twoLinesIntersection(snap.a, snap.b, snap.c, a, b, c);
}).filter(function (intersection) {
return intersection !== undefined;
}).forEach(function (_ref4) {
var x = _ref4.x,
y = _ref4.y;
return addPointSnap(snapElements, x, y, 20, 40);
});
snapElements.push(new LineSnap({
a: a,
b: b,
c: c,
radius: radius,
priority: priority,
related: related
}));
});
}
function addLineSegmentSnap(snapElements, x1, y1, x2, y2, radius, priority, related) {
related = new _immutable.List([related]);
return snapElements.push(new LineSegmentSnap({
x1: x1,
y1: y1,
x2: x2,
y2: y2,
radius: radius,
priority: priority,
related: related
}));
}
function addGridSnap(snapElements, x, y, radius, priority, related) {
related = new _immutable.List([related]);
return snapElements.push(new GridSnap({
x: x,
y: y,
radius: radius,
priority: priority,
related: related
}));
}