@bwp-web/canvas 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ import { type ReactNode } from 'react';
2
+ import { useEditCanvas, type UseEditCanvasOptions } from '../hooks/useEditCanvas';
3
+ /** The full return value of {@link useEditCanvas}, exposed via context. */
4
+ export type EditCanvasContextValue = ReturnType<typeof useEditCanvas>;
5
+ export interface EditCanvasProviderProps {
6
+ /** Options forwarded to the underlying {@link useEditCanvas} hook. */
7
+ options?: UseEditCanvasOptions;
8
+ children: ReactNode;
9
+ }
10
+ /**
11
+ * Calls {@link useEditCanvas} internally and provides the full canvas API
12
+ * to all descendants via React context.
13
+ *
14
+ * Use {@link useEditCanvasContext} in any descendant to access `canvasRef`,
15
+ * `isDirty`, `viewport`, `setMode`, `setBackground`, and every other value
16
+ * that `useEditCanvas` returns — without prop drilling or bridge contexts.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * <EditCanvasProvider options={{ trackChanges: true, history: true }}>
21
+ * <MyCanvas />
22
+ * <MySidebar />
23
+ * <MyToolbar />
24
+ * </EditCanvasProvider>
25
+ *
26
+ * function MyCanvas() {
27
+ * const { onReady } = useEditCanvasContext();
28
+ * return <Canvas onReady={onReady} />;
29
+ * }
30
+ *
31
+ * function MySidebar() {
32
+ * const { isDirty, resetDirty } = useEditCanvasContext();
33
+ * return <SaveButton disabled={!isDirty} onClick={() => { save(); resetDirty(); }} />;
34
+ * }
35
+ * ```
36
+ */
37
+ export declare function EditCanvasProvider({ options, children, }: EditCanvasProviderProps): import("react/jsx-runtime").JSX.Element;
38
+ /**
39
+ * Access the full {@link useEditCanvas} API from any descendant of
40
+ * {@link EditCanvasProvider}.
41
+ *
42
+ * Throws if called outside of a provider.
43
+ */
44
+ export declare function useEditCanvasContext(): EditCanvasContextValue;
45
+ //# sourceMappingURL=EditCanvasContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EditCanvasContext.d.ts","sourceRoot":"","sources":["../../src/context/EditCanvasContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAClE,OAAO,EACL,aAAa,EACb,KAAK,oBAAoB,EAC1B,MAAM,wBAAwB,CAAC;AAEhC,2EAA2E;AAC3E,MAAM,MAAM,sBAAsB,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AAItE,MAAM,WAAW,uBAAuB;IACtC,sEAAsE;IACtE,OAAO,CAAC,EAAE,oBAAoB,CAAC;IAC/B,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,OAAO,EACP,QAAQ,GACT,EAAE,uBAAuB,2CAOzB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,sBAAsB,CAQ7D"}
@@ -0,0 +1,44 @@
1
+ import { type ReactNode } from 'react';
2
+ import { useViewCanvas, type UseViewCanvasOptions } from '../hooks/useViewCanvas';
3
+ /** The full return value of {@link useViewCanvas}, exposed via context. */
4
+ export type ViewCanvasContextValue = ReturnType<typeof useViewCanvas>;
5
+ export interface ViewCanvasProviderProps {
6
+ /** Options forwarded to the underlying {@link useViewCanvas} hook. */
7
+ options?: UseViewCanvasOptions;
8
+ children: ReactNode;
9
+ }
10
+ /**
11
+ * Calls {@link useViewCanvas} internally and provides the full canvas API
12
+ * to all descendants via React context.
13
+ *
14
+ * Use {@link useViewCanvasContext} in any descendant to access `canvasRef`,
15
+ * `viewport`, `setObjectStyle`, and every other value that `useViewCanvas`
16
+ * returns — without prop drilling or bridge contexts.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * <ViewCanvasProvider options={{ scaledStrokes: true }}>
21
+ * <MyCanvas />
22
+ * <MyToolbar />
23
+ * </ViewCanvasProvider>
24
+ *
25
+ * function MyCanvas() {
26
+ * const { onReady } = useViewCanvasContext();
27
+ * return <Canvas onReady={onReady} />;
28
+ * }
29
+ *
30
+ * function MyToolbar() {
31
+ * const { viewport } = useViewCanvasContext();
32
+ * return <ZoomControls onZoomIn={viewport.zoomIn} onZoomOut={viewport.zoomOut} />;
33
+ * }
34
+ * ```
35
+ */
36
+ export declare function ViewCanvasProvider({ options, children, }: ViewCanvasProviderProps): import("react/jsx-runtime").JSX.Element;
37
+ /**
38
+ * Access the full {@link useViewCanvas} API from any descendant of
39
+ * {@link ViewCanvasProvider}.
40
+ *
41
+ * Throws if called outside of a provider.
42
+ */
43
+ export declare function useViewCanvasContext(): ViewCanvasContextValue;
44
+ //# sourceMappingURL=ViewCanvasContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ViewCanvasContext.d.ts","sourceRoot":"","sources":["../../src/context/ViewCanvasContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAClE,OAAO,EACL,aAAa,EACb,KAAK,oBAAoB,EAC1B,MAAM,wBAAwB,CAAC;AAEhC,2EAA2E;AAC3E,MAAM,MAAM,sBAAsB,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AAItE,MAAM,WAAW,uBAAuB;IACtC,sEAAsE;IACtE,OAAO,CAAC,EAAE,oBAAoB,CAAC;IAC/B,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,OAAO,EACP,QAAQ,GACT,EAAE,uBAAuB,2CAOzB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,sBAAsB,CAQ7D"}
@@ -0,0 +1,5 @@
1
+ export { EditCanvasProvider, useEditCanvasContext } from './EditCanvasContext';
2
+ export type { EditCanvasProviderProps, EditCanvasContextValue, } from './EditCanvasContext';
3
+ export { ViewCanvasProvider, useViewCanvasContext } from './ViewCanvasContext';
4
+ export type { ViewCanvasProviderProps, ViewCanvasContextValue, } from './ViewCanvasContext';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,YAAY,EACV,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,YAAY,EACV,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,qBAAqB,CAAC"}
package/dist/index.cjs CHANGED
@@ -27,6 +27,7 @@ __export(index_exports, {
27
27
  DEFAULT_DRAG_SHAPE_STYLE: () => DEFAULT_DRAG_SHAPE_STYLE,
28
28
  DEFAULT_GUIDELINE_SHAPE_STYLE: () => DEFAULT_GUIDELINE_SHAPE_STYLE,
29
29
  DEFAULT_SHAPE_STYLE: () => DEFAULT_SHAPE_STYLE,
30
+ EditCanvasProvider: () => EditCanvasProvider,
30
31
  FabricCanvas: () => import_fabric19.Canvas,
31
32
  FabricImage: () => import_fabric19.FabricImage,
32
33
  FabricObject: () => import_fabric19.FabricObject,
@@ -37,6 +38,7 @@ __export(index_exports, {
37
38
  Point: () => import_fabric19.Point,
38
39
  Polygon: () => import_fabric19.Polygon,
39
40
  Rect: () => import_fabric19.Rect,
41
+ ViewCanvasProvider: () => ViewCanvasProvider,
40
42
  createCircle: () => createCircle,
41
43
  createCircleAtPoint: () => createCircleAtPoint,
42
44
  createHistoryTracker: () => createHistoryTracker,
@@ -79,7 +81,9 @@ __export(index_exports, {
79
81
  useCanvasEvents: () => useCanvasEvents,
80
82
  useCanvasTooltip: () => useCanvasTooltip,
81
83
  useEditCanvas: () => useEditCanvas,
84
+ useEditCanvasContext: () => useEditCanvasContext,
82
85
  useViewCanvas: () => useViewCanvas,
86
+ useViewCanvasContext: () => useViewCanvasContext,
83
87
  util: () => import_fabric19.util
84
88
  });
85
89
  module.exports = __toCommonJS(index_exports);
@@ -2389,6 +2393,10 @@ async function loadCanvas(canvas, json, options) {
2389
2393
  });
2390
2394
  bg.setCoords();
2391
2395
  }
2396
+ if (bg.filters?.some((f) => f instanceof import_fabric16.filters.Invert)) {
2397
+ bg.filters = bg.filters.filter((f) => !(f instanceof import_fabric16.filters.Invert));
2398
+ bg.applyFilters();
2399
+ }
2392
2400
  }
2393
2401
  if (options?.filter) {
2394
2402
  const toRemove = [];
@@ -3061,11 +3069,53 @@ function useCanvasClick(canvasRef, onClick, options) {
3061
3069
  }, [canvasRef]);
3062
3070
  }
