1 line
13 KiB
Plaintext
1 line
13 KiB
Plaintext
|
|
{"version":3,"file":"LayoutManager.mjs","names":[],"sources":["../../../src/LayoutManager/LayoutManager.ts"],"sourcesContent":["import { Point } from '../Point';\nimport {\n CENTER,\n CHANGED,\n MODIFIED,\n MODIFY_PATH,\n MODIFY_POLY,\n MOVING,\n RESIZING,\n ROTATING,\n SCALING,\n SKEWING,\n iMatrix,\n} from '../constants';\nimport type { Group } from '../shapes/Group';\nimport type { FabricObject } from '../shapes/Object/FabricObject';\nimport { invertTransform } from '../util/misc/matrix';\nimport { resolveOrigin } from '../util/misc/resolveOrigin';\nimport { FitContentLayout } from './LayoutStrategies/FitContentLayout';\nimport type { LayoutStrategy } from './LayoutStrategies/LayoutStrategy';\nimport {\n LAYOUT_TYPE_INITIALIZATION,\n LAYOUT_TYPE_ADDED,\n LAYOUT_TYPE_REMOVED,\n LAYOUT_TYPE_IMPERATIVE,\n LAYOUT_TYPE_OBJECT_MODIFIED,\n LAYOUT_TYPE_OBJECT_MODIFYING,\n} from './constants';\nimport type {\n LayoutContext,\n LayoutResult,\n RegistrationContext,\n StrictLayoutContext,\n} from './types';\nimport { classRegistry } from '../ClassRegistry';\nimport type { TModificationEvents } from '../EventTypeDefs';\n\nconst LAYOUT_MANAGER = 'layoutManager';\n\nexport type SerializedLayoutManager = {\n type: string;\n strategy: string;\n};\n\nexport class LayoutManager {\n declare private _prevLayoutStrategy?: LayoutStrategy;\n declare protected _subscriptions: Map<FabricObject, VoidFunction[]>;\n\n strategy: LayoutStrategy;\n\n constructor(strategy: LayoutStrategy = new FitContentLayout()) {\n this.strategy = strategy;\n this._subscriptions = new Map();\n }\n\n public performLayout(context: LayoutContext) {\n const strictContext: StrictLayoutContext = {\n bubbles: true,\n strategy: this.strategy,\n ...context,\n prevStrategy: this._prevLayoutStrategy,\n stopPropagation() {\n this.bubbles = false;\n },\n };\n\n this.onBeforeLayout(strictContext);\n\n const layoutResult = this.getLayoutResult(strictContext);\n if (layoutResult) {\n this.commitLayout(strictContext, layoutResult);\n }\n\n this.onAfterLayout(strictContext, layoutResult);\n this._prevLayoutStrategy = strictContext.strategy;\n }\n\n /**\n * Attach handlers for events that we know will invalidate the layout when\n * performed on child objects ( general transforms ).\n * Returns the disposers for later unsubscribing and cleanup\n * @param {FabricObject} object\n * @param {RegistrationContext & Partial<StrictLayoutContext>} context\n * @returns {VoidFunction[]} disposers remove the handlers\n */\n protected attachHandlers(\n object: FabricObject,\n context: RegistrationContext & Partial<StrictLayoutContext>,\n ): VoidFunction[] {\n const { target } = context;\n return (\n [\n MODIFIED,\n MOVING,\n RESIZING,\n ROTATING,\n SCALING,\n SKEWING,\n CHANGED,\n MODIFY_POLY,\n MODIFY_PATH,\n ] as (TModificationEvents & 'modified')[]\n ).map((key) =>\n object.on(key, (e) =>\n this.performLayout(\n key === MODIFIED\n ? {\n type: LAYOUT_TYPE_OBJECT_MODIFIED,\n trigger: key,\n e,\n target,\n }\n : {\n type: LAYOUT_TYPE_OBJECT_MODIFYING,\n trigger: key,\n e,\n target,\n },\n ),\n ),\n );\n }\n\n /**\n * Subscribe an object to transform events that will trigger a layout change on the parent\n * This is important only for interactive groups.\n * @param object\n * @param context\n */\n protected subscribe(\n object: FabricObject,\n context: RegistrationContext & Partial<StrictLayoutContext>,\n ) {\n this.unsubscribe(object, context);\n const disposers = this.attachHandlers(object, context);\n this._subscriptions.set(object, disposers);\n }\n\n /**\n * unsubscribe object layout triggers\n */\n protected unsubscribe(\n object: FabricObject,\n
|