canvas-react-easy 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect } from "react";
3
2
  import Tools from "./Tools";
4
3
  import Canvas from "./Canvas";
5
- import toolState from "../store/ToolState";
6
- import CanvasState from "../store/CanvasState";
7
- import { TextHoverTool } from "../tools/TextHoverTool";
8
4
  const Border = ({ userName = "Гость" }) => {
9
- useEffect(() => {
10
- var _a;
11
- // активируем инструмент "имя пользователя" сразу после монтирования
12
- if (CanvasState.canvas) {
13
- (_a = toolState.tool) === null || _a === void 0 ? void 0 : _a.clearEvents();
14
- const tool = new TextHoverTool(CanvasState.canvas, userName);
15
- tool.listen();
16
- toolState.setTool(tool);
17
- }
18
- }, [userName]);
19
- return (_jsxs("div", { style: { position: "relative" }, children: [_jsx(Tools, { userName: userName }), _jsx(Canvas, {})] }));
5
+ return (_jsxs("div", { style: { position: "relative" }, children: [_jsx(Tools, { userName: userName }), _jsx(Canvas, { userName: userName })] }));
20
6
  };
21
7
  export default Border;
@@ -3,14 +3,28 @@ import { useEffect, useRef } from "react";
3
3
  import CanvasState from "../store/CanvasState";
4
4
  import toolState from "../store/ToolState";
5
5
  import { Brush } from "../tools/Brush";
6
- const Canvas = () => {
6
+ import TextHoverTool from "../tools/TextHoverTool";
7
+ const Canvas = ({ userName = "Гость" }) => {
7
8
  const canvasRef = useRef(null);
8
9
  useEffect(() => {
9
- if (canvasRef.current) {
10
- CanvasState.setCanvas(canvasRef.current);
11
- toolState.setTool(new Brush(canvasRef.current));
12
- }
13
- }, []);
14
- return _jsx("canvas", { ref: canvasRef, width: 1600, height: 900 });
10
+ const canvas = canvasRef.current;
11
+ if (!canvas)
12
+ return;
13
+ CanvasState.setCanvas(canvas);
14
+ // По умолчанию кисть
15
+ const brush = new Brush(canvas);
16
+ toolState.setTool(brush);
17
+ // Подключаем TextHoverTool сразу после монтирования, если нужно
18
+ const hoverTool = new TextHoverTool(canvas, userName);
19
+ hoverTool.listen();
20
+ // Если нужно оставить hoverTool активным
21
+ toolState.setTool(hoverTool);
22
+ // Очистка при размонтировании
23
+ return () => {
24
+ hoverTool.clearEvents();
25
+ brush.clearEvents();
26
+ };
27
+ }, [userName]);
28
+ return (_jsx("canvas", { ref: canvasRef, width: 1600, height: 900, style: { border: "1px solid black" } }));
15
29
  };
16
30
  export default Canvas;
@@ -7,16 +7,13 @@ import { Circle } from "../tools/Circle";
7
7
  import { TextTool } from "../tools/Text";
8
8
  import { ImageTool } from "../tools/ImageTool";
