@markup-canvas/core 1.1.7 → 1.2.1

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.
Files changed (161) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/lib/MarkupCanvas.d.ts +9 -12
  3. package/dist/lib/actions/config/getConfig.d.ts +2 -0
  4. package/dist/lib/actions/index.d.ts +3 -0
  5. package/dist/lib/actions/pan/centerContent.d.ts +2 -0
  6. package/dist/lib/actions/pan/index.d.ts +6 -0
  7. package/dist/lib/actions/pan/panDown.d.ts +2 -0
  8. package/dist/lib/actions/pan/panLeft.d.ts +2 -0
  9. package/dist/lib/actions/pan/panRight.d.ts +2 -0
  10. package/dist/lib/actions/pan/panUp.d.ts +2 -0
  11. package/dist/lib/actions/pan/scrollToPoint.d.ts +2 -0
  12. package/dist/lib/actions/transform/index.d.ts +2 -0
  13. package/dist/lib/actions/transform/resetTransform.d.ts +2 -0
  14. package/dist/lib/actions/transform/updateTransform.d.ts +2 -0
  15. package/dist/lib/actions/ui/grid/hideGrid.d.ts +2 -0
  16. package/dist/lib/actions/ui/grid/index.d.ts +4 -0
  17. package/dist/lib/actions/ui/grid/isGridVisible.d.ts +2 -0
  18. package/dist/lib/actions/ui/grid/showGrid.d.ts +2 -0
  19. package/dist/lib/actions/ui/grid/toggleGrid.d.ts +2 -0
  20. package/dist/lib/actions/ui/index.d.ts +4 -0
  21. package/dist/lib/actions/ui/rulers/areRulersVisible.d.ts +2 -0
  22. package/dist/lib/actions/ui/rulers/hideRulers.d.ts +2 -0
  23. package/dist/lib/actions/ui/rulers/index.d.ts +4 -0
  24. package/dist/lib/actions/ui/rulers/showRulers.d.ts +2 -0
  25. package/dist/lib/actions/ui/rulers/toggleRulers.d.ts +2 -0
  26. package/dist/lib/actions/ui/toggleTransition.d.ts +1 -0
  27. package/dist/lib/actions/ui/updateThemeMode.d.ts +2 -0
  28. package/dist/lib/actions/zoom/index.d.ts +6 -0
  29. package/dist/lib/actions/zoom/resetView.d.ts +2 -0
  30. package/dist/lib/actions/zoom/resetViewToCenter.d.ts +3 -0
  31. package/dist/lib/actions/zoom/setZoom.d.ts +3 -0
  32. package/dist/lib/actions/zoom/zoomIn.d.ts +3 -0
  33. package/dist/lib/actions/zoom/zoomOut.d.ts +3 -0
  34. package/dist/lib/actions/zoom/zoomToPoint.d.ts +2 -0
  35. package/dist/lib/canvas/createCanvas.d.ts +2 -2
  36. package/dist/lib/canvas/fitToScreen.d.ts +2 -0
  37. package/dist/lib/canvas/getCanvasBounds.d.ts +2 -2
  38. package/dist/lib/canvas/index.d.ts +1 -1
  39. package/dist/lib/events/emitTransformEvents.d.ts +3 -0
  40. package/dist/lib/events/keyboard/handleKeyDown.d.ts +3 -2
  41. package/dist/lib/events/keyboard/handleKeyUp.d.ts +3 -2
  42. package/dist/lib/events/keyboard/setupKeyboardEvents.d.ts +3 -2
  43. package/dist/lib/events/mouse/createMouseDragControls.d.ts +7 -0
  44. package/dist/lib/events/mouse/handleClickToZoom.d.ts +3 -2
  45. package/dist/lib/events/mouse/handleMouseDown.d.ts +3 -2
  46. package/dist/lib/events/mouse/handleMouseLeave.d.ts +3 -2
  47. package/dist/lib/events/mouse/handleMouseMove.d.ts +3 -2
  48. package/dist/lib/events/mouse/handleMouseUp.d.ts +3 -2
  49. package/dist/lib/events/mouse/setupMouseDrag.d.ts +4 -3
  50. package/dist/lib/events/mouse/setupMouseEvents.d.ts +4 -3
  51. package/dist/lib/events/touch/handleTouchMove.d.ts +3 -2
  52. package/dist/lib/events/touch/setupTouchEvents.d.ts +2 -2
  53. package/dist/lib/events/trackpad/createTrackpadPanHandler.d.ts +2 -2
  54. package/dist/lib/events/utils/getAdaptiveZoomSpeed.d.ts +2 -2
  55. package/dist/lib/events/utils/getViewportCenter.d.ts +2 -2
  56. package/dist/lib/events/utils/resetDragState.d.ts +3 -2
  57. package/dist/lib/events/utils/updateCursor.d.ts +3 -2
  58. package/dist/lib/events/wheel/handleWheel.d.ts +3 -2
  59. package/dist/lib/events/wheel/setupWheelEvents.d.ts +3 -2
  60. package/dist/lib/events/wheel/setupWheelHandler.d.ts +3 -2
  61. package/dist/lib/helpers/getVisibleArea.d.ts +7 -0
  62. package/dist/lib/helpers/index.d.ts +2 -0
  63. package/dist/lib/helpers/isPointVisible.d.ts +2 -0
  64. package/dist/lib/transform/applyZoomToCanvas.d.ts +2 -2
  65. package/dist/lib/window/bindCanvasToWindow.d.ts +3 -0
  66. package/dist/lib/window/broadcastEvent.d.ts +2 -0
  67. package/dist/lib/window/cleanupWindowBinding.d.ts +2 -0
  68. package/dist/lib/window/index.d.ts +3 -0
  69. package/dist/markup-canvas.cjs.js +689 -529
  70. package/dist/markup-canvas.esm.js +689 -529
  71. package/dist/markup-canvas.umd.js +687 -524
  72. package/dist/markup-canvas.umd.min.js +1 -1
  73. package/dist/types/canvas.d.ts +1 -48
  74. package/dist/types/config.d.ts +0 -3
  75. package/dist/types/events.d.ts +4 -1
  76. package/dist/types/index.d.ts +3 -2
  77. package/dist/types/window.d.ts +84 -0
  78. package/package.json +1 -1
  79. package/src/index.ts +1 -1
  80. package/src/lib/MarkupCanvas.ts +139 -315
  81. package/src/lib/actions/config/getConfig.ts +5 -0
  82. package/src/lib/actions/index.ts +6 -0
  83. package/src/lib/actions/pan/centerContent.ts +21 -0
  84. package/src/lib/actions/pan/index.ts +6 -0
  85. package/src/lib/actions/pan/panDown.ts +13 -0
  86. package/src/lib/actions/pan/panLeft.ts +13 -0
  87. package/src/lib/actions/pan/panRight.ts +13 -0
  88. package/src/lib/actions/pan/panUp.ts +13 -0
  89. package/src/lib/actions/pan/scrollToPoint.ts +27 -0
  90. package/src/lib/actions/transform/index.ts +2 -0
  91. package/src/lib/actions/transform/resetTransform.ts +11 -0
  92. package/src/lib/actions/transform/updateTransform.ts +9 -0
  93. package/src/lib/actions/ui/grid/hideGrid.ts +9 -0
  94. package/src/lib/actions/ui/grid/index.ts +4 -0
  95. package/src/lib/actions/ui/grid/isGridVisible.ts +8 -0
  96. package/src/lib/actions/ui/grid/showGrid.ts +9 -0
  97. package/src/lib/actions/ui/grid/toggleGrid.ts +9 -0
  98. package/src/lib/actions/ui/index.ts +4 -0
  99. package/src/lib/actions/ui/rulers/areRulersVisible.ts +8 -0
  100. package/src/lib/actions/ui/rulers/hideRulers.ts +9 -0
  101. package/src/lib/actions/ui/rulers/index.ts +4 -0
  102. package/src/lib/actions/ui/rulers/showRulers.ts +9 -0
  103. package/src/lib/actions/ui/rulers/toggleRulers.ts +14 -0
  104. package/src/lib/actions/ui/toggleTransition.ts +3 -0
  105. package/src/lib/actions/ui/updateThemeMode.ts +25 -0
  106. package/src/lib/actions/zoom/index.ts +6 -0
  107. package/src/lib/actions/zoom/resetView.ts +17 -0
  108. package/src/lib/actions/zoom/resetViewToCenter.ts +21 -0
  109. package/src/lib/actions/zoom/setZoom.ts +22 -0
  110. package/src/lib/actions/zoom/zoomIn.ts +21 -0
  111. package/src/lib/actions/zoom/zoomOut.ts +21 -0
  112. package/src/lib/actions/zoom/zoomToPoint.ts +18 -0
  113. package/src/lib/canvas/createCanvas.ts +6 -14
  114. package/src/lib/canvas/fitToScreen.ts +27 -0
  115. package/src/lib/canvas/getCanvasBounds.ts +3 -4
  116. package/src/lib/canvas/index.ts +1 -1
  117. package/src/lib/config/constants.ts +2 -6
  118. package/src/lib/config/presets/editor-preset.ts +2 -6
  119. package/src/lib/events/emitTransformEvents.ts +9 -0
  120. package/src/lib/events/keyboard/handleKeyDown.ts +3 -2
  121. package/src/lib/events/keyboard/handleKeyUp.ts +3 -2
  122. package/src/lib/events/keyboard/setupKeyboardEvents.ts +3 -2
  123. package/src/lib/events/mouse/createMouseDragControls.ts +21 -0
  124. package/src/lib/events/mouse/handleClickToZoom.ts +3 -2
  125. package/src/lib/events/mouse/handleMouseDown.ts +3 -2
  126. package/src/lib/events/mouse/handleMouseLeave.ts +3 -2
  127. package/src/lib/events/mouse/handleMouseMove.ts +3 -2
  128. package/src/lib/events/mouse/handleMouseUp.ts +3 -2
  129. package/src/lib/events/mouse/setupMouseDrag.ts +5 -4
  130. package/src/lib/events/mouse/setupMouseEvents.ts +5 -4
  131. package/src/lib/events/postMessage/setupPostMessageEvents.ts +10 -0
  132. package/src/lib/events/touch/handleTouchMove.ts +3 -2
  133. package/src/lib/events/touch/setupTouchEvents.ts +3 -2
  134. package/src/lib/events/trackpad/createTrackpadPanHandler.ts +3 -2
  135. package/src/lib/events/utils/getAdaptiveZoomSpeed.ts +2 -2
  136. package/src/lib/events/utils/getViewportCenter.ts +2 -2
  137. package/src/lib/events/utils/resetDragState.ts +3 -2
  138. package/src/lib/events/utils/updateCursor.ts +3 -2
  139. package/src/lib/events/wheel/handleWheel.ts +3 -2
  140. package/src/lib/events/wheel/setupWheelEvents.ts +3 -2
  141. package/src/lib/events/wheel/setupWheelHandler.ts +3 -2
  142. package/src/lib/helpers/getVisibleArea.ts +6 -0
  143. package/src/lib/helpers/index.ts +2 -0
  144. package/src/lib/helpers/isPointVisible.ts +7 -0
  145. package/src/lib/rulers/createRulers.ts +0 -1
  146. package/src/lib/transform/applyZoomToCanvas.ts +2 -2
  147. package/src/lib/window/bindCanvasToWindow.ts +128 -0
  148. package/src/lib/window/broadcastEvent.ts +38 -0
  149. package/src/lib/window/cleanupWindowBinding.ts +15 -0
  150. package/src/lib/window/index.ts +3 -0
  151. package/src/types/canvas.ts +1 -48
  152. package/src/types/config.ts +1 -7
  153. package/src/types/events.ts +7 -1
  154. package/src/types/index.ts +4 -2
  155. package/src/types/window.ts +77 -0
  156. package/dist/lib/canvas/config.d.ts +0 -2
  157. package/dist/lib/canvas/getCanvasMethods.d.ts +0 -12
  158. package/src/lib/canvas/config.ts +0 -29
  159. package/src/lib/canvas/getCanvasMethods.ts +0 -102
  160. /package/dist/lib/canvas/{calcVisibleArea.d.ts → calculateVisibleArea.d.ts} +0 -0
  161. /package/src/lib/canvas/{calcVisibleArea.ts → calculateVisibleArea.ts} +0 -0