3063
3071
 
3064
- // src/overlay/ObjectOverlay.tsx
3072
+ // src/context/EditCanvasContext.tsx
3065
3073
  var import_react8 = require("react");
3074
+ var import_jsx_runtime2 = require("react/jsx-runtime");
3075
+ var EditCanvasContext = (0, import_react8.createContext)(null);
3076
+ function EditCanvasProvider({
3077
+ options,
3078
+ children
3079
+ }) {
3080
+ const canvas = useEditCanvas(options);
3081
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(EditCanvasContext.Provider, { value: canvas, children });
3082
+ }
3083
+ function useEditCanvasContext() {
3084
+ const ctx = (0, import_react8.useContext)(EditCanvasContext);
3085
+ if (ctx === null) {
3086
+ throw new Error(
3087
+ "useEditCanvasContext must be used within an <EditCanvasProvider>"
3088
+ );
3089
+ }
3090
+ return ctx;
3091
+ }
3092
+
3093
+ // src/context/ViewCanvasContext.tsx
3094
+ var import_react9 = require("react");
3095
+ var import_jsx_runtime3 = require("react/jsx-runtime");
3096
+ var ViewCanvasContext = (0, import_react9.createContext)(null);
3097
+ function ViewCanvasProvider({
3098
+ options,
3099
+ children
3100
+ }) {
3101
+ const canvas = useViewCanvas(options);
3102
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ViewCanvasContext.Provider, { value: canvas, children });
3103
+ }
3104
+ function useViewCanvasContext() {
3105
+ const ctx = (0, import_react9.useContext)(ViewCanvasContext);
3106
+ if (ctx === null) {
3107
+ throw new Error(
3108
+ "useViewCanvasContext must be used within a <ViewCanvasProvider>"
3109
+ );
3110
+ }
3111
+ return ctx;
3112
+ }
3113
+
3114
+ // src/overlay/ObjectOverlay.tsx
3115
+ var import_react10 = require("react");
3066
3116
  var import_material = require("@mui/material");
