| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833 |
- import { createSelectorHook, createState } from '@state-designer/react'
- import { updateFromCode } from './code/generate'
- import { createShape, getShapeUtils } from './shape-utils'
- import vec from 'utils/vec'
- import inputs from './inputs'
- import history from './history'
- import storage from './storage'
- import clipboard from './clipboard'
- import * as Sessions from './sessions'
- import commands from './commands'
- import {
- getChildren,
- getCommonBounds,
- getCurrent,
- getCurrentCamera,
- getPage,
- getSelectedBounds,
- getSelectedShapes,
- getShape,
- screenToWorld,
- setZoomCSS,
- rotateBounds,
- getBoundsCenter,
- getDocumentBranch,
- getCameraZoom,
- getSelectedIds,
- setSelectedIds,
- getPageState,
- setToArray,
- } from 'utils/utils'
- import {
- Data,
- PointerInfo,
- Shape,
- ShapeType,
- Corner,
- Edge,
- CodeControl,
- MoveType,
- ShapeStyles,
- DistributeType,
- AlignType,
- StretchType,
- DashStyle,
- SizeStyle,
- ColorStyle,
- } from 'types'
- import session from './session'
- import { pointInBounds } from 'utils/hitTests'
-
- const initialData: Data = {
- isReadOnly: false,
- settings: {
- fontSize: 13,
- isDarkMode: false,
- isCodeOpen: false,
- isStyleOpen: false,
- isToolLocked: false,
- isPenLocked: false,
- nudgeDistanceLarge: 10,
- nudgeDistanceSmall: 1,
- },
- currentStyle: {
- size: SizeStyle.Medium,
- color: ColorStyle.Black,
- dash: DashStyle.Solid,
- isFilled: false,
- },
- activeTool: 'select',
- brush: undefined,
- boundsRotation: 0,
- pointedId: null,
- hoveredId: null,
- editingId: null,
- currentPageId: 'page1',
- currentParentId: 'page1',
- currentCodeFileId: 'file0',
- codeControls: {},
- document: {
- id: '0001',
- name: 'My Document',
- pages: {
- page1: {
- id: 'page1',
- type: 'page',
- name: 'Page 1',
- childIndex: 0,
- shapes: {},
- },
- },
- code: {
- file0: {
- id: 'file0',
- name: 'index.ts',
- code: ``,
- },
- },
- },
- pageStates: {
- page1: {
- id: 'page1',
- selectedIds: new Set([]),
- camera: {
- point: [0, 0],
- zoom: 1,
- },
- },
- },
- }
-
- const state = createState({
- data: initialData,
- on: {
- UNMOUNTED: { to: 'loading' },
- },
- initial: 'loading',
- states: {
- loading: {
- on: {
- MOUNTED: {
- do: 'restoreSavedData',
- to: 'ready',
- },
- },
- },
- ready: {
- onEnter: {
- wait: 0.01,
- if: 'hasSelection',
- do: 'zoomCameraToSelectionActual',
- else: ['zoomCameraToActual'],
- },
- on: {
- COPIED: { if: 'hasSelection', do: 'copyToClipboard' },
- PASTED: { do: 'pasteFromClipboard' },
- PASTED_SHAPES_FROM_CLIPBOARD: 'pasteShapesFromClipboard',
- LOADED_FONTS: 'resetShapes',
- TOGGLED_SHAPE_LOCK: { if: 'hasSelection', do: 'lockSelection' },
- TOGGLED_SHAPE_HIDE: { if: 'hasSelection', do: 'hideSelection' },
- TOGGLED_SHAPE_ASPECT_LOCK: {
- if: 'hasSelection',
- do: 'aspectLockSelection',
- },
- TOGGLED_CODE_PANEL_OPEN: 'toggleCodePanel',
- TOGGLED_STYLE_PANEL_OPEN: 'toggleStylePanel',
- POINTED_CANVAS: ['closeStylePanel', 'clearCurrentParentId'],
- CHANGED_STYLE: ['updateStyles', 'applyStylesToSelection'],
- USED_PEN_DEVICE: 'enablePenLock',
- DISABLED_PEN_LOCK: 'disablePenLock',
- CLEARED_PAGE: {
- if: 'hasSelection',
- do: 'deleteSelection',
- else: ['selectAll', 'deleteSelection'],
- },
- SELECTED_ALL: { to: 'selecting', do: 'selectAll' },
- CHANGED_PAGE: 'changePage',
- CREATED_PAGE: ['clearSelectedIds', 'createPage'],
- DELETED_PAGE: { unless: 'hasOnlyOnePage', do: 'deletePage' },
- LOADED_FROM_FILE: ['loadDocumentFromJson', 'resetHistory'],
- PANNED_CAMERA: 'panCamera',
- SELECTED_SELECT_TOOL: { to: 'selecting' },
- SELECTED_DRAW_TOOL: { unless: 'isReadOnly', to: 'draw' },
- SELECTED_ARROW_TOOL: { unless: 'isReadOnly', to: 'arrow' },
- SELECTED_DOT_TOOL: { unless: 'isReadOnly', to: 'dot' },
- SELECTED_ELLIPSE_TOOL: { unless: 'isReadOnly', to: 'ellipse' },
- SELECTED_RAY_TOOL: { unless: 'isReadOnly', to: 'ray' },
- SELECTED_LINE_TOOL: { unless: 'isReadOnly', to: 'line' },
- SELECTED_POLYLINE_TOOL: { unless: 'isReadOnly', to: 'polyline' },
- SELECTED_RECTANGLE_TOOL: { unless: 'isReadOnly', to: 'rectangle' },
- SELECTED_TEXT_TOOL: { unless: 'isReadOnly', to: 'text' },
- },
- initial: 'selecting',
- states: {
- selecting: {
- onEnter: ['setActiveToolSelect', 'clearInputs'],
- on: {
- UNDO: 'undo',
- REDO: 'redo',
- SAVED: 'forceSave',
- LOADED_FROM_FILE_STSTEM: 'loadFromFileSystem',
- SAVED_TO_FILESYSTEM: 'saveToFileSystem',
- SAVED_AS_TO_FILESYSTEM: 'saveAsToFileSystem',
- SAVED_CODE: 'saveCode',
- DELETED: 'deleteSelection',
- INCREASED_CODE_FONT_SIZE: 'increaseCodeFontSize',
- DECREASED_CODE_FONT_SIZE: 'decreaseCodeFontSize',
- CHANGED_CODE_CONTROL: 'updateControls',
- GENERATED_FROM_CODE: ['setCodeControls', 'setGeneratedShapes'],
- TOGGLED_TOOL_LOCK: 'toggleToolLock',
- MOVED_TO_PAGE: {
- if: 'hasSelection',
- do: ['moveSelectionToPage', 'zoomCameraToSelectionActual'],
- },
- MOVED: { if: 'hasSelection', do: 'moveSelection' },
- DUPLICATED: { if: 'hasSelection', do: 'duplicateSelection' },
- ROTATED_CCW: { if: 'hasSelection', do: 'rotateSelectionCcw' },
- ALIGNED: { if: 'hasMultipleSelection', do: 'alignSelection' },
- STRETCHED: { if: 'hasMultipleSelection', do: 'stretchSelection' },
- DISTRIBUTED: {
- if: 'hasMultipleSelection',
- do: 'distributeSelection',
- },
- GROUPED: { if: 'hasMultipleSelection', do: 'groupSelection' },
- UNGROUPED: {
- if: ['hasSelection', 'selectionIncludesGroups'],
- do: 'ungroupSelection',
- },
- NUDGED: { do: 'nudgeSelection' },
- ZOOMED_CAMERA: {
- do: 'zoomCamera',
- },
- ZOOMED_TO_ACTUAL: {
- if: 'hasSelection',
- do: 'zoomCameraToSelectionActual',
- else: 'zoomCameraToActual',
- },
- ZOOMED_TO_SELECTION: {
- if: 'hasSelection',
- do: 'zoomCameraToSelection',
- },
- ZOOMED_TO_FIT: ['zoomCameraToFit', 'zoomCameraToActual'],
- ZOOMED_IN: 'zoomIn',
- ZOOMED_OUT: 'zoomOut',
- RESET_CAMERA: 'resetCamera',
- COPIED_TO_SVG: 'copyToSvg',
- },
- initial: 'notPointing',
- states: {
- notPointing: {
- on: {
- CANCELLED: 'clearSelectedIds',
- STARTED_PINCHING: { to: 'pinching' },
- POINTED_CANVAS: { to: 'brushSelecting' },
- POINTED_BOUNDS: [
- {
- if: 'isPressingMetaKey',
- to: 'brushSelecting',
- },
- { to: 'pointingBounds' },
- ],
- POINTED_BOUNDS_HANDLE: {
- if: 'isPointingRotationHandle',
- to: 'rotatingSelection',
- else: { to: 'transformingSelection' },
- },
- STARTED_EDITING_SHAPE: {
- get: 'firstSelectedShape',
- if: ['hasSingleSelection', 'canEditSelectedShape'],
- do: 'setEditingId',
- to: 'editingShape',
- },
- DOUBLE_POINTED_BOUNDS_HANDLE: {
- if: 'hasSingleSelection',
- do: 'resetShapeBounds',
- },
- POINTED_HANDLE: { to: 'translatingHandles' },
- MOVED_OVER_SHAPE: {
- if: 'pointHitsShape',
- then: {
- unless: 'shapeIsHovered',
- do: 'setHoveredId',
- },
- else: {
- if: 'shapeIsHovered',
- do: 'clearHoveredId',
- },
- },
- UNHOVERED_SHAPE: 'clearHoveredId',
- DOUBLE_POINTED_SHAPE: [
- 'setPointedId',
- {
- if: 'isPointedShapeSelected',
- then: {
- get: 'firstSelectedShape',
- if: 'canEditSelectedShape',
- do: 'setEditingId',
- to: 'editingShape',
- },
- },
- {
- unless: 'isPressingShiftKey',
- do: [
- 'setDrilledPointedId',
- 'clearSelectedIds',
- 'pushPointedIdToSelectedIds',
- ],
- to: 'pointingBounds',
- },
- ],
- POINTED_SHAPE: [
- {
- if: 'isPressingMetaKey',
- to: 'brushSelecting',
- },
- 'setPointedId',
- {
- if: 'pointInSelectionBounds',
- to: 'pointingBounds',
- },
- {
- unless: 'isPointedShapeSelected',
- then: {
- if: 'isPressingShiftKey',
- do: ['pushPointedIdToSelectedIds', 'clearPointedId'],
- else: ['clearSelectedIds', 'pushPointedIdToSelectedIds'],
- },
- },
- {
- to: 'pointingBounds',
- },
- ],
- RIGHT_POINTED: [
- {
- if: 'isPointingCanvas',
- do: 'clearSelectedIds',
- else: {
- if: 'isPointingShape',
- then: [
- 'setPointedId',
- {
- unless: 'isPointedShapeSelected',
- do: [
- 'clearSelectedIds',
- 'pushPointedIdToSelectedIds',
- ],
- },
- ],
- },
- },
- ],
- },
- },
- pointingBounds: {
- on: {
- STOPPED_POINTING_BOUNDS: [],
- STOPPED_POINTING: [
- {
- if: 'isPointingBounds',
- do: 'clearSelectedIds',
- },
- {
- if: 'isPressingShiftKey',
- then: [
- {
- if: 'isPointedShapeSelected',
- do: 'pullPointedIdFromSelectedIds',
- },
- ],
- else: {
- unless: 'isPointingBounds',
- do: ['clearSelectedIds', 'pushPointedIdToSelectedIds'],
- },
- },
- { to: 'notPointing' },
- ],
- MOVED_POINTER: {
- unless: 'isReadOnly',
- if: 'distanceImpliesDrag',
- to: 'translatingSelection',
- },
- },
- },
- rotatingSelection: {
- onEnter: 'startRotateSession',
- onExit: ['completeSession', 'clearBoundsRotation'],
- on: {
- MOVED_POINTER: 'updateRotateSession',
- PANNED_CAMERA: 'updateRotateSession',
- PRESSED_SHIFT_KEY: 'keyUpdateRotateSession',
- RELEASED_SHIFT_KEY: 'keyUpdateRotateSession',
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { do: 'cancelSession', to: 'selecting' },
- },
- },
- transformingSelection: {
- onEnter: 'startTransformSession',
- onExit: 'completeSession',
- on: {
- // MOVED_POINTER: 'updateTransformSession', using hacks.fastTransform
- PANNED_CAMERA: 'updateTransformSession',
- PRESSED_SHIFT_KEY: 'keyUpdateTransformSession',
- RELEASED_SHIFT_KEY: 'keyUpdateTransformSession',
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { do: 'cancelSession', to: 'selecting' },
- },
- },
- translatingSelection: {
- onEnter: 'startTranslateSession',
- onExit: 'completeSession',
- on: {
- STARTED_PINCHING: { to: 'pinching' },
- MOVED_POINTER: 'updateTranslateSession',
- PANNED_CAMERA: 'updateTranslateSession',
- PRESSED_SHIFT_KEY: 'keyUpdateTranslateSession',
- RELEASED_SHIFT_KEY: 'keyUpdateTranslateSession',
- PRESSED_ALT_KEY: 'keyUpdateTranslateSession',
- RELEASED_ALT_KEY: 'keyUpdateTranslateSession',
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { do: 'cancelSession', to: 'selecting' },
- },
- },
- translatingHandles: {
- onEnter: 'startHandleSession',
- onExit: 'completeSession',
- on: {
- MOVED_POINTER: 'updateHandleSession',
- PANNED_CAMERA: 'updateHandleSession',
- PRESSED_SHIFT_KEY: 'keyUpdateHandleSession',
- RELEASED_SHIFT_KEY: 'keyUpdateHandleSession',
- STOPPED_POINTING: { to: 'selecting' },
- DOUBLE_POINTED_HANDLE: {
- do: ['cancelSession', 'doublePointHandle'],
- to: 'selecting',
- },
- CANCELLED: { do: 'cancelSession', to: 'selecting' },
- },
- },
- brushSelecting: {
- onExit: 'completeSession',
- onEnter: [
- {
- unless: ['isPressingMetaKey', 'isPressingShiftKey'],
- do: 'clearSelectedIds',
- },
- 'clearBoundsRotation',
- 'startBrushSession',
- ],
- on: {
- // MOVED_POINTER: 'updateBrushSession', using hacks.fastBrushSelect
- PANNED_CAMERA: 'updateBrushSession',
- STOPPED_POINTING: { to: 'selecting' },
- STARTED_PINCHING: { to: 'pinching' },
- CANCELLED: { do: 'cancelSession', to: 'selecting' },
- },
- },
- },
- },
- editingShape: {
- onEnter: 'startEditSession',
- onExit: ['completeSession', 'clearEditingId'],
- on: {
- EDITED_SHAPE: { do: 'updateEditSession' },
-
- BLURRED_EDITING_SHAPE: { to: 'selecting' },
- CANCELLED: [
- {
- get: 'editingShape',
- if: 'shouldDeleteShape',
- do: 'breakSession',
- else: 'cancelSession',
- },
- { to: 'selecting' },
- ],
- },
- },
- pinching: {
- on: {
- // PINCHED: { do: 'pinchCamera' }, using hacks.fastPinchCamera
- },
- initial: 'selectPinching',
- onExit: { secretlyDo: 'updateZoomCSS' },
- states: {
- selectPinching: {
- on: {
- STOPPED_PINCHING: { to: 'selecting' },
- },
- },
- toolPinching: {
- on: {
- STOPPED_PINCHING: { to: 'usingTool.previous' },
- },
- },
- },
- },
- usingTool: {
- initial: 'draw',
- onEnter: 'clearSelectedIds',
- on: {
- STARTED_PINCHING: {
- do: 'breakSession',
- to: 'pinching.toolPinching',
- },
- TOGGLED_TOOL_LOCK: 'toggleToolLock',
- },
- states: {
- draw: {
- onEnter: 'setActiveToolDraw',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: {
- get: 'newDraw',
- do: 'createShape',
- to: 'draw.editing',
- },
- POINTED_CANVAS: {
- get: 'newDraw',
- do: 'createShape',
- to: 'draw.editing',
- },
- UNDO: 'undo',
- REDO: 'redo',
- SAVED: 'forceSave',
- LOADED_FROM_FILE_STSTEM: 'loadFromFileSystem',
- SAVED_TO_FILESYSTEM: 'saveToFileSystem',
- SAVED_AS_TO_FILESYSTEM: 'saveAsToFileSystem',
- },
- },
- editing: {
- onEnter: 'startDrawSession',
- onExit: 'completeSession',
- on: {
- CANCELLED: {
- do: 'breakSession',
- to: 'selecting',
- },
- STOPPED_POINTING: {
- do: 'completeSession',
- to: 'draw.creating',
- },
- PRESSED_SHIFT: 'keyUpdateDrawSession',
- RELEASED_SHIFT: 'keyUpdateDrawSession',
- // MOVED_POINTER: 'updateDrawSession',
- PANNED_CAMERA: 'updateDrawSession',
- },
- },
- },
- },
- dot: {
- onEnter: 'setActiveToolDot',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: {
- get: 'newDot',
- do: 'createShape',
- to: 'dot.editing',
- },
- POINTED_CANVAS: {
- get: 'newDot',
- do: 'createShape',
- to: 'dot.editing',
- },
- },
- },
- editing: {
- on: {
- STOPPED_POINTING: [
- 'completeSession',
- {
- if: 'isToolLocked',
- to: 'dot.creating',
- else: { to: 'selecting' },
- },
- ],
- CANCELLED: {
- do: 'breakSession',
- to: 'selecting',
- },
- },
- initial: 'inactive',
- states: {
- inactive: {
- on: {
- MOVED_POINTER: {
- if: 'distanceImpliesDrag',
- to: 'dot.editing.active',
- },
- },
- },
- active: {
- onExit: 'completeSession',
- onEnter: 'startTranslateSession',
- on: {
- MOVED_POINTER: 'updateTranslateSession',
- PANNED_CAMERA: 'updateTranslateSession',
- },
- },
- },
- },
- },
- },
- arrow: {
- onEnter: 'setActiveToolArrow',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: {
- get: 'newArrow',
- do: 'createShape',
- to: 'arrow.editing',
- },
- POINTED_CANVAS: {
- get: 'newArrow',
- do: 'createShape',
- to: 'arrow.editing',
- },
- UNDO: { do: 'undo' },
- REDO: { do: 'redo' },
- },
- },
- editing: {
- onExit: 'completeSession',
- onEnter: 'startArrowSession',
- on: {
- STOPPED_POINTING: [
- 'completeSession',
- {
- if: 'isToolLocked',
- to: 'arrow.creating',
- else: { to: 'selecting' },
- },
- ],
- CANCELLED: {
- do: 'breakSession',
- if: 'isToolLocked',
- to: 'arrow.creating',
- else: { to: 'selecting' },
- },
- PRESSED_SHIFT: 'keyUpdateArrowSession',
- RELEASED_SHIFT: 'keyUpdateArrowSession',
- MOVED_POINTER: 'updateArrowSession',
- PANNED_CAMERA: 'updateArrowSession',
- },
- },
- },
- },
- ellipse: {
- onEnter: 'setActiveToolEllipse',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_CANVAS: {
- to: 'ellipse.editing',
- },
- },
- },
- editing: {
- on: {
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { to: 'selecting' },
- MOVED_POINTER: {
- if: 'distanceImpliesDrag',
- then: {
- get: 'newEllipse',
- do: 'createShape',
- to: 'drawingShape.bounds',
- },
- },
- },
- },
- },
- },
- rectangle: {
- onEnter: 'setActiveToolRectangle',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: {
- to: 'rectangle.editing',
- },
- POINTED_CANVAS: {
- to: 'rectangle.editing',
- },
- },
- },
- editing: {
- on: {
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { to: 'selecting' },
- MOVED_POINTER: {
- if: 'distanceImpliesDrag',
- then: {
- get: 'newRectangle',
- do: 'createShape',
- to: 'drawingShape.bounds',
- },
- },
- },
- },
- },
- },
- text: {
- onEnter: 'setActiveToolText',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: [
- {
- get: 'newText',
- do: 'createShape',
- },
- {
- get: 'firstSelectedShape',
- if: 'canEditSelectedShape',
- do: 'setEditingId',
- to: 'editingShape',
- },
- ],
- POINTED_CANVAS: [
- {
- get: 'newText',
- do: 'createShape',
- to: 'editingShape',
- },
- ],
- },
- },
- },
- },
- ray: {
- onEnter: 'setActiveToolRay',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: {
- get: 'newRay',
- do: 'createShape',
- to: 'ray.editing',
- },
- POINTED_CANVAS: {
- get: 'newRay',
- do: 'createShape',
- to: 'ray.editing',
- },
- },
- },
- editing: {
- on: {
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { to: 'selecting' },
- MOVED_POINTER: {
- if: 'distanceImpliesDrag',
- to: 'drawingShape.direction',
- },
- },
- },
- },
- },
- line: {
- onEnter: 'setActiveToolLine',
- initial: 'creating',
- states: {
- creating: {
- on: {
- CANCELLED: { to: 'selecting' },
- POINTED_SHAPE: {
- get: 'newLine',
- do: 'createShape',
- to: 'line.editing',
- },
- POINTED_CANVAS: {
- get: 'newLine',
- do: 'createShape',
- to: 'line.editing',
- },
- },
- },
- editing: {
- on: {
- STOPPED_POINTING: { to: 'selecting' },
- CANCELLED: { to: 'selecting' },
- MOVED_POINTER: {
- if: 'distanceImpliesDrag',
- to: 'drawingShape.direction',
- },
- },
- },
- },
- },
- polyline: {
- onEnter: 'setActiveToolPolyline',
- },
- },
- },
- drawingShape: {
- onExit: 'completeSession',
- on: {
- STOPPED_POINTING: [
- 'completeSession',
- {
- if: 'isToolLocked',
- to: 'usingTool.previous',
- else: { to: 'selecting' },
- },
- ],
- CANCELLED: {
- do: 'breakSession',
- to: 'selecting',
- },
- },
- initial: 'drawingShapeBounds',
- states: {
- bounds: {
- onEnter: 'startDrawTransformSession',
- on: {
- MOVED_POINTER: 'updateTransformSession',
- PANNED_CAMERA: 'updateTransformSession',
- },
- },
- direction: {
- onEnter: 'startDirectionSession',
- onExit: 'completeSession',
- on: {
- MOVED_POINTER: 'updateDirectionSession',
- PANNED_CAMERA: 'updateDirectionSession',
- },
- },
- },
- },
- },
- },
- },
- results: {
- newDot() {
- return ShapeType.Dot
- },
- newRay() {
- return ShapeType.Ray
- },
- newLine() {
- return ShapeType.Line
- },
- newText() {
- return ShapeType.Text
- },
- newDraw() {
- return ShapeType.Draw
- },
- newArrow() {
- return ShapeType.Arrow
- },
- newEllipse() {
- return ShapeType.Ellipse
- },
- newRectangle() {
- return ShapeType.Rectangle
- },
- firstSelectedShape(data) {
- return getSelectedShapes(data)[0]
- },
- editingShape(data) {
- return getShape(data, data.editingId)
- },
- },
- conditions: {
- shouldDeleteShape(data, payload, shape: Shape) {
- return getShapeUtils(shape).shouldDelete(shape)
- },
- isPointingCanvas(data, payload: PointerInfo) {
- return payload.target === 'canvas'
- },
- isPointingBounds(data, payload: PointerInfo) {
- return getSelectedIds(data).size > 0 && payload.target === 'bounds'
- },
- isPointingShape(data, payload: PointerInfo) {
- return (
- payload.target &&
- payload.target !== 'canvas' &&
- payload.target !== 'bounds'
- )
- },
- isReadOnly(data) {
- return data.isReadOnly
- },
- canEditSelectedShape(data, payload, result: Shape) {
- return getShapeUtils(result).canEdit && !result.isLocked
- },
- distanceImpliesDrag(data, payload: PointerInfo) {
- return vec.dist2(payload.origin, payload.point) > 8
- },
- hasPointedTarget(data, payload: PointerInfo) {
- return payload.target !== undefined
- },
- isPointedShapeSelected(data) {
- return getSelectedIds(data).has(data.pointedId)
- },
- isPressingShiftKey(data, payload: PointerInfo) {
- return payload.shiftKey
- },
- isPressingMetaKey(data, payload: PointerInfo) {
- return payload.metaKey
- },
- shapeIsHovered(data, payload: { target: string }) {
- return data.hoveredId === payload.target
- },
- pointInSelectionBounds(data, payload: PointerInfo) {
- const bounds = getSelectionBounds(data)
-
- if (!bounds) return false
-
- return pointInBounds(screenToWorld(payload.point, data), bounds)
- },
- pointHitsShape(data, payload: PointerInfo) {
- const shape = getShape(data, payload.target)
-
- return getShapeUtils(shape).hitTest(
- shape,
- screenToWorld(payload.point, data)
- )
- },
- isPointingRotationHandle(
- data,
- payload: { target: Edge | Corner | 'rotate' }
- ) {
- return payload.target === 'rotate'
- },
- hasSelection(data) {
- return getSelectedIds(data).size > 0
- },
- hasSingleSelection(data) {
- return getSelectedIds(data).size === 1
- },
- hasMultipleSelection(data) {
- return getSelectedIds(data).size > 1
- },
- isToolLocked(data) {
- return data.settings.isToolLocked
- },
- isPenLocked(data) {
- return data.settings.isPenLocked
- },
- hasOnlyOnePage(data) {
- return Object.keys(data.document.pages).length === 1
- },
- selectionIncludesGroups(data) {
- return getSelectedShapes(data).some(
- (shape) => shape.type === ShapeType.Group
- )
- },
- },
- actions: {
- /* ---------------------- Pages --------------------- */
- changePage(data, payload: { id: string }) {
- commands.changePage(data, payload.id)
- },
- createPage(data) {
- commands.createPage(data, true)
- },
- deletePage(data, payload: { id: string }) {
- commands.deletePage(data, payload.id)
- },
- /* --------------------- Shapes --------------------- */
- resetShapes(data) {
- const page = getPage(data)
- Object.values(page.shapes).forEach((shape) => {
- page.shapes[shape.id] = { ...shape }
- })
- },
-
- createShape(data, payload, type: ShapeType) {
- const shape = createShape(type, {
- parentId: data.currentPageId,
- point: vec.round(screenToWorld(payload.point, data)),
- style: getCurrent(data.currentStyle),
- })
-
- const siblings = getChildren(data, shape.parentId)
- const childIndex = siblings.length
- ? siblings[siblings.length - 1].childIndex + 1
- : 1
-
- data.editingId = shape.id
-
- getShapeUtils(shape).setProperty(shape, 'childIndex', childIndex)
-
- getPage(data).shapes[shape.id] = shape
-
- setSelectedIds(data, [shape.id])
- },
- /* -------------------- Sessions -------------------- */
-
- // Shared
- breakSession(data) {
- session.cancel(data).clear()
- history.disable()
- commands.deleteSelected(data)
- history.enable()
- },
- cancelSession(data) {
- session.cancel(data).clear()
- },
- completeSession(data) {
- session.complete(data).clear()
- },
-
- // Editing
- startEditSession(data) {
- session.begin(new Sessions.EditSession(data))
- },
- updateEditSession(data, payload: { change: Partial<Shape> }) {
- session.update<Sessions.EditSession>(data, payload.change)
- },
-
- // Brushing
- startBrushSession(data, payload: PointerInfo) {
- session.begin(
- new Sessions.BrushSession(data, screenToWorld(payload.point, data))
- )
- },
- updateBrushSession(data, payload: PointerInfo) {
- session.update<Sessions.BrushSession>(
- data,
- screenToWorld(payload.point, data)
- )
- },
-
- // Rotating
- startRotateSession(data, payload: PointerInfo) {
- session.begin(
- new Sessions.RotateSession(data, screenToWorld(payload.point, data))
- )
- },
- keyUpdateRotateSession(data, payload: PointerInfo) {
- session.update<Sessions.RotateSession>(
- data,
- screenToWorld(inputs.pointer.point, data),
- payload.shiftKey
- )
- },
- updateRotateSession(data, payload: PointerInfo) {
- session.update<Sessions.RotateSession>(
- data,
- screenToWorld(payload.point, data),
- payload.shiftKey
- )
- },
-
- // Dragging / Translating
- startTranslateSession(data) {
- session.begin(
- new Sessions.TranslateSession(
- data,
- screenToWorld(inputs.pointer.origin, data)
- )
- )
- },
- keyUpdateTranslateSession(
- data,
- payload: { shiftKey: boolean; altKey: boolean }
- ) {
- session.update<Sessions.TranslateSession>(
- data,
- screenToWorld(inputs.pointer.point, data),
- payload.shiftKey,
- payload.altKey
- )
- },
- updateTranslateSession(data, payload: PointerInfo) {
- session.update<Sessions.TranslateSession>(
- data,
- screenToWorld(payload.point, data),
- payload.shiftKey,
- payload.altKey
- )
- },
-
- // Handles
- doublePointHandle(data, payload: PointerInfo) {
- const id = setToArray(getSelectedIds(data))[0]
- commands.doublePointHandle(data, id, payload)
- },
-
- // Dragging Handle
- startHandleSession(data, payload: PointerInfo) {
- const shapeId = Array.from(getSelectedIds(data).values())[0]
- const handleId = payload.target
-
- session.begin(
- new Sessions.HandleSession(
- data,
- shapeId,
- handleId,
- screenToWorld(inputs.pointer.origin, data)
- )
- )
- },
- keyUpdateHandleSession(
- data,
- payload: { shiftKey: boolean; altKey: boolean }
- ) {
- session.update<Sessions.HandleSession>(
- data,
- screenToWorld(inputs.pointer.point, data),
- payload.shiftKey
- )
- },
- updateHandleSession(data, payload: PointerInfo) {
- session.update<Sessions.HandleSession>(
- data,
- screenToWorld(payload.point, data),
- payload.shiftKey
- )
- },
-
- // Transforming
- startTransformSession(
- data,
- payload: PointerInfo & { target: Corner | Edge }
- ) {
- const point = screenToWorld(inputs.pointer.origin, data)
- session.begin(
- getSelectedIds(data).size === 1
- ? new Sessions.TransformSingleSession(data, payload.target, point)
- : new Sessions.TransformSession(data, payload.target, point)
- )
- },
- startDrawTransformSession(data, payload: PointerInfo) {
- session.begin(
- new Sessions.TransformSingleSession(
- data,
- Corner.BottomRight,
- screenToWorld(payload.point, data),
- true
- )
- )
- },
- keyUpdateTransformSession(data, payload: PointerInfo) {
- session.update<Sessions.TransformSession>(
- data,
- screenToWorld(inputs.pointer.point, data),
- payload.shiftKey
- )
- },
- updateTransformSession(data, payload: PointerInfo) {
- session.update<Sessions.TransformSession>(
- data,
- screenToWorld(payload.point, data),
- payload.shiftKey
- )
- },
-
- // Direction
- startDirectionSession(data) {
- session.begin(
- new Sessions.DirectionSession(
- data,
- screenToWorld(inputs.pointer.origin, data)
- )
- )
- },
- updateDirectionSession(data, payload: PointerInfo) {
- session.update<Sessions.DirectionSession>(
- data,
- screenToWorld(payload.point, data)
- )
- },
-
- // Drawing
- startDrawSession(data, payload: PointerInfo) {
- const id = Array.from(getSelectedIds(data).values())[0]
- session.begin(
- new Sessions.DrawSession(
- data,
- id,
- screenToWorld(inputs.pointer.origin, data),
- payload.shiftKey
- )
- )
- },
- keyUpdateDrawSession(data, payload: PointerInfo) {
- session.update<Sessions.DrawSession>(
- data,
- screenToWorld(inputs.pointer.point, data),
- payload.pressure,
- payload.shiftKey
- )
- },
- updateDrawSession(data, payload: PointerInfo) {
- session.update<Sessions.DrawSession>(
- data,
- screenToWorld(payload.point, data),
- payload.pressure,
- payload.shiftKey
- )
- },
-
- // Arrow
- startArrowSession(data, payload: PointerInfo) {
- const id = Array.from(getSelectedIds(data).values())[0]
-
- session.begin(
- new Sessions.ArrowSession(
- data,
- id,
- screenToWorld(inputs.pointer.origin, data),
- payload.shiftKey
- )
- )
- },
- keyUpdateArrowSession(data, payload: PointerInfo) {
- session.update<Sessions.ArrowSession>(
- data,
- screenToWorld(inputs.pointer.point, data),
- payload.shiftKey
- )
- },
- updateArrowSession(data, payload: PointerInfo) {
- session.update<Sessions.ArrowSession>(
- data,
- screenToWorld(payload.point, data),
- payload.shiftKey
- )
- },
-
- /* -------------------- Selection ------------------- */
-
- // Nudges
- nudgeSelection(data, payload: { delta: number[]; shiftKey: boolean }) {
- commands.nudge(
- data,
- vec.mul(
- payload.delta,
- payload.shiftKey
- ? data.settings.nudgeDistanceLarge
- : data.settings.nudgeDistanceSmall
- )
- )
- },
-
- clearInputs() {
- inputs.clear()
- },
-
- selectAll(data) {
- const selectedIds = getSelectedIds(data)
- const page = getPage(data)
- selectedIds.clear()
- for (const id in page.shapes) {
- if (page.shapes[id].parentId === data.currentPageId) {
- selectedIds.add(id)
- }
- }
- },
- setHoveredId(data, payload: PointerInfo) {
- data.hoveredId = payload.target
- },
- clearHoveredId(data) {
- data.hoveredId = undefined
- },
- setPointedId(data, payload: PointerInfo) {
- data.pointedId = getPointedId(data, payload.target)
- data.currentParentId = getParentId(data, data.pointedId)
- },
- setDrilledPointedId(data, payload: PointerInfo) {
- data.pointedId = getDrilledPointedId(data, payload.target)
- data.currentParentId = getParentId(data, data.pointedId)
- },
- clearCurrentParentId(data) {
- data.currentParentId = data.currentPageId
- data.pointedId = undefined
- },
- clearPointedId(data) {
- data.pointedId = undefined
- },
- clearSelectedIds(data) {
- setSelectedIds(data, [])
- },
- pullPointedIdFromSelectedIds(data) {
- const { pointedId } = data
- const selectedIds = getSelectedIds(data)
- selectedIds.delete(pointedId)
- },
- pushPointedIdToSelectedIds(data) {
- getSelectedIds(data).add(data.pointedId)
- },
- moveSelection(data, payload: { type: MoveType }) {
- commands.move(data, payload.type)
- },
- moveSelectionToPage(data, payload: { id: string }) {
- commands.moveToPage(data, payload.id)
- },
- alignSelection(data, payload: { type: AlignType }) {
- commands.align(data, payload.type)
- },
- stretchSelection(data, payload: { type: StretchType }) {
- commands.stretch(data, payload.type)
- },
- distributeSelection(data, payload: { type: DistributeType }) {
- commands.distribute(data, payload.type)
- },
- duplicateSelection(data) {
- commands.duplicate(data)
- },
- lockSelection(data) {
- commands.toggle(data, 'isLocked')
- },
- hideSelection(data) {
- commands.toggle(data, 'isHidden')
- },
- aspectLockSelection(data) {
- commands.toggle(data, 'isAspectRatioLocked')
- },
- deleteSelection(data) {
- commands.deleteSelected(data)
- },
- rotateSelectionCcw(data) {
- commands.rotateCcw(data)
- },
- groupSelection(data) {
- commands.group(data)
- },
- ungroupSelection(data) {
- commands.ungroup(data)
- },
- resetShapeBounds(data) {
- commands.resetBounds(data)
- },
-
- /* --------------------- Editing -------------------- */
-
- setEditingId(data) {
- const selectedShape = getSelectedShapes(data)[0]
- if (getShapeUtils(selectedShape).canEdit) {
- data.editingId = selectedShape.id
- }
-
- getPageState(data).selectedIds = new Set([selectedShape.id])
- },
- clearEditingId(data) {
- data.editingId = null
- },
-
- /* ---------------------- Tool ---------------------- */
-
- setActiveTool(data, payload: { tool: ShapeType | 'select' }) {
- data.activeTool = payload.tool
- },
- setActiveToolSelect(data) {
- data.activeTool = 'select'
- },
- setActiveToolDraw(data) {
- data.activeTool = ShapeType.Draw
- },
- setActiveToolRectangle(data) {
- data.activeTool = ShapeType.Rectangle
- },
- setActiveToolEllipse(data) {
- data.activeTool = ShapeType.Ellipse
- },
- setActiveToolArrow(data) {
- data.activeTool = ShapeType.Arrow
- },
- setActiveToolDot(data) {
- data.activeTool = ShapeType.Dot
- },
- setActiveToolPolyline(data) {
- data.activeTool = ShapeType.Polyline
- },
- setActiveToolRay(data) {
- data.activeTool = ShapeType.Ray
- },
- setActiveToolLine(data) {
- data.activeTool = ShapeType.Line
- },
- setActiveToolText(data) {
- data.activeTool = ShapeType.Text
- },
-
- /* --------------------- Camera --------------------- */
-
- zoomIn(data) {
- const camera = getCurrentCamera(data)
- const i = Math.round((camera.zoom * 100) / 25)
- const center = [window.innerWidth / 2, window.innerHeight / 2]
-
- const p0 = screenToWorld(center, data)
- camera.zoom = getCameraZoom((i + 1) * 0.25)
- const p1 = screenToWorld(center, data)
- camera.point = vec.add(camera.point, vec.sub(p1, p0))
-
- setZoomCSS(camera.zoom)
- },
- zoomOut(data) {
- const camera = getCurrentCamera(data)
- const i = Math.round((camera.zoom * 100) / 25)
- const center = [window.innerWidth / 2, window.innerHeight / 2]
-
- const p0 = screenToWorld(center, data)
- camera.zoom = getCameraZoom((i - 1) * 0.25)
- const p1 = screenToWorld(center, data)
- camera.point = vec.add(camera.point, vec.sub(p1, p0))
-
- setZoomCSS(camera.zoom)
- },
- zoomCameraToActual(data) {
- const camera = getCurrentCamera(data)
- const center = [window.innerWidth / 2, window.innerHeight / 2]
-
- const p0 = screenToWorld(center, data)
- camera.zoom = 1
- const p1 = screenToWorld(center, data)
- camera.point = vec.add(camera.point, vec.sub(p1, p0))
-
- setZoomCSS(camera.zoom)
- },
- zoomCameraToSelectionActual(data) {
- const camera = getCurrentCamera(data)
-
- const bounds = getSelectedBounds(data)
-
- const mx = (window.innerWidth - bounds.width) / 2
- const my = (window.innerHeight - bounds.height) / 2
-
- camera.zoom = 1
- camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
-
- setZoomCSS(camera.zoom)
- },
- zoomCameraToSelection(data) {
- const camera = getCurrentCamera(data)
- const bounds = getSelectedBounds(data)
-
- const zoom = getCameraZoom(
- bounds.width > bounds.height
- ? (window.innerWidth - 128) / bounds.width
- : (window.innerHeight - 128) / bounds.height
- )
-
- const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
- const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
-
- camera.zoom = zoom
- camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
-
- setZoomCSS(camera.zoom)
- },
- zoomCameraToFit(data) {
- const camera = getCurrentCamera(data)
- const page = getPage(data)
-
- const shapes = Object.values(page.shapes)
-
- if (shapes.length === 0) {
- return
- }
-
- const bounds = getCommonBounds(
- ...Object.values(shapes).map((shape) =>
- getShapeUtils(shape).getBounds(shape)
- )
- )
-
- const zoom = getCameraZoom(
- bounds.width > bounds.height
- ? (window.innerWidth - 128) / bounds.width
- : (window.innerHeight - 128) / bounds.height
- )
-
- const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
- const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
-
- camera.zoom = zoom
- camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
-
- setZoomCSS(camera.zoom)
- },
- zoomCamera(data, payload: { delta: number; point: number[] }) {
- const camera = getCurrentCamera(data)
- const next = camera.zoom - (payload.delta / 100) * camera.zoom
-
- const p0 = screenToWorld(payload.point, data)
- camera.zoom = getCameraZoom(next)
- const p1 = screenToWorld(payload.point, data)
- camera.point = vec.add(camera.point, vec.sub(p1, p0))
-
- setZoomCSS(camera.zoom)
- },
- panCamera(data, payload: { delta: number[] }) {
- const camera = getCurrentCamera(data)
- camera.point = vec.sub(camera.point, vec.div(payload.delta, camera.zoom))
- },
- updateZoomCSS(data) {
- const camera = getCurrentCamera(data)
- setZoomCSS(camera.zoom)
- },
- pinchCamera(
- data,
- payload: {
- delta: number[]
- distanceDelta: number
- angleDelta: number
- point: number[]
- }
- ) {
- // This is usually replaced with hacks.fastPinchCamera!
-
- const camera = getCurrentCamera(data)
- camera.point = vec.sub(camera.point, vec.div(payload.delta, camera.zoom))
-
- const next = camera.zoom - (payload.distanceDelta / 300) * camera.zoom
-
- const p0 = screenToWorld(payload.point, data)
- camera.zoom = getCameraZoom(next)
- const p1 = screenToWorld(payload.point, data)
- camera.point = vec.add(camera.point, vec.sub(p1, p0))
-
- setZoomCSS(camera.zoom)
- },
- resetCamera(data) {
- const camera = getCurrentCamera(data)
- camera.zoom = 1
- camera.point = [window.innerWidth / 2, window.innerHeight / 2]
- document.documentElement.style.setProperty('--camera-zoom', '1')
- },
-
- /* ---------------------- History ---------------------- */
-
- // History
- popHistory() {
- history.pop()
- },
- enableHistory() {
- history.enable()
- },
- disableHistory() {
- history.disable()
- },
- undo(data) {
- history.undo(data)
- },
- redo(data) {
- history.redo(data)
- },
- resetHistory() {
- history.reset()
- },
-
- /* --------------------- Styles --------------------- */
-
- toggleStylePanel(data) {
- data.settings.isStyleOpen = !data.settings.isStyleOpen
- },
- closeStylePanel(data) {
- data.settings.isStyleOpen = false
- },
- updateStyles(data, payload: Partial<ShapeStyles>) {
- Object.assign(data.currentStyle, payload)
- },
- applyStylesToSelection(data, payload: Partial<ShapeStyles>) {
- commands.style(data, payload)
- },
-
- /* ---------------------- Code ---------------------- */
-
- closeCodePanel(data) {
- data.settings.isCodeOpen = false
- },
- openCodePanel(data) {
- data.settings.isCodeOpen = true
- },
- toggleCodePanel(data) {
- data.settings.isCodeOpen = !data.settings.isCodeOpen
- },
- setGeneratedShapes(
- data,
- payload: { shapes: Shape[]; controls: CodeControl[] }
- ) {
- commands.generate(data, data.currentPageId, payload.shapes)
- },
- setCodeControls(data, payload: { controls: CodeControl[] }) {
- data.codeControls = Object.fromEntries(
- payload.controls.map((control) => [control.id, control])
- )
- },
- increaseCodeFontSize(data) {
- data.settings.fontSize++
- },
- decreaseCodeFontSize(data) {
- data.settings.fontSize--
- },
- updateControls(data, payload: { [key: string]: any }) {
- for (const key in payload) {
- data.codeControls[key].value = payload[key]
- }
-
- history.disable()
-
- setSelectedIds(data, [])
-
- try {
- const { shapes } = updateFromCode(
- data,
- data.document.code[data.currentCodeFileId].code
- )
-
- commands.generate(data, data.currentPageId, shapes)
- } catch (e) {
- console.error(e)
- }
-
- history.enable()
- },
-
- /* -------------------- Settings -------------------- */
-
- enablePenLock(data) {
- data.settings.isPenLocked = true
- },
- disablePenLock(data) {
- data.settings.isPenLocked = false
- },
- toggleToolLock(data) {
- data.settings.isToolLocked = !data.settings.isToolLocked
- },
-
- /* ---------------------- Data ---------------------- */
-
- copyToSvg(data) {
- clipboard.copySelectionToSvg(data)
- },
-
- copyToClipboard(data) {
- clipboard.copy(getSelectedShapes(data))
- },
-
- pasteFromClipboard() {
- clipboard.paste()
- },
-
- pasteShapesFromClipboard(data, payload: { shapes: Shape[] }) {
- commands.paste(data, payload.shapes)
- },
-
- restoreSavedData(data) {
- storage.firstLoad(data)
- },
-
- saveToFileSystem(data) {
- storage.saveToFileSystem(data)
- },
-
- saveAsToFileSystem(data) {
- storage.saveAsToFileSystem(data)
- },
-
- loadFromFileSystem() {
- storage.loadDocumentFromFilesystem()
- },
-
- loadDocumentFromJson(data, payload: { json: any }) {
- storage.loadDocumentFromJson(data, payload.json)
- },
-
- forceSave(data) {
- storage.saveToFileSystem(data)
- },
-
- savePage(data) {
- storage.savePage(data)
- },
-
- loadPage(data) {
- storage.loadPage(data)
- },
-
- saveCode(data, payload: { code: string }) {
- data.document.code[data.currentCodeFileId].code = payload.code
- storage.saveDocumentToLocalStorage(data)
- },
-
- clearBoundsRotation(data) {
- data.boundsRotation = 0
- },
- },
- values: {
- selectedIds(data) {
- return new Set(getSelectedIds(data))
- },
- selectedBounds(data) {
- return getSelectionBounds(data)
- },
- currentShapes(data) {
- const page = getPage(data)
-
- return Object.values(page.shapes)
- .filter((shape) => shape.parentId === page.id)
- .sort((a, b) => a.childIndex - b.childIndex)
- },
- selectedStyle(data) {
- const selectedIds = Array.from(getSelectedIds(data).values())
- const { currentStyle } = data
-
- if (selectedIds.length === 0) {
- return currentStyle
- }
- const page = getPage(data)
-
- const shapeStyles = selectedIds.map((id) => page.shapes[id].style)
-
- const commonStyle: ShapeStyles = {} as ShapeStyles
-
- const overrides = new Set<string>([])
-
- for (const shapeStyle of shapeStyles) {
- for (const key in currentStyle) {
- if (overrides.has(key)) continue
- if (commonStyle[key] === undefined) {
- commonStyle[key] = shapeStyle[key]
- } else {
- if (commonStyle[key] === shapeStyle[key]) continue
- commonStyle[key] = currentStyle[key]
- overrides.add(key)
- }
- }
- }
-
- return commonStyle
- },
- },
- })
-
- export default state
-
- export const useSelector = createSelectorHook(state)
-
- function getParentId(data: Data, id: string) {
- const shape = getPage(data).shapes[id]
- return shape.parentId
- }
-
- function getPointedId(data: Data, id: string) {
- const shape = getPage(data).shapes[id]
-
- return shape.parentId === data.currentParentId ||
- shape.parentId === data.currentPageId
- ? id
- : getPointedId(data, shape.parentId)
- }
-
- function getDrilledPointedId(data: Data, id: string) {
- const shape = getPage(data).shapes[id]
- return shape.parentId === data.currentPageId ||
- shape.parentId === data.pointedId ||
- shape.parentId === data.currentParentId
- ? id
- : getDrilledPointedId(data, shape.parentId)
- }
-
- // function hasPointedIdInChildren(data: Data, id: string, pointedId: string) {
- // const shape = getPage(data).shapes[id]
-
- // if (shape.type !== ShapeType.Group) {
- // return false
- // }
-
- // if (shape.children.includes(pointedId)) {
- // return true
- // }
-
- // return shape.children.some((childId) =>
- // hasPointedIdInChildren(data, childId, pointedId)
- // )
- // }
-
- function getSelectionBounds(data: Data) {
- const selectedIds = getSelectedIds(data)
-
- const page = getPage(data)
-
- const shapes = Array.from(selectedIds.values())
- .map((id) => page.shapes[id])
- .filter(Boolean)
-
- if (selectedIds.size === 0) return null
-
- if (selectedIds.size === 1) {
- if (!shapes[0]) {
- console.error('Could not find that shape! Clearing selected IDs.')
- setSelectedIds(data, [])
- return null
- }
-
- const shape = shapes[0]
- const shapeUtils = getShapeUtils(shape)
-
- if (!shapeUtils.canTransform) return null
-
- let bounds = shapeUtils.getBounds(shape)
-
- let parentId = shape.parentId
-
- while (parentId !== data.currentPageId) {
- const parent = page.shapes[parentId]
-
- bounds = rotateBounds(
- bounds,
- getBoundsCenter(getShapeUtils(parent).getBounds(parent)),
- parent.rotation
- )
-
- bounds.rotation = parent.rotation
-
- parentId = parent.parentId
- }
-
- return bounds
- }
-
- const uniqueSelectedShapeIds: string[] = Array.from(
- new Set(
- Array.from(selectedIds.values()).flatMap((id) =>
- getDocumentBranch(data, id)
- )
- ).values()
- )
-
- const commonBounds = getCommonBounds(
- ...uniqueSelectedShapeIds
- .map((id) => page.shapes[id])
- .filter((shape) => shape.type !== ShapeType.Group)
- .map((shape) => getShapeUtils(shape).getRotatedBounds(shape))
- )
-
- return commonBounds
- }
-
- // state.enableLog(true)
- // state.onUpdate((s) => console.log(s.log.filter((l) => l !== 'MOVED_POINTER')))
|