@@ -0,0 +1,21 @@
1
+ import { getCanvasBounds } from "@/lib/canvas/getCanvasBounds.js";
2
+ import { withTransition } from "@/lib/transition/withTransition.js";
3
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
4
+
5
+ export function centerContent(
6
+ canvas: Canvas,
7
+ config: Required<MarkupCanvasConfig>,
8
+ updateTransformFn: (newTransform: Partial<Transform>) => boolean,
9
+ transformLayer: HTMLElement
10
+ ): boolean {
11
+ return withTransition(transformLayer, config, () => {
12
+ const bounds = getCanvasBounds(canvas, config);
13
+ const centerX = (bounds.width - bounds.contentWidth * canvas.transform.scale) / 2;
14
+ const centerY = (bounds.height - bounds.contentHeight * canvas.transform.scale) / 2;
15
+
16
+ return updateTransformFn({
17
+ translateX: centerX,
18
+ translateY: centerY,
19
+ });
20
+ });
21
+ }
@@ -0,0 +1,6 @@
1
+ export { centerContent } from "./centerContent.js";
2
+ export { panDown } from "./panDown.js";
3
+ export { panLeft } from "./panLeft.js";
4
+ export { panRight } from "./panRight.js";
5
+ export { panUp } from "./panUp.js";
6
+ export { scrollToPoint } from "./scrollToPoint.js";
@@ -0,0 +1,13 @@
1
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
2
+
3
+ export function panDown(
4
+ canvas: Canvas,
5
+ config: Required<MarkupCanvasConfig>,
6
+ updateTransform: (newTransform: Partial<Transform>) => boolean
7
+ ): boolean {
8
+ const panDistance = config.keyboardPanStep;
9
+ const newTransform: Partial<Transform> = {
10
+ translateY: canvas.transform.translateY - panDistance,
11
+ };
12
+ return updateTransform(newTransform);
13
+ }
@@ -0,0 +1,13 @@
1
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
2
+
3
+ export function panLeft(
4
+ canvas: Canvas,
5
+ config: Required<MarkupCanvasConfig>,
6
+ updateTransform: (newTransform: Partial<Transform>) => boolean
7
+ ): boolean {
8
+ const panDistance = config.keyboardPanStep;
9
+ const newTransform: Partial<Transform> = {
10
+ translateX: canvas.transform.translateX + panDistance,
11
+ };
12
+ return updateTransform(newTransform);
13
+ }
@@ -0,0 +1,13 @@
1
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
2
+
3
+ export function panRight(
4
+ canvas: Canvas,
5
+ config: Required<MarkupCanvasConfig>,
6
+ updateTransform: (newTransform: Partial<Transform>) => boolean
7
+ ): boolean {
8
+ const panDistance = config.keyboardPanStep;
9
+ const newTransform: Partial<Transform> = {
10
+ translateX: canvas.transform.translateX - panDistance,
11
+ };
12
+ return updateTransform(newTransform);
13
+ }
@@ -0,0 +1,13 @@
1
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
2
+
3
+ export function panUp(
4
+ canvas: Canvas,
5
+ config: Required<MarkupCanvasConfig>,
6
+ updateTransform: (newTransform: Partial<Transform>) => boolean
7
+ ): boolean {
8
+ const panDistance = config.keyboardPanStep;
9
+ const newTransform: Partial<Transform> = {
10
+ translateY: canvas.transform.translateY + panDistance,
11
+ };
12
+ return updateTransform(newTransform);
13
+ }
@@ -0,0 +1,27 @@
1
+ import { getCanvasBounds } from "@/lib/canvas/getCanvasBounds.js";
2
+ import { withTransition } from "@/lib/transition/withTransition.js";
3
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
4
+
5
+ export function scrollToPoint(
6
+ canvas: Canvas,
7
+ config: Required<MarkupCanvasConfig>,
8
+ x: number,
9
+ y: number,
10
+ updateTransform: (newTransform: Partial<Transform>) => boolean,
11
+ transformLayer: HTMLElement
12
+ ): boolean {
13
+ return withTransition(transformLayer, config, () => {
14
+ const bounds = getCanvasBounds(canvas, config);
15
+ const centerX = bounds.width / 2;
16
+ const centerY = bounds.height / 2;
17
+
18
+ // Calculate new translation to center the point
19
+ const newTranslateX = centerX - x * canvas.transform.scale;
20
+ const newTranslateY = centerY - y * canvas.transform.scale;
21
+
22
+ return updateTransform({
23
+ translateX: newTranslateX,
24
+ translateY: newTranslateY,
25
+ });
26
+ });
27
+ }
@@ -0,0 +1,2 @@
1
+ export { resetTransform } from "./resetTransform.js";
2
+ export { updateTransform } from "./updateTransform.js";
@@ -0,0 +1,11 @@
1
+ import type { Canvas, Transform } from "@/types/index.js";
2
+ import { updateTransform } from "./updateTransform.js";
3
+
4
+ export function resetTransform(canvas: Canvas): boolean {
5
+ const resetTransformData: Transform = {
6
+ scale: 1.0,
7
+ translateX: 0,
8
+ translateY: 0,
9
+ };
10
+ return updateTransform(canvas, resetTransformData);
11
+ }
@@ -0,0 +1,9 @@
1
+ import { createMatrix } from "@/lib/matrix/createMatrix.js";
2
+ import { applyTransform } from "@/lib/transform/index.js";
3
+ import type { Canvas, Transform } from "@/types/index.js";
4
+
5
+ export function updateTransform(canvas: Canvas, newTransform: Partial<Transform>): boolean {
6
+ canvas.transform = { ...canvas.transform, ...newTransform };
7
+ const matrix = createMatrix(canvas.transform.scale, canvas.transform.translateX, canvas.transform.translateY);
8
+ return applyTransform(canvas.transformLayer, matrix);
9
+ }
@@ -0,0 +1,9 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function hideGrid(rulers: RulerSystem | null): boolean {
4
+ if (rulers?.gridOverlay) {
5
+ rulers.gridOverlay.style.display = "none";
6
+ return true;
7
+ }
8
+ return false;
9
+ }
@@ -0,0 +1,4 @@
1
+ export { hideGrid } from "./hideGrid.js";
2
+ export { isGridVisible } from "./isGridVisible.js";
3
+ export { showGrid } from "./showGrid.js";
4
+ export { toggleGrid } from "./toggleGrid.js";
@@ -0,0 +1,8 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function isGridVisible(rulers: RulerSystem | null): boolean {
4
+ if (rulers?.gridOverlay) {
5
+ return rulers.gridOverlay.style.display !== "none";
6
+ }
7
+ return false;
8
+ }
@@ -0,0 +1,9 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function showGrid(rulers: RulerSystem | null): boolean {
4
+ if (rulers?.gridOverlay) {
5
+ rulers.gridOverlay.style.display = "block";
6
+ return true;
7
+ }
8
+ return false;
9
+ }
@@ -0,0 +1,9 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function toggleGrid(rulers: RulerSystem | null): boolean {
4
+ if (rulers?.toggleGrid) {
5
+ rulers.toggleGrid();
6
+ return true;
7
+ }
8
+ return false;
9
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./grid/index.js";
2
+ export * from "./rulers/index.js";
3
+ export { toggleTransition } from "./toggleTransition.js";
4
+ export { updateThemeMode } from "./updateThemeMode.js";
@@ -0,0 +1,8 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function areRulersVisible(rulers: RulerSystem | null): boolean {
4
+ if (rulers?.horizontalRuler) {
5
+ return rulers.horizontalRuler.style.display !== "none";
6
+ }
7
+ return false;
8
+ }
@@ -0,0 +1,9 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function hideRulers(rulers: RulerSystem | null): boolean {
4
+ if (rulers) {
5
+ rulers.hide();
6
+ return true;
7
+ }
8
+ return false;
9
+ }
@@ -0,0 +1,4 @@
1
+ export { areRulersVisible } from "./areRulersVisible.js";
2
+ export { hideRulers } from "./hideRulers.js";
3
+ export { showRulers } from "./showRulers.js";
4
+ export { toggleRulers } from "./toggleRulers.js";
@@ -0,0 +1,9 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function showRulers(rulers: RulerSystem | null): boolean {
4
+ if (rulers) {
5
+ rulers.show();
6
+ return true;
7
+ }
8
+ return false;
9
+ }
@@ -0,0 +1,14 @@
1
+ import type { RulerSystem } from "@/types/index.js";
2
+
3
+ export function toggleRulers(rulers: RulerSystem | null, areRulersVisible: () => boolean): boolean {
4
+ if (rulers) {
5
+ const isVisible = areRulersVisible();
6
+ if (isVisible) {
7
+ rulers.hide();
8
+ } else {
9
+ rulers.show();
10
+ }
11
+ return true;
12
+ }
13
+ return false;
14
+ }
@@ -0,0 +1,3 @@
1
+ export function toggleTransition(enableTransition: boolean): boolean {
2
+ return !enableTransition;
3
+ }
@@ -0,0 +1,25 @@
1
+ import { createMarkupCanvasConfig } from "@/lib/config/createMarkupCanvasConfig.js";
2
+ import { getThemeValue } from "@/lib/helpers/index.js";
3
+ import type { MarkupCanvasConfig, RulerSystem } from "@/types/index.js";
4
+
5
+ export function updateThemeMode(
6
+ canvasContainer: HTMLElement,
7
+ config: Required<MarkupCanvasConfig>,
8
+ rulers: RulerSystem | null,
9
+ mode: "light" | "dark"
10
+ ): void {
11
+ const newConfig = {
12
+ ...config,
13
+ themeMode: mode,
14
+ };
15
+ const updatedConfig = createMarkupCanvasConfig(newConfig);
16
+
17
+ // Update canvas background color
18
+ const backgroundColor = getThemeValue(updatedConfig, "canvasBackgroundColor");
19
+ canvasContainer.style.backgroundColor = backgroundColor;
20
+
21
+ // Update rulers if they exist
22
+ if (rulers) {
23
+ rulers.updateTheme(updatedConfig);
24
+ }
25
+ }
@@ -0,0 +1,6 @@
1
+ export { resetView } from "./resetView.js";
2
+ export { resetViewToCenter } from "./resetViewToCenter.js";
3
+ export { setZoom } from "./setZoom.js";
4
+ export { zoomIn } from "./zoomIn.js";
5
+ export { zoomOut } from "./zoomOut.js";
6
+ export { zoomToPoint } from "./zoomToPoint.js";
@@ -0,0 +1,17 @@
1
+ import { updateTransform } from "@/lib/actions/transform/updateTransform.js";
2
+ import { withRulerSize } from "@/lib/helpers/index.js";
3
+ import { withTransition } from "@/lib/transition/index.js";
4
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
5
+
6
+ export function resetView(canvas: Canvas, transformLayer: HTMLElement, config: Required<MarkupCanvasConfig>): boolean {
7
+ return withTransition(transformLayer, config, () => {
8
+ return withRulerSize(canvas, config.rulerSize, (rulerSize) => {
9
+ const resetTransformData: Transform = {
10
+ scale: 1.0,
11
+ translateX: rulerSize * -1,
12
+ translateY: rulerSize * -1,
13
+ };
14
+ return updateTransform(canvas, resetTransformData);
15
+ });
16
+ });
17
+ }
@@ -0,0 +1,21 @@
1
+ import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
2
+ import { withClampedZoom } from "@/lib/helpers/index.js";
3
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
4
+ import { withTransition } from "@/lib/transition/withTransition.js";
5
+ import type { MarkupCanvasConfig } from "@/types/index.js";
6
+
7
+ export function resetViewToCenter(
8
+ canvas: MarkupCanvas,
9
+ transformLayer: HTMLElement,
10
+ config: Required<MarkupCanvasConfig>,
11
+ zoomToPoint: (x: number, y: number, targetScale: number) => boolean
12
+ ): boolean {
13
+ return withTransition(transformLayer, config, () => {
14
+ return withClampedZoom(config, (clamp) => {
15
+ const newScale = clamp(1.0);
16
+
17
+ const center = getViewportCenter(canvas);
18
+ return zoomToPoint(center.x, center.y, newScale);
19
+ });
20
+ });
21
+ }
@@ -0,0 +1,22 @@
1
+ import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
2
+ import { withClampedZoom } from "@/lib/helpers/index.js";
3
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
4
+ import { withTransition } from "@/lib/transition/withTransition.js";
5
+ import type { MarkupCanvasConfig } from "@/types/index.js";
6
+
7
+ export function setZoom(
8
+ canvas: MarkupCanvas,
9
+ transformLayer: HTMLElement,
10
+ config: Required<MarkupCanvasConfig>,
11
+ zoomToPoint: (x: number, y: number, targetScale: number) => boolean,
12
+ zoomLevel: number
13
+ ): boolean {
14
+ return withTransition(transformLayer, config, () => {
15
+ return withClampedZoom(config, (clamp) => {
16
+ const newScale = clamp(zoomLevel);
17
+
18
+ const center = getViewportCenter(canvas);
19
+ return zoomToPoint(center.x, center.y, newScale);
20
+ });
21
+ });
22
+ }
@@ -0,0 +1,21 @@
1
+ import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
2
+ import { withClampedZoom } from "@/lib/helpers/index.js";
3
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
4
+ import { withTransition } from "@/lib/transition/withTransition.js";
5
+ import type { MarkupCanvasConfig } from "@/types/index.js";
6
+
7
+ export function zoomIn(
8
+ canvas: MarkupCanvas,
9
+ transformLayer: HTMLElement,
10
+ config: Required<MarkupCanvasConfig>,
11
+ zoomToPoint: (x: number, y: number, targetScale: number) => boolean,
12
+ factor: number = 0.5
13
+ ): boolean {
14
+ return withTransition(transformLayer, config, () => {
15
+ return withClampedZoom(config, (clamp) => {
16
+ const newScale = clamp(canvas.transform.scale * (1 + factor));
17
+ const center = getViewportCenter(canvas);
18
+ return zoomToPoint(center.x, center.y, newScale);
19
+ });
20
+ });
21
+ }
@@ -0,0 +1,21 @@
1
+ import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
2
+ import { withClampedZoom } from "@/lib/helpers/index.js";
3
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
4
+ import { withTransition } from "@/lib/transition/withTransition.js";
5
+ import type { MarkupCanvasConfig } from "@/types/index.js";
6
+
7
+ export function zoomOut(
8
+ canvas: MarkupCanvas,
9
+ transformLayer: HTMLElement,
10
+ config: Required<MarkupCanvasConfig>,
11
+ zoomToPoint: (x: number, y: number, targetScale: number) => boolean,
12
+ factor: number = 0.5
13
+ ): boolean {
14
+ return withTransition(transformLayer, config, () => {
15
+ return withClampedZoom(config, (clamp) => {
16
+ const newScale = clamp(canvas.transform.scale * (1 - factor));
17
+ const center = getViewportCenter(canvas);
18
+ return zoomToPoint(center.x, center.y, newScale);
19
+ });
20
+ });
21
+ }
@@ -0,0 +1,18 @@
1
+ import { updateTransform } from "@/lib/actions/transform/updateTransform.js";
2
+ import { getZoomToMouseTransform } from "@/lib/matrix/getZoomToMouseTransform.js";
3
+ import { withTransition } from "@/lib/transition/index.js";
4
+ import type { Canvas, MarkupCanvasConfig } from "@/types/index.js";
5
+
6
+ export function zoomToPoint(
7
+ canvas: Canvas,
8
+ transformLayer: HTMLElement,
9
+ config: Required<MarkupCanvasConfig>,
10
+ x: number,
11
+ y: number,
12
+ targetScale: number
13
+ ): boolean {
14
+ return withTransition(transformLayer, config, () => {
15
+ const newTransform = getZoomToMouseTransform(x, y, canvas.transform, targetScale / canvas.transform.scale, config);
16
+ return updateTransform(canvas, newTransform);
17
+ });
18
+ }
@@ -1,13 +1,12 @@
1
1
  import { createCanvasLayers } from "@/lib/canvas/createCanvasLayers.js";
2
- import { getCanvasMethods } from "@/lib/canvas/getCanvasMethods.js";
3
2
  import { setupCanvasContainer } from "@/lib/canvas/setupCanvasContainer.js";
4
3
  import { DEFAULT_ZOOM } from "@/lib/constants.js";
4
+ import { withFeatureEnabled } from "@/lib/helpers/index.js";
5
5
  import { createMatrix } from "@/lib/matrix/createMatrix.js";
6
6
  import { applyTransform, enableHardwareAcceleration } from "@/lib/transform/index.js";
7
- import type { BaseCanvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
7
+ import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
8
8
 
9
- // Creates and initializes a canvas with the required DOM structure
10
- export function createCanvas(container: HTMLElement, config: Required<MarkupCanvasConfig>): BaseCanvas | null {
9
+ export function createCanvas(container: HTMLElement, config: Required<MarkupCanvasConfig>): Canvas | null {
11
10
  if (!container?.appendChild) {
12
11
  console.error("Invalid container element provided to createCanvas");
13
12
  return null;
@@ -18,10 +17,9 @@ export function createCanvas(container: HTMLElement, config: Required<MarkupCanv
18
17
 
19
18
  const { transformLayer, contentLayer } = createCanvasLayers(container, config);
20
19
 
21
- // Enable hardware acceleration if requested
22
- if (config.enableAcceleration) {
20
+ withFeatureEnabled(config, "enableAcceleration", () => {
23
21
  enableHardwareAcceleration(transformLayer);
24
- }
22
+ });
25
23
 
26
24
  const rulerOffset = config.enableRulers ? -config.rulerSize : 0;
27
25
 
@@ -36,20 +34,14 @@ export function createCanvas(container: HTMLElement, config: Required<MarkupCanv
36
34
 
37
35
  applyTransform(transformLayer, initialMatrix);
38
36
 
39
- const canvas: BaseCanvas = {
37
+ const canvas: Canvas = {
40
38
  // DOM references
41
39
  container,
42
40
  transformLayer,
43
41
  contentLayer,
44
42
 
45
- // Configuration
46
- config: config,
47
-
48
43
  // Current state
49
44
  transform: initialTransform,
50
-
51
- // Add all canvas methods
52
- ...getCanvasMethods(),
53
45
  };
54
46
 
55
47
  return canvas;
@@ -0,0 +1,27 @@
1
+ import { updateTransform } from "@/lib/actions/transform/updateTransform.js";
2
+ import { getCanvasBounds } from "@/lib/canvas/getCanvasBounds.js";
3
+ import { ZOOM_FIT_PADDING } from "@/lib/constants.js";
4
+ import { withClampedZoom } from "@/lib/helpers/index.js";
5
+ import { withTransition } from "@/lib/transition/index.js";
6
+ import type { Canvas, MarkupCanvasConfig } from "@/types/index.js";
7
+
8
+ export function fitToScreen(canvas: Canvas, transformLayer: HTMLElement, config: Required<MarkupCanvasConfig>): boolean {
9
+ return withTransition(transformLayer, config, () => {
10
+ const bounds = getCanvasBounds(canvas, config);
11
+ const scaleX = bounds.width / config.width;
12
+ const scaleY = bounds.height / config.height;
13
+ const fitScale = withClampedZoom(config, (clamp) => clamp(Math.min(scaleX, scaleY) * ZOOM_FIT_PADDING));
14
+
15
+ // Center the content
16
+ const scaledWidth = config.width * fitScale;
17
+ const scaledHeight = config.height * fitScale;
18
+ const centerX = (bounds.width - scaledWidth) / 2;
19
+ const centerY = (bounds.height - scaledHeight) / 2;
20
+
21
+ return updateTransform(canvas, {
22
+ scale: fitScale,
23
+ translateX: centerX,
24
+ translateY: centerY,
25
+ });
26
+ });
27
+ }
@@ -1,13 +1,12 @@
1
- import { calculateVisibleArea } from "@/lib/canvas/calcVisibleArea.js";
1
+ import { calculateVisibleArea } from "@/lib/canvas/calculateVisibleArea.js";
2
2
  import { getEmptyBounds } from "@/lib/canvas/getEmptyBounds.js";
3
3
  import { withRulerSize } from "@/lib/helpers/index.js";
4
- import type { BaseCanvas, CanvasBounds } from "@/types/index.js";
4
+ import type { Canvas, CanvasBounds, MarkupCanvasConfig } from "@/types/index.js";
5
5
  import { DEFAULT_CONFIG } from "../config/constants";
6
6
 
7
- export function getCanvasBounds(canvas: BaseCanvas): CanvasBounds {
7
+ export function getCanvasBounds(canvas: Canvas, config: Required<MarkupCanvasConfig>): CanvasBounds {
8
8
  try {
9
9
  const container = canvas.container;
10
- const config = canvas.config;
11
10
  const transform = canvas.transform || {
12
11
  scale: 1.0,
13
12
  translateX: 0,
@@ -1,3 +1,3 @@
1
- export { createCanvasConfig } from "./config.js";
2
1
  export { createCanvas } from "./createCanvas.js";
2
+ export { fitToScreen } from "./fitToScreen.js";
3
3
  export { getCanvasBounds } from "./getCanvasBounds.js";
@@ -6,8 +6,7 @@ export const DEFAULT_CONFIG: Required<MarkupCanvasConfig> = {
6
6
  height: 8000,
7
7
  enableAcceleration: true,
8
8
 
9
- // Global Binding & Instance Access
10
- bindToWindow: false,
9
+ // Global Instance Access
11
10
  name: "markupCanvas",
12
11
  enablePostMessageAPI: false,
13
12
 
@@ -16,7 +15,7 @@ export const DEFAULT_CONFIG: Required<MarkupCanvasConfig> = {
16
15
  enablePan: true,
17
16
  enableTouch: true,
18
17
  enableKeyboard: true,
19
- bindKeyboardEventsTo: "canvas",
18
+ bindKeyboardEventsTo: "document",
20
19
 
21
20
  // Zoom behavior
22
21
  zoomSpeed: 1.5,
@@ -71,7 +70,4 @@ export const DEFAULT_CONFIG: Required<MarkupCanvasConfig> = {
71
70
 
72
71
  // Theme
73
72
  themeMode: "light",
74
-
75
- // Callbacks
76
- onTransformUpdate: () => {},
77
73
  };
@@ -6,8 +6,7 @@ export const EDITOR_PRESET: Required<MarkupCanvasConfig> = {
6
6
  height: 4000,
7
7
  enableAcceleration: true,
8
8
 
9
- // Global Binding & Instance Access
10
- bindToWindow: true,
9
+ // Global Instance Access
11
10
  name: "canvas",
12
11
  enablePostMessageAPI: true,
13
12
 
@@ -43,7 +42,7 @@ export const EDITOR_PRESET: Required<MarkupCanvasConfig> = {
43
42
 
44
43
  // Visual elements
45
44
  enableRulers: true,
46
- enableGrid: false,
45
+ enableGrid: true,
47
46
  showRulers: true,
48
47
  showGrid: false,
49
48
 
@@ -72,7 +71,4 @@ export const EDITOR_PRESET: Required<MarkupCanvasConfig> = {
72
71
 
73
72
  // Theme
74
73
  themeMode: "light",
75
-
76
- // Callbacks
77
- onTransformUpdate: () => {},
78
74
  };
@@ -0,0 +1,9 @@
1
+ import type { EventEmitter } from "@/lib/events/EventEmitter.js";
2
+ import type { Canvas, MarkupCanvasEvents } from "@/types/index.js";
3
+
4
+ export function emitTransformEvents(listen: EventEmitter<MarkupCanvasEvents>, canvas: Canvas): void {
5
+ const transform = canvas.transform;
6
+ listen.emit("transform", transform);
7
+ listen.emit("zoom", transform.scale);
8
+ listen.emit("pan", { x: transform.translateX, y: transform.translateY });
9
+ }
@@ -1,9 +1,10 @@
1
1
  import { updateCursor } from "@/lib/events/utils/updateCursor.js";
2
- import type { Canvas, MarkupCanvasConfig } from "@/types/index.js";
2
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
3
+ import type { MarkupCanvasConfig } from "@/types/index.js";
3
4
 
4
5
  export function handleKeyDown(
5
6
  event: KeyboardEvent,
6
- canvas: Canvas,
7
+ canvas: MarkupCanvas,
7
8
  config: Required<MarkupCanvasConfig>,
8
9
  isDragEnabled: boolean,
9
10
  isDragging: boolean,
@@ -1,10 +1,11 @@
1
1
  import { resetDragState } from "@/lib/events/utils/resetDragState.js";
2
2
  import { updateCursor } from "@/lib/events/utils/updateCursor.js";
3
- import type { Canvas, MarkupCanvasConfig } from "@/types/index.js";
3
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
4
+ import type { MarkupCanvasConfig } from "@/types/index.js";
4
5
 
5
6
  export function handleKeyUp(
6
7
  event: KeyboardEvent,
7
- canvas: Canvas,
8
+ canvas: MarkupCanvas,
8
9
  config: Required<MarkupCanvasConfig>,
9
10
  isDragEnabled: boolean,
10
11
  isDragging: boolean,
@@ -1,7 +1,8 @@
1
1
  import { getAdaptiveZoomSpeed } from "@/lib/events/utils/getAdaptiveZoomSpeed.js";
2
- import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
2
+ import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
3
+ import type { MarkupCanvasConfig, Transform } from "@/types/index.js";
3
4
 
4
- export function setupKeyboardEvents(canvas: Canvas, config: Required<MarkupCanvasConfig>): () => void {
5
+ export function setupKeyboardEvents(canvas: MarkupCanvas, config: Required<MarkupCanvasConfig>): () => void {
5
6
  function handleKeyDown(event: Event): void {
6
7
  if (!(event instanceof KeyboardEvent)) return;
7
8