3067
3117
  var import_fabric18 = require("fabric");
3068
- var import_jsx_runtime2 = require("react/jsx-runtime");
3118
+ var import_jsx_runtime4 = require("react/jsx-runtime");
3069
3119
  function ObjectOverlay({
3070
3120
  canvasRef,
3071
3121
  object,
@@ -3073,8 +3123,8 @@ function ObjectOverlay({
3073
3123
  children,
3074
3124
  ...rest
3075
3125
  }) {
3076
- const stackRef = (0, import_react8.useRef)(null);
3077
- (0, import_react8.useEffect)(() => {
3126
+ const stackRef = (0, import_react10.useRef)(null);
3127
+ (0, import_react10.useEffect)(() => {
3078
3128
  const canvas = canvasRef.current;
3079
3129
  if (!canvas || !object) return;
3080
3130
  function update() {
@@ -3109,7 +3159,7 @@ function ObjectOverlay({
3109
3159
  };
3110
3160
  }, [canvasRef, object]);
3111
3161
  if (!object) return null;
3112
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
3162
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
3113
3163
  import_material.Stack,
3114
3164
  {
3115
3165
  ref: stackRef,
@@ -3129,8 +3179,8 @@ function ObjectOverlay({
3129
3179
 
3130
3180
  // src/overlay/OverlayContent.tsx
3131
3181
  var import_material2 = require("@mui/material");
3132
- var import_react9 = require("react");
3133
- var import_jsx_runtime3 = require("react/jsx-runtime");
3182
+ var import_react11 = require("react");
3183
+ var import_jsx_runtime5 = require("react/jsx-runtime");
3134
3184
  function OverlayContent({
3135
3185
  children,
3136
3186
  padding = 4,
@@ -3138,9 +3188,9 @@ function OverlayContent({
3138
3188
  sx,
3139
3189
  ...rest
3140
3190
  }) {
3141
- const outerRef = (0, import_react9.useRef)(null);
3142
- const innerRef = (0, import_react9.useRef)(null);
3143
- (0, import_react9.useEffect)(() => {
3191
+ const outerRef = (0, import_react11.useRef)(null);
3192
+ const innerRef = (0, import_react11.useRef)(null);
3193
+ (0, import_react11.useEffect)(() => {
3144
3194
  const outer = outerRef.current;
3145
3195
  const inner = innerRef.current;
3146
3196
  if (!outer || !inner) return;
@@ -3169,7 +3219,7 @@ function OverlayContent({
3169
3219
  fit();
3170
3220
  return () => observer.disconnect();
3171
3221
  }, [padding, maxScale]);
3172
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
3222
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
3173
3223
  import_material2.Stack,
3174
3224
  {
3175
3225
  ref: outerRef,
@@ -3182,7 +3232,7 @@ function OverlayContent({
3182
3232
  ...sx
3183
3233
  },
3184
3234
  ...rest,
3185
- children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
3235
+ children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
3186
3236
  import_material2.Stack,
3187
3237
  {
3188
3238
  ref: innerRef,
@@ -3201,8 +3251,8 @@ function OverlayContent({
3201
3251
 
3202
3252
  // src/overlay/FixedSizeContent.tsx
3203
3253
  var import_material3 = require("@mui/material");
3204
- var import_react10 = require("react");
3205
- var import_jsx_runtime4 = require("react/jsx-runtime");
3254
+ var import_react12 = require("react");
3255
+ var import_jsx_runtime6 = require("react/jsx-runtime");
3206
3256
  function FixedSizeContent({
3207
3257
  children,
3208
3258
  hideOnOverflow = true,
@@ -3210,9 +3260,9 @@ function FixedSizeContent({
3210
3260
  sx,
3211
3261
  ...rest
3212
3262
  }) {
3213
- const ref = (0, import_react10.useRef)(null);
3214
- const totalContentHeightRef = (0, import_react10.useRef)(0);
3215
- (0, import_react10.useEffect)(() => {
3263
+ const ref = (0, import_react12.useRef)(null);
3264
+ const totalContentHeightRef = (0, import_react12.useRef)(0);
3265
+ (0, import_react12.useEffect)(() => {
3216
3266
  const el = ref.current;
3217
3267
  if (!el) return;
3218
3268
  let clipAncestor = el.parentElement;
@@ -3249,7 +3299,7 @@ function FixedSizeContent({
3249
3299
  check();
3250
3300
  return () => observer.disconnect();
3251
3301
  }, [hideOnOverflow, truncationPadding]);
3252
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
3302
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
3253
3303
  import_material3.Stack,
3254
3304
  {
3255
3305
  ref,
@@ -3275,8 +3325,8 @@ function FixedSizeContent({
3275
3325
 
3276
3326
  // src/overlay/OverlayBadge.tsx
3277
3327
  var import_material4 = require("@mui/material");
3278
- var import_react11 = require("react");
3279
- var import_jsx_runtime5 = require("react/jsx-runtime");
3328
+ var import_react13 = require("react");
3329
+ var import_jsx_runtime7 = require("react/jsx-runtime");
3280
3330
  function toPx(v) {
3281
3331
  if (v === void 0) return void 0;
3282
3332
  return typeof v === "number" ? `${v}px` : v;
@@ -3318,9 +3368,9 @@ function OverlayBadge({
3318
3368
  sx,
3319
3369
  ...rest
3320
3370
  }) {
3321
- const ref = (0, import_react11.useRef)(null);
3322
- const baseSize = (0, import_react11.useRef)(null);
3323
- (0, import_react11.useEffect)(() => {
3371
+ const ref = (0, import_react13.useRef)(null);
3372
+ const baseSize = (0, import_react13.useRef)(null);
3373
+ (0, import_react13.useEffect)(() => {
3324
3374
  const el = ref.current;
3325
3375
  if (!el) return;
3326
3376
  const ancestor = el.parentElement;
@@ -3372,7 +3422,7 @@ function OverlayBadge({
3372
3422
  bottom: toPx(bottom),
3373
3423
  left: toPx(left)
3374
3424
  };
3375
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
3425
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
3376
3426
  import_material4.Stack,
3377
3427
  {
3378
3428
  ref,
@@ -3403,6 +3453,7 @@ var import_fabric19 = require("fabric");
3403
3453
  DEFAULT_DRAG_SHAPE_STYLE,
3404
3454
  DEFAULT_GUIDELINE_SHAPE_STYLE,
3405
3455
  DEFAULT_SHAPE_STYLE,
3456
+ EditCanvasProvider,
3406
3457
  FabricCanvas,
3407
3458
  FabricImage,
3408
3459
  FabricObject,
@@ -3413,6 +3464,7 @@ var import_fabric19 = require("fabric");
3413
3464
  Point,
3414
3465
  Polygon,
3415
3466
  Rect,
3467
+ ViewCanvasProvider,
3416
3468
  createCircle,
3417
3469
  createCircleAtPoint,
3418
3470
  createHistoryTracker,
@@ -3455,7 +3507,9 @@ var import_fabric19 = require("fabric");
3455
3507
  useCanvasEvents,
3456
3508
  useCanvasTooltip,
3457
3509
  useEditCanvas,
3510
+ useEditCanvasContext,
3458
3511
  useViewCanvas,
3512
+ useViewCanvasContext,
3459
3513
  util
3460
3514
  });
3461
3515
  //# sourceMappingURL=index.cjs.map