@markup-canvas/core 1.4.2 → 1.4.4

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.
@@ -1,2 +1,2 @@
1
1
  import type { MarkupCanvasConfig, RulerSystem } from "@/types/index.js";
2
- export declare function updateThemeMode(canvasContainer: HTMLElement, config: Required<MarkupCanvasConfig>, rulers: RulerSystem | null, mode: "light" | "dark"): void;
2
+ export declare function updateThemeMode(config: Required<MarkupCanvasConfig>, rulers: RulerSystem | null, mode: "light" | "dark"): void;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Markup Canvas
3
3
  * High-performance markup canvas with zoom and pan capabilities
4
- * @version 1.4.2
4
+ * @version 1.4.4
5
5
  */
6
6
  'use strict';
7
7
 
@@ -253,14 +253,6 @@ function withRulerOffset(canvas, x, y, rulerSize, operation) {
253
253
  return operation(adjustedX, adjustedY);
254
254
  }
255
255
 
256
- function getThemeValue(config, key) {
257
- if (config.themeMode === "dark") {
258
- const darkKey = `${key}Dark`;
259
- return config[darkKey];
260
- }
261
- return config[key];
262
- }
263
-
264
256
  const DEFAULT_CONFIG = {
265
257
  // Canvas dimensions
266
258
  width: 8000,
@@ -670,15 +662,12 @@ function createMarkupCanvasConfig(options = {}) {
670
662
  return config;
671
663
  }
672
664
 
673
- function updateThemeMode(canvasContainer, config, rulers, mode) {
665
+ function updateThemeMode(config, rulers, mode) {
674
666
  const newConfig = {
675
667
  ...config,
676
668
  themeMode: mode,
677
669
  };
678
670
  const updatedConfig = createMarkupCanvasConfig(newConfig);
679
- // Update canvas background color
680
- const backgroundColor = getThemeValue(updatedConfig, "canvasBackgroundColor");
681
- canvasContainer.style.backgroundColor = backgroundColor;
682
671
  // Update rulers if they exist
683
672
  if (rulers) {
684
673
  rulers.updateTheme(updatedConfig);
@@ -846,9 +835,9 @@ function setupCanvasContainer(container, config) {
846
835
  container.style.overflow = "hidden";
847
836
  container.style.cursor = "grab";
848
837
  container.style.overscrollBehavior = "none";
849
- // Apply canvas background color
838
+ // Apply canvas background color using light-dark() CSS function
850
839
  if (config) {
851
- const backgroundColor = getThemeValue(config, "canvasBackgroundColor");
840
+ const backgroundColor = `light-dark(${config.canvasBackgroundColor}, ${config.canvasBackgroundColorDark})`;
852
841
  container.style.backgroundColor = backgroundColor;
853
842
  }
854
843
  if (!container.hasAttribute("tabindex")) {
@@ -2047,12 +2036,12 @@ function updateRulers(canvas, horizontalRuler, verticalRuler, gridOverlay, confi
2047
2036
  }
2048
2037
 
2049
2038
  function updateRulerTheme(elements, config) {
2050
- // Get theme-aware colors
2051
- const backgroundColor = getThemeValue(config, "rulerBackgroundColor");
2052
- const borderColor = getThemeValue(config, "rulerBorderColor");
2053
- const textColor = getThemeValue(config, "rulerTextColor");
2054
- const tickColor = getThemeValue(config, "rulerTickColor");
2055
- const gridColor = getThemeValue(config, "gridColor");
2039
+ // Create theme-aware colors using light-dark() CSS function
2040
+ const backgroundColor = `light-dark(${config.rulerBackgroundColor}, ${config.rulerBackgroundColorDark})`;
2041
+ const borderColor = `light-dark(${config.rulerBorderColor}, ${config.rulerBorderColorDark})`;
2042
+ const textColor = `light-dark(${config.rulerTextColor}, ${config.rulerTextColorDark})`;
2043
+ const tickColor = `light-dark(${config.rulerTickColor}, ${config.rulerTickColorDark})`;
2044
+ const gridColor = `light-dark(${config.gridColor}, ${config.gridColorDark})`;
2056
2045
  // Update horizontal ruler with CSS variables
2057
2046
  if (elements.horizontalRuler) {
2058
2047
  elements.horizontalRuler.style.setProperty("--ruler-background-color", backgroundColor);
@@ -2357,9 +2346,6 @@ class MarkupCanvas {
2357
2346
  }
2358
2347
  });
2359
2348
  });
2360
- if (options.themeMode) {
2361
- updateThemeMode(this.canvas.container, this.config, this.rulers, options.themeMode);
2362
- }
2363
2349
  // Always bind canvas to window
2364
2350
  this.event.setEmitInterceptor((event, data) => {
2365
2351
  broadcastEvent(event, data, this.config);
@@ -2650,7 +2636,7 @@ class MarkupCanvas {
2650
2636
  // Theme management
2651
2637
  updateThemeMode(mode) {
2652
2638
  this.config = createMarkupCanvasConfig({ ...this.config, themeMode: mode });
2653
- updateThemeMode(this.canvas.container, this.config, this.rulers, mode);
2639
+ updateThemeMode(this.config, this.rulers, mode);
2654
2640
  }
2655
2641
  toggleThemeMode() {
2656
2642
  const currentMode = this.config.themeMode;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Markup Canvas
3
3
  * High-performance markup canvas with zoom and pan capabilities
4
- * @version 1.4.2
4
+ * @version 1.4.4
5
5
  */
6
6
  const EDITOR_PRESET = {
7
7
  // Canvas dimensions
@@ -249,14 +249,6 @@ function withRulerOffset(canvas, x, y, rulerSize, operation) {
249
249
  return operation(adjustedX, adjustedY);
250
250
  }
251
251
 
252
- function getThemeValue(config, key) {
253
- if (config.themeMode === "dark") {
254
- const darkKey = `${key}Dark`;
255
- return config[darkKey];
256
- }
257
- return config[key];
258
- }
259
-
260
252
  const DEFAULT_CONFIG = {
261
253
  // Canvas dimensions
262
254
  width: 8000,
@@ -666,15 +658,12 @@ function createMarkupCanvasConfig(options = {}) {
666
658
  return config;
667
659
  }
668
660
 
669
- function updateThemeMode(canvasContainer, config, rulers, mode) {
661
+ function updateThemeMode(config, rulers, mode) {
670
662
  const newConfig = {
671
663
  ...config,
672
664
  themeMode: mode,
673
665
  };
674
666
  const updatedConfig = createMarkupCanvasConfig(newConfig);
675
- // Update canvas background color
676
- const backgroundColor = getThemeValue(updatedConfig, "canvasBackgroundColor");
677
- canvasContainer.style.backgroundColor = backgroundColor;
678
667
  // Update rulers if they exist
679
668
  if (rulers) {
680
669
  rulers.updateTheme(updatedConfig);
@@ -842,9 +831,9 @@ function setupCanvasContainer(container, config) {
842
831
  container.style.overflow = "hidden";
843
832
  container.style.cursor = "grab";
844
833
  container.style.overscrollBehavior = "none";
845
- // Apply canvas background color
834
+ // Apply canvas background color using light-dark() CSS function
846
835
  if (config) {
847
- const backgroundColor = getThemeValue(config, "canvasBackgroundColor");
836
+ const backgroundColor = `light-dark(${config.canvasBackgroundColor}, ${config.canvasBackgroundColorDark})`;
848
837
  container.style.backgroundColor = backgroundColor;
849
838
  }
850
839
  if (!container.hasAttribute("tabindex")) {
@@ -2043,12 +2032,12 @@ function updateRulers(canvas, horizontalRuler, verticalRuler, gridOverlay, confi
2043
2032
  }
2044
2033
 
2045
2034
  function updateRulerTheme(elements, config) {
2046
- // Get theme-aware colors
2047
- const backgroundColor = getThemeValue(config, "rulerBackgroundColor");
2048
- const borderColor = getThemeValue(config, "rulerBorderColor");
2049
- const textColor = getThemeValue(config, "rulerTextColor");
2050
- const tickColor = getThemeValue(config, "rulerTickColor");
2051
- const gridColor = getThemeValue(config, "gridColor");
2035
+ // Create theme-aware colors using light-dark() CSS function
2036
+ const backgroundColor = `light-dark(${config.rulerBackgroundColor}, ${config.rulerBackgroundColorDark})`;
2037
+ const borderColor = `light-dark(${config.rulerBorderColor}, ${config.rulerBorderColorDark})`;
2038
+ const textColor = `light-dark(${config.rulerTextColor}, ${config.rulerTextColorDark})`;
2039
+ const tickColor = `light-dark(${config.rulerTickColor}, ${config.rulerTickColorDark})`;
2040
+ const gridColor = `light-dark(${config.gridColor}, ${config.gridColorDark})`;
2052
2041
  // Update horizontal ruler with CSS variables
2053
2042
  if (elements.horizontalRuler) {
2054
2043
  elements.horizontalRuler.style.setProperty("--ruler-background-color", backgroundColor);
@@ -2353,9 +2342,6 @@ class MarkupCanvas {
2353
2342
  }
2354
2343
  });
2355
2344
  });
2356
- if (options.themeMode) {
2357
- updateThemeMode(this.canvas.container, this.config, this.rulers, options.themeMode);
2358
- }
2359
2345
  // Always bind canvas to window
2360
2346
  this.event.setEmitInterceptor((event, data) => {
2361
2347
  broadcastEvent(event, data, this.config);
@@ -2646,7 +2632,7 @@ class MarkupCanvas {
2646
2632
  // Theme management
2647
2633
  updateThemeMode(mode) {
2648
2634
  this.config = createMarkupCanvasConfig({ ...this.config, themeMode: mode });
2649
- updateThemeMode(this.canvas.container, this.config, this.rulers, mode);
2635
+ updateThemeMode(this.config, this.rulers, mode);
2650
2636
  }
2651
2637
  toggleThemeMode() {
2652
2638
  const currentMode = this.config.themeMode;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Markup Canvas
3
3
  * High-performance markup canvas with zoom and pan capabilities
4
- * @version 1.4.2
4
+ * @version 1.4.4
5
5
  */
6
6
  (function (global, factory) {
7
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@@ -193,14 +193,6 @@
193
193
  return operation(adjustedX, adjustedY);
194
194
  }
195
195
 
196
- function getThemeValue(config, key) {
197
- if (config.themeMode === "dark") {
198
- const darkKey = `${key}Dark`;
199
- return config[darkKey];
200
- }
201
- return config[key];
202
- }
203
-
204
196
  const DEFAULT_CONFIG = {
205
197
  // Canvas dimensions
206
198
  width: 8000,
@@ -610,15 +602,12 @@
610
602
  return config;
611
603
  }
612
604
 
613
- function updateThemeMode(canvasContainer, config, rulers, mode) {
605
+ function updateThemeMode(config, rulers, mode) {
614
606
  const newConfig = {
615
607
  ...config,
616
608
  themeMode: mode,
617
609
  };
618
610
  const updatedConfig = createMarkupCanvasConfig(newConfig);
619
- // Update canvas background color
620
- const backgroundColor = getThemeValue(updatedConfig, "canvasBackgroundColor");
621
- canvasContainer.style.backgroundColor = backgroundColor;
622
611
  // Update rulers if they exist
623
612
  if (rulers) {
624
613
  rulers.updateTheme(updatedConfig);
@@ -786,9 +775,9 @@
786
775
  container.style.overflow = "hidden";
787
776
  container.style.cursor = "grab";
788
777
  container.style.overscrollBehavior = "none";
789
- // Apply canvas background color
778
+ // Apply canvas background color using light-dark() CSS function
790
779
  if (config) {
791
- const backgroundColor = getThemeValue(config, "canvasBackgroundColor");
780
+ const backgroundColor = `light-dark(${config.canvasBackgroundColor}, ${config.canvasBackgroundColorDark})`;
792
781
  container.style.backgroundColor = backgroundColor;
793
782
  }
794
783
  if (!container.hasAttribute("tabindex")) {
@@ -1987,12 +1976,12 @@
1987
1976
  }
1988
1977
 
1989
1978
  function updateRulerTheme(elements, config) {
1990
- // Get theme-aware colors
1991
- const backgroundColor = getThemeValue(config, "rulerBackgroundColor");
1992
- const borderColor = getThemeValue(config, "rulerBorderColor");
1993
- const textColor = getThemeValue(config, "rulerTextColor");
1994
- const tickColor = getThemeValue(config, "rulerTickColor");
1995
- const gridColor = getThemeValue(config, "gridColor");
1979
+ // Create theme-aware colors using light-dark() CSS function
1980
+ const backgroundColor = `light-dark(${config.rulerBackgroundColor}, ${config.rulerBackgroundColorDark})`;
1981
+ const borderColor = `light-dark(${config.rulerBorderColor}, ${config.rulerBorderColorDark})`;
1982
+ const textColor = `light-dark(${config.rulerTextColor}, ${config.rulerTextColorDark})`;
1983
+ const tickColor = `light-dark(${config.rulerTickColor}, ${config.rulerTickColorDark})`;
1984
+ const gridColor = `light-dark(${config.gridColor}, ${config.gridColorDark})`;
1996
1985
  // Update horizontal ruler with CSS variables
1997
1986
  if (elements.horizontalRuler) {
1998
1987
  elements.horizontalRuler.style.setProperty("--ruler-background-color", backgroundColor);
@@ -2297,9 +2286,6 @@
2297
2286
  }
2298
2287
  });
2299
2288
  });
2300
- if (options.themeMode) {
2301
- updateThemeMode(this.canvas.container, this.config, this.rulers, options.themeMode);
2302
- }
2303
2289
  // Always bind canvas to window
2304
2290
  this.event.setEmitInterceptor((event, data) => {
2305
2291
  broadcastEvent(event, data, this.config);
@@ -2590,7 +2576,7 @@
2590
2576
  // Theme management
2591
2577
  updateThemeMode(mode) {
2592
2578
  this.config = createMarkupCanvasConfig({ ...this.config, themeMode: mode });
2593
- updateThemeMode(this.canvas.container, this.config, this.rulers, mode);
2579
+ updateThemeMode(this.config, this.rulers, mode);
2594
2580
  }
2595
2581
  toggleThemeMode() {
2596
2582
  const currentMode = this.config.themeMode;
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).MarkupCanvas=t()}(this,function(){"use strict";function e(e,t,n){if(!n?.inverse)return{x:e,y:t};try{const r=n.inverse(),a=new DOMPoint(e,t).matrixTransform(r);return{x:a.x,y:a.y}}catch(n){return console.warn("Canvas to content conversion failed:",n),{x:e,y:t}}}function t(e,t){return Math.max(t.minZoom,Math.min(t.maxZoom,e))}function n(e,t,n){return new DOMMatrix([e,0,0,e,t,n])}const r="canvas-container",a="transform-layer",o="content-layer";function i(e,n,r,a,o){const i=o.enableRulers?-o.rulerSize:0,s=r||{scale:1,translateX:i,translateY:i},{scale:l,translateX:c,translateY:u}=s,d=t(l*a,o);if(Math.abs(d-l)<.001)return{scale:l,translateX:c,translateY:u};return{scale:d,translateX:e-(e-c)/l*d,translateY:n-(n-u)/l*d}}function s(e){return e.getBounds().visibleArea}function l(e,n){return n(n=>t(n,e))}const c=new Map;function u(e,t,n){return e[t]?n():null}function d(e){let t=null,n=null;const r=(...r)=>{n=r,null===t&&(t=requestAnimationFrame(()=>{n&&e(...n),t=null,n=null}))};return r.cleanup=()=>{null!==t&&(cancelAnimationFrame(t),t=null,n=null)},r}function h(e,t,n){return n(null!==e.container.querySelector(".canvas-ruler")?t:0)}function m(e,t,n,r,a){const o=null!==e.container.querySelector(".canvas-ruler");return a(o?t-r:t,o?n-r:n)}function g(e,t){if("dark"===e.themeMode){return e[`${t}Dark`]}return e[t]}const p={width:8e3,height:8e3,enableAcceleration:!0,initialZoom:1,initialPan:{x:0,y:0},name:"markupCanvas",enablePostMessageAPI:!1,enableZoom:!0,enablePan:!0,enableTouch:!0,enableKeyboard:!0,bindKeyboardEventsTo:"document",sendKeyboardEventsToParent:!1,zoomSpeed:1.5,minZoom:.05,maxZoom:80,enableTransition:!0,transitionDuration:.2,enableAdaptiveSpeed:!0,enableLeftDrag:!0,enableMiddleDrag:!0,requireSpaceForMouseDrag:!1,keyboardPanStep:50,keyboardFastMultiplier:20,keyboardZoomStep:.2,enableClickToZoom:!0,clickZoomLevel:1,requireOptionForClickZoom:!1,enableRulers:!0,enableGrid:!1,showRulers:!0,showGrid:!1,rulerFontSize:9,rulerFontFamily:"Monaco, Menlo, monospace",rulerUnits:"px",rulerSize:20,canvasBackgroundColor:"rgba(250, 250, 250, 1)",canvasBackgroundColorDark:"rgba(40, 40, 40, 1)",rulerBackgroundColor:"rgba(255, 255, 255, 0.95)",rulerBorderColor:"rgba(240, 240, 240, 1)",rulerTextColor:"rgba(102, 102, 102, 1)",rulerTickColor:"rgba(204, 204, 204, 1)",gridColor:"rgba(232, 86, 193, 0.5)",rulerBackgroundColorDark:"rgba(30, 30, 30, 0.95)",rulerBorderColorDark:"rgba(68, 68, 68, 1)",rulerTextColorDark:"rgba(170, 170, 170, 1)",rulerTickColorDark:"rgba(104, 104, 104, 1)",gridColorDark:"rgba(232, 86, 193, 0.5)",themeMode:"light"};function f(t,r){try{const a=t.container,o=t.transform||{scale:1,translateX:0,translateY:0},i=a.getBoundingClientRect(),s=i.width||a.clientWidth||0,l=i.height||a.clientHeight||0,c=h({container:a},r.rulerSize,e=>Math.max(0,s-e)),u=h({container:a},r.rulerSize,e=>Math.max(0,l-e)),d=r.width||p.width,m=r.height||p.height,g=function(t,r,a,o,i){const s=e(0,0,n(i.scale,i.translateX,i.translateY)),l=e(t,r,n(i.scale,i.translateX,i.translateY));return{x:Math.max(0,Math.min(a,s.x)),y:Math.max(0,Math.min(o,s.y)),width:Math.max(0,Math.min(a-s.x,l.x-s.x)),height:Math.max(0,Math.min(o-s.y,l.y-s.y))}}(c,u,d,m,o);return{width:c,height:u,contentWidth:d,contentHeight:m,scale:o.scale,translateX:o.translateX,translateY:o.translateY,visibleArea:g,scaledContentWidth:d*o.scale,scaledContentHeight:m*o.scale,canPanLeft:o.translateX<0,canPanRight:o.translateX+d*o.scale>c,canPanUp:o.translateY<0,canPanDown:o.translateY+m*o.scale>u,canZoomIn:o.scale<3.5,canZoomOut:o.scale>.1}}catch(e){return console.error("Failed to calculate canvas bounds:",e),{width:0,height:0,contentWidth:0,contentHeight:0,scale:1,translateX:0,translateY:0,visibleArea:{x:0,y:0,width:0,height:0},scaledContentWidth:0,scaledContentHeight:0,canPanLeft:!1,canPanRight:!1,canPanUp:!1,canPanDown:!1,canZoomIn:!1,canZoomOut:!1}}}function b(e,t){try{if(t.enableTransition){window.__markupCanvasTransitionTimeout&&(clearTimeout(window.__markupCanvasTransitionTimeout),window.__markupCanvasTransitionTimeout=void 0);return function(e,t,n){const r=c.get(e);r&&clearTimeout(r);const a=window.setTimeout(()=>{n(),c.delete(e)},t);c.set(e,a)}("disableTransition",1e3*(t.transitionDuration??.2),()=>{e.style.transition="none",window.__markupCanvasTransitionTimeout=void 0}),!0}return!1}catch(e){return console.error("Failed to disable transitions:",e),!0}}function y(e,t,n){!function(e,t){try{return!!t.enableTransition&&(window.__markupCanvasTransitionTimeout&&(clearTimeout(window.__markupCanvasTransitionTimeout),window.__markupCanvasTransitionTimeout=void 0),e.style.transition=`transform ${t.transitionDuration}s linear`,!0)}catch(e){return console.error("Failed to enable transitions:",e),!1}}(e,t);try{return n()}finally{b(e,t)}}function v(e,t,n){const r=t.keyboardPanStep;return n({translateY:e.transform.translateY-r})}function k(e,t,n){const r=t.keyboardPanStep;return n({translateX:e.transform.translateX+r})}function w(e,t,n){const r=t.keyboardPanStep;return n({translateX:e.transform.translateX-r})}function x(e,t,n){const r=t.keyboardPanStep;return n({translateY:e.transform.translateY+r})}function T(e,t){if(!e?.style||!t)return!1;try{return e.style.transform=function(e){return`matrix3d(${e.m11}, ${e.m12}, ${e.m13}, ${e.m14}, ${e.m21}, ${e.m22}, ${e.m23}, ${e.m24}, ${e.m31}, ${e.m32}, ${e.m33}, ${e.m34}, ${e.m41}, ${e.m42}, ${e.m43}, ${e.m44})`}(t),!0}catch(e){return console.warn("Transform application failed:",e),!1}}function C(e,t){e.transform={...e.transform,...t};const r=n(e.transform.scale,e.transform.translateX,e.transform.translateY);return T(e.transformLayer,r)}function M(e={}){const t={...p,...e};return("number"!=typeof t.width||t.width<=0)&&(console.warn("Invalid width, using default"),t.width=p.width),("number"!=typeof t.height||t.height<=0)&&(console.warn("Invalid height, using default"),t.height=p.height),("number"!=typeof t.zoomSpeed||t.zoomSpeed<=0)&&(console.warn("Invalid zoomSpeed, using default"),t.zoomSpeed=p.zoomSpeed),("number"!=typeof t.minZoom||t.minZoom<=0)&&(console.warn("Invalid minZoom, using default"),t.minZoom=p.minZoom),("number"!=typeof t.maxZoom||t.maxZoom<=t.minZoom)&&(console.warn("Invalid maxZoom, using default"),t.maxZoom=p.maxZoom),("number"!=typeof t.keyboardPanStep||t.keyboardPanStep<=0)&&(console.warn("Invalid keyboardPanStep, using default"),t.keyboardPanStep=p.keyboardPanStep),("number"!=typeof t.keyboardFastMultiplier||t.keyboardFastMultiplier<=0)&&(console.warn("Invalid keyboardFastMultiplier, using default"),t.keyboardFastMultiplier=p.keyboardFastMultiplier),("number"!=typeof t.clickZoomLevel||t.clickZoomLevel<=0)&&(console.warn("Invalid clickZoomLevel, using default"),t.clickZoomLevel=p.clickZoomLevel),("number"!=typeof t.rulerFontSize||t.rulerFontSize<=0)&&(console.warn("Invalid rulerFontSize, using default"),t.rulerFontSize=p.rulerFontSize),("number"!=typeof t.rulerSize||t.rulerSize<=0)&&(console.warn("Invalid rulerSize, using default"),t.rulerSize=p.rulerSize),t}function S(e,t,n,r){const a=M({...t,themeMode:r}),o=g(a,"canvasBackgroundColor");e.style.backgroundColor=o,n&&n.updateTheme(a)}function D(e){try{const t=e.getBounds();return{x:t.width/2,y:t.height/2}}catch(e){return console.warn("Failed to calculate viewport center:",e),{x:0,y:0}}}function E(e,t){const n=Array.from(e.children);let r=e.querySelector(`.${a}`);r||(r=document.createElement("div"),r.className=a,e.appendChild(r)),function(e,t){e.style.position="absolute";const n=t.rulerSize;e.style.top=`${n}px`,e.style.left=`${n}px`,e.style.width=`${t.width}px`,e.style.height=`${t.height}px`,e.style.transformOrigin="0 0"}(r,t);let i=r.querySelector(`.${o}`);return i||(i=document.createElement("div"),i.className=o,r.appendChild(i),function(e,t,n){e.forEach(e=>{e===n||e.classList.contains(a)||t.appendChild(e)})}(n,i,r)),function(e){e.style.position="relative",e.style.width="100%",e.style.height="100%",e.style.pointerEvents="auto"}(i),{transformLayer:r,contentLayer:i}}function z(e,t){if("static"===getComputedStyle(e).position&&(e.style.position="relative"),e.style.overflow="hidden",e.style.cursor="grab",e.style.overscrollBehavior="none",t){const n=g(t,"canvasBackgroundColor");e.style.backgroundColor=n}e.hasAttribute("tabindex")||e.setAttribute("tabindex","0"),function(e){const t=e.getBoundingClientRect(),n=getComputedStyle(e);0===t.height&&"auto"===n.height&&console.error("MarkupCanvas: Container height is 0. Please set a height on your container element using CSS.","Examples: height: 100vh, height: 500px, or use flexbox/grid layout.",e),0===t.width&&"auto"===n.width&&console.error("MarkupCanvas: Container width is 0. Please set a width on your container element using CSS.","Examples: width: 100vw, width: 800px, or use flexbox/grid layout.",e)}(e),e.classList.contains(r)||e.classList.add(r)}function L(e,t){if(!e?.appendChild)return console.error("Invalid container element provided to createCanvas"),null;try{z(e,t);const{transformLayer:r,contentLayer:a}=E(e,t);u(t,"enableAcceleration",()=>{!function(e){try{return e.style.transform=e.style.transform||"translateZ(0)",e.style.backfaceVisibility="hidden",!0}catch(e){return console.error("Failed to enable hardware acceleration:",e),!1}}(r)});const o=t.enableRulers?-t.rulerSize:0,i={scale:t.initialZoom??1,translateX:(t.initialPan?.x??0)+o,translateY:(t.initialPan?.y??0)+o},s=n(i.scale,i.translateX,i.translateY);T(r,s);return{container:e,transformLayer:r,contentLayer:a,transform:i}}catch(e){return console.error("Failed to create canvas:",e),null}}class R{constructor(){this.listeners=new Map}setEmitInterceptor(e){this.emitInterceptor=e}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t)}off(e,t){const n=this.listeners.get(e);n&&n.delete(t)}emit(e,t){this.emitInterceptor?.(e,t);const n=this.listeners.get(e);n&&n.forEach(n=>{try{n(t)}catch(t){console.error(`Error in event handler for "${String(e)}":`,t)}})}removeAllListeners(){this.listeners.clear()}}function P(e,t){const n=t.transform;e.emit("transform",n),e.emit("zoom",n.scale),e.emit("pan",{x:n.translateX,y:n.translateY})}const Y=300,X=5;function I(e,t){if(!e?.getBounds)return t;try{const n=e.getBounds(),r=n.width*n.height;return t*(r/2073600)**1}catch(e){return console.warn("Failed to calculate adaptive zoom speed, using base speed:",e),t}}function $(e,t,n){const r=n?.textEditModeEnabled??!1;function a(n){if(!(n instanceof KeyboardEvent))return;if("canvas"===t.bindKeyboardEventsTo&&document.activeElement!==e.container)return;if(u(t,"sendKeyboardEventsToParent",()=>{r&&!function(e){return!("0"!==e.key||!e.ctrlKey&&!e.metaKey)||!("+"!==e.key||!e.ctrlKey&&!e.metaKey)||!("-"!==e.key||!e.ctrlKey&&!e.metaKey)}(n)||(!function(e,t){if("undefined"==typeof window||!window.parent)return;const n=t.name||"markupCanvas";window.parent.postMessage({source:"markup-canvas",action:"keyboardShortcut",data:{key:e.key,ctrlKey:e.ctrlKey,metaKey:e.metaKey,shiftKey:e.shiftKey,altKey:e.altKey,code:e.code},timestamp:Date.now(),canvasName:n},"*")}(n,t),n.preventDefault())}),t.sendKeyboardEventsToParent)return;let a=!1;const o={};switch(n.key){case"ArrowLeft":if(r)return;o.translateX=e.transform.translateX+t.keyboardPanStep,a=!0;break;case"ArrowRight":if(r)return;o.translateX=e.transform.translateX-t.keyboardPanStep,a=!0;break;case"ArrowUp":if(r)return;o.translateY=e.transform.translateY+t.keyboardPanStep,a=!0;break;case"ArrowDown":if(r)return;o.translateY=e.transform.translateY-t.keyboardPanStep,a=!0;break;case"=":case"+":if(r)return;{const n=t.enableAdaptiveSpeed?I(e,t.keyboardZoomStep):t.keyboardZoomStep;e.zoomIn(n),a=!0}break;case"-":if(r)return;{const n=t.enableAdaptiveSpeed?I(e,t.keyboardZoomStep):t.keyboardZoomStep;e.zoomOut(n),a=!0}break;case"0":n.ctrlKey?(e.resetView&&e.resetView(),a=!0):n.metaKey&&(e.resetViewToCenter&&e.resetViewToCenter(),a=!0);break;case"g":case"G":!n.shiftKey||n.ctrlKey||n.metaKey||n.altKey||!e.toggleGrid||(e.toggleGrid(),a=!0);break;case"r":case"R":!n.shiftKey||n.metaKey||n.ctrlKey||n.altKey||!e.toggleRulers||(e.toggleRulers(),a=!0)}a&&(n.preventDefault(),Object.keys(o).length>0&&e.updateTransform(o))}const o="canvas"===t.bindKeyboardEventsTo?e.container:document;return o.addEventListener("keydown",a),()=>{o.removeEventListener("keydown",a)}}function B(e,t,n,r,a){n?t.requireSpaceForMouseDrag?e.container.style.cursor=r?"grab":"default":e.container.style.cursor=a?"grabbing":"grab":e.container.style.cursor="default"}function Z(e,t,n,r,a){a.setIsDragging(!1),a.setDragButton(-1),B(e,t,n,r,!1)}function F(e,t,n,r,a,o,i,s,l,c){o&&e.button===i&&Z(t,n,r,a,{setIsDragging:c.setIsDragging,setDragButton:c.setDragButton}),r&&0===e.button&&n.enableClickToZoom&&s>0&&function(e,t,n,r,a,o){const i=Date.now()-r,s=e.altKey,l=!n.requireOptionForClickZoom||s;if(i<Y&&!a&&!o&&l){e.preventDefault();const r=t.container.getBoundingClientRect(),a=e.clientX-r.left,o=e.clientY-r.top,{clickX:i,clickY:s}=m(t,a,o,n.rulerSize,(e,t)=>({clickX:e,clickY:t})),l=t.canvasToContent(i,s),c=r.width/2,u=r.height/2,d=n.clickZoomLevel,h={scale:d,translateX:c-l.x*d,translateY:u-l.y*d};y(t.transformLayer,t.config,()=>{t.updateTransform(h)})}}(e,t,n,s,l,o),0===e.button&&function(e){e.setMouseDownTime(0),e.setHasDragged(!1)}({setMouseDownTime:c.setMouseDownTime,setHasDragged:c.setHasDragged})}function K(e,t,n=!0){let r=!0,a=!1,o=0,i=0,s=-1,l=!1,c=0,u=0,h=0,m=!1;const g={setIsDragging:e=>{a=e},setDragButton:e=>{s=e},setIsSpacePressed:e=>{l=e},setMouseDownTime:e=>{c=e},setMouseDownX:e=>{u=e},setMouseDownY:e=>{h=e},setHasDragged:e=>{m=e},setLastMouseX:e=>{o=e},setLastMouseY:e=>{i=e}},p=n=>{!function(e,t,n,r,a,o){n.requireSpaceForMouseDrag&&" "===e.key&&(o.setIsSpacePressed(!0),B(t,n,r,!0,a))}(n,e,t,r,a,{setIsSpacePressed:g.setIsSpacePressed})},f=n=>{!function(e,t,n,r,a,o){n.requireSpaceForMouseDrag&&" "===e.key&&(o.setIsSpacePressed(!1),B(t,n,r,!1,a),a&&Z(t,n,r,!1,{setIsDragging:o.setIsDragging,setDragButton:o.setDragButton}))}(n,e,t,r,a,{setIsSpacePressed:g.setIsSpacePressed,setIsDragging:g.setIsDragging,setDragButton:g.setDragButton})},b=n=>{!function(e,t,n,r,a,o){const i=0===e.button,s=1===e.button;if(i&&(o.setMouseDownTime(Date.now()),o.setMouseDownX(e.clientX),o.setMouseDownY(e.clientY),o.setHasDragged(!1)),!r)return;(!n.requireSpaceForMouseDrag||a)&&(i&&n.enableLeftDrag||s&&n.enableMiddleDrag)&&(e.preventDefault(),o.setDragButton(e.button),o.setLastMouseX(e.clientX),o.setLastMouseY(e.clientY),B(t,n,r,a,!1))}(n,e,t,r,l,g)},y=n=>{!function(e,t,n,r,a,o,i,s,l,c,u,h){if(i>0){const t=Math.abs(e.clientX-s),i=Math.abs(e.clientY-l);if(t>X||i>X){h.setHasDragged(!0);const e=!n.requireSpaceForMouseDrag||o;!a&&r&&e&&h.setIsDragging(!0)}}if(!a||!r)return;e.preventDefault(),d((...e)=>{const n=e[0];if(!a||!r)return;const o=n.clientX-c,i=n.clientY-u,s={translateX:t.transform.translateX+o,translateY:t.transform.translateY+i};t.updateTransform(s),h.setLastMouseX(n.clientX),h.setLastMouseY(n.clientY)})(e)}(n,e,t,r,a,l,c,u,h,o,i,{setHasDragged:g.setHasDragged,setIsDragging:g.setIsDragging,setLastMouseX:g.setLastMouseX,setLastMouseY:g.setLastMouseY})},v=n=>{F(n,e,t,r,l,a,s,c,m,{setIsDragging:g.setIsDragging,setDragButton:g.setDragButton,setMouseDownTime:g.setMouseDownTime,setHasDragged:g.setHasDragged})},k=()=>{!function(e,t,n,r,a,o){a&&Z(e,t,n,r,o)}(e,t,r,l,a,{setIsDragging:g.setIsDragging,setDragButton:g.setDragButton})};e.container.addEventListener("mousedown",b),document.addEventListener("mousemove",y),document.addEventListener("mouseup",v),e.container.addEventListener("mouseleave",k),t.requireSpaceForMouseDrag&&(document.addEventListener("keydown",p),document.addEventListener("keyup",f)),B(e,t,r,l,a);const w=()=>{e.container.removeEventListener("mousedown",b),document.removeEventListener("mousemove",y),document.removeEventListener("mouseup",v),e.container.removeEventListener("mouseleave",k),t.requireSpaceForMouseDrag&&(document.removeEventListener("keydown",p),document.removeEventListener("keyup",f))};return n?{cleanup:w,enable:()=>(r=!0,B(e,t,r,l,a),!0),disable:()=>(r=!1,a&&Z(e,t,r,l,{setIsDragging:g.setIsDragging,setDragButton:g.setDragButton}),B(e,t,r,l,a),!0),isEnabled:()=>r}:w}function O(e,t,n,r){try{switch(t){case"zoomIn":e.zoomIn(n);break;case"zoomOut":e.zoomOut(n);break;case"setZoom":{const t=n;if("number"!=typeof t||t<=0)throw new Error(`Invalid zoom level: ${t}. Must be a positive number.`);e.setZoom(t);break}case"resetZoom":e.resetZoom();break;case"panLeft":e.panLeft(n);break;case"panRight":e.panRight(n);break;case"panUp":e.panUp(n);break;case"panDown":e.panDown(n);break;case"fitToScreen":e.fitToScreen();break;case"centerContent":e.centerContent();break;case"panToPoint":{const t=n;e.panToPoint(t.x,t.y);break}case"resetView":e.resetView();break;case"resetViewToCenter":e.resetViewToCenter();break;case"resetToInitial":e.resetToInitial();break;case"toggleRulers":e.toggleRulers();break;case"showRulers":e.showRulers();break;case"hideRulers":e.hideRulers();break;case"toggleGrid":e.toggleGrid();break;case"showGrid":e.showGrid();break;case"hideGrid":e.hideGrid();break;case"updateThemeMode":{const t=n;if("light"!==t&&"dark"!==t)throw new Error(`Invalid theme mode: ${t}`);e.updateThemeMode(t);break}case"toggleThemeMode":{const t="light"===e.getConfig().themeMode?"dark":"light";e.updateThemeMode(t);break}case"updateTransition":{const t=n;if("boolean"!=typeof t)throw new Error(`Invalid transition enabled value: ${t}. Must be a boolean.`);e.updateTransition(t);break}case"toggleTransitionMode":e.toggleTransitionMode();break;default:throw new Error(`Unknown action: ${t}`)}}catch(e){!function(e,t,n){window.postMessage({source:"markup-canvas-error",canvasName:e,action:t,error:n,timestamp:Date.now()},"*")}(r,t,e instanceof Error?e.message:String(e))}}function V(e,t){return{x:(e.clientX+t.clientX)/2,y:(e.clientY+t.clientY)/2}}function A(e,t){const n=e.clientX-t.clientX,r=e.clientY-t.clientY;return Math.sqrt(n*n+r*r)}function _(e,t,n,r){const a=i(n,r,e.transform,t,e.config);return e.updateTransform(a)}function G(e,t,n){e.preventDefault();const r=Array.from(e.touches);d((...e)=>{const r=e[0];if(1===r.length){if(1===n.touches.length){const e=r[0].clientX-n.touches[0].clientX,a=r[0].clientY-n.touches[0].clientY,o={translateX:t.transform.translateX+e,translateY:t.transform.translateY+a};t.updateTransform(o)}}else if(2===r.length){const e=A(r[0],r[1]),a=V(r[0],r[1]);if(n.lastDistance>0){const r=e/n.lastDistance,o=t.container.getBoundingClientRect();let i=a.x-o.left,s=a.y-o.top;const l=function(e,t,n,r){return h(e,t,e=>{const t={...n,x:n.x-e,y:n.y-e};return r(t)})}(t,t.config.rulerSize,{x:i,y:s},e=>e);i=l.x,s=l.y,_(t,r,i,s)}n.lastDistance=e,n.lastCenter=a}n.touches=r})(r)}function N(e){const t={touches:[],lastDistance:0,lastCenter:{}},n=e=>{!function(e,t){e.preventDefault(),t.touches=Array.from(e.touches),2===t.touches.length&&(t.lastDistance=A(t.touches[0],t.touches[1]),t.lastCenter=V(t.touches[0],t.touches[1]))}(e,t)},r=n=>{G(n,e,t)},a=e=>{!function(e,t){t.touches=Array.from(e.touches),t.touches.length<2&&(t.lastDistance=0)}(e,t)};return e.container.addEventListener("touchstart",n,{passive:!1}),e.container.addEventListener("touchmove",r,{passive:!1}),e.container.addEventListener("touchend",a,{passive:!1}),()=>{e.container.removeEventListener("touchstart",n),e.container.removeEventListener("touchmove",r),e.container.removeEventListener("touchend",a)}}function H(e){const t=e.ctrlKey||e.metaKey,n=[0===e.deltaMode,Math.abs(e.deltaY)<50,e.deltaY%1!=0,Math.abs(e.deltaX)>0&&Math.abs(e.deltaY)>0].filter(Boolean).length>=2;return{isTrackpad:n,isMouseWheel:!n,isTrackpadScroll:n&&!t,isTrackpadPinch:n&&t,isZoomGesture:t}}function q(e,t){const n=(e=>d((...t)=>{const n=t[0];if(!n||!e?.updateTransform)return!1;try{const t=e.transform,r=1,a=n.deltaX*r,o=n.deltaY*r,i={scale:t.scale,translateX:t.translateX-a,translateY:t.translateY-o};return b(e.transformLayer,e.config),e.updateTransform(i)}catch(e){return console.error("Error handling trackpad pan:",e),!1}}))(e),r=r=>H(r).isTrackpadScroll?n(r):function(e,t,n){if(!e||"number"!=typeof e.deltaY)return console.warn("Invalid wheel event provided"),!1;if(!t?.updateTransform)return console.warn("Invalid canvas provided to handleWheelEvent"),!1;try{e.preventDefault();const r=t.container.getBoundingClientRect(),a=e.clientX-r.left,o=e.clientY-r.top,{mouseX:i,mouseY:s}=m(t,a,o,n.rulerSize,(e,t)=>({mouseX:e,mouseY:t})),l=n.zoomSpeed,c=H(e);if(!c.isZoomGesture)return!1;let u=n.enableAdaptiveSpeed?I(t,l):l;if(c.isTrackpadPinch){const e=.05*n.zoomSpeed;u=n.enableAdaptiveSpeed?I(t,e):e}return _(t,(e.deltaY<0?1:-1)>0?1+u:1/(1+u),i,s)}catch(e){return console.error("Error handling wheel event:",e),!1}}(r,e,t);return e.container.addEventListener("wheel",r,{passive:!1}),()=>{e.container.removeEventListener("wheel",r)}}const U=100,W=1e3,j=1001,J=4,Q=4,ee=100,te=100,ne=20,re=200;function ae(e,t){const n=function(e){const t=document.createElement("div");return t.className="canvas-ruler horizontal-ruler",t.style.cssText=`\n\tposition: absolute;\n\ttop: 0;\n\tleft: ${e.rulerSize}px;\n\tright: 0;\n\theight: ${e.rulerSize}px;\n\tbackground: var(--ruler-background-color);\n\tborder-bottom: 1px solid var(--ruler-border-color);\n\tz-index: ${W};\n\tpointer-events: none;\n\tfont-family: ${e.rulerFontFamily};\n\tfont-size: ${e.rulerFontSize}px;\n\tcolor: var(--ruler-text-color);\n\toverflow: hidden;\n `,t}(t),r=function(e){const t=document.createElement("div");return t.className="canvas-ruler vertical-ruler",t.style.cssText=`\n\tposition: absolute;\n\ttop: ${e.rulerSize}px;\n\tleft: 0;\n\tbottom: 0;\n\twidth: ${e.rulerSize}px;\n\tbackground: var(--ruler-background-color);\n\tborder-right: 1px solid var(--ruler-border-color);\n\tz-index: ${W};\n\tpointer-events: none;\n\tfont-family: ${e.rulerFontFamily};\n\tfont-size: ${e.rulerFontSize}px;\n\tcolor: var(--ruler-text-color);\n\toverflow: hidden;\n `,t}(t),a=function(e){const t=document.createElement("div");return t.className="canvas-ruler corner-box",t.style.cssText=`\n\t\tposition: absolute;\n\t\ttop: 0;\n\t\tleft: 0;\n\t\twidth: ${e.rulerSize}px;\n\t\theight: ${e.rulerSize}px;\n\t\tbackground: var(--ruler-background-color);\n\t\tborder-right: 1px solid var(--ruler-border-color);\n\t\tborder-bottom: 1px solid var(--ruler-border-color);\n\t\tz-index: ${j};\n\t\tdisplay: flex;\n\t\talign-items: center;\n\t\tjustify-content: center;\n\t\tfont-family: ${e.rulerFontFamily};\n\t\tfont-size: ${e.rulerFontSize-2}px;\n\t\tcolor: var(--ruler-text-color);\n\t\tpointer-events: none;\n\t`,t.textContent=e.rulerUnits,t}(t),o=t.enableGrid?function(e){const t=document.createElement("div");return t.className="canvas-ruler grid-overlay",t.style.cssText=`\n\t\tposition: absolute;\n\t\ttop: ${e.rulerSize}px;\n\t\tleft: ${e.rulerSize}px;\n\t\tright: 0;\n\t\tbottom: 0;\n\t\tpointer-events: none;\n\t\tz-index: ${U};\n\t\tbackground-image: \n\t\t\tlinear-gradient(var(--grid-color) 1px, transparent 1px),\n\t\t\tlinear-gradient(90deg, var(--grid-color) 1px, transparent 1px);\n\t\tbackground-size: 100px 100px;\n\t\topacity: 0.5;\n\t`,t}(t):void 0;return e.appendChild(n),e.appendChild(r),e.appendChild(a),o&&e.appendChild(o),{horizontalRuler:n,verticalRuler:r,cornerBox:a,gridOverlay:o}}function oe(e,t){const n=e/Math.max(5,Math.min(20,t/50)),r=10**Math.floor(Math.log10(n)),a=n/r;let o;return o=a<=1?1:a<=2?2:a<=5?5:10,o*r}function ie(e,t,n,r,a){const o=document.createElement("div");o.className="tick",o.style.cssText=`\n\t\tposition: absolute;\n\t\tleft: ${n}px;\n\t\tbottom: 0;\n\t\twidth: 1px;\n\t\theight: ${J}px;\n\t\tbackground: var(--ruler-tick-color);\n\t`,e.appendChild(o);if(t%ee===0){const r=document.createElement("div");r.style.cssText=`\n\t\t\tposition: absolute;\n\t\t\tleft: ${n}px;\n\t\t\tbottom: ${J+2}px;\n\t\t\tfont-size: ${a.rulerFontSize}px;\n\t\t\tline-height: 1;\n\t\t\tcolor: var(--ruler-text-color);\n\t\t\twhite-space: nowrap;\n\t\t\tpointer-events: none;\n\t\t`,r.textContent=Math.round(t).toString(),e.appendChild(r)}}function se(e,t,n,r,a){const o=document.createElement("div");o.className="tick",o.style.cssText=`\n\t\tposition: absolute;\n\t\ttop: ${n}px;\n\t\tright: 0;\n\t\twidth: ${Q}px;\n\t\theight: 1px;\n\t\tbackground: var(--ruler-tick-color);\n\t`,e.appendChild(o);if(t%ee===0){const r=document.createElement("div");r.style.cssText=`\n\t\t\tposition: absolute;\n\t\t\ttop: ${n-6}px;\n\t\t\tright: ${Q+6}px;\n\t\t\tfont-size: ${a.rulerFontSize}px;\n\t\t\tline-height: 1;\n\t\t\tcolor: var(--ruler-text-color);\n\t\t\twhite-space: nowrap;\n\t\t\tpointer-events: none;\n\t\t\ttransform: rotate(-90deg);\n\t\t\ttransform-origin: right center;\n\t\t`,r.textContent=Math.round(t).toString(),e.appendChild(r)}}function le(e,t,n,r,a){const o=e.getBounds(),i=o.scale||1,s=o.translateX||0,l=o.translateY||0,c=o.width-a.rulerSize,u=o.height-a.rulerSize,d=-s/i,h=-l/i,m=h+u/i;!function(e,t,n,r,a,o){const i=r,s=oe(n-t,i),l=document.createDocumentFragment(),c=Math.floor(t/s)*s,u=Math.ceil(n/s)*s;for(let e=c;e<=u;e+=s){const n=(e-t)*a;n>=-50&&n<=i+50&&ie(l,e,n,0,o)}e.innerHTML="",e.appendChild(l)}(t,d,d+c/i,c,i,a),function(e,t,n,r,a,o){const i=r,s=oe(n-t,i),l=document.createDocumentFragment(),c=Math.floor(t/s)*s,u=Math.ceil(n/s)*s;for(let e=c;e<=u;e+=s){const n=(e-t)*a;n>=-50&&n<=i+50&&se(l,e,n,0,o)}e.innerHTML="",e.appendChild(l)}(n,h,m,u,i,a),r&&function(e,t,n,r){let a=te*t;for(;a<ne;)a*=2;for(;a>re;)a/=2;e.style.backgroundSize=`${a}px ${a}px`,e.style.backgroundPosition=`${n%a}px ${r%a}px`}(r,i,s,l)}function ce(e,t){const n=g(t,"rulerBackgroundColor"),r=g(t,"rulerBorderColor"),a=g(t,"rulerTextColor"),o=g(t,"rulerTickColor"),i=g(t,"gridColor");e.horizontalRuler&&(e.horizontalRuler.style.setProperty("--ruler-background-color",n),e.horizontalRuler.style.setProperty("--ruler-border-color",r),e.horizontalRuler.style.setProperty("--ruler-text-color",a),e.horizontalRuler.style.setProperty("--ruler-tick-color",o)),e.verticalRuler&&(e.verticalRuler.style.setProperty("--ruler-background-color",n),e.verticalRuler.style.setProperty("--ruler-border-color",r),e.verticalRuler.style.setProperty("--ruler-text-color",a),e.verticalRuler.style.setProperty("--ruler-tick-color",o)),e.cornerBox&&(e.cornerBox.style.setProperty("--ruler-background-color",n),e.cornerBox.style.setProperty("--ruler-border-color",r),e.cornerBox.style.setProperty("--ruler-text-color",a)),e.gridOverlay&&e.gridOverlay.style.setProperty("--grid-color",i)}function ue(e,t){if(!e?.container)return console.error("Invalid canvas provided to createRulers"),null;let n,r=null,a=!1;const o=()=>{!a&&n.horizontalRuler&&n.verticalRuler&&le(e,n.horizontalRuler,n.verticalRuler,n.gridOverlay,t)};try{return n=ae(e.container,t),r=function(e,t){const n=d(t),r=e.updateTransform;e.updateTransform=function(e){const t=r.call(this,e);return n(),t};const a=d(t);return window.addEventListener("resize",a),()=>{window.removeEventListener("resize",a),e.updateTransform=r,n.cleanup(),a.cleanup()}}(e,o),ce(n,t),o(),t.showRulers||(n.horizontalRuler.style.display="none",n.verticalRuler.style.display="none",n.cornerBox.style.display="none"),!t.showGrid&&n.gridOverlay&&(n.gridOverlay.style.display="none"),{horizontalRuler:n.horizontalRuler,verticalRuler:n.verticalRuler,cornerBox:n.cornerBox,gridOverlay:n.gridOverlay,update:o,updateTheme:e=>{a||ce(n,e)},show:()=>{n.horizontalRuler&&(n.horizontalRuler.style.display="block"),n.verticalRuler&&(n.verticalRuler.style.display="block"),n.cornerBox&&(n.cornerBox.style.display="flex")},hide:()=>{n.horizontalRuler&&(n.horizontalRuler.style.display="none"),n.verticalRuler&&(n.verticalRuler.style.display="none"),n.cornerBox&&(n.cornerBox.style.display="none"),n.gridOverlay&&(n.gridOverlay.style.display="none")},toggleGrid:()=>{if(n.gridOverlay){const e="none"!==n.gridOverlay.style.display;n.gridOverlay.style.display=e?"none":"block"}},destroy:()=>{a=!0,r&&r(),n.horizontalRuler?.parentNode&&n.horizontalRuler.parentNode.removeChild(n.horizontalRuler),n.verticalRuler?.parentNode&&n.verticalRuler.parentNode.removeChild(n.verticalRuler),n.cornerBox?.parentNode&&n.cornerBox.parentNode.removeChild(n.cornerBox),n.gridOverlay?.parentNode&&n.gridOverlay.parentNode.removeChild(n.gridOverlay)}}}catch(e){return console.error("Failed to create rulers:",e),null}}return class{constructor(e,t={}){if(this.cleanupCallbacks=[],this.rulers=null,this.dragSetup=null,this.keyboardCleanup=null,this.textEditModeEnabled=!1,this.event=new R,this._isReady=!1,!e)throw new Error("Container element is required");this.config=M(t);const n=L(e,this.config);if(!n)throw new Error("Failed to create canvas");if(this.canvas=n,u(this.config,"enableRulers",()=>{this.rulers=ue(this,this.config),this.cleanupCallbacks.push(()=>{this.rulers&&this.rulers.destroy()})}),t.themeMode&&S(this.canvas.container,this.config,this.rulers,t.themeMode),this.event.setEmitInterceptor((e,t)=>{!function(e,t,n){if("undefined"==typeof window)return;let r=t;"ready"===e&&(r={ready:!0});const a=n.name||"markupCanvas";window.postMessage({source:"markup-canvas",action:e,data:r,timestamp:Date.now(),canvasName:a},"*"),window.parent&&window.parent.postMessage({source:"markup-canvas",action:e,data:r,timestamp:Date.now(),canvasName:a},"*")}(e,t,this.config)}),function(e,t){if("undefined"==typeof window)return;const n=t.name||"markupCanvas",r=window,a={config:{get current(){return e.config},get:e.getConfig.bind(e),update:e.updateConfig.bind(e)},transform:{update:e.updateTransform.bind(e),reset:e.reset.bind(e),resetToInitial:e.resetToInitial.bind(e)},zoom:{get current(){return e.transform.scale||1},set:e.setZoom.bind(e),toPoint:e.zoomToPoint.bind(e),in:e.zoomIn.bind(e),out:e.zoomOut.bind(e),reset:e.resetZoom.bind(e),resetToCenter:e.resetViewToCenter.bind(e),fitToScreen:e.fitToScreen.bind(e)},pan:{left:e.panLeft.bind(e),right:e.panRight.bind(e),up:e.panUp.bind(e),down:e.panDown.bind(e),toPoint:e.panToPoint.bind(e),toCenter:e.centerContent.bind(e)},mouseDrag:{enable:e.enableMouseDrag.bind(e),disable:e.disableMouseDrag.bind(e),isEnabled:e.isMouseDragEnabled.bind(e)},keyboard:{enable:e.enableKeyboard.bind(e),disable:e.disableKeyboard.bind(e),isEnabled:e.isKeyboardEnabled.bind(e),enableTextEditMode:e.enableTextEditMode.bind(e),disableTextEditMode:e.disableTextEditMode.bind(e),isTextEditModeEnabled:e.isTextEditModeEnabled.bind(e)},grid:{toggle:e.toggleGrid.bind(e),show:e.showGrid.bind(e),hide:e.hideGrid.bind(e),isVisible:e.isGridVisible.bind(e)},rulers:{toggle:e.toggleRulers.bind(e),show:e.showRulers.bind(e),hide:e.hideRulers.bind(e),isVisible:e.areRulersVisible.bind(e)},canvas:{canvasToContent:e.canvasToContent.bind(e),getVisibleArea:e.getVisibleArea.bind(e),isPointVisible:e.isPointVisible.bind(e),getBounds:e.getBounds.bind(e)},theme:{get current(){return e.config.themeMode},update:e.updateThemeMode.bind(e),toggle:e.toggleThemeMode.bind(e)},transition:{get current(){return e.config.enableTransition},set:e.updateTransition.bind(e),toggle:e.toggleTransitionMode.bind(e)},event:e.event,lifecycle:{cleanup:e.cleanup.bind(e),destroy:e.destroy.bind(e)},state:{get isReady(){return e.isReady},get isTransforming(){return e.isTransforming},get visibleBounds(){return e.visibleBounds},get transform(){return e.transform}}};r[n]=a,r.__markupCanvasInstances||(r.__markupCanvasInstances=new Map),r.__markupCanvasInstances.set(n,a)}(this,this.config),this.config.enablePostMessageAPI){const e=function(e){const t=t=>{const n=t.data;if(!["markup-canvas","application"].includes(n.source))return;const r=e.config.name||"markupCanvas";if(n.canvasName!==r)return;const a=n.action,o=n.data;O(e,a,o,r)};return"undefined"!=typeof window&&window.addEventListener("message",t),()=>{"undefined"!=typeof window&&window.removeEventListener("message",t)}}(this);this.cleanupCallbacks.push(e)}this.setupEventHandlers(),this._isReady=!0,this.event.emit("ready",this)}setupEventHandlers(){try{u(this.config,"enableZoom",()=>{const e=q(this,this.config);this.cleanupCallbacks.push(e)}),(this.config.enablePan||this.config.enableClickToZoom)&&(this.dragSetup=K(this,this.config,!0),this.cleanupCallbacks.push(this.dragSetup.cleanup)),u(this.config,"enableKeyboard",()=>{const e=$(this,this.config,{textEditModeEnabled:this.textEditModeEnabled});this.keyboardCleanup=e,this.cleanupCallbacks.push(e)}),u(this.config,"enableTouch",()=>{const e=N(this);this.cleanupCallbacks.push(e)})}catch(e){throw console.error("Failed to set up event handlers:",e),this.cleanup(),e}}get container(){return this.canvas.container}get transformLayer(){return this.canvas.transformLayer}get contentLayer(){return this.canvas.contentLayer}get transform(){return this.canvas.transform}get isReady(){return this._isReady}get isTransforming(){return this.dragSetup?.isEnabled()||!1}get visibleBounds(){return s(this)}getBounds(){return f(this.canvas,this.config)}updateTransform(e){const t=C(this.canvas,e);return t&&P(this.event,this.canvas),t}reset(){const e=C(this.canvas,{scale:1,translateX:0,translateY:0});return e&&P(this.event,this.canvas),e}resetToInitial(){const e=(t=this.canvas,n=this.transformLayer,r=this.config,y(n,r,()=>h(t,r.rulerSize,e=>{const n=r.initialZoom??1,a=r.initialPan??{x:0,y:0},o=-1*e,i={scale:n,translateX:a.x+o,translateY:a.y+o};return C(t,i)})));var t,n,r;return e&&P(this.event,this.canvas),e}setZoom(e){return function(e,t,n,r,a){return y(t,n,()=>l(n,t=>{const n=t(a),o=D(e);return r(o.x,o.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this),e)}canvasToContent(t,r){return e(t,r,n(this.canvas.transform.scale,this.canvas.transform.translateX,this.canvas.transform.translateY))}zoomToPoint(e,t,n){const r=function(e,t,n,r,a,o){return y(t,n,()=>{const t=i(r,a,e.transform,o/e.transform.scale,n);return C(e,t)})}(this.canvas,this.transformLayer,this.config,e,t,n);return r&&P(this.event,this.canvas),r}resetView(){return e=this.canvas,t=this.transformLayer,n=this.config,y(t,n,()=>h(e,n.rulerSize,t=>C(e,{scale:1,translateX:-1*t,translateY:-1*t})));var e,t,n}resetViewToCenter(){return function(e,t,n,r){return y(t,n,()=>l(n,t=>{const n=t(1),a=D(e);return r(a.x,a.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this))}panLeft(e){return k(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&k(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}panRight(e){return w(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&w(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}panUp(e){return x(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&x(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}panDown(e){return v(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&v(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}zoomIn(e=.5){return function(e,t,n,r,a=.5){return y(t,n,()=>l(n,t=>{const n=t(e.transform.scale*(1+a)),o=D(e);return r(o.x,o.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this),e)}zoomOut(e=.5){return function(e,t,n,r,a=.5){return y(t,n,()=>l(n,t=>{const n=t(e.transform.scale*(1-a)),o=D(e);return r(o.x,o.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this),e)}resetZoom(){return this.resetViewToCenter()}enableMouseDrag(){return this.dragSetup?.enable()??!1}disableMouseDrag(){return this.dragSetup?.disable()??!1}isMouseDragEnabled(){return this.dragSetup?.isEnabled()??!1}enableKeyboard(){return this.keyboardCleanup||(this.keyboardCleanup=$(this,this.config,{textEditModeEnabled:this.textEditModeEnabled}),this.cleanupCallbacks.push(this.keyboardCleanup)),!0}disableKeyboard(){return!!this.keyboardCleanup&&(this.keyboardCleanup(),this.keyboardCleanup=null,!0)}isKeyboardEnabled(){return null!==this.keyboardCleanup}enableTextEditMode(){if(this.textEditModeEnabled)return!0;if(this.textEditModeEnabled=!0,this.keyboardCleanup){const e=this.cleanupCallbacks.indexOf(this.keyboardCleanup);e>-1&&this.cleanupCallbacks.splice(e,1),this.keyboardCleanup(),this.keyboardCleanup=$(this,this.config,{textEditModeEnabled:!0}),this.cleanupCallbacks.push(this.keyboardCleanup)}return!0}disableTextEditMode(){if(!this.textEditModeEnabled)return!0;if(this.textEditModeEnabled=!1,this.keyboardCleanup){const e=this.cleanupCallbacks.indexOf(this.keyboardCleanup);e>-1&&this.cleanupCallbacks.splice(e,1),this.keyboardCleanup(),this.keyboardCleanup=$(this,this.config,{textEditModeEnabled:!1}),this.cleanupCallbacks.push(this.keyboardCleanup)}return!0}isTextEditModeEnabled(){return this.textEditModeEnabled}toggleGrid(){const e=(t=this.rulers,!!t?.toggleGrid&&(t.toggleGrid(),!0));var t;return e&&this.event.emit("gridVisibility",this.isGridVisible()),e}showGrid(){const e=(t=this.rulers,!!t?.gridOverlay&&(t.gridOverlay.style.display="block",!0));var t;return e&&this.event.emit("gridVisibility",!0),e}hideGrid(){const e=(t=this.rulers,!!t?.gridOverlay&&(t.gridOverlay.style.display="none",!0));var t;return e&&this.event.emit("gridVisibility",!1),e}isGridVisible(){return e=this.rulers,!!e?.gridOverlay&&"none"!==e.gridOverlay.style.display;var e}toggleRulers(){const e=function(e,t){if(e)return t()?e.hide():e.show(),!0;return!1}(this.rulers,()=>this.areRulersVisible());return e&&this.event.emit("rulersVisibility",this.areRulersVisible()),e}showRulers(){const e=!!(t=this.rulers)&&(t.show(),!0);var t;return e&&this.event.emit("rulersVisibility",!0),e}hideRulers(){const e=!!(t=this.rulers)&&(t.hide(),!0);var t;return e&&this.event.emit("rulersVisibility",!1),e}areRulersVisible(){return e=this.rulers,!!e?.horizontalRuler&&"none"!==e.horizontalRuler.style.display;var e}centerContent(){return e=this.canvas,t=this.config,n=this.updateTransform.bind(this),y(this.transformLayer,t,()=>{const r=f(e,t),a=(r.width-r.contentWidth*e.transform.scale)/2,o=(r.height-r.contentHeight*e.transform.scale)/2;return n({translateX:a,translateY:o})});var e,t,n}fitToScreen(){return e=this.canvas,t=this.transformLayer,n=this.config,y(t,n,()=>{const t=f(e,n),r=t.width/n.width,a=t.height/n.height,o=l(n,e=>e(.9*Math.min(r,a))),i=n.width*o,s=n.height*o,c=(t.width-i)/2,u=(t.height-s)/2;return C(e,{scale:o,translateX:c,translateY:u})});var e,t,n}getVisibleArea(){return s(this)}isPointVisible(e,t){return function(e,t,n){const r=s(e);return t>=r.x&&t<=r.x+r.width&&n>=r.y&&n<=r.y+r.height}(this,e,t)}panToPoint(e,t){return function(e,t,n,r,a,o){return y(o,t,()=>{const o=f(e,t),i=o.width/2,s=o.height/2,l=i-n*e.transform.scale,c=s-r*e.transform.scale;return a({translateX:l,translateY:c})})}(this.canvas,this.config,e,t,this.updateTransform.bind(this),this.transformLayer)}getConfig(){return{...this.config}}updateConfig(e){this.config=M({...this.config,...e})}updateThemeMode(e){this.config=M({...this.config,themeMode:e}),S(this.canvas.container,this.config,this.rulers,e)}toggleThemeMode(){const e="light"===this.config.themeMode?"dark":"light";return this.updateThemeMode(e),e}updateTransition(e){this.config=M({...this.config,enableTransition:e})}toggleTransitionMode(){const e=function(e){return!e}(this.config.enableTransition);return this.updateTransition(e),e}cleanup(){!function(e){if("undefined"==typeof window)return;const t=e.name||"markupCanvas",n=window;delete n[t],n.__markupCanvasInstances&&n.__markupCanvasInstances.delete(t)}(this.config),this.cleanupCallbacks.forEach(e=>{try{e()}catch(e){console.warn("Error during cleanup:",e)}}),this.cleanupCallbacks=[],this.removeAllListeners()}on(e,t){this.event.on(e,t)}off(e,t){this.event.off(e,t)}emit(e,t){this.event.emit(e,t)}removeAllListeners(){this.event.removeAllListeners()}destroy(){this.cleanup(),window.__markupCanvasTransitionTimeout&&clearTimeout(window.__markupCanvasTransitionTimeout)}}});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).MarkupCanvas=t()}(this,function(){"use strict";function e(e,t,n){if(!n?.inverse)return{x:e,y:t};try{const r=n.inverse(),a=new DOMPoint(e,t).matrixTransform(r);return{x:a.x,y:a.y}}catch(n){return console.warn("Canvas to content conversion failed:",n),{x:e,y:t}}}function t(e,t){return Math.max(t.minZoom,Math.min(t.maxZoom,e))}function n(e,t,n){return new DOMMatrix([e,0,0,e,t,n])}const r="canvas-container",a="transform-layer",o="content-layer";function i(e,n,r,a,o){const i=o.enableRulers?-o.rulerSize:0,s=r||{scale:1,translateX:i,translateY:i},{scale:l,translateX:c,translateY:u}=s,d=t(l*a,o);if(Math.abs(d-l)<.001)return{scale:l,translateX:c,translateY:u};return{scale:d,translateX:e-(e-c)/l*d,translateY:n-(n-u)/l*d}}function s(e){return e.getBounds().visibleArea}function l(e,n){return n(n=>t(n,e))}const c=new Map;function u(e,t,n){return e[t]?n():null}function d(e){let t=null,n=null;const r=(...r)=>{n=r,null===t&&(t=requestAnimationFrame(()=>{n&&e(...n),t=null,n=null}))};return r.cleanup=()=>{null!==t&&(cancelAnimationFrame(t),t=null,n=null)},r}function h(e,t,n){return n(null!==e.container.querySelector(".canvas-ruler")?t:0)}function g(e,t,n,r,a){const o=null!==e.container.querySelector(".canvas-ruler");return a(o?t-r:t,o?n-r:n)}const m={width:8e3,height:8e3,enableAcceleration:!0,initialZoom:1,initialPan:{x:0,y:0},name:"markupCanvas",enablePostMessageAPI:!1,enableZoom:!0,enablePan:!0,enableTouch:!0,enableKeyboard:!0,bindKeyboardEventsTo:"document",sendKeyboardEventsToParent:!1,zoomSpeed:1.5,minZoom:.05,maxZoom:80,enableTransition:!0,transitionDuration:.2,enableAdaptiveSpeed:!0,enableLeftDrag:!0,enableMiddleDrag:!0,requireSpaceForMouseDrag:!1,keyboardPanStep:50,keyboardFastMultiplier:20,keyboardZoomStep:.2,enableClickToZoom:!0,clickZoomLevel:1,requireOptionForClickZoom:!1,enableRulers:!0,enableGrid:!1,showRulers:!0,showGrid:!1,rulerFontSize:9,rulerFontFamily:"Monaco, Menlo, monospace",rulerUnits:"px",rulerSize:20,canvasBackgroundColor:"rgba(250, 250, 250, 1)",canvasBackgroundColorDark:"rgba(40, 40, 40, 1)",rulerBackgroundColor:"rgba(255, 255, 255, 0.95)",rulerBorderColor:"rgba(240, 240, 240, 1)",rulerTextColor:"rgba(102, 102, 102, 1)",rulerTickColor:"rgba(204, 204, 204, 1)",gridColor:"rgba(232, 86, 193, 0.5)",rulerBackgroundColorDark:"rgba(30, 30, 30, 0.95)",rulerBorderColorDark:"rgba(68, 68, 68, 1)",rulerTextColorDark:"rgba(170, 170, 170, 1)",rulerTickColorDark:"rgba(104, 104, 104, 1)",gridColorDark:"rgba(232, 86, 193, 0.5)",themeMode:"light"};function p(t,r){try{const a=t.container,o=t.transform||{scale:1,translateX:0,translateY:0},i=a.getBoundingClientRect(),s=i.width||a.clientWidth||0,l=i.height||a.clientHeight||0,c=h({container:a},r.rulerSize,e=>Math.max(0,s-e)),u=h({container:a},r.rulerSize,e=>Math.max(0,l-e)),d=r.width||m.width,g=r.height||m.height,p=function(t,r,a,o,i){const s=e(0,0,n(i.scale,i.translateX,i.translateY)),l=e(t,r,n(i.scale,i.translateX,i.translateY));return{x:Math.max(0,Math.min(a,s.x)),y:Math.max(0,Math.min(o,s.y)),width:Math.max(0,Math.min(a-s.x,l.x-s.x)),height:Math.max(0,Math.min(o-s.y,l.y-s.y))}}(c,u,d,g,o);return{width:c,height:u,contentWidth:d,contentHeight:g,scale:o.scale,translateX:o.translateX,translateY:o.translateY,visibleArea:p,scaledContentWidth:d*o.scale,scaledContentHeight:g*o.scale,canPanLeft:o.translateX<0,canPanRight:o.translateX+d*o.scale>c,canPanUp:o.translateY<0,canPanDown:o.translateY+g*o.scale>u,canZoomIn:o.scale<3.5,canZoomOut:o.scale>.1}}catch(e){return console.error("Failed to calculate canvas bounds:",e),{width:0,height:0,contentWidth:0,contentHeight:0,scale:1,translateX:0,translateY:0,visibleArea:{x:0,y:0,width:0,height:0},scaledContentWidth:0,scaledContentHeight:0,canPanLeft:!1,canPanRight:!1,canPanUp:!1,canPanDown:!1,canZoomIn:!1,canZoomOut:!1}}}function f(e,t){try{if(t.enableTransition){window.__markupCanvasTransitionTimeout&&(clearTimeout(window.__markupCanvasTransitionTimeout),window.__markupCanvasTransitionTimeout=void 0);return function(e,t,n){const r=c.get(e);r&&clearTimeout(r);const a=window.setTimeout(()=>{n(),c.delete(e)},t);c.set(e,a)}("disableTransition",1e3*(t.transitionDuration??.2),()=>{e.style.transition="none",window.__markupCanvasTransitionTimeout=void 0}),!0}return!1}catch(e){return console.error("Failed to disable transitions:",e),!0}}function b(e,t,n){!function(e,t){try{return!!t.enableTransition&&(window.__markupCanvasTransitionTimeout&&(clearTimeout(window.__markupCanvasTransitionTimeout),window.__markupCanvasTransitionTimeout=void 0),e.style.transition=`transform ${t.transitionDuration}s linear`,!0)}catch(e){return console.error("Failed to enable transitions:",e),!1}}(e,t);try{return n()}finally{f(e,t)}}function y(e,t,n){const r=t.keyboardPanStep;return n({translateY:e.transform.translateY-r})}function v(e,t,n){const r=t.keyboardPanStep;return n({translateX:e.transform.translateX+r})}function k(e,t,n){const r=t.keyboardPanStep;return n({translateX:e.transform.translateX-r})}function w(e,t,n){const r=t.keyboardPanStep;return n({translateY:e.transform.translateY+r})}function x(e,t){if(!e?.style||!t)return!1;try{return e.style.transform=function(e){return`matrix3d(${e.m11}, ${e.m12}, ${e.m13}, ${e.m14}, ${e.m21}, ${e.m22}, ${e.m23}, ${e.m24}, ${e.m31}, ${e.m32}, ${e.m33}, ${e.m34}, ${e.m41}, ${e.m42}, ${e.m43}, ${e.m44})`}(t),!0}catch(e){return console.warn("Transform application failed:",e),!1}}function T(e,t){e.transform={...e.transform,...t};const r=n(e.transform.scale,e.transform.translateX,e.transform.translateY);return x(e.transformLayer,r)}function C(e={}){const t={...m,...e};return("number"!=typeof t.width||t.width<=0)&&(console.warn("Invalid width, using default"),t.width=m.width),("number"!=typeof t.height||t.height<=0)&&(console.warn("Invalid height, using default"),t.height=m.height),("number"!=typeof t.zoomSpeed||t.zoomSpeed<=0)&&(console.warn("Invalid zoomSpeed, using default"),t.zoomSpeed=m.zoomSpeed),("number"!=typeof t.minZoom||t.minZoom<=0)&&(console.warn("Invalid minZoom, using default"),t.minZoom=m.minZoom),("number"!=typeof t.maxZoom||t.maxZoom<=t.minZoom)&&(console.warn("Invalid maxZoom, using default"),t.maxZoom=m.maxZoom),("number"!=typeof t.keyboardPanStep||t.keyboardPanStep<=0)&&(console.warn("Invalid keyboardPanStep, using default"),t.keyboardPanStep=m.keyboardPanStep),("number"!=typeof t.keyboardFastMultiplier||t.keyboardFastMultiplier<=0)&&(console.warn("Invalid keyboardFastMultiplier, using default"),t.keyboardFastMultiplier=m.keyboardFastMultiplier),("number"!=typeof t.clickZoomLevel||t.clickZoomLevel<=0)&&(console.warn("Invalid clickZoomLevel, using default"),t.clickZoomLevel=m.clickZoomLevel),("number"!=typeof t.rulerFontSize||t.rulerFontSize<=0)&&(console.warn("Invalid rulerFontSize, using default"),t.rulerFontSize=m.rulerFontSize),("number"!=typeof t.rulerSize||t.rulerSize<=0)&&(console.warn("Invalid rulerSize, using default"),t.rulerSize=m.rulerSize),t}function M(e){try{const t=e.getBounds();return{x:t.width/2,y:t.height/2}}catch(e){return console.warn("Failed to calculate viewport center:",e),{x:0,y:0}}}function S(e,t){const n=Array.from(e.children);let r=e.querySelector(`.${a}`);r||(r=document.createElement("div"),r.className=a,e.appendChild(r)),function(e,t){e.style.position="absolute";const n=t.rulerSize;e.style.top=`${n}px`,e.style.left=`${n}px`,e.style.width=`${t.width}px`,e.style.height=`${t.height}px`,e.style.transformOrigin="0 0"}(r,t);let i=r.querySelector(`.${o}`);return i||(i=document.createElement("div"),i.className=o,r.appendChild(i),function(e,t,n){e.forEach(e=>{e===n||e.classList.contains(a)||t.appendChild(e)})}(n,i,r)),function(e){e.style.position="relative",e.style.width="100%",e.style.height="100%",e.style.pointerEvents="auto"}(i),{transformLayer:r,contentLayer:i}}function D(e,t){if("static"===getComputedStyle(e).position&&(e.style.position="relative"),e.style.overflow="hidden",e.style.cursor="grab",e.style.overscrollBehavior="none",t){const n=`light-dark(${t.canvasBackgroundColor}, ${t.canvasBackgroundColorDark})`;e.style.backgroundColor=n}e.hasAttribute("tabindex")||e.setAttribute("tabindex","0"),function(e){const t=e.getBoundingClientRect(),n=getComputedStyle(e);0===t.height&&"auto"===n.height&&console.error("MarkupCanvas: Container height is 0. Please set a height on your container element using CSS.","Examples: height: 100vh, height: 500px, or use flexbox/grid layout.",e),0===t.width&&"auto"===n.width&&console.error("MarkupCanvas: Container width is 0. Please set a width on your container element using CSS.","Examples: width: 100vw, width: 800px, or use flexbox/grid layout.",e)}(e),e.classList.contains(r)||e.classList.add(r)}function E(e,t){if(!e?.appendChild)return console.error("Invalid container element provided to createCanvas"),null;try{D(e,t);const{transformLayer:r,contentLayer:a}=S(e,t);u(t,"enableAcceleration",()=>{!function(e){try{return e.style.transform=e.style.transform||"translateZ(0)",e.style.backfaceVisibility="hidden",!0}catch(e){return console.error("Failed to enable hardware acceleration:",e),!1}}(r)});const o=t.enableRulers?-t.rulerSize:0,i={scale:t.initialZoom??1,translateX:(t.initialPan?.x??0)+o,translateY:(t.initialPan?.y??0)+o},s=n(i.scale,i.translateX,i.translateY);x(r,s);return{container:e,transformLayer:r,contentLayer:a,transform:i}}catch(e){return console.error("Failed to create canvas:",e),null}}class z{constructor(){this.listeners=new Map}setEmitInterceptor(e){this.emitInterceptor=e}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t)}off(e,t){const n=this.listeners.get(e);n&&n.delete(t)}emit(e,t){this.emitInterceptor?.(e,t);const n=this.listeners.get(e);n&&n.forEach(n=>{try{n(t)}catch(t){console.error(`Error in event handler for "${String(e)}":`,t)}})}removeAllListeners(){this.listeners.clear()}}function L(e,t){const n=t.transform;e.emit("transform",n),e.emit("zoom",n.scale),e.emit("pan",{x:n.translateX,y:n.translateY})}const R=300,P=5;function $(e,t){if(!e?.getBounds)return t;try{const n=e.getBounds(),r=n.width*n.height;return t*(r/2073600)**1}catch(e){return console.warn("Failed to calculate adaptive zoom speed, using base speed:",e),t}}function Y(e,t,n){const r=n?.textEditModeEnabled??!1;function a(n){if(!(n instanceof KeyboardEvent))return;if("canvas"===t.bindKeyboardEventsTo&&document.activeElement!==e.container)return;if(u(t,"sendKeyboardEventsToParent",()=>{r&&!function(e){return!("0"!==e.key||!e.ctrlKey&&!e.metaKey)||!("+"!==e.key||!e.ctrlKey&&!e.metaKey)||!("-"!==e.key||!e.ctrlKey&&!e.metaKey)}(n)||(!function(e,t){if("undefined"==typeof window||!window.parent)return;const n=t.name||"markupCanvas";window.parent.postMessage({source:"markup-canvas",action:"keyboardShortcut",data:{key:e.key,ctrlKey:e.ctrlKey,metaKey:e.metaKey,shiftKey:e.shiftKey,altKey:e.altKey,code:e.code},timestamp:Date.now(),canvasName:n},"*")}(n,t),n.preventDefault())}),t.sendKeyboardEventsToParent)return;let a=!1;const o={};switch(n.key){case"ArrowLeft":if(r)return;o.translateX=e.transform.translateX+t.keyboardPanStep,a=!0;break;case"ArrowRight":if(r)return;o.translateX=e.transform.translateX-t.keyboardPanStep,a=!0;break;case"ArrowUp":if(r)return;o.translateY=e.transform.translateY+t.keyboardPanStep,a=!0;break;case"ArrowDown":if(r)return;o.translateY=e.transform.translateY-t.keyboardPanStep,a=!0;break;case"=":case"+":if(r)return;{const n=t.enableAdaptiveSpeed?$(e,t.keyboardZoomStep):t.keyboardZoomStep;e.zoomIn(n),a=!0}break;case"-":if(r)return;{const n=t.enableAdaptiveSpeed?$(e,t.keyboardZoomStep):t.keyboardZoomStep;e.zoomOut(n),a=!0}break;case"0":n.ctrlKey?(e.resetView&&e.resetView(),a=!0):n.metaKey&&(e.resetViewToCenter&&e.resetViewToCenter(),a=!0);break;case"g":case"G":!n.shiftKey||n.ctrlKey||n.metaKey||n.altKey||!e.toggleGrid||(e.toggleGrid(),a=!0);break;case"r":case"R":!n.shiftKey||n.metaKey||n.ctrlKey||n.altKey||!e.toggleRulers||(e.toggleRulers(),a=!0)}a&&(n.preventDefault(),Object.keys(o).length>0&&e.updateTransform(o))}const o="canvas"===t.bindKeyboardEventsTo?e.container:document;return o.addEventListener("keydown",a),()=>{o.removeEventListener("keydown",a)}}function X(e,t,n,r,a){n?t.requireSpaceForMouseDrag?e.container.style.cursor=r?"grab":"default":e.container.style.cursor=a?"grabbing":"grab":e.container.style.cursor="default"}function I(e,t,n,r,a){a.setIsDragging(!1),a.setDragButton(-1),X(e,t,n,r,!1)}function B(e,t,n,r,a,o,i,s,l,c){o&&e.button===i&&I(t,n,r,a,{setIsDragging:c.setIsDragging,setDragButton:c.setDragButton}),r&&0===e.button&&n.enableClickToZoom&&s>0&&function(e,t,n,r,a,o){const i=Date.now()-r,s=e.altKey,l=!n.requireOptionForClickZoom||s;if(i<R&&!a&&!o&&l){e.preventDefault();const r=t.container.getBoundingClientRect(),a=e.clientX-r.left,o=e.clientY-r.top,{clickX:i,clickY:s}=g(t,a,o,n.rulerSize,(e,t)=>({clickX:e,clickY:t})),l=t.canvasToContent(i,s),c=r.width/2,u=r.height/2,d=n.clickZoomLevel,h={scale:d,translateX:c-l.x*d,translateY:u-l.y*d};b(t.transformLayer,t.config,()=>{t.updateTransform(h)})}}(e,t,n,s,l,o),0===e.button&&function(e){e.setMouseDownTime(0),e.setHasDragged(!1)}({setMouseDownTime:c.setMouseDownTime,setHasDragged:c.setHasDragged})}function Z(e,t,n=!0){let r=!0,a=!1,o=0,i=0,s=-1,l=!1,c=0,u=0,h=0,g=!1;const m={setIsDragging:e=>{a=e},setDragButton:e=>{s=e},setIsSpacePressed:e=>{l=e},setMouseDownTime:e=>{c=e},setMouseDownX:e=>{u=e},setMouseDownY:e=>{h=e},setHasDragged:e=>{g=e},setLastMouseX:e=>{o=e},setLastMouseY:e=>{i=e}},p=n=>{!function(e,t,n,r,a,o){n.requireSpaceForMouseDrag&&" "===e.key&&(o.setIsSpacePressed(!0),X(t,n,r,!0,a))}(n,e,t,r,a,{setIsSpacePressed:m.setIsSpacePressed})},f=n=>{!function(e,t,n,r,a,o){n.requireSpaceForMouseDrag&&" "===e.key&&(o.setIsSpacePressed(!1),X(t,n,r,!1,a),a&&I(t,n,r,!1,{setIsDragging:o.setIsDragging,setDragButton:o.setDragButton}))}(n,e,t,r,a,{setIsSpacePressed:m.setIsSpacePressed,setIsDragging:m.setIsDragging,setDragButton:m.setDragButton})},b=n=>{!function(e,t,n,r,a,o){const i=0===e.button,s=1===e.button;if(i&&(o.setMouseDownTime(Date.now()),o.setMouseDownX(e.clientX),o.setMouseDownY(e.clientY),o.setHasDragged(!1)),!r)return;(!n.requireSpaceForMouseDrag||a)&&(i&&n.enableLeftDrag||s&&n.enableMiddleDrag)&&(e.preventDefault(),o.setDragButton(e.button),o.setLastMouseX(e.clientX),o.setLastMouseY(e.clientY),X(t,n,r,a,!1))}(n,e,t,r,l,m)},y=n=>{!function(e,t,n,r,a,o,i,s,l,c,u,h){if(i>0){const t=Math.abs(e.clientX-s),i=Math.abs(e.clientY-l);if(t>P||i>P){h.setHasDragged(!0);const e=!n.requireSpaceForMouseDrag||o;!a&&r&&e&&h.setIsDragging(!0)}}if(!a||!r)return;e.preventDefault(),d((...e)=>{const n=e[0];if(!a||!r)return;const o=n.clientX-c,i=n.clientY-u,s={translateX:t.transform.translateX+o,translateY:t.transform.translateY+i};t.updateTransform(s),h.setLastMouseX(n.clientX),h.setLastMouseY(n.clientY)})(e)}(n,e,t,r,a,l,c,u,h,o,i,{setHasDragged:m.setHasDragged,setIsDragging:m.setIsDragging,setLastMouseX:m.setLastMouseX,setLastMouseY:m.setLastMouseY})},v=n=>{B(n,e,t,r,l,a,s,c,g,{setIsDragging:m.setIsDragging,setDragButton:m.setDragButton,setMouseDownTime:m.setMouseDownTime,setHasDragged:m.setHasDragged})},k=()=>{!function(e,t,n,r,a,o){a&&I(e,t,n,r,o)}(e,t,r,l,a,{setIsDragging:m.setIsDragging,setDragButton:m.setDragButton})};e.container.addEventListener("mousedown",b),document.addEventListener("mousemove",y),document.addEventListener("mouseup",v),e.container.addEventListener("mouseleave",k),t.requireSpaceForMouseDrag&&(document.addEventListener("keydown",p),document.addEventListener("keyup",f)),X(e,t,r,l,a);const w=()=>{e.container.removeEventListener("mousedown",b),document.removeEventListener("mousemove",y),document.removeEventListener("mouseup",v),e.container.removeEventListener("mouseleave",k),t.requireSpaceForMouseDrag&&(document.removeEventListener("keydown",p),document.removeEventListener("keyup",f))};return n?{cleanup:w,enable:()=>(r=!0,X(e,t,r,l,a),!0),disable:()=>(r=!1,a&&I(e,t,r,l,{setIsDragging:m.setIsDragging,setDragButton:m.setDragButton}),X(e,t,r,l,a),!0),isEnabled:()=>r}:w}function F(e,t,n,r){try{switch(t){case"zoomIn":e.zoomIn(n);break;case"zoomOut":e.zoomOut(n);break;case"setZoom":{const t=n;if("number"!=typeof t||t<=0)throw new Error(`Invalid zoom level: ${t}. Must be a positive number.`);e.setZoom(t);break}case"resetZoom":e.resetZoom();break;case"panLeft":e.panLeft(n);break;case"panRight":e.panRight(n);break;case"panUp":e.panUp(n);break;case"panDown":e.panDown(n);break;case"fitToScreen":e.fitToScreen();break;case"centerContent":e.centerContent();break;case"panToPoint":{const t=n;e.panToPoint(t.x,t.y);break}case"resetView":e.resetView();break;case"resetViewToCenter":e.resetViewToCenter();break;case"resetToInitial":e.resetToInitial();break;case"toggleRulers":e.toggleRulers();break;case"showRulers":e.showRulers();break;case"hideRulers":e.hideRulers();break;case"toggleGrid":e.toggleGrid();break;case"showGrid":e.showGrid();break;case"hideGrid":e.hideGrid();break;case"updateThemeMode":{const t=n;if("light"!==t&&"dark"!==t)throw new Error(`Invalid theme mode: ${t}`);e.updateThemeMode(t);break}case"toggleThemeMode":{const t="light"===e.getConfig().themeMode?"dark":"light";e.updateThemeMode(t);break}case"updateTransition":{const t=n;if("boolean"!=typeof t)throw new Error(`Invalid transition enabled value: ${t}. Must be a boolean.`);e.updateTransition(t);break}case"toggleTransitionMode":e.toggleTransitionMode();break;default:throw new Error(`Unknown action: ${t}`)}}catch(e){!function(e,t,n){window.postMessage({source:"markup-canvas-error",canvasName:e,action:t,error:n,timestamp:Date.now()},"*")}(r,t,e instanceof Error?e.message:String(e))}}function K(e,t){return{x:(e.clientX+t.clientX)/2,y:(e.clientY+t.clientY)/2}}function O(e,t){const n=e.clientX-t.clientX,r=e.clientY-t.clientY;return Math.sqrt(n*n+r*r)}function V(e,t,n,r){const a=i(n,r,e.transform,t,e.config);return e.updateTransform(a)}function A(e,t,n){e.preventDefault();const r=Array.from(e.touches);d((...e)=>{const r=e[0];if(1===r.length){if(1===n.touches.length){const e=r[0].clientX-n.touches[0].clientX,a=r[0].clientY-n.touches[0].clientY,o={translateX:t.transform.translateX+e,translateY:t.transform.translateY+a};t.updateTransform(o)}}else if(2===r.length){const e=O(r[0],r[1]),a=K(r[0],r[1]);if(n.lastDistance>0){const r=e/n.lastDistance,o=t.container.getBoundingClientRect();let i=a.x-o.left,s=a.y-o.top;const l=function(e,t,n,r){return h(e,t,e=>{const t={...n,x:n.x-e,y:n.y-e};return r(t)})}(t,t.config.rulerSize,{x:i,y:s},e=>e);i=l.x,s=l.y,V(t,r,i,s)}n.lastDistance=e,n.lastCenter=a}n.touches=r})(r)}function _(e){const t={touches:[],lastDistance:0,lastCenter:{}},n=e=>{!function(e,t){e.preventDefault(),t.touches=Array.from(e.touches),2===t.touches.length&&(t.lastDistance=O(t.touches[0],t.touches[1]),t.lastCenter=K(t.touches[0],t.touches[1]))}(e,t)},r=n=>{A(n,e,t)},a=e=>{!function(e,t){t.touches=Array.from(e.touches),t.touches.length<2&&(t.lastDistance=0)}(e,t)};return e.container.addEventListener("touchstart",n,{passive:!1}),e.container.addEventListener("touchmove",r,{passive:!1}),e.container.addEventListener("touchend",a,{passive:!1}),()=>{e.container.removeEventListener("touchstart",n),e.container.removeEventListener("touchmove",r),e.container.removeEventListener("touchend",a)}}function G(e){const t=e.ctrlKey||e.metaKey,n=[0===e.deltaMode,Math.abs(e.deltaY)<50,e.deltaY%1!=0,Math.abs(e.deltaX)>0&&Math.abs(e.deltaY)>0].filter(Boolean).length>=2;return{isTrackpad:n,isMouseWheel:!n,isTrackpadScroll:n&&!t,isTrackpadPinch:n&&t,isZoomGesture:t}}function N(e,t){const n=(e=>d((...t)=>{const n=t[0];if(!n||!e?.updateTransform)return!1;try{const t=e.transform,r=1,a=n.deltaX*r,o=n.deltaY*r,i={scale:t.scale,translateX:t.translateX-a,translateY:t.translateY-o};return f(e.transformLayer,e.config),e.updateTransform(i)}catch(e){return console.error("Error handling trackpad pan:",e),!1}}))(e),r=r=>G(r).isTrackpadScroll?n(r):function(e,t,n){if(!e||"number"!=typeof e.deltaY)return console.warn("Invalid wheel event provided"),!1;if(!t?.updateTransform)return console.warn("Invalid canvas provided to handleWheelEvent"),!1;try{e.preventDefault();const r=t.container.getBoundingClientRect(),a=e.clientX-r.left,o=e.clientY-r.top,{mouseX:i,mouseY:s}=g(t,a,o,n.rulerSize,(e,t)=>({mouseX:e,mouseY:t})),l=n.zoomSpeed,c=G(e);if(!c.isZoomGesture)return!1;let u=n.enableAdaptiveSpeed?$(t,l):l;if(c.isTrackpadPinch){const e=.05*n.zoomSpeed;u=n.enableAdaptiveSpeed?$(t,e):e}return V(t,(e.deltaY<0?1:-1)>0?1+u:1/(1+u),i,s)}catch(e){return console.error("Error handling wheel event:",e),!1}}(r,e,t);return e.container.addEventListener("wheel",r,{passive:!1}),()=>{e.container.removeEventListener("wheel",r)}}const H=100,q=1e3,U=1001,W=4,j=4,J=100,Q=100,ee=20,te=200;function ne(e,t){const n=function(e){const t=document.createElement("div");return t.className="canvas-ruler horizontal-ruler",t.style.cssText=`\n\tposition: absolute;\n\ttop: 0;\n\tleft: ${e.rulerSize}px;\n\tright: 0;\n\theight: ${e.rulerSize}px;\n\tbackground: var(--ruler-background-color);\n\tborder-bottom: 1px solid var(--ruler-border-color);\n\tz-index: ${q};\n\tpointer-events: none;\n\tfont-family: ${e.rulerFontFamily};\n\tfont-size: ${e.rulerFontSize}px;\n\tcolor: var(--ruler-text-color);\n\toverflow: hidden;\n `,t}(t),r=function(e){const t=document.createElement("div");return t.className="canvas-ruler vertical-ruler",t.style.cssText=`\n\tposition: absolute;\n\ttop: ${e.rulerSize}px;\n\tleft: 0;\n\tbottom: 0;\n\twidth: ${e.rulerSize}px;\n\tbackground: var(--ruler-background-color);\n\tborder-right: 1px solid var(--ruler-border-color);\n\tz-index: ${q};\n\tpointer-events: none;\n\tfont-family: ${e.rulerFontFamily};\n\tfont-size: ${e.rulerFontSize}px;\n\tcolor: var(--ruler-text-color);\n\toverflow: hidden;\n `,t}(t),a=function(e){const t=document.createElement("div");return t.className="canvas-ruler corner-box",t.style.cssText=`\n\t\tposition: absolute;\n\t\ttop: 0;\n\t\tleft: 0;\n\t\twidth: ${e.rulerSize}px;\n\t\theight: ${e.rulerSize}px;\n\t\tbackground: var(--ruler-background-color);\n\t\tborder-right: 1px solid var(--ruler-border-color);\n\t\tborder-bottom: 1px solid var(--ruler-border-color);\n\t\tz-index: ${U};\n\t\tdisplay: flex;\n\t\talign-items: center;\n\t\tjustify-content: center;\n\t\tfont-family: ${e.rulerFontFamily};\n\t\tfont-size: ${e.rulerFontSize-2}px;\n\t\tcolor: var(--ruler-text-color);\n\t\tpointer-events: none;\n\t`,t.textContent=e.rulerUnits,t}(t),o=t.enableGrid?function(e){const t=document.createElement("div");return t.className="canvas-ruler grid-overlay",t.style.cssText=`\n\t\tposition: absolute;\n\t\ttop: ${e.rulerSize}px;\n\t\tleft: ${e.rulerSize}px;\n\t\tright: 0;\n\t\tbottom: 0;\n\t\tpointer-events: none;\n\t\tz-index: ${H};\n\t\tbackground-image: \n\t\t\tlinear-gradient(var(--grid-color) 1px, transparent 1px),\n\t\t\tlinear-gradient(90deg, var(--grid-color) 1px, transparent 1px);\n\t\tbackground-size: 100px 100px;\n\t\topacity: 0.5;\n\t`,t}(t):void 0;return e.appendChild(n),e.appendChild(r),e.appendChild(a),o&&e.appendChild(o),{horizontalRuler:n,verticalRuler:r,cornerBox:a,gridOverlay:o}}function re(e,t){const n=e/Math.max(5,Math.min(20,t/50)),r=10**Math.floor(Math.log10(n)),a=n/r;let o;return o=a<=1?1:a<=2?2:a<=5?5:10,o*r}function ae(e,t,n,r,a){const o=document.createElement("div");o.className="tick",o.style.cssText=`\n\t\tposition: absolute;\n\t\tleft: ${n}px;\n\t\tbottom: 0;\n\t\twidth: 1px;\n\t\theight: ${W}px;\n\t\tbackground: var(--ruler-tick-color);\n\t`,e.appendChild(o);if(t%J===0){const r=document.createElement("div");r.style.cssText=`\n\t\t\tposition: absolute;\n\t\t\tleft: ${n}px;\n\t\t\tbottom: ${W+2}px;\n\t\t\tfont-size: ${a.rulerFontSize}px;\n\t\t\tline-height: 1;\n\t\t\tcolor: var(--ruler-text-color);\n\t\t\twhite-space: nowrap;\n\t\t\tpointer-events: none;\n\t\t`,r.textContent=Math.round(t).toString(),e.appendChild(r)}}function oe(e,t,n,r,a){const o=document.createElement("div");o.className="tick",o.style.cssText=`\n\t\tposition: absolute;\n\t\ttop: ${n}px;\n\t\tright: 0;\n\t\twidth: ${j}px;\n\t\theight: 1px;\n\t\tbackground: var(--ruler-tick-color);\n\t`,e.appendChild(o);if(t%J===0){const r=document.createElement("div");r.style.cssText=`\n\t\t\tposition: absolute;\n\t\t\ttop: ${n-6}px;\n\t\t\tright: ${j+6}px;\n\t\t\tfont-size: ${a.rulerFontSize}px;\n\t\t\tline-height: 1;\n\t\t\tcolor: var(--ruler-text-color);\n\t\t\twhite-space: nowrap;\n\t\t\tpointer-events: none;\n\t\t\ttransform: rotate(-90deg);\n\t\t\ttransform-origin: right center;\n\t\t`,r.textContent=Math.round(t).toString(),e.appendChild(r)}}function ie(e,t,n,r,a){const o=e.getBounds(),i=o.scale||1,s=o.translateX||0,l=o.translateY||0,c=o.width-a.rulerSize,u=o.height-a.rulerSize,d=-s/i,h=-l/i,g=h+u/i;!function(e,t,n,r,a,o){const i=r,s=re(n-t,i),l=document.createDocumentFragment(),c=Math.floor(t/s)*s,u=Math.ceil(n/s)*s;for(let e=c;e<=u;e+=s){const n=(e-t)*a;n>=-50&&n<=i+50&&ae(l,e,n,0,o)}e.innerHTML="",e.appendChild(l)}(t,d,d+c/i,c,i,a),function(e,t,n,r,a,o){const i=r,s=re(n-t,i),l=document.createDocumentFragment(),c=Math.floor(t/s)*s,u=Math.ceil(n/s)*s;for(let e=c;e<=u;e+=s){const n=(e-t)*a;n>=-50&&n<=i+50&&oe(l,e,n,0,o)}e.innerHTML="",e.appendChild(l)}(n,h,g,u,i,a),r&&function(e,t,n,r){let a=Q*t;for(;a<ee;)a*=2;for(;a>te;)a/=2;e.style.backgroundSize=`${a}px ${a}px`,e.style.backgroundPosition=`${n%a}px ${r%a}px`}(r,i,s,l)}function se(e,t){const n=`light-dark(${t.rulerBackgroundColor}, ${t.rulerBackgroundColorDark})`,r=`light-dark(${t.rulerBorderColor}, ${t.rulerBorderColorDark})`,a=`light-dark(${t.rulerTextColor}, ${t.rulerTextColorDark})`,o=`light-dark(${t.rulerTickColor}, ${t.rulerTickColorDark})`,i=`light-dark(${t.gridColor}, ${t.gridColorDark})`;e.horizontalRuler&&(e.horizontalRuler.style.setProperty("--ruler-background-color",n),e.horizontalRuler.style.setProperty("--ruler-border-color",r),e.horizontalRuler.style.setProperty("--ruler-text-color",a),e.horizontalRuler.style.setProperty("--ruler-tick-color",o)),e.verticalRuler&&(e.verticalRuler.style.setProperty("--ruler-background-color",n),e.verticalRuler.style.setProperty("--ruler-border-color",r),e.verticalRuler.style.setProperty("--ruler-text-color",a),e.verticalRuler.style.setProperty("--ruler-tick-color",o)),e.cornerBox&&(e.cornerBox.style.setProperty("--ruler-background-color",n),e.cornerBox.style.setProperty("--ruler-border-color",r),e.cornerBox.style.setProperty("--ruler-text-color",a)),e.gridOverlay&&e.gridOverlay.style.setProperty("--grid-color",i)}function le(e,t){if(!e?.container)return console.error("Invalid canvas provided to createRulers"),null;let n,r=null,a=!1;const o=()=>{!a&&n.horizontalRuler&&n.verticalRuler&&ie(e,n.horizontalRuler,n.verticalRuler,n.gridOverlay,t)};try{return n=ne(e.container,t),r=function(e,t){const n=d(t),r=e.updateTransform;e.updateTransform=function(e){const t=r.call(this,e);return n(),t};const a=d(t);return window.addEventListener("resize",a),()=>{window.removeEventListener("resize",a),e.updateTransform=r,n.cleanup(),a.cleanup()}}(e,o),se(n,t),o(),t.showRulers||(n.horizontalRuler.style.display="none",n.verticalRuler.style.display="none",n.cornerBox.style.display="none"),!t.showGrid&&n.gridOverlay&&(n.gridOverlay.style.display="none"),{horizontalRuler:n.horizontalRuler,verticalRuler:n.verticalRuler,cornerBox:n.cornerBox,gridOverlay:n.gridOverlay,update:o,updateTheme:e=>{a||se(n,e)},show:()=>{n.horizontalRuler&&(n.horizontalRuler.style.display="block"),n.verticalRuler&&(n.verticalRuler.style.display="block"),n.cornerBox&&(n.cornerBox.style.display="flex")},hide:()=>{n.horizontalRuler&&(n.horizontalRuler.style.display="none"),n.verticalRuler&&(n.verticalRuler.style.display="none"),n.cornerBox&&(n.cornerBox.style.display="none"),n.gridOverlay&&(n.gridOverlay.style.display="none")},toggleGrid:()=>{if(n.gridOverlay){const e="none"!==n.gridOverlay.style.display;n.gridOverlay.style.display=e?"none":"block"}},destroy:()=>{a=!0,r&&r(),n.horizontalRuler?.parentNode&&n.horizontalRuler.parentNode.removeChild(n.horizontalRuler),n.verticalRuler?.parentNode&&n.verticalRuler.parentNode.removeChild(n.verticalRuler),n.cornerBox?.parentNode&&n.cornerBox.parentNode.removeChild(n.cornerBox),n.gridOverlay?.parentNode&&n.gridOverlay.parentNode.removeChild(n.gridOverlay)}}}catch(e){return console.error("Failed to create rulers:",e),null}}return class{constructor(e,t={}){if(this.cleanupCallbacks=[],this.rulers=null,this.dragSetup=null,this.keyboardCleanup=null,this.textEditModeEnabled=!1,this.event=new z,this._isReady=!1,!e)throw new Error("Container element is required");this.config=C(t);const n=E(e,this.config);if(!n)throw new Error("Failed to create canvas");if(this.canvas=n,u(this.config,"enableRulers",()=>{this.rulers=le(this,this.config),this.cleanupCallbacks.push(()=>{this.rulers&&this.rulers.destroy()})}),this.event.setEmitInterceptor((e,t)=>{!function(e,t,n){if("undefined"==typeof window)return;let r=t;"ready"===e&&(r={ready:!0});const a=n.name||"markupCanvas";window.postMessage({source:"markup-canvas",action:e,data:r,timestamp:Date.now(),canvasName:a},"*"),window.parent&&window.parent.postMessage({source:"markup-canvas",action:e,data:r,timestamp:Date.now(),canvasName:a},"*")}(e,t,this.config)}),function(e,t){if("undefined"==typeof window)return;const n=t.name||"markupCanvas",r=window,a={config:{get current(){return e.config},get:e.getConfig.bind(e),update:e.updateConfig.bind(e)},transform:{update:e.updateTransform.bind(e),reset:e.reset.bind(e),resetToInitial:e.resetToInitial.bind(e)},zoom:{get current(){return e.transform.scale||1},set:e.setZoom.bind(e),toPoint:e.zoomToPoint.bind(e),in:e.zoomIn.bind(e),out:e.zoomOut.bind(e),reset:e.resetZoom.bind(e),resetToCenter:e.resetViewToCenter.bind(e),fitToScreen:e.fitToScreen.bind(e)},pan:{left:e.panLeft.bind(e),right:e.panRight.bind(e),up:e.panUp.bind(e),down:e.panDown.bind(e),toPoint:e.panToPoint.bind(e),toCenter:e.centerContent.bind(e)},mouseDrag:{enable:e.enableMouseDrag.bind(e),disable:e.disableMouseDrag.bind(e),isEnabled:e.isMouseDragEnabled.bind(e)},keyboard:{enable:e.enableKeyboard.bind(e),disable:e.disableKeyboard.bind(e),isEnabled:e.isKeyboardEnabled.bind(e),enableTextEditMode:e.enableTextEditMode.bind(e),disableTextEditMode:e.disableTextEditMode.bind(e),isTextEditModeEnabled:e.isTextEditModeEnabled.bind(e)},grid:{toggle:e.toggleGrid.bind(e),show:e.showGrid.bind(e),hide:e.hideGrid.bind(e),isVisible:e.isGridVisible.bind(e)},rulers:{toggle:e.toggleRulers.bind(e),show:e.showRulers.bind(e),hide:e.hideRulers.bind(e),isVisible:e.areRulersVisible.bind(e)},canvas:{canvasToContent:e.canvasToContent.bind(e),getVisibleArea:e.getVisibleArea.bind(e),isPointVisible:e.isPointVisible.bind(e),getBounds:e.getBounds.bind(e)},theme:{get current(){return e.config.themeMode},update:e.updateThemeMode.bind(e),toggle:e.toggleThemeMode.bind(e)},transition:{get current(){return e.config.enableTransition},set:e.updateTransition.bind(e),toggle:e.toggleTransitionMode.bind(e)},event:e.event,lifecycle:{cleanup:e.cleanup.bind(e),destroy:e.destroy.bind(e)},state:{get isReady(){return e.isReady},get isTransforming(){return e.isTransforming},get visibleBounds(){return e.visibleBounds},get transform(){return e.transform}}};r[n]=a,r.__markupCanvasInstances||(r.__markupCanvasInstances=new Map),r.__markupCanvasInstances.set(n,a)}(this,this.config),this.config.enablePostMessageAPI){const e=function(e){const t=t=>{const n=t.data;if(!["markup-canvas","application"].includes(n.source))return;const r=e.config.name||"markupCanvas";if(n.canvasName!==r)return;const a=n.action,o=n.data;F(e,a,o,r)};return"undefined"!=typeof window&&window.addEventListener("message",t),()=>{"undefined"!=typeof window&&window.removeEventListener("message",t)}}(this);this.cleanupCallbacks.push(e)}this.setupEventHandlers(),this._isReady=!0,this.event.emit("ready",this)}setupEventHandlers(){try{u(this.config,"enableZoom",()=>{const e=N(this,this.config);this.cleanupCallbacks.push(e)}),(this.config.enablePan||this.config.enableClickToZoom)&&(this.dragSetup=Z(this,this.config,!0),this.cleanupCallbacks.push(this.dragSetup.cleanup)),u(this.config,"enableKeyboard",()=>{const e=Y(this,this.config,{textEditModeEnabled:this.textEditModeEnabled});this.keyboardCleanup=e,this.cleanupCallbacks.push(e)}),u(this.config,"enableTouch",()=>{const e=_(this);this.cleanupCallbacks.push(e)})}catch(e){throw console.error("Failed to set up event handlers:",e),this.cleanup(),e}}get container(){return this.canvas.container}get transformLayer(){return this.canvas.transformLayer}get contentLayer(){return this.canvas.contentLayer}get transform(){return this.canvas.transform}get isReady(){return this._isReady}get isTransforming(){return this.dragSetup?.isEnabled()||!1}get visibleBounds(){return s(this)}getBounds(){return p(this.canvas,this.config)}updateTransform(e){const t=T(this.canvas,e);return t&&L(this.event,this.canvas),t}reset(){const e=T(this.canvas,{scale:1,translateX:0,translateY:0});return e&&L(this.event,this.canvas),e}resetToInitial(){const e=(t=this.canvas,n=this.transformLayer,r=this.config,b(n,r,()=>h(t,r.rulerSize,e=>{const n=r.initialZoom??1,a=r.initialPan??{x:0,y:0},o=-1*e,i={scale:n,translateX:a.x+o,translateY:a.y+o};return T(t,i)})));var t,n,r;return e&&L(this.event,this.canvas),e}setZoom(e){return function(e,t,n,r,a){return b(t,n,()=>l(n,t=>{const n=t(a),o=M(e);return r(o.x,o.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this),e)}canvasToContent(t,r){return e(t,r,n(this.canvas.transform.scale,this.canvas.transform.translateX,this.canvas.transform.translateY))}zoomToPoint(e,t,n){const r=function(e,t,n,r,a,o){return b(t,n,()=>{const t=i(r,a,e.transform,o/e.transform.scale,n);return T(e,t)})}(this.canvas,this.transformLayer,this.config,e,t,n);return r&&L(this.event,this.canvas),r}resetView(){return e=this.canvas,t=this.transformLayer,n=this.config,b(t,n,()=>h(e,n.rulerSize,t=>T(e,{scale:1,translateX:-1*t,translateY:-1*t})));var e,t,n}resetViewToCenter(){return function(e,t,n,r){return b(t,n,()=>l(n,t=>{const n=t(1),a=M(e);return r(a.x,a.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this))}panLeft(e){return v(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&v(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}panRight(e){return k(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&k(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}panUp(e){return w(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&w(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}panDown(e){return y(this.canvas,this.config,this.updateTransform.bind(this))||!!e&&y(this.canvas,{...this.config,keyboardPanStep:e},this.updateTransform.bind(this))}zoomIn(e=.5){return function(e,t,n,r,a=.5){return b(t,n,()=>l(n,t=>{const n=t(e.transform.scale*(1+a)),o=M(e);return r(o.x,o.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this),e)}zoomOut(e=.5){return function(e,t,n,r,a=.5){return b(t,n,()=>l(n,t=>{const n=t(e.transform.scale*(1-a)),o=M(e);return r(o.x,o.y,n)}))}(this,this.transformLayer,this.config,this.zoomToPoint.bind(this),e)}resetZoom(){return this.resetViewToCenter()}enableMouseDrag(){return this.dragSetup?.enable()??!1}disableMouseDrag(){return this.dragSetup?.disable()??!1}isMouseDragEnabled(){return this.dragSetup?.isEnabled()??!1}enableKeyboard(){return this.keyboardCleanup||(this.keyboardCleanup=Y(this,this.config,{textEditModeEnabled:this.textEditModeEnabled}),this.cleanupCallbacks.push(this.keyboardCleanup)),!0}disableKeyboard(){return!!this.keyboardCleanup&&(this.keyboardCleanup(),this.keyboardCleanup=null,!0)}isKeyboardEnabled(){return null!==this.keyboardCleanup}enableTextEditMode(){if(this.textEditModeEnabled)return!0;if(this.textEditModeEnabled=!0,this.keyboardCleanup){const e=this.cleanupCallbacks.indexOf(this.keyboardCleanup);e>-1&&this.cleanupCallbacks.splice(e,1),this.keyboardCleanup(),this.keyboardCleanup=Y(this,this.config,{textEditModeEnabled:!0}),this.cleanupCallbacks.push(this.keyboardCleanup)}return!0}disableTextEditMode(){if(!this.textEditModeEnabled)return!0;if(this.textEditModeEnabled=!1,this.keyboardCleanup){const e=this.cleanupCallbacks.indexOf(this.keyboardCleanup);e>-1&&this.cleanupCallbacks.splice(e,1),this.keyboardCleanup(),this.keyboardCleanup=Y(this,this.config,{textEditModeEnabled:!1}),this.cleanupCallbacks.push(this.keyboardCleanup)}return!0}isTextEditModeEnabled(){return this.textEditModeEnabled}toggleGrid(){const e=(t=this.rulers,!!t?.toggleGrid&&(t.toggleGrid(),!0));var t;return e&&this.event.emit("gridVisibility",this.isGridVisible()),e}showGrid(){const e=(t=this.rulers,!!t?.gridOverlay&&(t.gridOverlay.style.display="block",!0));var t;return e&&this.event.emit("gridVisibility",!0),e}hideGrid(){const e=(t=this.rulers,!!t?.gridOverlay&&(t.gridOverlay.style.display="none",!0));var t;return e&&this.event.emit("gridVisibility",!1),e}isGridVisible(){return e=this.rulers,!!e?.gridOverlay&&"none"!==e.gridOverlay.style.display;var e}toggleRulers(){const e=function(e,t){if(e)return t()?e.hide():e.show(),!0;return!1}(this.rulers,()=>this.areRulersVisible());return e&&this.event.emit("rulersVisibility",this.areRulersVisible()),e}showRulers(){const e=!!(t=this.rulers)&&(t.show(),!0);var t;return e&&this.event.emit("rulersVisibility",!0),e}hideRulers(){const e=!!(t=this.rulers)&&(t.hide(),!0);var t;return e&&this.event.emit("rulersVisibility",!1),e}areRulersVisible(){return e=this.rulers,!!e?.horizontalRuler&&"none"!==e.horizontalRuler.style.display;var e}centerContent(){return e=this.canvas,t=this.config,n=this.updateTransform.bind(this),b(this.transformLayer,t,()=>{const r=p(e,t),a=(r.width-r.contentWidth*e.transform.scale)/2,o=(r.height-r.contentHeight*e.transform.scale)/2;return n({translateX:a,translateY:o})});var e,t,n}fitToScreen(){return e=this.canvas,t=this.transformLayer,n=this.config,b(t,n,()=>{const t=p(e,n),r=t.width/n.width,a=t.height/n.height,o=l(n,e=>e(.9*Math.min(r,a))),i=n.width*o,s=n.height*o,c=(t.width-i)/2,u=(t.height-s)/2;return T(e,{scale:o,translateX:c,translateY:u})});var e,t,n}getVisibleArea(){return s(this)}isPointVisible(e,t){return function(e,t,n){const r=s(e);return t>=r.x&&t<=r.x+r.width&&n>=r.y&&n<=r.y+r.height}(this,e,t)}panToPoint(e,t){return function(e,t,n,r,a,o){return b(o,t,()=>{const o=p(e,t),i=o.width/2,s=o.height/2,l=i-n*e.transform.scale,c=s-r*e.transform.scale;return a({translateX:l,translateY:c})})}(this.canvas,this.config,e,t,this.updateTransform.bind(this),this.transformLayer)}getConfig(){return{...this.config}}updateConfig(e){this.config=C({...this.config,...e})}updateThemeMode(e){this.config=C({...this.config,themeMode:e}),function(e,t,n){const r=C({...e,themeMode:n});t&&t.updateTheme(r)}(this.config,this.rulers,e)}toggleThemeMode(){const e="light"===this.config.themeMode?"dark":"light";return this.updateThemeMode(e),e}updateTransition(e){this.config=C({...this.config,enableTransition:e})}toggleTransitionMode(){const e=function(e){return!e}(this.config.enableTransition);return this.updateTransition(e),e}cleanup(){!function(e){if("undefined"==typeof window)return;const t=e.name||"markupCanvas",n=window;delete n[t],n.__markupCanvasInstances&&n.__markupCanvasInstances.delete(t)}(this.config),this.cleanupCallbacks.forEach(e=>{try{e()}catch(e){console.warn("Error during cleanup:",e)}}),this.cleanupCallbacks=[],this.removeAllListeners()}on(e,t){this.event.on(e,t)}off(e,t){this.event.off(e,t)}emit(e,t){this.event.emit(e,t)}removeAllListeners(){this.event.removeAllListeners()}destroy(){this.cleanup(),window.__markupCanvasTransitionTimeout&&clearTimeout(window.__markupCanvasTransitionTimeout)}}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markup-canvas/core",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "High-performance canvas-like container with pan and zoom capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/markup-canvas.cjs.js",
@@ -59,10 +59,6 @@ export class MarkupCanvas {
59
59
  });
60
60
  });
61
61
 
62
- if (options.themeMode) {
63
- updateThemeMode(this.canvas.container, this.config, this.rulers, options.themeMode);
64
- }
65
-
66
62
  // Always bind canvas to window
67
63
  this.event.setEmitInterceptor((event, data) => {
68
64
  broadcastEvent(event as string, data, this.config);
@@ -421,7 +417,7 @@ export class MarkupCanvas {
421
417
  // Theme management
422
418
  updateThemeMode(mode: "light" | "dark"): void {
423
419
  this.config = createMarkupCanvasConfig({ ...this.config, themeMode: mode });
424
- updateThemeMode(this.canvas.container, this.config, this.rulers, mode);
420
+ updateThemeMode(this.config, this.rulers, mode);
425
421
  }
426
422
 
427
423
  toggleThemeMode(): "light" | "dark" {
@@ -1,9 +1,7 @@
1
1
  import { createMarkupCanvasConfig } from "@/lib/config/createMarkupCanvasConfig.js";
2
- import { getThemeValue } from "@/lib/helpers/index.js";
3
2
  import type { MarkupCanvasConfig, RulerSystem } from "@/types/index.js";
4
3
 
5
4
  export function updateThemeMode(
6
- canvasContainer: HTMLElement,
7
5
  config: Required<MarkupCanvasConfig>,
8
6
  rulers: RulerSystem | null,
9
7
  mode: "light" | "dark"
@@ -14,10 +12,6 @@ export function updateThemeMode(
14
12
  };
15
13
  const updatedConfig = createMarkupCanvasConfig(newConfig);
16
14
 
17
- // Update canvas background color
18
- const backgroundColor = getThemeValue(updatedConfig, "canvasBackgroundColor");
19
- canvasContainer.style.backgroundColor = backgroundColor;
20
-
21
15
  // Update rulers if they exist
22
16
  if (rulers) {
23
17
  rulers.updateTheme(updatedConfig);
@@ -1,6 +1,5 @@
1
1
  import { checkContainerDimensions } from "@/lib/canvas/checkContainerDimensions";
2
2
  import { CANVAS_CONTAINER_CLASS } from "@/lib/constants";
3
- import { getThemeValue } from "@/lib/helpers/index.js";
4
3
  import type { MarkupCanvasConfig } from "@/types/index.js";
5
4
 
6
5
  export function setupCanvasContainer(container: HTMLElement, config?: Required<MarkupCanvasConfig>): void {
@@ -12,9 +11,9 @@ export function setupCanvasContainer(container: HTMLElement, config?: Required<M
12
11
  container.style.cursor = "grab";
13
12
  container.style.overscrollBehavior = "none";
14
13
 
15
- // Apply canvas background color
14
+ // Apply canvas background color using light-dark() CSS function
16
15
  if (config) {
17
- const backgroundColor = getThemeValue(config, "canvasBackgroundColor");
16
+ const backgroundColor = `light-dark(${config.canvasBackgroundColor}, ${config.canvasBackgroundColorDark})`;
18
17
  container.style.backgroundColor = backgroundColor;
19
18
  }
20
19
 
@@ -1,4 +1,3 @@
1
- import { getThemeValue } from "@/lib/helpers/index.js";
2
1
  import type { MarkupCanvasConfig } from "@/types/index.js";
3
2
 
4
3
  export interface RulerThemeUpdater {
@@ -9,12 +8,12 @@ export interface RulerThemeUpdater {
9
8
  }
10
9
 
11
10
  export function updateRulerTheme(elements: RulerThemeUpdater, config: Required<MarkupCanvasConfig>): void {
12
- // Get theme-aware colors
13
- const backgroundColor = getThemeValue(config, "rulerBackgroundColor");
14
- const borderColor = getThemeValue(config, "rulerBorderColor");
15
- const textColor = getThemeValue(config, "rulerTextColor");
16
- const tickColor = getThemeValue(config, "rulerTickColor");
17
- const gridColor = getThemeValue(config, "gridColor");
11
+ // Create theme-aware colors using light-dark() CSS function
12
+ const backgroundColor = `light-dark(${config.rulerBackgroundColor}, ${config.rulerBackgroundColorDark})`;
13
+ const borderColor = `light-dark(${config.rulerBorderColor}, ${config.rulerBorderColorDark})`;
14
+ const textColor = `light-dark(${config.rulerTextColor}, ${config.rulerTextColorDark})`;
15
+ const tickColor = `light-dark(${config.rulerTickColor}, ${config.rulerTickColorDark})`;
16
+ const gridColor = `light-dark(${config.gridColor}, ${config.gridColorDark})`;
18
17
 
19
18
  // Update horizontal ruler with CSS variables
20
19
  if (elements.horizontalRuler) {