9
9
  const Tools = ({ userName = "Гость" }) => {
10
- // универсальный выбор инструмента
11
10
  const selectTool = (ToolClass) => {
12
11
  var _a, _b;
13
12
  if (!CanvasState.canvas)
14
13
  return;
15
- // очищаем старый инструмент
16
14
  (_a = toolState.tool) === null || _a === void 0 ? void 0 : _a.clearEvents();
17
- // создаём новый инструмент и подключаем слушатели
18
15
  const tool = new ToolClass(CanvasState.canvas);
19
- (_b = tool.listen) === null || _b === void 0 ? void 0 : _b.call(tool); // если инструмент имеет метод listen
16
+ (_b = tool.listen) === null || _b === void 0 ? void 0 : _b.call(tool);
20
17
  toolState.setTool(tool);
21
18
  };
22
19
  return (_jsxs("div", { style: { marginBottom: "10px", display: "flex", gap: "5px" }, children: [_jsx("button", { onClick: () => selectTool(Brush), children: "\u041A\u0438\u0441\u0442\u044C" }), _jsx("button", { onClick: () => selectTool(Rectangle), children: "\u041F\u0440\u044F\u043C\u043E\u0443\u0433\u043E\u043B\u044C\u043D\u0438\u043A" }), _jsx("button", { onClick: () => selectTool(Circle), children: "\u041A\u0440\u0443\u0433" }), _jsx("button", { onClick: () => selectTool(TextTool), children: "\u0422\u0435\u043A\u0441\u0442" }), _jsx("button", { onClick: () => selectTool(ImageTool), children: "\u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u0435" })] }));
@@ -1,2 +1,6 @@
1
- declare const Canvas: () => import("react/jsx-runtime").JSX.Element;
1
+ import React from "react";
2
+ interface CanvasProps {
3
+ userName?: string;
4
+ }
5
+ declare const Canvas: React.FC<CanvasProps>;
2
6
  export default Canvas;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-react-easy",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "license": "MIT",
@@ -3,27 +3,17 @@ import Tools from "./Tools";
3
3
  import Canvas from "./Canvas";
4
4
  import toolState from "../store/ToolState";
5
5
  import CanvasState from "../store/CanvasState";
6
- import { TextHoverTool } from "../tools/TextHoverTool";
6
+ import TextHoverTool from "../tools/TextHoverTool";
7
7
 
8
8
  interface BorderProps {
9
9
  userName?: string;
10
10
  }
11
11
 
12
12
  const Border: React.FC<BorderProps> = ({ userName = "Гость" }) => {
13
- useEffect(() => {
14
- // активируем инструмент "имя пользователя" сразу после монтирования
15
- if (CanvasState.canvas) {
16
- toolState.tool?.clearEvents();
17
- const tool = new TextHoverTool(CanvasState.canvas, userName);
18
- tool.listen();
19
- toolState.setTool(tool);
20
- }
21
- }, [userName]);
22
-
23
13
  return (
24
14
  <div style={{ position: "relative" }}>
25
15
  <Tools userName={userName} />
26
- <Canvas />
16
+ <Canvas userName={userName} />
27
17
  </div>
28
18
  );
29
19
  };
@@ -1,19 +1,48 @@
1
- import { useEffect, useRef } from "react";
1
+ import React, { useEffect, useRef } from "react";
2
2
  import CanvasState from "../store/CanvasState";
3
3
  import toolState from "../store/ToolState";
4
4
  import { Brush } from "../tools/Brush";
5
+ import TextHoverTool from "../tools/TextHoverTool";
5
6
 
6
- const Canvas = () => {
7
+ interface CanvasProps {
8
+ userName?: string;
9
+ }
10
+
11
+ const Canvas: React.FC<CanvasProps> = ({ userName = "Гость" }) => {
7
12
  const canvasRef = useRef<HTMLCanvasElement>(null);
8
13
 
9
14
  useEffect(() => {
10
- if (canvasRef.current) {
11
- CanvasState.setCanvas(canvasRef.current);
12
- toolState.setTool(new Brush(canvasRef.current));
13
- }
14
- }, []);
15
+ const canvas = canvasRef.current;
16
+ if (!canvas) return;
17
+
18
+ CanvasState.setCanvas(canvas);
19
+
20
+ // По умолчанию кисть
21
+ const brush = new Brush(canvas);
22
+ toolState.setTool(brush);
23
+
24
+ // Подключаем TextHoverTool сразу после монтирования, если нужно
25
+ const hoverTool = new TextHoverTool(canvas, userName);
26
+ hoverTool.listen();
27
+
28
+ // Если нужно оставить hoverTool активным
29
+ toolState.setTool(hoverTool);
30
+
31
+ // Очистка при размонтировании
32
+ return () => {
33
+ hoverTool.clearEvents();
34
+ brush.clearEvents();
35
+ };
36
+ }, [userName]);
15
37
 
16
- return <canvas ref={canvasRef} width={1600} height={900} />;
38
+ return (
39
+ <canvas
40
+ ref={canvasRef}
41
+ width={1600}
42
+ height={900}
43
+ style={{ border: "1px solid black" }}
44
+ />
45
+ );
17
46
  };
18
47
 
19
48
  export default Canvas;
@@ -12,16 +12,13 @@ interface ToolsProps {
12
12
  }
13
13
 
14
14
  const Tools: React.FC<ToolsProps> = ({ userName = "Гость" }) => {
15
- // универсальный выбор инструмента
16
15
  const selectTool = (ToolClass: any) => {
17
16
  if (!CanvasState.canvas) return;
18
17
 
19
- // очищаем старый инструмент
20
18
  toolState.tool?.clearEvents();
21
19
 
22
- // создаём новый инструмент и подключаем слушатели
23
20
  const tool = new ToolClass(CanvasState.canvas);
24
- tool.listen?.(); // если инструмент имеет метод listen
21
+ tool.listen?.();
25
22
  toolState.setTool(tool);
26
23
  };
27
24