UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

68 lines (67 loc) 2.43 kB
import { computed, signal } from "@preact/signals-core"; import cloneDeep from "lodash/cloneDeep"; export var ECanChangeBlockGeometry; (function (ECanChangeBlockGeometry) { ECanChangeBlockGeometry["ALL"] = "all"; ECanChangeBlockGeometry["ONLY_SELECTED"] = "onlySelected"; ECanChangeBlockGeometry["NONE"] = "none"; })(ECanChangeBlockGeometry || (ECanChangeBlockGeometry = {})); const getInitState = { canDragCamera: true, canZoomCamera: true, canDuplicateBlocks: false, canChangeBlockGeometry: ECanChangeBlockGeometry.NONE, canCreateNewConnections: false, showConnectionArrows: true, scaleFontSize: 1, useBezierConnections: true, bezierConnectionDirection: "horizontal", useBlocksAnchors: true, connectivityComponentOnClickRaise: true, showConnectionLabels: false, blockComponents: {}, }; export class GraphEditorSettings { constructor(rootStore) { this.rootStore = rootStore; this.$settings = signal(getInitState); this.$blockComponents = computed(() => { return this.$settings.value.blockComponents; }); this.$background = computed(() => { return this.$settings.value.background; }); this.$connection = computed(() => { return this.$settings.value.connection; }); this.$connectionsSettings = computed(() => { return { useBezierConnections: this.$settings.value.useBezierConnections, showConnectionLabels: this.$settings.value.showConnectionLabels, canCreateNewConnections: this.$settings.value.canCreateNewConnections, showConnectionArrows: this.$settings.value.showConnectionArrows, bezierConnectionDirection: this.$settings.value.bezierConnectionDirection, }; }); } setupSettings(config) { this.$settings.value = Object.assign({}, this.$settings.value, config); } setConfigFlag(flagPath, value) { if (typeof this.$settings.value[flagPath] === typeof value) { this.$settings.value[flagPath] = value; } } getConfigFlag(flagPath) { return this.$settings.value[flagPath]; } toJSON() { return cloneDeep(this.$settings.toJSON()); } get asConfig() { return this.toJSON(); } reset() { this.setupSettings(getInitState); } }