js2flowchart
Version:
> Why? While I've been working on [Under-the-hood-ReactJS](https://github.com/Bogdan-Lyashenko/Under-the-hood-ReactJS) I spent enormous amount of time on creating schemes. Each change in code or flowchart affects all entire scheme instantly, forcing you t
112 lines • 6.49 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { TOKEN_KEYS, TOKEN_TYPES } from 'shared/constants';
import { getRhombus, getRoundedRectangle, getText } from 'shared/utils/svgPrimitives';
import { assignState } from 'shared/utils/composition';
import { setupBasicBehaviour, setupInitialSelectors, calculateBackPoint, calculateBoundaries, calculatePosition, delegateInit } from './BaseShape';
import { calculateDimensions, calculateFromPoint, calculateChildOffsetPoint } from './Rhombus';
var ENTITY_FIELD_NAME = 'ConditionRhombus';
var calculateAlternateFromPoint = function calculateAlternateFromPoint(_ref) {
var position = _ref.position,
dimensions = _ref.dimensions;
return {
x: position.x + dimensions.w,
y: position.y + dimensions.h / 2
};
};
var calculateToPoint = function calculateToPoint(_ref2) {
var position = _ref2.position,
dimensions = _ref2.dimensions;
return {
x: position.x,
y: position.y + dimensions.h / 2
};
};
var setupInitialProperties = function setupInitialProperties(state) {
return {
fromPoint: calculateFromPoint(state),
childOffsetPoint: calculateChildOffsetPoint(state),
toPoint: calculateToPoint(state),
backPoint: calculateBackPoint(state),
boundaries: calculateBoundaries(state),
alternateFromPoint: calculateAlternateFromPoint(state)
};
};
var setupAdditionalSelectors = function setupAdditionalSelectors(state) {
return {
getAlternateFromPoint: function getAlternateFromPoint() {
return state.alternateFromPoint;
}
};
};
export var setupConditionRhombusBehavior = function setupConditionRhombusBehavior(state) {
return {
getConsequentBranchChildBoundary: function getConsequentBranchChildBoundary() {
return this.getChildBoundaries(function (child) {
return child.state.node.key === TOKEN_KEYS.CONSEQUENT;
});
},
getAlternativeBranchChildOffsetPoint: function getAlternativeBranchChildOffsetPoint() {
var theme = state.theme,
position = {};
position.y = state.position.y + state.childOffsetPoint.y;
position.x = this.getConsequentBranchChildBoundary().max.x;
position.x += theme.alternateBranchOffset;
var rightLimit = state.position.x + state.dimensions.w + theme.childOffset;
if (position.x <= rightLimit) {
position.x = rightLimit;
}
return position;
},
checkIfChildExist: function checkIfChildExist(key) {
return state.body.filter(function (shape) {
return shape.getNodeKey() === key;
}).length;
},
printConditionMarks: function printConditionMarks() {
var theme = state.theme;
var _state$position = state.position,
x = _state$position.x,
y = _state$position.y,
R = state.dimensions.h,
w = state.dimensions.w,
node = state.node;
var text = node.subType === TOKEN_TYPES.CONDITIONAL_EXPRESSION ? '?' : 'if',
positive = '+',
alternative = '-';
return "".concat(getText(x + R / 2 - text.length * theme.symbolWidth / 2, y + R / 2 + theme.symbolHeight / 2, theme, text), " ").concat(getText(x + R / 2 + theme.symbolWidth, y + R + theme.symbolWidth / 4, theme, positive), " ").concat(this.checkIfChildExist(TOKEN_KEYS.ALTERNATE) ? getText(x + w + theme.symbolWidth / 2, y + R / 2 - theme.symbolWidth / 4, theme, alternative) : '');
},
print: function print(config) {
var theme = state.theme,
_state$position2 = state.position,
x = _state$position2.x,
y = _state$position2.y,
_state$dimensions = state.dimensions,
w = _state$dimensions.w,
h = _state$dimensions.h;
var R = h,
rH = h - 2 * theme.thinPartOffset;
var namePosition = {
x: x + R,
y: y + rH / 2
};
return "<g>\n ".concat(getRoundedRectangle(x + h / 2, y + h / 4, w - R / 2, rH, theme), " \n ").concat(getRhombus(x, y, R, R, theme), "\n ").concat(this.printName(namePosition), "\n ").concat(this.printDebugInfo(config), "\n ").concat(this.printConditionMarks(), "\n </g>");
}
};
};
var extractBasicState = function extractBasicState(state) {
return _objectSpread(_objectSpread({}, state), {}, {
position: calculatePosition(state),
dimensions: calculateDimensions(state)
});
};
export var ConditionRhombus = function ConditionRhombus(initialState) {
var state = extractBasicState(initialState);
state = _objectSpread(_objectSpread({}, state), setupInitialProperties(state));
return assignState(state, [setupInitialSelectors, setupAdditionalSelectors, setupBasicBehaviour, setupConditionRhombusBehavior]);
};
export default delegateInit(ConditionRhombus, ENTITY_FIELD_NAME);