UNPKG

@gorpacrate/core-graphics

Version:

A core library for creating shape-based graphic editors

90 lines 4.29 kB
"use strict"; exports.__esModule = true; var i = require("icepick"); var declarations_1 = require("../../declarations"); var data_1 = require("../../utils/data"); var rubber_band_1 = require("../../utils/rubber-band"); var editor_events_1 = require("../editor-events"); var REDUCERS = [ rubberBandEnterReducer, rubberBandStateUpdateReducer, rubberBandFinishingReducer ]; function rubberBandStateReducer(currentState, ev) { return REDUCERS.reduce(function (res, fn) { return fn(res, ev); }, currentState); } exports["default"] = rubberBandStateReducer; function rubberBandEnterReducer(currentState, ev) { var mode = currentState.mode; if (declarations_1.isSelectMode(mode)) { var mouse = currentState.mouse; if (mouse.down && editor_events_1.isEditorInputEvent(ev) && editor_events_1.isMouseMoveEvent(ev)) { var lastDownX = mouse.lastDownX, lastDownY = mouse.lastDownY; var _a = ev.payload, x = _a.x, y = _a.y; var nextMode = declarations_1.modeRubberbandSelection({ pStart: { x: lastDownX, y: lastDownY }, pEnd: { x: x, y: y }, newlySelectedShapes: [], previouslySelectedShapes: [] }); return i.assoc(currentState, 'mode', nextMode); } return currentState; } if (declarations_1.isSelectedMode(mode)) { var mouse = currentState.mouse, modifierKeys = currentState.modifierKeys; if (mouse.down && modifierKeys[editor_events_1.SceneModifierKey.Shift] && editor_events_1.isEditorInputEvent(ev) && editor_events_1.isMouseMoveEvent(ev)) { var ids = mode.payload.ids; var lastDownX = mouse.lastDownX, lastDownY = mouse.lastDownY; var _b = ev.payload, x = _b.x, y = _b.y; var nextMode = declarations_1.modeRubberbandSelection({ pStart: { x: lastDownX, y: lastDownY }, pEnd: { x: x, y: y }, newlySelectedShapes: [], previouslySelectedShapes: ids }); return i.assoc(currentState, 'mode', nextMode); } return currentState; } return currentState; } exports.rubberBandEnterReducer = rubberBandEnterReducer; function rubberBandStateUpdateReducer(currentState, ev) { var mode = currentState.mode; if (declarations_1.isRubberBandSelectionMode(mode)) { var mouse = currentState.mouse; if (mouse.down && editor_events_1.isEditorInputEvent(ev) && editor_events_1.isMouseMoveEvent(ev)) { var _a = ev.payload, x = _a.x, y = _a.y; var _b = mode.payload, pStart = _b.pStart, pEnd = _b.pEnd; var shapesDeclarations = currentState.shapesDeclarations; var _c = currentState.scene, shapes_1 = _c.shapes, shapesOrder = _c.shapesOrder; var shapesData = shapesOrder.map(function (id) { return shapes_1[id]; }); var selectedShapes = rubber_band_1.getShapesSelectedWithRubberBand({ pStart: pStart, pEnd: pEnd, shapesData: shapesData, shapesDeclarations: shapesDeclarations }); var nextMode = i.merge(mode, { payload: { pEnd: { x: x, y: y }, newlySelectedShapes: selectedShapes } }); return i.assoc(currentState, 'mode', nextMode); } return currentState; } return currentState; } exports.rubberBandStateUpdateReducer = rubberBandStateUpdateReducer; function rubberBandFinishingReducer(currentState, ev) { var mode = currentState.mode; if (declarations_1.isRubberBandSelectionMode(mode) && editor_events_1.isEditorInputEvent(ev) && (editor_events_1.isMouseUpEvent(ev) || editor_events_1.isMouseLeaveEvent(ev))) { var _a = mode.payload, newlySelectedShapes = _a.newlySelectedShapes, previouslySelectedShapes = _a.previouslySelectedShapes; var selected = data_1.uniq(previouslySelectedShapes.concat(newlySelectedShapes)); var newMode = declarations_1.modeSelected(selected); return i.assoc(currentState, 'mode', newMode); } return currentState; } exports.rubberBandFinishingReducer = rubberBandFinishingReducer; //# sourceMappingURL=rubber-band-select.js.map