Compare commits
	
		
			4 Commits
		
	
	
		
			feat-actio
			...
			mobile_tex
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | 44660ce313 | ||
|   | ac00b4ef2b | ||
|   | bf7810306c | ||
|   | 9d0b7b0f2c | 
| @@ -1,43 +0,0 @@ | ||||
| { | ||||
|   // These tasks will run in order when initializing your CodeSandbox project. | ||||
|   "setupTasks": [ | ||||
|     { | ||||
|       "name": "Install Dependencies", | ||||
|       "command": "yarn install" | ||||
|     } | ||||
|   ], | ||||
|  | ||||
|   // These tasks can be run from CodeSandbox. Running one will open a log in the app. | ||||
|   "tasks": { | ||||
|     "build": { | ||||
|       "name": "Build", | ||||
|       "command": "yarn build", | ||||
|       "runAtStart": false | ||||
|     }, | ||||
|     "fix": { | ||||
|       "name": "Fix", | ||||
|       "command": "yarn fix", | ||||
|       "runAtStart": false | ||||
|     }, | ||||
|     "prettier": { | ||||
|       "name": "Prettify", | ||||
|       "command": "yarn prettier", | ||||
|       "runAtStart": false | ||||
|     }, | ||||
|     "start": { | ||||
|       "name": "Start Excalidraw", | ||||
|       "command": "yarn start", | ||||
|       "runAtStart": true | ||||
|     }, | ||||
|     "test": { | ||||
|       "name": "Run Tests", | ||||
|       "command": "yarn test", | ||||
|       "runAtStart": false | ||||
|     }, | ||||
|     "install-deps": { | ||||
|       "name": "Install Dependencies", | ||||
|       "command": "yarn install", | ||||
|       "restartOn": { "files": ["yarn.lock"] } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @@ -1,6 +1,5 @@ | ||||
| * | ||||
| !.env.development | ||||
| !.env.production | ||||
| !.env | ||||
| !.eslintrc.json | ||||
| !.npmrc | ||||
| !.prettierrc | ||||
|   | ||||
							
								
								
									
										5
									
								
								.env
									
									
									
									
									
										Normal file
									
								
							
							
						
						| @@ -0,0 +1,5 @@ | ||||
| REACT_APP_BACKEND_V1_GET_URL=https://json.excalidraw.com/api/v1/ | ||||
| REACT_APP_BACKEND_V2_GET_URL=https://json.excalidraw.com/api/v2/ | ||||
| REACT_APP_BACKEND_V2_POST_URL=https://json.excalidraw.com/api/v2/post/ | ||||
| REACT_APP_SOCKET_SERVER_URL=https://portal.excalidraw.com | ||||
| REACT_APP_FIREBASE_CONFIG='{"apiKey":"AIzaSyAd15pYlMci_xIp9ko6wkEsDzAAA0Dn0RU","authDomain":"excalidraw-room-persistence.firebaseapp.com","databaseURL":"https://excalidraw-room-persistence.firebaseio.com","projectId":"excalidraw-room-persistence","storageBucket":"excalidraw-room-persistence.appspot.com","messagingSenderId":"654800341332","appId":"1:654800341332:web:4a692de832b55bd57ce0c1"}' | ||||
| @@ -1,24 +0,0 @@ | ||||
| REACT_APP_BACKEND_V2_GET_URL=https://json-dev.excalidraw.com/api/v2/ | ||||
| REACT_APP_BACKEND_V2_POST_URL=https://json-dev.excalidraw.com/api/v2/post/ | ||||
|  | ||||
| REACT_APP_LIBRARY_URL=https://libraries.excalidraw.com | ||||
| REACT_APP_LIBRARY_BACKEND=https://us-central1-excalidraw-room-persistence.cloudfunctions.net/libraries | ||||
|  | ||||
| # collaboration WebSocket server (https://github.com/excalidraw/excalidraw-room) | ||||
| REACT_APP_WS_SERVER_URL=http://localhost:3002 | ||||
|  | ||||
| # set this only if using the collaboration workflow we use on excalidraw.com | ||||
| REACT_APP_PORTAL_URL= | ||||
|  | ||||
| REACT_APP_FIREBASE_CONFIG='{"apiKey":"AIzaSyCMkxA60XIW8KbqMYL7edC4qT5l4qHX2h8","authDomain":"excalidraw-oss-dev.firebaseapp.com","projectId":"excalidraw-oss-dev","storageBucket":"excalidraw-oss-dev.appspot.com","messagingSenderId":"664559512677","appId":"1:664559512677:web:a385181f2928d328a7aa8c"}' | ||||
|  | ||||
| # put these in your .env.local, or make sure you don't commit! | ||||
| # must be lowercase `true` when turned on | ||||
| # | ||||
| # whether to enable Service Workers in development | ||||
| REACT_APP_DEV_ENABLE_SW= | ||||
| # whether to disable live reload / HMR. Usuaully what you want to do when | ||||
| # debugging Service Workers. | ||||
| REACT_APP_DEV_DISABLE_LIVE_RELOAD= | ||||
|  | ||||
| FAST_REFRESH=false | ||||
| @@ -1,17 +1 @@ | ||||
| REACT_APP_BACKEND_V2_GET_URL=https://json.excalidraw.com/api/v2/ | ||||
| REACT_APP_BACKEND_V2_POST_URL=https://json.excalidraw.com/api/v2/post/ | ||||
|  | ||||
| REACT_APP_LIBRARY_URL=https://libraries.excalidraw.com | ||||
| REACT_APP_LIBRARY_BACKEND=https://us-central1-excalidraw-room-persistence.cloudfunctions.net/libraries | ||||
|  | ||||
| REACT_APP_PORTAL_URL=https://portal.excalidraw.com | ||||
| # Fill to set socket server URL used for collaboration. | ||||
| # Meant for forks only: excalidraw.com uses custom REACT_APP_PORTAL_URL flow | ||||
| REACT_APP_WS_SERVER_URL= | ||||
|  | ||||
| REACT_APP_FIREBASE_CONFIG='{"apiKey":"AIzaSyAd15pYlMci_xIp9ko6wkEsDzAAA0Dn0RU","authDomain":"excalidraw-room-persistence.firebaseapp.com","databaseURL":"https://excalidraw-room-persistence.firebaseio.com","projectId":"excalidraw-room-persistence","storageBucket":"excalidraw-room-persistence.appspot.com","messagingSenderId":"654800341332","appId":"1:654800341332:web:4a692de832b55bd57ce0c1"}' | ||||
|  | ||||
| # production-only vars | ||||
| REACT_APP_GOOGLE_ANALYTICS_ID=UA-387204-13 | ||||
|  | ||||
| REACT_APP_PLUS_APP=https://app.excalidraw.com | ||||
|   | ||||
| @@ -5,4 +5,3 @@ package-lock.json | ||||
| firebase/ | ||||
| dist/ | ||||
| public/workbox | ||||
| src/packages/excalidraw/types | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| { | ||||
|   "extends": ["@excalidraw/eslint-config", "react-app"], | ||||
|   "rules": { | ||||
|     "import/no-anonymous-default-export": "off", | ||||
|     "no-restricted-globals": "off" | ||||
|     "import/no-anonymous-default-export": "off" | ||||
|   } | ||||
| } | ||||
|   | ||||
							
								
								
									
										34
									
								
								.github/dependabot.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						| @@ -0,0 +1,34 @@ | ||||
| version: 2 | ||||
| updates: | ||||
|   - package-ecosystem: npm | ||||
|     directory: / | ||||
|     schedule: | ||||
|       interval: weekly | ||||
|       day: sunday | ||||
|       time: "01:00" | ||||
|     reviewers: | ||||
|       - lipis | ||||
|     assignees: | ||||
|       - lipis | ||||
|  | ||||
|   - package-ecosystem: npm | ||||
|     directory: /src/packages/excalidraw/ | ||||
|     schedule: | ||||
|       interval: weekly | ||||
|       day: sunday | ||||
|       time: "01:00" | ||||
|     reviewers: | ||||
|       - ad1992 | ||||
|     assignees: | ||||
|       - ad1992 | ||||
|  | ||||
|   - package-ecosystem: npm | ||||
|     directory: /src/packages/utils/ | ||||
|     schedule: | ||||
|       interval: weekly | ||||
|       day: sunday | ||||
|       time: "01:00" | ||||
|     reviewers: | ||||
|       - ad1992 | ||||
|     assignees: | ||||
|       - ad1992 | ||||
							
								
								
									
										5
									
								
								.github/workflows/autorelease-excalidraw.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -1,8 +1,8 @@ | ||||
| name: Auto release excalidraw next | ||||
| name: Auto release @excalidraw/excalidraw-next | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|       - release | ||||
|       - master | ||||
|  | ||||
| jobs: | ||||
|   Auto-release-excalidraw-next: | ||||
| @@ -23,5 +23,4 @@ jobs: | ||||
|           NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||||
|       - name: Auto release | ||||
|         run: | | ||||
|           yarn add @actions/core | ||||
|           yarn autorelease | ||||
|   | ||||
							
								
								
									
										55
									
								
								.github/workflows/autorelease-preview.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -1,55 +0,0 @@ | ||||
| name: Auto release excalidraw preview | ||||
| on: | ||||
|   issue_comment: | ||||
|     types: [created, edited] | ||||
|  | ||||
| jobs: | ||||
|   Auto-release-excalidraw-preview: | ||||
|     name: Auto release preview | ||||
|     if: github.event.comment.body == '@excalibot trigger release' && github.event.issue.pull_request | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - name: React to release comment | ||||
|         uses: peter-evans/create-or-update-comment@v1 | ||||
|         with: | ||||
|           token: ${{ secrets.PUSH_TRANSLATIONS_COVERAGE_PAT }} | ||||
|           comment-id: ${{ github.event.comment.id }} | ||||
|           reactions: "+1" | ||||
|       - name: Get PR SHA | ||||
|         id: sha | ||||
|         uses: actions/github-script@v4 | ||||
|         with: | ||||
|           result-encoding: string | ||||
|           script: | | ||||
|             const { owner, repo, number } = context.issue; | ||||
|             const pr = await github.pulls.get({ | ||||
|               owner, | ||||
|               repo, | ||||
|               pull_number: number, | ||||
|             }); | ||||
|             return pr.data.head.sha | ||||
|       - uses: actions/checkout@v2 | ||||
|         with: | ||||
|           ref: ${{ steps.sha.outputs.result }} | ||||
|           fetch-depth: 2 | ||||
|       - name: Setup Node.js 14.x | ||||
|         uses: actions/setup-node@v2 | ||||
|         with: | ||||
|           node-version: 14.x | ||||
|       - name: Set up publish access | ||||
|         run: | | ||||
|           npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN} | ||||
|         env: | ||||
|           NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||||
|       - name: Auto release preview | ||||
|         id: "autorelease" | ||||
|         run: | | ||||
|           yarn add @actions/core | ||||
|           yarn autorelease preview ${{ github.event.issue.number }} | ||||
|       - name: Post comment post release | ||||
|         if: always() | ||||
|         uses: peter-evans/create-or-update-comment@v1 | ||||
|         with: | ||||
|           token: ${{ secrets.PUSH_TRANSLATIONS_COVERAGE_PAT }} | ||||
|           issue-number: ${{ github.event.issue.number }} | ||||
|           body: "@${{ github.event.comment.user.login }} ${{ steps.autorelease.outputs.result }}" | ||||
							
								
								
									
										2
									
								
								.github/workflows/build-docker.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -3,7 +3,7 @@ name: Build Docker image | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|       - release | ||||
|       - master | ||||
|  | ||||
| jobs: | ||||
|   build-docker: | ||||
|   | ||||
							
								
								
									
										29
									
								
								.github/workflows/build-packages.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						| @@ -0,0 +1,29 @@ | ||||
| name: Build packages | ||||
|  | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|       - master | ||||
|   pull_request: | ||||
|  | ||||
| jobs: | ||||
|   packages: | ||||
|     runs-on: ubuntu-latest | ||||
|  | ||||
|     steps: | ||||
|       - uses: actions/checkout@v2 | ||||
|       - name: Setup Node.js 14.x | ||||
|         uses: actions/setup-node@v2 | ||||
|         with: | ||||
|           node-version: 14.x | ||||
|       - name: Install dependencies | ||||
|         run: | | ||||
|           yarn --frozen-lockfile | ||||
|           yarn --cwd src/packages/excalidraw | ||||
|           yarn --cwd src/packages/utils | ||||
|       - name: Build @excalidraw/excalidraw | ||||
|         run: | | ||||
|           yarn --cwd src/packages/excalidraw run pack | ||||
|       - name: Build @excalidraw/utils | ||||
|         run: | | ||||
|           yarn --cwd src/packages/utils run pack | ||||
							
								
								
									
										2
									
								
								.github/workflows/cancel.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -3,7 +3,7 @@ name: Cancel previous runs | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|       - release | ||||
|       - master | ||||
|   pull_request: | ||||
|  | ||||
| jobs: | ||||
|   | ||||
							
								
								
									
										17
									
								
								.github/workflows/publish-docker.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -3,23 +3,18 @@ name: Publish Docker | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|       - release | ||||
|       - master | ||||
|  | ||||
| jobs: | ||||
|   publish-docker: | ||||
|     runs-on: ubuntu-latest | ||||
|  | ||||
|     steps: | ||||
|       - name: Checkout repository | ||||
|         uses: actions/checkout@v3 | ||||
|       - name: Login to DockerHub | ||||
|         uses: docker/login-action@v2 | ||||
|       - uses: actions/checkout@v2 | ||||
|       - uses: docker/build-push-action@v1 | ||||
|         with: | ||||
|           username: ${{ secrets.DOCKER_USERNAME }} | ||||
|           password: ${{ secrets.DOCKER_PASSWORD }} | ||||
|       - name: Build and push | ||||
|         uses: docker/build-push-action@v3 | ||||
|         with: | ||||
|           context: . | ||||
|           push: true | ||||
|           tags: excalidraw/excalidraw:latest | ||||
|           repository: excalidraw/excalidraw | ||||
|           tag_with_ref: true | ||||
|           tag_with_sha: true | ||||
|   | ||||
							
								
								
									
										2
									
								
								.github/workflows/sentry-production.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -3,7 +3,7 @@ name: New Sentry production release | ||||
| on: | ||||
|   push: | ||||
|     branches: | ||||
|       - release | ||||
|       - master | ||||
|  | ||||
| jobs: | ||||
|   sentry: | ||||
|   | ||||
							
								
								
									
										6
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -5,11 +5,9 @@ | ||||
| .env.test.local | ||||
| .envrc | ||||
| .eslintcache | ||||
| .history | ||||
| .idea | ||||
| .vercel | ||||
| .vscode | ||||
| .yarn | ||||
| *.log | ||||
| *.tgz | ||||
| build | ||||
| @@ -19,9 +17,7 @@ logs | ||||
| node_modules | ||||
| npm-debug.log* | ||||
| package-lock.json | ||||
| static | ||||
| yarn-debug.log* | ||||
| yarn-error.log* | ||||
| src/packages/excalidraw/types | ||||
| src/packages/excalidraw/example/public/bundle.js | ||||
| src/packages/excalidraw/example/public/excalidraw-assets-dev | ||||
| src/packages/excalidraw/example/public/excalidraw.development.js | ||||
|   | ||||
| @@ -1,2 +0,0 @@ | ||||
| #!/bin/sh | ||||
| # yarn lint-staged | ||||
| @@ -10,7 +10,7 @@ ARG NODE_ENV=production | ||||
| COPY . . | ||||
| RUN yarn build:app:docker | ||||
|  | ||||
| FROM nginx:1.21-alpine | ||||
| FROM nginx:1.17-alpine | ||||
|  | ||||
| COPY --from=build /opt/node_app/build /usr/share/nginx/html | ||||
|  | ||||
|   | ||||
							
								
								
									
										57
									
								
								README.md
									
									
									
									
									
								
							
							
						
						| @@ -32,10 +32,6 @@ Last but not least, we're thankful to these companies for offering their service | ||||
|  | ||||
| [](https://vercel.com) [](https://sentry.io) [](https://crowdin.com) | ||||
|  | ||||
| ## Who's integrating Excalidraw | ||||
|  | ||||
| [Google Cloud](https://googlecloudcheatsheet.withgoogle.com/architecture) • [Meta](https://meta.com/) • [CodeSandbox](https://codesandbox.io/) • [Obsidian Excalidraw](https://github.com/zsviczian/obsidian-excalidraw-plugin) • [Replit](https://replit.com/) • [Slite](https://slite.com/) • [Notion](https://notion.so/) • [HackerRank](https://www.hackerrank.com/) • | ||||
|  | ||||
| ## Documentation | ||||
|  | ||||
| ### Shortcuts | ||||
| @@ -74,8 +70,6 @@ The first set of digits is the room. This is visible from the server that’s go | ||||
|  | ||||
| The second set of digits is the encryption key. The Excalidraw server doesn’t know about it. This is what all the participants use to encrypt/decrypt the messages. | ||||
|  | ||||
| > Note: Please ensure that the encryption key is 22 characters long. | ||||
|  | ||||
| ## Shape libraries | ||||
|  | ||||
| Find a growing list of libraries containing assets for your drawings at [libraries.excalidraw.com](https://libraries.excalidraw.com). | ||||
| @@ -88,7 +82,7 @@ Try out [`@excalidraw/excalidraw`](https://www.npmjs.com/package/@excalidraw/exc | ||||
|  | ||||
| ### Code Sandbox | ||||
|  | ||||
| - Go to https://codesandbox.io/p/github/excalidraw/excalidraw | ||||
| - Go to https://codesandbox.io/s/github/excalidraw/excalidraw | ||||
|   - You may need to sign in with GitHub and reload the page | ||||
| - You can start coding instantly, and even send PRs from there! | ||||
|  | ||||
| @@ -99,7 +93,7 @@ These instructions will get you a copy of the project up and running on your loc | ||||
| #### Requirements | ||||
|  | ||||
| - [Node.js](https://nodejs.org/en/) | ||||
| - [Yarn](https://yarnpkg.com/getting-started/install) (v1 or v2.4.2+) | ||||
| - [Yarn](https://yarnpkg.com/getting-started/install) | ||||
| - [Git](https://git-scm.com/downloads) | ||||
|  | ||||
| #### Clone the repo | ||||
| @@ -122,47 +116,16 @@ yarn start | ||||
|  | ||||
| Now you can open [http://localhost:3000](http://localhost:3000) and start coding in your favorite code editor. | ||||
|  | ||||
| #### Collaboration | ||||
|  | ||||
| For collaboration, you will need to set up [collab server](https://github.com/excalidraw/excalidraw-room) in local. | ||||
|  | ||||
| #### Commands | ||||
|  | ||||
| ##### Install the dependencies | ||||
|  | ||||
| ``` | ||||
| yarn | ||||
| ``` | ||||
|  | ||||
| ##### Run the project | ||||
|  | ||||
| ``` | ||||
| yarn start | ||||
| ``` | ||||
|  | ||||
| ##### Reformat all files with Prettier | ||||
|  | ||||
| ``` | ||||
| yarn fix | ||||
| ``` | ||||
|  | ||||
| ##### Run tests | ||||
|  | ||||
| ``` | ||||
| yarn test | ||||
| ``` | ||||
|  | ||||
| ##### Update test snapshots | ||||
|  | ||||
| ``` | ||||
| yarn test:update | ||||
| ``` | ||||
|  | ||||
| ##### Test for formatting with Prettier | ||||
|  | ||||
| ``` | ||||
| yarn test:code | ||||
| ``` | ||||
| | Command            | Description                       | | ||||
| | ------------------ | --------------------------------- | | ||||
| | `yarn`             | Install the dependencies          | | ||||
| | `yarn start`       | Run the project                   | | ||||
| | `yarn fix`         | Reformat all files with Prettier  | | ||||
| | `yarn test`        | Run tests                         | | ||||
| | `yarn test:update` | Update test snapshots             | | ||||
| | `yarn test:code`   | Test for formatting with Prettier | | ||||
|  | ||||
| #### Docker Compose | ||||
|  | ||||
|   | ||||
							
								
								
									
										20
									
								
								dev-docs/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						| @@ -1,20 +0,0 @@ | ||||
| # Dependencies | ||||
| /node_modules | ||||
|  | ||||
| # Production | ||||
| /build | ||||
|  | ||||
| # Generated files | ||||
| .docusaurus | ||||
| .cache-loader | ||||
|  | ||||
| # Misc | ||||
| .DS_Store | ||||
| .env.local | ||||
| .env.development.local | ||||
| .env.test.local | ||||
| .env.production.local | ||||
|  | ||||
| npm-debug.log* | ||||
| yarn-debug.log* | ||||
| yarn-error.log* | ||||
| @@ -1,41 +0,0 @@ | ||||
| # Website | ||||
|  | ||||
| This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator. | ||||
|  | ||||
| ### Installation | ||||
|  | ||||
| ``` | ||||
| $ yarn | ||||
| ``` | ||||
|  | ||||
| ### Local Development | ||||
|  | ||||
| ``` | ||||
| $ yarn start | ||||
| ``` | ||||
|  | ||||
| This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server. | ||||
|  | ||||
| ### Build | ||||
|  | ||||
| ``` | ||||
| $ yarn build | ||||
| ``` | ||||
|  | ||||
| This command generates static content into the `build` directory and can be served using any static contents hosting service. | ||||
|  | ||||
| ### Deployment | ||||
|  | ||||
| Using SSH: | ||||
|  | ||||
| ``` | ||||
| $ USE_SSH=true yarn deploy | ||||
| ``` | ||||
|  | ||||
| Not using SSH: | ||||
|  | ||||
| ``` | ||||
| $ GIT_USER=<Your GitHub username> yarn deploy | ||||
| ``` | ||||
|  | ||||
| If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. | ||||
| @@ -1,3 +0,0 @@ | ||||
| module.exports = { | ||||
|   presets: [require.resolve("@docusaurus/core/lib/babel/preset")], | ||||
| }; | ||||
| @@ -1,6 +0,0 @@ | ||||
| --- | ||||
| sidebar_position: 1 | ||||
| title: Overview | ||||
| --- | ||||
|  | ||||
| In development. For now, refer to [excalidraw Readme](https://github.com/excalidraw/excalidraw/blob/master/README.md). | ||||
| @@ -1,8 +0,0 @@ | ||||
| --- | ||||
| sidebar_position: 1 | ||||
| title: Introduction | ||||
| --- | ||||
|  | ||||
| Want to integrate Excalidraw into your app? Head over to the [package docs](/docs/package/overview). | ||||
|  | ||||
| If you're looking into the Excalidraw codebase itself, start [here](/docs/codebase/overview). | ||||
| @@ -1,6 +0,0 @@ | ||||
| --- | ||||
| sidebar_position: 1 | ||||
| title: Overview | ||||
| --- | ||||
|  | ||||
| In development. For now, refer to [excalidraw package readme](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md). | ||||
| @@ -1,121 +0,0 @@ | ||||
| // @ts-check | ||||
| // Note: type annotations allow type checking and IDEs autocompletion | ||||
|  | ||||
| const lightCodeTheme = require("prism-react-renderer/themes/github"); | ||||
| const darkCodeTheme = require("prism-react-renderer/themes/dracula"); | ||||
|  | ||||
| /** @type {import('@docusaurus/types').Config} */ | ||||
| const config = { | ||||
|   title: "Excalidraw developer docs", | ||||
|   tagline: | ||||
|     "For Excalidraw contributors or those integrating the Excalidraw editor", | ||||
|   url: "https://docs.excalidraw.com.com", | ||||
|   baseUrl: "/", | ||||
|   onBrokenLinks: "throw", | ||||
|   onBrokenMarkdownLinks: "warn", | ||||
|   favicon: "img/favicon.ico", | ||||
|   organizationName: "Excalidraw", // Usually your GitHub org/user name. | ||||
|   projectName: "excalidraw", // Usually your repo name. | ||||
|  | ||||
|   // Even if you don't use internalization, you can use this field to set useful | ||||
|   // metadata like html lang. For example, if your site is Chinese, you may want | ||||
|   // to replace "en" with "zh-Hans". | ||||
|   i18n: { | ||||
|     defaultLocale: "en", | ||||
|     locales: ["en"], | ||||
|   }, | ||||
|  | ||||
|   presets: [ | ||||
|     [ | ||||
|       "classic", | ||||
|       /** @type {import('@docusaurus/preset-classic').Options} */ | ||||
|       ({ | ||||
|         docs: { | ||||
|           sidebarPath: require.resolve("./sidebars.js"), | ||||
|           // Please change this to your repo. | ||||
|           editUrl: "https://github.com/excalidraw/docs/tree/master/", | ||||
|         }, | ||||
|         theme: { | ||||
|           customCss: require.resolve("./src/css/custom.css"), | ||||
|         }, | ||||
|       }), | ||||
|     ], | ||||
|   ], | ||||
|  | ||||
|   themeConfig: | ||||
|     /** @type {import('@docusaurus/preset-classic').ThemeConfig} */ | ||||
|     ({ | ||||
|       navbar: { | ||||
|         title: "Excalidraw Docs", | ||||
|         logo: { | ||||
|           alt: "Excalidraw Logo", | ||||
|           src: "img/logo.svg", | ||||
|         }, | ||||
|         items: [ | ||||
|           { | ||||
|             type: "doc", | ||||
|             docId: "get-started", | ||||
|             position: "left", | ||||
|             label: "Get started", | ||||
|           }, | ||||
|           { | ||||
|             to: "https://blog.excalidraw.com", | ||||
|             label: "Blog", | ||||
|             position: "left", | ||||
|           }, | ||||
|           { | ||||
|             to: "https://github.com/excalidraw/excalidraw", | ||||
|             label: "GitHub", | ||||
|             position: "right", | ||||
|           }, | ||||
|         ], | ||||
|       }, | ||||
|       footer: { | ||||
|         style: "dark", | ||||
|         links: [ | ||||
|           { | ||||
|             title: "Docs", | ||||
|             items: [ | ||||
|               { | ||||
|                 label: "Get Started", | ||||
|                 to: "/docs/get-started", | ||||
|               }, | ||||
|             ], | ||||
|           }, | ||||
|           { | ||||
|             title: "Community", | ||||
|             items: [ | ||||
|               { | ||||
|                 label: "Discord", | ||||
|                 href: "https://discord.gg/UexuTaE", | ||||
|               }, | ||||
|               { | ||||
|                 label: "Twitter", | ||||
|                 href: "https://twitter.com/excalidraw", | ||||
|               }, | ||||
|             ], | ||||
|           }, | ||||
|           { | ||||
|             title: "More", | ||||
|             items: [ | ||||
|               { | ||||
|                 label: "Blog", | ||||
|                 to: "https://blog.excalidraw.com", | ||||
|               }, | ||||
|               { | ||||
|                 label: "GitHub", | ||||
|                 to: "https://github.com/excalidraw/excalidraw", | ||||
|               }, | ||||
|             ], | ||||
|           }, | ||||
|         ], | ||||
|         copyright: `Made with ❤️ Built with Docusaurus`, | ||||
|       }, | ||||
|       prism: { | ||||
|         theme: lightCodeTheme, | ||||
|         darkTheme: darkCodeTheme, | ||||
|       }, | ||||
|     }), | ||||
| }; | ||||
|  | ||||
| module.exports = config; | ||||
| @@ -1,46 +0,0 @@ | ||||
| { | ||||
|   "name": "docs", | ||||
|   "version": "0.0.0", | ||||
|   "private": true, | ||||
|   "scripts": { | ||||
|     "docusaurus": "docusaurus", | ||||
|     "start": "docusaurus start --port 3003", | ||||
|     "build": "docusaurus build", | ||||
|     "swizzle": "docusaurus swizzle", | ||||
|     "deploy": "docusaurus deploy", | ||||
|     "clear": "docusaurus clear", | ||||
|     "serve": "docusaurus serve", | ||||
|     "write-translations": "docusaurus write-translations", | ||||
|     "write-heading-ids": "docusaurus write-heading-ids", | ||||
|     "typecheck": "tsc" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "@docusaurus/core": "2.0.0-rc.1", | ||||
|     "@docusaurus/preset-classic": "2.0.0-rc.1", | ||||
|     "@mdx-js/react": "^1.6.22", | ||||
|     "clsx": "^1.2.1", | ||||
|     "prism-react-renderer": "^1.3.5", | ||||
|     "react": "^17.0.2", | ||||
|     "react-dom": "^17.0.2" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "@docusaurus/module-type-aliases": "2.0.0-rc.1", | ||||
|     "@tsconfig/docusaurus": "^1.0.5", | ||||
|     "typescript": "^4.7.4" | ||||
|   }, | ||||
|   "browserslist": { | ||||
|     "production": [ | ||||
|       ">0.5%", | ||||
|       "not dead", | ||||
|       "not op_mini all" | ||||
|     ], | ||||
|     "development": [ | ||||
|       "last 1 chrome version", | ||||
|       "last 1 firefox version", | ||||
|       "last 1 safari version" | ||||
|     ] | ||||
|   }, | ||||
|   "engines": { | ||||
|     "node": ">=16.14" | ||||
|   } | ||||
| } | ||||
| @@ -1,31 +0,0 @@ | ||||
| /** | ||||
|  * Creating a sidebar enables you to: | ||||
|  - create an ordered group of docs | ||||
|  - render a sidebar for each doc of that group | ||||
|  - provide next/previous navigation | ||||
|  | ||||
|  The sidebars can be generated from the filesystem, or explicitly defined here. | ||||
|  | ||||
|  Create as many sidebars as you want. | ||||
|  */ | ||||
|  | ||||
| // @ts-check | ||||
|  | ||||
| /** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */ | ||||
| const sidebars = { | ||||
|   // By default, Docusaurus generates a sidebar from the docs folder structure | ||||
|   tutorialSidebar: [{ type: "autogenerated", dirName: "." }], | ||||
|  | ||||
|   // But you can create a sidebar manually | ||||
|   /* | ||||
|   tutorialSidebar: [ | ||||
|     { | ||||
|       type: 'category', | ||||
|       label: 'Tutorial', | ||||
|       items: ['hello'], | ||||
|     }, | ||||
|   ], | ||||
|    */ | ||||
| }; | ||||
|  | ||||
| module.exports = sidebars; | ||||
| @@ -1,62 +0,0 @@ | ||||
| import React from "react"; | ||||
| import clsx from "clsx"; | ||||
| import styles from "./styles.module.css"; | ||||
|  | ||||
| const FeatureList = [ | ||||
|   { | ||||
|     title: "Learn how Excalidraw works", | ||||
|     Svg: require("@site/static/img/undraw_innovative.svg").default, | ||||
|     description: ( | ||||
|       <>Want to contribute to Excalidraw but got lost in the codebase?</> | ||||
|     ), | ||||
|   }, | ||||
|   { | ||||
|     title: "Integrate Excalidraw", | ||||
|     Svg: require("@site/static/img/undraw_blank_canvas.svg").default, | ||||
|     description: ( | ||||
|       <> | ||||
|         Want to build your own app powered by Excalidraw by don't know where to | ||||
|         start? | ||||
|       </> | ||||
|     ), | ||||
|   }, | ||||
|   { | ||||
|     title: "Help us improve", | ||||
|     Svg: require("@site/static/img/undraw_add_files.svg").default, | ||||
|     description: ( | ||||
|       <> | ||||
|         Are the docs missing something? Anything you had trouble understanding | ||||
|         or needs an explanation? Come contribute to the docs to make them even | ||||
|         better! | ||||
|       </> | ||||
|     ), | ||||
|   }, | ||||
| ]; | ||||
|  | ||||
| function Feature({ Svg, title, description }) { | ||||
|   return ( | ||||
|     <div className={clsx("col col--4")}> | ||||
|       <div className="text--center"> | ||||
|         <Svg className={styles.featureSvg} role="img" /> | ||||
|       </div> | ||||
|       <div className="text--center padding-horiz--md"> | ||||
|         <h3>{title}</h3> | ||||
|         <p>{description}</p> | ||||
|       </div> | ||||
|     </div> | ||||
|   ); | ||||
| } | ||||
|  | ||||
| export default function HomepageFeatures() { | ||||
|   return ( | ||||
|     <section className={styles.features}> | ||||
|       <div className="container"> | ||||
|         <div className="row"> | ||||
|           {FeatureList.map((props, idx) => ( | ||||
|             <Feature key={idx} {...props} /> | ||||
|           ))} | ||||
|         </div> | ||||
|       </div> | ||||
|     </section> | ||||
|   ); | ||||
| } | ||||
| @@ -1,70 +0,0 @@ | ||||
| import React from "react"; | ||||
| import clsx from "clsx"; | ||||
| import styles from "./styles.module.css"; | ||||
|  | ||||
| type FeatureItem = { | ||||
|   title: string; | ||||
|   Svg: React.ComponentType<React.ComponentProps<"svg">>; | ||||
|   description: JSX.Element; | ||||
| }; | ||||
|  | ||||
| const FeatureList: FeatureItem[] = [ | ||||
|   { | ||||
|     title: "Easy to Use", | ||||
|     Svg: require("@site/static/img/undraw_docusaurus_mountain.svg").default, | ||||
|     description: ( | ||||
|       <> | ||||
|         Docusaurus was designed from the ground up to be easily installed and | ||||
|         used to get your website up and running quickly. | ||||
|       </> | ||||
|     ), | ||||
|   }, | ||||
|   { | ||||
|     title: "Focus on What Matters", | ||||
|     Svg: require("@site/static/img/undraw_docusaurus_tree.svg").default, | ||||
|     description: ( | ||||
|       <> | ||||
|         Docusaurus lets you focus on your docs, and we'll do the chores. Go | ||||
|         ahead and move your docs into the <code>docs</code> directory. | ||||
|       </> | ||||
|     ), | ||||
|   }, | ||||
|   { | ||||
|     title: "Powered by React", | ||||
|     Svg: require("@site/static/img/undraw_docusaurus_react.svg").default, | ||||
|     description: ( | ||||
|       <> | ||||
|         Extend or customize your website layout by reusing React. Docusaurus can | ||||
|         be extended while reusing the same header and footer. | ||||
|       </> | ||||
|     ), | ||||
|   }, | ||||
| ]; | ||||
|  | ||||
| function Feature({ title, Svg, description }: FeatureItem) { | ||||
|   return ( | ||||
|     <div className={clsx("col col--4")}> | ||||
|       <div className="text--center"> | ||||
|         <Svg className={styles.featureSvg} role="img" /> | ||||
|       </div> | ||||
|       <div className="text--center padding-horiz--md"> | ||||
|         <h3>{title}</h3> | ||||
|         <p>{description}</p> | ||||
|       </div> | ||||
|     </div> | ||||
|   ); | ||||
| } | ||||
|  | ||||
| export default function HomepageFeatures(): JSX.Element { | ||||
|   return ( | ||||
|     <section className={styles.features}> | ||||
|       <div className="container"> | ||||
|         <div className="row"> | ||||
|           {FeatureList.map((props, idx) => ( | ||||
|             <Feature key={idx} {...props} /> | ||||
|           ))} | ||||
|         </div> | ||||
|       </div> | ||||
|     </section> | ||||
|   ); | ||||
| } | ||||
| @@ -1,11 +0,0 @@ | ||||
| .features { | ||||
|   display: flex; | ||||
|   align-items: center; | ||||
|   padding: 2rem 0; | ||||
|   width: 100%; | ||||
| } | ||||
|  | ||||
| .featureSvg { | ||||
|   height: 200px; | ||||
|   width: 200px; | ||||
| } | ||||
| @@ -1,43 +0,0 @@ | ||||
| /** | ||||
|  * Any CSS included here will be global. The classic template | ||||
|  * bundles Infima by default. Infima is a CSS framework designed to | ||||
|  * work well for content-centric websites. | ||||
|  */ | ||||
|  | ||||
| /* You can override the default Infima variables here. */ | ||||
| :root { | ||||
|   --ifm-color-primary: #6965db; | ||||
|   --ifm-color-primary-dark: #5b57d1; | ||||
|   --ifm-color-primary-darker: #5b57d1; | ||||
|   --ifm-color-primary-darkest: #4a47b1; | ||||
|   --ifm-color-primary-light: #5b57d1; | ||||
|   --ifm-color-primary-lighter: #5b57d1; | ||||
|   --ifm-color-primary-lightest: #5b57d1; | ||||
|   --ifm-code-font-size: 95%; | ||||
| } | ||||
|  | ||||
| /* For readability concerns, you should choose a lighter palette in dark mode. */ | ||||
| [data-theme="dark"] { | ||||
|   --ifm-color-primary: #5650f0; | ||||
|   --ifm-color-primary-dark: #4b46d8; | ||||
|   --ifm-color-primary-darker: #4b46d8; | ||||
|   --ifm-color-primary-darkest: #3e39be; | ||||
|   --ifm-color-primary-light: #3f3d64; | ||||
|   --ifm-color-primary-lighter: #3f3d64; | ||||
|   --ifm-color-primary-lightest: #3f3d64; | ||||
| } | ||||
|  | ||||
| .docusaurus-highlight-code-line { | ||||
|   background-color: rgba(0, 0, 0, 0.1); | ||||
|   display: block; | ||||
|   margin: 0 calc(-1 * var(--ifm-pre-padding)); | ||||
|   padding: 0 var(--ifm-pre-padding); | ||||
| } | ||||
|  | ||||
| [data-theme="dark"] .docusaurus-highlight-code-line { | ||||
|   background-color: rgba(0, 0, 0, 0.3); | ||||
| } | ||||
|  | ||||
| [data-theme="dark"] .navbar__logo { | ||||
|   filter: invert(93%) hue-rotate(180deg); | ||||
| } | ||||
| @@ -1,42 +0,0 @@ | ||||
| import React from "react"; | ||||
| import clsx from "clsx"; | ||||
| import Layout from "@theme/Layout"; | ||||
| import Link from "@docusaurus/Link"; | ||||
| import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; | ||||
| import styles from "./index.module.css"; | ||||
| import HomepageFeatures from "@site/src/components/Homepage"; | ||||
|  | ||||
| function HomepageHeader() { | ||||
|   const { siteConfig } = useDocusaurusContext(); | ||||
|   return ( | ||||
|     <header className={clsx("hero hero--primary", styles.heroBanner)}> | ||||
|       <div className="container"> | ||||
|         <h1 className="hero__title">{siteConfig.title}</h1> | ||||
|         <p className="hero__subtitle">{siteConfig.tagline}</p> | ||||
|         <div className={styles.buttons}> | ||||
|           <Link | ||||
|             className="button button--secondary button--lg" | ||||
|             to="/docs/get-started" | ||||
|           > | ||||
|             Get started | ||||
|           </Link> | ||||
|         </div> | ||||
|       </div> | ||||
|     </header> | ||||
|   ); | ||||
| } | ||||
|  | ||||
| export default function Home() { | ||||
|   const { siteConfig } = useDocusaurusContext(); | ||||
|   return ( | ||||
|     <Layout | ||||
|       title={`Hello from ${siteConfig.title}`} | ||||
|       description="Description will go into a meta tag in <head />" | ||||
|     > | ||||
|       <HomepageHeader /> | ||||
|       <main> | ||||
|         <HomepageFeatures /> | ||||
|       </main> | ||||
|     </Layout> | ||||
|   ); | ||||
| } | ||||
| @@ -1,27 +0,0 @@ | ||||
| /** | ||||
|  * CSS files with the .module.css suffix will be treated as CSS modules | ||||
|  * and scoped locally. | ||||
|  */ | ||||
|  | ||||
| .heroBanner { | ||||
|   padding: 4rem 0; | ||||
|   text-align: center; | ||||
|   position: relative; | ||||
|   overflow: hidden; | ||||
| } | ||||
|  | ||||
| [data-theme="dark"] .heroBanner { | ||||
|   color: #fff; | ||||
| } | ||||
|  | ||||
| @media screen and (max-width: 996px) { | ||||
|   .heroBanner { | ||||
|     padding: 2rem; | ||||
|   } | ||||
| } | ||||
|  | ||||
| .buttons { | ||||
|   display: flex; | ||||
|   align-items: center; | ||||
|   justify-content: center; | ||||
| } | ||||
| @@ -1,42 +0,0 @@ | ||||
| import React from "react"; | ||||
| import clsx from "clsx"; | ||||
| import Layout from "@theme/Layout"; | ||||
| import Link from "@docusaurus/Link"; | ||||
| import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; | ||||
| import styles from "./index.module.css"; | ||||
| import HomepageFeatures from "@site/src/components/Homepage"; | ||||
|  | ||||
| function HomepageHeader() { | ||||
|   const { siteConfig } = useDocusaurusContext(); | ||||
|   return ( | ||||
|     <header className={clsx("hero hero--primary", styles.heroBanner)}> | ||||
|       <div className="container"> | ||||
|         <h1 className="hero__title">{siteConfig.title}</h1> | ||||
|         <p className="hero__subtitle">{siteConfig.tagline}</p> | ||||
|         <div className={styles.buttons}> | ||||
|           <Link | ||||
|             className="button button--secondary button--lg" | ||||
|             to="/docs/get-started" | ||||
|           > | ||||
|             Get started | ||||
|           </Link> | ||||
|         </div> | ||||
|       </div> | ||||
|     </header> | ||||
|   ); | ||||
| } | ||||
|  | ||||
| export default function Home() { | ||||
|   const { siteConfig } = useDocusaurusContext(); | ||||
|   return ( | ||||
|     <Layout | ||||
|       title={`Hello from ${siteConfig.title}`} | ||||
|       description="Description will go into a meta tag in <head />" | ||||
|     > | ||||
|       <HomepageHeader /> | ||||
|       <main> | ||||
|         <HomepageFeatures /> | ||||
|       </main> | ||||
|     </Layout> | ||||
|   ); | ||||
| } | ||||
| @@ -1,7 +0,0 @@ | ||||
| --- | ||||
| title: Markdown page example | ||||
| --- | ||||
|  | ||||
| # Markdown page example | ||||
|  | ||||
| You don't need React to write simple standalone pages. | ||||
| Before Width: | Height: | Size: 5.0 KiB | 
| Before Width: | Height: | Size: 26 KiB | 
| @@ -1,4 +0,0 @@ | ||||
| <svg viewBox="0 0 80 180" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2"> | ||||
| 	<path d="M22.197 150.382c-4.179-3.359-10.618-9.051-15.702-13.946l-4.01-3.813.734-5.009c.396-2.732 1.13-8.083 1.582-11.839.508-3.757 1.017-7.286 1.186-7.798.226-.683 0-1.025-.621-1.025-1.073 0-1.13.285 1.807-9.107a617.602 617.602 0 0 1 2.203-7.229c.113-.398.565-.569 1.073-.398.508.227.791.683.621 1.081-.169.455.113.911.565 1.082.621.227.565.683-.395 2.333-1.525 2.562-5.422 24.419-5.648 31.477-.17 5.009-.17 5.066 1.92 7.912 2.033 2.789 6.721 7.001 13.951 12.351 2.033 1.537 4.067 3.245 4.631 3.814.848 1.024 1.243.74 8.36-6.887 4.123-4.383 8.698-8.88 10.166-10.018l2.711-2.049-2.089-4.44c-1.13-2.391-5.705-11.612-10.223-20.377-9.433-18.442-7.513-16.678-18.47-16.849l-7.117-.056-2.372-2.733c-2.485-2.903-2.824-3.984-1.638-5.805.452-.627.791-1.651.791-2.277 0-1.025.395-1.196 2.655-1.309 1.412-.057 2.711-.228 2.88-.399.17-.171.396-3.7.565-7.855l.226-7.513-3.784-8.197C2.485 39.844 0 33.583 0 31.533c0-1.081.226-1.992.452-1.992.565 0 .565.057 23.553 48.382 10.675 22.426 20.785 43.544 22.479 47.016 1.695 3.472 3.22 6.659 3.333 7.115.113.512-3.785 4.439-9.998 9.961-5.591 5.008-10.505 9.562-10.957 10.074-1.299 1.594-3.219 1.082-6.665-1.707Zm1.921-65.458c-2.599-5.066-2.712-5.123-9.828-5.464-6.27-.342-6.383-.285-6.383.911 0 .683-.226 1.593-.508 2.049-.339.512-.113 1.423.678 2.675l1.242 1.935h5.649c3.106.057 6.664.285 7.907.512 1.243.228 2.316.342 2.429.285.113-.057-.452-1.366-1.186-2.903Zm-4.745-9.107c-.452-1.195-1.638-3.7-2.598-5.578-1.581-3.188-1.751-3.301-2.146-1.992-.226.797-.396 3.13-.452 5.236-.057 4.155-.17 4.098 4.575 4.383l1.525.057-.904-2.106Z" style="fill-rule:nonzero;stroke:#000;stroke-width:2px" transform="matrix(1.01351 0 0 -1 9.088 166.517)" /> | ||||
| 	<path d="M23.892 136.835c-1.017-.74-1.299-1.48-1.299-3.358 0-2.22.169-2.562 1.694-3.188 1.525-.626 1.92-.569 3.671.626 2.316 1.594 2.373 1.992.678 4.554-1.468 2.22-2.937 2.618-4.744 1.366Zm3.219-2.049c.904-1.594.339-2.789-1.355-2.789-1.525 0-2.203 1.536-1.356 3.073.678 1.253 1.977 1.139 2.711-.284ZM59.306 124.028c0-.285-.339-.569-.735-.569-.339 0-1.299-1.594-2.033-3.529-2.259-5.92-24.852-50.943-24.908-49.52 0 .74-.339 1.252-.904 1.252-.791 0-.904-.456-.565-2.675.339-2.562.113-3.131-7.907-18.841-4.519-8.936-9.376-18.271-10.788-20.775-1.469-2.619-2.598-5.465-2.711-6.66-.17-2.049.056-2.334 4.97-6.603 2.824-2.504 6.439-5.635 8.02-7.058C28.862 2.504 32.194-.114 33.098.057c1.356.228 22.31 22.369 22.367 23.622 0 .569-1.017 9.221-2.259 19.238-2.147 17.076-4.18 37.055-3.954 38.99.169 1.196-.678 7.229-1.299 9.847-.509 2.05-.283 2.903 3.784 12.238 2.372 5.521 5.479 12.295 6.834 15.027 1.299 2.732 2.429 5.123 2.429 5.294 0 .17-.395.284-.847.284-.452 0-.847-.228-.847-.569ZM46.315 81.509c.621-3.984 1.864-13.547 2.767-21.231 1.751-14.116 3.785-29.769 4.349-33.753.339-1.993.113-2.391-3.558-6.489-6.382-7.229-13.16-14.344-15.476-16.165l-2.146-1.708-11.014 10.359C11.07 21.971 10.223 22.939 10.844 24.077c.339.626 3.22 5.92 6.383 11.725 3.163 5.806 7.342 13.547 9.263 17.19 1.977 3.7 3.784 6.887 4.123 7.058.395.228.508-5.521.395-17.759-.226-18.271-.169-18.328 1.638-17.929.226 0 .396 9.221.396 20.434v20.377l5.93 11.953c3.276 6.603 5.987 11.896 6.1 11.84.113-.058.678-3.416 1.243-7.457Z" style="fill-rule:nonzero;stroke:#000;stroke-width:2px" transform="matrix(1.01351 0 0 -1 9.088 166.517)" /> | ||||
| </svg> | ||||
| Before Width: | Height: | Size: 3.4 KiB | 
| Before Width: | Height: | Size: 5.7 KiB | 
| Before Width: | Height: | Size: 12 KiB | 
| Before Width: | Height: | Size: 5.4 KiB | 
| @@ -1,7 +0,0 @@ | ||||
| { | ||||
|   // This file is not used in compilation. It is here just for a nice editor experience. | ||||
|   "extends": "@tsconfig/docusaurus/tsconfig.json", | ||||
|   "compilerOptions": { | ||||
|     "baseUrl": "." | ||||
|   } | ||||
| } | ||||
							
								
								
									
										7489
									
								
								dev-docs/yarn.lock
									
									
									
									
									
								
							
							
						
						| @@ -2,8 +2,5 @@ | ||||
|   "firestore": { | ||||
|     "rules": "firestore.rules", | ||||
|     "indexes": "firestore.indexes.json" | ||||
|   }, | ||||
|   "storage": { | ||||
|     "rules": "storage.rules" | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,11 +0,0 @@ | ||||
| rules_version = '2'; | ||||
| service firebase.storage { | ||||
|   match /b/{bucket}/o { | ||||
|     match /{files}/rooms/{room}/{file} { | ||||
|     	allow get, write: if true; | ||||
|     } | ||||
|     match /{files}/shareLinks/{shareLink}/{file} { | ||||
|     	allow get, write: if true; | ||||
|     } | ||||
|   } | ||||
| } | ||||
							
								
								
									
										90
									
								
								package.json
									
									
									
									
									
								
							
							
						
						| @@ -21,77 +21,59 @@ | ||||
|   "dependencies": { | ||||
|     "@sentry/browser": "6.2.5", | ||||
|     "@sentry/integrations": "6.2.5", | ||||
|     "@testing-library/jest-dom": "5.16.2", | ||||
|     "@testing-library/react": "12.1.5", | ||||
|     "@tldraw/vec": "1.7.1", | ||||
|     "@types/jest": "27.4.0", | ||||
|     "@types/pica": "5.1.3", | ||||
|     "@types/react": "18.0.15", | ||||
|     "@types/react-dom": "18.0.6", | ||||
|     "@testing-library/jest-dom": "5.11.10", | ||||
|     "@testing-library/react": "11.2.6", | ||||
|     "@types/jest": "26.0.22", | ||||
|     "@types/react": "17.0.3", | ||||
|     "@types/react-dom": "17.0.3", | ||||
|     "@types/socket.io-client": "1.4.36", | ||||
|     "browser-fs-access": "0.29.1", | ||||
|     "browser-fs-access": "0.16.4", | ||||
|     "clsx": "1.1.1", | ||||
|     "cross-env": "7.0.3", | ||||
|     "fake-indexeddb": "3.1.7", | ||||
|     "firebase": "8.3.3", | ||||
|     "i18next-browser-languagedetector": "6.1.4", | ||||
|     "idb-keyval": "6.0.3", | ||||
|     "image-blob-reduce": "3.0.1", | ||||
|     "jotai": "1.6.4", | ||||
|     "i18next-browser-languagedetector": "6.1.0", | ||||
|     "lodash.throttle": "4.1.1", | ||||
|     "nanoid": "3.3.3", | ||||
|     "open-color": "1.9.1", | ||||
|     "nanoid": "3.1.22", | ||||
|     "open-color": "1.8.0", | ||||
|     "pako": "1.0.11", | ||||
|     "perfect-freehand": "1.2.0", | ||||
|     "pica": "7.1.1", | ||||
|     "perfect-freehand": "0.4.7", | ||||
|     "png-chunk-text": "1.0.0", | ||||
|     "png-chunks-encode": "1.0.0", | ||||
|     "png-chunks-extract": "1.0.0", | ||||
|     "points-on-curve": "0.2.0", | ||||
|     "pwacompat": "2.0.17", | ||||
|     "react": "18.2.0", | ||||
|     "react-dom": "18.2.0", | ||||
|     "react-scripts": "5.0.1", | ||||
|     "roughjs": "4.5.2", | ||||
|     "sass": "1.51.0", | ||||
|     "react": "17.0.2", | ||||
|     "react-dom": "17.0.2", | ||||
|     "react-scripts": "4.0.3", | ||||
|     "roughjs": "4.4.1", | ||||
|     "sass": "1.32.10", | ||||
|     "socket.io-client": "2.3.1", | ||||
|     "typescript": "4.9.4", | ||||
|     "workbox-background-sync": "^6.5.4", | ||||
|     "workbox-broadcast-update": "^6.5.4", | ||||
|     "workbox-cacheable-response": "^6.5.4", | ||||
|     "workbox-core": "^6.5.4", | ||||
|     "workbox-expiration": "^6.5.4", | ||||
|     "workbox-google-analytics": "^6.5.4", | ||||
|     "workbox-navigation-preload": "^6.5.4", | ||||
|     "workbox-precaching": "^6.5.4", | ||||
|     "workbox-range-requests": "^6.5.4", | ||||
|     "workbox-routing": "^6.5.4", | ||||
|     "workbox-strategies": "^6.5.4", | ||||
|     "workbox-streams": "^6.5.4" | ||||
|     "typescript": "4.2.4" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "@excalidraw/eslint-config": "1.0.0", | ||||
|     "@excalidraw/prettier-config": "1.0.2", | ||||
|     "@types/chai": "4.3.0", | ||||
|     "@types/lodash.throttle": "4.1.7", | ||||
|     "@types/pako": "1.0.3", | ||||
|     "@types/resize-observer-browser": "0.1.7", | ||||
|     "chai": "4.3.6", | ||||
|     "dotenv": "16.0.1", | ||||
|     "eslint-config-prettier": "8.5.0", | ||||
|     "@types/lodash.throttle": "4.1.6", | ||||
|     "@types/pako": "1.0.1", | ||||
|     "@types/resize-observer-browser": "0.1.5", | ||||
|     "eslint-config-prettier": "8.3.0", | ||||
|     "eslint-plugin-prettier": "3.3.1", | ||||
|     "http-server": "14.1.1", | ||||
|     "husky": "7.0.4", | ||||
|     "jest-canvas-mock": "2.4.0", | ||||
|     "lint-staged": "12.3.7", | ||||
|     "firebase-tools": "9.9.0", | ||||
|     "husky": "4.3.8", | ||||
|     "jest-canvas-mock": "2.3.1", | ||||
|     "lint-staged": "10.5.4", | ||||
|     "pepjs": "0.5.3", | ||||
|     "prettier": "2.6.2", | ||||
|     "rewire": "6.0.0" | ||||
|     "prettier": "2.2.1", | ||||
|     "rewire": "5.0.0" | ||||
|   }, | ||||
|   "engines": { | ||||
|     "node": ">=14.0.0" | ||||
|   }, | ||||
|   "homepage": ".", | ||||
|   "husky": { | ||||
|     "hooks": { | ||||
|       "pre-commit": "lint-staged" | ||||
|     } | ||||
|   }, | ||||
|   "jest": { | ||||
|     "transformIgnorePatterns": [ | ||||
|       "node_modules/(?!(roughjs|points-on-curve|path-data-parser|points-on-path|browser-fs-access)/)" | ||||
| @@ -103,8 +85,8 @@ | ||||
|   "private": true, | ||||
|   "scripts": { | ||||
|     "build-node": "node ./scripts/build-node.js", | ||||
|     "build:app:docker": "cross-env REACT_APP_DISABLE_SENTRY=true REACT_APP_DISABLE_TRACKING=true react-scripts build", | ||||
|     "build:app": "cross-env REACT_APP_GIT_SHA=$VERCEL_GIT_COMMIT_SHA react-scripts build", | ||||
|     "build:app:docker": "REACT_APP_DISABLE_SENTRY=true react-scripts build", | ||||
|     "build:app": "REACT_APP_GIT_SHA=$VERCEL_GIT_COMMIT_SHA react-scripts build", | ||||
|     "build:version": "node ./scripts/build-version.js", | ||||
|     "build": "yarn build:app && yarn build:version", | ||||
|     "eject": "react-scripts eject", | ||||
| @@ -113,10 +95,8 @@ | ||||
|     "fix": "yarn fix:other && yarn fix:code", | ||||
|     "locales-coverage": "node scripts/build-locales-coverage.js", | ||||
|     "locales-coverage:description": "node scripts/locales-coverage-description.js", | ||||
|     "prepare": "husky install", | ||||
|     "prettier": "prettier \"**/*.{css,scss,json,md,html,yml}\" --ignore-path=.eslintignore", | ||||
|     "start": "react-scripts start", | ||||
|     "start:production": "npm run build && npx http-server build -a localhost -p 5001 -o", | ||||
|     "test:all": "yarn test:typecheck && yarn test:code && yarn test:other && yarn test:app --watchAll=false", | ||||
|     "test:app": "react-scripts test --passWithNoTests", | ||||
|     "test:code": "eslint --max-warnings=0 --ext .js,.ts,.tsx .", | ||||
| @@ -125,8 +105,6 @@ | ||||
|     "test:typecheck": "tsc", | ||||
|     "test:update": "yarn test:app --updateSnapshot --watchAll=false", | ||||
|     "test": "yarn test:app", | ||||
|     "autorelease": "node scripts/autorelease.js", | ||||
|     "prerelease": "node scripts/prerelease.js", | ||||
|     "release": "node scripts/release.js" | ||||
|     "autorelease": "node scripts/autorelease.js" | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -11,28 +11,3 @@ | ||||
|   src: url("Cascadia.woff2"); | ||||
|   font-display: swap; | ||||
| } | ||||
|  | ||||
| @font-face { | ||||
|   font-family: "Assistant"; | ||||
|   src: url("Assistant-Regular.woff2"); | ||||
|   font-display: swap; | ||||
|   font-weight: 400; | ||||
| } | ||||
| @font-face { | ||||
|   font-family: "Assistant"; | ||||
|   src: url("Assistant-Medium.woff2"); | ||||
|   font-display: swap; | ||||
|   font-weight: 500; | ||||
| } | ||||
| @font-face { | ||||
|   font-family: "Assistant"; | ||||
|   src: url("Assistant-SemiBold.woff2"); | ||||
|   font-display: swap; | ||||
|   font-weight: 600; | ||||
| } | ||||
| @font-face { | ||||
|   font-family: "Assistant"; | ||||
|   src: url("Assistant-Bold.woff2"); | ||||
|   font-display: swap; | ||||
|   font-weight: 700; | ||||
| } | ||||
|   | ||||
| @@ -8,95 +8,49 @@ | ||||
|       content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover, shrink-to-fit=no" | ||||
|     /> | ||||
|     <meta name="referrer" content="origin" /> | ||||
|  | ||||
|     <meta name="mobile-web-app-capable" content="yes" /> | ||||
|     <meta name="theme-color" content="#121212" /> | ||||
|  | ||||
|     <!-- Primary Meta Tags --> | ||||
|     <meta | ||||
|       name="title" | ||||
|       content="Excalidraw — Collaborative whiteboarding made easy" | ||||
|     /> | ||||
|     <meta | ||||
|       name="description" | ||||
|       content="Excalidraw is a virtual collaborative whiteboard tool that lets you easily sketch diagrams that have a hand-drawn feel to them." | ||||
|     /> | ||||
|     <meta name="image" content="https://excalidraw.com/og-general-v1.png" /> | ||||
|  | ||||
|     <!-- Open Graph / Facebook --> | ||||
|     <meta property="og:site_name" content="Excalidraw" /> | ||||
|     <meta property="og:type" content="website" /> | ||||
|     <meta property="og:url" content="https://excalidraw.com" /> | ||||
|     <meta | ||||
|       property="og:title" | ||||
|       content="Excalidraw — Collaborative whiteboarding made easy" | ||||
|     /> | ||||
|     <meta property="og:image:alt" content="Excalidraw logo" /> | ||||
|     <meta | ||||
|       property="og:description" | ||||
|       content="Excalidraw is a virtual collaborative whiteboard tool that lets you easily sketch diagrams that have a hand-drawn feel to them." | ||||
|     /> | ||||
|     <meta property="og:image" content="https://excalidraw.com/og-fb-v1.png" /> | ||||
|  | ||||
|     <!-- Twitter --> | ||||
|     <meta property="twitter:card" content="summary_large_image" /> | ||||
|     <meta property="twitter:site" content="@excalidraw" /> | ||||
|     <meta property="twitter:url" content="https://excalidraw.com" /> | ||||
|     <meta | ||||
|       property="twitter:title" | ||||
|       content="Excalidraw — Collaborative whiteboarding made easy" | ||||
|     /> | ||||
|     <meta | ||||
|       property="twitter:description" | ||||
|       content="Excalidraw is a virtual collaborative whiteboard tool that lets you easily sketch diagrams that have a hand-drawn feel to them." | ||||
|     /> | ||||
|     <meta | ||||
|       property="twitter:image" | ||||
|       content="https://excalidraw.com/og-twitter-v1.png" | ||||
|     /> | ||||
|     <meta name="theme-color" content="#000" /> | ||||
|  | ||||
|     <!-- General tags --> | ||||
|     <meta | ||||
|       name="description" | ||||
|       content="Excalidraw is a virtual collaborative whiteboard tool that lets you easily sketch diagrams that have a hand-drawn feel to them." | ||||
|     /> | ||||
|     <meta name="image" content="og-image.png" /> | ||||
|  | ||||
|     <!-------------------------------------------------------------------------> | ||||
|     <!--   to minimize white flash on load when user has dark mode enabled   --> | ||||
|     <script> | ||||
|       try { | ||||
|         // | ||||
|         const theme = window.localStorage.getItem("excalidraw-theme"); | ||||
|         if (theme === "dark") { | ||||
|           document.documentElement.classList.add("dark"); | ||||
|         } | ||||
|       } catch {} | ||||
|     </script> | ||||
|     <style> | ||||
|       html.dark { | ||||
|         background-color: #121212; | ||||
|         color: #fff; | ||||
|       } | ||||
|     </style> | ||||
|     <!-------------------------------------------------------------------------> | ||||
|     <!-- OpenGraph tags --> | ||||
|     <meta property="og:url" content="https://excalidraw.com" /> | ||||
|     <meta property="og:site_name" content="Excalidraw" /> | ||||
|     <meta property="og:type" content="website" /> | ||||
|     <meta property="og:title" content="Excalidraw" /> | ||||
|     <meta | ||||
|       property="og:description" | ||||
|       content="Excalidraw is a whiteboard tool that lets you easily sketch diagrams that have a hand-drawn feel to them." | ||||
|     /> | ||||
|     <!-- OG tags require an absolute url for images --> | ||||
|     <meta | ||||
|       property="og:image" | ||||
|       name="twitter:image" | ||||
|       content="https://excalidraw.com/og-image.png" | ||||
|     /> | ||||
|     <meta | ||||
|       property="og:image:secure_url" | ||||
|       name="twitter:image" | ||||
|       content="https://excalidraw.com/og-image.png" | ||||
|     /> | ||||
|     <meta property="og:image:width" content="1280" /> | ||||
|     <meta property="og:image:height" content="669" /> | ||||
|     <meta property="og:image:alt" content="Excalidraw logo with byline." /> | ||||
|  | ||||
|     <script> | ||||
|       // Redirect Excalidraw+ users which have auto-redirect enabled. | ||||
|       // | ||||
|       // Redirect only the bare root path, so link/room/library urls are not | ||||
|       // redirected. | ||||
|       // | ||||
|       // Putting into index.html for best performance (can't redirect on server | ||||
|       // due to location.hash checks). | ||||
|       if ( | ||||
|         window.location.pathname === "/" && | ||||
|         !window.location.hash && | ||||
|         !window.location.search && | ||||
|         // if its present redirect | ||||
|         document.cookie.includes("excplus-autoredirect=true") | ||||
|       ) { | ||||
|         window.location.href = "https://app.excalidraw.com"; | ||||
|       } | ||||
|     </script> | ||||
|     <!-- Twitter Card tags --> | ||||
|     <meta name="twitter:card" content="summary_large_image" /> | ||||
|     <meta name="twitter:title" content="Excalidraw" /> | ||||
|     <meta | ||||
|       name="twitter:description" | ||||
|       content="Excalidraw is a whiteboard tool that lets you easily sketch diagrams that have a hand-drawn feel to them." | ||||
|     /> | ||||
|  | ||||
|     <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /> | ||||
|  | ||||
| @@ -118,6 +72,12 @@ | ||||
|       crossorigin="anonymous" | ||||
|     /> | ||||
|  | ||||
|     <link | ||||
|       href="%REACT_APP_SOCKET_SERVER_URL%/socket.io" | ||||
|       rel="preconnect" | ||||
|       crossorigin="anonymous" | ||||
|     /> | ||||
|  | ||||
|     <link | ||||
|       rel="manifest" | ||||
|       href="manifest.json" | ||||
| @@ -125,29 +85,12 @@ | ||||
|     /> | ||||
|  | ||||
|     <link rel="stylesheet" href="fonts.css" type="text/css" /> | ||||
|     <% if (process.env.REACT_APP_DEV_DISABLE_LIVE_RELOAD==="true" ) { %> | ||||
|     <script> | ||||
|       { | ||||
|         const _WebSocket = window.WebSocket; | ||||
|         window.WebSocket = function (url) { | ||||
|           if (/ws:\/\/localhost:.+?\/sockjs-node/.test(url)) { | ||||
|             console.info( | ||||
|               "[!!!] Live reload is disabled via process.env.REACT_APP_DEV_DISABLE_LIVE_RELOAD [!!!]", | ||||
|             ); | ||||
|           } else { | ||||
|             return new _WebSocket(url); | ||||
|           } | ||||
|         }; | ||||
|       } | ||||
|     </script> | ||||
|     <% } %> | ||||
|     <script> | ||||
|       window.EXCALIDRAW_ASSET_PATH = "/"; | ||||
|       // setting this so that libraries installation reuses this window tab. | ||||
|       window.name = "_excalidraw"; | ||||
|     </script> | ||||
|     <% if (process.env.REACT_APP_DISABLE_TRACKING !== 'true' && | ||||
|     process.env.REACT_APP_GOOGLE_ANALYTICS_ID) { %> | ||||
|     <% if (process.env.REACT_APP_GOOGLE_ANALYTICS_ID) { %> | ||||
|     <script | ||||
|       async | ||||
|       src="https://www.googletagmanager.com/gtag/js?id=%REACT_APP_GOOGLE_ANALYTICS_ID%" | ||||
| @@ -167,14 +110,13 @@ | ||||
|       body, | ||||
|       html { | ||||
|         margin: 0; | ||||
|         --ui-font: Assistant, system-ui, BlinkMacSystemFont, -apple-system, | ||||
|           Segoe UI, Roboto, Helvetica, Arial, sans-serif; | ||||
|         --ui-font: system-ui, BlinkMacSystemFont, -apple-system, Segoe UI, | ||||
|           Roboto, Helvetica, Arial, sans-serif; | ||||
|         font-family: var(--ui-font); | ||||
|         -webkit-text-size-adjust: 100%; | ||||
|  | ||||
|         width: 100%; | ||||
|         height: 100%; | ||||
|         overflow: hidden; | ||||
|       } | ||||
|  | ||||
|       .visually-hidden { | ||||
| @@ -183,10 +125,30 @@ | ||||
|         width: 1px; | ||||
|         overflow: hidden; | ||||
|         clip: rect(1px, 1px, 1px, 1px); | ||||
|         white-space: nowrap; | ||||
|         white-space: nowrap; /* added line */ | ||||
|         user-select: none; | ||||
|       } | ||||
|  | ||||
|       .LoadingMessage { | ||||
|         position: absolute; | ||||
|         top: 0; | ||||
|         right: 0; | ||||
|         bottom: 0; | ||||
|         left: 0; | ||||
|         z-index: 999; | ||||
|         display: flex; | ||||
|         align-items: center; | ||||
|         justify-content: center; | ||||
|         pointer-events: none; | ||||
|       } | ||||
|  | ||||
|       .LoadingMessage span { | ||||
|         background-color: var(--button-gray-1); | ||||
|         border-radius: 5px; | ||||
|         padding: 0.8em 1.2em; | ||||
|         color: var(--popup-text-color); | ||||
|         font-size: 1.3em; | ||||
|       } | ||||
|       #root { | ||||
|         height: 100%; | ||||
|         -webkit-touch-callout: none; | ||||
| @@ -195,10 +157,8 @@ | ||||
|         -moz-user-select: none; | ||||
|         -ms-user-select: none; | ||||
|         user-select: none; | ||||
|       } | ||||
|  | ||||
|       @media screen and (min-width: 1200px) { | ||||
|         #root { | ||||
|         @media screen and (min-width: 1200px) { | ||||
|           -webkit-touch-callout: default; | ||||
|           -webkit-user-select: auto; | ||||
|           -khtml-user-select: auto; | ||||
| @@ -215,6 +175,10 @@ | ||||
|     <header> | ||||
|       <h1 class="visually-hidden">Excalidraw</h1> | ||||
|     </header> | ||||
|     <div id="root"></div> | ||||
|     <div id="root"> | ||||
|       <div class="LoadingMessage"> | ||||
|         <span>Loading scene...</span> | ||||
|       </div> | ||||
|     </div> | ||||
|   </body> | ||||
| </html> | ||||
|   | ||||
| @@ -26,6 +26,7 @@ | ||||
|       } | ||||
|     } | ||||
|   ], | ||||
|   "capture_links": "new_client", | ||||
|   "share_target": { | ||||
|     "action": "/web-share-target", | ||||
|     "method": "POST", | ||||
|   | ||||
| Before Width: | Height: | Size: 26 KiB | 
| Before Width: | Height: | Size: 26 KiB | 
| Before Width: | Height: | Size: 27 KiB | 
| @@ -1,9 +1,3 @@ | ||||
| User-agent: Twitterbot | ||||
| Disallow: | ||||
|  | ||||
| User-agent: facebookexternalhit | ||||
| Disallow: | ||||
|  | ||||
| user-agent: * | ||||
| Allow: /$ | ||||
| Disallow: / | ||||
|   | ||||
| @@ -1,72 +1,51 @@ | ||||
| const fs = require("fs"); | ||||
| const { exec, execSync } = require("child_process"); | ||||
| const core = require("@actions/core"); | ||||
|  | ||||
| const excalidrawDir = `${__dirname}/../src/packages/excalidraw`; | ||||
| const excalidrawPackage = `${excalidrawDir}/package.json`; | ||||
| const pkg = require(excalidrawPackage); | ||||
| const isPreview = process.argv.slice(2)[0] === "preview"; | ||||
|  | ||||
| const getShortCommitHash = () => { | ||||
|   return execSync("git rev-parse --short HEAD").toString().trim(); | ||||
| }; | ||||
|  | ||||
| const publish = () => { | ||||
|   const tag = isPreview ? "preview" : "next"; | ||||
|  | ||||
|   try { | ||||
|     execSync(`yarn  --frozen-lockfile`); | ||||
|     execSync(`yarn --frozen-lockfile`, { cwd: excalidrawDir }); | ||||
|     execSync(`yarn run build:umd`, { cwd: excalidrawDir }); | ||||
|     execSync(`yarn --cwd ${excalidrawDir} publish --tag ${tag}`); | ||||
|     console.info(`Published ${pkg.name}@${tag}🎉`); | ||||
|     core.setOutput( | ||||
|       "result", | ||||
|       `**Preview version has been shipped** :rocket: | ||||
|     You can use [@excalidraw/excalidraw@${pkg.version}](https://www.npmjs.com/package/@excalidraw/excalidraw/v/${pkg.version}) for testing!`, | ||||
|     ); | ||||
|   } catch (error) { | ||||
|     core.setOutput("result", "package couldn't be published :warning:!"); | ||||
|     console.error(error); | ||||
|     process.exit(1); | ||||
|     execSync(`yarn --cwd ${excalidrawDir} publish`); | ||||
|   } catch (e) { | ||||
|     console.error(e); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| // get files changed between prev and head commit | ||||
| exec(`git diff --name-only HEAD^ HEAD`, async (error, stdout, stderr) => { | ||||
|   if (error || stderr) { | ||||
|     console.error(error); | ||||
|     core.setOutput("result", ":warning: Package couldn't be published!"); | ||||
|     process.exit(1); | ||||
|   } | ||||
|  | ||||
|   const changedFiles = stdout.trim().split("\n"); | ||||
|   const filesToIgnoreRegex = /src\/excalidraw-app|packages\/utils/; | ||||
|  | ||||
|   const excalidrawPackageFiles = changedFiles.filter((file) => { | ||||
|     return ( | ||||
|       (file.indexOf("src") >= 0 || file.indexOf("package.json")) >= 0 && | ||||
|       !filesToIgnoreRegex.test(file) | ||||
|     ); | ||||
|     return file.indexOf("src") >= 0 && !filesToIgnoreRegex.test(file); | ||||
|   }); | ||||
|  | ||||
|   if (!excalidrawPackageFiles.length) { | ||||
|     console.info("Skipping release as no valid diff found"); | ||||
|     core.setOutput("result", "Skipping release as no valid diff found"); | ||||
|     process.exit(0); | ||||
|   } | ||||
|  | ||||
|   // update package.json | ||||
|   let version = `${pkg.version}-${getShortCommitHash()}`; | ||||
|  | ||||
|   // update readme | ||||
|  | ||||
|   if (isPreview) { | ||||
|     // use pullNumber-commithash as the version for preview | ||||
|     const pullRequestNumber = process.argv.slice(3)[0]; | ||||
|     version = `${pkg.version}-${pullRequestNumber}-${getShortCommitHash()}`; | ||||
|   } | ||||
|   pkg.version = version; | ||||
|  | ||||
|   pkg.version = `${pkg.version}-${getShortCommitHash()}`; | ||||
|   pkg.name = "@excalidraw/excalidraw-next"; | ||||
|   fs.writeFileSync(excalidrawPackage, JSON.stringify(pkg, null, 2), "utf8"); | ||||
|  | ||||
|   console.info("Publish in progress..."); | ||||
|   // update readme | ||||
|   const data = fs.readFileSync(`${excalidrawDir}/README_NEXT.md`, "utf8"); | ||||
|   fs.writeFileSync(`${excalidrawDir}/README.md`, data, "utf8"); | ||||
|  | ||||
|   publish(); | ||||
| }); | ||||
|   | ||||
| @@ -1,16 +1,11 @@ | ||||
| const { readdirSync, writeFileSync } = require("fs"); | ||||
| const files = readdirSync(`${__dirname}/../src/locales`); | ||||
|  | ||||
| const flatten = (object = {}, result = {}, extraKey = "") => { | ||||
|   for (const key in object) { | ||||
|     if (typeof object[key] !== "object") { | ||||
|       result[extraKey + key] = object[key]; | ||||
|     } else { | ||||
|       flatten(object[key], result, `${extraKey}${key}.`); | ||||
|     } | ||||
|   } | ||||
|   return result; | ||||
| }; | ||||
| const flatten = (object) => | ||||
|   Object.keys(object).reduce( | ||||
|     (initial, current) => ({ ...initial, ...object[current] }), | ||||
|     {}, | ||||
|   ); | ||||
|  | ||||
| const locales = files.filter( | ||||
|   (file) => file !== "README.md" && file !== "percentages.json", | ||||
| @@ -24,8 +19,10 @@ for (let index = 0; index < locales.length; index++) { | ||||
|  | ||||
|   const allKeys = Object.keys(data); | ||||
|   const translatedKeys = allKeys.filter((item) => data[item] !== ""); | ||||
|   const percentage = Math.floor((100 * translatedKeys.length) / allKeys.length); | ||||
|   percentages[currentLocale.replace(".json", "")] = percentage; | ||||
|  | ||||
|   const percentage = (100 * translatedKeys.length) / allKeys.length; | ||||
|  | ||||
|   percentages[currentLocale.replace(".json", "")] = parseInt(percentage); | ||||
| } | ||||
|  | ||||
| writeFileSync( | ||||
|   | ||||
| @@ -1,21 +0,0 @@ | ||||
| const { exec } = require("child_process"); | ||||
|  | ||||
| // get files changed between prev and head commit | ||||
| exec(`git diff --name-only HEAD^ HEAD`, async (error, stdout, stderr) => { | ||||
|   if (error || stderr) { | ||||
|     console.error(error); | ||||
|     process.exit(1); | ||||
|   } | ||||
|   const changedFiles = stdout.trim().split("\n"); | ||||
|  | ||||
|   const docFiles = changedFiles.filter((file) => { | ||||
|     return file.indexOf("docs") >= 0; | ||||
|   }); | ||||
|  | ||||
|   if (!docFiles.length) { | ||||
|     console.info("Skipping building docs as no valid diff found"); | ||||
|     process.exit(0); | ||||
|   } | ||||
|   // Exit code 1 to build the docs in ignoredBuildStep | ||||
|   process.exit(1); | ||||
| }); | ||||
| @@ -5,17 +5,13 @@ const THRESSHOLD = 85; | ||||
| const crowdinMap = { | ||||
|   "ar-SA": "en-ar", | ||||
|   "bg-BG": "en-bg", | ||||
|   "bn-BD": "en-bn", | ||||
|   "ca-ES": "en-ca", | ||||
|   "da-DK": "en-da", | ||||
|   "de-DE": "en-de", | ||||
|   "el-GR": "en-el", | ||||
|   "es-ES": "en-es", | ||||
|   "eu-ES": "en-eu", | ||||
|   "fa-IR": "en-fa", | ||||
|   "fi-FI": "en-fi", | ||||
|   "fr-FR": "en-fr", | ||||
|   "gl-ES": "en-gl", | ||||
|   "he-IL": "en-he", | ||||
|   "hi-IN": "en-hi", | ||||
|   "hu-HU": "en-hu", | ||||
| @@ -24,7 +20,6 @@ const crowdinMap = { | ||||
|   "ja-JP": "en-ja", | ||||
|   "kab-KAB": "en-kab", | ||||
|   "ko-KR": "en-ko", | ||||
|   "ku-TR": "en-ku", | ||||
|   "my-MM": "en-my", | ||||
|   "nb-NO": "en-nb", | ||||
|   "nl-NL": "en-nl", | ||||
| @@ -36,38 +31,25 @@ const crowdinMap = { | ||||
|   "pt-PT": "en-pt", | ||||
|   "ro-RO": "en-ro", | ||||
|   "ru-RU": "en-ru", | ||||
|   "si-LK": "en-silk", | ||||
|   "sk-SK": "en-sk", | ||||
|   "sl-SI": "en-sl", | ||||
|   "sv-SE": "en-sv", | ||||
|   "ta-IN": "en-ta", | ||||
|   "tr-TR": "en-tr", | ||||
|   "uk-UA": "en-uk", | ||||
|   "zh-CN": "en-zhcn", | ||||
|   "zh-HK": "en-zhhk", | ||||
|   "zh-TW": "en-zhtw", | ||||
|   "lt-LT": "en-lt", | ||||
|   "lv-LV": "en-lv", | ||||
|   "cs-CZ": "en-cs", | ||||
|   "kk-KZ": "en-kk", | ||||
|   "vi-VN": "en-vi", | ||||
|   "mr-IN": "en-mr", | ||||
| }; | ||||
|  | ||||
| const flags = { | ||||
|   "ar-SA": "🇸🇦", | ||||
|   "bg-BG": "🇧🇬", | ||||
|   "bn-BD": "🇧🇩", | ||||
|   "ca-ES": "🏳", | ||||
|   "cs-CZ": "🇨🇿", | ||||
|   "da-DK": "🇩🇰", | ||||
|   "de-DE": "🇩🇪", | ||||
|   "el-GR": "🇬🇷", | ||||
|   "es-ES": "🇪🇸", | ||||
|   "fa-IR": "🇮🇷", | ||||
|   "fi-FI": "🇫🇮", | ||||
|   "fr-FR": "🇫🇷", | ||||
|   "gl-ES": "🇪🇸", | ||||
|   "he-IL": "🇮🇱", | ||||
|   "hi-IN": "🇮🇳", | ||||
|   "hu-HU": "🇭🇺", | ||||
| @@ -75,11 +57,7 @@ const flags = { | ||||
|   "it-IT": "🇮🇹", | ||||
|   "ja-JP": "🇯🇵", | ||||
|   "kab-KAB": "🏳", | ||||
|   "kk-KZ": "🇰🇿", | ||||
|   "ko-KR": "🇰🇷", | ||||
|   "ku-TR": "🏳", | ||||
|   "lt-LT": "🇱🇹", | ||||
|   "lv-LV": "🇱🇻", | ||||
|   "my-MM": "🇲🇲", | ||||
|   "nb-NO": "🇳🇴", | ||||
|   "nl-NL": "🇳🇱", | ||||
| @@ -91,36 +69,25 @@ const flags = { | ||||
|   "pt-PT": "🇵🇹", | ||||
|   "ro-RO": "🇷🇴", | ||||
|   "ru-RU": "🇷🇺", | ||||
|   "si-LK": "🇱🇰", | ||||
|   "sk-SK": "🇸🇰", | ||||
|   "sl-SI": "🇸🇮", | ||||
|   "sv-SE": "🇸🇪", | ||||
|   "ta-IN": "🇮🇳", | ||||
|   "tr-TR": "🇹🇷", | ||||
|   "uk-UA": "🇺🇦", | ||||
|   "zh-CN": "🇨🇳", | ||||
|   "zh-HK": "🇭🇰", | ||||
|   "zh-TW": "🇹🇼", | ||||
|   "eu-ES": "🇪🇦", | ||||
|   "vi-VN": "🇻🇳", | ||||
|   "mr-IN": "🇮🇳", | ||||
|   "lv-LV": "🇱🇻", | ||||
| }; | ||||
|  | ||||
| const languages = { | ||||
|   "ar-SA": "العربية", | ||||
|   "bg-BG": "Български", | ||||
|   "bn-BD": "Bengali", | ||||
|   "ca-ES": "Català", | ||||
|   "cs-CZ": "Česky", | ||||
|   "da-DK": "Dansk", | ||||
|   "de-DE": "Deutsch", | ||||
|   "el-GR": "Ελληνικά", | ||||
|   "es-ES": "Español", | ||||
|   "eu-ES": "Euskara", | ||||
|   "fa-IR": "فارسی", | ||||
|   "fi-FI": "Suomi", | ||||
|   "fr-FR": "Français", | ||||
|   "gl-ES": "Galego", | ||||
|   "he-IL": "עברית", | ||||
|   "hi-IN": "हिन्दी", | ||||
|   "hu-HU": "Magyar", | ||||
| @@ -128,11 +95,7 @@ const languages = { | ||||
|   "it-IT": "Italiano", | ||||
|   "ja-JP": "日本語", | ||||
|   "kab-KAB": "Taqbaylit", | ||||
|   "kk-KZ": "Қазақ тілі", | ||||
|   "ko-KR": "한국어", | ||||
|   "ku-TR": "Kurdî", | ||||
|   "lt-LT": "Lietuvių", | ||||
|   "lv-LV": "Latviešu", | ||||
|   "my-MM": "Burmese", | ||||
|   "nb-NO": "Norsk bokmål", | ||||
|   "nl-NL": "Nederlands", | ||||
| @@ -144,18 +107,13 @@ const languages = { | ||||
|   "pt-PT": "Português", | ||||
|   "ro-RO": "Română", | ||||
|   "ru-RU": "Русский", | ||||
|   "si-LK": "සිංහල", | ||||
|   "sk-SK": "Slovenčina", | ||||
|   "sl-SI": "Slovenščina", | ||||
|   "sv-SE": "Svenska", | ||||
|   "ta-IN": "Tamil", | ||||
|   "tr-TR": "Türkçe", | ||||
|   "uk-UA": "Українська", | ||||
|   "zh-CN": "简体中文", | ||||
|   "zh-HK": "繁體中文 (香港)", | ||||
|   "zh-TW": "繁體中文", | ||||
|   "vi-VN": "Tiếng Việt", | ||||
|   "mr-IN": "मराठी", | ||||
|   "lv-LV": "Latviešu", | ||||
| }; | ||||
|  | ||||
| const percentages = fs.readFileSync( | ||||
|   | ||||
| @@ -1,37 +0,0 @@ | ||||
| const fs = require("fs"); | ||||
| const util = require("util"); | ||||
| const exec = util.promisify(require("child_process").exec); | ||||
| const updateChangelog = require("./updateChangelog"); | ||||
|  | ||||
| const excalidrawDir = `${__dirname}/../src/packages/excalidraw`; | ||||
| const excalidrawPackage = `${excalidrawDir}/package.json`; | ||||
|  | ||||
| const updatePackageVersion = (nextVersion) => { | ||||
|   const pkg = require(excalidrawPackage); | ||||
|   pkg.version = nextVersion; | ||||
|   const content = `${JSON.stringify(pkg, null, 2)}\n`; | ||||
|   fs.writeFileSync(excalidrawPackage, content, "utf-8"); | ||||
| }; | ||||
|  | ||||
| const prerelease = async (nextVersion) => { | ||||
|   try { | ||||
|     await updateChangelog(nextVersion); | ||||
|     updatePackageVersion(nextVersion); | ||||
|     await exec(`git add -u`); | ||||
|     await exec( | ||||
|       `git commit -m "docs: release @excalidraw/excalidraw@${nextVersion}  🎉"`, | ||||
|     ); | ||||
|  | ||||
|     console.info("Done!"); | ||||
|   } catch (error) { | ||||
|     console.error(error); | ||||
|     process.exit(1); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const nextVersion = process.argv.slice(2)[0]; | ||||
| if (!nextVersion) { | ||||
|   console.error("Pass the next version to release!"); | ||||
|   process.exit(1); | ||||
| } | ||||
| prerelease(nextVersion); | ||||
| @@ -1,44 +0,0 @@ | ||||
| const fs = require("fs"); | ||||
| const { execSync } = require("child_process"); | ||||
|  | ||||
| const excalidrawDir = `${__dirname}/../src/packages/excalidraw`; | ||||
| const excalidrawPackage = `${excalidrawDir}/package.json`; | ||||
| const pkg = require(excalidrawPackage); | ||||
|  | ||||
| const originalReadMe = fs.readFileSync(`${excalidrawDir}/README.md`, "utf8"); | ||||
|  | ||||
| const updateReadme = () => { | ||||
|   const excalidrawIndex = originalReadMe.indexOf("### Excalidraw"); | ||||
|  | ||||
|   // remove note for stable readme | ||||
|   const data = originalReadMe.slice(excalidrawIndex); | ||||
|  | ||||
|   // update readme | ||||
|   fs.writeFileSync(`${excalidrawDir}/README.md`, data, "utf8"); | ||||
| }; | ||||
|  | ||||
| const publish = () => { | ||||
|   try { | ||||
|     execSync(`yarn  --frozen-lockfile`); | ||||
|     execSync(`yarn --frozen-lockfile`, { cwd: excalidrawDir }); | ||||
|     execSync(`yarn run build:umd`, { cwd: excalidrawDir }); | ||||
|     execSync(`yarn --cwd ${excalidrawDir} publish`); | ||||
|   } catch (error) { | ||||
|     console.error(error); | ||||
|     process.exit(1); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const release = () => { | ||||
|   updateReadme(); | ||||
|   console.info("Note for stable readme removed"); | ||||
|  | ||||
|   publish(); | ||||
|   console.info(`Published ${pkg.version}!`); | ||||
|  | ||||
|   // revert readme after release | ||||
|   fs.writeFileSync(`${excalidrawDir}/README.md`, originalReadMe, "utf8"); | ||||
|   console.info("Readme reverted"); | ||||
| }; | ||||
|  | ||||
| release(); | ||||
| @@ -1,104 +0,0 @@ | ||||
| const fs = require("fs"); | ||||
| const util = require("util"); | ||||
| const exec = util.promisify(require("child_process").exec); | ||||
|  | ||||
| const excalidrawDir = `${__dirname}/../src/packages/excalidraw`; | ||||
| const excalidrawPackage = `${excalidrawDir}/package.json`; | ||||
| const pkg = require(excalidrawPackage); | ||||
| const lastVersion = pkg.version; | ||||
| const existingChangeLog = fs.readFileSync( | ||||
|   `${excalidrawDir}/CHANGELOG.md`, | ||||
|   "utf8", | ||||
| ); | ||||
|  | ||||
| const supportedTypes = ["feat", "fix", "style", "refactor", "perf", "build"]; | ||||
| const headerForType = { | ||||
|   feat: "Features", | ||||
|   fix: "Fixes", | ||||
|   style: "Styles", | ||||
|   refactor: " Refactor", | ||||
|   perf: "Performance", | ||||
|   build: "Build", | ||||
| }; | ||||
| const badCommits = []; | ||||
| const getCommitHashForLastVersion = async () => { | ||||
|   try { | ||||
|     const commitMessage = `"release @excalidraw/excalidraw@${lastVersion}"`; | ||||
|     const { stdout } = await exec( | ||||
|       `git log --format=format:"%H" --grep=${commitMessage}`, | ||||
|     ); | ||||
|     return stdout; | ||||
|   } catch (error) { | ||||
|     console.error(error); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const getLibraryCommitsSinceLastRelease = async () => { | ||||
|   const commitHash = await getCommitHashForLastVersion(); | ||||
|   const { stdout } = await exec( | ||||
|     `git log --pretty=format:%s ${commitHash}...master`, | ||||
|   ); | ||||
|   const commitsSinceLastRelease = stdout.split("\n"); | ||||
|   const commitList = {}; | ||||
|   supportedTypes.forEach((type) => { | ||||
|     commitList[type] = []; | ||||
|   }); | ||||
|  | ||||
|   commitsSinceLastRelease.forEach((commit) => { | ||||
|     const indexOfColon = commit.indexOf(":"); | ||||
|     const type = commit.slice(0, indexOfColon); | ||||
|     if (!supportedTypes.includes(type)) { | ||||
|       return; | ||||
|     } | ||||
|     const messageWithoutType = commit.slice(indexOfColon + 1).trim(); | ||||
|     const messageWithCapitalizeFirst = | ||||
|       messageWithoutType.charAt(0).toUpperCase() + messageWithoutType.slice(1); | ||||
|     const prMatch = commit.match(/\(#([0-9]*)\)/); | ||||
|     if (prMatch) { | ||||
|       const prNumber = prMatch[1]; | ||||
|  | ||||
|       // return if the changelog already contains the pr number which would happen for package updates | ||||
|       if (existingChangeLog.includes(prNumber)) { | ||||
|         return; | ||||
|       } | ||||
|       const prMarkdown = `[#${prNumber}](https://github.com/excalidraw/excalidraw/pull/${prNumber})`; | ||||
|       const messageWithPRLink = messageWithCapitalizeFirst.replace( | ||||
|         /\(#[0-9]*\)/, | ||||
|         prMarkdown, | ||||
|       ); | ||||
|       commitList[type].push(messageWithPRLink); | ||||
|     } else { | ||||
|       badCommits.push(commit); | ||||
|       commitList[type].push(messageWithCapitalizeFirst); | ||||
|     } | ||||
|   }); | ||||
|   console.info("Bad commits:", badCommits); | ||||
|   return commitList; | ||||
| }; | ||||
|  | ||||
| const updateChangelog = async (nextVersion) => { | ||||
|   const commitList = await getLibraryCommitsSinceLastRelease(); | ||||
|   let changelogForLibrary = | ||||
|     "## Excalidraw Library\n\n**_This section lists the updates made to the excalidraw library and will not affect the integration._**\n\n"; | ||||
|   supportedTypes.forEach((type) => { | ||||
|     if (commitList[type].length) { | ||||
|       changelogForLibrary += `### ${headerForType[type]}\n\n`; | ||||
|       const commits = commitList[type]; | ||||
|       commits.forEach((commit) => { | ||||
|         changelogForLibrary += `- ${commit}\n\n`; | ||||
|       }); | ||||
|     } | ||||
|   }); | ||||
|   changelogForLibrary += "---\n"; | ||||
|   const lastVersionIndex = existingChangeLog.indexOf(`## ${lastVersion}`); | ||||
|   let updatedContent = | ||||
|     existingChangeLog.slice(0, lastVersionIndex) + | ||||
|     changelogForLibrary + | ||||
|     existingChangeLog.slice(lastVersionIndex); | ||||
|   const currentDate = new Date().toISOString().slice(0, 10); | ||||
|   const newVersion = `## ${nextVersion} (${currentDate})`; | ||||
|   updatedContent = updatedContent.replace(`## Unreleased`, newVersion); | ||||
|   fs.writeFileSync(`${excalidrawDir}/CHANGELOG.md`, updatedContent, "utf8"); | ||||
| }; | ||||
|  | ||||
| module.exports = updateChangelog; | ||||
| @@ -2,59 +2,22 @@ import { register } from "./register"; | ||||
| import { getSelectedElements } from "../scene"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { deepCopyElement } from "../element/newElement"; | ||||
| import { randomId } from "../random"; | ||||
| import { t } from "../i18n"; | ||||
|  | ||||
| export const actionAddToLibrary = register({ | ||||
|   name: "addToLibrary", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState, _, app) => { | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     ); | ||||
|     if (selectedElements.some((element) => element.type === "image")) { | ||||
|       return { | ||||
|         commitToHistory: false, | ||||
|         appState: { | ||||
|           ...appState, | ||||
|           errorMessage: "Support for adding images to the library coming soon!", | ||||
|         }, | ||||
|       }; | ||||
|     } | ||||
|  | ||||
|     return app.library | ||||
|       .getLatestLibrary() | ||||
|       .then((items) => { | ||||
|         return app.library.setLibrary([ | ||||
|           { | ||||
|             id: randomId(), | ||||
|             status: "unpublished", | ||||
|             elements: selectedElements.map(deepCopyElement), | ||||
|             created: Date.now(), | ||||
|           }, | ||||
|           ...items, | ||||
|         ]); | ||||
|       }) | ||||
|       .then(() => { | ||||
|         return { | ||||
|           commitToHistory: false, | ||||
|           appState: { | ||||
|             ...appState, | ||||
|             toast: { message: t("toast.addedToLibrary") }, | ||||
|           }, | ||||
|         }; | ||||
|       }) | ||||
|       .catch((error) => { | ||||
|         return { | ||||
|           commitToHistory: false, | ||||
|           appState: { | ||||
|             ...appState, | ||||
|             errorMessage: error.message, | ||||
|           }, | ||||
|         }; | ||||
|       }); | ||||
|     app.library.loadLibrary().then((items) => { | ||||
|       app.library.saveLibrary([ | ||||
|         ...items, | ||||
|         selectedElements.map(deepCopyElement), | ||||
|       ]); | ||||
|     }); | ||||
|     return false; | ||||
|   }, | ||||
|   contextItemLabel: "labels.addToLibrary", | ||||
| }); | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| import React from "react"; | ||||
| import { alignElements, Alignment } from "../align"; | ||||
| import { | ||||
|   AlignBottomIcon, | ||||
| @@ -8,13 +9,13 @@ import { | ||||
|   CenterVerticallyIcon, | ||||
| } from "../components/icons"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { getElementMap, getNonDeletedElements } from "../element"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { t } from "../i18n"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { getSelectedElements, isSomeElementSelected } from "../scene"; | ||||
| import { AppState } from "../types"; | ||||
| import { arrayToMap, getShortcutKey } from "../utils"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { register } from "./register"; | ||||
|  | ||||
| const enableActionGroup = ( | ||||
| @@ -34,16 +35,13 @@ const alignSelectedElements = ( | ||||
|  | ||||
|   const updatedElements = alignElements(selectedElements, alignment); | ||||
|  | ||||
|   const updatedElementsMap = arrayToMap(updatedElements); | ||||
|   const updatedElementsMap = getElementMap(updatedElements); | ||||
|  | ||||
|   return elements.map( | ||||
|     (element) => updatedElementsMap.get(element.id) || element, | ||||
|   ); | ||||
|   return elements.map((element) => updatedElementsMap[element.id] || element); | ||||
| }; | ||||
|  | ||||
| export const actionAlignTop = register({ | ||||
|   name: "alignTop", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -60,7 +58,7 @@ export const actionAlignTop = register({ | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={AlignTopIcon} | ||||
|       icon={<AlignTopIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.alignTop")} — ${getShortcutKey( | ||||
|         "CtrlOrCmd+Shift+Up", | ||||
| @@ -73,7 +71,6 @@ export const actionAlignTop = register({ | ||||
|  | ||||
| export const actionAlignBottom = register({ | ||||
|   name: "alignBottom", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -90,7 +87,7 @@ export const actionAlignBottom = register({ | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={AlignBottomIcon} | ||||
|       icon={<AlignBottomIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.alignBottom")} — ${getShortcutKey( | ||||
|         "CtrlOrCmd+Shift+Down", | ||||
| @@ -103,7 +100,6 @@ export const actionAlignBottom = register({ | ||||
|  | ||||
| export const actionAlignLeft = register({ | ||||
|   name: "alignLeft", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -120,7 +116,7 @@ export const actionAlignLeft = register({ | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={AlignLeftIcon} | ||||
|       icon={<AlignLeftIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.alignLeft")} — ${getShortcutKey( | ||||
|         "CtrlOrCmd+Shift+Left", | ||||
| @@ -133,8 +129,6 @@ export const actionAlignLeft = register({ | ||||
|  | ||||
| export const actionAlignRight = register({ | ||||
|   name: "alignRight", | ||||
|   trackEvent: { category: "element" }, | ||||
|  | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -151,7 +145,7 @@ export const actionAlignRight = register({ | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={AlignRightIcon} | ||||
|       icon={<AlignRightIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.alignRight")} — ${getShortcutKey( | ||||
|         "CtrlOrCmd+Shift+Right", | ||||
| @@ -164,8 +158,6 @@ export const actionAlignRight = register({ | ||||
|  | ||||
| export const actionAlignVerticallyCentered = register({ | ||||
|   name: "alignVerticallyCentered", | ||||
|   trackEvent: { category: "element" }, | ||||
|  | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -180,7 +172,7 @@ export const actionAlignVerticallyCentered = register({ | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={CenterVerticallyIcon} | ||||
|       icon={<CenterVerticallyIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={t("labels.centerVertically")} | ||||
|       aria-label={t("labels.centerVertically")} | ||||
| @@ -191,7 +183,6 @@ export const actionAlignVerticallyCentered = register({ | ||||
|  | ||||
| export const actionAlignHorizontallyCentered = register({ | ||||
|   name: "alignHorizontallyCentered", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -206,7 +197,7 @@ export const actionAlignHorizontallyCentered = register({ | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={CenterHorizontallyIcon} | ||||
|       icon={<CenterHorizontallyIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={t("labels.centerHorizontally")} | ||||
|       aria-label={t("labels.centerHorizontally")} | ||||
|   | ||||
| @@ -1,148 +0,0 @@ | ||||
| import { VERTICAL_ALIGN } from "../constants"; | ||||
| import { getNonDeletedElements, isTextElement } from "../element"; | ||||
| import { mutateElement } from "../element/mutateElement"; | ||||
| import { | ||||
|   getBoundTextElement, | ||||
|   measureText, | ||||
|   redrawTextBoundingBox, | ||||
| } from "../element/textElement"; | ||||
| import { | ||||
|   getOriginalContainerHeightFromCache, | ||||
|   resetOriginalContainerCache, | ||||
| } from "../element/textWysiwyg"; | ||||
| import { | ||||
|   hasBoundTextElement, | ||||
|   isTextBindableContainer, | ||||
| } from "../element/typeChecks"; | ||||
| import { | ||||
|   ExcalidrawTextContainer, | ||||
|   ExcalidrawTextElement, | ||||
| } from "../element/types"; | ||||
| import { getSelectedElements } from "../scene"; | ||||
| import { getFontString } from "../utils"; | ||||
| import { register } from "./register"; | ||||
|  | ||||
| export const actionUnbindText = register({ | ||||
|   name: "unbindText", | ||||
|   contextItemLabel: "labels.unbindText", | ||||
|   trackEvent: { category: "element" }, | ||||
|   predicate: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements(elements, appState); | ||||
|     return selectedElements.some((element) => hasBoundTextElement(element)); | ||||
|   }, | ||||
|   perform: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|     ); | ||||
|     selectedElements.forEach((element) => { | ||||
|       const boundTextElement = getBoundTextElement(element); | ||||
|       if (boundTextElement) { | ||||
|         const { width, height, baseline } = measureText( | ||||
|           boundTextElement.originalText, | ||||
|           getFontString(boundTextElement), | ||||
|         ); | ||||
|         const originalContainerHeight = getOriginalContainerHeightFromCache( | ||||
|           element.id, | ||||
|         ); | ||||
|         resetOriginalContainerCache(element.id); | ||||
|  | ||||
|         mutateElement(boundTextElement as ExcalidrawTextElement, { | ||||
|           containerId: null, | ||||
|           width, | ||||
|           height, | ||||
|           baseline, | ||||
|           text: boundTextElement.originalText, | ||||
|         }); | ||||
|         mutateElement(element, { | ||||
|           boundElements: element.boundElements?.filter( | ||||
|             (ele) => ele.id !== boundTextElement.id, | ||||
|           ), | ||||
|           height: originalContainerHeight | ||||
|             ? originalContainerHeight | ||||
|             : element.height, | ||||
|         }); | ||||
|       } | ||||
|     }); | ||||
|     return { | ||||
|       elements, | ||||
|       appState, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
| }); | ||||
|  | ||||
| export const actionBindText = register({ | ||||
|   name: "bindText", | ||||
|   contextItemLabel: "labels.bindText", | ||||
|   trackEvent: { category: "element" }, | ||||
|   predicate: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements(elements, appState); | ||||
|  | ||||
|     if (selectedElements.length === 2) { | ||||
|       const textElement = | ||||
|         isTextElement(selectedElements[0]) || | ||||
|         isTextElement(selectedElements[1]); | ||||
|  | ||||
|       let bindingContainer; | ||||
|       if (isTextBindableContainer(selectedElements[0])) { | ||||
|         bindingContainer = selectedElements[0]; | ||||
|       } else if (isTextBindableContainer(selectedElements[1])) { | ||||
|         bindingContainer = selectedElements[1]; | ||||
|       } | ||||
|       if ( | ||||
|         textElement && | ||||
|         bindingContainer && | ||||
|         getBoundTextElement(bindingContainer) === null | ||||
|       ) { | ||||
|         return true; | ||||
|       } | ||||
|     } | ||||
|     return false; | ||||
|   }, | ||||
|   perform: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|     ); | ||||
|  | ||||
|     let textElement: ExcalidrawTextElement; | ||||
|     let container: ExcalidrawTextContainer; | ||||
|  | ||||
|     if ( | ||||
|       isTextElement(selectedElements[0]) && | ||||
|       isTextBindableContainer(selectedElements[1]) | ||||
|     ) { | ||||
|       textElement = selectedElements[0]; | ||||
|       container = selectedElements[1]; | ||||
|     } else { | ||||
|       textElement = selectedElements[1] as ExcalidrawTextElement; | ||||
|       container = selectedElements[0] as ExcalidrawTextContainer; | ||||
|     } | ||||
|     mutateElement(textElement, { | ||||
|       containerId: container.id, | ||||
|       verticalAlign: VERTICAL_ALIGN.MIDDLE, | ||||
|     }); | ||||
|     mutateElement(container, { | ||||
|       boundElements: (container.boundElements || []).concat({ | ||||
|         type: "text", | ||||
|         id: textElement.id, | ||||
|       }), | ||||
|     }); | ||||
|     redrawTextBoundingBox(textElement, container); | ||||
|     const updatedElements = elements.slice(); | ||||
|     const textElementIndex = updatedElements.findIndex( | ||||
|       (ele) => ele.id === textElement.id, | ||||
|     ); | ||||
|     updatedElements.splice(textElementIndex, 1); | ||||
|     const containerIndex = updatedElements.findIndex( | ||||
|       (ele) => ele.id === container.id, | ||||
|     ); | ||||
|     updatedElements.splice(containerIndex + 1, 0, textElement); | ||||
|     return { | ||||
|       elements: updatedElements, | ||||
|       appState: { ...appState, selectedElementIds: { [container.id]: true } }, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
| }); | ||||
| @@ -1,56 +1,40 @@ | ||||
| import React from "react"; | ||||
| import { getDefaultAppState } from "../appState"; | ||||
| import { ColorPicker } from "../components/ColorPicker"; | ||||
| import { ZoomInIcon, ZoomOutIcon } from "../components/icons"; | ||||
| import { resetZoom, trash, zoomIn, zoomOut } from "../components/icons"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { CURSOR_TYPE, MIN_ZOOM, THEME, ZOOM_STEP } from "../constants"; | ||||
| import { DarkModeToggle } from "../components/DarkModeToggle"; | ||||
| import { ZOOM_STEP } from "../constants"; | ||||
| import { getCommonBounds, getNonDeletedElements } from "../element"; | ||||
| import { newElementWith } from "../element/mutateElement"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { t } from "../i18n"; | ||||
| import { useIsMobile } from "../components/App"; | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { getNormalizedZoom, getSelectedElements } from "../scene"; | ||||
| import { centerScrollOn } from "../scene/scroll"; | ||||
| import { getStateForZoom } from "../scene/zoom"; | ||||
| import { getNewZoom } from "../scene/zoom"; | ||||
| import { AppState, NormalizedZoomValue } from "../types"; | ||||
| import { getShortcutKey, setCursor, updateActiveTool } from "../utils"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { register } from "./register"; | ||||
| import { Tooltip } from "../components/Tooltip"; | ||||
| import { newElementWith } from "../element/mutateElement"; | ||||
| import { | ||||
|   getDefaultAppState, | ||||
|   isEraserActive, | ||||
|   isHandToolActive, | ||||
| } from "../appState"; | ||||
|  | ||||
| export const actionChangeViewBackgroundColor = register({ | ||||
|   name: "changeViewBackgroundColor", | ||||
|   trackEvent: false, | ||||
|   predicate: (elements, appState, props, app) => { | ||||
|     return ( | ||||
|       !!app.props.UIOptions.canvasActions.changeViewBackgroundColor && | ||||
|       !appState.viewModeEnabled | ||||
|     ); | ||||
|   }, | ||||
|   perform: (_, appState, value) => { | ||||
|     return { | ||||
|       appState: { ...appState, ...value }, | ||||
|       commitToHistory: !!value.viewBackgroundColor, | ||||
|       appState: { ...appState, viewBackgroundColor: value }, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ elements, appState, updateData }) => { | ||||
|     // FIXME move me to src/components/mainMenu/DefaultItems.tsx | ||||
|   PanelComponent: ({ appState, updateData }) => { | ||||
|     return ( | ||||
|       <div style={{ position: "relative" }}> | ||||
|         <ColorPicker | ||||
|           label={t("labels.canvasBackground")} | ||||
|           type="canvasBackground" | ||||
|           color={appState.viewBackgroundColor} | ||||
|           onChange={(color) => updateData({ viewBackgroundColor: color })} | ||||
|           isActive={appState.openPopup === "canvasColorPicker"} | ||||
|           setActive={(active) => | ||||
|             updateData({ openPopup: active ? "canvasColorPicker" : null }) | ||||
|           } | ||||
|           onChange={(color) => updateData(color)} | ||||
|           data-testid="canvas-background-picker" | ||||
|           elements={elements} | ||||
|           appState={appState} | ||||
|         /> | ||||
|       </div> | ||||
|     ); | ||||
| @@ -59,56 +43,54 @@ export const actionChangeViewBackgroundColor = register({ | ||||
|  | ||||
| export const actionClearCanvas = register({ | ||||
|   name: "clearCanvas", | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   predicate: (elements, appState, props, app) => { | ||||
|     return ( | ||||
|       !!app.props.UIOptions.canvasActions.clearCanvas && | ||||
|       !appState.viewModeEnabled | ||||
|     ); | ||||
|   }, | ||||
|   perform: (elements, appState, _, app) => { | ||||
|     app.imageCache.clear(); | ||||
|   perform: (elements, appState: AppState) => { | ||||
|     return { | ||||
|       elements: elements.map((element) => | ||||
|         newElementWith(element, { isDeleted: true }), | ||||
|       ), | ||||
|       appState: { | ||||
|         ...getDefaultAppState(), | ||||
|         files: {}, | ||||
|         theme: appState.theme, | ||||
|         penMode: appState.penMode, | ||||
|         penDetected: appState.penDetected, | ||||
|         elementLocked: appState.elementLocked, | ||||
|         exportBackground: appState.exportBackground, | ||||
|         exportEmbedScene: appState.exportEmbedScene, | ||||
|         gridSize: appState.gridSize, | ||||
|         showStats: appState.showStats, | ||||
|         pasteDialog: appState.pasteDialog, | ||||
|         activeTool: | ||||
|           appState.activeTool.type === "image" | ||||
|             ? { ...appState.activeTool, type: "selection" } | ||||
|             : appState.activeTool, | ||||
|       }, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={trash} | ||||
|       title={t("buttons.clearReset")} | ||||
|       aria-label={t("buttons.clearReset")} | ||||
|       showAriaLabel={useIsMobile()} | ||||
|       onClick={() => { | ||||
|         if (window.confirm(t("alerts.clearReset"))) { | ||||
|           updateData(null); | ||||
|         } | ||||
|       }} | ||||
|       data-testid="clear-canvas-button" | ||||
|     /> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionZoomIn = register({ | ||||
|   name: "zoomIn", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (_elements, appState, _, app) => { | ||||
|   perform: (_elements, appState) => { | ||||
|     const zoom = getNewZoom( | ||||
|       getNormalizedZoom(appState.zoom.value + ZOOM_STEP), | ||||
|       appState.zoom, | ||||
|       { left: appState.offsetLeft, top: appState.offsetTop }, | ||||
|       { x: appState.width / 2, y: appState.height / 2 }, | ||||
|     ); | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         ...getStateForZoom( | ||||
|           { | ||||
|             viewportX: appState.width / 2 + appState.offsetLeft, | ||||
|             viewportY: appState.height / 2 + appState.offsetTop, | ||||
|             nextZoom: getNormalizedZoom(appState.zoom.value + ZOOM_STEP), | ||||
|           }, | ||||
|           appState, | ||||
|         ), | ||||
|         zoom, | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
| @@ -116,8 +98,7 @@ export const actionZoomIn = register({ | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       className="zoom-in-button zoom-button" | ||||
|       icon={ZoomInIcon} | ||||
|       icon={zoomIn} | ||||
|       title={`${t("buttons.zoomIn")} — ${getShortcutKey("CtrlOrCmd++")}`} | ||||
|       aria-label={t("buttons.zoomIn")} | ||||
|       onClick={() => { | ||||
| @@ -132,20 +113,18 @@ export const actionZoomIn = register({ | ||||
|  | ||||
| export const actionZoomOut = register({ | ||||
|   name: "zoomOut", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (_elements, appState, _, app) => { | ||||
|   perform: (_elements, appState) => { | ||||
|     const zoom = getNewZoom( | ||||
|       getNormalizedZoom(appState.zoom.value - ZOOM_STEP), | ||||
|       appState.zoom, | ||||
|       { left: appState.offsetLeft, top: appState.offsetTop }, | ||||
|       { x: appState.width / 2, y: appState.height / 2 }, | ||||
|     ); | ||||
|  | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         ...getStateForZoom( | ||||
|           { | ||||
|             viewportX: appState.width / 2 + appState.offsetLeft, | ||||
|             viewportY: appState.height / 2 + appState.offsetTop, | ||||
|             nextZoom: getNormalizedZoom(appState.zoom.value - ZOOM_STEP), | ||||
|           }, | ||||
|           appState, | ||||
|         ), | ||||
|         zoom, | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
| @@ -153,8 +132,7 @@ export const actionZoomOut = register({ | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       className="zoom-out-button zoom-button" | ||||
|       icon={ZoomOutIcon} | ||||
|       icon={zoomOut} | ||||
|       title={`${t("buttons.zoomOut")} — ${getShortcutKey("CtrlOrCmd+-")}`} | ||||
|       aria-label={t("buttons.zoomOut")} | ||||
|       onClick={() => { | ||||
| @@ -169,38 +147,33 @@ export const actionZoomOut = register({ | ||||
|  | ||||
| export const actionResetZoom = register({ | ||||
|   name: "resetZoom", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (_elements, appState, _, app) => { | ||||
|   perform: (_elements, appState) => { | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         ...getStateForZoom( | ||||
|         zoom: getNewZoom( | ||||
|           1 as NormalizedZoomValue, | ||||
|           appState.zoom, | ||||
|           { left: appState.offsetLeft, top: appState.offsetTop }, | ||||
|           { | ||||
|             viewportX: appState.width / 2 + appState.offsetLeft, | ||||
|             viewportY: appState.height / 2 + appState.offsetTop, | ||||
|             nextZoom: getNormalizedZoom(1), | ||||
|             x: appState.width / 2, | ||||
|             y: appState.height / 2, | ||||
|           }, | ||||
|           appState, | ||||
|         ), | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ updateData, appState }) => ( | ||||
|     <Tooltip label={t("buttons.resetZoom")} style={{ height: "100%" }}> | ||||
|       <ToolButton | ||||
|         type="button" | ||||
|         className="reset-zoom-button zoom-button" | ||||
|         title={t("buttons.resetZoom")} | ||||
|         aria-label={t("buttons.resetZoom")} | ||||
|         onClick={() => { | ||||
|           updateData(null); | ||||
|         }} | ||||
|       > | ||||
|         {(appState.zoom.value * 100).toFixed(0)}% | ||||
|       </ToolButton> | ||||
|     </Tooltip> | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={resetZoom} | ||||
|       title={t("buttons.resetZoom")} | ||||
|       aria-label={t("buttons.resetZoom")} | ||||
|       onClick={() => { | ||||
|         updateData(null); | ||||
|       }} | ||||
|     /> | ||||
|   ), | ||||
|   keyTest: (event) => | ||||
|     (event.code === CODES.ZERO || event.code === CODES.NUM_ZERO) && | ||||
| @@ -220,7 +193,7 @@ const zoomValueToFitBoundsOnViewport = ( | ||||
|   const zoomAdjustedToSteps = | ||||
|     Math.floor(smallestZoomValue / ZOOM_STEP) * ZOOM_STEP; | ||||
|   const clampedZoomValueToFitElements = Math.min( | ||||
|     Math.max(zoomAdjustedToSteps, MIN_ZOOM), | ||||
|     Math.max(zoomAdjustedToSteps, ZOOM_STEP), | ||||
|     1, | ||||
|   ); | ||||
|   return clampedZoomValueToFitElements as NormalizedZoomValue; | ||||
| @@ -239,12 +212,14 @@ const zoomToFitElements = ( | ||||
|       ? getCommonBounds(selectedElements) | ||||
|       : getCommonBounds(nonDeletedElements); | ||||
|  | ||||
|   const newZoom = { | ||||
|     value: zoomValueToFitBoundsOnViewport(commonBounds, { | ||||
|       width: appState.width, | ||||
|       height: appState.height, | ||||
|     }), | ||||
|   }; | ||||
|   const zoomValue = zoomValueToFitBoundsOnViewport(commonBounds, { | ||||
|     width: appState.width, | ||||
|     height: appState.height, | ||||
|   }); | ||||
|   const newZoom = getNewZoom(zoomValue, appState.zoom, { | ||||
|     left: appState.offsetLeft, | ||||
|     top: appState.offsetTop, | ||||
|   }); | ||||
|  | ||||
|   const [x1, y1, x2, y2] = commonBounds; | ||||
|   const centerX = (x1 + x2) / 2; | ||||
| @@ -268,7 +243,6 @@ const zoomToFitElements = ( | ||||
|  | ||||
| export const actionZoomToSelected = register({ | ||||
|   name: "zoomToSelection", | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (elements, appState) => zoomToFitElements(elements, appState, true), | ||||
|   keyTest: (event) => | ||||
|     event.code === CODES.TWO && | ||||
| @@ -279,8 +253,6 @@ export const actionZoomToSelected = register({ | ||||
|  | ||||
| export const actionZoomToFit = register({ | ||||
|   name: "zoomToFit", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (elements, appState) => zoomToFitElements(elements, appState, false), | ||||
|   keyTest: (event) => | ||||
|     event.code === CODES.ONE && | ||||
| @@ -291,87 +263,24 @@ export const actionZoomToFit = register({ | ||||
|  | ||||
| export const actionToggleTheme = register({ | ||||
|   name: "toggleTheme", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (_, appState, value) => { | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         theme: | ||||
|           value || (appState.theme === THEME.LIGHT ? THEME.DARK : THEME.LIGHT), | ||||
|         theme: value || (appState.theme === "light" ? "dark" : "light"), | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ appState, updateData }) => ( | ||||
|     <div style={{ marginInlineStart: "0.25rem" }}> | ||||
|       <DarkModeToggle | ||||
|         value={appState.theme} | ||||
|         onChange={(theme) => { | ||||
|           updateData(theme); | ||||
|         }} | ||||
|       /> | ||||
|     </div> | ||||
|   ), | ||||
|   keyTest: (event) => event.altKey && event.shiftKey && event.code === CODES.D, | ||||
|   predicate: (elements, appState, props, app) => { | ||||
|     return !!app.props.UIOptions.canvasActions.toggleTheme; | ||||
|   }, | ||||
| }); | ||||
|  | ||||
| export const actionToggleEraserTool = register({ | ||||
|   name: "toggleEraserTool", | ||||
|   trackEvent: { category: "toolbar" }, | ||||
|   perform: (elements, appState) => { | ||||
|     let activeTool: AppState["activeTool"]; | ||||
|  | ||||
|     if (isEraserActive(appState)) { | ||||
|       activeTool = updateActiveTool(appState, { | ||||
|         ...(appState.activeTool.lastActiveTool || { | ||||
|           type: "selection", | ||||
|         }), | ||||
|         lastActiveToolBeforeEraser: null, | ||||
|       }); | ||||
|     } else { | ||||
|       activeTool = updateActiveTool(appState, { | ||||
|         type: "eraser", | ||||
|         lastActiveToolBeforeEraser: appState.activeTool, | ||||
|       }); | ||||
|     } | ||||
|  | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         selectedElementIds: {}, | ||||
|         selectedGroupIds: {}, | ||||
|         activeTool, | ||||
|       }, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => event.key === KEYS.E, | ||||
| }); | ||||
|  | ||||
| export const actionToggleHandTool = register({ | ||||
|   name: "toggleHandTool", | ||||
|   trackEvent: { category: "toolbar" }, | ||||
|   perform: (elements, appState, _, app) => { | ||||
|     let activeTool: AppState["activeTool"]; | ||||
|  | ||||
|     if (isHandToolActive(appState)) { | ||||
|       activeTool = updateActiveTool(appState, { | ||||
|         ...(appState.activeTool.lastActiveTool || { | ||||
|           type: "selection", | ||||
|         }), | ||||
|         lastActiveToolBeforeEraser: null, | ||||
|       }); | ||||
|     } else { | ||||
|       activeTool = updateActiveTool(appState, { | ||||
|         type: "hand", | ||||
|         lastActiveToolBeforeEraser: appState.activeTool, | ||||
|       }); | ||||
|       setCursor(app.canvas, CURSOR_TYPE.GRAB); | ||||
|     } | ||||
|  | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         selectedElementIds: {}, | ||||
|         selectedGroupIds: {}, | ||||
|         activeTool, | ||||
|       }, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => event.key === KEYS.H, | ||||
| }); | ||||
|   | ||||
| @@ -1,71 +1,38 @@ | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { | ||||
|   copyTextToSystemClipboard, | ||||
|   copyToClipboard, | ||||
|   probablySupportsClipboardBlob, | ||||
|   probablySupportsClipboardWriteText, | ||||
| } from "../clipboard"; | ||||
| import { copyToClipboard } from "../clipboard"; | ||||
| import { actionDeleteSelected } from "./actionDeleteSelected"; | ||||
| import { getSelectedElements } from "../scene/selection"; | ||||
| import { exportCanvas } from "../data/index"; | ||||
| import { getNonDeletedElements, isTextElement } from "../element"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { t } from "../i18n"; | ||||
|  | ||||
| export const actionCopy = register({ | ||||
|   name: "copy", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState, _, app) => { | ||||
|     const selectedElements = getSelectedElements(elements, appState, true); | ||||
|  | ||||
|     copyToClipboard(selectedElements, appState, app.files); | ||||
|   perform: (elements, appState) => { | ||||
|     copyToClipboard(getNonDeletedElements(elements), appState); | ||||
|  | ||||
|     return { | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   predicate: (elements, appState, appProps, app) => { | ||||
|     return app.device.isMobile && !!navigator.clipboard; | ||||
|   }, | ||||
|   contextItemLabel: "labels.copy", | ||||
|   // don't supply a shortcut since we handle this conditionally via onCopy event | ||||
|   keyTest: undefined, | ||||
| }); | ||||
|  | ||||
| export const actionPaste = register({ | ||||
|   name: "paste", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements: any, appStates: any, data, app) => { | ||||
|     app.pasteFromClipboard(null); | ||||
|     return { | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   predicate: (elements, appState, appProps, app) => { | ||||
|     return app.device.isMobile && !!navigator.clipboard; | ||||
|   }, | ||||
|   contextItemLabel: "labels.paste", | ||||
|   // don't supply a shortcut since we handle this conditionally via onCopy event | ||||
|   keyTest: undefined, | ||||
| }); | ||||
|  | ||||
| export const actionCut = register({ | ||||
|   name: "cut", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState, data, app) => { | ||||
|     actionCopy.perform(elements, appState, data, app); | ||||
|     return actionDeleteSelected.perform(elements, appState); | ||||
|   }, | ||||
|   predicate: (elements, appState, appProps, app) => { | ||||
|     return app.device.isMobile && !!navigator.clipboard; | ||||
|     return actionDeleteSelected.perform(elements, appState, data, app); | ||||
|   }, | ||||
|   contextItemLabel: "labels.cut", | ||||
|   keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === KEYS.X, | ||||
|   keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.X, | ||||
| }); | ||||
|  | ||||
| export const actionCopyAsSvg = register({ | ||||
|   name: "copyAsSvg", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: async (elements, appState, _data, app) => { | ||||
|     if (!app.canvas) { | ||||
|       return { | ||||
| @@ -75,7 +42,6 @@ export const actionCopyAsSvg = register({ | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     ); | ||||
|     try { | ||||
|       await exportCanvas( | ||||
| @@ -84,13 +50,12 @@ export const actionCopyAsSvg = register({ | ||||
|           ? selectedElements | ||||
|           : getNonDeletedElements(elements), | ||||
|         appState, | ||||
|         app.files, | ||||
|         appState, | ||||
|       ); | ||||
|       return { | ||||
|         commitToHistory: false, | ||||
|       }; | ||||
|     } catch (error: any) { | ||||
|     } catch (error) { | ||||
|       console.error(error); | ||||
|       return { | ||||
|         appState: { | ||||
| @@ -101,15 +66,11 @@ export const actionCopyAsSvg = register({ | ||||
|       }; | ||||
|     } | ||||
|   }, | ||||
|   predicate: (elements) => { | ||||
|     return probablySupportsClipboardWriteText && elements.length > 0; | ||||
|   }, | ||||
|   contextItemLabel: "labels.copyAsSvg", | ||||
| }); | ||||
|  | ||||
| export const actionCopyAsPng = register({ | ||||
|   name: "copyAsPng", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: async (elements, appState, _data, app) => { | ||||
|     if (!app.canvas) { | ||||
|       return { | ||||
| @@ -119,7 +80,6 @@ export const actionCopyAsPng = register({ | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     ); | ||||
|     try { | ||||
|       await exportCanvas( | ||||
| @@ -128,26 +88,23 @@ export const actionCopyAsPng = register({ | ||||
|           ? selectedElements | ||||
|           : getNonDeletedElements(elements), | ||||
|         appState, | ||||
|         app.files, | ||||
|         appState, | ||||
|       ); | ||||
|       return { | ||||
|         appState: { | ||||
|           ...appState, | ||||
|           toast: { | ||||
|             message: t("toast.copyToClipboardAsPng", { | ||||
|               exportSelection: selectedElements.length | ||||
|                 ? t("toast.selection") | ||||
|                 : t("toast.canvas"), | ||||
|               exportColorScheme: appState.exportWithDarkMode | ||||
|                 ? t("buttons.darkMode") | ||||
|                 : t("buttons.lightMode"), | ||||
|             }), | ||||
|           }, | ||||
|           toastMessage: t("toast.copyToClipboardAsPng", { | ||||
|             exportSelection: selectedElements.length | ||||
|               ? t("toast.selection") | ||||
|               : t("toast.canvas"), | ||||
|             exportColorScheme: appState.exportWithDarkMode | ||||
|               ? t("buttons.darkMode") | ||||
|               : t("buttons.lightMode"), | ||||
|           }), | ||||
|         }, | ||||
|         commitToHistory: false, | ||||
|       }; | ||||
|     } catch (error: any) { | ||||
|     } catch (error) { | ||||
|       console.error(error); | ||||
|       return { | ||||
|         appState: { | ||||
| @@ -158,41 +115,6 @@ export const actionCopyAsPng = register({ | ||||
|       }; | ||||
|     } | ||||
|   }, | ||||
|   predicate: (elements) => { | ||||
|     return probablySupportsClipboardBlob && elements.length > 0; | ||||
|   }, | ||||
|   contextItemLabel: "labels.copyAsPng", | ||||
|   keyTest: (event) => event.code === CODES.C && event.altKey && event.shiftKey, | ||||
| }); | ||||
|  | ||||
| export const copyText = register({ | ||||
|   name: "copyText", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     ); | ||||
|  | ||||
|     const text = selectedElements | ||||
|       .reduce((acc: string[], element) => { | ||||
|         if (isTextElement(element)) { | ||||
|           acc.push(element.text); | ||||
|         } | ||||
|         return acc; | ||||
|       }, []) | ||||
|       .join("\n\n"); | ||||
|     copyTextToSystemClipboard(text); | ||||
|     return { | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   predicate: (elements, appState) => { | ||||
|     return ( | ||||
|       probablySupportsClipboardWriteText && | ||||
|       getSelectedElements(elements, appState, true).some(isTextElement) | ||||
|     ); | ||||
|   }, | ||||
|   contextItemLabel: "labels.copyText", | ||||
| }); | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| import { isSomeElementSelected } from "../scene"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import React from "react"; | ||||
| import { trash } from "../components/icons"; | ||||
| import { t } from "../i18n"; | ||||
| import { register } from "./register"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| @@ -10,9 +12,6 @@ import { newElementWith } from "../element/mutateElement"; | ||||
| import { getElementsInGroup } from "../groups"; | ||||
| import { LinearElementEditor } from "../element/linearElementEditor"; | ||||
| import { fixBindingsAfterDeletion } from "../element/binding"; | ||||
| import { isBoundToContainer } from "../element/typeChecks"; | ||||
| import { updateActiveTool } from "../utils"; | ||||
| import { TrashIcon } from "../components/icons"; | ||||
|  | ||||
| const deleteSelectedElements = ( | ||||
|   elements: readonly ExcalidrawElement[], | ||||
| @@ -23,12 +22,6 @@ const deleteSelectedElements = ( | ||||
|       if (appState.selectedElementIds[el.id]) { | ||||
|         return newElementWith(el, { isDeleted: true }); | ||||
|       } | ||||
|       if ( | ||||
|         isBoundToContainer(el) && | ||||
|         appState.selectedElementIds[el.containerId] | ||||
|       ) { | ||||
|         return newElementWith(el, { isDeleted: true }); | ||||
|       } | ||||
|       return el; | ||||
|     }), | ||||
|     appState: { | ||||
| @@ -59,12 +52,11 @@ const handleGroupEditingState = ( | ||||
|  | ||||
| export const actionDeleteSelected = register({ | ||||
|   name: "deleteSelectedElements", | ||||
|   trackEvent: { category: "element", action: "delete" }, | ||||
|   perform: (elements, appState) => { | ||||
|     if (appState.editingLinearElement) { | ||||
|       const { | ||||
|         elementId, | ||||
|         selectedPointsIndices, | ||||
|         activePointIndex, | ||||
|         startBindingElement, | ||||
|         endBindingElement, | ||||
|       } = appState.editingLinearElement; | ||||
| @@ -72,22 +64,14 @@ export const actionDeleteSelected = register({ | ||||
|       if (!element) { | ||||
|         return false; | ||||
|       } | ||||
|       // case: no point selected → do nothing, as deleting the whole element | ||||
|       // is most likely a mistake, where you wanted to delete a specific point | ||||
|       // but failed to select it (or you thought it's selected, while it was | ||||
|       // only in a hover state) | ||||
|       if (selectedPointsIndices == null) { | ||||
|         return false; | ||||
|       } | ||||
|  | ||||
|       // case: deleting last remaining point | ||||
|       if (element.points.length < 2) { | ||||
|         const nextElements = elements.map((el) => { | ||||
|           if (el.id === element.id) { | ||||
|             return newElementWith(el, { isDeleted: true }); | ||||
|           } | ||||
|           return el; | ||||
|         }); | ||||
|       if ( | ||||
|         // case: no point selected → delete whole element | ||||
|         activePointIndex == null || | ||||
|         activePointIndex === -1 || | ||||
|         // case: deleting last remaining point | ||||
|         element.points.length < 2 | ||||
|       ) { | ||||
|         const nextElements = elements.filter((el) => el.id !== element.id); | ||||
|         const nextAppState = handleGroupEditingState(appState, nextElements); | ||||
|  | ||||
|         return { | ||||
| @@ -103,17 +87,15 @@ export const actionDeleteSelected = register({ | ||||
|       // We cannot do this inside `movePoint` because it is also called | ||||
|       // when deleting the uncommitted point (which hasn't caused any binding) | ||||
|       const binding = { | ||||
|         startBindingElement: selectedPointsIndices?.includes(0) | ||||
|           ? null | ||||
|           : startBindingElement, | ||||
|         endBindingElement: selectedPointsIndices?.includes( | ||||
|           element.points.length - 1, | ||||
|         ) | ||||
|           ? null | ||||
|           : endBindingElement, | ||||
|         startBindingElement: | ||||
|           activePointIndex === 0 ? null : startBindingElement, | ||||
|         endBindingElement: | ||||
|           activePointIndex === element.points.length - 1 | ||||
|             ? null | ||||
|             : endBindingElement, | ||||
|       }; | ||||
|  | ||||
|       LinearElementEditor.deletePoints(element, selectedPointsIndices); | ||||
|       LinearElementEditor.movePoint(element, activePointIndex, "delete"); | ||||
|  | ||||
|       return { | ||||
|         elements, | ||||
| @@ -122,17 +104,17 @@ export const actionDeleteSelected = register({ | ||||
|           editingLinearElement: { | ||||
|             ...appState.editingLinearElement, | ||||
|             ...binding, | ||||
|             selectedPointsIndices: | ||||
|               selectedPointsIndices?.[0] > 0 | ||||
|                 ? [selectedPointsIndices[0] - 1] | ||||
|                 : [0], | ||||
|             activePointIndex: activePointIndex > 0 ? activePointIndex - 1 : 0, | ||||
|           }, | ||||
|         }, | ||||
|         commitToHistory: true, | ||||
|       }; | ||||
|     } | ||||
|     let { elements: nextElements, appState: nextAppState } = | ||||
|       deleteSelectedElements(elements, appState); | ||||
|  | ||||
|     let { | ||||
|       elements: nextElements, | ||||
|       appState: nextAppState, | ||||
|     } = deleteSelectedElements(elements, appState); | ||||
|     fixBindingsAfterDeletion( | ||||
|       nextElements, | ||||
|       elements.filter(({ id }) => appState.selectedElementIds[id]), | ||||
| @@ -144,7 +126,7 @@ export const actionDeleteSelected = register({ | ||||
|       elements: nextElements, | ||||
|       appState: { | ||||
|         ...nextAppState, | ||||
|         activeTool: updateActiveTool(appState, { type: "selection" }), | ||||
|         elementType: "selection", | ||||
|         multiElement: null, | ||||
|       }, | ||||
|       commitToHistory: isSomeElementSelected( | ||||
| @@ -158,7 +140,7 @@ export const actionDeleteSelected = register({ | ||||
|   PanelComponent: ({ elements, appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={TrashIcon} | ||||
|       icon={trash} | ||||
|       title={t("labels.delete")} | ||||
|       aria-label={t("labels.delete")} | ||||
|       onClick={() => updateData(null)} | ||||
|   | ||||
| @@ -1,16 +1,17 @@ | ||||
| import React from "react"; | ||||
| import { | ||||
|   DistributeHorizontallyIcon, | ||||
|   DistributeVerticallyIcon, | ||||
| } from "../components/icons"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { distributeElements, Distribution } from "../distribute"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { distributeElements, Distribution } from "../disitrubte"; | ||||
| import { getElementMap, getNonDeletedElements } from "../element"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { t } from "../i18n"; | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { CODES } from "../keys"; | ||||
| import { getSelectedElements, isSomeElementSelected } from "../scene"; | ||||
| import { AppState } from "../types"; | ||||
| import { arrayToMap, getShortcutKey } from "../utils"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { register } from "./register"; | ||||
|  | ||||
| const enableActionGroup = ( | ||||
| @@ -30,16 +31,13 @@ const distributeSelectedElements = ( | ||||
|  | ||||
|   const updatedElements = distributeElements(selectedElements, distribution); | ||||
|  | ||||
|   const updatedElementsMap = arrayToMap(updatedElements); | ||||
|   const updatedElementsMap = getElementMap(updatedElements); | ||||
|  | ||||
|   return elements.map( | ||||
|     (element) => updatedElementsMap.get(element.id) || element, | ||||
|   ); | ||||
|   return elements.map((element) => updatedElementsMap[element.id] || element); | ||||
| }; | ||||
|  | ||||
| export const distributeHorizontally = register({ | ||||
|   name: "distributeHorizontally", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -50,13 +48,12 @@ export const distributeHorizontally = register({ | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => | ||||
|     !event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.H, | ||||
|   keyTest: (event) => event.altKey && event.code === CODES.H, | ||||
|   PanelComponent: ({ elements, appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={DistributeHorizontallyIcon} | ||||
|       icon={<DistributeHorizontallyIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.distributeHorizontally")} — ${getShortcutKey( | ||||
|         "Alt+H", | ||||
| @@ -69,7 +66,6 @@ export const distributeHorizontally = register({ | ||||
|  | ||||
| export const distributeVertically = register({ | ||||
|   name: "distributeVertically", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       appState, | ||||
| @@ -80,13 +76,12 @@ export const distributeVertically = register({ | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => | ||||
|     !event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.V, | ||||
|   keyTest: (event) => event.altKey && event.code === CODES.V, | ||||
|   PanelComponent: ({ elements, appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
|       type="button" | ||||
|       icon={DistributeVerticallyIcon} | ||||
|       icon={<DistributeVerticallyIcon theme={appState.theme} />} | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.distributeVertically")} — ${getShortcutKey("Alt+V")}`} | ||||
|       aria-label={t("labels.distributeVertically")} | ||||
|   | ||||
| @@ -1,12 +1,15 @@ | ||||
| import React from "react"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { duplicateElement, getNonDeletedElements } from "../element"; | ||||
| import { getSelectedElements, isSomeElementSelected } from "../scene"; | ||||
| import { isSomeElementSelected } from "../scene"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { clone } from "../components/icons"; | ||||
| import { t } from "../i18n"; | ||||
| import { arrayToMap, getShortcutKey } from "../utils"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { LinearElementEditor } from "../element/linearElementEditor"; | ||||
| import { mutateElement } from "../element/mutateElement"; | ||||
| import { | ||||
|   selectGroupsForSelectedElements, | ||||
|   getSelectedGroupForElement, | ||||
| @@ -16,25 +19,41 @@ import { AppState } from "../types"; | ||||
| import { fixBindingsAfterDuplication } from "../element/binding"; | ||||
| import { ActionResult } from "./types"; | ||||
| import { GRID_SIZE } from "../constants"; | ||||
| import { bindTextToShapeAfterDuplication } from "../element/textElement"; | ||||
| import { isBoundToContainer } from "../element/typeChecks"; | ||||
| import { DuplicateIcon } from "../components/icons"; | ||||
|  | ||||
| export const actionDuplicateSelection = register({ | ||||
|   name: "duplicateSelection", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     // duplicate selected point(s) if editing a line | ||||
|     // duplicate point if selected while editing multi-point element | ||||
|     if (appState.editingLinearElement) { | ||||
|       const ret = LinearElementEditor.duplicateSelectedPoints(appState); | ||||
|  | ||||
|       if (!ret) { | ||||
|       const { activePointIndex, elementId } = appState.editingLinearElement; | ||||
|       const element = LinearElementEditor.getElement(elementId); | ||||
|       if (!element || activePointIndex === null) { | ||||
|         return false; | ||||
|       } | ||||
|  | ||||
|       const { points } = element; | ||||
|       const selectedPoint = points[activePointIndex]; | ||||
|       const nextPoint = points[activePointIndex + 1]; | ||||
|       mutateElement(element, { | ||||
|         points: [ | ||||
|           ...points.slice(0, activePointIndex + 1), | ||||
|           nextPoint | ||||
|             ? [ | ||||
|                 (selectedPoint[0] + nextPoint[0]) / 2, | ||||
|                 (selectedPoint[1] + nextPoint[1]) / 2, | ||||
|               ] | ||||
|             : [selectedPoint[0] + 30, selectedPoint[1] + 30], | ||||
|           ...points.slice(activePointIndex + 1), | ||||
|         ], | ||||
|       }); | ||||
|       return { | ||||
|         appState: { | ||||
|           ...appState, | ||||
|           editingLinearElement: { | ||||
|             ...appState.editingLinearElement, | ||||
|             activePointIndex: activePointIndex + 1, | ||||
|           }, | ||||
|         }, | ||||
|         elements, | ||||
|         appState: ret.appState, | ||||
|         commitToHistory: true, | ||||
|       }; | ||||
|     } | ||||
| @@ -49,7 +68,7 @@ export const actionDuplicateSelection = register({ | ||||
|   PanelComponent: ({ elements, appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={DuplicateIcon} | ||||
|       icon={clone} | ||||
|       title={`${t("labels.duplicateSelection")} — ${getShortcutKey( | ||||
|         "CtrlOrCmd+D", | ||||
|       )}`} | ||||
| @@ -88,12 +107,9 @@ const duplicateElements = ( | ||||
|   const finalElements: ExcalidrawElement[] = []; | ||||
|  | ||||
|   let index = 0; | ||||
|   const selectedElementIds = arrayToMap( | ||||
|     getSelectedElements(elements, appState, true), | ||||
|   ); | ||||
|   while (index < elements.length) { | ||||
|     const element = elements[index]; | ||||
|     if (selectedElementIds.get(element.id)) { | ||||
|     if (appState.selectedElementIds[element.id]) { | ||||
|       if (element.groupIds.length) { | ||||
|         const groupId = getSelectedGroupForElement(appState, element); | ||||
|         // if group selected, duplicate it atomically | ||||
| @@ -115,11 +131,7 @@ const duplicateElements = ( | ||||
|     } | ||||
|     index++; | ||||
|   } | ||||
|   bindTextToShapeAfterDuplication( | ||||
|     finalElements, | ||||
|     oldElements, | ||||
|     oldIdToDuplicatedId, | ||||
|   ); | ||||
|  | ||||
|   fixBindingsAfterDuplication(finalElements, oldElements, oldIdToDuplicatedId); | ||||
|  | ||||
|   return { | ||||
| @@ -128,15 +140,10 @@ const duplicateElements = ( | ||||
|       { | ||||
|         ...appState, | ||||
|         selectedGroupIds: {}, | ||||
|         selectedElementIds: newElements.reduce( | ||||
|           (acc: Record<ExcalidrawElement["id"], true>, element) => { | ||||
|             if (!isBoundToContainer(element)) { | ||||
|               acc[element.id] = true; | ||||
|             } | ||||
|             return acc; | ||||
|           }, | ||||
|           {}, | ||||
|         ), | ||||
|         selectedElementIds: newElements.reduce((acc, element) => { | ||||
|           acc[element.id] = true; | ||||
|           return acc; | ||||
|         }, {} as any), | ||||
|       }, | ||||
|       getNonDeletedElements(finalElements), | ||||
|     ), | ||||
|   | ||||
| @@ -1,29 +1,23 @@ | ||||
| import { questionCircle, saveAs } from "../components/icons"; | ||||
| import React from "react"; | ||||
| import { trackEvent } from "../analytics"; | ||||
| import { load, questionCircle, save, saveAs } from "../components/icons"; | ||||
| import { ProjectName } from "../components/ProjectName"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import "../components/ToolIcon.scss"; | ||||
| import { Tooltip } from "../components/Tooltip"; | ||||
| import { DarkModeToggle } from "../components/DarkModeToggle"; | ||||
| import { DarkModeToggle, Appearence } from "../components/DarkModeToggle"; | ||||
| import { loadFromJSON, saveAsJSON } from "../data"; | ||||
| import { resaveAsImageWithScene } from "../data/resave"; | ||||
| import { t } from "../i18n"; | ||||
| import { useDevice } from "../components/App"; | ||||
| import { useIsMobile } from "../components/App"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { supported as fsSupported } from "browser-fs-access"; | ||||
| import { CheckboxItem } from "../components/CheckboxItem"; | ||||
| import { getExportSize } from "../scene/export"; | ||||
| import { DEFAULT_EXPORT_PADDING, EXPORT_SCALES, THEME } from "../constants"; | ||||
| import { getSelectedElements, isSomeElementSelected } from "../scene"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { isImageFileHandle } from "../data/blob"; | ||||
| import { nativeFileSystemSupported } from "../data/filesystem"; | ||||
| import { Theme } from "../element/types"; | ||||
|  | ||||
| import "../components/ToolIcon.scss"; | ||||
|  | ||||
| export const actionChangeProjectName = register({ | ||||
|   name: "changeProjectName", | ||||
|   trackEvent: false, | ||||
|   perform: (_elements, appState, value) => { | ||||
|     trackEvent("change", "title"); | ||||
|     return { appState: { ...appState, name: value }, commitToHistory: false }; | ||||
|   }, | ||||
|   PanelComponent: ({ appState, updateData, appProps }) => ( | ||||
| @@ -38,58 +32,8 @@ export const actionChangeProjectName = register({ | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionChangeExportScale = register({ | ||||
|   name: "changeExportScale", | ||||
|   trackEvent: { category: "export", action: "scale" }, | ||||
|   perform: (_elements, appState, value) => { | ||||
|     return { | ||||
|       appState: { ...appState, exportScale: value }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ elements: allElements, appState, updateData }) => { | ||||
|     const elements = getNonDeletedElements(allElements); | ||||
|     const exportSelected = isSomeElementSelected(elements, appState); | ||||
|     const exportedElements = exportSelected | ||||
|       ? getSelectedElements(elements, appState) | ||||
|       : elements; | ||||
|  | ||||
|     return ( | ||||
|       <> | ||||
|         {EXPORT_SCALES.map((s) => { | ||||
|           const [width, height] = getExportSize( | ||||
|             exportedElements, | ||||
|             DEFAULT_EXPORT_PADDING, | ||||
|             s, | ||||
|           ); | ||||
|  | ||||
|           const scaleButtonTitle = `${t( | ||||
|             "buttons.scale", | ||||
|           )} ${s}x (${width}x${height})`; | ||||
|  | ||||
|           return ( | ||||
|             <ToolButton | ||||
|               key={s} | ||||
|               size="small" | ||||
|               type="radio" | ||||
|               icon={`${s}x`} | ||||
|               name="export-canvas-scale" | ||||
|               title={scaleButtonTitle} | ||||
|               aria-label={scaleButtonTitle} | ||||
|               id="export-canvas-scale" | ||||
|               checked={s === appState.exportScale} | ||||
|               onChange={() => updateData(s)} | ||||
|             /> | ||||
|           ); | ||||
|         })} | ||||
|       </> | ||||
|     ); | ||||
|   }, | ||||
| }); | ||||
|  | ||||
| export const actionChangeExportBackground = register({ | ||||
|   name: "changeExportBackground", | ||||
|   trackEvent: { category: "export", action: "toggleBackground" }, | ||||
|   perform: (_elements, appState, value) => { | ||||
|     return { | ||||
|       appState: { ...appState, exportBackground: value }, | ||||
| @@ -108,7 +52,6 @@ export const actionChangeExportBackground = register({ | ||||
|  | ||||
| export const actionChangeExportEmbedScene = register({ | ||||
|   name: "changeExportEmbedScene", | ||||
|   trackEvent: { category: "export", action: "embedScene" }, | ||||
|   perform: (_elements, appState, value) => { | ||||
|     return { | ||||
|       appState: { ...appState, exportEmbedScene: value }, | ||||
| @@ -122,80 +65,66 @@ export const actionChangeExportEmbedScene = register({ | ||||
|     > | ||||
|       {t("labels.exportEmbedScene")} | ||||
|       <Tooltip label={t("labels.exportEmbedScene_details")} long={true}> | ||||
|         <div className="excalidraw-tooltip-icon">{questionCircle}</div> | ||||
|         <div className="Tooltip-icon">{questionCircle}</div> | ||||
|       </Tooltip> | ||||
|     </CheckboxItem> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionSaveToActiveFile = register({ | ||||
|   name: "saveToActiveFile", | ||||
|   trackEvent: { category: "export" }, | ||||
|   predicate: (elements, appState, props, app) => { | ||||
|     return ( | ||||
|       !!app.props.UIOptions.canvasActions.saveToActiveFile && | ||||
|       !!appState.fileHandle && | ||||
|       !appState.viewModeEnabled | ||||
|     ); | ||||
|   }, | ||||
|   perform: async (elements, appState, value, app) => { | ||||
| export const actionSaveScene = register({ | ||||
|   name: "saveScene", | ||||
|   perform: async (elements, appState, value) => { | ||||
|     const fileHandleExists = !!appState.fileHandle; | ||||
|  | ||||
|     try { | ||||
|       const { fileHandle } = isImageFileHandle(appState.fileHandle) | ||||
|         ? await resaveAsImageWithScene(elements, appState, app.files) | ||||
|         : await saveAsJSON(elements, appState, app.files); | ||||
|  | ||||
|       const { fileHandle } = await saveAsJSON(elements, appState); | ||||
|       return { | ||||
|         commitToHistory: false, | ||||
|         appState: { | ||||
|           ...appState, | ||||
|           fileHandle, | ||||
|           toast: fileHandleExists | ||||
|             ? { | ||||
|                 message: fileHandle?.name | ||||
|                   ? t("toast.fileSavedToFilename").replace( | ||||
|                       "{filename}", | ||||
|                       `"${fileHandle.name}"`, | ||||
|                     ) | ||||
|                   : t("toast.fileSaved"), | ||||
|               } | ||||
|           toastMessage: fileHandleExists | ||||
|             ? fileHandle.name | ||||
|               ? t("toast.fileSavedToFilename").replace( | ||||
|                   "{filename}", | ||||
|                   `"${fileHandle.name}"`, | ||||
|                 ) | ||||
|               : t("toast.fileSaved") | ||||
|             : null, | ||||
|         }, | ||||
|       }; | ||||
|     } catch (error: any) { | ||||
|     } catch (error) { | ||||
|       if (error?.name !== "AbortError") { | ||||
|         console.error(error); | ||||
|       } else { | ||||
|         console.warn(error); | ||||
|       } | ||||
|       return { commitToHistory: false }; | ||||
|     } | ||||
|   }, | ||||
|   keyTest: (event) => | ||||
|     event.key === KEYS.S && event[KEYS.CTRL_OR_CMD] && !event.shiftKey, | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="icon" | ||||
|       icon={save} | ||||
|       title={t("buttons.save")} | ||||
|       aria-label={t("buttons.save")} | ||||
|       onClick={() => updateData(null)} | ||||
|       data-testid="save-button" | ||||
|     /> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionSaveFileToDisk = register({ | ||||
|   name: "saveFileToDisk", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "export" }, | ||||
|   perform: async (elements, appState, value, app) => { | ||||
| export const actionSaveAsScene = register({ | ||||
|   name: "saveAsScene", | ||||
|   perform: async (elements, appState, value) => { | ||||
|     try { | ||||
|       const { fileHandle } = await saveAsJSON( | ||||
|         elements, | ||||
|         { | ||||
|           ...appState, | ||||
|           fileHandle: null, | ||||
|         }, | ||||
|         app.files, | ||||
|       ); | ||||
|       const { fileHandle } = await saveAsJSON(elements, { | ||||
|         ...appState, | ||||
|         fileHandle: null, | ||||
|       }); | ||||
|       return { commitToHistory: false, appState: { ...appState, fileHandle } }; | ||||
|     } catch (error: any) { | ||||
|     } catch (error) { | ||||
|       if (error?.name !== "AbortError") { | ||||
|         console.error(error); | ||||
|       } else { | ||||
|         console.warn(error); | ||||
|       } | ||||
|       return { commitToHistory: false }; | ||||
|     } | ||||
| @@ -208,8 +137,8 @@ export const actionSaveFileToDisk = register({ | ||||
|       icon={saveAs} | ||||
|       title={t("buttons.saveAs")} | ||||
|       aria-label={t("buttons.saveAs")} | ||||
|       showAriaLabel={useDevice().isMobile} | ||||
|       hidden={!nativeFileSystemSupported} | ||||
|       showAriaLabel={useIsMobile()} | ||||
|       hidden={!fsSupported} | ||||
|       onClick={() => updateData(null)} | ||||
|       data-testid="save-as-button" | ||||
|     /> | ||||
| @@ -218,44 +147,44 @@ export const actionSaveFileToDisk = register({ | ||||
|  | ||||
| export const actionLoadScene = register({ | ||||
|   name: "loadScene", | ||||
|   trackEvent: { category: "export" }, | ||||
|   predicate: (elements, appState, props, app) => { | ||||
|     return ( | ||||
|       !!app.props.UIOptions.canvasActions.loadScene && !appState.viewModeEnabled | ||||
|     ); | ||||
|   }, | ||||
|   perform: async (elements, appState, _, app) => { | ||||
|   perform: async (elements, appState) => { | ||||
|     try { | ||||
|       const { | ||||
|         elements: loadedElements, | ||||
|         appState: loadedAppState, | ||||
|         files, | ||||
|       } = await loadFromJSON(appState, elements); | ||||
|       } = await loadFromJSON(appState); | ||||
|       return { | ||||
|         elements: loadedElements, | ||||
|         appState: loadedAppState, | ||||
|         files, | ||||
|         commitToHistory: true, | ||||
|       }; | ||||
|     } catch (error: any) { | ||||
|     } catch (error) { | ||||
|       if (error?.name === "AbortError") { | ||||
|         console.warn(error); | ||||
|         return false; | ||||
|       } | ||||
|       return { | ||||
|         elements, | ||||
|         appState: { ...appState, errorMessage: error.message }, | ||||
|         files: app.files, | ||||
|         commitToHistory: false, | ||||
|       }; | ||||
|     } | ||||
|   }, | ||||
|   keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === KEYS.O, | ||||
|   PanelComponent: ({ updateData, appState }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={load} | ||||
|       title={t("buttons.load")} | ||||
|       aria-label={t("buttons.load")} | ||||
|       showAriaLabel={useIsMobile()} | ||||
|       onClick={updateData} | ||||
|       data-testid="load-button" | ||||
|     /> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionExportWithDarkMode = register({ | ||||
|   name: "exportWithDarkMode", | ||||
|   trackEvent: { category: "export", action: "toggleTheme" }, | ||||
|   perform: (_elements, appState, value) => { | ||||
|     return { | ||||
|       appState: { ...appState, exportWithDarkMode: value }, | ||||
| @@ -272,9 +201,9 @@ export const actionExportWithDarkMode = register({ | ||||
|       }} | ||||
|     > | ||||
|       <DarkModeToggle | ||||
|         value={appState.exportWithDarkMode ? THEME.DARK : THEME.LIGHT} | ||||
|         onChange={(theme: Theme) => { | ||||
|           updateData(theme === THEME.DARK); | ||||
|         value={appState.exportWithDarkMode ? "dark" : "light"} | ||||
|         onChange={(theme: Appearence) => { | ||||
|           updateData(theme === "dark"); | ||||
|         }} | ||||
|         title={t("labels.toggleExportColorScheme")} | ||||
|       /> | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| import { KEYS } from "../keys"; | ||||
| import { isInvisiblySmallElement } from "../element"; | ||||
| import { updateActiveTool, resetCursor } from "../utils"; | ||||
| import { resetCursor } from "../utils"; | ||||
| import React from "react"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { done } from "../components/icons"; | ||||
| import { t } from "../i18n"; | ||||
| @@ -13,16 +14,17 @@ import { | ||||
|   maybeBindLinearElement, | ||||
|   bindOrUnbindLinearElement, | ||||
| } from "../element/binding"; | ||||
| import { isBindingElement, isLinearElement } from "../element/typeChecks"; | ||||
| import { AppState } from "../types"; | ||||
| import { isBindingElement } from "../element/typeChecks"; | ||||
|  | ||||
| export const actionFinalize = register({ | ||||
|   name: "finalize", | ||||
|   trackEvent: false, | ||||
|   perform: (elements, appState, _, { canvas, focusContainer, scene }) => { | ||||
|   perform: (elements, appState, _, { canvas, focusContainer }) => { | ||||
|     if (appState.editingLinearElement) { | ||||
|       const { elementId, startBindingElement, endBindingElement } = | ||||
|         appState.editingLinearElement; | ||||
|       const { | ||||
|         elementId, | ||||
|         startBindingElement, | ||||
|         endBindingElement, | ||||
|       } = appState.editingLinearElement; | ||||
|       const element = LinearElementEditor.getElement(elementId); | ||||
|  | ||||
|       if (element) { | ||||
| @@ -40,7 +42,6 @@ export const actionFinalize = register({ | ||||
|               : undefined, | ||||
|           appState: { | ||||
|             ...appState, | ||||
|             cursorButton: "up", | ||||
|             editingLinearElement: null, | ||||
|           }, | ||||
|           commitToHistory: true, | ||||
| @@ -49,15 +50,6 @@ export const actionFinalize = register({ | ||||
|     } | ||||
|  | ||||
|     let newElements = elements; | ||||
|  | ||||
|     const pendingImageElement = | ||||
|       appState.pendingImageElementId && | ||||
|       scene.getElement(appState.pendingImageElementId); | ||||
|  | ||||
|     if (pendingImageElement) { | ||||
|       mutateElement(pendingImageElement, { isDeleted: true }, false); | ||||
|     } | ||||
|  | ||||
|     if (window.document.activeElement instanceof HTMLElement) { | ||||
|       focusContainer(); | ||||
|     } | ||||
| @@ -126,47 +118,27 @@ export const actionFinalize = register({ | ||||
|         ); | ||||
|       } | ||||
|  | ||||
|       if ( | ||||
|         !appState.activeTool.locked && | ||||
|         appState.activeTool.type !== "freedraw" | ||||
|       ) { | ||||
|       if (!appState.elementLocked && appState.elementType !== "freedraw") { | ||||
|         appState.selectedElementIds[multiPointElement.id] = true; | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     if ( | ||||
|       (!appState.activeTool.locked && | ||||
|         appState.activeTool.type !== "freedraw") || | ||||
|       (!appState.elementLocked && appState.elementType !== "freedraw") || | ||||
|       !multiPointElement | ||||
|     ) { | ||||
|       resetCursor(canvas); | ||||
|     } | ||||
|  | ||||
|     let activeTool: AppState["activeTool"]; | ||||
|     if (appState.activeTool.type === "eraser") { | ||||
|       activeTool = updateActiveTool(appState, { | ||||
|         ...(appState.activeTool.lastActiveTool || { | ||||
|           type: "selection", | ||||
|         }), | ||||
|         lastActiveToolBeforeEraser: null, | ||||
|       }); | ||||
|     } else { | ||||
|       activeTool = updateActiveTool(appState, { | ||||
|         type: "selection", | ||||
|       }); | ||||
|     } | ||||
|  | ||||
|     return { | ||||
|       elements: newElements, | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         cursorButton: "up", | ||||
|         activeTool: | ||||
|           (appState.activeTool.locked || | ||||
|             appState.activeTool.type === "freedraw") && | ||||
|         elementType: | ||||
|           (appState.elementLocked || appState.elementType === "freedraw") && | ||||
|           multiPointElement | ||||
|             ? appState.activeTool | ||||
|             : activeTool, | ||||
|             ? appState.elementType | ||||
|             : "selection", | ||||
|         draggingElement: null, | ||||
|         multiElement: null, | ||||
|         editingElement: null, | ||||
| @@ -174,21 +146,15 @@ export const actionFinalize = register({ | ||||
|         suggestedBindings: [], | ||||
|         selectedElementIds: | ||||
|           multiPointElement && | ||||
|           !appState.activeTool.locked && | ||||
|           appState.activeTool.type !== "freedraw" | ||||
|           !appState.elementLocked && | ||||
|           appState.elementType !== "freedraw" | ||||
|             ? { | ||||
|                 ...appState.selectedElementIds, | ||||
|                 [multiPointElement.id]: true, | ||||
|               } | ||||
|             : appState.selectedElementIds, | ||||
|         // To select the linear element when user has finished mutipoint editing | ||||
|         selectedLinearElement: | ||||
|           multiPointElement && isLinearElement(multiPointElement) | ||||
|             ? new LinearElementEditor(multiPointElement, scene) | ||||
|             : appState.selectedLinearElement, | ||||
|         pendingImageElementId: null, | ||||
|       }, | ||||
|       commitToHistory: appState.activeTool.type === "freedraw", | ||||
|       commitToHistory: appState.elementType === "freedraw", | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event, appState) => | ||||
| @@ -197,7 +163,7 @@ export const actionFinalize = register({ | ||||
|         (!appState.draggingElement && appState.multiElement === null))) || | ||||
|     ((event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) && | ||||
|       appState.multiElement !== null), | ||||
|   PanelComponent: ({ appState, updateData, data }) => ( | ||||
|   PanelComponent: ({ appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={done} | ||||
| @@ -205,7 +171,6 @@ export const actionFinalize = register({ | ||||
|       aria-label={t("buttons.done")} | ||||
|       onClick={updateData} | ||||
|       visible={appState.multiElement != null} | ||||
|       size={data?.size || "medium"} | ||||
|     /> | ||||
|   ), | ||||
| }); | ||||
|   | ||||
| @@ -1,20 +1,14 @@ | ||||
| import { register } from "./register"; | ||||
| import { getSelectedElements } from "../scene"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { getElementMap, getNonDeletedElements } from "../element"; | ||||
| import { mutateElement } from "../element/mutateElement"; | ||||
| import { ExcalidrawElement, NonDeleted } from "../element/types"; | ||||
| import { normalizeAngle, resizeSingleElement } from "../element/resizeElements"; | ||||
| import { AppState } from "../types"; | ||||
| import { getTransformHandles } from "../element/transformHandles"; | ||||
| import { isFreeDrawElement, isLinearElement } from "../element/typeChecks"; | ||||
| import { updateBoundElements } from "../element/binding"; | ||||
| import { arrayToMap } from "../utils"; | ||||
| import { | ||||
|   getElementAbsoluteCoords, | ||||
|   getElementPointsCoords, | ||||
| } from "../element/bounds"; | ||||
| import { isLinearElement } from "../element/typeChecks"; | ||||
| import { LinearElementEditor } from "../element/linearElementEditor"; | ||||
| import { KEYS } from "../keys"; | ||||
|  | ||||
| const enableActionFlipHorizontal = ( | ||||
|   elements: readonly ExcalidrawElement[], | ||||
| @@ -40,7 +34,6 @@ const enableActionFlipVertical = ( | ||||
|  | ||||
| export const actionFlipHorizontal = register({ | ||||
|   name: "flipHorizontal", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       elements: flipSelectedElements(elements, appState, "horizontal"), | ||||
| @@ -50,13 +43,12 @@ export const actionFlipHorizontal = register({ | ||||
|   }, | ||||
|   keyTest: (event) => event.shiftKey && event.code === "KeyH", | ||||
|   contextItemLabel: "labels.flipHorizontal", | ||||
|   predicate: (elements, appState) => | ||||
|   contextItemPredicate: (elements, appState) => | ||||
|     enableActionFlipHorizontal(elements, appState), | ||||
| }); | ||||
|  | ||||
| export const actionFlipVertical = register({ | ||||
|   name: "flipVertical", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       elements: flipSelectedElements(elements, appState, "vertical"), | ||||
| @@ -64,10 +56,9 @@ export const actionFlipVertical = register({ | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => | ||||
|     event.shiftKey && event.code === "KeyV" && !event[KEYS.CTRL_OR_CMD], | ||||
|   keyTest: (event) => event.shiftKey && event.code === "KeyV", | ||||
|   contextItemLabel: "labels.flipVertical", | ||||
|   predicate: (elements, appState) => | ||||
|   contextItemPredicate: (elements, appState) => | ||||
|     enableActionFlipVertical(elements, appState), | ||||
| }); | ||||
|  | ||||
| @@ -92,11 +83,9 @@ const flipSelectedElements = ( | ||||
|     flipDirection, | ||||
|   ); | ||||
|  | ||||
|   const updatedElementsMap = arrayToMap(updatedElements); | ||||
|   const updatedElementsMap = getElementMap(updatedElements); | ||||
|  | ||||
|   return elements.map( | ||||
|     (element) => updatedElementsMap.get(element.id) || element, | ||||
|   ); | ||||
|   return elements.map((element) => updatedElementsMap[element.id] || element); | ||||
| }; | ||||
|  | ||||
| const flipElements = ( | ||||
| @@ -104,13 +93,13 @@ const flipElements = ( | ||||
|   appState: AppState, | ||||
|   flipDirection: "horizontal" | "vertical", | ||||
| ): ExcalidrawElement[] => { | ||||
|   elements.forEach((element) => { | ||||
|     flipElement(element, appState); | ||||
|   for (let i = 0; i < elements.length; i++) { | ||||
|     flipElement(elements[i], appState); | ||||
|     // If vertical flip, rotate an extra 180 | ||||
|     if (flipDirection === "vertical") { | ||||
|       rotateElement(element, Math.PI); | ||||
|       rotateElement(elements[i], Math.PI); | ||||
|     } | ||||
|   }); | ||||
|   } | ||||
|   return elements; | ||||
| }; | ||||
|  | ||||
| @@ -124,6 +113,13 @@ const flipElement = ( | ||||
|   const height = element.height; | ||||
|   const originalAngle = normalizeAngle(element.angle); | ||||
|  | ||||
|   let finalOffsetX = 0; | ||||
|   if (isLinearElement(element) || isFreeDrawElement(element)) { | ||||
|     finalOffsetX = | ||||
|       element.points.reduce((max, point) => Math.max(max, point[0]), 0) * 2 - | ||||
|       element.width; | ||||
|   } | ||||
|  | ||||
|   // Rotate back to zero, if necessary | ||||
|   mutateElement(element, { | ||||
|     angle: normalizeAngle(0), | ||||
| @@ -131,6 +127,7 @@ const flipElement = ( | ||||
|   // Flip unrotated by pulling TransformHandle to opposite side | ||||
|   const transformHandles = getTransformHandles(element, appState.zoom); | ||||
|   let usingNWHandle = true; | ||||
|   let newNCoordsX = 0; | ||||
|   let nHandle = transformHandles.nw; | ||||
|   if (!nHandle) { | ||||
|     // Use ne handle instead | ||||
| @@ -144,47 +141,31 @@ const flipElement = ( | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   let finalOffsetX = 0; | ||||
|   if (isLinearElement(element) && element.points.length < 3) { | ||||
|     finalOffsetX = | ||||
|       element.points.reduce((max, point) => Math.max(max, point[0]), 0) * 2 - | ||||
|       element.width; | ||||
|   } | ||||
|  | ||||
|   let initialPointsCoords; | ||||
|   if (isLinearElement(element)) { | ||||
|     initialPointsCoords = getElementPointsCoords(element, element.points); | ||||
|   } | ||||
|   const initialElementAbsoluteCoords = getElementAbsoluteCoords(element); | ||||
|  | ||||
|   if (isLinearElement(element) && element.points.length < 3) { | ||||
|     for (let index = 1; index < element.points.length; index++) { | ||||
|       LinearElementEditor.movePoints(element, [ | ||||
|         { | ||||
|           index, | ||||
|           point: [-element.points[index][0], element.points[index][1]], | ||||
|         }, | ||||
|     for (let i = 1; i < element.points.length; i++) { | ||||
|       LinearElementEditor.movePoint(element, i, [ | ||||
|         -element.points[i][0], | ||||
|         element.points[i][1], | ||||
|       ]); | ||||
|     } | ||||
|     LinearElementEditor.normalizePoints(element); | ||||
|   } else { | ||||
|     const elWidth = initialPointsCoords | ||||
|       ? initialPointsCoords[2] - initialPointsCoords[0] | ||||
|       : initialElementAbsoluteCoords[2] - initialElementAbsoluteCoords[0]; | ||||
|  | ||||
|     const startPoint = initialPointsCoords | ||||
|       ? [initialPointsCoords[0], initialPointsCoords[1]] | ||||
|       : [initialElementAbsoluteCoords[0], initialElementAbsoluteCoords[1]]; | ||||
|  | ||||
|     // calculate new x-coord for transformation | ||||
|     newNCoordsX = usingNWHandle ? element.x + 2 * width : element.x - 2 * width; | ||||
|     resizeSingleElement( | ||||
|       new Map().set(element.id, element), | ||||
|       false, | ||||
|       element, | ||||
|       true, | ||||
|       element, | ||||
|       usingNWHandle ? "nw" : "ne", | ||||
|       true, | ||||
|       usingNWHandle ? startPoint[0] + elWidth : startPoint[0] - elWidth, | ||||
|       startPoint[1], | ||||
|       false, | ||||
|       newNCoordsX, | ||||
|       nHandle[1], | ||||
|     ); | ||||
|     // fix the size to account for handle sizes | ||||
|     mutateElement(element, { | ||||
|       width, | ||||
|       height, | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   // Rotate by (360 degrees - original angle) | ||||
| @@ -201,30 +182,9 @@ const flipElement = ( | ||||
|   mutateElement(element, { | ||||
|     x: originalX + finalOffsetX, | ||||
|     y: originalY, | ||||
|     width, | ||||
|     height, | ||||
|   }); | ||||
|  | ||||
|   updateBoundElements(element); | ||||
|  | ||||
|   if (initialPointsCoords && isLinearElement(element)) { | ||||
|     // Adjusting origin because when a beizer curve path exceeds min/max points it offsets the origin. | ||||
|     // There's still room for improvement since when the line roughness is > 1 | ||||
|     // we still have a small offset of the origin when fliipping the element. | ||||
|     const finalPointsCoords = getElementPointsCoords(element, element.points); | ||||
|  | ||||
|     const topLeftCoordsDiff = initialPointsCoords[0] - finalPointsCoords[0]; | ||||
|     const topRightCoordDiff = initialPointsCoords[2] - finalPointsCoords[2]; | ||||
|  | ||||
|     const coordsDiff = topLeftCoordsDiff + topRightCoordDiff; | ||||
|  | ||||
|     mutateElement(element, { | ||||
|       x: element.x + coordsDiff * 0.5, | ||||
|       y: element.y, | ||||
|       width, | ||||
|       height, | ||||
|     }); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const rotateElement = (element: ExcalidrawElement, rotationAngle: number) => { | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| import { KEYS } from "../keys"; | ||||
| import React from "react"; | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { t } from "../i18n"; | ||||
| import { arrayToMap, getShortcutKey } from "../utils"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { register } from "./register"; | ||||
| import { UngroupIcon, GroupIcon } from "../components/icons"; | ||||
| import { newElementWith } from "../element/mutateElement"; | ||||
| @@ -17,9 +18,8 @@ import { | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { randomId } from "../random"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { AppState } from "../types"; | ||||
| import { isBoundToContainer } from "../element/typeChecks"; | ||||
|  | ||||
| const allElementsInSameGroup = (elements: readonly ExcalidrawElement[]) => { | ||||
|   if (elements.length >= 2) { | ||||
| @@ -45,7 +45,6 @@ const enableActionGroup = ( | ||||
|   const selectedElements = getSelectedElements( | ||||
|     getNonDeletedElements(elements), | ||||
|     appState, | ||||
|     true, | ||||
|   ); | ||||
|   return ( | ||||
|     selectedElements.length >= 2 && !allElementsInSameGroup(selectedElements) | ||||
| @@ -54,12 +53,10 @@ const enableActionGroup = ( | ||||
|  | ||||
| export const actionGroup = register({ | ||||
|   name: "group", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     ); | ||||
|     if (selectedElements.length < 2) { | ||||
|       // nothing to group | ||||
| @@ -87,9 +84,8 @@ export const actionGroup = register({ | ||||
|       } | ||||
|     } | ||||
|     const newGroupId = randomId(); | ||||
|     const selectElementIds = arrayToMap(selectedElements); | ||||
|     const updatedElements = elements.map((element) => { | ||||
|       if (!selectElementIds.get(element.id)) { | ||||
|       if (!appState.selectedElementIds[element.id]) { | ||||
|         return element; | ||||
|       } | ||||
|       return newElementWith(element, { | ||||
| @@ -104,8 +100,9 @@ export const actionGroup = register({ | ||||
|     // to the z order of the highest element in the layer stack | ||||
|     const elementsInGroup = getElementsInGroup(updatedElements, newGroupId); | ||||
|     const lastElementInGroup = elementsInGroup[elementsInGroup.length - 1]; | ||||
|     const lastGroupElementIndex = | ||||
|       updatedElements.lastIndexOf(lastElementInGroup); | ||||
|     const lastGroupElementIndex = updatedElements.lastIndexOf( | ||||
|       lastElementInGroup, | ||||
|     ); | ||||
|     const elementsAfterGroup = updatedElements.slice(lastGroupElementIndex + 1); | ||||
|     const elementsBeforeGroup = updatedElements | ||||
|       .slice(0, lastGroupElementIndex) | ||||
| @@ -129,9 +126,10 @@ export const actionGroup = register({ | ||||
|     }; | ||||
|   }, | ||||
|   contextItemLabel: "labels.group", | ||||
|   predicate: (elements, appState) => enableActionGroup(elements, appState), | ||||
|   contextItemPredicate: (elements, appState) => | ||||
|     enableActionGroup(elements, appState), | ||||
|   keyTest: (event) => | ||||
|     !event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.key === KEYS.G, | ||||
|     !event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.code === CODES.G, | ||||
|   PanelComponent: ({ elements, appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       hidden={!enableActionGroup(elements, appState)} | ||||
| @@ -147,18 +145,12 @@ export const actionGroup = register({ | ||||
|  | ||||
| export const actionUngroup = register({ | ||||
|   name: "ungroup", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     const groupIds = getSelectedGroupIds(appState); | ||||
|     if (groupIds.length === 0) { | ||||
|       return { appState, elements, commitToHistory: false }; | ||||
|     } | ||||
|  | ||||
|     const boundTextElementIds: ExcalidrawTextElement["id"][] = []; | ||||
|     const nextElements = elements.map((element) => { | ||||
|       if (isBoundToContainer(element)) { | ||||
|         boundTextElementIds.push(element.id); | ||||
|       } | ||||
|       const nextGroupIds = removeFromSelectedGroups( | ||||
|         element.groupIds, | ||||
|         appState.selectedGroupIds, | ||||
| @@ -170,29 +162,20 @@ export const actionUngroup = register({ | ||||
|         groupIds: nextGroupIds, | ||||
|       }); | ||||
|     }); | ||||
|  | ||||
|     const updateAppState = selectGroupsForSelectedElements( | ||||
|       { ...appState, selectedGroupIds: {} }, | ||||
|       getNonDeletedElements(nextElements), | ||||
|     ); | ||||
|  | ||||
|     // remove binded text elements from selection | ||||
|     boundTextElementIds.forEach( | ||||
|       (id) => (updateAppState.selectedElementIds[id] = false), | ||||
|     ); | ||||
|     return { | ||||
|       appState: updateAppState, | ||||
|  | ||||
|       appState: selectGroupsForSelectedElements( | ||||
|         { ...appState, selectedGroupIds: {} }, | ||||
|         getNonDeletedElements(nextElements), | ||||
|       ), | ||||
|       elements: nextElements, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => | ||||
|     event.shiftKey && | ||||
|     event[KEYS.CTRL_OR_CMD] && | ||||
|     event.key === KEYS.G.toUpperCase(), | ||||
|     event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.code === CODES.G, | ||||
|   contextItemLabel: "labels.ungroup", | ||||
|   predicate: (elements, appState) => getSelectedGroupIds(appState).length > 0, | ||||
|   contextItemPredicate: (elements, appState) => | ||||
|     getSelectedGroupIds(appState).length > 0, | ||||
|  | ||||
|   PanelComponent: ({ elements, appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|   | ||||
| @@ -1,15 +1,15 @@ | ||||
| import { Action, ActionResult } from "./types"; | ||||
| import { UndoIcon, RedoIcon } from "../components/icons"; | ||||
| import React from "react"; | ||||
| import { undo, redo } from "../components/icons"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { t } from "../i18n"; | ||||
| import History, { HistoryEntry } from "../history"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { AppState } from "../types"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { isWindows, KEYS } from "../keys"; | ||||
| import { getElementMap } from "../element"; | ||||
| import { newElementWith } from "../element/mutateElement"; | ||||
| import { fixBindingsAfterDeletion } from "../element/binding"; | ||||
| import { arrayToMap } from "../utils"; | ||||
| import { isWindows } from "../constants"; | ||||
|  | ||||
| const writeData = ( | ||||
|   prevElements: readonly ExcalidrawElement[], | ||||
| @@ -28,17 +28,17 @@ const writeData = ( | ||||
|       return { commitToHistory }; | ||||
|     } | ||||
|  | ||||
|     const prevElementMap = arrayToMap(prevElements); | ||||
|     const prevElementMap = getElementMap(prevElements); | ||||
|     const nextElements = data.elements; | ||||
|     const nextElementMap = arrayToMap(nextElements); | ||||
|     const nextElementMap = getElementMap(nextElements); | ||||
|  | ||||
|     const deletedElements = prevElements.filter( | ||||
|       (prevElement) => !nextElementMap.has(prevElement.id), | ||||
|       (prevElement) => !nextElementMap.hasOwnProperty(prevElement.id), | ||||
|     ); | ||||
|     const elements = nextElements | ||||
|       .map((nextElement) => | ||||
|         newElementWith( | ||||
|           prevElementMap.get(nextElement.id) || nextElement, | ||||
|           prevElementMap[nextElement.id] || nextElement, | ||||
|           nextElement, | ||||
|         ), | ||||
|       ) | ||||
| @@ -63,20 +63,18 @@ type ActionCreator = (history: History) => Action; | ||||
|  | ||||
| export const createUndoAction: ActionCreator = (history) => ({ | ||||
|   name: "undo", | ||||
|   trackEvent: { category: "history" }, | ||||
|   perform: (elements, appState) => | ||||
|     writeData(elements, appState, () => history.undoOnce()), | ||||
|   keyTest: (event) => | ||||
|     event[KEYS.CTRL_OR_CMD] && | ||||
|     event.key.toLowerCase() === KEYS.Z && | ||||
|     !event.shiftKey, | ||||
|   PanelComponent: ({ updateData, data }) => ( | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={UndoIcon} | ||||
|       icon={undo} | ||||
|       aria-label={t("buttons.undo")} | ||||
|       onClick={updateData} | ||||
|       size={data?.size || "medium"} | ||||
|     /> | ||||
|   ), | ||||
|   commitToHistory: () => false, | ||||
| @@ -84,7 +82,6 @@ export const createUndoAction: ActionCreator = (history) => ({ | ||||
|  | ||||
| export const createRedoAction: ActionCreator = (history) => ({ | ||||
|   name: "redo", | ||||
|   trackEvent: { category: "history" }, | ||||
|   perform: (elements, appState) => | ||||
|     writeData(elements, appState, () => history.redoOnce()), | ||||
|   keyTest: (event) => | ||||
| @@ -92,13 +89,12 @@ export const createRedoAction: ActionCreator = (history) => ({ | ||||
|       event.shiftKey && | ||||
|       event.key.toLowerCase() === KEYS.Z) || | ||||
|     (isWindows && event.ctrlKey && !event.shiftKey && event.key === KEYS.Y), | ||||
|   PanelComponent: ({ updateData, data }) => ( | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={RedoIcon} | ||||
|       icon={redo} | ||||
|       aria-label={t("buttons.redo")} | ||||
|       onClick={updateData} | ||||
|       size={data?.size || "medium"} | ||||
|     /> | ||||
|   ), | ||||
|   commitToHistory: () => false, | ||||
|   | ||||
| @@ -1,49 +0,0 @@ | ||||
| import { getNonDeletedElements } from "../element"; | ||||
| import { LinearElementEditor } from "../element/linearElementEditor"; | ||||
| import { isLinearElement } from "../element/typeChecks"; | ||||
| import { ExcalidrawLinearElement } from "../element/types"; | ||||
| import { getSelectedElements } from "../scene"; | ||||
| import { register } from "./register"; | ||||
|  | ||||
| export const actionToggleLinearEditor = register({ | ||||
|   name: "toggleLinearEditor", | ||||
|   trackEvent: { | ||||
|     category: "element", | ||||
|   }, | ||||
|   predicate: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements(elements, appState); | ||||
|     if (selectedElements.length === 1 && isLinearElement(selectedElements[0])) { | ||||
|       return true; | ||||
|     } | ||||
|     return false; | ||||
|   }, | ||||
|   perform(elements, appState, _, app) { | ||||
|     const selectedElement = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     )[0] as ExcalidrawLinearElement; | ||||
|  | ||||
|     const editingLinearElement = | ||||
|       appState.editingLinearElement?.elementId === selectedElement.id | ||||
|         ? null | ||||
|         : new LinearElementEditor(selectedElement, app.scene); | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         editingLinearElement, | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   contextItemLabel: (elements, appState) => { | ||||
|     const selectedElement = getSelectedElements( | ||||
|       getNonDeletedElements(elements), | ||||
|       appState, | ||||
|       true, | ||||
|     )[0] as ExcalidrawLinearElement; | ||||
|     return appState.editingLinearElement?.elementId === selectedElement.id | ||||
|       ? "labels.lineEditor.exit" | ||||
|       : "labels.lineEditor.edit"; | ||||
|   }, | ||||
| }); | ||||
| @@ -1,14 +1,15 @@ | ||||
| import { HamburgerMenuIcon, palette } from "../components/icons"; | ||||
| import React from "react"; | ||||
| import { menu, palette } from "../components/icons"; | ||||
| import { ToolButton } from "../components/ToolButton"; | ||||
| import { t } from "../i18n"; | ||||
| import { showSelectedShapeActions, getNonDeletedElements } from "../element"; | ||||
| import { register } from "./register"; | ||||
| import { allowFullScreen, exitFullScreen, isFullScreen } from "../utils"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { HelpIcon } from "../components/HelpIcon"; | ||||
|  | ||||
| export const actionToggleCanvasMenu = register({ | ||||
|   name: "toggleCanvasMenu", | ||||
|   trackEvent: { category: "menu" }, | ||||
|   perform: (_, appState) => ({ | ||||
|     appState: { | ||||
|       ...appState, | ||||
| @@ -19,7 +20,7 @@ export const actionToggleCanvasMenu = register({ | ||||
|   PanelComponent: ({ appState, updateData }) => ( | ||||
|     <ToolButton | ||||
|       type="button" | ||||
|       icon={HamburgerMenuIcon} | ||||
|       icon={menu} | ||||
|       aria-label={t("buttons.menu")} | ||||
|       onClick={updateData} | ||||
|       selected={appState.openMenu === "canvas"} | ||||
| @@ -29,7 +30,6 @@ export const actionToggleCanvasMenu = register({ | ||||
|  | ||||
| export const actionToggleEditMenu = register({ | ||||
|   name: "toggleEditMenu", | ||||
|   trackEvent: { category: "menu" }, | ||||
|   perform: (_elements, appState) => ({ | ||||
|     appState: { | ||||
|       ...appState, | ||||
| @@ -54,8 +54,6 @@ export const actionToggleEditMenu = register({ | ||||
|  | ||||
| export const actionFullScreen = register({ | ||||
|   name: "toggleFullScreen", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "canvas", predicate: (appState) => !isFullScreen() }, | ||||
|   perform: () => { | ||||
|     if (!isFullScreen()) { | ||||
|       allowFullScreen(); | ||||
| @@ -67,24 +65,25 @@ export const actionFullScreen = register({ | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   keyTest: (event) => event.key === KEYS.F && !event[KEYS.CTRL_OR_CMD], | ||||
|   keyTest: (event) => event.code === CODES.F && !event[KEYS.CTRL_OR_CMD], | ||||
| }); | ||||
|  | ||||
| export const actionShortcuts = register({ | ||||
|   name: "toggleShortcuts", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "menu", action: "toggleHelpDialog" }, | ||||
|   perform: (_elements, appState, _, { focusContainer }) => { | ||||
|     if (appState.openDialog === "help") { | ||||
|     if (appState.showHelpDialog) { | ||||
|       focusContainer(); | ||||
|     } | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         openDialog: appState.openDialog === "help" ? null : "help", | ||||
|         showHelpDialog: !appState.showHelpDialog, | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ updateData }) => ( | ||||
|     <HelpIcon title={t("helpDialog.title")} onClick={updateData} /> | ||||
|   ), | ||||
|   keyTest: (event) => event.key === KEYS.QUESTION_MARK, | ||||
| }); | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| import { getClientColors } from "../clients"; | ||||
| import React from "react"; | ||||
| import { getClientColors, getClientInitials } from "../clients"; | ||||
| import { Avatar } from "../components/Avatar"; | ||||
| import { centerScrollOn } from "../scene/scroll"; | ||||
| import { Collaborator } from "../types"; | ||||
| @@ -6,8 +7,6 @@ import { register } from "./register"; | ||||
|  | ||||
| export const actionGoToCollaborator = register({ | ||||
|   name: "goToCollaborator", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "collab" }, | ||||
|   perform: (_elements, appState, value) => { | ||||
|     const point = value as Collaborator["pointer"]; | ||||
|     if (!point) { | ||||
| @@ -31,19 +30,29 @@ export const actionGoToCollaborator = register({ | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   PanelComponent: ({ appState, updateData, data }) => { | ||||
|     const [clientId, collaborator] = data as [string, Collaborator]; | ||||
|   PanelComponent: ({ appState, updateData, id }) => { | ||||
|     const clientId = id; | ||||
|     if (!clientId) { | ||||
|       return null; | ||||
|     } | ||||
|  | ||||
|     const collaborator = appState.collaborators.get(clientId); | ||||
|  | ||||
|     if (!collaborator) { | ||||
|       return null; | ||||
|     } | ||||
|  | ||||
|     const { background, stroke } = getClientColors(clientId, appState); | ||||
|     const shortName = getClientInitials(collaborator.username); | ||||
|  | ||||
|     return ( | ||||
|       <Avatar | ||||
|         color={background} | ||||
|         border={stroke} | ||||
|         onClick={() => updateData(collaborator.pointer)} | ||||
|         name={collaborator.username || ""} | ||||
|         src={collaborator.avatarUrl} | ||||
|       /> | ||||
|       > | ||||
|         {shortName} | ||||
|       </Avatar> | ||||
|     ); | ||||
|   }, | ||||
| }); | ||||
|   | ||||
| @@ -1,44 +1,25 @@ | ||||
| import { KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { selectGroupsForSelectedElements } from "../groups"; | ||||
| import { getNonDeletedElements, isTextElement } from "../element"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { isLinearElement } from "../element/typeChecks"; | ||||
| import { LinearElementEditor } from "../element/linearElementEditor"; | ||||
| import { getNonDeletedElements } from "../element"; | ||||
|  | ||||
| export const actionSelectAll = register({ | ||||
|   name: "selectAll", | ||||
|   trackEvent: { category: "canvas" }, | ||||
|   perform: (elements, appState, value, app) => { | ||||
|   perform: (elements, appState) => { | ||||
|     if (appState.editingLinearElement) { | ||||
|       return false; | ||||
|     } | ||||
|     const selectedElementIds = elements.reduce( | ||||
|       (map: Record<ExcalidrawElement["id"], true>, element) => { | ||||
|         if ( | ||||
|           !element.isDeleted && | ||||
|           !(isTextElement(element) && element.containerId) && | ||||
|           !element.locked | ||||
|         ) { | ||||
|           map[element.id] = true; | ||||
|         } | ||||
|         return map; | ||||
|       }, | ||||
|       {}, | ||||
|     ); | ||||
|  | ||||
|     return { | ||||
|       appState: selectGroupsForSelectedElements( | ||||
|         { | ||||
|           ...appState, | ||||
|           selectedLinearElement: | ||||
|             // single linear element selected | ||||
|             Object.keys(selectedElementIds).length === 1 && | ||||
|             isLinearElement(elements[0]) | ||||
|               ? new LinearElementEditor(elements[0], app.scene) | ||||
|               : null, | ||||
|           editingGroupId: null, | ||||
|           selectedElementIds, | ||||
|           selectedElementIds: elements.reduce((map, element) => { | ||||
|             if (!element.isDeleted) { | ||||
|               map[element.id] = true; | ||||
|             } | ||||
|             return map; | ||||
|           }, {} as any), | ||||
|         }, | ||||
|         getNonDeletedElements(elements), | ||||
|       ), | ||||
|   | ||||
| @@ -1,71 +0,0 @@ | ||||
| import ExcalidrawApp from "../excalidraw-app"; | ||||
| import { t } from "../i18n"; | ||||
| import { CODES } from "../keys"; | ||||
| import { API } from "../tests/helpers/api"; | ||||
| import { Keyboard, Pointer, UI } from "../tests/helpers/ui"; | ||||
| import { fireEvent, render, screen } from "../tests/test-utils"; | ||||
| import { copiedStyles } from "./actionStyles"; | ||||
|  | ||||
| const { h } = window; | ||||
|  | ||||
| const mouse = new Pointer("mouse"); | ||||
|  | ||||
| describe("actionStyles", () => { | ||||
|   beforeEach(async () => { | ||||
|     await render(<ExcalidrawApp />); | ||||
|   }); | ||||
|   it("should copy & paste styles via keyboard", () => { | ||||
|     UI.clickTool("rectangle"); | ||||
|     mouse.down(10, 10); | ||||
|     mouse.up(20, 20); | ||||
|  | ||||
|     UI.clickTool("rectangle"); | ||||
|     mouse.down(10, 10); | ||||
|     mouse.up(20, 20); | ||||
|  | ||||
|     // Change some styles of second rectangle | ||||
|     UI.clickLabeledElement("Stroke"); | ||||
|     UI.clickLabeledElement(t("colors.c92a2a")); | ||||
|     UI.clickLabeledElement("Background"); | ||||
|     UI.clickLabeledElement(t("colors.e64980")); | ||||
|     // Fill style | ||||
|     fireEvent.click(screen.getByTitle("Cross-hatch")); | ||||
|     // Stroke width | ||||
|     fireEvent.click(screen.getByTitle("Bold")); | ||||
|     // Stroke style | ||||
|     fireEvent.click(screen.getByTitle("Dotted")); | ||||
|     // Roughness | ||||
|     fireEvent.click(screen.getByTitle("Cartoonist")); | ||||
|     // Opacity | ||||
|     fireEvent.change(screen.getByLabelText("Opacity"), { | ||||
|       target: { value: "60" }, | ||||
|     }); | ||||
|  | ||||
|     mouse.reset(); | ||||
|  | ||||
|     API.setSelectedElements([h.elements[1]]); | ||||
|  | ||||
|     Keyboard.withModifierKeys({ ctrl: true, alt: true }, () => { | ||||
|       Keyboard.codeDown(CODES.C); | ||||
|     }); | ||||
|     const secondRect = JSON.parse(copiedStyles)[0]; | ||||
|     expect(secondRect.id).toBe(h.elements[1].id); | ||||
|  | ||||
|     mouse.reset(); | ||||
|     // Paste styles to first rectangle | ||||
|     API.setSelectedElements([h.elements[0]]); | ||||
|     Keyboard.withModifierKeys({ ctrl: true, alt: true }, () => { | ||||
|       Keyboard.codeDown(CODES.V); | ||||
|     }); | ||||
|  | ||||
|     const firstRect = API.getSelectedElement(); | ||||
|     expect(firstRect.id).toBe(h.elements[0].id); | ||||
|     expect(firstRect.strokeColor).toBe("#c92a2a"); | ||||
|     expect(firstRect.backgroundColor).toBe("#e64980"); | ||||
|     expect(firstRect.fillStyle).toBe("cross-hatch"); | ||||
|     expect(firstRect.strokeWidth).toBe(2); // Bold: 2 | ||||
|     expect(firstRect.strokeStyle).toBe("dotted"); | ||||
|     expect(firstRect.roughness).toBe(2); // Cartoonist: 2 | ||||
|     expect(firstRect.opacity).toBe(60); | ||||
|   }); | ||||
| }); | ||||
| @@ -6,41 +6,27 @@ import { | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { t } from "../i18n"; | ||||
| import { register } from "./register"; | ||||
| import { newElementWith } from "../element/mutateElement"; | ||||
| import { mutateElement, newElementWith } from "../element/mutateElement"; | ||||
| import { | ||||
|   DEFAULT_FONT_SIZE, | ||||
|   DEFAULT_FONT_FAMILY, | ||||
|   DEFAULT_TEXT_ALIGN, | ||||
| } from "../constants"; | ||||
| import { getBoundTextElement } from "../element/textElement"; | ||||
| import { | ||||
|   hasBoundTextElement, | ||||
|   canApplyRoundnessTypeToElement, | ||||
|   getDefaultRoundnessTypeForElement, | ||||
| } from "../element/typeChecks"; | ||||
| import { getSelectedElements } from "../scene"; | ||||
|  | ||||
| // `copiedStyles` is exported only for tests. | ||||
| export let copiedStyles: string = "{}"; | ||||
|  | ||||
| export const actionCopyStyles = register({ | ||||
|   name: "copyStyles", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     const elementsCopied = []; | ||||
|     const element = elements.find((el) => appState.selectedElementIds[el.id]); | ||||
|     elementsCopied.push(element); | ||||
|     if (element && hasBoundTextElement(element)) { | ||||
|       const boundTextElement = getBoundTextElement(element); | ||||
|       elementsCopied.push(boundTextElement); | ||||
|     } | ||||
|     if (element) { | ||||
|       copiedStyles = JSON.stringify(elementsCopied); | ||||
|       copiedStyles = JSON.stringify(element); | ||||
|     } | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         toast: { message: t("toast.copyStyles") }, | ||||
|         toastMessage: t("toast.copyStyles"), | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
| @@ -52,72 +38,31 @@ export const actionCopyStyles = register({ | ||||
|  | ||||
| export const actionPasteStyles = register({ | ||||
|   name: "pasteStyles", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     const elementsCopied = JSON.parse(copiedStyles); | ||||
|     const pastedElement = elementsCopied[0]; | ||||
|     const boundTextElement = elementsCopied[1]; | ||||
|     const pastedElement = JSON.parse(copiedStyles); | ||||
|     if (!isExcalidrawElement(pastedElement)) { | ||||
|       return { elements, commitToHistory: false }; | ||||
|     } | ||||
|  | ||||
|     const selectedElements = getSelectedElements(elements, appState, true); | ||||
|     const selectedElementIds = selectedElements.map((element) => element.id); | ||||
|     return { | ||||
|       elements: elements.map((element) => { | ||||
|         if (selectedElementIds.includes(element.id)) { | ||||
|           let elementStylesToCopyFrom = pastedElement; | ||||
|           if (isTextElement(element) && element.containerId) { | ||||
|             elementStylesToCopyFrom = boundTextElement; | ||||
|           } | ||||
|           if (!elementStylesToCopyFrom) { | ||||
|             return element; | ||||
|           } | ||||
|           let newElement = newElementWith(element, { | ||||
|             backgroundColor: elementStylesToCopyFrom?.backgroundColor, | ||||
|             strokeWidth: elementStylesToCopyFrom?.strokeWidth, | ||||
|             strokeColor: elementStylesToCopyFrom?.strokeColor, | ||||
|             strokeStyle: elementStylesToCopyFrom?.strokeStyle, | ||||
|             fillStyle: elementStylesToCopyFrom?.fillStyle, | ||||
|             opacity: elementStylesToCopyFrom?.opacity, | ||||
|             roughness: elementStylesToCopyFrom?.roughness, | ||||
|             roundness: elementStylesToCopyFrom.roundness | ||||
|               ? canApplyRoundnessTypeToElement( | ||||
|                   elementStylesToCopyFrom.roundness.type, | ||||
|                   element, | ||||
|                 ) | ||||
|                 ? elementStylesToCopyFrom.roundness | ||||
|                 : getDefaultRoundnessTypeForElement(element) | ||||
|               : null, | ||||
|         if (appState.selectedElementIds[element.id]) { | ||||
|           const newElement = newElementWith(element, { | ||||
|             backgroundColor: pastedElement?.backgroundColor, | ||||
|             strokeWidth: pastedElement?.strokeWidth, | ||||
|             strokeColor: pastedElement?.strokeColor, | ||||
|             strokeStyle: pastedElement?.strokeStyle, | ||||
|             fillStyle: pastedElement?.fillStyle, | ||||
|             opacity: pastedElement?.opacity, | ||||
|             roughness: pastedElement?.roughness, | ||||
|           }); | ||||
|  | ||||
|           if (isTextElement(newElement)) { | ||||
|             newElement = newElementWith(newElement, { | ||||
|               fontSize: elementStylesToCopyFrom?.fontSize || DEFAULT_FONT_SIZE, | ||||
|               fontFamily: | ||||
|                 elementStylesToCopyFrom?.fontFamily || DEFAULT_FONT_FAMILY, | ||||
|               textAlign: | ||||
|                 elementStylesToCopyFrom?.textAlign || DEFAULT_TEXT_ALIGN, | ||||
|             mutateElement(newElement, { | ||||
|               fontSize: pastedElement?.fontSize || DEFAULT_FONT_SIZE, | ||||
|               fontFamily: pastedElement?.fontFamily || DEFAULT_FONT_FAMILY, | ||||
|               textAlign: pastedElement?.textAlign || DEFAULT_TEXT_ALIGN, | ||||
|             }); | ||||
|             let container = null; | ||||
|             if (newElement.containerId) { | ||||
|               container = | ||||
|                 selectedElements.find( | ||||
|                   (element) => | ||||
|                     isTextElement(newElement) && | ||||
|                     element.id === newElement.containerId, | ||||
|                 ) || null; | ||||
|             } | ||||
|             redrawTextBoundingBox(newElement, container); | ||||
|             redrawTextBoundingBox(newElement); | ||||
|           } | ||||
|  | ||||
|           if (newElement.type === "arrow") { | ||||
|             newElement = newElementWith(newElement, { | ||||
|               startArrowhead: elementStylesToCopyFrom.startArrowhead, | ||||
|               endArrowhead: elementStylesToCopyFrom.endArrowhead, | ||||
|             }); | ||||
|           } | ||||
|  | ||||
|           return newElement; | ||||
|         } | ||||
|         return element; | ||||
|   | ||||
| @@ -2,15 +2,12 @@ import { CODES, KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { GRID_SIZE } from "../constants"; | ||||
| import { AppState } from "../types"; | ||||
| import { trackEvent } from "../analytics"; | ||||
|  | ||||
| export const actionToggleGridMode = register({ | ||||
|   name: "gridMode", | ||||
|   viewMode: true, | ||||
|   trackEvent: { | ||||
|     category: "canvas", | ||||
|     predicate: (appState) => !appState.gridSize, | ||||
|   }, | ||||
|   perform(elements, appState) { | ||||
|     trackEvent("view", "mode", "grid"); | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
| @@ -20,9 +17,6 @@ export const actionToggleGridMode = register({ | ||||
|     }; | ||||
|   }, | ||||
|   checked: (appState: AppState) => appState.gridSize !== null, | ||||
|   predicate: (element, appState, props) => { | ||||
|     return typeof props.gridModeEnabled === "undefined"; | ||||
|   }, | ||||
|   contextItemLabel: "labels.showGrid", | ||||
|   keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.QUOTE, | ||||
| }); | ||||
|   | ||||
| @@ -1,60 +0,0 @@ | ||||
| import { newElementWith } from "../element/mutateElement"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { KEYS } from "../keys"; | ||||
| import { getSelectedElements } from "../scene"; | ||||
| import { arrayToMap } from "../utils"; | ||||
| import { register } from "./register"; | ||||
|  | ||||
| export const actionToggleLock = register({ | ||||
|   name: "toggleLock", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     const selectedElements = getSelectedElements(elements, appState, true); | ||||
|  | ||||
|     if (!selectedElements.length) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     const operation = getOperation(selectedElements); | ||||
|     const selectedElementsMap = arrayToMap(selectedElements); | ||||
|     const lock = operation === "lock"; | ||||
|     return { | ||||
|       elements: elements.map((element) => { | ||||
|         if (!selectedElementsMap.has(element.id)) { | ||||
|           return element; | ||||
|         } | ||||
|  | ||||
|         return newElementWith(element, { locked: lock }); | ||||
|       }), | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         selectedLinearElement: lock ? null : appState.selectedLinearElement, | ||||
|       }, | ||||
|       commitToHistory: true, | ||||
|     }; | ||||
|   }, | ||||
|   contextItemLabel: (elements, appState) => { | ||||
|     const selected = getSelectedElements(elements, appState, false); | ||||
|     if (selected.length === 1) { | ||||
|       return selected[0].locked | ||||
|         ? "labels.elementLock.unlock" | ||||
|         : "labels.elementLock.lock"; | ||||
|     } | ||||
|  | ||||
|     return getOperation(selected) === "lock" | ||||
|       ? "labels.elementLock.lockAll" | ||||
|       : "labels.elementLock.unlockAll"; | ||||
|   }, | ||||
|   keyTest: (event, appState, elements) => { | ||||
|     return ( | ||||
|       event.key.toLocaleLowerCase() === KEYS.L && | ||||
|       event[KEYS.CTRL_OR_CMD] && | ||||
|       event.shiftKey && | ||||
|       getSelectedElements(elements, appState, false).length > 0 | ||||
|     ); | ||||
|   }, | ||||
| }); | ||||
|  | ||||
| const getOperation = ( | ||||
|   elements: readonly ExcalidrawElement[], | ||||
| ): "lock" | "unlock" => (elements.some((el) => !el.locked) ? "lock" : "unlock"); | ||||
| @@ -3,8 +3,6 @@ import { CODES, KEYS } from "../keys"; | ||||
|  | ||||
| export const actionToggleStats = register({ | ||||
|   name: "stats", | ||||
|   viewMode: true, | ||||
|   trackEvent: { category: "menu" }, | ||||
|   perform(elements, appState) { | ||||
|     return { | ||||
|       appState: { | ||||
|   | ||||
| @@ -1,26 +1,21 @@ | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { trackEvent } from "../analytics"; | ||||
|  | ||||
| export const actionToggleViewMode = register({ | ||||
|   name: "viewMode", | ||||
|   viewMode: true, | ||||
|   trackEvent: { | ||||
|     category: "canvas", | ||||
|     predicate: (appState) => !appState.viewModeEnabled, | ||||
|   }, | ||||
|   perform(elements, appState) { | ||||
|     trackEvent("view", "mode", "view"); | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
|         viewModeEnabled: !this.checked!(appState), | ||||
|         selectedElementIds: {}, | ||||
|       }, | ||||
|       commitToHistory: false, | ||||
|     }; | ||||
|   }, | ||||
|   checked: (appState) => appState.viewModeEnabled, | ||||
|   predicate: (elements, appState, appProps) => { | ||||
|     return typeof appProps.viewModeEnabled === "undefined"; | ||||
|   }, | ||||
|   contextItemLabel: "labels.viewMode", | ||||
|   keyTest: (event) => | ||||
|     !event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.R, | ||||
|   | ||||
| @@ -1,14 +1,12 @@ | ||||
| import { CODES, KEYS } from "../keys"; | ||||
| import { register } from "./register"; | ||||
| import { trackEvent } from "../analytics"; | ||||
|  | ||||
| export const actionToggleZenMode = register({ | ||||
|   name: "zenMode", | ||||
|   viewMode: true, | ||||
|   trackEvent: { | ||||
|     category: "canvas", | ||||
|     predicate: (appState) => !appState.zenModeEnabled, | ||||
|   }, | ||||
|   perform(elements, appState) { | ||||
|     trackEvent("view", "mode", "zen"); | ||||
|  | ||||
|     return { | ||||
|       appState: { | ||||
|         ...appState, | ||||
| @@ -18,9 +16,6 @@ export const actionToggleZenMode = register({ | ||||
|     }; | ||||
|   }, | ||||
|   checked: (appState) => appState.zenModeEnabled, | ||||
|   predicate: (elements, appState, appProps) => { | ||||
|     return typeof appProps.zenModeEnabled === "undefined"; | ||||
|   }, | ||||
|   contextItemLabel: "buttons.zenMode", | ||||
|   keyTest: (event) => | ||||
|     !event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.Z, | ||||
|   | ||||
| @@ -5,21 +5,19 @@ import { | ||||
|   moveAllLeft, | ||||
|   moveAllRight, | ||||
| } from "../zindex"; | ||||
| import { KEYS, CODES } from "../keys"; | ||||
| import { KEYS, isDarwin, CODES } from "../keys"; | ||||
| import { t } from "../i18n"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { register } from "./register"; | ||||
| import { | ||||
|   BringForwardIcon, | ||||
|   BringToFrontIcon, | ||||
|   SendBackwardIcon, | ||||
|   BringToFrontIcon, | ||||
|   SendToBackIcon, | ||||
|   BringForwardIcon, | ||||
| } from "../components/icons"; | ||||
| import { isDarwin } from "../constants"; | ||||
|  | ||||
| export const actionSendBackward = register({ | ||||
|   name: "sendBackward", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       elements: moveOneLeft(elements, appState), | ||||
| @@ -40,14 +38,13 @@ export const actionSendBackward = register({ | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.sendBackward")} — ${getShortcutKey("CtrlOrCmd+[")}`} | ||||
|     > | ||||
|       {SendBackwardIcon} | ||||
|       <SendBackwardIcon theme={appState.theme} /> | ||||
|     </button> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionBringForward = register({ | ||||
|   name: "bringForward", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       elements: moveOneRight(elements, appState), | ||||
| @@ -68,14 +65,13 @@ export const actionBringForward = register({ | ||||
|       onClick={() => updateData(null)} | ||||
|       title={`${t("labels.bringForward")} — ${getShortcutKey("CtrlOrCmd+]")}`} | ||||
|     > | ||||
|       {BringForwardIcon} | ||||
|       <BringForwardIcon theme={appState.theme} /> | ||||
|     </button> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionSendToBack = register({ | ||||
|   name: "sendToBack", | ||||
|   trackEvent: { category: "element" }, | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       elements: moveAllLeft(elements, appState), | ||||
| @@ -103,15 +99,13 @@ export const actionSendToBack = register({ | ||||
|           : getShortcutKey("CtrlOrCmd+Shift+[") | ||||
|       }`} | ||||
|     > | ||||
|       {SendToBackIcon} | ||||
|       <SendToBackIcon theme={appState.theme} /> | ||||
|     </button> | ||||
|   ), | ||||
| }); | ||||
|  | ||||
| export const actionBringToFront = register({ | ||||
|   name: "bringToFront", | ||||
|   trackEvent: { category: "element" }, | ||||
|  | ||||
|   perform: (elements, appState) => { | ||||
|     return { | ||||
|       elements: moveAllRight(elements, appState), | ||||
| @@ -139,7 +133,7 @@ export const actionBringToFront = register({ | ||||
|           : getShortcutKey("CtrlOrCmd+Shift+]") | ||||
|       }`} | ||||
|     > | ||||
|       {BringToFrontIcon} | ||||
|       <BringToFrontIcon theme={appState.theme} /> | ||||
|     </button> | ||||
|   ), | ||||
| }); | ||||
|   | ||||
| @@ -17,7 +17,6 @@ export { | ||||
|   actionChangeFontSize, | ||||
|   actionChangeFontFamily, | ||||
|   actionChangeTextAlign, | ||||
|   actionChangeVerticalAlign, | ||||
| } from "./actionProperties"; | ||||
|  | ||||
| export { | ||||
| @@ -35,8 +34,8 @@ export { actionFinalize } from "./actionFinalize"; | ||||
| export { | ||||
|   actionChangeProjectName, | ||||
|   actionChangeExportBackground, | ||||
|   actionSaveToActiveFile, | ||||
|   actionSaveFileToDisk, | ||||
|   actionSaveScene, | ||||
|   actionSaveAsScene, | ||||
|   actionLoadScene, | ||||
| } from "./actionExport"; | ||||
|  | ||||
| @@ -75,14 +74,9 @@ export { | ||||
|   actionCut, | ||||
|   actionCopyAsPng, | ||||
|   actionCopyAsSvg, | ||||
|   copyText, | ||||
| } from "./actionClipboard"; | ||||
|  | ||||
| export { actionToggleGridMode } from "./actionToggleGridMode"; | ||||
| export { actionToggleZenMode } from "./actionToggleZenMode"; | ||||
|  | ||||
| export { actionToggleStats } from "./actionToggleStats"; | ||||
| export { actionUnbindText, actionBindText } from "./actionBoundText"; | ||||
| export { actionLink } from "../element/Hyperlink"; | ||||
| export { actionToggleLock } from "./actionToggleLock"; | ||||
| export { actionToggleLinearEditor } from "./actionLinearEditor"; | ||||
|   | ||||
| @@ -1,58 +1,39 @@ | ||||
| import React from "react"; | ||||
| import { | ||||
|   Action, | ||||
|   ActionsManagerInterface, | ||||
|   UpdaterFn, | ||||
|   ActionName, | ||||
|   ActionResult, | ||||
|   PanelComponentProps, | ||||
|   ActionSource, | ||||
| } from "./types"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { AppClassProperties, AppState } from "../types"; | ||||
| import { trackEvent } from "../analytics"; | ||||
| import { AppProps, AppState } from "../types"; | ||||
| import { MODES } from "../constants"; | ||||
| import Library from "../data/library"; | ||||
|  | ||||
| const trackAction = ( | ||||
|   action: Action, | ||||
|   source: ActionSource, | ||||
|   appState: Readonly<AppState>, | ||||
|   elements: readonly ExcalidrawElement[], | ||||
|   app: AppClassProperties, | ||||
|   value: any, | ||||
| ) => { | ||||
|   if (action.trackEvent) { | ||||
|     try { | ||||
|       if (typeof action.trackEvent === "object") { | ||||
|         const shouldTrack = action.trackEvent.predicate | ||||
|           ? action.trackEvent.predicate(appState, elements, value) | ||||
|           : true; | ||||
|         if (shouldTrack) { | ||||
|           trackEvent( | ||||
|             action.trackEvent.category, | ||||
|             action.trackEvent.action || action.name, | ||||
|             `${source} (${app.device.isMobile ? "mobile" : "desktop"})`, | ||||
|           ); | ||||
|         } | ||||
|       } | ||||
|     } catch (error) { | ||||
|       console.error("error while logging action:", error); | ||||
|     } | ||||
|   } | ||||
| // This is the <App> component, but for now we don't care about anything but its | ||||
| // `canvas` state. | ||||
| type App = { | ||||
|   canvas: HTMLCanvasElement | null; | ||||
|   focusContainer: () => void; | ||||
|   props: AppProps; | ||||
|   library: Library; | ||||
| }; | ||||
|  | ||||
| export class ActionManager { | ||||
|   actions = {} as Record<ActionName, Action>; | ||||
| export class ActionManager implements ActionsManagerInterface { | ||||
|   actions = {} as ActionsManagerInterface["actions"]; | ||||
|  | ||||
|   updater: (actionResult: ActionResult | Promise<ActionResult>) => void; | ||||
|  | ||||
|   getAppState: () => Readonly<AppState>; | ||||
|   getElementsIncludingDeleted: () => readonly ExcalidrawElement[]; | ||||
|   app: AppClassProperties; | ||||
|   app: App; | ||||
|  | ||||
|   constructor( | ||||
|     updater: UpdaterFn, | ||||
|     getAppState: () => AppState, | ||||
|     getElementsIncludingDeleted: () => readonly ExcalidrawElement[], | ||||
|     app: AppClassProperties, | ||||
|     app: App, | ||||
|   ) { | ||||
|     this.updater = (actionResult) => { | ||||
|       if (actionResult && "then" in actionResult) { | ||||
| @@ -93,45 +74,44 @@ export class ActionManager { | ||||
|           ), | ||||
|       ); | ||||
|  | ||||
|     if (data.length !== 1) { | ||||
|       if (data.length > 1) { | ||||
|         console.warn("Canceling as multiple actions match this shortcut", data); | ||||
|     if (data.length === 0) { | ||||
|       return false; | ||||
|     } | ||||
|     const { viewModeEnabled } = this.getAppState(); | ||||
|     if (viewModeEnabled) { | ||||
|       if (!Object.values(MODES).includes(data[0].name)) { | ||||
|         return false; | ||||
|       } | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     const action = data[0]; | ||||
|  | ||||
|     if (this.getAppState().viewModeEnabled && action.viewMode !== true) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     const elements = this.getElementsIncludingDeleted(); | ||||
|     const appState = this.getAppState(); | ||||
|     const value = null; | ||||
|  | ||||
|     trackAction(action, "keyboard", appState, elements, this.app, null); | ||||
|  | ||||
|     event.preventDefault(); | ||||
|     event.stopPropagation(); | ||||
|     this.updater(data[0].perform(elements, appState, value, this.app)); | ||||
|     this.updater( | ||||
|       data[0].perform( | ||||
|         this.getElementsIncludingDeleted(), | ||||
|         this.getAppState(), | ||||
|         null, | ||||
|         this.app, | ||||
|       ), | ||||
|     ); | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   executeAction(action: Action, source: ActionSource = "api") { | ||||
|     const elements = this.getElementsIncludingDeleted(); | ||||
|     const appState = this.getAppState(); | ||||
|     const value = null; | ||||
|  | ||||
|     trackAction(action, source, appState, elements, this.app, value); | ||||
|  | ||||
|     this.updater(action.perform(elements, appState, value, this.app)); | ||||
|   executeAction(action: Action) { | ||||
|     this.updater( | ||||
|       action.perform( | ||||
|         this.getElementsIncludingDeleted(), | ||||
|         this.getAppState(), | ||||
|         null, | ||||
|         this.app, | ||||
|       ), | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * @param data additional data sent to the PanelComponent | ||||
|    */ | ||||
|   renderAction = (name: ActionName, data?: PanelComponentProps["data"]) => { | ||||
|   // Id is an attribute that we can use to pass in data like keys. | ||||
|   // This is needed for dynamically generated action components | ||||
|   // like the user list. We can use this key to extract more | ||||
|   // data from app state. This is an alternative to generic prop hell! | ||||
|   renderAction = (name: ActionName, id?: string) => { | ||||
|     const canvasActions = this.app.props.UIOptions.canvasActions; | ||||
|  | ||||
|     if ( | ||||
| @@ -143,12 +123,7 @@ export class ActionManager { | ||||
|     ) { | ||||
|       const action = this.actions[name]; | ||||
|       const PanelComponent = action.PanelComponent!; | ||||
|       PanelComponent.displayName = "PanelComponent"; | ||||
|       const elements = this.getElementsIncludingDeleted(); | ||||
|       const appState = this.getAppState(); | ||||
|       const updateData = (formState?: any) => { | ||||
|         trackAction(action, "ui", appState, elements, this.app, formState); | ||||
|  | ||||
|         this.updater( | ||||
|           action.perform( | ||||
|             this.getElementsIncludingDeleted(), | ||||
| @@ -164,22 +139,12 @@ export class ActionManager { | ||||
|           elements={this.getElementsIncludingDeleted()} | ||||
|           appState={this.getAppState()} | ||||
|           updateData={updateData} | ||||
|           id={id} | ||||
|           appProps={this.app.props} | ||||
|           data={data} | ||||
|         /> | ||||
|       ); | ||||
|     } | ||||
|  | ||||
|     return null; | ||||
|   }; | ||||
|  | ||||
|   isActionEnabled = (action: Action) => { | ||||
|     const elements = this.getElementsIncludingDeleted(); | ||||
|     const appState = this.getAppState(); | ||||
|  | ||||
|     return ( | ||||
|       !action.predicate || | ||||
|       action.predicate(elements, appState, this.app.props, this.app) | ||||
|     ); | ||||
|   }; | ||||
| } | ||||
|   | ||||
| @@ -2,9 +2,7 @@ import { Action } from "./types"; | ||||
|  | ||||
| export let actions: readonly Action[] = []; | ||||
|  | ||||
| export const register = <T extends Action>(action: T) => { | ||||
| export const register = (action: Action): Action => { | ||||
|   actions = actions.concat(action); | ||||
|   return action as T & { | ||||
|     keyTest?: unknown extends T["keyTest"] ? never : T["keyTest"]; | ||||
|   }; | ||||
|   return action; | ||||
| }; | ||||
|   | ||||
| @@ -1,54 +1,40 @@ | ||||
| import { isDarwin } from "../constants"; | ||||
| import { t } from "../i18n"; | ||||
| import { isDarwin } from "../keys"; | ||||
| import { getShortcutKey } from "../utils"; | ||||
| import { ActionName } from "./types"; | ||||
|  | ||||
| export type ShortcutName = | ||||
|   | SubtypeOf< | ||||
|       ActionName, | ||||
|       | "toggleTheme" | ||||
|       | "loadScene" | ||||
|       | "cut" | ||||
|       | "copy" | ||||
|       | "paste" | ||||
|       | "copyStyles" | ||||
|       | "pasteStyles" | ||||
|       | "selectAll" | ||||
|       | "deleteSelectedElements" | ||||
|       | "duplicateSelection" | ||||
|       | "sendBackward" | ||||
|       | "bringForward" | ||||
|       | "sendToBack" | ||||
|       | "bringToFront" | ||||
|       | "copyAsPng" | ||||
|       | "copyAsSvg" | ||||
|       | "group" | ||||
|       | "ungroup" | ||||
|       | "gridMode" | ||||
|       | "zenMode" | ||||
|       | "stats" | ||||
|       | "addToLibrary" | ||||
|       | "viewMode" | ||||
|       | "flipHorizontal" | ||||
|       | "flipVertical" | ||||
|       | "hyperlink" | ||||
|       | "toggleLock" | ||||
|     > | ||||
|   | "saveScene" | ||||
|   | "imageExport"; | ||||
|   | "cut" | ||||
|   | "copy" | ||||
|   | "paste" | ||||
|   | "copyStyles" | ||||
|   | "pasteStyles" | ||||
|   | "selectAll" | ||||
|   | "deleteSelectedElements" | ||||
|   | "duplicateSelection" | ||||
|   | "sendBackward" | ||||
|   | "bringForward" | ||||
|   | "sendToBack" | ||||
|   | "bringToFront" | ||||
|   | "copyAsPng" | ||||
|   | "copyAsSvg" | ||||
|   | "group" | ||||
|   | "ungroup" | ||||
|   | "gridMode" | ||||
|   | "zenMode" | ||||
|   | "stats" | ||||
|   | "addToLibrary" | ||||
|   | "viewMode" | ||||
|   | "flipHorizontal" | ||||
|   | "flipVertical"; | ||||
|  | ||||
| const shortcutMap: Record<ShortcutName, string[]> = { | ||||
|   toggleTheme: [getShortcutKey("Shift+Alt+D")], | ||||
|   saveScene: [getShortcutKey("CtrlOrCmd+S")], | ||||
|   loadScene: [getShortcutKey("CtrlOrCmd+O")], | ||||
|   imageExport: [getShortcutKey("CtrlOrCmd+Shift+E")], | ||||
|   cut: [getShortcutKey("CtrlOrCmd+X")], | ||||
|   copy: [getShortcutKey("CtrlOrCmd+C")], | ||||
|   paste: [getShortcutKey("CtrlOrCmd+V")], | ||||
|   copyStyles: [getShortcutKey("CtrlOrCmd+Alt+C")], | ||||
|   pasteStyles: [getShortcutKey("CtrlOrCmd+Alt+V")], | ||||
|   selectAll: [getShortcutKey("CtrlOrCmd+A")], | ||||
|   deleteSelectedElements: [getShortcutKey("Delete")], | ||||
|   deleteSelectedElements: [getShortcutKey("Del")], | ||||
|   duplicateSelection: [ | ||||
|     getShortcutKey("CtrlOrCmd+D"), | ||||
|     getShortcutKey(`Alt+${t("helpDialog.drag")}`), | ||||
| @@ -76,12 +62,10 @@ const shortcutMap: Record<ShortcutName, string[]> = { | ||||
|   flipHorizontal: [getShortcutKey("Shift+H")], | ||||
|   flipVertical: [getShortcutKey("Shift+V")], | ||||
|   viewMode: [getShortcutKey("Alt+R")], | ||||
|   hyperlink: [getShortcutKey("CtrlOrCmd+K")], | ||||
|   toggleLock: [getShortcutKey("CtrlOrCmd+Shift+L")], | ||||
| }; | ||||
|  | ||||
| export const getShortcutFromShortcutName = (name: ShortcutName) => { | ||||
|   const shortcuts = shortcutMap[name]; | ||||
|   // if multiple shortcuts available, take the first one | ||||
|   // if multiple shortcuts availiable, take the first one | ||||
|   return shortcuts && shortcuts.length > 0 ? shortcuts[0] : ""; | ||||
| }; | ||||
|   | ||||
| @@ -1,13 +1,7 @@ | ||||
| import React from "react"; | ||||
| import { ExcalidrawElement } from "../element/types"; | ||||
| import { | ||||
|   AppClassProperties, | ||||
|   AppState, | ||||
|   ExcalidrawProps, | ||||
|   BinaryFiles, | ||||
| } from "../types"; | ||||
|  | ||||
| export type ActionSource = "ui" | "keyboard" | "contextMenu" | "api"; | ||||
| import { AppState, ExcalidrawProps } from "../types"; | ||||
| import Library from "../data/library"; | ||||
|  | ||||
| /** if false, the action should be prevented */ | ||||
| export type ActionResult = | ||||
| @@ -17,121 +11,105 @@ export type ActionResult = | ||||
|         AppState, | ||||
|         "offsetTop" | "offsetLeft" | "width" | "height" | ||||
|       > | null; | ||||
|       files?: BinaryFiles | null; | ||||
|       commitToHistory: boolean; | ||||
|       syncHistory?: boolean; | ||||
|       replaceFiles?: boolean; | ||||
|     } | ||||
|   | false; | ||||
|  | ||||
| type AppAPI = { | ||||
|   canvas: HTMLCanvasElement | null; | ||||
|   focusContainer(): void; | ||||
|   library: Library; | ||||
| }; | ||||
|  | ||||
| type ActionFn = ( | ||||
|   elements: readonly ExcalidrawElement[], | ||||
|   appState: Readonly<AppState>, | ||||
|   formData: any, | ||||
|   app: AppClassProperties, | ||||
|   app: AppAPI, | ||||
| ) => ActionResult | Promise<ActionResult>; | ||||
|  | ||||
| export type UpdaterFn = (res: ActionResult) => void; | ||||
| export type ActionFilterFn = (action: Action) => void; | ||||
|  | ||||
| const actionNames = [ | ||||
|   "copy", | ||||
|   "cut", | ||||
|   "paste", | ||||
|   "copyAsPng", | ||||
|   "copyAsSvg", | ||||
|   "copyText", | ||||
|   "sendBackward", | ||||
|   "bringForward", | ||||
|   "sendToBack", | ||||
|   "bringToFront", | ||||
|   "copyStyles", | ||||
|   "selectAll", | ||||
|   "pasteStyles", | ||||
|   "gridMode", | ||||
|   "zenMode", | ||||
|   "stats", | ||||
|   "changeStrokeColor", | ||||
|   "changeBackgroundColor", | ||||
|   "changeFillStyle", | ||||
|   "changeStrokeWidth", | ||||
|   "changeStrokeShape", | ||||
|   "changeSloppiness", | ||||
|   "changeStrokeStyle", | ||||
|   "changeArrowhead", | ||||
|   "changeOpacity", | ||||
|   "changeFontSize", | ||||
|   "toggleCanvasMenu", | ||||
|   "toggleEditMenu", | ||||
|   "undo", | ||||
|   "redo", | ||||
|   "finalize", | ||||
|   "changeProjectName", | ||||
|   "changeExportBackground", | ||||
|   "changeExportEmbedScene", | ||||
|   "changeExportScale", | ||||
|   "saveToActiveFile", | ||||
|   "saveFileToDisk", | ||||
|   "loadScene", | ||||
|   "duplicateSelection", | ||||
|   "deleteSelectedElements", | ||||
|   "changeViewBackgroundColor", | ||||
|   "clearCanvas", | ||||
|   "zoomIn", | ||||
|   "zoomOut", | ||||
|   "resetZoom", | ||||
|   "zoomToFit", | ||||
|   "zoomToSelection", | ||||
|   "changeFontFamily", | ||||
|   "changeTextAlign", | ||||
|   "changeVerticalAlign", | ||||
|   "toggleFullScreen", | ||||
|   "toggleShortcuts", | ||||
|   "group", | ||||
|   "ungroup", | ||||
|   "goToCollaborator", | ||||
|   "addToLibrary", | ||||
|   "changeRoundness", | ||||
|   "alignTop", | ||||
|   "alignBottom", | ||||
|   "alignLeft", | ||||
|   "alignRight", | ||||
|   "alignVerticallyCentered", | ||||
|   "alignHorizontallyCentered", | ||||
|   "distributeHorizontally", | ||||
|   "distributeVertically", | ||||
|   "flipHorizontal", | ||||
|   "flipVertical", | ||||
|   "viewMode", | ||||
|   "exportWithDarkMode", | ||||
|   "toggleTheme", | ||||
|   "increaseFontSize", | ||||
|   "decreaseFontSize", | ||||
|   "unbindText", | ||||
|   "hyperlink", | ||||
|   "bindText", | ||||
|   "toggleLock", | ||||
|   "toggleLinearEditor", | ||||
|   "toggleEraserTool", | ||||
|   "toggleHandTool", | ||||
| ] as const; | ||||
|  | ||||
| // So we can have the `isActionName` type guard | ||||
| export type ActionName = typeof actionNames[number]; | ||||
| export const isActionName = (n: any): n is ActionName => | ||||
|   actionNames.includes(n); | ||||
|  | ||||
| export type PanelComponentProps = { | ||||
|   elements: readonly ExcalidrawElement[]; | ||||
|   appState: AppState; | ||||
|   updateData: (formData?: any) => void; | ||||
|   appProps: ExcalidrawProps; | ||||
|   data?: Record<string, any>; | ||||
| }; | ||||
| export type ActionName = | ||||
|   | "copy" | ||||
|   | "cut" | ||||
|   | "paste" | ||||
|   | "copyAsPng" | ||||
|   | "copyAsSvg" | ||||
|   | "sendBackward" | ||||
|   | "bringForward" | ||||
|   | "sendToBack" | ||||
|   | "bringToFront" | ||||
|   | "copyStyles" | ||||
|   | "selectAll" | ||||
|   | "pasteStyles" | ||||
|   | "gridMode" | ||||
|   | "zenMode" | ||||
|   | "stats" | ||||
|   | "changeStrokeColor" | ||||
|   | "changeBackgroundColor" | ||||
|   | "changeFillStyle" | ||||
|   | "changeStrokeWidth" | ||||
|   | "changeStrokeShape" | ||||
|   | "changeSloppiness" | ||||
|   | "changeStrokeStyle" | ||||
|   | "changeArrowhead" | ||||
|   | "changeOpacity" | ||||
|   | "changeFontSize" | ||||
|   | "toggleCanvasMenu" | ||||
|   | "toggleEditMenu" | ||||
|   | "undo" | ||||
|   | "redo" | ||||
|   | "finalize" | ||||
|   | "changeProjectName" | ||||
|   | "changeExportBackground" | ||||
|   | "changeExportEmbedScene" | ||||
|   | "saveScene" | ||||
|   | "saveAsScene" | ||||
|   | "loadScene" | ||||
|   | "duplicateSelection" | ||||
|   | "deleteSelectedElements" | ||||
|   | "changeViewBackgroundColor" | ||||
|   | "clearCanvas" | ||||
|   | "zoomIn" | ||||
|   | "zoomOut" | ||||
|   | "resetZoom" | ||||
|   | "zoomToFit" | ||||
|   | "zoomToSelection" | ||||
|   | "changeFontFamily" | ||||
|   | "changeTextAlign" | ||||
|   | "toggleFullScreen" | ||||
|   | "toggleShortcuts" | ||||
|   | "group" | ||||
|   | "ungroup" | ||||
|   | "goToCollaborator" | ||||
|   | "addToLibrary" | ||||
|   | "changeSharpness" | ||||
|   | "alignTop" | ||||
|   | "alignBottom" | ||||
|   | "alignLeft" | ||||
|   | "alignRight" | ||||
|   | "alignVerticallyCentered" | ||||
|   | "alignHorizontallyCentered" | ||||
|   | "distributeHorizontally" | ||||
|   | "distributeVertically" | ||||
|   | "flipHorizontal" | ||||
|   | "flipVertical" | ||||
|   | "viewMode" | ||||
|   | "exportWithDarkMode" | ||||
|   | "toggleTheme"; | ||||
|  | ||||
| export interface Action { | ||||
|   name: ActionName; | ||||
|   PanelComponent?: React.FC<PanelComponentProps>; | ||||
|   PanelComponent?: React.FC<{ | ||||
|     elements: readonly ExcalidrawElement[]; | ||||
|     appState: AppState; | ||||
|     updateData: (formData?: any) => void; | ||||
|     appProps: ExcalidrawProps; | ||||
|     id?: string; | ||||
|   }>; | ||||
|   perform: ActionFn; | ||||
|   keyPriority?: number; | ||||
|   keyTest?: ( | ||||
| @@ -139,39 +117,18 @@ export interface Action { | ||||
|     appState: AppState, | ||||
|     elements: readonly ExcalidrawElement[], | ||||
|   ) => boolean; | ||||
|   contextItemLabel?: | ||||
|     | string | ||||
|     | (( | ||||
|         elements: readonly ExcalidrawElement[], | ||||
|         appState: Readonly<AppState>, | ||||
|       ) => string); | ||||
|   predicate?: ( | ||||
|   contextItemLabel?: string; | ||||
|   contextItemPredicate?: ( | ||||
|     elements: readonly ExcalidrawElement[], | ||||
|     appState: AppState, | ||||
|     appProps: ExcalidrawProps, | ||||
|     app: AppClassProperties, | ||||
|   ) => boolean; | ||||
|   checked?: (appState: Readonly<AppState>) => boolean; | ||||
|   trackEvent: | ||||
|     | false | ||||
|     | { | ||||
|         category: | ||||
|           | "toolbar" | ||||
|           | "element" | ||||
|           | "canvas" | ||||
|           | "export" | ||||
|           | "history" | ||||
|           | "menu" | ||||
|           | "collab" | ||||
|           | "hyperlink"; | ||||
|         action?: string; | ||||
|         predicate?: ( | ||||
|           appState: Readonly<AppState>, | ||||
|           elements: readonly ExcalidrawElement[], | ||||
|           value: any, | ||||
|         ) => boolean; | ||||
|       }; | ||||
|   /** if set to `true`, allow action to be performed in viewMode. | ||||
|    *  Defaults to `false` */ | ||||
|   viewMode?: boolean; | ||||
| } | ||||
|  | ||||
| export interface ActionsManagerInterface { | ||||
|   actions: Record<ActionName, Action>; | ||||
|   registerAction: (action: Action) => void; | ||||
|   handleKeyDown: (event: React.KeyboardEvent | KeyboardEvent) => boolean; | ||||
|   renderAction: (name: ActionName) => React.ReactElement | null; | ||||
|   executeAction: (action: Action) => void; | ||||
| } | ||||
|   | ||||
							
								
								
									
										37
									
								
								src/align.ts
									
									
									
									
									
								
							
							
						
						| @@ -1,7 +1,13 @@ | ||||
| import { ExcalidrawElement } from "./element/types"; | ||||
| import { newElementWith } from "./element/mutateElement"; | ||||
| import { Box, getCommonBoundingBox } from "./element/bounds"; | ||||
| import { getMaximumGroups } from "./groups"; | ||||
| import { getCommonBounds } from "./element"; | ||||
|  | ||||
| interface Box { | ||||
|   minX: number; | ||||
|   minY: number; | ||||
|   maxX: number; | ||||
|   maxY: number; | ||||
| } | ||||
|  | ||||
| export interface Alignment { | ||||
|   position: "start" | "center" | "end"; | ||||
| @@ -31,6 +37,28 @@ export const alignElements = ( | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const getMaximumGroups = ( | ||||
|   elements: ExcalidrawElement[], | ||||
| ): ExcalidrawElement[][] => { | ||||
|   const groups: Map<String, ExcalidrawElement[]> = new Map< | ||||
|     String, | ||||
|     ExcalidrawElement[] | ||||
|   >(); | ||||
|  | ||||
|   elements.forEach((element: ExcalidrawElement) => { | ||||
|     const groupId = | ||||
|       element.groupIds.length === 0 | ||||
|         ? element.id | ||||
|         : element.groupIds[element.groupIds.length - 1]; | ||||
|  | ||||
|     const currentGroupMembers = groups.get(groupId) || []; | ||||
|  | ||||
|     groups.set(groupId, [...currentGroupMembers, element]); | ||||
|   }); | ||||
|  | ||||
|   return Array.from(groups.values()); | ||||
| }; | ||||
|  | ||||
| const calculateTranslation = ( | ||||
|   group: ExcalidrawElement[], | ||||
|   selectionBoundingBox: Box, | ||||
| @@ -60,3 +88,8 @@ const calculateTranslation = ( | ||||
|       (groupBoundingBox[min] + groupBoundingBox[max]) / 2, | ||||
|   }; | ||||
| }; | ||||
|  | ||||
| const getCommonBoundingBox = (elements: ExcalidrawElement[]): Box => { | ||||
|   const [minX, minY, maxX, maxY] = getCommonBounds(elements); | ||||
|   return { minX, minY, maxX, maxY }; | ||||
| }; | ||||
|   | ||||
| @@ -3,20 +3,16 @@ export const trackEvent = | ||||
|   process.env?.REACT_APP_GOOGLE_ANALYTICS_ID && | ||||
|   typeof window !== "undefined" && | ||||
|   window.gtag | ||||
|     ? (category: string, action: string, label?: string, value?: number) => { | ||||
|         try { | ||||
|           window.gtag("event", action, { | ||||
|             event_category: category, | ||||
|             event_label: label, | ||||
|             value, | ||||
|           }); | ||||
|         } catch (error) { | ||||
|           console.error("error logging to ga", error); | ||||
|         } | ||||
|     ? (category: string, name: string, label?: string, value?: number) => { | ||||
|         window.gtag("event", name, { | ||||
|           event_category: category, | ||||
|           event_label: label, | ||||
|           value, | ||||
|         }); | ||||
|       } | ||||
|     : typeof process !== "undefined" && process.env?.JEST_WORKER_ID | ||||
|     ? (category: string, action: string, label?: string, value?: number) => {} | ||||
|     : (category: string, action: string, label?: string, value?: number) => { | ||||
|     ? (category: string, name: string, label?: string, value?: number) => {} | ||||
|     : (category: string, name: string, label?: string, value?: number) => { | ||||
|         // Uncomment the next line to track locally | ||||
|         // console.log("Track Event", { category, action, label, value }); | ||||
|         // console.info("Track Event", category, name, label, value); | ||||
|       }; | ||||
|   | ||||