Spaces:
Sleeping
Sleeping
Commit
·
4f0c4ca
1
Parent(s):
c555902
add initial project structure with essential files and configurations
Browse files- .599ce698a84d43160da884347f22f6b77d0c8415-ssh-auth.sock +1 -0
- freeform/.gitignore +23 -0
- freeform/README.md +46 -0
- freeform/package-lock.json +0 -0
- freeform/package.json +37 -0
- freeform/public/favicon.ico +0 -0
- freeform/public/index.html +43 -0
- freeform/public/logo192.png +0 -0
- freeform/public/logo512.png +0 -0
- freeform/public/manifest.json +25 -0
- freeform/public/robots.txt +3 -0
- freeform/src/App.css +38 -0
- freeform/src/App.test.tsx +9 -0
- freeform/src/App.tsx +293 -0
- freeform/src/core/Canvas.ts +52 -0
- freeform/src/core/History.ts +40 -0
- freeform/src/core/StickyNote.ts +96 -0
- freeform/src/core/buffer/BufferManager.ts +63 -0
- freeform/src/core/constants.ts +19 -0
- freeform/src/core/events/Event.ts +56 -0
- freeform/src/core/interfaces.ts +46 -0
- freeform/src/core/logger/Logger.ts +78 -0
- freeform/src/core/spatial/Grid.ts +63 -0
- freeform/src/core/utility/AsyncOperation.ts +55 -0
- freeform/src/core/utility/LazyLoader.ts +43 -0
- freeform/src/index.css +13 -0
- freeform/src/index.tsx +19 -0
- freeform/src/logo.svg +1 -0
- freeform/src/reportWebVitals.ts +15 -0
- freeform/src/setupTests.ts +5 -0
- freeform/yarn.lock +0 -0
- schemas/freeform.fbs +29 -0
.599ce698a84d43160da884347f22f6b77d0c8415-ssh-auth.sock
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/home/user/.windsurf-server/.599ce698a84d43160da884347f22f6b77d0c8415-ssh-auth.sock
|
freeform/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
2 |
+
|
3 |
+
# dependencies
|
4 |
+
/node_modules
|
5 |
+
/.pnp
|
6 |
+
.pnp.js
|
7 |
+
|
8 |
+
# testing
|
9 |
+
/coverage
|
10 |
+
|
11 |
+
# production
|
12 |
+
/build
|
13 |
+
|
14 |
+
# misc
|
15 |
+
.DS_Store
|
16 |
+
.env.local
|
17 |
+
.env.development.local
|
18 |
+
.env.test.local
|
19 |
+
.env.production.local
|
20 |
+
|
21 |
+
npm-debug.log*
|
22 |
+
yarn-debug.log*
|
23 |
+
yarn-error.log*
|
freeform/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Getting Started with Create React App
|
2 |
+
|
3 |
+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
4 |
+
|
5 |
+
## Available Scripts
|
6 |
+
|
7 |
+
In the project directory, you can run:
|
8 |
+
|
9 |
+
### `npm start`
|
10 |
+
|
11 |
+
Runs the app in the development mode.\
|
12 |
+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
13 |
+
|
14 |
+
The page will reload if you make edits.\
|
15 |
+
You will also see any lint errors in the console.
|
16 |
+
|
17 |
+
### `npm test`
|
18 |
+
|
19 |
+
Launches the test runner in the interactive watch mode.\
|
20 |
+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
21 |
+
|
22 |
+
### `npm run build`
|
23 |
+
|
24 |
+
Builds the app for production to the `build` folder.\
|
25 |
+
It correctly bundles React in production mode and optimizes the build for the best performance.
|
26 |
+
|
27 |
+
The build is minified and the filenames include the hashes.\
|
28 |
+
Your app is ready to be deployed!
|
29 |
+
|
30 |
+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
31 |
+
|
32 |
+
### `npm run eject`
|
33 |
+
|
34 |
+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
35 |
+
|
36 |
+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
37 |
+
|
38 |
+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
39 |
+
|
40 |
+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
41 |
+
|
42 |
+
## Learn More
|
43 |
+
|
44 |
+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
45 |
+
|
46 |
+
To learn React, check out the [React documentation](https://reactjs.org/).
|
freeform/package-lock.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
freeform/package.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "freeform",
|
3 |
+
"version": "0.1.0",
|
4 |
+
"private": true,
|
5 |
+
"dependencies": {
|
6 |
+
"cra-template-typescript": "1.2.0",
|
7 |
+
"lucide-react": "^0.469.0",
|
8 |
+
"react": "^19.0.0",
|
9 |
+
"react-dom": "^19.0.0",
|
10 |
+
"react-scripts": "5.0.1",
|
11 |
+
"tailwindcss": "^3.4.17"
|
12 |
+
},
|
13 |
+
"scripts": {
|
14 |
+
"start": "react-scripts start",
|
15 |
+
"build": "react-scripts build",
|
16 |
+
"test": "react-scripts test",
|
17 |
+
"eject": "react-scripts eject"
|
18 |
+
},
|
19 |
+
"eslintConfig": {
|
20 |
+
"extends": [
|
21 |
+
"react-app",
|
22 |
+
"react-app/jest"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
"browserslist": {
|
26 |
+
"production": [
|
27 |
+
">0.2%",
|
28 |
+
"not dead",
|
29 |
+
"not op_mini all"
|
30 |
+
],
|
31 |
+
"development": [
|
32 |
+
"last 1 chrome version",
|
33 |
+
"last 1 firefox version",
|
34 |
+
"last 1 safari version"
|
35 |
+
]
|
36 |
+
}
|
37 |
+
}
|
freeform/public/favicon.ico
ADDED
|
freeform/public/index.html
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8" />
|
5 |
+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
|
6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
7 |
+
<meta name="theme-color" content="#000000" />
|
8 |
+
<meta
|
9 |
+
name="description"
|
10 |
+
content="Web site created using create-react-app"
|
11 |
+
/>
|
12 |
+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
|
13 |
+
<!--
|
14 |
+
manifest.json provides metadata used when your web app is installed on a
|
15 |
+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
16 |
+
-->
|
17 |
+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
18 |
+
<!--
|
19 |
+
Notice the use of %PUBLIC_URL% in the tags above.
|
20 |
+
It will be replaced with the URL of the `public` folder during the build.
|
21 |
+
Only files inside the `public` folder can be referenced from the HTML.
|
22 |
+
|
23 |
+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
24 |
+
work correctly both with client-side routing and a non-root public URL.
|
25 |
+
Learn how to configure a non-root public URL by running `npm run build`.
|
26 |
+
-->
|
27 |
+
<title>React App</title>
|
28 |
+
</head>
|
29 |
+
<body>
|
30 |
+
<noscript>You need to enable JavaScript to run this app.</noscript>
|
31 |
+
<div id="root"></div>
|
32 |
+
<!--
|
33 |
+
This HTML file is a template.
|
34 |
+
If you open it directly in the browser, you will see an empty page.
|
35 |
+
|
36 |
+
You can add webfonts, meta tags, or analytics to this file.
|
37 |
+
The build step will place the bundled scripts into the <body> tag.
|
38 |
+
|
39 |
+
To begin the development, run `npm start` or `yarn start`.
|
40 |
+
To create a production bundle, use `npm run build` or `yarn build`.
|
41 |
+
-->
|
42 |
+
</body>
|
43 |
+
</html>
|
freeform/public/logo192.png
ADDED
![]() |
freeform/public/logo512.png
ADDED
![]() |
freeform/public/manifest.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"short_name": "React App",
|
3 |
+
"name": "Create React App Sample",
|
4 |
+
"icons": [
|
5 |
+
{
|
6 |
+
"src": "favicon.ico",
|
7 |
+
"sizes": "64x64 32x32 24x24 16x16",
|
8 |
+
"type": "image/x-icon"
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"src": "logo192.png",
|
12 |
+
"type": "image/png",
|
13 |
+
"sizes": "192x192"
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"src": "logo512.png",
|
17 |
+
"type": "image/png",
|
18 |
+
"sizes": "512x512"
|
19 |
+
}
|
20 |
+
],
|
21 |
+
"start_url": ".",
|
22 |
+
"display": "standalone",
|
23 |
+
"theme_color": "#000000",
|
24 |
+
"background_color": "#ffffff"
|
25 |
+
}
|
freeform/public/robots.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# https://www.robotstxt.org/robotstxt.html
|
2 |
+
User-agent: *
|
3 |
+
Disallow:
|
freeform/src/App.css
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.App {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
.App-logo {
|
6 |
+
height: 40vmin;
|
7 |
+
pointer-events: none;
|
8 |
+
}
|
9 |
+
|
10 |
+
@media (prefers-reduced-motion: no-preference) {
|
11 |
+
.App-logo {
|
12 |
+
animation: App-logo-spin infinite 20s linear;
|
13 |
+
}
|
14 |
+
}
|
15 |
+
|
16 |
+
.App-header {
|
17 |
+
background-color: #282c34;
|
18 |
+
min-height: 100vh;
|
19 |
+
display: flex;
|
20 |
+
flex-direction: column;
|
21 |
+
align-items: center;
|
22 |
+
justify-content: center;
|
23 |
+
font-size: calc(10px + 2vmin);
|
24 |
+
color: white;
|
25 |
+
}
|
26 |
+
|
27 |
+
.App-link {
|
28 |
+
color: #61dafb;
|
29 |
+
}
|
30 |
+
|
31 |
+
@keyframes App-logo-spin {
|
32 |
+
from {
|
33 |
+
transform: rotate(0deg);
|
34 |
+
}
|
35 |
+
to {
|
36 |
+
transform: rotate(360deg);
|
37 |
+
}
|
38 |
+
}
|
freeform/src/App.test.tsx
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React from 'react';
|
2 |
+
import { render, screen } from '@testing-library/react';
|
3 |
+
import App from './App';
|
4 |
+
|
5 |
+
test('renders learn react link', () => {
|
6 |
+
render(<App />);
|
7 |
+
const linkElement = screen.getByText(/learn react/i);
|
8 |
+
expect(linkElement).toBeInTheDocument();
|
9 |
+
});
|
freeform/src/App.tsx
ADDED
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React, { useState, useEffect, useCallback, useRef } from 'react';
|
2 |
+
import {
|
3 |
+
ChevronLeft, Undo, Redo, Share, Grid3X3, MoreHorizontal,
|
4 |
+
Pen, FileText, Square, TextCursor, ImagePlus,
|
5 |
+
Plus, Minus
|
6 |
+
} from 'lucide-react';
|
7 |
+
|
8 |
+
import { Canvas } from './core/Canvas';
|
9 |
+
import { StickyNote } from './core/StickyNote';
|
10 |
+
import { Grid } from './core/spatial/Grid';
|
11 |
+
import { History } from './core/History';
|
12 |
+
import { Logger } from './core/logger/Logger';
|
13 |
+
import { BufferManager } from './core/buffer/BufferManager';
|
14 |
+
import { LazyLoader } from './core/utility/LazyLoader';
|
15 |
+
import { AsyncOperation } from './core/utility/AsyncOperation';
|
16 |
+
import { EventEmitter, EventType, Event } from './core/events/Event';
|
17 |
+
|
18 |
+
// Constants
|
19 |
+
const GRID_SIZE = 20;
|
20 |
+
const STICKY_BUFFER_SIZE = 1000;
|
21 |
+
const HISTORY_BUFFER_SIZE = 100;
|
22 |
+
|
23 |
+
// Initialize core services
|
24 |
+
const logger = Logger.getInstance();
|
25 |
+
const eventEmitter = new EventEmitter();
|
26 |
+
const stickyBuffer = new BufferManager<StickyNote>(STICKY_BUFFER_SIZE);
|
27 |
+
const spatialGrid = new Grid(GRID_SIZE);
|
28 |
+
const history = new History();
|
29 |
+
|
30 |
+
export default function App() {
|
31 |
+
// Core state
|
32 |
+
const [canvas] = useState(() => new Canvas());
|
33 |
+
const [zoom, setZoom] = useState(1);
|
34 |
+
const [entities, setEntities] = useState<StickyNote[]>([]);
|
35 |
+
const canvasRef = useRef<HTMLDivElement>(null);
|
36 |
+
|
37 |
+
// Initialize lazy loaded resources
|
38 |
+
const assetsLoader = new LazyLoader(async () => {
|
39 |
+
// Load any required assets
|
40 |
+
await new Promise(resolve => setTimeout(resolve, 100));
|
41 |
+
return {};
|
42 |
+
});
|
43 |
+
|
44 |
+
// Event handlers
|
45 |
+
const handleCanvasOperation = useCallback(async (operation: () => Promise<void>) => {
|
46 |
+
const asyncOp = new AsyncOperation(5000);
|
47 |
+
try {
|
48 |
+
await operation();
|
49 |
+
asyncOp.complete(undefined);
|
50 |
+
} catch (error) {
|
51 |
+
logger.error('Canvas operation failed', { error });
|
52 |
+
asyncOp.abort(error);
|
53 |
+
}
|
54 |
+
}, []);
|
55 |
+
|
56 |
+
// Canvas event handlers
|
57 |
+
const handleZoomChange = useCallback((newZoom: number) => {
|
58 |
+
handleCanvasOperation(async () => {
|
59 |
+
canvas.setZoom(newZoom);
|
60 |
+
setZoom(newZoom);
|
61 |
+
|
62 |
+
// Emit zoom event
|
63 |
+
eventEmitter.emit(new Event(
|
64 |
+
EventType.Canvas,
|
65 |
+
'zoom',
|
66 |
+
{ zoom: newZoom }
|
67 |
+
));
|
68 |
+
|
69 |
+
// Save state
|
70 |
+
history.pushState({
|
71 |
+
entities,
|
72 |
+
canvasOffset: canvas.position,
|
73 |
+
zoom: newZoom
|
74 |
+
});
|
75 |
+
|
76 |
+
logger.info('Canvas zoomed', { zoom: newZoom });
|
77 |
+
});
|
78 |
+
}, [canvas, entities]);
|
79 |
+
|
80 |
+
const handleStickyCreate = useCallback(() => {
|
81 |
+
handleCanvasOperation(async () => {
|
82 |
+
const newSticky = new StickyNote({
|
83 |
+
id: Date.now().toString(),
|
84 |
+
type: 'sticky',
|
85 |
+
position: { x: 200, y: 200 },
|
86 |
+
size: { width: 9 * GRID_SIZE, height: 9 * GRID_SIZE },
|
87 |
+
content: 'Chạm hai lần để nhập văn bản',
|
88 |
+
color: '#FFE082',
|
89 |
+
selected: true
|
90 |
+
});
|
91 |
+
|
92 |
+
// Add to buffer and spatial grid
|
93 |
+
stickyBuffer.write(newSticky);
|
94 |
+
spatialGrid.insert(
|
95 |
+
newSticky.id,
|
96 |
+
newSticky.position,
|
97 |
+
newSticky.size
|
98 |
+
);
|
99 |
+
|
100 |
+
setEntities(prev => [...prev.map(e => ({...e, selected: false})), newSticky]);
|
101 |
+
|
102 |
+
// Emit create event
|
103 |
+
eventEmitter.emit(new Event(
|
104 |
+
EventType.Sticky,
|
105 |
+
'create',
|
106 |
+
{ sticky: newSticky }
|
107 |
+
));
|
108 |
+
|
109 |
+
// Save state
|
110 |
+
history.pushState({
|
111 |
+
entities: [...entities, newSticky],
|
112 |
+
canvasOffset: canvas.position,
|
113 |
+
zoom
|
114 |
+
});
|
115 |
+
|
116 |
+
logger.info('Sticky note created', { sticky: newSticky });
|
117 |
+
});
|
118 |
+
}, [canvas, entities, zoom]);
|
119 |
+
|
120 |
+
// History operations
|
121 |
+
const handleUndo = useCallback(() => {
|
122 |
+
const prevState = history.undo();
|
123 |
+
if (prevState) {
|
124 |
+
setEntities(prevState.entities);
|
125 |
+
canvas.updatePosition(prevState.canvasOffset.x, prevState.canvasOffset.y);
|
126 |
+
setZoom(prevState.zoom);
|
127 |
+
logger.info('Undo performed');
|
128 |
+
}
|
129 |
+
}, [canvas]);
|
130 |
+
|
131 |
+
const handleRedo = useCallback(() => {
|
132 |
+
const nextState = history.redo();
|
133 |
+
if (nextState) {
|
134 |
+
setEntities(nextState.entities);
|
135 |
+
canvas.updatePosition(nextState.canvasOffset.x, nextState.canvasOffset.y);
|
136 |
+
setZoom(nextState.zoom);
|
137 |
+
logger.info('Redo performed');
|
138 |
+
}
|
139 |
+
}, [canvas]);
|
140 |
+
|
141 |
+
// Initialize
|
142 |
+
useEffect(() => {
|
143 |
+
const loadAssets = async () => {
|
144 |
+
try {
|
145 |
+
await assetsLoader.get();
|
146 |
+
logger.info('Assets loaded successfully');
|
147 |
+
} catch (error) {
|
148 |
+
logger.error('Failed to load assets', { error });
|
149 |
+
}
|
150 |
+
};
|
151 |
+
loadAssets();
|
152 |
+
|
153 |
+
// Subscribe to events
|
154 |
+
const unsubscribe = eventEmitter.subscribe(
|
155 |
+
EventType.Sticky,
|
156 |
+
(event) => logger.debug('Sticky event received', { event })
|
157 |
+
);
|
158 |
+
|
159 |
+
return () => {
|
160 |
+
unsubscribe();
|
161 |
+
logger.info('App cleanup performed');
|
162 |
+
};
|
163 |
+
}, []);
|
164 |
+
|
165 |
+
return (
|
166 |
+
<div className="h-screen w-screen bg-gray-50 flex flex-col">
|
167 |
+
{/* Top Toolbar */}
|
168 |
+
<div className="h-12 border-b flex items-center justify-between px-4 bg-white shadow-sm">
|
169 |
+
<div className="flex items-center space-x-4">
|
170 |
+
<button className="hover:bg-gray-100 p-2 rounded-full">
|
171 |
+
<ChevronLeft className="w-5 h-5 text-blue-500" />
|
172 |
+
</button>
|
173 |
+
<button
|
174 |
+
className="hover:bg-gray-100 p-2 rounded-full"
|
175 |
+
onClick={handleUndo}
|
176 |
+
disabled={!history.canUndo()}
|
177 |
+
>
|
178 |
+
<Undo className={`w-5 h-5 ${!history.canUndo() ? 'text-gray-300' : 'text-gray-500'}`} />
|
179 |
+
</button>
|
180 |
+
<button
|
181 |
+
className="hover:bg-gray-100 p-2 rounded-full"
|
182 |
+
onClick={handleRedo}
|
183 |
+
disabled={!history.canRedo()}
|
184 |
+
>
|
185 |
+
<Redo className={`w-5 h-5 ${!history.canRedo() ? 'text-gray-300' : 'text-gray-500'}`} />
|
186 |
+
</button>
|
187 |
+
</div>
|
188 |
+
|
189 |
+
{/* Right toolbar items */}
|
190 |
+
<div className="flex items-center space-x-4">
|
191 |
+
<button className="hover:bg-gray-100 p-2 rounded-full">
|
192 |
+
<Share className="w-5 h-5 text-blue-500" />
|
193 |
+
</button>
|
194 |
+
<button className="hover:bg-gray-100 p-2 rounded-full">
|
195 |
+
<Grid3X3 className="w-5 h-5 text-blue-500" />
|
196 |
+
</button>
|
197 |
+
<button className="hover:bg-gray-100 p-2 rounded-full">
|
198 |
+
<MoreHorizontal className="w-5 h-5 text-blue-500" />
|
199 |
+
</button>
|
200 |
+
<button className="text-blue-500 font-medium px-3 py-1 hover:bg-blue-50 rounded">
|
201 |
+
Xong
|
202 |
+
</button>
|
203 |
+
</div>
|
204 |
+
</div>
|
205 |
+
|
206 |
+
{/* Zoom Controls */}
|
207 |
+
<div className="absolute top-20 right-6 bg-white rounded-lg shadow-lg border p-2 space-y-2">
|
208 |
+
<button
|
209 |
+
onClick={() => handleZoomChange(zoom + 0.1)}
|
210 |
+
className="w-8 h-8 flex items-center justify-center hover:bg-gray-100 rounded"
|
211 |
+
>
|
212 |
+
<Plus className="w-5 h-5 text-gray-600" />
|
213 |
+
</button>
|
214 |
+
<div className="text-center text-sm text-gray-600">
|
215 |
+
{Math.round(zoom * 100)}%
|
216 |
+
</div>
|
217 |
+
<button
|
218 |
+
onClick={() => handleZoomChange(zoom - 0.1)}
|
219 |
+
className="w-8 h-8 flex items-center justify-center hover:bg-gray-100 rounded"
|
220 |
+
>
|
221 |
+
<Minus className="w-5 h-5 text-gray-600" />
|
222 |
+
</button>
|
223 |
+
</div>
|
224 |
+
|
225 |
+
{/* Canvas */}
|
226 |
+
<div
|
227 |
+
className="flex-1 relative overflow-hidden cursor-grab active:cursor-grabbing"
|
228 |
+
ref={canvasRef}
|
229 |
+
>
|
230 |
+
<div
|
231 |
+
className="absolute inset-0"
|
232 |
+
style={{
|
233 |
+
width: '5000px',
|
234 |
+
height: '5000px',
|
235 |
+
transform: `scale(${zoom}) translate(${canvas.position.x}px, ${canvas.position.y}px)`,
|
236 |
+
transformOrigin: '0 0',
|
237 |
+
backgroundImage: 'radial-gradient(circle, #ddd 1px, transparent 1px)',
|
238 |
+
backgroundSize: `${GRID_SIZE}px ${GRID_SIZE}px`
|
239 |
+
}}
|
240 |
+
>
|
241 |
+
{entities.map(entity => (
|
242 |
+
<div
|
243 |
+
key={entity.id}
|
244 |
+
className={`absolute shadow-sm rounded-sm transition-shadow
|
245 |
+
${entity.selected ? 'ring-2 ring-blue-500 shadow-md' : ''}`}
|
246 |
+
style={{
|
247 |
+
left: entity.position.x,
|
248 |
+
top: entity.position.y,
|
249 |
+
width: entity.size.width,
|
250 |
+
height: entity.size.height,
|
251 |
+
backgroundColor: entity.color
|
252 |
+
}}
|
253 |
+
>
|
254 |
+
<div className="p-3 text-gray-600 select-none">
|
255 |
+
{entity.content}
|
256 |
+
</div>
|
257 |
+
{entity.selected && (
|
258 |
+
<>
|
259 |
+
<div className="absolute w-3 h-3 bg-blue-500 rounded-full -top-1.5 left-1/2 transform -translate-x-1/2" />
|
260 |
+
<div className="absolute w-3 h-3 bg-blue-500 rounded-full top-1/2 -right-1.5 transform -translate-y-1/2" />
|
261 |
+
<div className="absolute w-3 h-3 bg-blue-500 rounded-full -bottom-1.5 left-1/2 transform -translate-x-1/2" />
|
262 |
+
<div className="absolute w-3 h-3 bg-blue-500 rounded-full top-1/2 -left-1.5 transform -translate-y-1/2" />
|
263 |
+
</>
|
264 |
+
)}
|
265 |
+
</div>
|
266 |
+
))}
|
267 |
+
</div>
|
268 |
+
</div>
|
269 |
+
|
270 |
+
{/* Bottom Toolbar */}
|
271 |
+
<div className="h-16 border-t flex items-center justify-around px-4 bg-white shadow-lg">
|
272 |
+
<button className="w-10 h-10 flex items-center justify-center hover:bg-gray-100 rounded-full">
|
273 |
+
<Pen className="w-6 h-6 text-blue-500" />
|
274 |
+
</button>
|
275 |
+
<button className="w-10 h-10 flex items-center justify-center hover:bg-gray-100 rounded-full">
|
276 |
+
<FileText className="w-6 h-6 text-blue-500" />
|
277 |
+
</button>
|
278 |
+
<button
|
279 |
+
onClick={handleStickyCreate}
|
280 |
+
className="w-10 h-10 flex items-center justify-center hover:bg-gray-100 rounded-full"
|
281 |
+
>
|
282 |
+
<Square className="w-6 h-6 text-blue-500" />
|
283 |
+
</button>
|
284 |
+
<button className="w-10 h-10 flex items-center justify-center hover:bg-gray-100 rounded-full">
|
285 |
+
<TextCursor className="w-6 h-6 text-blue-500" />
|
286 |
+
</button>
|
287 |
+
<button className="w-10 h-10 flex items-center justify-center hover:bg-gray-100 rounded-full">
|
288 |
+
<ImagePlus className="w-6 h-6 text-blue-500" />
|
289 |
+
</button>
|
290 |
+
</div>
|
291 |
+
</div>
|
292 |
+
);
|
293 |
+
}
|
freeform/src/core/Canvas.ts
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Position, Draggable } from './interfaces';
|
2 |
+
import * as Constants from './constants';
|
3 |
+
|
4 |
+
export class Canvas implements Draggable {
|
5 |
+
position: Position = { x: 0, y: 0 };
|
6 |
+
private isDragging: boolean = false;
|
7 |
+
private dragStart: Position = { x: 0, y: 0 };
|
8 |
+
zoom: number = 1;
|
9 |
+
|
10 |
+
constructor() {}
|
11 |
+
|
12 |
+
updatePosition(x: number, y: number): void {
|
13 |
+
this.position = { x, y };
|
14 |
+
}
|
15 |
+
|
16 |
+
onDragStart(e: React.MouseEvent): void {
|
17 |
+
if (e.button === 1 || (e.button === 0 && !e.target.closest('.sticky'))) {
|
18 |
+
this.isDragging = true;
|
19 |
+
this.dragStart = { x: e.clientX, y: e.clientY };
|
20 |
+
}
|
21 |
+
}
|
22 |
+
|
23 |
+
onDrag(e: React.MouseEvent): void {
|
24 |
+
if (!this.isDragging) return;
|
25 |
+
|
26 |
+
const deltaX = e.clientX - this.dragStart.x;
|
27 |
+
const deltaY = e.clientY - this.dragStart.y;
|
28 |
+
|
29 |
+
this.updatePosition(
|
30 |
+
this.position.x + deltaX / this.zoom,
|
31 |
+
this.position.y + deltaY / this.zoom
|
32 |
+
);
|
33 |
+
|
34 |
+
this.dragStart = { x: e.clientX, y: e.clientY };
|
35 |
+
}
|
36 |
+
|
37 |
+
onDragEnd(): void {
|
38 |
+
this.isDragging = false;
|
39 |
+
}
|
40 |
+
|
41 |
+
setZoom(zoom: number): void {
|
42 |
+
this.zoom = Math.min(Math.max(zoom, Constants.MIN_ZOOM), Constants.MAX_ZOOM);
|
43 |
+
}
|
44 |
+
|
45 |
+
zoomIn(): void {
|
46 |
+
this.setZoom(this.zoom + 0.1);
|
47 |
+
}
|
48 |
+
|
49 |
+
zoomOut(): void {
|
50 |
+
this.setZoom(this.zoom - 0.1);
|
51 |
+
}
|
52 |
+
}
|
freeform/src/core/History.ts
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { HistoryState } from './interfaces';
|
2 |
+
|
3 |
+
export class History {
|
4 |
+
private states: HistoryState[] = [];
|
5 |
+
private currentIndex: number = -1;
|
6 |
+
|
7 |
+
pushState(state: HistoryState): void {
|
8 |
+
this.states = this.states.slice(0, this.currentIndex + 1);
|
9 |
+
this.states.push(state);
|
10 |
+
this.currentIndex++;
|
11 |
+
}
|
12 |
+
|
13 |
+
undo(): HistoryState | null {
|
14 |
+
if (this.currentIndex > 0) {
|
15 |
+
this.currentIndex--;
|
16 |
+
return this.states[this.currentIndex];
|
17 |
+
}
|
18 |
+
return null;
|
19 |
+
}
|
20 |
+
|
21 |
+
redo(): HistoryState | null {
|
22 |
+
if (this.currentIndex < this.states.length - 1) {
|
23 |
+
this.currentIndex++;
|
24 |
+
return this.states[this.currentIndex];
|
25 |
+
}
|
26 |
+
return null;
|
27 |
+
}
|
28 |
+
|
29 |
+
canUndo(): boolean {
|
30 |
+
return this.currentIndex > 0;
|
31 |
+
}
|
32 |
+
|
33 |
+
canRedo(): boolean {
|
34 |
+
return this.currentIndex < this.states.length - 1;
|
35 |
+
}
|
36 |
+
|
37 |
+
getCurrentState(): HistoryState | null {
|
38 |
+
return this.currentIndex >= 0 ? this.states[this.currentIndex] : null;
|
39 |
+
}
|
40 |
+
}
|
freeform/src/core/StickyNote.ts
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Entity, Position, Size, Draggable, Resizable, Selectable } from './interfaces';
|
2 |
+
import * as Constants from './constants';
|
3 |
+
|
4 |
+
export class StickyNote implements Entity, Draggable, Resizable, Selectable {
|
5 |
+
id: string;
|
6 |
+
type: 'sticky' = 'sticky';
|
7 |
+
position: Position;
|
8 |
+
size: Size;
|
9 |
+
content: string;
|
10 |
+
color: string;
|
11 |
+
selected: boolean;
|
12 |
+
|
13 |
+
private isDragging: boolean = false;
|
14 |
+
private dragStart: Position = { x: 0, y: 0 };
|
15 |
+
|
16 |
+
constructor(entity: Entity) {
|
17 |
+
this.id = entity.id;
|
18 |
+
this.position = entity.position;
|
19 |
+
this.size = entity.size;
|
20 |
+
this.content = entity.content;
|
21 |
+
this.color = entity.color;
|
22 |
+
this.selected = entity.selected;
|
23 |
+
}
|
24 |
+
|
25 |
+
private snapToGrid(value: number): number {
|
26 |
+
return Math.round(value / Constants.GRID_SIZE) * Constants.GRID_SIZE;
|
27 |
+
}
|
28 |
+
|
29 |
+
updatePosition(x: number, y: number): void {
|
30 |
+
this.position = {
|
31 |
+
x: this.snapToGrid(x),
|
32 |
+
y: this.snapToGrid(y)
|
33 |
+
};
|
34 |
+
}
|
35 |
+
|
36 |
+
updateSize(width: number, height: number): void {
|
37 |
+
this.size = {
|
38 |
+
width: Math.max(Constants.GRID_SIZE, this.snapToGrid(width)),
|
39 |
+
height: Math.max(Constants.GRID_SIZE, this.snapToGrid(height))
|
40 |
+
};
|
41 |
+
}
|
42 |
+
|
43 |
+
updateSelection(selected: boolean): void {
|
44 |
+
this.selected = selected;
|
45 |
+
}
|
46 |
+
|
47 |
+
onDragStart(e: React.MouseEvent): void {
|
48 |
+
e.stopPropagation();
|
49 |
+
this.isDragging = true;
|
50 |
+
this.dragStart = { x: e.clientX, y: e.clientY };
|
51 |
+
this.selected = true;
|
52 |
+
}
|
53 |
+
|
54 |
+
onDrag(e: React.MouseEvent): void {
|
55 |
+
if (!this.isDragging) return;
|
56 |
+
|
57 |
+
const deltaX = e.clientX - this.dragStart.x;
|
58 |
+
const deltaY = e.clientY - this.dragStart.y;
|
59 |
+
|
60 |
+
this.updatePosition(
|
61 |
+
this.position.x + deltaX,
|
62 |
+
this.position.y + deltaY
|
63 |
+
);
|
64 |
+
|
65 |
+
this.dragStart = { x: e.clientX, y: e.clientY };
|
66 |
+
}
|
67 |
+
|
68 |
+
onDragEnd(): void {
|
69 |
+
this.isDragging = false;
|
70 |
+
}
|
71 |
+
|
72 |
+
resize(direction: string, deltaX: number, deltaY: number): void {
|
73 |
+
const newSize = { ...this.size };
|
74 |
+
const newPosition = { ...this.position };
|
75 |
+
|
76 |
+
switch (direction) {
|
77 |
+
case 'top':
|
78 |
+
newSize.height = this.snapToGrid(this.size.height - deltaY);
|
79 |
+
newPosition.y = this.snapToGrid(this.position.y + deltaY);
|
80 |
+
break;
|
81 |
+
case 'right':
|
82 |
+
newSize.width = this.snapToGrid(this.size.width + deltaX);
|
83 |
+
break;
|
84 |
+
case 'bottom':
|
85 |
+
newSize.height = this.snapToGrid(this.size.height + deltaY);
|
86 |
+
break;
|
87 |
+
case 'left':
|
88 |
+
newSize.width = this.snapToGrid(this.size.width - deltaX);
|
89 |
+
newPosition.x = this.snapToGrid(this.position.x + deltaX);
|
90 |
+
break;
|
91 |
+
}
|
92 |
+
|
93 |
+
this.updateSize(newSize.width, newSize.height);
|
94 |
+
this.updatePosition(newPosition.x, newPosition.y);
|
95 |
+
}
|
96 |
+
}
|
freeform/src/core/buffer/BufferManager.ts
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export class BufferManager<T> {
|
2 |
+
private readonly maxSize: number;
|
3 |
+
private buffer: T[] = [];
|
4 |
+
private writeIndex: number = 0;
|
5 |
+
private readIndex: number = 0;
|
6 |
+
private count: number = 0;
|
7 |
+
|
8 |
+
constructor(maxSize: number) {
|
9 |
+
this.maxSize = maxSize;
|
10 |
+
this.buffer = new Array(maxSize);
|
11 |
+
}
|
12 |
+
|
13 |
+
write(item: T): boolean {
|
14 |
+
if (this.count === this.maxSize) {
|
15 |
+
return false;
|
16 |
+
}
|
17 |
+
|
18 |
+
this.buffer[this.writeIndex] = item;
|
19 |
+
this.writeIndex = (this.writeIndex + 1) % this.maxSize;
|
20 |
+
this.count++;
|
21 |
+
return true;
|
22 |
+
}
|
23 |
+
|
24 |
+
read(): T | undefined {
|
25 |
+
if (this.count === 0) {
|
26 |
+
return undefined;
|
27 |
+
}
|
28 |
+
|
29 |
+
const item = this.buffer[this.readIndex];
|
30 |
+
this.readIndex = (this.readIndex + 1) % this.maxSize;
|
31 |
+
this.count--;
|
32 |
+
return item;
|
33 |
+
}
|
34 |
+
|
35 |
+
peek(): T | undefined {
|
36 |
+
if (this.count === 0) {
|
37 |
+
return undefined;
|
38 |
+
}
|
39 |
+
return this.buffer[this.readIndex];
|
40 |
+
}
|
41 |
+
|
42 |
+
clear(): void {
|
43 |
+
this.writeIndex = 0;
|
44 |
+
this.readIndex = 0;
|
45 |
+
this.count = 0;
|
46 |
+
}
|
47 |
+
|
48 |
+
get size(): number {
|
49 |
+
return this.count;
|
50 |
+
}
|
51 |
+
|
52 |
+
get capacity(): number {
|
53 |
+
return this.maxSize;
|
54 |
+
}
|
55 |
+
|
56 |
+
get isEmpty(): boolean {
|
57 |
+
return this.count === 0;
|
58 |
+
}
|
59 |
+
|
60 |
+
get isFull(): boolean {
|
61 |
+
return this.count === this.maxSize;
|
62 |
+
}
|
63 |
+
}
|
freeform/src/core/constants.ts
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export const GRID_SIZE = 20;
|
2 |
+
export const MIN_ZOOM = 0.1;
|
3 |
+
export const MAX_ZOOM = 5;
|
4 |
+
export const CANVAS_SIZE = 5000;
|
5 |
+
export const COLORS = ['#FFE082', '#FFCC80', '#81D4FA', '#A5D6A7', '#EF9A9A'];
|
6 |
+
export const DEFAULT_STICKY_SIZE = {
|
7 |
+
width: 9 * GRID_SIZE,
|
8 |
+
height: 9 * GRID_SIZE
|
9 |
+
};
|
10 |
+
|
11 |
+
export const INITIAL_STICKY = {
|
12 |
+
id: '1',
|
13 |
+
type: 'sticky' as const,
|
14 |
+
position: { x: 100, y: 100 },
|
15 |
+
size: DEFAULT_STICKY_SIZE,
|
16 |
+
content: 'Chạm hai lần để nhập văn bản',
|
17 |
+
color: COLORS[0],
|
18 |
+
selected: true
|
19 |
+
};
|
freeform/src/core/events/Event.ts
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export enum EventType {
|
2 |
+
Canvas,
|
3 |
+
Sticky,
|
4 |
+
Mouse,
|
5 |
+
Keyboard,
|
6 |
+
System
|
7 |
+
}
|
8 |
+
|
9 |
+
export interface EventData {
|
10 |
+
type: EventType;
|
11 |
+
timestamp: number;
|
12 |
+
source: string;
|
13 |
+
payload: any;
|
14 |
+
}
|
15 |
+
|
16 |
+
export class Event<T> implements EventData {
|
17 |
+
readonly type: EventType;
|
18 |
+
readonly timestamp: number;
|
19 |
+
readonly source: string;
|
20 |
+
readonly payload: T;
|
21 |
+
|
22 |
+
constructor(type: EventType, source: string, payload: T) {
|
23 |
+
this.type = type;
|
24 |
+
this.timestamp = Date.now();
|
25 |
+
this.source = source;
|
26 |
+
this.payload = payload;
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
export class EventEmitter<T> {
|
31 |
+
private handlers: Map<EventType, Set<(event: Event<T>) => void>> = new Map();
|
32 |
+
|
33 |
+
subscribe(type: EventType, handler: (event: Event<T>) => void): () => void {
|
34 |
+
if (!this.handlers.has(type)) {
|
35 |
+
this.handlers.set(type, new Set());
|
36 |
+
}
|
37 |
+
this.handlers.get(type)?.add(handler);
|
38 |
+
|
39 |
+
return () => {
|
40 |
+
const handlers = this.handlers.get(type);
|
41 |
+
if (handlers) {
|
42 |
+
handlers.delete(handler);
|
43 |
+
if (handlers.size === 0) {
|
44 |
+
this.handlers.delete(type);
|
45 |
+
}
|
46 |
+
}
|
47 |
+
};
|
48 |
+
}
|
49 |
+
|
50 |
+
emit(event: Event<T>): void {
|
51 |
+
const handlers = this.handlers.get(event.type);
|
52 |
+
if (handlers) {
|
53 |
+
handlers.forEach(handler => handler(event));
|
54 |
+
}
|
55 |
+
}
|
56 |
+
}
|
freeform/src/core/interfaces.ts
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export interface Position {
|
2 |
+
x: number;
|
3 |
+
y: number;
|
4 |
+
}
|
5 |
+
|
6 |
+
export interface Size {
|
7 |
+
width: number;
|
8 |
+
height: number;
|
9 |
+
}
|
10 |
+
|
11 |
+
export interface Entity {
|
12 |
+
id: string;
|
13 |
+
type: 'sticky';
|
14 |
+
position: Position;
|
15 |
+
size: Size;
|
16 |
+
content: string;
|
17 |
+
color: string;
|
18 |
+
selected: boolean;
|
19 |
+
}
|
20 |
+
|
21 |
+
export interface HistoryState {
|
22 |
+
entities: Entity[];
|
23 |
+
canvasOffset: Position;
|
24 |
+
zoom: number;
|
25 |
+
}
|
26 |
+
|
27 |
+
export interface Moveable {
|
28 |
+
position: Position;
|
29 |
+
updatePosition(x: number, y: number): void;
|
30 |
+
}
|
31 |
+
|
32 |
+
export interface Resizable {
|
33 |
+
size: Size;
|
34 |
+
updateSize(width: number, height: number): void;
|
35 |
+
}
|
36 |
+
|
37 |
+
export interface Selectable {
|
38 |
+
selected: boolean;
|
39 |
+
updateSelection(selected: boolean): void;
|
40 |
+
}
|
41 |
+
|
42 |
+
export interface Draggable extends Moveable {
|
43 |
+
onDragStart(e: React.MouseEvent): void;
|
44 |
+
onDrag(e: React.MouseEvent): void;
|
45 |
+
onDragEnd(): void;
|
46 |
+
}
|
freeform/src/core/logger/Logger.ts
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export enum LogLevel {
|
2 |
+
Debug,
|
3 |
+
Info,
|
4 |
+
Warn,
|
5 |
+
Error
|
6 |
+
}
|
7 |
+
|
8 |
+
export interface LogEntry {
|
9 |
+
level: LogLevel;
|
10 |
+
message: string;
|
11 |
+
timestamp: number;
|
12 |
+
context?: Record<string, any>;
|
13 |
+
}
|
14 |
+
|
15 |
+
export class Logger {
|
16 |
+
private static instance: Logger;
|
17 |
+
private buffer: LogEntry[] = [];
|
18 |
+
private readonly maxBufferSize: number = 1000;
|
19 |
+
|
20 |
+
private constructor() {}
|
21 |
+
|
22 |
+
static getInstance(): Logger {
|
23 |
+
if (!Logger.instance) {
|
24 |
+
Logger.instance = new Logger();
|
25 |
+
}
|
26 |
+
return Logger.instance;
|
27 |
+
}
|
28 |
+
|
29 |
+
private log(level: LogLevel, message: string, context?: Record<string, any>): void {
|
30 |
+
const entry: LogEntry = {
|
31 |
+
level,
|
32 |
+
message,
|
33 |
+
timestamp: Date.now(),
|
34 |
+
context
|
35 |
+
};
|
36 |
+
|
37 |
+
this.buffer.push(entry);
|
38 |
+
if (this.buffer.length > this.maxBufferSize) {
|
39 |
+
this.buffer.shift();
|
40 |
+
}
|
41 |
+
|
42 |
+
// Output to console in development
|
43 |
+
if (process.env.NODE_ENV === 'development') {
|
44 |
+
const logMethod = {
|
45 |
+
[LogLevel.Debug]: console.debug,
|
46 |
+
[LogLevel.Info]: console.info,
|
47 |
+
[LogLevel.Warn]: console.warn,
|
48 |
+
[LogLevel.Error]: console.error
|
49 |
+
}[level];
|
50 |
+
|
51 |
+
logMethod(`[${new Date(entry.timestamp).toISOString()}] ${message}`, context);
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
debug(message: string, context?: Record<string, any>): void {
|
56 |
+
this.log(LogLevel.Debug, message, context);
|
57 |
+
}
|
58 |
+
|
59 |
+
info(message: string, context?: Record<string, any>): void {
|
60 |
+
this.log(LogLevel.Info, message, context);
|
61 |
+
}
|
62 |
+
|
63 |
+
warn(message: string, context?: Record<string, any>): void {
|
64 |
+
this.log(LogLevel.Warn, message, context);
|
65 |
+
}
|
66 |
+
|
67 |
+
error(message: string, context?: Record<string, any>): void {
|
68 |
+
this.log(LogLevel.Error, message, context);
|
69 |
+
}
|
70 |
+
|
71 |
+
getBuffer(): LogEntry[] {
|
72 |
+
return [...this.buffer];
|
73 |
+
}
|
74 |
+
|
75 |
+
clearBuffer(): void {
|
76 |
+
this.buffer = [];
|
77 |
+
}
|
78 |
+
}
|
freeform/src/core/spatial/Grid.ts
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Position, Size } from '../interfaces';
|
2 |
+
|
3 |
+
export class Grid {
|
4 |
+
private readonly cellSize: number;
|
5 |
+
private cells: Map<string, Set<string>> = new Map();
|
6 |
+
|
7 |
+
constructor(cellSize: number) {
|
8 |
+
this.cellSize = cellSize;
|
9 |
+
}
|
10 |
+
|
11 |
+
private getCellKey(x: number, y: number): string {
|
12 |
+
const cellX = Math.floor(x / this.cellSize);
|
13 |
+
const cellY = Math.floor(y / this.cellSize);
|
14 |
+
return `${cellX},${cellY}`;
|
15 |
+
}
|
16 |
+
|
17 |
+
insert(id: string, position: Position, size: Size): void {
|
18 |
+
const startX = Math.floor(position.x / this.cellSize);
|
19 |
+
const startY = Math.floor(position.y / this.cellSize);
|
20 |
+
const endX = Math.floor((position.x + size.width) / this.cellSize);
|
21 |
+
const endY = Math.floor((position.y + size.height) / this.cellSize);
|
22 |
+
|
23 |
+
for (let x = startX; x <= endX; x++) {
|
24 |
+
for (let y = startY; y <= endY; y++) {
|
25 |
+
const key = `${x},${y}`;
|
26 |
+
if (!this.cells.has(key)) {
|
27 |
+
this.cells.set(key, new Set());
|
28 |
+
}
|
29 |
+
this.cells.get(key)?.add(id);
|
30 |
+
}
|
31 |
+
}
|
32 |
+
}
|
33 |
+
|
34 |
+
query(position: Position, size: Size): Set<string> {
|
35 |
+
const result = new Set<string>();
|
36 |
+
const startX = Math.floor(position.x / this.cellSize);
|
37 |
+
const startY = Math.floor(position.y / this.cellSize);
|
38 |
+
const endX = Math.floor((position.x + size.width) / this.cellSize);
|
39 |
+
const endY = Math.floor((position.y + size.height) / this.cellSize);
|
40 |
+
|
41 |
+
for (let x = startX; x <= endX; x++) {
|
42 |
+
for (let y = startY; y <= endY; y++) {
|
43 |
+
const key = `${x},${y}`;
|
44 |
+
const cell = this.cells.get(key);
|
45 |
+
if (cell) {
|
46 |
+
cell.forEach(id => result.add(id));
|
47 |
+
}
|
48 |
+
}
|
49 |
+
}
|
50 |
+
|
51 |
+
return result;
|
52 |
+
}
|
53 |
+
|
54 |
+
remove(id: string): void {
|
55 |
+
this.cells.forEach(cell => {
|
56 |
+
cell.delete(id);
|
57 |
+
});
|
58 |
+
}
|
59 |
+
|
60 |
+
clear(): void {
|
61 |
+
this.cells.clear();
|
62 |
+
}
|
63 |
+
}
|
freeform/src/core/utility/AsyncOperation.ts
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export class AsyncOperation<T> {
|
2 |
+
private promise: Promise<T>;
|
3 |
+
private resolve!: (value: T) => void;
|
4 |
+
private reject!: (reason?: any) => void;
|
5 |
+
private completed: boolean = false;
|
6 |
+
private aborted: boolean = false;
|
7 |
+
private timeout?: NodeJS.Timeout;
|
8 |
+
|
9 |
+
constructor(timeoutMs?: number) {
|
10 |
+
this.promise = new Promise<T>((resolve, reject) => {
|
11 |
+
this.resolve = resolve;
|
12 |
+
this.reject = reject;
|
13 |
+
});
|
14 |
+
|
15 |
+
if (timeoutMs) {
|
16 |
+
this.timeout = setTimeout(() => {
|
17 |
+
if (!this.completed) {
|
18 |
+
this.abort(new Error('Operation timed out'));
|
19 |
+
}
|
20 |
+
}, timeoutMs);
|
21 |
+
}
|
22 |
+
}
|
23 |
+
|
24 |
+
complete(value: T): void {
|
25 |
+
if (this.aborted || this.completed) return;
|
26 |
+
|
27 |
+
this.completed = true;
|
28 |
+
if (this.timeout) {
|
29 |
+
clearTimeout(this.timeout);
|
30 |
+
}
|
31 |
+
this.resolve(value);
|
32 |
+
}
|
33 |
+
|
34 |
+
abort(reason?: any): void {
|
35 |
+
if (this.completed || this.aborted) return;
|
36 |
+
|
37 |
+
this.aborted = true;
|
38 |
+
if (this.timeout) {
|
39 |
+
clearTimeout(this.timeout);
|
40 |
+
}
|
41 |
+
this.reject(reason || new Error('Operation aborted'));
|
42 |
+
}
|
43 |
+
|
44 |
+
async wait(): Promise<T> {
|
45 |
+
return this.promise;
|
46 |
+
}
|
47 |
+
|
48 |
+
isCompleted(): boolean {
|
49 |
+
return this.completed;
|
50 |
+
}
|
51 |
+
|
52 |
+
isAborted(): boolean {
|
53 |
+
return this.aborted;
|
54 |
+
}
|
55 |
+
}
|
freeform/src/core/utility/LazyLoader.ts
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export class LazyLoader<T> {
|
2 |
+
private value: T | undefined;
|
3 |
+
private loader: () => Promise<T>;
|
4 |
+
private loading: boolean = false;
|
5 |
+
private loadPromise: Promise<T> | undefined;
|
6 |
+
|
7 |
+
constructor(loader: () => Promise<T>) {
|
8 |
+
this.loader = loader;
|
9 |
+
}
|
10 |
+
|
11 |
+
async get(): Promise<T> {
|
12 |
+
if (this.value !== undefined) {
|
13 |
+
return this.value;
|
14 |
+
}
|
15 |
+
|
16 |
+
if (this.loading) {
|
17 |
+
return this.loadPromise!;
|
18 |
+
}
|
19 |
+
|
20 |
+
this.loading = true;
|
21 |
+
try {
|
22 |
+
this.loadPromise = this.loader();
|
23 |
+
this.value = await this.loadPromise;
|
24 |
+
return this.value;
|
25 |
+
} finally {
|
26 |
+
this.loading = false;
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
isLoaded(): boolean {
|
31 |
+
return this.value !== undefined;
|
32 |
+
}
|
33 |
+
|
34 |
+
isLoading(): boolean {
|
35 |
+
return this.loading;
|
36 |
+
}
|
37 |
+
|
38 |
+
reset(): void {
|
39 |
+
this.value = undefined;
|
40 |
+
this.loading = false;
|
41 |
+
this.loadPromise = undefined;
|
42 |
+
}
|
43 |
+
}
|
freeform/src/index.css
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
margin: 0;
|
3 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
4 |
+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
5 |
+
sans-serif;
|
6 |
+
-webkit-font-smoothing: antialiased;
|
7 |
+
-moz-osx-font-smoothing: grayscale;
|
8 |
+
}
|
9 |
+
|
10 |
+
code {
|
11 |
+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
12 |
+
monospace;
|
13 |
+
}
|
freeform/src/index.tsx
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React from 'react';
|
2 |
+
import ReactDOM from 'react-dom/client';
|
3 |
+
import './index.css';
|
4 |
+
import App from './App';
|
5 |
+
import reportWebVitals from './reportWebVitals';
|
6 |
+
|
7 |
+
const root = ReactDOM.createRoot(
|
8 |
+
document.getElementById('root') as HTMLElement
|
9 |
+
);
|
10 |
+
root.render(
|
11 |
+
<React.StrictMode>
|
12 |
+
<App />
|
13 |
+
</React.StrictMode>
|
14 |
+
);
|
15 |
+
|
16 |
+
// If you want to start measuring performance in your app, pass a function
|
17 |
+
// to log results (for example: reportWebVitals(console.log))
|
18 |
+
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
19 |
+
reportWebVitals();
|
freeform/src/logo.svg
ADDED
|
freeform/src/reportWebVitals.ts
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { ReportHandler } from 'web-vitals';
|
2 |
+
|
3 |
+
const reportWebVitals = (onPerfEntry?: ReportHandler) => {
|
4 |
+
if (onPerfEntry && onPerfEntry instanceof Function) {
|
5 |
+
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
6 |
+
getCLS(onPerfEntry);
|
7 |
+
getFID(onPerfEntry);
|
8 |
+
getFCP(onPerfEntry);
|
9 |
+
getLCP(onPerfEntry);
|
10 |
+
getTTFB(onPerfEntry);
|
11 |
+
});
|
12 |
+
}
|
13 |
+
};
|
14 |
+
|
15 |
+
export default reportWebVitals;
|
freeform/src/setupTests.ts
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
2 |
+
// allows you to do things like:
|
3 |
+
// expect(element).toHaveTextContent(/react/i)
|
4 |
+
// learn more: https://github.com/testing-library/jest-dom
|
5 |
+
import '@testing-library/jest-dom';
|
freeform/yarn.lock
ADDED
The diff for this file is too large to render.
See raw diff
|
|
schemas/freeform.fbs
ADDED
|