Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4330f3ec9d | |||
| d5aad6202d | |||
| db73e30eae | |||
| f472af04a9 | |||
| c08be69618 | |||
| b42b1a193d | |||
| f6d85bc80f | |||
| 0457ac9063 |
@@ -9,11 +9,11 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- name: Set up publish access
|
- name: Set up publish access
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ jobs:
|
|||||||
build-docker:
|
build-docker:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
- run: docker build -t excalidraw .
|
- run: docker build -t excalidraw .
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.PUSH_TRANSLATIONS_COVERAGE_PAT }}
|
token: ${{ secrets.PUSH_TRANSLATIONS_COVERAGE_PAT }}
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2
|
uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ jobs:
|
|||||||
sentry:
|
sentry:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- name: Install and build
|
- name: Install and build
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ jobs:
|
|||||||
CI_JOB_NUMBER: 1
|
CI_JOB_NUMBER: 1
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- name: Install in packages/excalidraw
|
- name: Install in packages/excalidraw
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
- name: "Install Node"
|
- name: "Install Node"
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: "20.x"
|
node-version: "20.x"
|
||||||
- name: "Install Deps"
|
- name: "Install Deps"
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- name: Install and test
|
- name: Install and test
|
||||||
|
|||||||
+3
-3
@@ -1,4 +1,4 @@
|
|||||||
FROM --platform=${BUILDPLATFORM} node:24 AS build
|
FROM --platform=${BUILDPLATFORM} node:24@sha256:8530f76a96d88820d288761f022e318970dda93d01536919fbc16076b7983e63 AS build
|
||||||
|
|
||||||
WORKDIR /opt/node_app
|
WORKDIR /opt/node_app
|
||||||
|
|
||||||
@@ -7,13 +7,13 @@ COPY . .
|
|||||||
# do not ignore optional dependencies:
|
# do not ignore optional dependencies:
|
||||||
# Error: Cannot find module @rollup/rollup-linux-x64-gnu
|
# Error: Cannot find module @rollup/rollup-linux-x64-gnu
|
||||||
RUN --mount=type=cache,target=/root/.cache/yarn \
|
RUN --mount=type=cache,target=/root/.cache/yarn \
|
||||||
npm_config_target_arch=${TARGETARCH} yarn --network-timeout 600000
|
npm_config_target_arch=${TARGETARCH} yarn --frozen-lockfile --network-timeout 600000
|
||||||
|
|
||||||
ARG NODE_ENV=production
|
ARG NODE_ENV=production
|
||||||
|
|
||||||
RUN npm_config_target_arch=${TARGETARCH} yarn build:app:docker
|
RUN npm_config_target_arch=${TARGETARCH} yarn build:app:docker
|
||||||
|
|
||||||
FROM nginx:1.27-alpine
|
FROM nginx:stable-alpine-slim@sha256:2c605dbeab79a6b2a63340474fe58119d0ef95bdc4b1f41df0aa689659b3d13b
|
||||||
|
|
||||||
COPY --from=build /opt/node_app/excalidraw-app/build /usr/share/nginx/html
|
COPY --from=build /opt/node_app/excalidraw-app/build /usr/share/nginx/html
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import * as excalidrawLib from "@excalidraw/excalidraw";
|
import * as excalidrawLib from "@excalidraw/excalidraw";
|
||||||
import { Excalidraw } from "@excalidraw/excalidraw";
|
import { Excalidraw } from "@excalidraw/excalidraw";
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ const ExcalidrawWrapper: React.FC = () => {
|
|||||||
appTitle={"Excalidraw with Nextjs Example"}
|
appTitle={"Excalidraw with Nextjs Example"}
|
||||||
useCustom={(api: any, args?: any[]) => {}}
|
useCustom={(api: any, args?: any[]) => {}}
|
||||||
excalidrawLib={excalidrawLib}
|
excalidrawLib={excalidrawLib}
|
||||||
|
showFadeDemo={true}
|
||||||
>
|
>
|
||||||
<Excalidraw />
|
<Excalidraw />
|
||||||
</App>
|
</App>
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ export interface AppProps {
|
|||||||
customArgs?: any[];
|
customArgs?: any[];
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
excalidrawLib: typeof TExcalidraw;
|
excalidrawLib: typeof TExcalidraw;
|
||||||
|
showFadeDemo?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ExampleApp({
|
export default function ExampleApp({
|
||||||
@@ -78,6 +79,7 @@ export default function ExampleApp({
|
|||||||
customArgs,
|
customArgs,
|
||||||
children,
|
children,
|
||||||
excalidrawLib,
|
excalidrawLib,
|
||||||
|
showFadeDemo = false,
|
||||||
}: AppProps) {
|
}: AppProps) {
|
||||||
const {
|
const {
|
||||||
exportToCanvas,
|
exportToCanvas,
|
||||||
@@ -116,6 +118,19 @@ export default function ExampleApp({
|
|||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
const [comment, setComment] = useState<Comment | null>(null);
|
const [comment, setComment] = useState<Comment | null>(null);
|
||||||
|
const [hideAllForFadeDemo, setHideAllForFadeDemo] = useState(false);
|
||||||
|
const [fadeDemoNextIndex, setFadeDemoNextIndex] = useState(0);
|
||||||
|
const [fadeDemoElementIds, setFadeDemoElementIds] = useState<string[]>([]);
|
||||||
|
const [demoAnimationType, setDemoAnimationType] = useState<"fade" | "fly">(
|
||||||
|
"fade",
|
||||||
|
);
|
||||||
|
const [demoAnimationDuration, setDemoAnimationDuration] = useState(500);
|
||||||
|
const [demoFlyFrom, setDemoFlyFrom] = useState<
|
||||||
|
"left" | "right" | "top" | "bottom"
|
||||||
|
>("left");
|
||||||
|
const [demoAnimationEasing, setDemoAnimationEasing] = useState<
|
||||||
|
"linear" | "easeOut" | "easeInOut"
|
||||||
|
>("easeOut");
|
||||||
|
|
||||||
const initialStatePromiseRef = useRef<{
|
const initialStatePromiseRef = useRef<{
|
||||||
promise: ResolvablePromise<ExcalidrawInitialDataState | null>;
|
promise: ResolvablePromise<ExcalidrawInitialDataState | null>;
|
||||||
@@ -178,7 +193,8 @@ export default function ExampleApp({
|
|||||||
const newElement = cloneElement(
|
const newElement = cloneElement(
|
||||||
Excalidraw,
|
Excalidraw,
|
||||||
{
|
{
|
||||||
excalidrawAPI: (api: ExcalidrawImperativeAPI) => setExcalidrawAPI(api),
|
onExcalidrawAPI: (api: ExcalidrawImperativeAPI | null) =>
|
||||||
|
setExcalidrawAPI(api),
|
||||||
initialData: initialStatePromiseRef.current.promise,
|
initialData: initialStatePromiseRef.current.promise,
|
||||||
onChange: (
|
onChange: (
|
||||||
elements: NonDeletedExcalidrawElement[],
|
elements: NonDeletedExcalidrawElement[],
|
||||||
@@ -208,6 +224,10 @@ export default function ExampleApp({
|
|||||||
onPointerDown,
|
onPointerDown,
|
||||||
onScrollChange: rerenderCommentIcons,
|
onScrollChange: rerenderCommentIcons,
|
||||||
validateEmbeddable: true,
|
validateEmbeddable: true,
|
||||||
|
resolveRenderOpacity: hideAllForFadeDemo
|
||||||
|
? (element: NonDeletedExcalidrawElement) =>
|
||||||
|
fadeDemoElementIds.includes(element.id) ? 0 : undefined
|
||||||
|
: undefined,
|
||||||
},
|
},
|
||||||
<>
|
<>
|
||||||
{excalidrawAPI && (
|
{excalidrawAPI && (
|
||||||
@@ -664,6 +684,229 @@ export default function ExampleApp({
|
|||||||
>
|
>
|
||||||
Reset Scene
|
Reset Scene
|
||||||
</button>
|
</button>
|
||||||
|
{showFadeDemo && (
|
||||||
|
<>
|
||||||
|
<label>
|
||||||
|
Animation type
|
||||||
|
<select
|
||||||
|
value={demoAnimationType}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoAnimationType(
|
||||||
|
event.target.value as "fade" | "fly",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="fade">fade</option>
|
||||||
|
<option value="fly">fly</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
Duration
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={demoAnimationDuration}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoAnimationDuration(Number(event.target.value) || 0)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
Easing
|
||||||
|
<select
|
||||||
|
value={demoAnimationEasing}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoAnimationEasing(
|
||||||
|
event.target.value as "linear" | "easeOut" | "easeInOut",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="linear">linear</option>
|
||||||
|
<option value="easeOut">easeOut</option>
|
||||||
|
<option value="easeInOut">easeInOut</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
{demoAnimationType === "fly" && (
|
||||||
|
<label>
|
||||||
|
Fly from
|
||||||
|
<select
|
||||||
|
value={demoFlyFrom}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoFlyFrom(
|
||||||
|
event.target.value as
|
||||||
|
| "left"
|
||||||
|
| "right"
|
||||||
|
| "top"
|
||||||
|
| "bottom",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="left">left</option>
|
||||||
|
<option value="right">right</option>
|
||||||
|
<option value="top">top</option>
|
||||||
|
<option value="bottom">bottom</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoElementIds(
|
||||||
|
excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.map((element) => element.id),
|
||||||
|
);
|
||||||
|
excalidrawAPI.clearElementAnimationOverrides();
|
||||||
|
setHideAllForFadeDemo(true);
|
||||||
|
setFadeDemoNextIndex(0);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Hide every element
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI || !hideAllForFadeDemo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.filter((element) =>
|
||||||
|
fadeDemoElementIds.includes(element.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!elements.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextIndex =
|
||||||
|
fadeDemoNextIndex >= elements.length
|
||||||
|
? 0
|
||||||
|
: fadeDemoNextIndex;
|
||||||
|
|
||||||
|
if (nextIndex === 0) {
|
||||||
|
excalidrawAPI.clearElementAnimationOverrides();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demoAnimationType === "fly") {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[nextIndex].id],
|
||||||
|
type: "fly",
|
||||||
|
from: demoFlyFrom,
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[nextIndex].id],
|
||||||
|
type: "fade",
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoNextIndex(nextIndex + 1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Animate in next element
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI || !hideAllForFadeDemo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.filter((element) =>
|
||||||
|
fadeDemoElementIds.includes(element.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!elements.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demoAnimationType === "fly") {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements,
|
||||||
|
type: "fly",
|
||||||
|
from: demoFlyFrom,
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
stagger: 120,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements,
|
||||||
|
type: "fade",
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
stagger: 120,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoNextIndex(elements.length);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Animate in all
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI || !hideAllForFadeDemo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.filter((element) =>
|
||||||
|
fadeDemoElementIds.includes(element.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!elements.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prevIndex = Math.min(
|
||||||
|
fadeDemoNextIndex - 1,
|
||||||
|
elements.length - 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (prevIndex < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demoAnimationType === "fly") {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[prevIndex].id],
|
||||||
|
type: "fly",
|
||||||
|
from: demoFlyFrom,
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "out",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[prevIndex].id],
|
||||||
|
type: "fade",
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "out",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoNextIndex(prevIndex);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Animate out prev element
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const libraryItems: LibraryItems = [
|
const libraryItems: LibraryItems = [
|
||||||
|
|||||||
@@ -2,6 +2,28 @@ import type { ExcalidrawElementSkeleton } from "@excalidraw/excalidraw/element/t
|
|||||||
import type { FileId } from "@excalidraw/excalidraw/element/types";
|
import type { FileId } from "@excalidraw/excalidraw/element/types";
|
||||||
|
|
||||||
const elements: ExcalidrawElementSkeleton[] = [
|
const elements: ExcalidrawElementSkeleton[] = [
|
||||||
|
// {
|
||||||
|
// type: "arrow",
|
||||||
|
// x: 100,
|
||||||
|
// y: 500,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// type: "arrow",
|
||||||
|
// x: 250,
|
||||||
|
// y: 250,
|
||||||
|
// label: {
|
||||||
|
// text: "HELLO WORLD!!",
|
||||||
|
// },
|
||||||
|
// start: {
|
||||||
|
// type: "rectangle",
|
||||||
|
// // x: -100,
|
||||||
|
// },
|
||||||
|
// end: {
|
||||||
|
// type: "ellipse",
|
||||||
|
// // x: 300,
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
|
||||||
{
|
{
|
||||||
type: "rectangle",
|
type: "rectangle",
|
||||||
x: 10,
|
x: 10,
|
||||||
@@ -22,14 +44,14 @@ const elements: ExcalidrawElementSkeleton[] = [
|
|||||||
},
|
},
|
||||||
id: "2",
|
id: "2",
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
type: "arrow",
|
// type: "arrow",
|
||||||
x: 100,
|
// x: 100,
|
||||||
y: 200,
|
// y: 200,
|
||||||
label: { text: "HELLO WORLD!!" },
|
// label: { text: "HELLO WORLD!!" },
|
||||||
start: { type: "rectangle" },
|
// start: { type: "rectangle" },
|
||||||
end: { type: "ellipse" },
|
// end: { type: "ellipse" },
|
||||||
},
|
// },
|
||||||
{
|
{
|
||||||
type: "image",
|
type: "image",
|
||||||
x: 606.1042326312408,
|
x: 606.1042326312408,
|
||||||
@@ -38,11 +60,11 @@ const elements: ExcalidrawElementSkeleton[] = [
|
|||||||
height: 230,
|
height: 230,
|
||||||
fileId: "rocket" as FileId,
|
fileId: "rocket" as FileId,
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
type: "frame",
|
// type: "frame",
|
||||||
children: ["1", "2"],
|
// children: ["1", "2"],
|
||||||
name: "My frame",
|
// name: "My frame",
|
||||||
},
|
// },
|
||||||
];
|
];
|
||||||
export default {
|
export default {
|
||||||
elements,
|
elements,
|
||||||
|
|||||||
@@ -337,9 +337,10 @@ export const MAX_DECIMALS_FOR_SVG_EXPORT = 2;
|
|||||||
export const EXPORT_SCALES = [1, 2, 3];
|
export const EXPORT_SCALES = [1, 2, 3];
|
||||||
export const DEFAULT_EXPORT_PADDING = 10; // px
|
export const DEFAULT_EXPORT_PADDING = 10; // px
|
||||||
|
|
||||||
export const DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT = 1440;
|
export const DEFAULT_IMAGE_OPTIONS: AppProps["imageOptions"] = {
|
||||||
|
maxWidthOrHeight: 1440,
|
||||||
export const MAX_ALLOWED_FILE_BYTES = 4 * 1024 * 1024;
|
maxFileSizeBytes: 4 * 1024 * 1024,
|
||||||
|
};
|
||||||
|
|
||||||
export const SVG_NS = "http://www.w3.org/2000/svg";
|
export const SVG_NS = "http://www.w3.org/2000/svg";
|
||||||
export const SVG_DOCUMENT_PREAMBLE = `<?xml version="1.0" standalone="no"?>
|
export const SVG_DOCUMENT_PREAMBLE = `<?xml version="1.0" standalone="no"?>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import rough from "roughjs/bin/rough";
|
import rough from "roughjs/bin/rough";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
clamp,
|
||||||
type GlobalPoint,
|
type GlobalPoint,
|
||||||
isRightAngleRads,
|
isRightAngleRads,
|
||||||
lineSegment,
|
lineSegment,
|
||||||
@@ -105,8 +106,62 @@ const getCanvasPadding = (element: ExcalidrawElement) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const resolveRenderOpacity = (
|
||||||
|
element: ExcalidrawElement,
|
||||||
|
renderConfig: Pick<
|
||||||
|
StaticCanvasRenderConfig,
|
||||||
|
"elementOpacityOverrides" | "resolveRenderOpacity"
|
||||||
|
>,
|
||||||
|
) => {
|
||||||
|
const override = renderConfig.elementOpacityOverrides?.get(element.id);
|
||||||
|
|
||||||
|
if (override !== undefined) {
|
||||||
|
return clamp(override, 0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedOpacity = renderConfig.resolveRenderOpacity?.(
|
||||||
|
element as NonDeletedExcalidrawElement,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (resolvedOpacity !== undefined) {
|
||||||
|
return clamp(resolvedOpacity, 0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return element.opacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const resolveRenderPositionOffset = (
|
||||||
|
element: ExcalidrawElement,
|
||||||
|
renderConfig: Pick<StaticCanvasRenderConfig, "elementPositionOverrides">,
|
||||||
|
) => {
|
||||||
|
return renderConfig.elementPositionOverrides?.get(element.id) ?? { x: 0, y: 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getRenderElementWithPositionOverride = <
|
||||||
|
TElement extends NonDeletedExcalidrawElement,
|
||||||
|
>(
|
||||||
|
element: TElement,
|
||||||
|
renderConfig: Pick<StaticCanvasRenderConfig, "elementPositionOverrides">,
|
||||||
|
): TElement => {
|
||||||
|
const positionOffset = resolveRenderPositionOffset(element, renderConfig);
|
||||||
|
|
||||||
|
if (positionOffset.x === 0 && positionOffset.y === 0) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...element,
|
||||||
|
x: element.x + positionOffset.x,
|
||||||
|
y: element.y + positionOffset.y,
|
||||||
|
} as TElement;
|
||||||
|
};
|
||||||
|
|
||||||
export const getRenderOpacity = (
|
export const getRenderOpacity = (
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
|
renderConfig: Pick<
|
||||||
|
StaticCanvasRenderConfig,
|
||||||
|
"elementOpacityOverrides" | "resolveRenderOpacity"
|
||||||
|
>,
|
||||||
containingFrame: ExcalidrawFrameLikeElement | null,
|
containingFrame: ExcalidrawFrameLikeElement | null,
|
||||||
elementsPendingErasure: ElementsPendingErasure,
|
elementsPendingErasure: ElementsPendingErasure,
|
||||||
pendingNodes: Readonly<PendingExcalidrawElements> | null,
|
pendingNodes: Readonly<PendingExcalidrawElements> | null,
|
||||||
@@ -115,7 +170,8 @@ export const getRenderOpacity = (
|
|||||||
// multiplying frame opacity with element opacity to combine them
|
// multiplying frame opacity with element opacity to combine them
|
||||||
// (e.g. frame 50% and element 50% opacity should result in 25% opacity)
|
// (e.g. frame 50% and element 50% opacity should result in 25% opacity)
|
||||||
let opacity =
|
let opacity =
|
||||||
(((containingFrame?.opacity ?? 100) * element.opacity) / 10000) *
|
(((containingFrame?.opacity ?? 100) * resolveRenderOpacity(element, renderConfig)) /
|
||||||
|
10000) *
|
||||||
globalAlpha;
|
globalAlpha;
|
||||||
|
|
||||||
// if pending erasure, multiply again to combine further
|
// if pending erasure, multiply again to combine further
|
||||||
@@ -791,8 +847,11 @@ export const renderElement = (
|
|||||||
!appState.selectedElementIds[element.id] &&
|
!appState.selectedElementIds[element.id] &&
|
||||||
!appState.hoveredElementIds[element.id];
|
!appState.hoveredElementIds[element.id];
|
||||||
|
|
||||||
|
element = getRenderElementWithPositionOverride(element, renderConfig);
|
||||||
|
|
||||||
context.globalAlpha = getRenderOpacity(
|
context.globalAlpha = getRenderOpacity(
|
||||||
element,
|
element,
|
||||||
|
renderConfig,
|
||||||
getContainingFrame(element, elementsMap),
|
getContainingFrame(element, elementsMap),
|
||||||
renderConfig.elementsPendingErasure,
|
renderConfig.elementsPendingErasure,
|
||||||
renderConfig.pendingFlowchartNodes,
|
renderConfig.pendingFlowchartNodes,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { LaserPointer } from "@excalidraw/laser-pointer";
|
import { LaserPointer } from "@excalidraw/laser-pointer";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
SVG_NS,
|
SVG_NS,
|
||||||
getSvgPathFromStroke,
|
getSvgPathFromStroke,
|
||||||
@@ -8,7 +7,8 @@ import {
|
|||||||
|
|
||||||
import type { LaserPointerOptions } from "@excalidraw/laser-pointer";
|
import type { LaserPointerOptions } from "@excalidraw/laser-pointer";
|
||||||
|
|
||||||
import type { AnimationFrameHandler } from "./animation-frame-handler";
|
import { AnimationController } from "./renderer/animation";
|
||||||
|
|
||||||
import type App from "./components/App";
|
import type App from "./components/App";
|
||||||
import type { AppState } from "./types";
|
import type { AppState } from "./types";
|
||||||
|
|
||||||
@@ -34,15 +34,16 @@ export class AnimatedTrail implements Trail {
|
|||||||
private container?: SVGSVGElement;
|
private container?: SVGSVGElement;
|
||||||
private trailElement: SVGPathElement;
|
private trailElement: SVGPathElement;
|
||||||
private trailAnimation?: SVGAnimateElement;
|
private trailAnimation?: SVGAnimateElement;
|
||||||
|
private key: string;
|
||||||
|
|
||||||
|
private static counter = 0;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private animationFrameHandler: AnimationFrameHandler,
|
|
||||||
protected app: App,
|
protected app: App,
|
||||||
private options: Partial<LaserPointerOptions> &
|
private options: Partial<LaserPointerOptions> &
|
||||||
Partial<AnimatedTrailOptions>,
|
Partial<AnimatedTrailOptions>,
|
||||||
) {
|
) {
|
||||||
this.animationFrameHandler.register(this, this.onFrame.bind(this));
|
this.key = `animated-trail-${AnimatedTrail.counter++}`;
|
||||||
|
|
||||||
this.trailElement = document.createElementNS(SVG_NS, "path");
|
this.trailElement = document.createElementNS(SVG_NS, "path");
|
||||||
if (this.options.animateTrail) {
|
if (this.options.animateTrail) {
|
||||||
this.trailAnimation = document.createElementNS(SVG_NS, "animate");
|
this.trailAnimation = document.createElementNS(SVG_NS, "animate");
|
||||||
@@ -73,6 +74,15 @@ export class AnimatedTrail implements Trail {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private cleanup() {
|
||||||
|
this.pastTrails = [];
|
||||||
|
this.currentTrail = undefined;
|
||||||
|
|
||||||
|
if (this.trailElement.parentNode === this.container) {
|
||||||
|
this.container?.removeChild(this.trailElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
start(container?: SVGSVGElement) {
|
start(container?: SVGSVGElement) {
|
||||||
if (container) {
|
if (container) {
|
||||||
this.container = container;
|
this.container = container;
|
||||||
@@ -82,15 +92,23 @@ export class AnimatedTrail implements Trail {
|
|||||||
this.container.appendChild(this.trailElement);
|
this.container.appendChild(this.trailElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.animationFrameHandler.start(this);
|
if (!AnimationController.running(this.key)) {
|
||||||
|
AnimationController.start(this.key, () => {
|
||||||
|
const needsNext = this.onFrame();
|
||||||
|
if (needsNext) {
|
||||||
|
return { keep: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cleanup();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.animationFrameHandler.stop(this);
|
AnimationController.cancel(this.key);
|
||||||
|
this.cleanup();
|
||||||
if (this.trailElement.parentNode === this.container) {
|
|
||||||
this.container?.removeChild(this.trailElement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
startPath(x: number, y: number) {
|
startPath(x: number, y: number) {
|
||||||
@@ -145,21 +163,25 @@ export class AnimatedTrail implements Trail {
|
|||||||
|
|
||||||
if (this.currentTrail) {
|
if (this.currentTrail) {
|
||||||
const currentPath = this.drawTrail(this.currentTrail, this.app.state);
|
const currentPath = this.drawTrail(this.currentTrail, this.app.state);
|
||||||
|
|
||||||
paths.push(currentPath);
|
paths.push(currentPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pastTrails = this.pastTrails.filter((trail) => {
|
this.pastTrails = this.pastTrails.filter(
|
||||||
return trail.getStrokeOutline().length !== 0;
|
(t) =>
|
||||||
});
|
t.getStrokeOutline(t.options.size / this.app.state.zoom.value)
|
||||||
|
.length !== 0,
|
||||||
|
);
|
||||||
|
|
||||||
if (paths.length === 0) {
|
if (paths.length === 0) {
|
||||||
this.stop();
|
// Clean up the SVG path if there are no trails to render
|
||||||
|
this.trailElement.setAttribute("d", "");
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const svgPaths = paths.join(" ").trim();
|
const svgPaths = paths.join(" ").trim();
|
||||||
|
|
||||||
this.trailElement.setAttribute("d", svgPaths);
|
this.trailElement.setAttribute("d", svgPaths);
|
||||||
|
|
||||||
if (this.trailAnimation) {
|
if (this.trailAnimation) {
|
||||||
this.trailElement.setAttribute(
|
this.trailElement.setAttribute(
|
||||||
"fill",
|
"fill",
|
||||||
@@ -175,6 +197,8 @@ export class AnimatedTrail implements Trail {
|
|||||||
(this.options.fill ?? (() => "black"))(this),
|
(this.options.fill ?? (() => "black"))(this),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private drawTrail(trail: LaserPointer, state: AppState): string {
|
private drawTrail(trail: LaserPointer, state: AppState): string {
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
export type AnimationCallback = (timestamp: number) => void | boolean;
|
|
||||||
|
|
||||||
export type AnimationTarget = {
|
|
||||||
callback: AnimationCallback;
|
|
||||||
stopped: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export class AnimationFrameHandler {
|
|
||||||
private targets = new WeakMap<object, AnimationTarget>();
|
|
||||||
private rafIds = new WeakMap<object, number>();
|
|
||||||
|
|
||||||
register(key: object, callback: AnimationCallback) {
|
|
||||||
this.targets.set(key, { callback, stopped: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
start(key: object) {
|
|
||||||
const target = this.targets.get(key);
|
|
||||||
|
|
||||||
if (!target) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.rafIds.has(key)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.targets.set(key, { ...target, stopped: false });
|
|
||||||
this.scheduleFrame(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
stop(key: object) {
|
|
||||||
const target = this.targets.get(key);
|
|
||||||
if (target && !target.stopped) {
|
|
||||||
this.targets.set(key, { ...target, stopped: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cancelFrame(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
private constructFrame(key: object): FrameRequestCallback {
|
|
||||||
return (timestamp: number) => {
|
|
||||||
const target = this.targets.get(key);
|
|
||||||
|
|
||||||
if (!target) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const shouldAbort = this.onFrame(target, timestamp);
|
|
||||||
|
|
||||||
if (!target.stopped && !shouldAbort) {
|
|
||||||
this.scheduleFrame(key);
|
|
||||||
} else {
|
|
||||||
this.cancelFrame(key);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private scheduleFrame(key: object) {
|
|
||||||
const rafId = requestAnimationFrame(this.constructFrame(key));
|
|
||||||
|
|
||||||
this.rafIds.set(key, rafId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private cancelFrame(key: object) {
|
|
||||||
if (this.rafIds.has(key)) {
|
|
||||||
const rafId = this.rafIds.get(key)!;
|
|
||||||
|
|
||||||
cancelAnimationFrame(rafId);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.rafIds.delete(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
private onFrame(target: AnimationTarget, timestamp: number): boolean {
|
|
||||||
const shouldAbort = target.callback(timestamp);
|
|
||||||
|
|
||||||
return shouldAbort ?? false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -28,7 +28,6 @@ import {
|
|||||||
APP_NAME,
|
APP_NAME,
|
||||||
CURSOR_TYPE,
|
CURSOR_TYPE,
|
||||||
DEFAULT_TRANSFORM_HANDLE_SPACING,
|
DEFAULT_TRANSFORM_HANDLE_SPACING,
|
||||||
DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT,
|
|
||||||
DEFAULT_VERTICAL_ALIGN,
|
DEFAULT_VERTICAL_ALIGN,
|
||||||
DRAGGING_THRESHOLD,
|
DRAGGING_THRESHOLD,
|
||||||
ELEMENT_SHIFT_TRANSLATE_AMOUNT,
|
ELEMENT_SHIFT_TRANSLATE_AMOUNT,
|
||||||
@@ -38,7 +37,6 @@ import {
|
|||||||
IMAGE_MIME_TYPES,
|
IMAGE_MIME_TYPES,
|
||||||
IMAGE_RENDER_TIMEOUT,
|
IMAGE_RENDER_TIMEOUT,
|
||||||
LINE_CONFIRM_THRESHOLD,
|
LINE_CONFIRM_THRESHOLD,
|
||||||
MAX_ALLOWED_FILE_BYTES,
|
|
||||||
MIME_TYPES,
|
MIME_TYPES,
|
||||||
MQ_RIGHT_SIDEBAR_MIN_WIDTH,
|
MQ_RIGHT_SIDEBAR_MIN_WIDTH,
|
||||||
POINTER_BUTTON,
|
POINTER_BUTTON,
|
||||||
@@ -209,10 +207,12 @@ import {
|
|||||||
getLineHeightInPx,
|
getLineHeightInPx,
|
||||||
getApproxMinLineWidth,
|
getApproxMinLineWidth,
|
||||||
getApproxMinLineHeight,
|
getApproxMinLineHeight,
|
||||||
getMinTextElementWidth,
|
getMinTextElementWidth,
|
||||||
ShapeCache,
|
ShapeCache,
|
||||||
getRenderOpacity,
|
getRenderOpacity,
|
||||||
editGroupForSelectedElement,
|
resolveRenderPositionOffset,
|
||||||
|
resolveRenderOpacity,
|
||||||
|
editGroupForSelectedElement,
|
||||||
getElementsInGroup,
|
getElementsInGroup,
|
||||||
getSelectedGroupIdForElement,
|
getSelectedGroupIdForElement,
|
||||||
getSelectedGroupIds,
|
getSelectedGroupIds,
|
||||||
@@ -345,7 +345,6 @@ import { ActionManager } from "../actions/manager";
|
|||||||
import { actions } from "../actions/register";
|
import { actions } from "../actions/register";
|
||||||
import { getShortcutFromShortcutName } from "../actions/shortcuts";
|
import { getShortcutFromShortcutName } from "../actions/shortcuts";
|
||||||
import { trackEvent } from "../analytics";
|
import { trackEvent } from "../analytics";
|
||||||
import { AnimationFrameHandler } from "../animation-frame-handler";
|
|
||||||
import {
|
import {
|
||||||
getDefaultAppState,
|
getDefaultAppState,
|
||||||
isEraserActive,
|
isEraserActive,
|
||||||
@@ -419,7 +418,7 @@ import {
|
|||||||
setCursorForShape,
|
setCursorForShape,
|
||||||
} from "../cursor";
|
} from "../cursor";
|
||||||
import { ElementCanvasButtons } from "../components/ElementCanvasButtons";
|
import { ElementCanvasButtons } from "../components/ElementCanvasButtons";
|
||||||
import { LaserTrails } from "../laser-trails";
|
import { LaserTrails } from "../laserTrails";
|
||||||
import { withBatchedUpdates, withBatchedUpdatesThrottled } from "../reactUtils";
|
import { withBatchedUpdates, withBatchedUpdatesThrottled } from "../reactUtils";
|
||||||
import { isPointHittingTextAutoResizeHandle } from "../textAutoResizeHandle";
|
import { isPointHittingTextAutoResizeHandle } from "../textAutoResizeHandle";
|
||||||
import { textWysiwyg } from "../wysiwyg/textWysiwyg";
|
import { textWysiwyg } from "../wysiwyg/textWysiwyg";
|
||||||
@@ -453,6 +452,7 @@ import { searchItemInFocusAtom } from "./SearchMenu";
|
|||||||
import { isSidebarDockedAtom } from "./Sidebar/Sidebar";
|
import { isSidebarDockedAtom } from "./Sidebar/Sidebar";
|
||||||
import { StaticCanvas, InteractiveCanvas } from "./canvases";
|
import { StaticCanvas, InteractiveCanvas } from "./canvases";
|
||||||
import NewElementCanvas from "./canvases/NewElementCanvas";
|
import NewElementCanvas from "./canvases/NewElementCanvas";
|
||||||
|
import { AnimationController } from "../renderer/animation";
|
||||||
import { isPointHittingLink } from "./hyperlink/helpers";
|
import { isPointHittingLink } from "./hyperlink/helpers";
|
||||||
import { MagicIcon, copyIcon, fullscreenIcon } from "./icons";
|
import { MagicIcon, copyIcon, fullscreenIcon } from "./icons";
|
||||||
import { AppStateObserver, type OnStateChange } from "./AppStateObserver";
|
import { AppStateObserver, type OnStateChange } from "./AppStateObserver";
|
||||||
@@ -705,11 +705,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
previousPointerMoveCoords: { x: number; y: number } | null = null;
|
previousPointerMoveCoords: { x: number; y: number } | null = null;
|
||||||
lastViewportPosition = { x: 0, y: 0 };
|
lastViewportPosition = { x: 0, y: 0 };
|
||||||
|
|
||||||
animationFrameHandler = new AnimationFrameHandler();
|
laserTrails = new LaserTrails(this);
|
||||||
|
eraserTrail = new EraserTrail(this);
|
||||||
laserTrails = new LaserTrails(this.animationFrameHandler, this);
|
lassoTrail = new LassoTrail(this);
|
||||||
eraserTrail = new EraserTrail(this.animationFrameHandler, this);
|
|
||||||
lassoTrail = new LassoTrail(this.animationFrameHandler, this);
|
|
||||||
|
|
||||||
onChangeEmitter = new Emitter<
|
onChangeEmitter = new Emitter<
|
||||||
[
|
[
|
||||||
@@ -745,6 +743,235 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
onRemoveEventListenersEmitter = new Emitter<[]>();
|
onRemoveEventListenersEmitter = new Emitter<[]>();
|
||||||
|
|
||||||
api: ExcalidrawImperativeAPI;
|
api: ExcalidrawImperativeAPI;
|
||||||
|
private renderAnimationVersion = 0;
|
||||||
|
private elementOpacityOverrides = new Map<string, number>();
|
||||||
|
private elementPositionOverrides = new Map<string, { x: number; y: number }>();
|
||||||
|
private elementAnimationStates = new Map<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
opacityFrom: number;
|
||||||
|
opacityTo: number;
|
||||||
|
positionFrom: { x: number; y: number };
|
||||||
|
positionTo: { x: number; y: number };
|
||||||
|
easing: "linear" | "easeOut" | "easeInOut";
|
||||||
|
duration: number;
|
||||||
|
delay: number;
|
||||||
|
elapsed: number;
|
||||||
|
}
|
||||||
|
>();
|
||||||
|
|
||||||
|
private getElementAnimationKey = () => `${this.id}:animate-element`;
|
||||||
|
|
||||||
|
private bumpRenderAnimationVersion = () => {
|
||||||
|
this.renderAnimationVersion++;
|
||||||
|
this.setState({});
|
||||||
|
};
|
||||||
|
|
||||||
|
private getRenderOpacityConfig = () => ({
|
||||||
|
elementOpacityOverrides: this.elementOpacityOverrides,
|
||||||
|
elementPositionOverrides: this.elementPositionOverrides,
|
||||||
|
resolveRenderOpacity: this.props.resolveRenderOpacity,
|
||||||
|
});
|
||||||
|
|
||||||
|
private getResolvedElementOpacity = (element: NonDeletedExcalidrawElement) => {
|
||||||
|
return resolveRenderOpacity(element, this.getRenderOpacityConfig());
|
||||||
|
};
|
||||||
|
|
||||||
|
private getElementVisibleOpacity = (element: NonDeletedExcalidrawElement) => {
|
||||||
|
return clamp(element.opacity, 0, 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
private applyAnimationEasing = (
|
||||||
|
progress: number,
|
||||||
|
easing: "linear" | "easeOut" | "easeInOut",
|
||||||
|
) => {
|
||||||
|
switch (easing) {
|
||||||
|
case "linear":
|
||||||
|
return progress;
|
||||||
|
case "easeOut":
|
||||||
|
return easeOut(progress);
|
||||||
|
case "easeInOut":
|
||||||
|
return progress < 0.5
|
||||||
|
? 4 * progress * progress * progress
|
||||||
|
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private getFlyPositionOffset = (
|
||||||
|
element: NonDeletedExcalidrawElement,
|
||||||
|
from: "left" | "right" | "top" | "bottom",
|
||||||
|
) => {
|
||||||
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(
|
||||||
|
element,
|
||||||
|
this.scene.getNonDeletedElementsMap(),
|
||||||
|
);
|
||||||
|
const viewportWidth = this.state.width / this.state.zoom.value;
|
||||||
|
const viewportHeight = this.state.height / this.state.zoom.value;
|
||||||
|
const elementWidth = x2 - x1;
|
||||||
|
const elementHeight = y2 - y1;
|
||||||
|
|
||||||
|
switch (from) {
|
||||||
|
case "left":
|
||||||
|
return { x: -(Math.max(viewportWidth, elementWidth) + 64), y: 0 };
|
||||||
|
case "right":
|
||||||
|
return { x: Math.max(viewportWidth, elementWidth) + 64, y: 0 };
|
||||||
|
case "top":
|
||||||
|
return { x: 0, y: -(Math.max(viewportHeight, elementHeight) + 64) };
|
||||||
|
case "bottom":
|
||||||
|
return { x: 0, y: Math.max(viewportHeight, elementHeight) + 64 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private animateElement = ({
|
||||||
|
id,
|
||||||
|
opacityFrom,
|
||||||
|
opacityTo,
|
||||||
|
positionFrom = { x: 0, y: 0 },
|
||||||
|
positionTo = { x: 0, y: 0 },
|
||||||
|
easing,
|
||||||
|
duration,
|
||||||
|
delay,
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
opacityFrom: number;
|
||||||
|
opacityTo: number;
|
||||||
|
positionFrom?: { x: number; y: number };
|
||||||
|
positionTo?: { x: number; y: number };
|
||||||
|
easing: "linear" | "easeOut" | "easeInOut";
|
||||||
|
duration: number;
|
||||||
|
delay: number;
|
||||||
|
}) => {
|
||||||
|
const normalizedOpacityFrom = clamp(opacityFrom, 0, 100);
|
||||||
|
const normalizedOpacityTo = clamp(opacityTo, 0, 100);
|
||||||
|
const normalizedDuration = Math.max(duration, 0);
|
||||||
|
const normalizedDelay = Math.max(delay, 0);
|
||||||
|
|
||||||
|
this.elementOpacityOverrides.set(id, normalizedOpacityFrom);
|
||||||
|
|
||||||
|
if (positionFrom.x !== 0 || positionFrom.y !== 0) {
|
||||||
|
this.elementPositionOverrides.set(id, positionFrom);
|
||||||
|
} else {
|
||||||
|
this.elementPositionOverrides.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalizedDuration === 0 && normalizedDelay === 0) {
|
||||||
|
this.elementAnimationStates.delete(id);
|
||||||
|
this.elementOpacityOverrides.set(id, normalizedOpacityTo);
|
||||||
|
|
||||||
|
if (positionTo.x !== 0 || positionTo.y !== 0) {
|
||||||
|
this.elementPositionOverrides.set(id, positionTo);
|
||||||
|
} else {
|
||||||
|
this.elementPositionOverrides.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.elementAnimationStates.set(id, {
|
||||||
|
opacityFrom: normalizedOpacityFrom,
|
||||||
|
opacityTo: normalizedOpacityTo,
|
||||||
|
positionFrom,
|
||||||
|
positionTo,
|
||||||
|
easing,
|
||||||
|
duration: normalizedDuration,
|
||||||
|
delay: normalizedDelay,
|
||||||
|
elapsed: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
this.syncElementAnimations();
|
||||||
|
};
|
||||||
|
|
||||||
|
private syncElementAnimations = () => {
|
||||||
|
const animationKey = this.getElementAnimationKey();
|
||||||
|
|
||||||
|
if (this.elementAnimationStates.size === 0) {
|
||||||
|
AnimationController.cancel(animationKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (AnimationController.running(animationKey)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AnimationController.start(animationKey, ({ deltaTime }) => {
|
||||||
|
let shouldRerender = false;
|
||||||
|
|
||||||
|
for (const [id, animation] of this.elementAnimationStates) {
|
||||||
|
const element = this.scene.getNonDeletedElement(id);
|
||||||
|
|
||||||
|
if (!element) {
|
||||||
|
this.elementAnimationStates.delete(id);
|
||||||
|
shouldRerender =
|
||||||
|
this.elementOpacityOverrides.delete(id) || shouldRerender;
|
||||||
|
shouldRerender =
|
||||||
|
this.elementPositionOverrides.delete(id) || shouldRerender;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
animation.elapsed += deltaTime;
|
||||||
|
|
||||||
|
const progress =
|
||||||
|
animation.elapsed <= animation.delay
|
||||||
|
? 0
|
||||||
|
: animation.duration === 0
|
||||||
|
? 1
|
||||||
|
: Math.min(
|
||||||
|
(animation.elapsed - animation.delay) / animation.duration,
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
const easedProgress = this.applyAnimationEasing(
|
||||||
|
progress,
|
||||||
|
animation.easing,
|
||||||
|
);
|
||||||
|
|
||||||
|
const nextOpacity =
|
||||||
|
animation.opacityFrom +
|
||||||
|
(animation.opacityTo - animation.opacityFrom) * easedProgress;
|
||||||
|
const nextPosition = {
|
||||||
|
x:
|
||||||
|
animation.positionFrom.x +
|
||||||
|
(animation.positionTo.x - animation.positionFrom.x) * easedProgress,
|
||||||
|
y:
|
||||||
|
animation.positionFrom.y +
|
||||||
|
(animation.positionTo.y - animation.positionFrom.y) * easedProgress,
|
||||||
|
};
|
||||||
|
|
||||||
|
const clampedOpacity = clamp(nextOpacity, 0, 100);
|
||||||
|
|
||||||
|
if (this.elementOpacityOverrides.get(id) !== clampedOpacity) {
|
||||||
|
this.elementOpacityOverrides.set(id, clampedOpacity);
|
||||||
|
shouldRerender = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentPositionOverride =
|
||||||
|
this.elementPositionOverrides.get(id) ?? ({ x: 0, y: 0 } as const);
|
||||||
|
|
||||||
|
if (
|
||||||
|
currentPositionOverride.x !== nextPosition.x ||
|
||||||
|
currentPositionOverride.y !== nextPosition.y
|
||||||
|
) {
|
||||||
|
if (nextPosition.x === 0 && nextPosition.y === 0) {
|
||||||
|
this.elementPositionOverrides.delete(id);
|
||||||
|
} else {
|
||||||
|
this.elementPositionOverrides.set(id, nextPosition);
|
||||||
|
}
|
||||||
|
shouldRerender = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animation.elapsed >= animation.delay + animation.duration) {
|
||||||
|
this.elementAnimationStates.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldRerender) {
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.elementAnimationStates.size > 0 ? {} : undefined;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
private createExcalidrawAPI(): ExcalidrawImperativeAPI {
|
private createExcalidrawAPI(): ExcalidrawImperativeAPI {
|
||||||
const api: ExcalidrawImperativeAPI = {
|
const api: ExcalidrawImperativeAPI = {
|
||||||
@@ -762,6 +989,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
clear: this.resetHistory,
|
clear: this.resetHistory,
|
||||||
},
|
},
|
||||||
scrollToContent: this.scrollToContent,
|
scrollToContent: this.scrollToContent,
|
||||||
|
animateElements: this.animateElements,
|
||||||
|
cancelElementAnimation: this.cancelElementAnimation,
|
||||||
|
clearElementAnimationOverrides: this.clearElementAnimationOverrides,
|
||||||
getSceneElements: this.getSceneElements,
|
getSceneElements: this.getSceneElements,
|
||||||
getAppState: () => this.state,
|
getAppState: () => this.state,
|
||||||
getFiles: () => this.files,
|
getFiles: () => this.files,
|
||||||
@@ -1739,6 +1969,10 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
const isHovered =
|
const isHovered =
|
||||||
this.state.activeEmbeddable?.element === el &&
|
this.state.activeEmbeddable?.element === el &&
|
||||||
this.state.activeEmbeddable?.state === "hover";
|
this.state.activeEmbeddable?.state === "hover";
|
||||||
|
const renderPositionOffset = resolveRenderPositionOffset(
|
||||||
|
el,
|
||||||
|
this.getRenderOpacityConfig(),
|
||||||
|
);
|
||||||
|
|
||||||
// scale video embeds based on zoom (capped) so that smaller embeds
|
// scale video embeds based on zoom (capped) so that smaller embeds
|
||||||
// on canvas when zoomed are still of legible quality
|
// on canvas when zoomed are still of legible quality
|
||||||
@@ -1760,13 +1994,14 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
})}
|
})}
|
||||||
style={{
|
style={{
|
||||||
transform: isVisible
|
transform: isVisible
|
||||||
? `translate(${x - this.state.offsetLeft}px, ${
|
? `translate(${x + renderPositionOffset.x * this.state.zoom.value - this.state.offsetLeft}px, ${
|
||||||
y - this.state.offsetTop
|
y + renderPositionOffset.y * this.state.zoom.value - this.state.offsetTop
|
||||||
}px) scale(${scale})`
|
}px) scale(${scale})`
|
||||||
: "none",
|
: "none",
|
||||||
display: isVisible ? "block" : "none",
|
display: isVisible ? "block" : "none",
|
||||||
opacity: getRenderOpacity(
|
opacity: getRenderOpacity(
|
||||||
el,
|
el,
|
||||||
|
this.getRenderOpacityConfig(),
|
||||||
getContainingFrame(el, this.scene.getNonDeletedElementsMap()),
|
getContainingFrame(el, this.scene.getNonDeletedElementsMap()),
|
||||||
this.elementsPendingErasure,
|
this.elementsPendingErasure,
|
||||||
null,
|
null,
|
||||||
@@ -2356,6 +2591,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
pendingFlowchartNodes:
|
pendingFlowchartNodes:
|
||||||
this.flowChartCreator.pendingNodes,
|
this.flowChartCreator.pendingNodes,
|
||||||
theme: this.state.theme,
|
theme: this.state.theme,
|
||||||
|
...this.getRenderOpacityConfig(),
|
||||||
|
renderAnimationVersion:
|
||||||
|
this.renderAnimationVersion,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{newElementCanvasElement && (
|
{newElementCanvasElement && (
|
||||||
@@ -2378,6 +2616,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.elementsPendingErasure,
|
this.elementsPendingErasure,
|
||||||
pendingFlowchartNodes: null,
|
pendingFlowchartNodes: null,
|
||||||
theme: this.state.theme,
|
theme: this.state.theme,
|
||||||
|
...this.getRenderOpacityConfig(),
|
||||||
|
renderAnimationVersion:
|
||||||
|
this.renderAnimationVersion,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@@ -3211,6 +3452,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.editorLifecycleEvents.emit("editor:unmount");
|
this.editorLifecycleEvents.emit("editor:unmount");
|
||||||
this.props.onUnmount?.();
|
this.props.onUnmount?.();
|
||||||
this.props.onExcalidrawAPI?.(null);
|
this.props.onExcalidrawAPI?.(null);
|
||||||
|
AnimationController.cancel(this.getElementAnimationKey());
|
||||||
|
|
||||||
(window as any).launchQueue?.setConsumer(() => {});
|
(window as any).launchQueue?.setConsumer(() => {});
|
||||||
|
|
||||||
@@ -4617,11 +4859,104 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (collaborators) {
|
if (collaborators) {
|
||||||
|
this.laserTrails.updateCollabTrails(collaborators);
|
||||||
this.setState({ collaborators });
|
this.setState({ collaborators });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public animateElements = ({
|
||||||
|
elements,
|
||||||
|
duration = 250,
|
||||||
|
delay = 0,
|
||||||
|
stagger = 0,
|
||||||
|
phase = "in",
|
||||||
|
easing,
|
||||||
|
...animation
|
||||||
|
}:
|
||||||
|
| {
|
||||||
|
elements: readonly (ExcalidrawElement | ExcalidrawElement["id"])[];
|
||||||
|
type: "fade";
|
||||||
|
duration?: number;
|
||||||
|
delay?: number;
|
||||||
|
stagger?: number;
|
||||||
|
phase?: "in" | "out";
|
||||||
|
easing?: "linear" | "easeOut" | "easeInOut";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
elements: readonly (ExcalidrawElement | ExcalidrawElement["id"])[];
|
||||||
|
type: "fly";
|
||||||
|
from: "left" | "right" | "top" | "bottom";
|
||||||
|
duration?: number;
|
||||||
|
delay?: number;
|
||||||
|
stagger?: number;
|
||||||
|
phase?: "in" | "out";
|
||||||
|
easing?: "linear" | "easeOut" | "easeInOut";
|
||||||
|
}) => {
|
||||||
|
const normalizedDelay = Math.max(delay, 0);
|
||||||
|
const normalizedStagger = Math.max(stagger, 0);
|
||||||
|
|
||||||
|
elements.forEach((elementOrId, index) => {
|
||||||
|
const id = typeof elementOrId === "string" ? elementOrId : elementOrId.id;
|
||||||
|
const element = this.scene.getNonDeletedElement(id);
|
||||||
|
|
||||||
|
if (!element) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animation.type === "fade") {
|
||||||
|
this.animateElement({
|
||||||
|
id,
|
||||||
|
opacityFrom:
|
||||||
|
phase === "in" ? 0 : this.getElementVisibleOpacity(element),
|
||||||
|
opacityTo:
|
||||||
|
phase === "in" ? this.getElementVisibleOpacity(element) : 0,
|
||||||
|
easing: easing ?? "easeInOut",
|
||||||
|
duration,
|
||||||
|
delay: normalizedDelay + index * normalizedStagger,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const flyOffset = this.getFlyPositionOffset(element, animation.from);
|
||||||
|
|
||||||
|
this.animateElement({
|
||||||
|
id,
|
||||||
|
opacityFrom: phase === "in" ? 0 : this.getElementVisibleOpacity(element),
|
||||||
|
opacityTo: phase === "in" ? this.getElementVisibleOpacity(element) : 0,
|
||||||
|
positionFrom: phase === "in" ? flyOffset : { x: 0, y: 0 },
|
||||||
|
positionTo: phase === "in" ? { x: 0, y: 0 } : flyOffset,
|
||||||
|
easing: easing ?? "easeOut",
|
||||||
|
duration,
|
||||||
|
delay: normalizedDelay + index * normalizedStagger,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
public cancelElementAnimation = (id: string) => {
|
||||||
|
if (!this.elementAnimationStates.delete(id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.syncElementAnimations();
|
||||||
|
};
|
||||||
|
|
||||||
|
public clearElementAnimationOverrides = () => {
|
||||||
|
if (
|
||||||
|
this.elementOpacityOverrides.size === 0 &&
|
||||||
|
this.elementPositionOverrides.size === 0 &&
|
||||||
|
this.elementAnimationStates.size === 0
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.elementAnimationStates.clear();
|
||||||
|
this.elementOpacityOverrides.clear();
|
||||||
|
this.elementPositionOverrides.clear();
|
||||||
|
this.syncElementAnimations();
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
};
|
||||||
|
|
||||||
public applyDeltas = (
|
public applyDeltas = (
|
||||||
deltas: StoreDelta[],
|
deltas: StoreDelta[],
|
||||||
options?: ApplyToOptions,
|
options?: ApplyToOptions,
|
||||||
@@ -11721,9 +12056,11 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
const existingFileData = this.files[fileId];
|
const existingFileData = this.files[fileId];
|
||||||
if (!existingFileData?.dataURL) {
|
if (!existingFileData?.dataURL) {
|
||||||
|
const { maxWidthOrHeight, maxFileSizeBytes } = this.props.imageOptions;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
imageFile = await resizeImageFile(imageFile, {
|
imageFile = await resizeImageFile(imageFile, {
|
||||||
maxWidthOrHeight: DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT,
|
maxWidthOrHeight,
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -11732,10 +12069,10 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imageFile.size > MAX_ALLOWED_FILE_BYTES) {
|
if (imageFile.size > maxFileSizeBytes) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
t("errors.fileTooBig", {
|
t("errors.fileTooBig", {
|
||||||
maxSize: `${Math.trunc(MAX_ALLOWED_FILE_BYTES / 1024 / 1024)}MB`,
|
maxSize: `${Math.trunc(maxFileSizeBytes / 1024 / 1024)}MB`,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useEffect, useRef } from "react";
|
|||||||
|
|
||||||
import "./SVGLayer.scss";
|
import "./SVGLayer.scss";
|
||||||
|
|
||||||
import type { Trail } from "../animated-trail";
|
import type { Trail } from "../animatedTrail";
|
||||||
|
|
||||||
type SVGLayerProps = {
|
type SVGLayerProps = {
|
||||||
trails: Trail[];
|
trails: Trail[];
|
||||||
|
|||||||
@@ -311,6 +311,48 @@ export const dataURLToString = (dataURL: DataURL) => {
|
|||||||
return base64ToString(dataURL.slice(dataURL.indexOf(",") + 1));
|
return base64ToString(dataURL.slice(dataURL.indexOf(",") + 1));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getImageFileDimensions = async (file: File) => {
|
||||||
|
const browserURL = typeof window !== "undefined" ? window.URL : undefined;
|
||||||
|
let objectURL: string | null = null;
|
||||||
|
let imageSource: string;
|
||||||
|
|
||||||
|
try {
|
||||||
|
imageSource = browserURL?.createObjectURL
|
||||||
|
? (objectURL = browserURL.createObjectURL(file))
|
||||||
|
: await getDataURL(file);
|
||||||
|
} catch {
|
||||||
|
objectURL = null;
|
||||||
|
imageSource = await getDataURL(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise<{ width: number; height: number }>((resolve, reject) => {
|
||||||
|
const image = new Image();
|
||||||
|
|
||||||
|
const cleanup = () => {
|
||||||
|
image.onload = null;
|
||||||
|
image.onerror = null;
|
||||||
|
|
||||||
|
if (objectURL && browserURL?.revokeObjectURL) {
|
||||||
|
browserURL.revokeObjectURL(objectURL);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
image.onload = () => {
|
||||||
|
cleanup();
|
||||||
|
resolve({
|
||||||
|
width: image.naturalWidth || image.width,
|
||||||
|
height: image.naturalHeight || image.height,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
image.onerror = (error) => {
|
||||||
|
cleanup();
|
||||||
|
reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
image.src = imageSource;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const resizeImageFile = async (
|
export const resizeImageFile = async (
|
||||||
file: File,
|
file: File,
|
||||||
opts: {
|
opts: {
|
||||||
@@ -324,6 +366,20 @@ export const resizeImageFile = async (
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isSupportedImageFile(file)) {
|
||||||
|
throw new Error("Error: unsupported file type", { cause: "UNSUPPORTED" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!opts.outputType || opts.outputType === file.type) {
|
||||||
|
const dimensions = await getImageFileDimensions(file);
|
||||||
|
|
||||||
|
if (
|
||||||
|
Math.max(dimensions.width, dimensions.height) <= opts.maxWidthOrHeight
|
||||||
|
) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [pica, imageBlobReduce] = await Promise.all([
|
const [pica, imageBlobReduce] = await Promise.all([
|
||||||
import("pica").then((res) => res.default),
|
import("pica").then((res) => res.default),
|
||||||
// a wrapper for pica for better API
|
// a wrapper for pica for better API
|
||||||
@@ -347,10 +403,6 @@ export const resizeImageFile = async (
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSupportedImageFile(file)) {
|
|
||||||
throw new Error("Error: unsupported file type", { cause: "UNSUPPORTED" });
|
|
||||||
}
|
|
||||||
|
|
||||||
return new File(
|
return new File(
|
||||||
[await reduce.toBlob(file, { max: opts.maxWidthOrHeight, alpha: true })],
|
[await reduce.toBlob(file, { max: opts.maxWidthOrHeight, alpha: true })],
|
||||||
file.name,
|
file.name,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { isFiniteNumber, pointFrom } from "@excalidraw/math";
|
import { isFiniteNumber, isValidPoint, pointFrom } from "@excalidraw/math";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
type CombineBrandsIfNeeded,
|
type CombineBrandsIfNeeded,
|
||||||
@@ -98,6 +98,67 @@ type RestoredAppState = Omit<
|
|||||||
|
|
||||||
const MAX_ARROW_PX = 75_000;
|
const MAX_ARROW_PX = 75_000;
|
||||||
|
|
||||||
|
const restoreLinearElementPoints = (
|
||||||
|
points: unknown,
|
||||||
|
width: unknown,
|
||||||
|
height: unknown,
|
||||||
|
): LocalPoint[] => {
|
||||||
|
const restoredPoints = Array.isArray(points)
|
||||||
|
? points.reduce<LocalPoint[]>((acc, point) => {
|
||||||
|
if (isValidPoint(point)) {
|
||||||
|
acc.push(pointFrom<LocalPoint>(point[0], point[1]));
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, [])
|
||||||
|
: [];
|
||||||
|
|
||||||
|
return restoredPoints.length < 2
|
||||||
|
? [
|
||||||
|
pointFrom<LocalPoint>(0, 0),
|
||||||
|
pointFrom<LocalPoint>(
|
||||||
|
isFiniteNumber(width) ? width : 0,
|
||||||
|
isFiniteNumber(height) ? height : 0,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
: restoredPoints;
|
||||||
|
};
|
||||||
|
|
||||||
|
const restoreFreedrawPoints = (
|
||||||
|
points: unknown,
|
||||||
|
pressures: unknown,
|
||||||
|
): {
|
||||||
|
points: LocalPoint[];
|
||||||
|
pressures: number[];
|
||||||
|
} => {
|
||||||
|
if (!Array.isArray(points)) {
|
||||||
|
return {
|
||||||
|
points: [],
|
||||||
|
pressures: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const pressureValues: readonly unknown[] = Array.isArray(pressures)
|
||||||
|
? pressures
|
||||||
|
: [];
|
||||||
|
const restoredPoints: LocalPoint[] = [];
|
||||||
|
const restoredPressures: number[] = [];
|
||||||
|
|
||||||
|
points.forEach((point, index) => {
|
||||||
|
if (isValidPoint(point)) {
|
||||||
|
restoredPoints.push(pointFrom<LocalPoint>(point[0], point[1]));
|
||||||
|
if (index in pressureValues) {
|
||||||
|
const pressure = pressureValues[index];
|
||||||
|
restoredPressures.push(isFiniteNumber(pressure) ? pressure : 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
points: restoredPoints,
|
||||||
|
pressures: restoredPressures,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const AllowedExcalidrawActiveTools: Record<
|
export const AllowedExcalidrawActiveTools: Record<
|
||||||
AppState["activeTool"]["type"],
|
AppState["activeTool"]["type"],
|
||||||
boolean
|
boolean
|
||||||
@@ -414,10 +475,15 @@ export const restoreElement = (
|
|||||||
|
|
||||||
return element;
|
return element;
|
||||||
case "freedraw": {
|
case "freedraw": {
|
||||||
|
const { points, pressures } = restoreFreedrawPoints(
|
||||||
|
element.points,
|
||||||
|
element.pressures,
|
||||||
|
);
|
||||||
|
|
||||||
return restoreElementWithProperties(element, {
|
return restoreElementWithProperties(element, {
|
||||||
points: element.points,
|
points,
|
||||||
simulatePressure: element.simulatePressure,
|
simulatePressure: element.simulatePressure,
|
||||||
pressures: element.pressures,
|
pressures,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
case "image":
|
case "image":
|
||||||
@@ -435,14 +501,20 @@ export const restoreElement = (
|
|||||||
const endArrowhead = normalizeArrowhead(element.endArrowhead);
|
const endArrowhead = normalizeArrowhead(element.endArrowhead);
|
||||||
let x = element.x;
|
let x = element.x;
|
||||||
let y = element.y;
|
let y = element.y;
|
||||||
let points = // migrate old arrow model to new one
|
let points = restoreLinearElementPoints(
|
||||||
!Array.isArray(element.points) || element.points.length < 2
|
element.points,
|
||||||
? [pointFrom(0, 0), pointFrom(element.width, element.height)]
|
element.width,
|
||||||
: element.points;
|
element.height,
|
||||||
|
);
|
||||||
|
|
||||||
if (points[0][0] !== 0 || points[0][1] !== 0) {
|
if (points[0][0] !== 0 || points[0][1] !== 0) {
|
||||||
({ points, x, y } =
|
({ points, x, y } =
|
||||||
LinearElementEditor.getNormalizeElementPointsAndCoords(element));
|
LinearElementEditor.getNormalizeElementPointsAndCoords({
|
||||||
|
...element,
|
||||||
|
points,
|
||||||
|
x: x ?? 0,
|
||||||
|
y: y ?? 0,
|
||||||
|
} as ExcalidrawLinearElement));
|
||||||
}
|
}
|
||||||
|
|
||||||
return restoreElementWithProperties(element, {
|
return restoreElementWithProperties(element, {
|
||||||
@@ -456,7 +528,7 @@ export const restoreElement = (
|
|||||||
y,
|
y,
|
||||||
...(isLineElement(element)
|
...(isLineElement(element)
|
||||||
? {
|
? {
|
||||||
polygon: isValidPolygon(element.points)
|
polygon: isValidPolygon(points)
|
||||||
? element.polygon ?? false
|
? element.polygon ?? false
|
||||||
: false,
|
: false,
|
||||||
}
|
}
|
||||||
@@ -471,22 +543,29 @@ export const restoreElement = (
|
|||||||
: normalizeArrowhead(element.endArrowhead);
|
: normalizeArrowhead(element.endArrowhead);
|
||||||
const x = element.x as number | undefined;
|
const x = element.x as number | undefined;
|
||||||
const y = element.y as number | undefined;
|
const y = element.y as number | undefined;
|
||||||
const points: readonly LocalPoint[] | undefined = // migrate old arrow model to new one
|
const points = restoreLinearElementPoints(
|
||||||
!Array.isArray(element.points) || element.points.length < 2
|
element.points,
|
||||||
? [pointFrom(0, 0), pointFrom(element.width, element.height)]
|
element.width,
|
||||||
: element.points;
|
element.height,
|
||||||
|
);
|
||||||
|
const elementWithRestoredPoints = {
|
||||||
|
...element,
|
||||||
|
points,
|
||||||
|
x: x ?? 0,
|
||||||
|
y: y ?? 0,
|
||||||
|
} as ExcalidrawArrowElement;
|
||||||
|
|
||||||
const base = {
|
const base = {
|
||||||
type: element.type,
|
type: element.type,
|
||||||
startBinding: repairBinding(
|
startBinding: repairBinding(
|
||||||
element as ExcalidrawArrowElement,
|
elementWithRestoredPoints,
|
||||||
element.startBinding,
|
element.startBinding,
|
||||||
targetElementsMap,
|
targetElementsMap,
|
||||||
existingElementsMap,
|
existingElementsMap,
|
||||||
"start",
|
"start",
|
||||||
),
|
),
|
||||||
endBinding: repairBinding(
|
endBinding: repairBinding(
|
||||||
element as ExcalidrawArrowElement,
|
elementWithRestoredPoints,
|
||||||
element.endBinding,
|
element.endBinding,
|
||||||
targetElementsMap,
|
targetElementsMap,
|
||||||
existingElementsMap,
|
existingElementsMap,
|
||||||
|
|||||||
@@ -33,9 +33,7 @@ import type { Bounds } from "@excalidraw/common";
|
|||||||
import type { GlobalPoint, LineSegment } from "@excalidraw/math/types";
|
import type { GlobalPoint, LineSegment } from "@excalidraw/math/types";
|
||||||
import type { ElementsMap, ExcalidrawElement } from "@excalidraw/element/types";
|
import type { ElementsMap, ExcalidrawElement } from "@excalidraw/element/types";
|
||||||
|
|
||||||
import { AnimatedTrail } from "../animated-trail";
|
import { AnimatedTrail } from "../animatedTrail";
|
||||||
|
|
||||||
import type { AnimationFrameHandler } from "../animation-frame-handler";
|
|
||||||
|
|
||||||
import type App from "../components/App";
|
import type App from "../components/App";
|
||||||
|
|
||||||
@@ -43,8 +41,8 @@ export class EraserTrail extends AnimatedTrail {
|
|||||||
private elementsToErase: Set<ExcalidrawElement["id"]> = new Set();
|
private elementsToErase: Set<ExcalidrawElement["id"]> = new Set();
|
||||||
private groupsToErase: Set<ExcalidrawElement["id"]> = new Set();
|
private groupsToErase: Set<ExcalidrawElement["id"]> = new Set();
|
||||||
|
|
||||||
constructor(animationFrameHandler: AnimationFrameHandler, app: App) {
|
constructor(app: App) {
|
||||||
super(animationFrameHandler, app, {
|
super(app, {
|
||||||
streamline: 0.2,
|
streamline: 0.2,
|
||||||
size: 5,
|
size: 5,
|
||||||
keepHead: true,
|
keepHead: true,
|
||||||
|
|||||||
@@ -6,7 +6,11 @@ import React, {
|
|||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
|
||||||
import { DEFAULT_UI_OPTIONS, isShallowEqual } from "@excalidraw/common";
|
import {
|
||||||
|
DEFAULT_IMAGE_OPTIONS,
|
||||||
|
DEFAULT_UI_OPTIONS,
|
||||||
|
isShallowEqual,
|
||||||
|
} from "@excalidraw/common";
|
||||||
|
|
||||||
import App, {
|
import App, {
|
||||||
ExcalidrawAPIContext,
|
ExcalidrawAPIContext,
|
||||||
@@ -95,9 +99,11 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
|
|||||||
children,
|
children,
|
||||||
validateEmbeddable,
|
validateEmbeddable,
|
||||||
renderEmbeddable,
|
renderEmbeddable,
|
||||||
|
resolveRenderOpacity,
|
||||||
aiEnabled,
|
aiEnabled,
|
||||||
showDeprecatedFonts,
|
showDeprecatedFonts,
|
||||||
renderScrollbars,
|
renderScrollbars,
|
||||||
|
imageOptions,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const canvasActions = props.UIOptions?.canvasActions;
|
const canvasActions = props.UIOptions?.canvasActions;
|
||||||
@@ -128,6 +134,13 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
|
|||||||
UIOptions.canvasActions.toggleTheme = true;
|
UIOptions.canvasActions.toggleTheme = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizedImageOptions: AppProps["imageOptions"] = {
|
||||||
|
maxFileSizeBytes:
|
||||||
|
imageOptions?.maxFileSizeBytes ?? DEFAULT_IMAGE_OPTIONS.maxFileSizeBytes,
|
||||||
|
maxWidthOrHeight:
|
||||||
|
imageOptions?.maxWidthOrHeight ?? DEFAULT_IMAGE_OPTIONS.maxWidthOrHeight,
|
||||||
|
};
|
||||||
|
|
||||||
const setExcalidrawAPI = useContext(ExcalidrawAPISetContext);
|
const setExcalidrawAPI = useContext(ExcalidrawAPISetContext);
|
||||||
|
|
||||||
const onExcalidrawAPIRef = useRef(onExcalidrawAPI);
|
const onExcalidrawAPIRef = useRef(onExcalidrawAPI);
|
||||||
@@ -205,9 +218,11 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
|
|||||||
onDuplicate={onDuplicate}
|
onDuplicate={onDuplicate}
|
||||||
validateEmbeddable={validateEmbeddable}
|
validateEmbeddable={validateEmbeddable}
|
||||||
renderEmbeddable={renderEmbeddable}
|
renderEmbeddable={renderEmbeddable}
|
||||||
|
resolveRenderOpacity={resolveRenderOpacity}
|
||||||
aiEnabled={aiEnabled !== false}
|
aiEnabled={aiEnabled !== false}
|
||||||
showDeprecatedFonts={showDeprecatedFonts}
|
showDeprecatedFonts={showDeprecatedFonts}
|
||||||
renderScrollbars={renderScrollbars}
|
renderScrollbars={renderScrollbars}
|
||||||
|
imageOptions={normalizedImageOptions}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</App>
|
</App>
|
||||||
@@ -225,11 +240,13 @@ const areEqual = (prevProps: ExcalidrawProps, nextProps: ExcalidrawProps) => {
|
|||||||
const {
|
const {
|
||||||
initialData: prevInitialData,
|
initialData: prevInitialData,
|
||||||
UIOptions: prevUIOptions = {},
|
UIOptions: prevUIOptions = {},
|
||||||
|
imageOptions: prevImageOptions,
|
||||||
...prev
|
...prev
|
||||||
} = prevProps;
|
} = prevProps;
|
||||||
const {
|
const {
|
||||||
initialData: nextInitialData,
|
initialData: nextInitialData,
|
||||||
UIOptions: nextUIOptions = {},
|
UIOptions: nextUIOptions = {},
|
||||||
|
imageOptions: nextImageOptions,
|
||||||
...next
|
...next
|
||||||
} = nextProps;
|
} = nextProps;
|
||||||
|
|
||||||
@@ -273,7 +290,17 @@ const areEqual = (prevProps: ExcalidrawProps, nextProps: ExcalidrawProps) => {
|
|||||||
return prevUIOptions[key] === nextUIOptions[key];
|
return prevUIOptions[key] === nextUIOptions[key];
|
||||||
});
|
});
|
||||||
|
|
||||||
return isUIOptionsSame && isShallowEqual(prev, next);
|
const isImageOptionsSame =
|
||||||
|
(prevImageOptions?.maxWidthOrHeight ??
|
||||||
|
DEFAULT_IMAGE_OPTIONS.maxWidthOrHeight) ===
|
||||||
|
(nextImageOptions?.maxWidthOrHeight ??
|
||||||
|
DEFAULT_IMAGE_OPTIONS.maxWidthOrHeight) &&
|
||||||
|
(prevImageOptions?.maxFileSizeBytes ??
|
||||||
|
DEFAULT_IMAGE_OPTIONS.maxFileSizeBytes) ===
|
||||||
|
(nextImageOptions?.maxFileSizeBytes ??
|
||||||
|
DEFAULT_IMAGE_OPTIONS.maxFileSizeBytes);
|
||||||
|
|
||||||
|
return isUIOptionsSame && isImageOptionsSame && isShallowEqual(prev, next);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Excalidraw = React.memo(ExcalidrawBase, areEqual);
|
export const Excalidraw = React.memo(ExcalidrawBase, areEqual);
|
||||||
|
|||||||
@@ -2,27 +2,20 @@ import { DEFAULT_LASER_COLOR, easeOut } from "@excalidraw/common";
|
|||||||
|
|
||||||
import type { LaserPointerOptions } from "@excalidraw/laser-pointer";
|
import type { LaserPointerOptions } from "@excalidraw/laser-pointer";
|
||||||
|
|
||||||
import { AnimatedTrail } from "./animated-trail";
|
import { AnimatedTrail } from "./animatedTrail";
|
||||||
import { getClientColor } from "./clients";
|
import { getClientColor } from "./clients";
|
||||||
|
|
||||||
import type { Trail } from "./animated-trail";
|
import type { Trail } from "./animatedTrail";
|
||||||
import type { AnimationFrameHandler } from "./animation-frame-handler";
|
|
||||||
import type App from "./components/App";
|
import type App from "./components/App";
|
||||||
import type { SocketId } from "./types";
|
import type { SocketId } from "./types";
|
||||||
|
|
||||||
export class LaserTrails implements Trail {
|
export class LaserTrails implements Trail {
|
||||||
public localTrail: AnimatedTrail;
|
public localTrail: AnimatedTrail;
|
||||||
private collabTrails = new Map<SocketId, AnimatedTrail>();
|
private collabTrails = new Map<SocketId, AnimatedTrail>();
|
||||||
|
|
||||||
private container?: SVGSVGElement;
|
private container?: SVGSVGElement;
|
||||||
|
|
||||||
constructor(
|
constructor(private app: App) {
|
||||||
private animationFrameHandler: AnimationFrameHandler,
|
this.localTrail = new AnimatedTrail(app, {
|
||||||
private app: App,
|
|
||||||
) {
|
|
||||||
this.animationFrameHandler.register(this, this.onFrame.bind(this));
|
|
||||||
|
|
||||||
this.localTrail = new AnimatedTrail(animationFrameHandler, app, {
|
|
||||||
...this.getTrailOptions(),
|
...this.getTrailOptions(),
|
||||||
fill: () => DEFAULT_LASER_COLOR,
|
fill: () => DEFAULT_LASER_COLOR,
|
||||||
});
|
});
|
||||||
@@ -63,30 +56,45 @@ export class LaserTrails implements Trail {
|
|||||||
|
|
||||||
start(container: SVGSVGElement) {
|
start(container: SVGSVGElement) {
|
||||||
this.container = container;
|
this.container = container;
|
||||||
|
|
||||||
this.animationFrameHandler.start(this);
|
|
||||||
this.localTrail.start(container);
|
this.localTrail.start(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.animationFrameHandler.stop(this);
|
|
||||||
this.localTrail.stop();
|
this.localTrail.stop();
|
||||||
|
this.stopCollabTrails();
|
||||||
|
this.container = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
onFrame() {
|
private stopCollabTrails(collaborators?: App["state"]["collaborators"]) {
|
||||||
this.updateCollabTrails();
|
for (const [key, trail] of this.collabTrails) {
|
||||||
|
const collaborator = collaborators?.get(key);
|
||||||
|
|
||||||
|
if (!collaborator) {
|
||||||
|
trail.stop();
|
||||||
|
this.collabTrails.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateCollabTrails() {
|
updateCollabTrails(collaborators: App["state"]["collaborators"]) {
|
||||||
if (!this.container || this.app.state.collaborators.size === 0) {
|
this.stopCollabTrails(collaborators);
|
||||||
|
|
||||||
|
if (!this.container || collaborators.size === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [key, collaborator] of this.app.state.collaborators.entries()) {
|
for (const [key, collaborator] of collaborators.entries()) {
|
||||||
let trail!: AnimatedTrail;
|
// Current user has their own trail drawn via localTrail
|
||||||
|
if (collaborator.isCurrentUser) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.collabTrails.has(key)) {
|
// IDEA: Use the collaborator pointer coordinates to trace out the
|
||||||
trail = new AnimatedTrail(this.animationFrameHandler, this.app, {
|
// laser pointer trail when 1) the selected collab tool is the laser
|
||||||
|
// pointer and 2) the collab pointer button is in the "down" state.
|
||||||
|
let trail = this.collabTrails.get(key);
|
||||||
|
if (!trail) {
|
||||||
|
trail = new AnimatedTrail(this.app, {
|
||||||
...this.getTrailOptions(),
|
...this.getTrailOptions(),
|
||||||
fill: () =>
|
fill: () =>
|
||||||
collaborator.pointer?.laserColor ||
|
collaborator.pointer?.laserColor ||
|
||||||
@@ -95,36 +103,33 @@ export class LaserTrails implements Trail {
|
|||||||
trail.start(this.container);
|
trail.start(this.container);
|
||||||
|
|
||||||
this.collabTrails.set(key, trail);
|
this.collabTrails.set(key, trail);
|
||||||
} else {
|
|
||||||
trail = this.collabTrails.get(key)!;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collaborator.pointer && collaborator.pointer.tool === "laser") {
|
if (collaborator.pointer && collaborator.pointer.tool === "laser") {
|
||||||
if (collaborator.button === "down" && !trail.hasCurrentTrail) {
|
const buttonDown = collaborator.button === "down";
|
||||||
|
const buttonUp = collaborator.button === "up";
|
||||||
|
const hasTrail = trail.hasCurrentTrail;
|
||||||
|
|
||||||
|
// Initialize a new trail
|
||||||
|
if (buttonDown && !hasTrail) {
|
||||||
trail.startPath(collaborator.pointer.x, collaborator.pointer.y);
|
trail.startPath(collaborator.pointer.x, collaborator.pointer.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
// Add only original points
|
||||||
collaborator.button === "down" &&
|
const lastPointOriginal = !trail.hasLastPoint(
|
||||||
trail.hasCurrentTrail &&
|
collaborator.pointer.x,
|
||||||
!trail.hasLastPoint(collaborator.pointer.x, collaborator.pointer.y)
|
collaborator.pointer.y,
|
||||||
) {
|
);
|
||||||
|
if (buttonDown && lastPointOriginal) {
|
||||||
trail.addPointToPath(collaborator.pointer.x, collaborator.pointer.y);
|
trail.addPointToPath(collaborator.pointer.x, collaborator.pointer.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collaborator.button === "up" && trail.hasCurrentTrail) {
|
// End the trail on button up
|
||||||
|
if (buttonUp && hasTrail) {
|
||||||
trail.addPointToPath(collaborator.pointer.x, collaborator.pointer.y);
|
trail.addPointToPath(collaborator.pointer.x, collaborator.pointer.y);
|
||||||
trail.endPath();
|
trail.endPath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key of this.collabTrails.keys()) {
|
|
||||||
if (!this.app.state.collaborators.has(key)) {
|
|
||||||
const trail = this.collabTrails.get(key)!;
|
|
||||||
trail.stop();
|
|
||||||
this.collabTrails.delete(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,9 +25,7 @@ import type {
|
|||||||
NonDeleted,
|
NonDeleted,
|
||||||
} from "@excalidraw/element/types";
|
} from "@excalidraw/element/types";
|
||||||
|
|
||||||
import { type AnimationFrameHandler } from "../animation-frame-handler";
|
import { AnimatedTrail } from "../animatedTrail";
|
||||||
|
|
||||||
import { AnimatedTrail } from "../animated-trail";
|
|
||||||
|
|
||||||
import { getLassoSelectedElementIds } from "./utils";
|
import { getLassoSelectedElementIds } from "./utils";
|
||||||
|
|
||||||
@@ -47,8 +45,8 @@ export class LassoTrail extends AnimatedTrail {
|
|||||||
private canvasTranslate: CanvasTranslate | null = null;
|
private canvasTranslate: CanvasTranslate | null = null;
|
||||||
private keepPreviousSelection: boolean = false;
|
private keepPreviousSelection: boolean = false;
|
||||||
|
|
||||||
constructor(animationFrameHandler: AnimationFrameHandler, app: App) {
|
constructor(app: App) {
|
||||||
super(animationFrameHandler, app, {
|
super(app, {
|
||||||
animateTrail: true,
|
animateTrail: true,
|
||||||
streamline: 0.4,
|
streamline: 0.4,
|
||||||
sizeMapping: (c) => {
|
sizeMapping: (c) => {
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ export type Animation<R extends object> = (params: {
|
|||||||
}) => R | null | undefined;
|
}) => R | null | undefined;
|
||||||
|
|
||||||
export class AnimationController {
|
export class AnimationController {
|
||||||
private static isRunning = false;
|
private static scheduledFrame:
|
||||||
|
| { id: ReturnType<typeof requestAnimationFrame>; type: "raf" }
|
||||||
|
| { id: ReturnType<typeof setTimeout>; type: "timeout" }
|
||||||
|
| null = null;
|
||||||
private static animations = new Map<
|
private static animations = new Map<
|
||||||
string,
|
string,
|
||||||
{
|
{
|
||||||
@@ -17,6 +20,10 @@ export class AnimationController {
|
|||||||
>();
|
>();
|
||||||
|
|
||||||
static start<R extends object>(key: string, animation: Animation<R>) {
|
static start<R extends object>(key: string, animation: Animation<R>) {
|
||||||
|
if (AnimationController.animations.has(key)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const initialState = animation({
|
const initialState = animation({
|
||||||
deltaTime: 0,
|
deltaTime: 0,
|
||||||
state: undefined,
|
state: undefined,
|
||||||
@@ -29,19 +36,54 @@ export class AnimationController {
|
|||||||
state: initialState,
|
state: initialState,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!AnimationController.isRunning) {
|
AnimationController.scheduleNextFrame();
|
||||||
AnimationController.isRunning = true;
|
|
||||||
|
|
||||||
if (isRenderThrottlingEnabled()) {
|
|
||||||
requestAnimationFrame(AnimationController.tick);
|
|
||||||
} else {
|
|
||||||
setTimeout(AnimationController.tick, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static scheduleNextFrame() {
|
||||||
|
if (AnimationController.scheduledFrame) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isRenderThrottlingEnabled()) {
|
||||||
|
AnimationController.scheduledFrame = {
|
||||||
|
id: requestAnimationFrame(AnimationController.tick),
|
||||||
|
type: "raf",
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
AnimationController.scheduledFrame = {
|
||||||
|
id: setTimeout(AnimationController.tick, 0),
|
||||||
|
type: "timeout",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static cancelScheduledFrame() {
|
||||||
|
if (!AnimationController.scheduledFrame) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (AnimationController.scheduledFrame.type === "raf") {
|
||||||
|
cancelAnimationFrame(AnimationController.scheduledFrame.id);
|
||||||
|
} else {
|
||||||
|
clearTimeout(AnimationController.scheduledFrame.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
AnimationController.scheduledFrame = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static cancelScheduledFrameIfIdle() {
|
||||||
|
if (AnimationController.animations.size > 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AnimationController.cancelScheduledFrame();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private static tick() {
|
private static tick() {
|
||||||
|
AnimationController.scheduledFrame = null;
|
||||||
|
|
||||||
if (AnimationController.animations.size > 0) {
|
if (AnimationController.animations.size > 0) {
|
||||||
for (const [key, animation] of AnimationController.animations) {
|
for (const [key, animation] of AnimationController.animations) {
|
||||||
const now = performance.now();
|
const now = performance.now();
|
||||||
@@ -56,8 +98,7 @@ export class AnimationController {
|
|||||||
if (!state) {
|
if (!state) {
|
||||||
AnimationController.animations.delete(key);
|
AnimationController.animations.delete(key);
|
||||||
|
|
||||||
if (AnimationController.animations.size === 0) {
|
if (AnimationController.cancelScheduledFrameIfIdle()) {
|
||||||
AnimationController.isRunning = false;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -66,11 +107,11 @@ export class AnimationController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isRenderThrottlingEnabled()) {
|
if (AnimationController.cancelScheduledFrameIfIdle()) {
|
||||||
requestAnimationFrame(AnimationController.tick);
|
return;
|
||||||
} else {
|
|
||||||
setTimeout(AnimationController.tick, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AnimationController.scheduleNextFrame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,5 +121,6 @@ export class AnimationController {
|
|||||||
|
|
||||||
static cancel(key: string) {
|
static cancel(key: string) {
|
||||||
AnimationController.animations.delete(key);
|
AnimationController.animations.delete(key);
|
||||||
|
AnimationController.cancelScheduledFrameIfIdle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,11 +14,16 @@ import {
|
|||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
import {
|
import {
|
||||||
elementOverlapsWithFrame,
|
elementOverlapsWithFrame,
|
||||||
|
getContainingFrame,
|
||||||
getTargetFrame,
|
getTargetFrame,
|
||||||
shouldApplyFrameClip,
|
shouldApplyFrameClip,
|
||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
import { renderElement } from "@excalidraw/element";
|
import {
|
||||||
|
getRenderElementWithPositionOverride,
|
||||||
|
getRenderOpacity,
|
||||||
|
renderElement,
|
||||||
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
import { getElementAbsoluteCoords } from "@excalidraw/element";
|
import { getElementAbsoluteCoords } from "@excalidraw/element";
|
||||||
|
|
||||||
@@ -170,7 +175,10 @@ const renderLinkIcon = (
|
|||||||
context: CanvasRenderingContext2D,
|
context: CanvasRenderingContext2D,
|
||||||
appState: StaticCanvasAppState,
|
appState: StaticCanvasAppState,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
|
renderConfig: StaticCanvasRenderConfig,
|
||||||
) => {
|
) => {
|
||||||
|
element = getRenderElementWithPositionOverride(element, renderConfig);
|
||||||
|
|
||||||
if (element.link && !appState.selectedElementIds[element.id]) {
|
if (element.link && !appState.selectedElementIds[element.id]) {
|
||||||
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
||||||
const [x, y, width, height] = getLinkHandleFromCoords(
|
const [x, y, width, height] = getLinkHandleFromCoords(
|
||||||
@@ -221,7 +229,13 @@ const renderLinkIcon = (
|
|||||||
|
|
||||||
linkCanvasCacheContext.restore();
|
linkCanvasCacheContext.restore();
|
||||||
}
|
}
|
||||||
context.globalAlpha = element.opacity / 100;
|
context.globalAlpha = getRenderOpacity(
|
||||||
|
element,
|
||||||
|
renderConfig,
|
||||||
|
getContainingFrame(element, elementsMap),
|
||||||
|
renderConfig.elementsPendingErasure,
|
||||||
|
renderConfig.pendingFlowchartNodes,
|
||||||
|
);
|
||||||
context.drawImage(linkCanvas, x - centerX, y - centerY, width, height);
|
context.drawImage(linkCanvas, x - centerX, y - centerY, width, height);
|
||||||
context.restore();
|
context.restore();
|
||||||
}
|
}
|
||||||
@@ -370,7 +384,7 @@ const _renderStaticScene = ({
|
|||||||
context.restore();
|
context.restore();
|
||||||
|
|
||||||
if (!isExporting) {
|
if (!isExporting) {
|
||||||
renderLinkIcon(element, context, appState, elementsMap);
|
renderLinkIcon(element, context, appState, elementsMap, renderConfig);
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -421,7 +435,7 @@ const _renderStaticScene = ({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (!isExporting) {
|
if (!isExporting) {
|
||||||
renderLinkIcon(element, context, appState, elementsMap);
|
renderLinkIcon(element, context, appState, elementsMap, renderConfig);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// - when exporting the whole canvas, we DO NOT apply clipping
|
// - when exporting the whole canvas, we DO NOT apply clipping
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import type {
|
|||||||
AppClassProperties,
|
AppClassProperties,
|
||||||
AppState,
|
AppState,
|
||||||
EmbedsValidationStatus,
|
EmbedsValidationStatus,
|
||||||
|
RenderOpacityResolver,
|
||||||
ElementsPendingErasure,
|
ElementsPendingErasure,
|
||||||
InteractiveCanvasAppState,
|
InteractiveCanvasAppState,
|
||||||
StaticCanvasAppState,
|
StaticCanvasAppState,
|
||||||
@@ -37,6 +38,13 @@ export type StaticCanvasRenderConfig = {
|
|||||||
elementsPendingErasure: ElementsPendingErasure;
|
elementsPendingErasure: ElementsPendingErasure;
|
||||||
pendingFlowchartNodes: PendingExcalidrawElements | null;
|
pendingFlowchartNodes: PendingExcalidrawElements | null;
|
||||||
theme: AppState["theme"];
|
theme: AppState["theme"];
|
||||||
|
resolveRenderOpacity?: RenderOpacityResolver;
|
||||||
|
elementOpacityOverrides?: ReadonlyMap<ExcalidrawElement["id"], number>;
|
||||||
|
elementPositionOverrides?: ReadonlyMap<
|
||||||
|
ExcalidrawElement["id"],
|
||||||
|
{ x: number; y: number }
|
||||||
|
>;
|
||||||
|
renderAnimationVersion?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SVGRenderConfig = {
|
export type SVGRenderConfig = {
|
||||||
|
|||||||
@@ -83,6 +83,26 @@ mockMermaidToExcalidraw({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const normalizeDialogSnapshot = (dialog: Element) => {
|
||||||
|
const dialogClone = dialog.cloneNode(true) as HTMLElement;
|
||||||
|
|
||||||
|
dialogClone
|
||||||
|
.querySelectorAll<HTMLElement>(".ttd-dialog-content")
|
||||||
|
.forEach((element) => {
|
||||||
|
// Radix Tabs injects this during initial mount animation prevention.
|
||||||
|
// Its presence depends on render timing and is unrelated to this test.
|
||||||
|
if (element.style.animationDuration === "0s") {
|
||||||
|
element.style.removeProperty("animation-duration");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!element.getAttribute("style")) {
|
||||||
|
element.removeAttribute("style");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return dialogClone.outerHTML;
|
||||||
|
};
|
||||||
|
|
||||||
describe("Test <MermaidToExcalidraw/>", () => {
|
describe("Test <MermaidToExcalidraw/>", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await render(
|
await render(
|
||||||
@@ -99,7 +119,7 @@ describe("Test <MermaidToExcalidraw/>", () => {
|
|||||||
it("should open mermaid popup when active tool is mermaid", async () => {
|
it("should open mermaid popup when active tool is mermaid", async () => {
|
||||||
const dialog = document.querySelector(".ttd-dialog")!;
|
const dialog = document.querySelector(".ttd-dialog")!;
|
||||||
await waitFor(() => expect(dialog.querySelector("canvas")).not.toBeNull());
|
await waitFor(() => expect(dialog.querySelector("canvas")).not.toBeNull());
|
||||||
expect(dialog.outerHTML).toMatchSnapshot();
|
expect(normalizeDialogSnapshot(dialog)).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should show error in preview when mermaid library throws error", async () => {
|
it("should show error in preview when mermaid library throws error", async () => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||||
|
|
||||||
exports[`Test <MermaidToExcalidraw/> > should open mermaid popup when active tool is mermaid 1`] = `"<div class="Modal Dialog ttd-dialog" role="dialog" aria-modal="true" aria-labelledby="dialog-title"><div class="Modal__background"></div><div class="Modal__content" style="--max-width: 1520px;" tabindex="0"><div class="Island"><div class="Dialog__content"><div dir="ltr" data-orientation="horizontal" class="ttd-dialog-tabs-root"><p class="dialog-mermaid-title">Mermaid to Excalidraw</p><div data-state="active" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:r4:-trigger-mermaid" id="radix-:r4:-content-mermaid" tabindex="0" class="ttd-dialog-content" style=""><div class="ttd-dialog-desc">Currently only <a href="https://mermaid.js.org/syntax/flowchart.html" target="_blank" rel="noreferrer">Flowchart</a>, <a href="https://mermaid.js.org/syntax/sequenceDiagram.html" target="_blank" rel="noreferrer">Sequence</a>, <a href="https://mermaid.js.org/syntax/classDiagram.html" target="_blank" rel="noreferrer">Class</a>, and <a href="https://mermaid.js.org/syntax/entityRelationshipDiagram.html" target="_blank" rel="noreferrer">Entity Relationship</a> Diagrams are supported. The other types will be rendered as image in Excalidraw.</div><div class="ttd-dialog-panels"><div class="ttd-dialog-panel"><div class="ttd-dialog-panel-button-container invisible" style="justify-content: flex-start;"></div></div><div class="ttd-dialog-panel"><div class="ttd-dialog-output-wrapper "><div class="ttd-dialog-output-canvas-container"><div class="ttd-dialog-output-canvas-content"><canvas width="89" height="158" dir="ltr"></canvas></div></div></div><div class="ttd-dialog-panel-button-container" style="justify-content: flex-start;"><button type="button" class="excalidraw-button ttd-dialog-panel-button"><div class="">Insert<span><svg aria-hidden="true" focusable="false" role="img" viewBox="0 0 20 20" class="" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><g stroke-width="1.25"><path d="M4.16602 10H15.8327"></path><path d="M12.5 13.3333L15.8333 10"></path><path d="M12.5 6.66666L15.8333 9.99999"></path></g></svg></span></div></button><div class="ttd-dialog-submit-shortcut"><div class="ttd-dialog-submit-shortcut__key">Ctrl</div><div class="ttd-dialog-submit-shortcut__key">Enter</div></div></div></div></div></div></div></div></div></div></div>"`;
|
exports[`Test <MermaidToExcalidraw/> > should open mermaid popup when active tool is mermaid 1`] = `"<div class="Modal Dialog ttd-dialog" role="dialog" aria-modal="true" aria-labelledby="dialog-title"><div class="Modal__background"></div><div class="Modal__content" style="--max-width: 1520px;" tabindex="0"><div class="Island"><div class="Dialog__content"><div dir="ltr" data-orientation="horizontal" class="ttd-dialog-tabs-root"><p class="dialog-mermaid-title">Mermaid to Excalidraw</p><div data-state="active" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:r4:-trigger-mermaid" id="radix-:r4:-content-mermaid" tabindex="0" class="ttd-dialog-content"><div class="ttd-dialog-desc">Currently only <a href="https://mermaid.js.org/syntax/flowchart.html" target="_blank" rel="noreferrer">Flowchart</a>, <a href="https://mermaid.js.org/syntax/sequenceDiagram.html" target="_blank" rel="noreferrer">Sequence</a>, <a href="https://mermaid.js.org/syntax/classDiagram.html" target="_blank" rel="noreferrer">Class</a>, and <a href="https://mermaid.js.org/syntax/entityRelationshipDiagram.html" target="_blank" rel="noreferrer">Entity Relationship</a> Diagrams are supported. The other types will be rendered as image in Excalidraw.</div><div class="ttd-dialog-panels"><div class="ttd-dialog-panel"><div class="ttd-dialog-panel-button-container invisible" style="justify-content: flex-start;"></div></div><div class="ttd-dialog-panel"><div class="ttd-dialog-output-wrapper "><div class="ttd-dialog-output-canvas-container"><div class="ttd-dialog-output-canvas-content"><canvas width="89" height="158" dir="ltr"></canvas></div></div></div><div class="ttd-dialog-panel-button-container" style="justify-content: flex-start;"><button type="button" class="excalidraw-button ttd-dialog-panel-button"><div class="">Insert<span><svg aria-hidden="true" focusable="false" role="img" viewBox="0 0 20 20" class="" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><g stroke-width="1.25"><path d="M4.16602 10H15.8327"></path><path d="M12.5 13.3333L15.8333 10"></path><path d="M12.5 6.66666L15.8333 9.99999"></path></g></svg></span></div></button><div class="ttd-dialog-submit-shortcut"><div class="ttd-dialog-submit-shortcut__key">Ctrl</div><div class="ttd-dialog-submit-shortcut__key">Enter</div></div></div></div></div></div></div></div></div></div></div>"`;
|
||||||
|
|
||||||
exports[`Test <MermaidToExcalidraw/> > should show error in preview when mermaid library throws error 1`] = `
|
exports[`Test <MermaidToExcalidraw/> > should show error in preview when mermaid library throws error 1`] = `
|
||||||
"flowchart TD
|
"flowchart TD
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { AnimationController } from "../renderer/animation";
|
||||||
|
|
||||||
|
const FIRST_KEY = "animation-test-first";
|
||||||
|
const SECOND_KEY = "animation-test-second";
|
||||||
|
|
||||||
|
describe("AnimationController", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
window.EXCALIDRAW_THROTTLE_RENDER = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
AnimationController.cancel(FIRST_KEY);
|
||||||
|
AnimationController.cancel(SECOND_KEY);
|
||||||
|
window.EXCALIDRAW_THROTTLE_RENDER = undefined;
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("starts a new animation after the previous last animation was cancelled", async () => {
|
||||||
|
let firstFrames = 0;
|
||||||
|
AnimationController.start(FIRST_KEY, () => {
|
||||||
|
firstFrames++;
|
||||||
|
return { keep: true };
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(firstFrames).toBe(1);
|
||||||
|
|
||||||
|
AnimationController.cancel(FIRST_KEY);
|
||||||
|
await vi.runOnlyPendingTimersAsync();
|
||||||
|
|
||||||
|
let secondFrames = 0;
|
||||||
|
AnimationController.start(SECOND_KEY, () => {
|
||||||
|
secondFrames++;
|
||||||
|
return secondFrames === 1 ? { keep: true } : null;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(secondFrames).toBe(1);
|
||||||
|
|
||||||
|
await vi.runOnlyPendingTimersAsync();
|
||||||
|
|
||||||
|
expect(secondFrames).toBe(2);
|
||||||
|
expect(AnimationController.running(SECOND_KEY)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cancels a frame scheduled during a tick if no animations remain", async () => {
|
||||||
|
let firstFrames = 0;
|
||||||
|
let secondFrames = 0;
|
||||||
|
|
||||||
|
AnimationController.start(FIRST_KEY, ({ state }) => {
|
||||||
|
if (!state) {
|
||||||
|
return { keep: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
firstFrames++;
|
||||||
|
|
||||||
|
AnimationController.start(SECOND_KEY, () => {
|
||||||
|
secondFrames++;
|
||||||
|
return { keep: true };
|
||||||
|
});
|
||||||
|
AnimationController.cancel(SECOND_KEY);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
await vi.runOnlyPendingTimersAsync();
|
||||||
|
|
||||||
|
expect(firstFrames).toBe(1);
|
||||||
|
expect(secondFrames).toBe(1);
|
||||||
|
expect(vi.getTimerCount()).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -160,6 +160,39 @@ describe("restoreElements", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should restore only valid freedraw points and keep pressures aligned", () => {
|
||||||
|
const freedrawElement = API.createElement({
|
||||||
|
type: "freedraw",
|
||||||
|
id: "id-freedraw-invalid-points",
|
||||||
|
points: [pointFrom(0, 0), pointFrom(10, 10)],
|
||||||
|
});
|
||||||
|
|
||||||
|
const restoredFreedraw = restore.restoreElements(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
...freedrawElement,
|
||||||
|
simulatePressure: false,
|
||||||
|
points: [
|
||||||
|
pointFrom(0, 0),
|
||||||
|
[Infinity, 10],
|
||||||
|
null,
|
||||||
|
pointFrom(20, 20),
|
||||||
|
[NaN, 30],
|
||||||
|
[40, null],
|
||||||
|
],
|
||||||
|
pressures: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
|
||||||
|
} as any,
|
||||||
|
],
|
||||||
|
null,
|
||||||
|
)[0] as ExcalidrawFreeDrawElement;
|
||||||
|
|
||||||
|
expect(restoredFreedraw.points).toEqual([
|
||||||
|
pointFrom(0, 0),
|
||||||
|
pointFrom(20, 20),
|
||||||
|
]);
|
||||||
|
expect(restoredFreedraw.pressures).toEqual([0.1, 0.4]);
|
||||||
|
});
|
||||||
|
|
||||||
it("should restore line and draw elements correctly", () => {
|
it("should restore line and draw elements correctly", () => {
|
||||||
const lineElement = API.createElement({ type: "line", id: "id-line01" });
|
const lineElement = API.createElement({ type: "line", id: "id-line01" });
|
||||||
|
|
||||||
@@ -400,6 +433,52 @@ describe("restoreElements", () => {
|
|||||||
expect(restoredLine.points).toMatchObject(expectedLinePoints);
|
expect(restoredLine.points).toMatchObject(expectedLinePoints);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should restore only valid linear points", () => {
|
||||||
|
const lineElement: any = API.createElement({
|
||||||
|
type: "line",
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
width: 100,
|
||||||
|
height: 200,
|
||||||
|
});
|
||||||
|
const arrowElement: any = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
|
width: 100,
|
||||||
|
height: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
lineElement.points = [
|
||||||
|
[2, 3],
|
||||||
|
null,
|
||||||
|
[Infinity, 4],
|
||||||
|
[5, 7],
|
||||||
|
[NaN, 8],
|
||||||
|
[9, null],
|
||||||
|
];
|
||||||
|
arrowElement.points = [
|
||||||
|
[null, 0],
|
||||||
|
[Infinity, 4],
|
||||||
|
];
|
||||||
|
|
||||||
|
const restoredElements = restore.restoreElements(
|
||||||
|
[lineElement, arrowElement],
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
const restoredLine = restoredElements[0] as ExcalidrawLinearElement;
|
||||||
|
const restoredArrow = restoredElements[1] as ExcalidrawArrowElement;
|
||||||
|
|
||||||
|
expect(restoredLine.points).toEqual([pointFrom(0, 0), pointFrom(3, 4)]);
|
||||||
|
expect(restoredLine.x).toBe(12);
|
||||||
|
expect(restoredLine.y).toBe(23);
|
||||||
|
expect(restoredLine.width).toBe(3);
|
||||||
|
expect(restoredLine.height).toBe(4);
|
||||||
|
|
||||||
|
expect(restoredArrow.points).toEqual([
|
||||||
|
pointFrom(0, 0),
|
||||||
|
pointFrom(100, 200),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it("when the number of points of a line is greater or equal 2", () => {
|
it("when the number of points of a line is greater or equal 2", () => {
|
||||||
const lineElement_0 = API.createElement({
|
const lineElement_0 = API.createElement({
|
||||||
type: "line",
|
type: "line",
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { randomId, reseed } from "@excalidraw/common";
|
import { MIME_TYPES, randomId, reseed } from "@excalidraw/common";
|
||||||
|
|
||||||
import type { FileId } from "@excalidraw/element/types";
|
import type { FileId } from "@excalidraw/element/types";
|
||||||
|
|
||||||
@@ -17,18 +17,41 @@ import {
|
|||||||
} from "./fixtures/constants";
|
} from "./fixtures/constants";
|
||||||
import { INITIALIZED_IMAGE_PROPS } from "./helpers/constants";
|
import { INITIALIZED_IMAGE_PROPS } from "./helpers/constants";
|
||||||
|
|
||||||
|
import type { ExcalidrawProps } from "../types";
|
||||||
|
|
||||||
const { h } = window;
|
const { h } = window;
|
||||||
|
|
||||||
export const setupImageTest = async (
|
export const setupImageTest = async (
|
||||||
sizes: { width: number; height: number }[],
|
sizes: { width: number; height: number }[],
|
||||||
|
props?: ExcalidrawProps,
|
||||||
) => {
|
) => {
|
||||||
await render(<Excalidraw autoFocus={true} handleKeyboardGlobally={true} />);
|
await render(
|
||||||
|
<Excalidraw autoFocus={true} handleKeyboardGlobally={true} {...props} />,
|
||||||
|
);
|
||||||
|
|
||||||
h.state.height = 1000;
|
h.state.height = 1000;
|
||||||
|
|
||||||
mockMultipleHTMLImageElements(sizes.map((size) => [size.width, size.height]));
|
mockMultipleHTMLImageElements(sizes.map((size) => [size.width, size.height]));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
describe("resizeImageFile", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns the original file when it already fits the max dimensions", async () => {
|
||||||
|
mockMultipleHTMLImageElements([[100, 100]]);
|
||||||
|
|
||||||
|
const imageFile = new File([new Uint8Array([1, 2, 3])], "image.png", {
|
||||||
|
type: MIME_TYPES.png,
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
blobModule.resizeImageFile(imageFile, { maxWidthOrHeight: 200 }),
|
||||||
|
).resolves.toBe(imageFile);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("image insertion", () => {
|
describe("image insertion", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
@@ -112,4 +135,42 @@ describe("image insertion", () => {
|
|||||||
|
|
||||||
await assert();
|
await assert();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("passes host-configured max image dimensions to the resize helper", async () => {
|
||||||
|
await setupImageTest([DEER_IMAGE_DIMENSIONS], {
|
||||||
|
imageOptions: { maxWidthOrHeight: 2048 },
|
||||||
|
});
|
||||||
|
|
||||||
|
await API.drop([
|
||||||
|
{ kind: "file", file: await API.loadFile("./fixtures/deer.png") },
|
||||||
|
]);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(blobModule.resizeImageFile).toHaveBeenCalledWith(
|
||||||
|
expect.any(File),
|
||||||
|
{ maxWidthOrHeight: 2048 },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("enforces host-configured max image file size", async () => {
|
||||||
|
await setupImageTest([DEER_IMAGE_DIMENSIONS], {
|
||||||
|
imageOptions: { maxFileSizeBytes: 1024 * 1024 },
|
||||||
|
});
|
||||||
|
|
||||||
|
await API.drop([
|
||||||
|
{
|
||||||
|
kind: "file",
|
||||||
|
file: new File([new Uint8Array(2 * 1024 * 1024)], "image.png", {
|
||||||
|
type: MIME_TYPES.png,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(h.state.errorMessage).toBe(
|
||||||
|
"File is too big. Maximum allowed size is 1MB.",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { API } from "./helpers/api";
|
|||||||
import { Pointer } from "./helpers/ui";
|
import { Pointer } from "./helpers/ui";
|
||||||
import { act, GlobalTestState, render, waitFor } from "./test-utils";
|
import { act, GlobalTestState, render, waitFor } from "./test-utils";
|
||||||
|
|
||||||
import type { ExcalidrawProps } from "../types";
|
import type { Collaborator, ExcalidrawProps, SocketId } from "../types";
|
||||||
|
|
||||||
describe("laser tool interactions", () => {
|
describe("laser tool interactions", () => {
|
||||||
const h = window.h;
|
const h = window.h;
|
||||||
@@ -128,4 +128,36 @@ describe("laser tool interactions", () => {
|
|||||||
expect(h.state.scrollY).toBe(initialScrollY);
|
expect(h.state.scrollY).toBe(initialScrollY);
|
||||||
expect(GlobalTestState.interactiveCanvas.style.cursor).toContain("");
|
expect(GlobalTestState.interactiveCanvas.style.cursor).toContain("");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("cleans up remote laser trails when the last collaborator leaves", async () => {
|
||||||
|
await render(<Excalidraw />);
|
||||||
|
|
||||||
|
const socketId = "socket-id" as SocketId;
|
||||||
|
const collaborators = new Map<SocketId, Collaborator>([
|
||||||
|
[
|
||||||
|
socketId,
|
||||||
|
{
|
||||||
|
pointer: {
|
||||||
|
x: 10,
|
||||||
|
y: 10,
|
||||||
|
tool: "laser",
|
||||||
|
},
|
||||||
|
button: "down",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
const svgLayer = document.querySelector(".SVGLayer svg")!;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
h.app.updateScene({ collaborators });
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(svgLayer.querySelectorAll("path")).toHaveLength(1);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
h.app.updateScene({ collaborators: new Map() });
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(svgLayer.querySelectorAll("path")).toHaveLength(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -567,6 +567,10 @@ export type OnExportProgress = {
|
|||||||
progress?: number;
|
progress?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type RenderOpacityResolver = (
|
||||||
|
element: NonDeletedExcalidrawElement,
|
||||||
|
) => ExcalidrawElement["opacity"] | undefined;
|
||||||
|
|
||||||
export interface ExcalidrawProps {
|
export interface ExcalidrawProps {
|
||||||
onChange?: (
|
onChange?: (
|
||||||
elements: readonly OrderedExcalidrawElement[],
|
elements: readonly OrderedExcalidrawElement[],
|
||||||
@@ -645,6 +649,10 @@ export interface ExcalidrawProps {
|
|||||||
appState: UIAppState,
|
appState: UIAppState,
|
||||||
) => JSX.Element;
|
) => JSX.Element;
|
||||||
UIOptions?: Partial<UIOptions>;
|
UIOptions?: Partial<UIOptions>;
|
||||||
|
/**
|
||||||
|
* dimensions and size constraints for inserted images
|
||||||
|
*/
|
||||||
|
imageOptions?: ImageOptions;
|
||||||
detectScroll?: boolean;
|
detectScroll?: boolean;
|
||||||
handleKeyboardGlobally?: boolean;
|
handleKeyboardGlobally?: boolean;
|
||||||
onLibraryChange?: (libraryItems: LibraryItems) => void | Promise<any>;
|
onLibraryChange?: (libraryItems: LibraryItems) => void | Promise<any>;
|
||||||
@@ -678,6 +686,7 @@ export interface ExcalidrawProps {
|
|||||||
element: NonDeleted<ExcalidrawEmbeddableElement>,
|
element: NonDeleted<ExcalidrawEmbeddableElement>,
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
) => JSX.Element | null;
|
) => JSX.Element | null;
|
||||||
|
resolveRenderOpacity?: RenderOpacityResolver;
|
||||||
aiEnabled?: boolean;
|
aiEnabled?: boolean;
|
||||||
showDeprecatedFonts?: boolean;
|
showDeprecatedFonts?: boolean;
|
||||||
renderScrollbars?: boolean;
|
renderScrollbars?: boolean;
|
||||||
@@ -731,6 +740,11 @@ export type ExportOpts = {
|
|||||||
) => JSX.Element;
|
) => JSX.Element;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ImageOptions = Partial<{
|
||||||
|
maxWidthOrHeight: number;
|
||||||
|
maxFileSizeBytes: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
// NOTE at the moment, if action name corresponds to canvasAction prop, its
|
// NOTE at the moment, if action name corresponds to canvasAction prop, its
|
||||||
// truthiness value will determine whether the action is rendered or not
|
// truthiness value will determine whether the action is rendered or not
|
||||||
// (see manager renderAction). We also override canvasAction values in
|
// (see manager renderAction). We also override canvasAction values in
|
||||||
@@ -772,6 +786,7 @@ export type AppProps = Merge<
|
|||||||
canvasActions: Required<CanvasActions> & { export: ExportOpts };
|
canvasActions: Required<CanvasActions> & { export: ExportOpts };
|
||||||
}
|
}
|
||||||
>;
|
>;
|
||||||
|
imageOptions: Required<ImageOptions>;
|
||||||
detectScroll: boolean;
|
detectScroll: boolean;
|
||||||
handleKeyboardGlobally: boolean;
|
handleKeyboardGlobally: boolean;
|
||||||
isCollaborating: boolean;
|
isCollaborating: boolean;
|
||||||
@@ -952,6 +967,11 @@ export interface ExcalidrawImperativeAPI {
|
|||||||
getFiles: () => InstanceType<typeof App>["files"];
|
getFiles: () => InstanceType<typeof App>["files"];
|
||||||
getName: InstanceType<typeof App>["getName"];
|
getName: InstanceType<typeof App>["getName"];
|
||||||
scrollToContent: InstanceType<typeof App>["scrollToContent"];
|
scrollToContent: InstanceType<typeof App>["scrollToContent"];
|
||||||
|
animateElements: InstanceType<typeof App>["animateElements"];
|
||||||
|
cancelElementAnimation: InstanceType<typeof App>["cancelElementAnimation"];
|
||||||
|
clearElementAnimationOverrides: InstanceType<
|
||||||
|
typeof App
|
||||||
|
>["clearElementAnimationOverrides"];
|
||||||
registerAction: (action: Action) => void;
|
registerAction: (action: Action) => void;
|
||||||
refresh: InstanceType<typeof App>["refresh"];
|
refresh: InstanceType<typeof App>["refresh"];
|
||||||
setToast: InstanceType<typeof App>["setToast"];
|
setToast: InstanceType<typeof App>["setToast"];
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { degreesToRadians } from "./angle";
|
import { degreesToRadians } from "./angle";
|
||||||
import { PRECISION } from "./utils";
|
import { isFiniteNumber, PRECISION } from "./utils";
|
||||||
import { vectorFromPoint, vectorScale } from "./vector";
|
import { vectorFromPoint, vectorScale } from "./vector";
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
@@ -253,3 +253,12 @@ export const isPointWithinBounds = <P extends GlobalPoint | LocalPoint>(
|
|||||||
q[1] >= Math.min(p[1], r[1])
|
q[1] >= Math.min(p[1], r[1])
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isValidPoint = (point: unknown): point is LocalPoint => {
|
||||||
|
return (
|
||||||
|
Array.isArray(point) &&
|
||||||
|
point.length === 2 &&
|
||||||
|
isFiniteNumber(point[0]) &&
|
||||||
|
isFiniteNumber(point[1])
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user