{"version":3,"file":"LayoutStrategy.min.mjs","names":[],"sources":["../../../../src/LayoutManager/LayoutStrategies/LayoutStrategy.ts"],"sourcesContent":["import { Point } from '../../Point';\nimport type { FabricObject } from '../../shapes/Object/FabricObject';\nimport { makeBoundingBoxFromPoints } from '../../util/misc/boundingBoxFromPoints';\nimport {\n  LAYOUT_TYPE_INITIALIZATION,\n  LAYOUT_TYPE_IMPERATIVE,\n} from '../constants';\nimport type {\n  InitializationLayoutContext,\n  LayoutStrategyResult,\n  StrictLayoutContext,\n} from '../types';\nimport { getObjectBounds } from './utils';\n\n/**\n * Exposes a main public method {@link calcLayoutResult} that is used by the `LayoutManager` to perform layout.\n * Returning `undefined` signals the `LayoutManager` to skip the layout.\n *\n * In charge of calculating the bounding box of the passed objects.\n */\nexport abstract class LayoutStrategy {\n  /**\n   * override by subclass for persistence (TS does not support `static abstract`)\n   */\n  static type = 'strategy';\n\n  /**\n   * Used by the `LayoutManager` to perform layout\n   * @TODO/fix: if this method is calcResult, should calc unconditionally.\n   * the condition to not calc should be evaluated by the layoutManager.\n   * @returns layout result **OR** `undefined` to skip layout\n   */\n  public calcLayoutResult(\n    context: StrictLayoutContext,\n    objects: FabricObject[],\n  ): LayoutStrategyResult | undefined {\n    if (this.shouldPerformLayout(context)) {\n      return this.calcBoundingBox(objects, context);\n    }\n  }\n\n  shouldPerformLayout({ type, prevStrategy, strategy }: StrictLayoutContext) {\n    return (\n      type === LAYOUT_TYPE_INITIALIZATION ||\n      type === LAYOUT_TYPE_IMPERATIVE ||\n      (!!prevStrategy && strategy !== prevStrategy)\n    );\n  }\n\n  shouldLayoutClipPath({ type, target: { clipPath } }: StrictLayoutContext) {\n    return (\n      type !== LAYOUT_TYPE_INITIALIZATION &&\n      clipPath &&\n      !clipPath.absolutePositioned\n    );\n  }\n\n  getInitialSize(\n    context: StrictLayoutContext & InitializationLayoutContext,\n    result: Pick<LayoutStrategyResult, 'center' | 'size'>,\n  ) {\n    return result.size;\n  }\n\n  /**\n   * Override this method to customize layout.\n   */\n  calcBoundingBox(\n    objects: FabricObject[],\n    context: StrictLayoutContext,\n  ): LayoutStrategyResult | undefined {\n    const { type, target } = context;\n    if (type === LAYOUT_TYPE_IMPERATIVE && context.overrides) {\n      return context.overrides;\n    }\n    if (objects.length === 0) {\n      return;\n    }\n    const { left, top, width, height } = makeBoundingBoxFromPoints(\n      objects\n        .map((object) => getObjectBounds(target, object))\n        .reduce<Point[]>((coords, curr) => coords.concat(curr), []),\n    );\n    const bboxSize = new Point(width, height);\n    const bboxLeftTop = new Point(left, top);\n    const bboxCenter = bboxLeftTop.add(bboxSize.scalarDivide(2));\n\n    if (type === LAYOUT_TYPE_INITIALIZATION) {\n      const actualSize = this.getInitialSize(context, {\n        size: bboxSize,\n        center: bboxCenter,\n      });\n      return {\n        // in `initialization` we do not account for target's transformation matrix\n        center: bboxCenter,\n        // TODO: investigate if this is still necessary\n        relativeCorrection: new Point(0, 0),\n        size: actualSize,\n      };\n    } else {\n      //  we send `relativeCenter` up to group's containing plane\n      const center = bboxCenter.transform(target.calcOwnMatrix());\n      return {\n        center,\n        size: bboxSize,\n      };\n    }\n  }\n}\n"],"mappings":"sUAoBA,IAAsB,EAAtB,KAAA,CAYE,iBACE,EACA,EAAA,CAEA,GAAI,KAAK,oBAAoB,EAAA,CAC3B,OAAO,KAAK,gBAAgB,EAAS,EAAA,CAIzC,oBAAA,CAAoB,KAAE,EAAA,aAAM,EAAA,SAAc,GAAA,CACxC,OACE,IAAA,kBACA,IAAA,cAAA,CAAA,CACG,GAAgB,IAAa,EAIpC,qBAAA,CAAqB,KAAE,EAAM,OAAA,CAAQ,SAAE,IAAA,CACrC,OACE,IAAA,kBACA,GAAA,CACC,EAAS,mBAId,eACE,EACA,EAAA,CAEA,OAAO,EAAO,KAMhB,gBACE,EACA,EAAA,CAEA,GAAA,CAAM,KAAE,EAAA,OAAM,GAAW,EACzB,GAAI,IAAA,cAAmC,EAAQ,UAC7C,OAAO,EAAQ,UAEjB,GAAI,EAAQ,SAAW,EACrB,OAEF,GAAA,CAAM,KAAE,EAAA,IAAM,EAAA,MAAK,EAAA,OAAO,GAAW,EACnC,EACG,IAAK,GAAW,EAAgB,EAAQ,EAAA,CAAA,CACxC,QAAiB,EAAQ,IAAS,EAAO,OAAO,EAAA,CAAO,EAAA,CAAA,CAAA,CAEtD,EAAW,IAAI,EAAM,EAAO,EAAA,CAE5B,EADc,IAAI,EAAM,EAAM,EAAA,CACL,IAAI,EAAS,aAAa,EAAA,CAAA,CAEzD,GAAI,IAAA,iBAAqC,CACvC,IAAM,EAAa,KAAK,eAAe,EAAS,CAC9C,KAAM,EACN,OAAQ,EAAA,CAAA,CAEV,MAAO,CAEL,OAAQ,EAER,mBAAoB,IAAI,EAAM,EAAG,EAAA,CACjC,KAAM,EAAA,CAKR,MAAO,CACL,OAFa,EAAW,UAAU,EAAO,eAAA,CAAA,CAGzC,KAAM,EAAA,GAAA,EAAA,EAhFL,OAAO,WAAA,CAAA,OAAA,KAAA"}