{"version":3,"file":"index.mjs","names":[],"sources":["../../../extensions/data_updaters/gradient/index.ts"],"sourcesContent":["import type { GradientOptions, ColorStop } from 'fabric';\nimport { Color, Gradient } from 'fabric';\n\n/**\n * Updates the fromObject function of a Gradient to return a version that can restore old data\n * with opactiy in color Stops\n * Used to upgrade from fabric 6 to fabric 7\n * @param originalFn the original fromObject function of an object,\n * @returns a wrapped fromObject function for the object\n */\n\ntype OldColorStop = ColorStop & {\n  opacity?: number;\n};\n\nexport const gradientUpdaterWrapper = <S, T extends Gradient<S> = Gradient<S>>(\n  originalFn: (\n    options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n  ) => Promise<T>,\n): ((\n  options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n) => Promise<T>) =>\n  async function (this: T, serializedGradient) {\n    // we default to left and top because those are defaults before deprecation\n    const { colorStops } = serializedGradient;\n    // and we do not want to pass those properties on the object anymore\n    const newColorStops: ColorStop[] = (\n      colorStops as OldColorStop[]\n    )?.map<ColorStop>(({ color, opacity, offset }) => {\n      if (opacity === undefined || opacity === 1) {\n        return {\n          color,\n          offset,\n        };\n      }\n      const col = new Color(color).setAlpha(opacity).toRgba();\n      return {\n        color: col,\n        offset,\n      };\n    });\n    const gradient = await originalFn.call(this, {\n      ...serializedGradient,\n      colorStops: newColorStops,\n    });\n    return gradient;\n  };\n\n/**\n * Wraps and override the current fabricJS fromObject static functions\n * Used to upgrade from fabric 7 to fabric 8\n * If you used to export with includeDefaultValues = false, you have to specify\n * which were yours default origins values\n */\nexport const installGradientUpdater = () => {\n  // @ts-expect-error untypable\n  Gradient.fromObject = gradientUpdaterWrapper(Gradient.fromObject);\n};\n"],"mappings":";;AAeA,MAAa,0BACX,eAMA,eAAyB,oBAAoB;CAE3C,MAAM,EAAE,eAAe;CAEvB,MAAM,gBAAA,eAAA,QAAA,eAAA,KAAA,IAAA,KAAA,IACJ,WACC,KAAgB,EAAE,OAAO,SAAS,aAAa;AAChD,MAAI,YAAY,KAAA,KAAa,YAAY,EACvC,QAAO;GACL;GACA;GACD;AAGH,SAAO;GACL,OAFU,IAAI,MAAM,MAAM,CAAC,SAAS,QAAQ,CAAC,QAAQ;GAGrD;GACD;GACD;AAKF,QAJiB,MAAM,WAAW,KAAK,MAAM;EAC3C,GAAG;EACH,YAAY;EACb,CAAC;;;;;;;;AAUN,MAAa,+BAA+B;AAE1C,UAAS,aAAa,uBAAuB,SAAS,WAAW"}