{"version":3,"file":"ActiveSelection.min.mjs","names":[],"sources":["../../../src/shapes/ActiveSelection.ts"],"sourcesContent":["import type { ControlRenderingStyleOverride } from '../controls/controlRendering';\nimport { classRegistry } from '../ClassRegistry';\nimport type { GroupProps } from './Group';\nimport { Group } from './Group';\nimport type { FabricObject } from './Object/FabricObject';\nimport {\n  LAYOUT_TYPE_ADDED,\n  LAYOUT_TYPE_REMOVED,\n} from '../LayoutManager/constants';\nimport type { TClassProperties } from '../typedefs';\nimport { log } from '../util/internals/console';\nimport { ActiveSelectionLayoutManager } from '../LayoutManager/ActiveSelectionLayoutManager';\n\nexport type MultiSelectionStacking = 'canvas-stacking' | 'selection-order';\n\nexport interface ActiveSelectionOptions extends GroupProps {\n  multiSelectionStacking: MultiSelectionStacking;\n}\n\nconst activeSelectionDefaultValues: Partial<TClassProperties<ActiveSelection>> =\n  {\n    multiSelectionStacking: 'canvas-stacking',\n  };\n\n/**\n * Used by Canvas to manage selection.\n *\n * @example\n * class MyActiveSelection extends ActiveSelection {\n *   ...\n * }\n *\n * // override the default `ActiveSelection` class\n * classRegistry.setClass(MyActiveSelection)\n */\nexport class ActiveSelection extends Group {\n  static type = 'ActiveSelection';\n\n  static ownDefaults: Record<string, any> = activeSelectionDefaultValues;\n\n  static getDefaults(): Record<string, any> {\n    return { ...super.getDefaults(), ...ActiveSelection.ownDefaults };\n  }\n\n  /**\n   * The ActiveSelection needs to use the ActiveSelectionLayoutManager\n   * or selections on interactive groups may be broken\n   */\n  declare layoutManager: ActiveSelectionLayoutManager;\n\n  /**\n   * controls how selected objects are added during a multiselection event\n   * - `canvas-stacking` adds the selected object to the active selection while respecting canvas object stacking order\n   * - `selection-order` adds the selected object to the top of the stack,\n   * meaning that the stack is ordered by the order in which objects were selected\n   * @default `canvas-stacking`\n   */\n  declare multiSelectionStacking: MultiSelectionStacking;\n\n  constructor(\n    objects: FabricObject[] = [],\n    options: Partial<ActiveSelectionOptions> = {},\n  ) {\n    super();\n    Object.assign(this, ActiveSelection.ownDefaults);\n    this.setOptions(options);\n    const { left, top, layoutManager } = options;\n    this.groupInit(objects, {\n      left,\n      top,\n      layoutManager: layoutManager ?? new ActiveSelectionLayoutManager(),\n    });\n  }\n\n  /**\n   * @private\n   */\n  _shouldSetNestedCoords() {\n    return true;\n  }\n\n  /**\n   * @private\n   * @override we don't want the selection monitor to be active\n   */\n  __objectSelectionMonitor() {\n    //  noop\n  }\n\n  /**\n   * Adds objects with respect to {@link multiSelectionStacking}\n   * @param targets object to add to selection\n   */\n  multiSelectAdd(...targets: FabricObject[]) {\n    if (this.multiSelectionStacking === 'selection-order') {\n      this.add(...targets);\n    } else {\n      //  respect object stacking as it is on canvas\n      //  perf enhancement for large ActiveSelection: consider a binary search of `isInFrontOf`\n      targets.forEach((target) => {\n        const index = this._objects.findIndex((obj) => obj.isInFrontOf(target));\n        const insertAt =\n          index === -1\n            ? //  `target` is in front of all other objects\n              this.size()\n            : index;\n        this.insertAt(insertAt, target);\n      });\n    }\n  }\n\n  /**\n   * @override block ancestors/descendants of selected objects from being selected to prevent a circular object tree\n   */\n  canEnterGroup(object: FabricObject) {\n    if (\n      this.getObjects().some(\n        (o) => o.isDescendantOf(object) || object.isDescendantOf(o),\n      )\n    ) {\n      //  prevent circular object tree\n      log(\n        'error',\n        'ActiveSelection: circular object trees are not supported, this call has no effect',\n      );\n      return false;\n    }\n\n    return super.canEnterGroup(object);\n  }\n\n  /**\n   * Change an object so that it can be part of an active selection.\n   * this method is called by multiselectAdd from canvas code.\n   * @private\n   * @param {FabricObject} object\n   * @param {boolean} [removeParentTransform] true if object is in canvas coordinate plane\n   */\n  enterGroup(object: FabricObject, removeParentTransform?: boolean) {\n    // This condition check that the object has currently a group, and the group\n    // is also its parent, meaning that is not in an active selection, but is\n    // in a normal group.\n    if (object.parent && object.parent === object.group) {\n      // Disconnect the object from the group functionalities, but keep the ref parent intact\n      // for later re-enter\n      object.parent._exitGroup(object);\n      // in this case the object is probably inside an active selection.\n    } else if (object.group && object.parent !== object.group) {\n      // in this case group.remove will also clear the old parent reference.\n      object.group.remove(object);\n    }\n    // enter the active selection from a render perspective\n    // the object will be in the objects array of both the ActiveSelection and the Group\n    // but referenced in the group's _activeObjects so that it won't be rendered twice.\n    this._enterGroup(object, removeParentTransform);\n  }\n\n  /**\n   * we want objects to retain their canvas ref when exiting instance\n   * @private\n   * @param {FabricObject} object\n   * @param {boolean} [removeParentTransform] true if object should exit group without applying group's transform to it\n   */\n  exitGroup(object: FabricObject, removeParentTransform?: boolean) {\n    this._exitGroup(object, removeParentTransform);\n    // return to parent\n    object.parent && object.parent._enterGroup(object, true);\n  }\n\n  /**\n   * @private\n   * @param {'added'|'removed'} type\n   * @param {FabricObject[]} targets\n   */\n  _onAfterObjectsChange(type: 'added' | 'removed', targets: FabricObject[]) {\n    super._onAfterObjectsChange(type, targets);\n    const groups = new Set<Group>();\n    targets.forEach((object) => {\n      const { parent } = object;\n      parent && groups.add(parent);\n    });\n    if (type === LAYOUT_TYPE_REMOVED) {\n      //  invalidate groups' layout and mark as dirty\n      groups.forEach((group) => {\n        group._onAfterObjectsChange(LAYOUT_TYPE_ADDED, targets);\n      });\n    } else {\n      //  mark groups as dirty\n      groups.forEach((group) => {\n        group._set('dirty', true);\n      });\n    }\n  }\n\n  /**\n   * @override remove all objects\n   */\n  onDeselect() {\n    this.removeAll();\n    return false;\n  }\n\n  /**\n   * Returns string representation of a group\n   * @return {String}\n   */\n  toString() {\n    return `#<ActiveSelection: (${this.complexity()})>`;\n  }\n\n  /**\n   * Decide if the object should cache or not. The Active selection never caches\n   * @return {Boolean}\n   */\n  shouldCache() {\n    return false;\n  }\n\n  /**\n   * Check if this group or its parent group are caching, recursively up\n   * @return {Boolean}\n   */\n  isOnACache() {\n    return false;\n  }\n\n  /**\n   * Renders controls and borders for the object\n   * @param {CanvasRenderingContext2D} ctx Context to render on\n   * @param {Object} [styleOverride] properties to override the object style\n   * @param {Object} [childrenOverride] properties to override the children overrides\n   */\n  _renderControls(\n    ctx: CanvasRenderingContext2D,\n    styleOverride?: ControlRenderingStyleOverride,\n    childrenOverride?: ControlRenderingStyleOverride,\n  ) {\n    ctx.save();\n    ctx.globalAlpha = this.isMoving ? this.borderOpacityWhenMoving : 1;\n    const options = {\n      hasControls: false,\n      ...childrenOverride,\n      forActiveSelection: true,\n    };\n    for (let i = 0; i < this._objects.length; i++) {\n      this._objects[i]._renderControls(ctx, options);\n    }\n    super._renderControls(ctx, styleOverride);\n    ctx.restore();\n  }\n}\n\nclassRegistry.setClass(ActiveSelection);\nclassRegistry.setClass(ActiveSelection, 'activeSelection');\n"],"mappings":"mbAmCA,IAAa,EAAb,MAAa,UAAwB,CAAA,CAKnC,OAAA,aAAO,CACL,MAAO,CAAA,GAAK,MAAM,aAAA,CAAA,GAAkB,EAAgB,YAAA,CAkBtD,YACE,EAA0B,EAAA,CAC1B,EAA2C,EAAA,CAAA,CAE3C,OAAA,CACA,OAAO,OAAO,KAAM,EAAgB,YAAA,CACpC,KAAK,WAAW,EAAA,CAChB,GAAA,CAAM,KAAE,EAAA,IAAM,EAAA,cAAK,GAAkB,EACrC,KAAK,UAAU,EAAS,CACtB,KAAA,EACA,IAAA,EACA,cAAe,GAAA,KAAiB,IAAI,EAArB,EAAqB,CAAA,CAOxC,wBAAA,CACE,MAAA,CAAO,EAOT,0BAAA,EAQA,eAAA,GAAkB,EAAA,CACZ,KAAK,yBAA2B,kBAClC,KAAK,IAAA,GAAO,EAAA,CAIZ,EAAQ,QAAS,GAAA,CACf,IAAM,EAAQ,KAAK,SAAS,UAAW,GAAQ,EAAI,YAAY,EAAA,CAAA,CACzD,EACJ,IADI,GAGA,KAAK,MAAA,CACL,EACN,KAAK,SAAS,EAAU,EAAA,EAAA,CAQ9B,cAAc,EAAA,CACZ,OACE,KAAK,YAAA,CAAa,KACf,GAAM,EAAE,eAAe,EAAA,EAAW,EAAO,eAAe,EAAA,CAAA,EAI3D,EACE,QACA,oFAAA,CAAA,CAEK,GAGF,MAAM,cAAc,EAAA,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