@mcut/react 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +187 -0
- package/README.md +11 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +681 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
https://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty percent
|
|
20
|
+
(50%) or more of the outstanding shares, or (iii) beneficial ownership of
|
|
21
|
+
such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation source, and
|
|
28
|
+
configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation
|
|
31
|
+
or translation of a Source form, including but not limited to compiled object
|
|
32
|
+
code, generated documentation, and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
35
|
+
made available under the License, as indicated by a copyright notice that is
|
|
36
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
37
|
+
below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object form,
|
|
40
|
+
that is based on (or derived from) the Work and for which the editorial
|
|
41
|
+
revisions, annotations, elaborations, or other modifications represent, as a
|
|
42
|
+
whole, an original work of authorship. For the purposes of this License,
|
|
43
|
+
Derivative Works shall not include works that remain separable from, or
|
|
44
|
+
merely link (or bind by name) to the interfaces of, the Work and Derivative
|
|
45
|
+
Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
48
|
+
version of the Work and any modifications or additions to that Work or
|
|
49
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
50
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
51
|
+
Entity authorized to submit on behalf of the copyright owner. For the
|
|
52
|
+
purposes of this definition, "submitted" means any form of electronic,
|
|
53
|
+
verbal, or written communication sent to the Licensor or its representatives,
|
|
54
|
+
including but not limited to communication on electronic mailing lists,
|
|
55
|
+
source code control systems, and issue tracking systems that are managed by,
|
|
56
|
+
or on behalf of, the Licensor for the purpose of discussing and improving the
|
|
57
|
+
Work, but excluding communication that is conspicuously marked or otherwise
|
|
58
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
59
|
+
|
|
60
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on
|
|
61
|
+
behalf of whom a Contribution has been received by Licensor and subsequently
|
|
62
|
+
incorporated within the Work.
|
|
63
|
+
|
|
64
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
65
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
66
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
67
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
68
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
69
|
+
Object form.
|
|
70
|
+
|
|
71
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
72
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
73
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
|
74
|
+
section) patent license to make, have made, use, offer to sell, sell, import,
|
|
75
|
+
and otherwise transfer the Work, where such license applies only to those
|
|
76
|
+
patent claims licensable by such Contributor that are necessarily infringed
|
|
77
|
+
by their Contribution(s) alone or by combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You institute
|
|
79
|
+
patent litigation against any entity (including a cross-claim or counterclaim
|
|
80
|
+
in a lawsuit) alleging that the Work or a Contribution incorporated within
|
|
81
|
+
the Work constitutes direct or contributory patent infringement, then any
|
|
82
|
+
patent licenses granted to You under this License for that Work shall
|
|
83
|
+
terminate as of the date such litigation is filed.
|
|
84
|
+
|
|
85
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
86
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
87
|
+
Source or Object form, provided that You meet the following conditions:
|
|
88
|
+
|
|
89
|
+
(a) You must give any other recipients of the Work or Derivative Works a
|
|
90
|
+
copy of this License; and
|
|
91
|
+
|
|
92
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
93
|
+
You changed the files; and
|
|
94
|
+
|
|
95
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
96
|
+
distribute, all copyright, patent, trademark, and attribution notices from
|
|
97
|
+
the Source form of the Work, excluding those notices that do not pertain to
|
|
98
|
+
any part of the Derivative Works; and
|
|
99
|
+
|
|
100
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution,
|
|
101
|
+
then any Derivative Works that You distribute must include a readable copy of
|
|
102
|
+
the attribution notices contained within such NOTICE file, excluding those
|
|
103
|
+
notices that do not pertain to any part of the Derivative Works, in at least
|
|
104
|
+
one of the following places: within a NOTICE text file distributed as part of
|
|
105
|
+
the Derivative Works; within the Source form or documentation, if provided
|
|
106
|
+
along with the Derivative Works; or, within a display generated by the
|
|
107
|
+
Derivative Works, if and wherever such third-party notices normally appear.
|
|
108
|
+
The contents of the NOTICE file are for informational purposes only and do
|
|
109
|
+
not modify the License. You may add Your own attribution notices within
|
|
110
|
+
Derivative Works that You distribute, alongside or as an addendum to the
|
|
111
|
+
NOTICE text from the Work, provided that such additional attribution notices
|
|
112
|
+
cannot be construed as modifying the License.
|
|
113
|
+
|
|
114
|
+
You may add Your own copyright statement to Your modifications and may
|
|
115
|
+
provide additional or different license terms and conditions for use,
|
|
116
|
+
reproduction, or distribution of Your modifications, or for any such
|
|
117
|
+
Derivative Works as a whole, provided Your use, reproduction, and
|
|
118
|
+
distribution of the Work otherwise complies with the conditions stated in
|
|
119
|
+
this License.
|
|
120
|
+
|
|
121
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
122
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
123
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
124
|
+
additional terms or conditions. Notwithstanding the above, nothing herein
|
|
125
|
+
shall supersede or modify the terms of any separate license agreement you may
|
|
126
|
+
have executed with Licensor regarding such Contributions.
|
|
127
|
+
|
|
128
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
129
|
+
trademarks, service marks, or product names of the Licensor, except as
|
|
130
|
+
required for reasonable and customary use in describing the origin of the
|
|
131
|
+
Work and reproducing the content of the NOTICE file.
|
|
132
|
+
|
|
133
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
134
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
135
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
136
|
+
KIND, either express or implied, including, without limitation, any warranties
|
|
137
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
138
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
139
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
140
|
+
associated with Your exercise of permissions under this License.
|
|
141
|
+
|
|
142
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
143
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
144
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to
|
|
145
|
+
in writing, shall any Contributor be liable to You for damages, including any
|
|
146
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
147
|
+
character arising as a result of this License or out of the use or inability
|
|
148
|
+
to use the Work (including but not limited to damages for loss of goodwill,
|
|
149
|
+
work stoppage, computer failure or malfunction, or any and all other
|
|
150
|
+
commercial damages or losses), even if such Contributor has been advised of
|
|
151
|
+
the possibility of such damages.
|
|
152
|
+
|
|
153
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work
|
|
154
|
+
or Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
155
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
156
|
+
and/or rights consistent with this License. However, in accepting such
|
|
157
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
158
|
+
responsibility, not on behalf of any other Contributor, and only if You agree
|
|
159
|
+
to indemnify, defend, and hold each Contributor harmless for any liability
|
|
160
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
|
161
|
+
accepting any such warranty or additional liability.
|
|
162
|
+
|
|
163
|
+
END OF TERMS AND CONDITIONS
|
|
164
|
+
|
|
165
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
166
|
+
|
|
167
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
168
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
169
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
170
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
171
|
+
recommend that a file or class name and description of purpose be included on
|
|
172
|
+
the same "printed page" as the copyright notice for easier identification
|
|
173
|
+
within third-party archives.
|
|
174
|
+
|
|
175
|
+
Copyright [yyyy] [name of copyright owner]
|
|
176
|
+
|
|
177
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
178
|
+
you may not use this file except in compliance with the License.
|
|
179
|
+
You may obtain a copy of the License at
|
|
180
|
+
|
|
181
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
182
|
+
|
|
183
|
+
Unless required by applicable law or agreed to in writing, software
|
|
184
|
+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
185
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
186
|
+
See the License for the specific language governing permissions and
|
|
187
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @mcut/react
|
|
2
|
+
|
|
3
|
+
React bindings for mcut.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
bun add @mcut/react react react-dom
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
This package provides the editor provider, hooks, canvas player binding,
|
|
10
|
+
selection overlay, playback clock, and audio preview integration for React apps.
|
|
11
|
+
React and React DOM are peer dependencies.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { PreviewMediaPool } from "@mcut/media";
|
|
3
|
+
import { EditorEngine, EditorState, ElementId, ElementLocation, PlaybackState, Project, SelectionState, Transform } from "@mcut/timeline";
|
|
4
|
+
import { HandleId, OBB } from "@mcut/compositor";
|
|
5
|
+
|
|
6
|
+
//#region src/context.d.ts
|
|
7
|
+
interface EditorContextValue {
|
|
8
|
+
engine: EditorEngine;
|
|
9
|
+
pool: PreviewMediaPool;
|
|
10
|
+
}
|
|
11
|
+
interface EditorProviderProps {
|
|
12
|
+
/** Bring your own engine (e.g. created outside React); otherwise one is created. */
|
|
13
|
+
engine?: EditorEngine;
|
|
14
|
+
/** Initial project for the internally-created engine. */
|
|
15
|
+
project?: Project;
|
|
16
|
+
maxHistorySize?: number;
|
|
17
|
+
children: ReactNode;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Provides the editor engine and the preview media pool to the component
|
|
21
|
+
* tree. All mcut UI (PlayerCanvas, timeline panels, ...) lives under this.
|
|
22
|
+
*/
|
|
23
|
+
declare function EditorProvider({
|
|
24
|
+
engine: externalEngine,
|
|
25
|
+
project,
|
|
26
|
+
maxHistorySize,
|
|
27
|
+
children
|
|
28
|
+
}: EditorProviderProps): import("react").JSX.Element;
|
|
29
|
+
declare function useEditorContext(): EditorContextValue;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/hooks.d.ts
|
|
32
|
+
/** The editor engine: dispatch commands, undo/redo, transport. */
|
|
33
|
+
declare function useEditor(): EditorEngine;
|
|
34
|
+
/** Subscribe to a slice of editor state (project, selection, history flags). */
|
|
35
|
+
declare function useEditorState<TSelected = EditorState>(selector?: (state: EditorState) => TSelected, compare?: (a: TSelected, b: TSelected) => boolean): TSelected;
|
|
36
|
+
/** Subscribe to a slice of playback state (time, playing, volume). */
|
|
37
|
+
declare function usePlayback<TSelected = PlaybackState>(selector?: (state: PlaybackState) => TSelected, compare?: (a: TSelected, b: TSelected) => boolean): TSelected;
|
|
38
|
+
declare function useProject(): Project;
|
|
39
|
+
declare function useSelection(): SelectionState;
|
|
40
|
+
/** The first selected element with its track, if any. */
|
|
41
|
+
declare function useSelectedElement(): ElementLocation | undefined;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/player-canvas.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* Preview raster resolution. `'auto'` renders at the displayed size,
|
|
46
|
+
* a number caps the project's short side (720 ≈ 720p), `'full'` always
|
|
47
|
+
* rasters at full project resolution. Every mode is capped at project
|
|
48
|
+
* resolution — CSS scales the canvas to fit either way.
|
|
49
|
+
*/
|
|
50
|
+
type PreviewQuality = 'auto' | 'full' | number;
|
|
51
|
+
interface PlayerCanvasProps {
|
|
52
|
+
className?: string;
|
|
53
|
+
/** Enable canvas selection/move/resize/rotate. Default true. */
|
|
54
|
+
interactive?: boolean;
|
|
55
|
+
/** Project background color. Default black. */
|
|
56
|
+
background?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Elements left out of the render — e.g. a text element while a DOM
|
|
59
|
+
* inline editor overlays it (renderFrame's skipElementIds).
|
|
60
|
+
*/
|
|
61
|
+
hiddenElementIds?: ReadonlySet<string>;
|
|
62
|
+
/** Double-click on an element (topmost hit). Hosts use it to open editors. */
|
|
63
|
+
onElementDoubleClick?: (elementId: ElementId) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Preview raster resolution; `'auto'` (default) matches the displayed
|
|
66
|
+
* size. A 4K project in an ~800px pane rasters ~20× fewer pixels per
|
|
67
|
+
* frame than `'full'`, which is usually the difference between smooth
|
|
68
|
+
* and dropped playback on big projects.
|
|
69
|
+
*/
|
|
70
|
+
quality?: PreviewQuality;
|
|
71
|
+
/**
|
|
72
|
+
* Compositor backend. `'webgpu'` renders through the WebGPU pass
|
|
73
|
+
* pipeline (requires `navigator.gpu`; falls back to canvas2d while the
|
|
74
|
+
* device initializes or when unavailable). Default `'canvas2d'`.
|
|
75
|
+
*/
|
|
76
|
+
renderer?: 'canvas2d' | 'webgpu';
|
|
77
|
+
}
|
|
78
|
+
declare function PlayerCanvas({
|
|
79
|
+
className,
|
|
80
|
+
interactive,
|
|
81
|
+
background,
|
|
82
|
+
quality,
|
|
83
|
+
hiddenElementIds,
|
|
84
|
+
onElementDoubleClick,
|
|
85
|
+
renderer
|
|
86
|
+
}: PlayerCanvasProps): import("react").JSX.Element;
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/gestures.d.ts
|
|
89
|
+
interface GesturePoint {
|
|
90
|
+
x: number;
|
|
91
|
+
y: number;
|
|
92
|
+
}
|
|
93
|
+
interface BoxResizeResult {
|
|
94
|
+
transform: Transform;
|
|
95
|
+
displayWidth: number;
|
|
96
|
+
displayHeight: number;
|
|
97
|
+
}
|
|
98
|
+
/** Translate gesture: offset the base transform by the pointer delta. */
|
|
99
|
+
declare function applyMove(base: Transform, start: GesturePoint, current: GesturePoint): Transform;
|
|
100
|
+
/** Rotate gesture around the element center. */
|
|
101
|
+
declare function applyRotate(base: Transform, obb: OBB, start: GesturePoint, current: GesturePoint): Transform;
|
|
102
|
+
/**
|
|
103
|
+
* Resize gesture. Corner handles scale uniformly; edge handles scale one
|
|
104
|
+
* axis. Pointer positions are mapped into the element's local (un-rotated)
|
|
105
|
+
* space so resizing behaves intuitively on rotated elements.
|
|
106
|
+
*/
|
|
107
|
+
declare function applyResize(base: Transform, obb: OBB, handle: Exclude<HandleId, 'rotate'>, start: GesturePoint, current: GesturePoint): Transform;
|
|
108
|
+
declare function applyBoxResize(base: Transform, obb: OBB, handle: Exclude<HandleId, 'rotate'>, current: GesturePoint, minSize?: number): BoxResizeResult;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { type BoxResizeResult, type EditorContextValue, EditorProvider, type EditorProviderProps, type GesturePoint, PlayerCanvas, type PlayerCanvasProps, type PreviewQuality, applyBoxResize, applyMove, applyResize, applyRotate, useEditor, useEditorContext, useEditorState, usePlayback, useProject, useSelectedElement, useSelection };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext, useEffect, useRef } from "react";
|
|
3
|
+
import { PreviewMediaPool, getActiveMediaItems } from "@mcut/media";
|
|
4
|
+
import { EditorEngine, getElement, getElementLocation, getProjectDurationMs, hasKeyframes, isElementActiveAt, resolveAnimatedElement } from "@mcut/timeline";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { useSelector } from "@tanstack/react-store";
|
|
7
|
+
import { WebGPUBackend, degToRad, getElementOBB, getHandles, hitTestHandles, hitTestOBB, isWebGPUSupported, layoutTextBlock, measureWith, renderFrame, renderFrameWith } from "@mcut/compositor";
|
|
8
|
+
//#region src/context.tsx
|
|
9
|
+
const EditorContext = createContext(null);
|
|
10
|
+
/**
|
|
11
|
+
* Provides the editor engine and the preview media pool to the component
|
|
12
|
+
* tree. All mcut UI (PlayerCanvas, timeline panels, ...) lives under this.
|
|
13
|
+
*/
|
|
14
|
+
function EditorProvider({ engine: externalEngine, project, maxHistorySize, children }) {
|
|
15
|
+
const valueRef = useRef(null);
|
|
16
|
+
if (valueRef.current === null) {
|
|
17
|
+
const engine = externalEngine ?? new EditorEngine({
|
|
18
|
+
...project ? { project } : {},
|
|
19
|
+
...maxHistorySize !== void 0 ? { maxHistorySize } : {}
|
|
20
|
+
});
|
|
21
|
+
valueRef.current = {
|
|
22
|
+
engine,
|
|
23
|
+
pool: new PreviewMediaPool((assetId) => engine.project.assets[assetId])
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const value = valueRef.current;
|
|
28
|
+
return () => {
|
|
29
|
+
value?.pool.dispose();
|
|
30
|
+
};
|
|
31
|
+
}, []);
|
|
32
|
+
return /* @__PURE__ */ jsx(EditorContext.Provider, {
|
|
33
|
+
value: valueRef.current,
|
|
34
|
+
children
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function useEditorContext() {
|
|
38
|
+
const context = useContext(EditorContext);
|
|
39
|
+
if (!context) throw new Error("mcut hooks must be used inside <EditorProvider>");
|
|
40
|
+
return context;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/hooks.ts
|
|
44
|
+
/** The editor engine: dispatch commands, undo/redo, transport. */
|
|
45
|
+
function useEditor() {
|
|
46
|
+
return useEditorContext().engine;
|
|
47
|
+
}
|
|
48
|
+
/** Subscribe to a slice of editor state (project, selection, history flags). */
|
|
49
|
+
function useEditorState(selector, compare) {
|
|
50
|
+
const { engine } = useEditorContext();
|
|
51
|
+
return useSelector(engine.store, selector, compare ? { compare } : void 0);
|
|
52
|
+
}
|
|
53
|
+
/** Subscribe to a slice of playback state (time, playing, volume). */
|
|
54
|
+
function usePlayback(selector, compare) {
|
|
55
|
+
const { engine } = useEditorContext();
|
|
56
|
+
return useSelector(engine.playback, selector, compare ? { compare } : void 0);
|
|
57
|
+
}
|
|
58
|
+
function useProject() {
|
|
59
|
+
return useEditorState((state) => state.project);
|
|
60
|
+
}
|
|
61
|
+
function useSelection() {
|
|
62
|
+
return useEditorState((state) => state.selection);
|
|
63
|
+
}
|
|
64
|
+
/** The first selected element with its track, if any. */
|
|
65
|
+
function useSelectedElement() {
|
|
66
|
+
return useEditorState((state) => {
|
|
67
|
+
const id = state.selection.elementIds[0];
|
|
68
|
+
return id ? getElementLocation(state.project, id) : void 0;
|
|
69
|
+
}, (a, b) => a?.element === b?.element && a?.track === b?.track && a?.trackIndex === b?.trackIndex);
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/gestures.ts
|
|
73
|
+
const MIN_SCALE = .05;
|
|
74
|
+
const MIN_BOX_SIZE = 8;
|
|
75
|
+
/** Keep magnitude ≥ MIN_SCALE, preserving sign (negative scale = flip). */
|
|
76
|
+
const clampScale = (value) => {
|
|
77
|
+
return (value < 0 ? -1 : 1) * Math.max(MIN_SCALE, Math.abs(value));
|
|
78
|
+
};
|
|
79
|
+
function pointToLocal(obb, point) {
|
|
80
|
+
const rad = degToRad(-obb.rotation);
|
|
81
|
+
const dx = point.x - obb.cx;
|
|
82
|
+
const dy = point.y - obb.cy;
|
|
83
|
+
return {
|
|
84
|
+
x: dx * Math.cos(rad) - dy * Math.sin(rad),
|
|
85
|
+
y: dx * Math.sin(rad) + dy * Math.cos(rad)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function localDeltaToCanvas(obb, delta) {
|
|
89
|
+
const rad = degToRad(obb.rotation);
|
|
90
|
+
return {
|
|
91
|
+
x: delta.x * Math.cos(rad) - delta.y * Math.sin(rad),
|
|
92
|
+
y: delta.x * Math.sin(rad) + delta.y * Math.cos(rad)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** Translate gesture: offset the base transform by the pointer delta. */
|
|
96
|
+
function applyMove(base, start, current) {
|
|
97
|
+
return {
|
|
98
|
+
...base,
|
|
99
|
+
x: Math.round(base.x + (current.x - start.x)),
|
|
100
|
+
y: Math.round(base.y + (current.y - start.y))
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** Rotate gesture around the element center. */
|
|
104
|
+
function applyRotate(base, obb, start, current) {
|
|
105
|
+
const startAngle = Math.atan2(start.y - obb.cy, start.x - obb.cx);
|
|
106
|
+
const currentAngle = Math.atan2(current.y - obb.cy, current.x - obb.cx);
|
|
107
|
+
let rotation = base.rotation + (currentAngle - startAngle) * 180 / Math.PI;
|
|
108
|
+
rotation = ((rotation + 180) % 360 + 360) % 360 - 180;
|
|
109
|
+
return {
|
|
110
|
+
...base,
|
|
111
|
+
rotation: Math.round(rotation * 10) / 10
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Resize gesture. Corner handles scale uniformly; edge handles scale one
|
|
116
|
+
* axis. Pointer positions are mapped into the element's local (un-rotated)
|
|
117
|
+
* space so resizing behaves intuitively on rotated elements.
|
|
118
|
+
*/
|
|
119
|
+
function applyResize(base, obb, handle, start, current) {
|
|
120
|
+
const localStart = pointToLocal(obb, start);
|
|
121
|
+
const localCurrent = pointToLocal(obb, current);
|
|
122
|
+
if (handle === "e" || handle === "w") {
|
|
123
|
+
const factor = localStart.x === 0 ? 1 : localCurrent.x / localStart.x;
|
|
124
|
+
return {
|
|
125
|
+
...base,
|
|
126
|
+
scaleX: clampScale(base.scaleX * factor)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (handle === "n" || handle === "s") {
|
|
130
|
+
const factor = localStart.y === 0 ? 1 : localCurrent.y / localStart.y;
|
|
131
|
+
return {
|
|
132
|
+
...base,
|
|
133
|
+
scaleY: clampScale(base.scaleY * factor)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const startDistance = Math.hypot(localStart.x, localStart.y);
|
|
137
|
+
const factor = startDistance === 0 ? 1 : Math.hypot(localCurrent.x, localCurrent.y) / startDistance;
|
|
138
|
+
return {
|
|
139
|
+
...base,
|
|
140
|
+
scaleX: clampScale(base.scaleX * factor),
|
|
141
|
+
scaleY: clampScale(base.scaleY * factor)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function applyBoxResize(base, obb, handle, current, minSize = MIN_BOX_SIZE) {
|
|
145
|
+
const localCurrent = pointToLocal(obb, current);
|
|
146
|
+
let left = -obb.width / 2;
|
|
147
|
+
let right = obb.width / 2;
|
|
148
|
+
let top = -obb.height / 2;
|
|
149
|
+
let bottom = obb.height / 2;
|
|
150
|
+
if (handle.includes("w")) left = localCurrent.x;
|
|
151
|
+
if (handle.includes("e")) right = localCurrent.x;
|
|
152
|
+
if (handle.includes("n")) top = localCurrent.y;
|
|
153
|
+
if (handle.includes("s")) bottom = localCurrent.y;
|
|
154
|
+
if (right - left < minSize) if (handle.includes("w")) left = right - minSize;
|
|
155
|
+
else right = left + minSize;
|
|
156
|
+
if (bottom - top < minSize) if (handle.includes("n")) top = bottom - minSize;
|
|
157
|
+
else bottom = top + minSize;
|
|
158
|
+
const centerDelta = localDeltaToCanvas(obb, {
|
|
159
|
+
x: (left + right) / 2,
|
|
160
|
+
y: (top + bottom) / 2
|
|
161
|
+
});
|
|
162
|
+
return {
|
|
163
|
+
transform: {
|
|
164
|
+
...base,
|
|
165
|
+
x: Math.round((base.x + centerDelta.x) * 10) / 10,
|
|
166
|
+
y: Math.round((base.y + centerDelta.y) * 10) / 10
|
|
167
|
+
},
|
|
168
|
+
displayWidth: Math.round((right - left) * 10) / 10,
|
|
169
|
+
displayHeight: Math.round((bottom - top) * 10) / 10
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/player-canvas.tsx
|
|
174
|
+
/** Snap-to-center threshold in screen px while moving an element. */
|
|
175
|
+
const CENTER_SNAP_PX = 10;
|
|
176
|
+
const GESTURE_PROPERTIES = [
|
|
177
|
+
["position.x", "x"],
|
|
178
|
+
["position.y", "y"],
|
|
179
|
+
["scale.x", "scaleX"],
|
|
180
|
+
["scale.y", "scaleY"],
|
|
181
|
+
["rotation", "rotation"]
|
|
182
|
+
];
|
|
183
|
+
/**
|
|
184
|
+
* Apply a gesture's transform: armed properties (Premiere stopwatch on)
|
|
185
|
+
* auto-key at the playhead; unarmed properties patch the static transform.
|
|
186
|
+
* Runs inside the gesture transaction, so a whole drag is one undo entry.
|
|
187
|
+
*/
|
|
188
|
+
function applyGestureTransform(engine, elementId, transform, timelineMs) {
|
|
189
|
+
const element = getElement(engine.project, elementId);
|
|
190
|
+
if (!element || !("transform" in element)) return;
|
|
191
|
+
if (GESTURE_PROPERTIES.filter(([property]) => hasKeyframes(element, property)).length === 0) {
|
|
192
|
+
engine.dispatch({
|
|
193
|
+
type: "updateElement",
|
|
194
|
+
elementId,
|
|
195
|
+
patch: { transform }
|
|
196
|
+
});
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const localMs = Math.max(0, Math.round(timelineMs - element.startMs));
|
|
200
|
+
const staticTransform = { ...element.transform };
|
|
201
|
+
for (const [property, key] of GESTURE_PROPERTIES) if (hasKeyframes(element, property)) engine.dispatch({
|
|
202
|
+
type: "setKeyframe",
|
|
203
|
+
elementId,
|
|
204
|
+
property,
|
|
205
|
+
timeMs: localMs,
|
|
206
|
+
value: transform[key]
|
|
207
|
+
});
|
|
208
|
+
else staticTransform[key] = transform[key];
|
|
209
|
+
engine.dispatch({
|
|
210
|
+
type: "updateElement",
|
|
211
|
+
elementId,
|
|
212
|
+
patch: { transform: staticTransform }
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* The preview player: renders the project through the shared compositor on
|
|
217
|
+
* every animation frame, advances the playback clock, keeps the media pool
|
|
218
|
+
* in sync, and hosts the selection overlay (drag / resize / rotate).
|
|
219
|
+
*/
|
|
220
|
+
/**
|
|
221
|
+
* The raster scale for one preview frame: displayed size for `'auto'`
|
|
222
|
+
* (quantized so sub-pixel layout jitter doesn't reallocate the canvas),
|
|
223
|
+
* short-side cap for numeric presets, 1 for `'full'`. Never upscales
|
|
224
|
+
* beyond project resolution.
|
|
225
|
+
*/
|
|
226
|
+
function getRenderScale(project, quality, container) {
|
|
227
|
+
if (quality === "full") return 1;
|
|
228
|
+
if (typeof quality === "number") {
|
|
229
|
+
const shortSide = Math.min(project.width, project.height);
|
|
230
|
+
return shortSide > 0 ? Math.min(1, quality / shortSide) : 1;
|
|
231
|
+
}
|
|
232
|
+
const displayWidth = (container?.clientWidth ?? 0) * (window.devicePixelRatio || 1);
|
|
233
|
+
if (displayWidth <= 0 || project.width <= 0) return 1;
|
|
234
|
+
return Math.min(1, Math.ceil(displayWidth / 64) * 64 / project.width);
|
|
235
|
+
}
|
|
236
|
+
function PlayerCanvas({ className, interactive = true, background, quality = "auto", hiddenElementIds, onElementDoubleClick, renderer = "canvas2d" }) {
|
|
237
|
+
const { engine, pool } = useEditorContext();
|
|
238
|
+
const containerRef = useRef(null);
|
|
239
|
+
const renderCanvasRef = useRef(null);
|
|
240
|
+
const gpuRef = useRef(null);
|
|
241
|
+
const overlayCanvasRef = useRef(null);
|
|
242
|
+
const measureCtxRef = useRef(null);
|
|
243
|
+
const gestureRef = useRef(null);
|
|
244
|
+
const feedbackRef = useRef({
|
|
245
|
+
guideVertical: false,
|
|
246
|
+
guideHorizontal: false,
|
|
247
|
+
label: null
|
|
248
|
+
});
|
|
249
|
+
const sizeHelpers = (project) => ({
|
|
250
|
+
getAssetSize: (assetId) => {
|
|
251
|
+
const asset = project.assets[assetId];
|
|
252
|
+
return asset?.width && asset?.height ? {
|
|
253
|
+
width: asset.width,
|
|
254
|
+
height: asset.height
|
|
255
|
+
} : null;
|
|
256
|
+
},
|
|
257
|
+
measureText: (text, style, box, runs) => {
|
|
258
|
+
if (!measureCtxRef.current) measureCtxRef.current = document.createElement("canvas").getContext("2d");
|
|
259
|
+
const ctx = measureCtxRef.current;
|
|
260
|
+
if (!ctx) return {
|
|
261
|
+
width: 0,
|
|
262
|
+
height: 0
|
|
263
|
+
};
|
|
264
|
+
const layout = layoutTextBlock(measureWith(ctx), text, style, {
|
|
265
|
+
box,
|
|
266
|
+
...runs ? { runs } : {}
|
|
267
|
+
});
|
|
268
|
+
return {
|
|
269
|
+
width: layout.width,
|
|
270
|
+
height: layout.height
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
const obbFor = (project, element) => getElementOBB(project, element, sizeHelpers(project));
|
|
275
|
+
useEffect(() => {
|
|
276
|
+
let raf = 0;
|
|
277
|
+
let lastNow = performance.now();
|
|
278
|
+
const loop = () => {
|
|
279
|
+
const now = performance.now();
|
|
280
|
+
const playback = engine.playback.state;
|
|
281
|
+
let project = engine.project;
|
|
282
|
+
if (playback.isPlaying) {
|
|
283
|
+
const durationMs = getProjectDurationMs(project);
|
|
284
|
+
const next = playback.currentTimeMs + (now - lastNow) * playback.playbackRate;
|
|
285
|
+
if (durationMs > 0 && next >= durationMs && playback.playbackRate > 0) {
|
|
286
|
+
engine.seek(durationMs);
|
|
287
|
+
engine.pause();
|
|
288
|
+
} else if (next <= 0 && playback.playbackRate < 0) {
|
|
289
|
+
engine.seek(0);
|
|
290
|
+
engine.pause();
|
|
291
|
+
} else engine.seek(next);
|
|
292
|
+
}
|
|
293
|
+
lastNow = now;
|
|
294
|
+
project = engine.project;
|
|
295
|
+
const { currentTimeMs, isPlaying, playbackRate, volume, muted } = engine.playback.state;
|
|
296
|
+
pool.sync(getActiveMediaItems(project, currentTimeMs), {
|
|
297
|
+
isPlaying,
|
|
298
|
+
playbackRate,
|
|
299
|
+
masterVolume: volume,
|
|
300
|
+
muted
|
|
301
|
+
});
|
|
302
|
+
const canvas = renderCanvasRef.current;
|
|
303
|
+
if (canvas) {
|
|
304
|
+
const scale = getRenderScale(project, quality, containerRef.current);
|
|
305
|
+
const width = Math.max(1, Math.round(project.width * scale));
|
|
306
|
+
const height = Math.max(1, Math.round(project.height * scale));
|
|
307
|
+
if (canvas.width !== width) canvas.width = width;
|
|
308
|
+
if (canvas.height !== height) canvas.height = height;
|
|
309
|
+
const renderOptions = {
|
|
310
|
+
source: pool,
|
|
311
|
+
...background ? { backgroundColor: background } : {},
|
|
312
|
+
...hiddenRef.current && hiddenRef.current.size > 0 ? { skipElementIds: hiddenRef.current } : {}
|
|
313
|
+
};
|
|
314
|
+
if (renderer === "webgpu" && isWebGPUSupported()) {
|
|
315
|
+
const key = `${project.width}x${project.height}`;
|
|
316
|
+
let gpu = gpuRef.current;
|
|
317
|
+
if (gpu && gpu.key !== key && gpu.backend) {
|
|
318
|
+
gpu.backend.dispose();
|
|
319
|
+
gpu = null;
|
|
320
|
+
}
|
|
321
|
+
if (!gpu) {
|
|
322
|
+
const created = {
|
|
323
|
+
key,
|
|
324
|
+
backend: null,
|
|
325
|
+
pending: true
|
|
326
|
+
};
|
|
327
|
+
gpuRef.current = created;
|
|
328
|
+
gpu = created;
|
|
329
|
+
WebGPUBackend.create({
|
|
330
|
+
canvas,
|
|
331
|
+
width: project.width,
|
|
332
|
+
height: project.height
|
|
333
|
+
}).then((backend) => {
|
|
334
|
+
if (gpuRef.current === created) {
|
|
335
|
+
created.backend = backend;
|
|
336
|
+
created.pending = false;
|
|
337
|
+
} else backend.dispose();
|
|
338
|
+
}).catch(() => {
|
|
339
|
+
created.pending = false;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
if (gpu.backend) renderFrameWith(gpu.backend, project, currentTimeMs, renderOptions);
|
|
343
|
+
} else {
|
|
344
|
+
const ctx = canvas.getContext("2d");
|
|
345
|
+
if (ctx) {
|
|
346
|
+
ctx.setTransform(scale, 0, 0, scale, 0, 0);
|
|
347
|
+
renderFrame(ctx, project, currentTimeMs, renderOptions);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
drawOverlay(project, currentTimeMs);
|
|
352
|
+
raf = requestAnimationFrame(loop);
|
|
353
|
+
};
|
|
354
|
+
const drawOverlay = (project, timeMs) => {
|
|
355
|
+
const overlay = overlayCanvasRef.current;
|
|
356
|
+
const container = containerRef.current;
|
|
357
|
+
if (!overlay || !container) return;
|
|
358
|
+
const dpr = window.devicePixelRatio || 1;
|
|
359
|
+
const width = Math.max(1, Math.round(container.clientWidth * dpr));
|
|
360
|
+
const height = Math.max(1, Math.round(container.clientHeight * dpr));
|
|
361
|
+
if (overlay.width !== width) overlay.width = width;
|
|
362
|
+
if (overlay.height !== height) overlay.height = height;
|
|
363
|
+
const ctx = overlay.getContext("2d");
|
|
364
|
+
if (!ctx) return;
|
|
365
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
366
|
+
ctx.clearRect(0, 0, width, height);
|
|
367
|
+
if (!interactive) return;
|
|
368
|
+
const selectedId = engine.selection.elementIds[0];
|
|
369
|
+
if (!selectedId) return;
|
|
370
|
+
const raw = getElement(project, selectedId);
|
|
371
|
+
if (!raw || !isElementActiveAt(raw, timeMs)) return;
|
|
372
|
+
const obb = obbFor(project, resolveAnimatedElement(raw, timeMs));
|
|
373
|
+
if (!obb) return;
|
|
374
|
+
const scale = width / project.width;
|
|
375
|
+
ctx.setTransform(scale, 0, 0, scale, 0, 0);
|
|
376
|
+
const px = (value) => value / scale;
|
|
377
|
+
ctx.save();
|
|
378
|
+
ctx.translate(obb.cx, obb.cy);
|
|
379
|
+
ctx.rotate(obb.rotation * Math.PI / 180);
|
|
380
|
+
ctx.strokeStyle = "#3b82f6";
|
|
381
|
+
ctx.lineWidth = px(1.5);
|
|
382
|
+
ctx.setLineDash([px(6), px(4)]);
|
|
383
|
+
ctx.strokeRect(-obb.width / 2, -obb.height / 2, obb.width, obb.height);
|
|
384
|
+
ctx.restore();
|
|
385
|
+
ctx.setLineDash([]);
|
|
386
|
+
for (const handle of getHandles(obb)) {
|
|
387
|
+
const size = px(handle.id === "rotate" ? 10 : 8);
|
|
388
|
+
ctx.fillStyle = "#ffffff";
|
|
389
|
+
ctx.strokeStyle = "#3b82f6";
|
|
390
|
+
ctx.lineWidth = px(1.5);
|
|
391
|
+
if (handle.id === "rotate") {
|
|
392
|
+
ctx.beginPath();
|
|
393
|
+
ctx.arc(handle.x, handle.y, size / 2, 0, Math.PI * 2);
|
|
394
|
+
ctx.fill();
|
|
395
|
+
ctx.stroke();
|
|
396
|
+
} else {
|
|
397
|
+
ctx.fillRect(handle.x - size / 2, handle.y - size / 2, size, size);
|
|
398
|
+
ctx.strokeRect(handle.x - size / 2, handle.y - size / 2, size, size);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const feedback = feedbackRef.current;
|
|
402
|
+
if (gestureRef.current) {
|
|
403
|
+
if (feedback.guideVertical || feedback.guideHorizontal) {
|
|
404
|
+
ctx.strokeStyle = "#e879f9";
|
|
405
|
+
ctx.lineWidth = px(1);
|
|
406
|
+
ctx.setLineDash([px(5), px(4)]);
|
|
407
|
+
ctx.beginPath();
|
|
408
|
+
if (feedback.guideVertical) {
|
|
409
|
+
ctx.moveTo(project.width / 2, 0);
|
|
410
|
+
ctx.lineTo(project.width / 2, project.height);
|
|
411
|
+
}
|
|
412
|
+
if (feedback.guideHorizontal) {
|
|
413
|
+
ctx.moveTo(0, project.height / 2);
|
|
414
|
+
ctx.lineTo(project.width, project.height / 2);
|
|
415
|
+
}
|
|
416
|
+
ctx.stroke();
|
|
417
|
+
ctx.setLineDash([]);
|
|
418
|
+
}
|
|
419
|
+
if (feedback.label) {
|
|
420
|
+
const fontPx = px(11);
|
|
421
|
+
ctx.font = `${fontPx}px ui-monospace, monospace`;
|
|
422
|
+
const metrics = ctx.measureText(feedback.label);
|
|
423
|
+
const padding = px(5);
|
|
424
|
+
const labelY = obb.cy + obb.height / 2 + px(18);
|
|
425
|
+
ctx.fillStyle = "rgba(0, 0, 0, 0.75)";
|
|
426
|
+
ctx.beginPath();
|
|
427
|
+
ctx.roundRect(obb.cx - metrics.width / 2 - padding, labelY - fontPx / 2 - padding, metrics.width + padding * 2, fontPx + padding * 2, px(4));
|
|
428
|
+
ctx.fill();
|
|
429
|
+
ctx.fillStyle = "#ffffff";
|
|
430
|
+
ctx.textAlign = "center";
|
|
431
|
+
ctx.textBaseline = "middle";
|
|
432
|
+
ctx.fillText(feedback.label, obb.cx, labelY);
|
|
433
|
+
ctx.textAlign = "left";
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
raf = requestAnimationFrame(loop);
|
|
438
|
+
return () => {
|
|
439
|
+
cancelAnimationFrame(raf);
|
|
440
|
+
gpuRef.current?.backend?.dispose();
|
|
441
|
+
gpuRef.current = null;
|
|
442
|
+
};
|
|
443
|
+
}, [
|
|
444
|
+
engine,
|
|
445
|
+
pool,
|
|
446
|
+
background,
|
|
447
|
+
interactive,
|
|
448
|
+
quality,
|
|
449
|
+
renderer
|
|
450
|
+
]);
|
|
451
|
+
const hiddenRef = useRef(hiddenElementIds);
|
|
452
|
+
hiddenRef.current = hiddenElementIds;
|
|
453
|
+
const doubleClickRef = useRef(onElementDoubleClick);
|
|
454
|
+
doubleClickRef.current = onElementDoubleClick;
|
|
455
|
+
const toProjectPoint = (event) => {
|
|
456
|
+
const overlay = overlayCanvasRef.current;
|
|
457
|
+
const project = engine.project;
|
|
458
|
+
if (!overlay) return null;
|
|
459
|
+
const rect = overlay.getBoundingClientRect();
|
|
460
|
+
if (rect.width === 0 || rect.height === 0) return null;
|
|
461
|
+
return {
|
|
462
|
+
x: (event.clientX - rect.left) / rect.width * project.width,
|
|
463
|
+
y: (event.clientY - rect.top) / rect.height * project.height
|
|
464
|
+
};
|
|
465
|
+
};
|
|
466
|
+
const screenToProject = () => {
|
|
467
|
+
const overlay = overlayCanvasRef.current;
|
|
468
|
+
if (!overlay) return 1;
|
|
469
|
+
const rect = overlay.getBoundingClientRect();
|
|
470
|
+
return rect.width === 0 ? 1 : engine.project.width / rect.width;
|
|
471
|
+
};
|
|
472
|
+
const handlePointerDown = (event) => {
|
|
473
|
+
if (!interactive) return;
|
|
474
|
+
const point = toProjectPoint(event);
|
|
475
|
+
if (!point) return;
|
|
476
|
+
const project = engine.project;
|
|
477
|
+
const timeMs = engine.playback.state.currentTimeMs;
|
|
478
|
+
const handleHitSize = 10 * screenToProject();
|
|
479
|
+
const selectedId = engine.selection.elementIds[0];
|
|
480
|
+
if (selectedId) {
|
|
481
|
+
const raw = getElement(project, selectedId);
|
|
482
|
+
if (raw && isElementActiveAt(raw, timeMs) && "transform" in raw) {
|
|
483
|
+
const element = resolveAnimatedElement(raw, timeMs);
|
|
484
|
+
const obb = obbFor(project, element);
|
|
485
|
+
if (obb) {
|
|
486
|
+
const handle = hitTestHandles(obb, point.x, point.y, handleHitSize);
|
|
487
|
+
if (handle) {
|
|
488
|
+
const baseBox = handle !== "rotate" && element.type === "text" && element.box ? {
|
|
489
|
+
...element.box,
|
|
490
|
+
height: element.box.height ?? obb.height / element.transform.scaleY,
|
|
491
|
+
hadHeight: element.box.height !== void 0
|
|
492
|
+
} : null;
|
|
493
|
+
gestureRef.current = {
|
|
494
|
+
kind: handle === "rotate" ? "rotate" : baseBox ? "box-resize" : "resize",
|
|
495
|
+
elementId: element.id,
|
|
496
|
+
baseTransform: element.transform,
|
|
497
|
+
baseOBB: obb,
|
|
498
|
+
baseBox,
|
|
499
|
+
handle: handle === "rotate" ? null : handle,
|
|
500
|
+
start: point
|
|
501
|
+
};
|
|
502
|
+
engine.beginTransaction();
|
|
503
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
for (let trackIndex = project.tracks.length - 1; trackIndex >= 0; trackIndex--) {
|
|
510
|
+
const track = project.tracks[trackIndex];
|
|
511
|
+
if (track.hidden || track.locked) continue;
|
|
512
|
+
for (let i = track.elements.length - 1; i >= 0; i--) {
|
|
513
|
+
const raw = track.elements[i];
|
|
514
|
+
if (!isElementActiveAt(raw, timeMs)) continue;
|
|
515
|
+
if (!("transform" in raw)) continue;
|
|
516
|
+
const element = resolveAnimatedElement(raw, timeMs);
|
|
517
|
+
const obb = obbFor(project, element);
|
|
518
|
+
if (!obb || !hitTestOBB(obb, point.x, point.y)) continue;
|
|
519
|
+
engine.select([element.id]);
|
|
520
|
+
gestureRef.current = {
|
|
521
|
+
kind: "move",
|
|
522
|
+
elementId: element.id,
|
|
523
|
+
baseTransform: element.transform,
|
|
524
|
+
baseOBB: obb,
|
|
525
|
+
baseBox: null,
|
|
526
|
+
handle: null,
|
|
527
|
+
start: point
|
|
528
|
+
};
|
|
529
|
+
engine.beginTransaction();
|
|
530
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
engine.clearSelection();
|
|
535
|
+
};
|
|
536
|
+
const handleDoubleClick = (event) => {
|
|
537
|
+
if (!interactive || !doubleClickRef.current) return;
|
|
538
|
+
const point = toProjectPoint(event);
|
|
539
|
+
if (!point) return;
|
|
540
|
+
const project = engine.project;
|
|
541
|
+
const timeMs = engine.playback.state.currentTimeMs;
|
|
542
|
+
for (let trackIndex = project.tracks.length - 1; trackIndex >= 0; trackIndex--) {
|
|
543
|
+
const track = project.tracks[trackIndex];
|
|
544
|
+
if (track.hidden || track.locked) continue;
|
|
545
|
+
for (let i = track.elements.length - 1; i >= 0; i--) {
|
|
546
|
+
const raw = track.elements[i];
|
|
547
|
+
if (!isElementActiveAt(raw, timeMs)) continue;
|
|
548
|
+
if (!("transform" in raw)) continue;
|
|
549
|
+
const element = resolveAnimatedElement(raw, timeMs);
|
|
550
|
+
const obb = obbFor(project, element);
|
|
551
|
+
if (!obb || !hitTestOBB(obb, point.x, point.y)) continue;
|
|
552
|
+
doubleClickRef.current(element.id);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
const handlePointerMove = (event) => {
|
|
558
|
+
const gesture = gestureRef.current;
|
|
559
|
+
if (!gesture) return;
|
|
560
|
+
const point = toProjectPoint(event);
|
|
561
|
+
if (!point) return;
|
|
562
|
+
let transform;
|
|
563
|
+
const feedback = feedbackRef.current;
|
|
564
|
+
if (gesture.kind === "move") {
|
|
565
|
+
transform = applyMove(gesture.baseTransform, gesture.start, point);
|
|
566
|
+
const threshold = CENTER_SNAP_PX * screenToProject();
|
|
567
|
+
feedback.guideVertical = false;
|
|
568
|
+
feedback.guideHorizontal = false;
|
|
569
|
+
if (!event.altKey) {
|
|
570
|
+
if (Math.abs(transform.x) < threshold) {
|
|
571
|
+
transform = {
|
|
572
|
+
...transform,
|
|
573
|
+
x: 0
|
|
574
|
+
};
|
|
575
|
+
feedback.guideVertical = true;
|
|
576
|
+
}
|
|
577
|
+
if (Math.abs(transform.y) < threshold) {
|
|
578
|
+
transform = {
|
|
579
|
+
...transform,
|
|
580
|
+
y: 0
|
|
581
|
+
};
|
|
582
|
+
feedback.guideHorizontal = true;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
feedback.label = null;
|
|
586
|
+
} else if (gesture.kind === "rotate") {
|
|
587
|
+
transform = applyRotate(gesture.baseTransform, gesture.baseOBB, gesture.start, point);
|
|
588
|
+
feedback.label = `${transform.rotation.toFixed(1)}°`;
|
|
589
|
+
feedback.guideVertical = false;
|
|
590
|
+
feedback.guideHorizontal = false;
|
|
591
|
+
} else if (gesture.kind === "resize") {
|
|
592
|
+
transform = applyResize(gesture.baseTransform, gesture.baseOBB, gesture.handle ?? "se", gesture.start, point);
|
|
593
|
+
const width = gesture.baseOBB.width / gesture.baseTransform.scaleX * transform.scaleX;
|
|
594
|
+
const height = gesture.baseOBB.height / gesture.baseTransform.scaleY * transform.scaleY;
|
|
595
|
+
feedback.label = `${Math.round(width)}×${Math.round(height)}`;
|
|
596
|
+
feedback.guideVertical = false;
|
|
597
|
+
feedback.guideHorizontal = false;
|
|
598
|
+
} else {
|
|
599
|
+
const baseBox = gesture.baseBox;
|
|
600
|
+
if (!baseBox || !gesture.handle) return;
|
|
601
|
+
const result = applyBoxResize(gesture.baseTransform, gesture.baseOBB, gesture.handle, point, 12 * screenToProject());
|
|
602
|
+
transform = result.transform;
|
|
603
|
+
const nextWidth = Math.max(1, Math.round(result.displayWidth / gesture.baseTransform.scaleX));
|
|
604
|
+
const nextHeight = Math.max(1, Math.round(result.displayHeight / gesture.baseTransform.scaleY));
|
|
605
|
+
const resizesHeight = gesture.handle.includes("n") || gesture.handle.includes("s");
|
|
606
|
+
const box = {
|
|
607
|
+
width: nextWidth,
|
|
608
|
+
overflow: baseBox.overflow,
|
|
609
|
+
...baseBox.hadHeight || resizesHeight ? { height: nextHeight } : {}
|
|
610
|
+
};
|
|
611
|
+
try {
|
|
612
|
+
applyGestureTransform(engine, gesture.elementId, transform, engine.playback.state.currentTimeMs);
|
|
613
|
+
engine.dispatch({
|
|
614
|
+
type: "updateElement",
|
|
615
|
+
elementId: gesture.elementId,
|
|
616
|
+
patch: { box }
|
|
617
|
+
});
|
|
618
|
+
} catch {
|
|
619
|
+
gestureRef.current = null;
|
|
620
|
+
engine.endTransaction();
|
|
621
|
+
}
|
|
622
|
+
feedback.label = `${Math.round(result.displayWidth)}×${Math.round(result.displayHeight)}`;
|
|
623
|
+
feedback.guideVertical = false;
|
|
624
|
+
feedback.guideHorizontal = false;
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
applyGestureTransform(engine, gesture.elementId, transform, engine.playback.state.currentTimeMs);
|
|
629
|
+
} catch {
|
|
630
|
+
gestureRef.current = null;
|
|
631
|
+
engine.endTransaction();
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
const endGesture = (event) => {
|
|
635
|
+
if (!gestureRef.current) return;
|
|
636
|
+
gestureRef.current = null;
|
|
637
|
+
feedbackRef.current = {
|
|
638
|
+
guideVertical: false,
|
|
639
|
+
guideHorizontal: false,
|
|
640
|
+
label: null
|
|
641
|
+
};
|
|
642
|
+
engine.endTransaction();
|
|
643
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
644
|
+
};
|
|
645
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
646
|
+
ref: containerRef,
|
|
647
|
+
className,
|
|
648
|
+
style: {
|
|
649
|
+
position: "relative",
|
|
650
|
+
aspectRatio: "var(--mcut-aspect, 16 / 9)",
|
|
651
|
+
width: "100%"
|
|
652
|
+
},
|
|
653
|
+
"data-mcut-player": "",
|
|
654
|
+
children: [/* @__PURE__ */ jsx("canvas", {
|
|
655
|
+
ref: renderCanvasRef,
|
|
656
|
+
style: {
|
|
657
|
+
position: "absolute",
|
|
658
|
+
inset: 0,
|
|
659
|
+
width: "100%",
|
|
660
|
+
height: "100%"
|
|
661
|
+
}
|
|
662
|
+
}, renderer), /* @__PURE__ */ jsx("canvas", {
|
|
663
|
+
ref: overlayCanvasRef,
|
|
664
|
+
style: {
|
|
665
|
+
position: "absolute",
|
|
666
|
+
inset: 0,
|
|
667
|
+
width: "100%",
|
|
668
|
+
height: "100%",
|
|
669
|
+
touchAction: "none",
|
|
670
|
+
cursor: interactive ? "default" : void 0
|
|
671
|
+
},
|
|
672
|
+
onPointerDown: handlePointerDown,
|
|
673
|
+
onPointerMove: handlePointerMove,
|
|
674
|
+
onPointerUp: endGesture,
|
|
675
|
+
onPointerCancel: endGesture,
|
|
676
|
+
onDoubleClick: handleDoubleClick
|
|
677
|
+
})]
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
//#endregion
|
|
681
|
+
export { EditorProvider, PlayerCanvas, applyBoxResize, applyMove, applyResize, applyRotate, useEditor, useEditorContext, useEditorState, usePlayback, useProject, useSelectedElement, useSelection };
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mcut/react",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "React bindings for mcut: EditorProvider, hooks, PlayerCanvas with selection overlay, playback clock, audio preview",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsdown",
|
|
21
|
+
"dev": "tsdown --watch --no-clean",
|
|
22
|
+
"typecheck": "tsc --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/mattppal/mcut.git",
|
|
27
|
+
"directory": "packages/react"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@mcut/compositor": "0.1.0-alpha.0",
|
|
31
|
+
"@mcut/media": "0.1.0-alpha.0",
|
|
32
|
+
"@mcut/timeline": "0.1.0-alpha.0",
|
|
33
|
+
"@tanstack/react-store": "0.11.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": ">=19",
|
|
37
|
+
"react-dom": ">=19"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"provenance": true
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/react": "19.2.17",
|
|
45
|
+
"@types/react-dom": "19.2.3"
|
|
46
|
+
},
|
|
47
|
+
"author": "Matt Palmer",
|
|
48
|
+
"homepage": "https://github.com/mattppal/mcut/tree/main/packages/react#readme",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/mattppal/mcut/issues"
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"mcut",
|
|
54
|
+
"video",
|
|
55
|
+
"video-editing",
|
|
56
|
+
"timeline",
|
|
57
|
+
"typescript",
|
|
58
|
+
"react",
|
|
59
|
+
"hooks",
|
|
60
|
+
"player"
|
|
61
|
+
]
|
|
62
|
+
}
|