{"version":3,"file":"LayoutStrategy.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":";;;;;;;;;;;;AAoBA,IAAsB,iBAAtB,MAAqC;;;;;;;CAYnC,iBACE,SACA,SACkC;AAClC,MAAI,KAAK,oBAAoB,QAAQ,CACnC,QAAO,KAAK,gBAAgB,SAAS,QAAQ;;CAIjD,oBAAoB,EAAE,MAAM,cAAc,YAAiC;AACzE,SACE,SAAA,oBACA,SAAA,gBACC,CAAC,CAAC,gBAAgB,aAAa;;CAIpC,qBAAqB,EAAE,MAAM,QAAQ,EAAE,cAAmC;AACxE,SACE,SAAA,oBACA,YACA,CAAC,SAAS;;CAId,eACE,SACA,QACA;AACA,SAAO,OAAO;;;;;CAMhB,gBACE,SACA,SACkC;EAClC,MAAM,EAAE,MAAM,WAAW;AACzB,MAAI,SAAA,gBAAmC,QAAQ,UAC7C,QAAO,QAAQ;AAEjB,MAAI,QAAQ,WAAW,EACrB;EAEF,MAAM,EAAE,MAAM,KAAK,OAAO,WAAW,0BACnC,QACG,KAAK,WAAW,gBAAgB,QAAQ,OAAO,CAAC,CAChD,QAAiB,QAAQ,SAAS,OAAO,OAAO,KAAK,EAAE,EAAE,CAAC,CAC9D;EACD,MAAM,WAAW,IAAI,MAAM,OAAO,OAAO;EAEzC,MAAM,aADc,IAAI,MAAM,MAAM,IAAI,CACT,IAAI,SAAS,aAAa,EAAE,CAAC;AAE5D,MAAI,SAAA,kBAAqC;GACvC,MAAM,aAAa,KAAK,eAAe,SAAS;IAC9C,MAAM;IACN,QAAQ;IACT,CAAC;AACF,UAAO;IAEL,QAAQ;IAER,oBAAoB,IAAI,MAAM,GAAG,EAAE;IACnC,MAAM;IACP;QAID,QAAO;GACL,QAFa,WAAW,UAAU,OAAO,eAAe,CAAC;GAGzD,MAAM;GACP;;;gCAjFE,QAAO,WAAW"}