@twick/2d 0.14.0 → 1.14.3
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 +21 -21
- package/editor/editor/tsconfig.build.tsbuildinfo +1 -1
- package/lib/components/Audio.d.ts.map +1 -1
- package/lib/components/Audio.js +33 -3
- package/lib/components/CodeBlock.d.ts +1 -1
- package/lib/components/Img.js +23 -23
- package/lib/components/Line.js +31 -31
- package/lib/components/Media.d.ts +6 -0
- package/lib/components/Media.d.ts.map +1 -1
- package/lib/components/Media.js +277 -61
- package/lib/components/Node.d.ts +1 -1
- package/lib/components/Path.d.ts +1 -1
- package/lib/components/SVG.d.ts +1 -1
- package/lib/components/Shape.d.ts +1 -1
- package/lib/components/Spline.js +25 -25
- package/lib/components/Video.d.ts +0 -1
- package/lib/components/Video.d.ts.map +1 -1
- package/lib/components/Video.js +70 -65
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +4 -5
- package/src/editor/NodeInspectorConfig.tsx +76 -76
- package/src/editor/PreviewOverlayConfig.tsx +67 -67
- package/src/editor/Provider.tsx +93 -93
- package/src/editor/SceneGraphTabConfig.tsx +81 -81
- package/src/editor/icons/CircleIcon.tsx +7 -7
- package/src/editor/icons/CodeBlockIcon.tsx +8 -8
- package/src/editor/icons/CurveIcon.tsx +7 -7
- package/src/editor/icons/GridIcon.tsx +7 -7
- package/src/editor/icons/IconMap.ts +35 -35
- package/src/editor/icons/ImgIcon.tsx +8 -8
- package/src/editor/icons/LayoutIcon.tsx +9 -9
- package/src/editor/icons/LineIcon.tsx +7 -7
- package/src/editor/icons/NodeIcon.tsx +7 -7
- package/src/editor/icons/RayIcon.tsx +7 -7
- package/src/editor/icons/RectIcon.tsx +7 -7
- package/src/editor/icons/ShapeIcon.tsx +7 -7
- package/src/editor/icons/TxtIcon.tsx +8 -8
- package/src/editor/icons/VideoIcon.tsx +7 -7
- package/src/editor/icons/View2DIcon.tsx +10 -10
- package/src/editor/index.ts +17 -17
- package/src/editor/tree/DetachedRoot.tsx +23 -23
- package/src/editor/tree/NodeElement.tsx +74 -74
- package/src/editor/tree/TreeElement.tsx +72 -72
- package/src/editor/tree/TreeRoot.tsx +10 -10
- package/src/editor/tree/ViewRoot.tsx +20 -20
- package/src/editor/tree/index.module.scss +38 -38
- package/src/editor/tree/index.ts +3 -3
- package/src/editor/tsconfig.build.json +5 -5
- package/src/editor/tsconfig.json +12 -12
- package/src/editor/tsdoc.json +4 -4
- package/src/editor/vite-env.d.ts +1 -1
- package/src/lib/code/CodeCursor.ts +445 -445
- package/src/lib/code/CodeDiffer.ts +78 -78
- package/src/lib/code/CodeFragment.ts +97 -97
- package/src/lib/code/CodeHighlighter.ts +75 -75
- package/src/lib/code/CodeMetrics.ts +47 -47
- package/src/lib/code/CodeRange.test.ts +74 -74
- package/src/lib/code/CodeRange.ts +216 -216
- package/src/lib/code/CodeScope.ts +101 -101
- package/src/lib/code/CodeSelection.ts +24 -24
- package/src/lib/code/CodeSignal.ts +327 -327
- package/src/lib/code/CodeTokenizer.ts +54 -54
- package/src/lib/code/DefaultHighlightStyle.ts +98 -98
- package/src/lib/code/LezerHighlighter.ts +113 -113
- package/src/lib/code/diff.test.ts +311 -311
- package/src/lib/code/diff.ts +319 -319
- package/src/lib/code/extractRange.ts +126 -126
- package/src/lib/code/index.ts +13 -13
- package/src/lib/components/Audio.ts +168 -131
- package/src/lib/components/Bezier.ts +105 -105
- package/src/lib/components/Circle.ts +266 -266
- package/src/lib/components/Code.ts +526 -526
- package/src/lib/components/CodeBlock.ts +576 -576
- package/src/lib/components/CubicBezier.ts +112 -112
- package/src/lib/components/Curve.ts +455 -455
- package/src/lib/components/Grid.ts +135 -135
- package/src/lib/components/Icon.ts +96 -96
- package/src/lib/components/Img.ts +319 -319
- package/src/lib/components/Knot.ts +157 -157
- package/src/lib/components/Latex.ts +122 -122
- package/src/lib/components/Layout.ts +1092 -1092
- package/src/lib/components/Line.ts +429 -429
- package/src/lib/components/Media.ts +576 -346
- package/src/lib/components/Node.ts +1940 -1940
- package/src/lib/components/Path.ts +137 -137
- package/src/lib/components/Polygon.ts +171 -171
- package/src/lib/components/QuadBezier.ts +100 -100
- package/src/lib/components/Ray.ts +125 -125
- package/src/lib/components/Rect.ts +187 -187
- package/src/lib/components/Rive.ts +156 -156
- package/src/lib/components/SVG.ts +797 -797
- package/src/lib/components/Shape.ts +143 -143
- package/src/lib/components/Spline.ts +344 -344
- package/src/lib/components/Txt.test.tsx +81 -81
- package/src/lib/components/Txt.ts +203 -203
- package/src/lib/components/TxtLeaf.ts +205 -205
- package/src/lib/components/Video.ts +461 -462
- package/src/lib/components/View2D.ts +98 -98
- package/src/lib/components/__tests__/children.test.tsx +142 -142
- package/src/lib/components/__tests__/clone.test.tsx +126 -126
- package/src/lib/components/__tests__/generatorTest.ts +28 -28
- package/src/lib/components/__tests__/mockScene2D.ts +45 -45
- package/src/lib/components/__tests__/query.test.tsx +122 -122
- package/src/lib/components/__tests__/state.test.tsx +60 -60
- package/src/lib/components/index.ts +28 -28
- package/src/lib/components/types.ts +35 -35
- package/src/lib/curves/ArcSegment.ts +159 -159
- package/src/lib/curves/CircleSegment.ts +77 -77
- package/src/lib/curves/CubicBezierSegment.ts +78 -78
- package/src/lib/curves/CurveDrawingInfo.ts +11 -11
- package/src/lib/curves/CurvePoint.ts +15 -15
- package/src/lib/curves/CurveProfile.ts +7 -7
- package/src/lib/curves/KnotInfo.ts +10 -10
- package/src/lib/curves/LineSegment.ts +62 -62
- package/src/lib/curves/Polynomial.ts +355 -355
- package/src/lib/curves/Polynomial2D.ts +62 -62
- package/src/lib/curves/PolynomialSegment.ts +124 -124
- package/src/lib/curves/QuadBezierSegment.ts +64 -64
- package/src/lib/curves/Segment.ts +17 -17
- package/src/lib/curves/UniformPolynomialCurveSampler.ts +94 -94
- package/src/lib/curves/createCurveProfileLerp.ts +471 -471
- package/src/lib/curves/getBezierSplineProfile.ts +223 -223
- package/src/lib/curves/getCircleProfile.ts +86 -86
- package/src/lib/curves/getPathProfile.ts +178 -178
- package/src/lib/curves/getPointAtDistance.ts +21 -21
- package/src/lib/curves/getPolylineProfile.test.ts +21 -21
- package/src/lib/curves/getPolylineProfile.ts +89 -89
- package/src/lib/curves/getRectProfile.ts +139 -139
- package/src/lib/curves/index.ts +16 -16
- package/src/lib/decorators/canvasStyleSignal.ts +16 -16
- package/src/lib/decorators/colorSignal.ts +9 -9
- package/src/lib/decorators/compound.ts +72 -72
- package/src/lib/decorators/computed.ts +18 -18
- package/src/lib/decorators/defaultStyle.ts +18 -18
- package/src/lib/decorators/filtersSignal.ts +136 -136
- package/src/lib/decorators/index.ts +10 -10
- package/src/lib/decorators/initializers.ts +32 -32
- package/src/lib/decorators/nodeName.ts +13 -13
- package/src/lib/decorators/signal.test.ts +90 -90
- package/src/lib/decorators/signal.ts +345 -345
- package/src/lib/decorators/spacingSignal.ts +15 -15
- package/src/lib/decorators/vector2Signal.ts +30 -30
- package/src/lib/globals.d.ts +2 -2
- package/src/lib/index.ts +8 -8
- package/src/lib/jsx-dev-runtime.ts +2 -2
- package/src/lib/jsx-runtime.ts +46 -46
- package/src/lib/parse-svg-path.d.ts +14 -14
- package/src/lib/partials/Filter.ts +180 -180
- package/src/lib/partials/Gradient.ts +102 -102
- package/src/lib/partials/Pattern.ts +34 -34
- package/src/lib/partials/ShaderConfig.ts +117 -117
- package/src/lib/partials/index.ts +4 -4
- package/src/lib/partials/types.ts +58 -58
- package/src/lib/scenes/Scene2D.ts +242 -242
- package/src/lib/scenes/index.ts +3 -3
- package/src/lib/scenes/makeScene2D.ts +16 -16
- package/src/lib/scenes/useScene2D.ts +6 -6
- package/src/lib/tsconfig.build.json +5 -5
- package/src/lib/tsconfig.json +10 -10
- package/src/lib/tsdoc.json +4 -4
- package/src/lib/utils/CanvasUtils.ts +306 -306
- package/src/lib/utils/diff.test.ts +453 -453
- package/src/lib/utils/diff.ts +148 -148
- package/src/lib/utils/index.ts +2 -2
- package/src/lib/utils/is.ts +11 -11
- package/src/lib/utils/makeSignalExtensions.ts +30 -30
- package/src/lib/utils/video/declarations.d.ts +1 -1
- package/src/lib/utils/video/ffmpeg-client.ts +50 -50
- package/src/lib/utils/video/mp4-parser-manager.ts +72 -72
- package/src/lib/utils/video/parser/index.ts +1 -1
- package/src/lib/utils/video/parser/parser.ts +257 -257
- package/src/lib/utils/video/parser/sampler.ts +72 -72
- package/src/lib/utils/video/parser/segment.ts +302 -302
- package/src/lib/utils/video/parser/sink.ts +29 -29
- package/src/lib/utils/video/parser/utils.ts +31 -31
- package/src/tsconfig.base.json +19 -19
- package/src/tsconfig.build.json +8 -8
- package/src/tsconfig.json +5 -5
- package/tsconfig.project.json +7 -7
- package/lib/components/utils/waitUntil.d.ts +0 -7
- package/lib/components/utils/waitUntil.d.ts.map +0 -1
- package/lib/components/utils/waitUntil.js +0 -15
- package/lib/utils/waitUntil.d.ts +0 -7
- package/lib/utils/waitUntil.d.ts.map +0 -1
- package/lib/utils/waitUntil.js +0 -15
- package/src/lib/utils/waitUntil.ts +0 -18
package/src/lib/utils/diff.ts
CHANGED
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
interface TransformDiff<T> {
|
|
2
|
-
inserted: TransformDiffItem<T>[];
|
|
3
|
-
deleted: TransformDiffItem<T>[];
|
|
4
|
-
transformed: TransformDiffItemTransformed<T>[];
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface TransformDiffItem<T> {
|
|
8
|
-
before?: T;
|
|
9
|
-
beforeIdIndex: number;
|
|
10
|
-
current: T;
|
|
11
|
-
currentIndex: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface TransformDiffItemTransformed<T> {
|
|
15
|
-
insert: boolean;
|
|
16
|
-
remove: boolean;
|
|
17
|
-
from: TransformDiffItem<T>;
|
|
18
|
-
to: TransformDiffItem<T>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface ApplyTransformInserted<T> {
|
|
22
|
-
item: TransformDiffItem<T>;
|
|
23
|
-
order: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface ApplyTransformResult<T> {
|
|
27
|
-
inserted: ApplyTransformInserted<T>[];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface Idable {
|
|
31
|
-
id: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function getIdMap<T extends Idable>(list: T[]) {
|
|
35
|
-
const map = new Map<string, TransformDiffItem<T>[]>();
|
|
36
|
-
let before: T | undefined = undefined;
|
|
37
|
-
for (const [index, current] of list.entries()) {
|
|
38
|
-
const currentArray = map.get(current.id) ?? [];
|
|
39
|
-
if (!map.has(current.id)) {
|
|
40
|
-
map.set(current.id, currentArray);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
currentArray.push({
|
|
44
|
-
before,
|
|
45
|
-
current,
|
|
46
|
-
beforeIdIndex: before ? map.get(before.id)!.length - 1 : -1,
|
|
47
|
-
currentIndex: index,
|
|
48
|
-
});
|
|
49
|
-
before = current;
|
|
50
|
-
}
|
|
51
|
-
return map;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function getTransformDiff<T extends Idable>(
|
|
55
|
-
from: T[],
|
|
56
|
-
to: T[],
|
|
57
|
-
): TransformDiff<T> {
|
|
58
|
-
const diff: TransformDiff<T> = {
|
|
59
|
-
inserted: [],
|
|
60
|
-
deleted: [],
|
|
61
|
-
transformed: [],
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const fromMap = getIdMap(from);
|
|
65
|
-
const toMap = getIdMap(to);
|
|
66
|
-
|
|
67
|
-
for (const [key, fromItem] of fromMap.entries()) {
|
|
68
|
-
const toItem = toMap.get(key);
|
|
69
|
-
if (toItem) {
|
|
70
|
-
toMap.delete(key);
|
|
71
|
-
for (let i = 0; i < Math.max(fromItem.length, toItem.length); i++) {
|
|
72
|
-
const insert = i >= fromItem.length;
|
|
73
|
-
const remove = i >= toItem.length;
|
|
74
|
-
|
|
75
|
-
const fromNode = !insert ? fromItem[i] : fromItem[fromItem.length - 1];
|
|
76
|
-
const toNode = !remove ? toItem[i] : toItem[toItem.length - 1];
|
|
77
|
-
|
|
78
|
-
diff.transformed.push({
|
|
79
|
-
insert,
|
|
80
|
-
remove,
|
|
81
|
-
from: fromNode,
|
|
82
|
-
to: toNode,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
} else {
|
|
86
|
-
for (const node of fromItem) {
|
|
87
|
-
diff.deleted.push(node);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
for (const toItem of toMap.values()) {
|
|
93
|
-
for (const node of toItem) {
|
|
94
|
-
diff.inserted.push(node);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return diff;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function applyTransformDiff<T extends Idable>(
|
|
102
|
-
current: T[],
|
|
103
|
-
diff: TransformDiff<T>,
|
|
104
|
-
cloner: (original: T) => T,
|
|
105
|
-
): ApplyTransformResult<T> {
|
|
106
|
-
function insert(item: TransformDiffItem<T>) {
|
|
107
|
-
let idIndex = -1;
|
|
108
|
-
const index = item.before
|
|
109
|
-
? current.findIndex(({id}) => {
|
|
110
|
-
if (id === item.before?.id) {
|
|
111
|
-
idIndex++;
|
|
112
|
-
if (idIndex === item.beforeIdIndex) return true;
|
|
113
|
-
}
|
|
114
|
-
return false;
|
|
115
|
-
})
|
|
116
|
-
: 0;
|
|
117
|
-
current.splice(index + 1, 0, item.current);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const result: ApplyTransformResult<T> = {
|
|
121
|
-
inserted: diff.inserted.map(item => ({
|
|
122
|
-
item,
|
|
123
|
-
order: item.currentIndex,
|
|
124
|
-
})),
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
for (const item of diff.transformed) {
|
|
128
|
-
if (!item.insert) continue;
|
|
129
|
-
|
|
130
|
-
const from = item.from;
|
|
131
|
-
item.from = {
|
|
132
|
-
...item.to,
|
|
133
|
-
current: cloner(from.current),
|
|
134
|
-
};
|
|
135
|
-
result.inserted.push({
|
|
136
|
-
item: item.from,
|
|
137
|
-
order: item.to.currentIndex,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
result.inserted.sort((a, b) => a.order - b.order);
|
|
142
|
-
|
|
143
|
-
for (const item of result.inserted) {
|
|
144
|
-
insert(item.item);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
1
|
+
interface TransformDiff<T> {
|
|
2
|
+
inserted: TransformDiffItem<T>[];
|
|
3
|
+
deleted: TransformDiffItem<T>[];
|
|
4
|
+
transformed: TransformDiffItemTransformed<T>[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface TransformDiffItem<T> {
|
|
8
|
+
before?: T;
|
|
9
|
+
beforeIdIndex: number;
|
|
10
|
+
current: T;
|
|
11
|
+
currentIndex: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface TransformDiffItemTransformed<T> {
|
|
15
|
+
insert: boolean;
|
|
16
|
+
remove: boolean;
|
|
17
|
+
from: TransformDiffItem<T>;
|
|
18
|
+
to: TransformDiffItem<T>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ApplyTransformInserted<T> {
|
|
22
|
+
item: TransformDiffItem<T>;
|
|
23
|
+
order: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ApplyTransformResult<T> {
|
|
27
|
+
inserted: ApplyTransformInserted<T>[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Idable {
|
|
31
|
+
id: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getIdMap<T extends Idable>(list: T[]) {
|
|
35
|
+
const map = new Map<string, TransformDiffItem<T>[]>();
|
|
36
|
+
let before: T | undefined = undefined;
|
|
37
|
+
for (const [index, current] of list.entries()) {
|
|
38
|
+
const currentArray = map.get(current.id) ?? [];
|
|
39
|
+
if (!map.has(current.id)) {
|
|
40
|
+
map.set(current.id, currentArray);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
currentArray.push({
|
|
44
|
+
before,
|
|
45
|
+
current,
|
|
46
|
+
beforeIdIndex: before ? map.get(before.id)!.length - 1 : -1,
|
|
47
|
+
currentIndex: index,
|
|
48
|
+
});
|
|
49
|
+
before = current;
|
|
50
|
+
}
|
|
51
|
+
return map;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function getTransformDiff<T extends Idable>(
|
|
55
|
+
from: T[],
|
|
56
|
+
to: T[],
|
|
57
|
+
): TransformDiff<T> {
|
|
58
|
+
const diff: TransformDiff<T> = {
|
|
59
|
+
inserted: [],
|
|
60
|
+
deleted: [],
|
|
61
|
+
transformed: [],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const fromMap = getIdMap(from);
|
|
65
|
+
const toMap = getIdMap(to);
|
|
66
|
+
|
|
67
|
+
for (const [key, fromItem] of fromMap.entries()) {
|
|
68
|
+
const toItem = toMap.get(key);
|
|
69
|
+
if (toItem) {
|
|
70
|
+
toMap.delete(key);
|
|
71
|
+
for (let i = 0; i < Math.max(fromItem.length, toItem.length); i++) {
|
|
72
|
+
const insert = i >= fromItem.length;
|
|
73
|
+
const remove = i >= toItem.length;
|
|
74
|
+
|
|
75
|
+
const fromNode = !insert ? fromItem[i] : fromItem[fromItem.length - 1];
|
|
76
|
+
const toNode = !remove ? toItem[i] : toItem[toItem.length - 1];
|
|
77
|
+
|
|
78
|
+
diff.transformed.push({
|
|
79
|
+
insert,
|
|
80
|
+
remove,
|
|
81
|
+
from: fromNode,
|
|
82
|
+
to: toNode,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
for (const node of fromItem) {
|
|
87
|
+
diff.deleted.push(node);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (const toItem of toMap.values()) {
|
|
93
|
+
for (const node of toItem) {
|
|
94
|
+
diff.inserted.push(node);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return diff;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function applyTransformDiff<T extends Idable>(
|
|
102
|
+
current: T[],
|
|
103
|
+
diff: TransformDiff<T>,
|
|
104
|
+
cloner: (original: T) => T,
|
|
105
|
+
): ApplyTransformResult<T> {
|
|
106
|
+
function insert(item: TransformDiffItem<T>) {
|
|
107
|
+
let idIndex = -1;
|
|
108
|
+
const index = item.before
|
|
109
|
+
? current.findIndex(({id}) => {
|
|
110
|
+
if (id === item.before?.id) {
|
|
111
|
+
idIndex++;
|
|
112
|
+
if (idIndex === item.beforeIdIndex) return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
})
|
|
116
|
+
: 0;
|
|
117
|
+
current.splice(index + 1, 0, item.current);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const result: ApplyTransformResult<T> = {
|
|
121
|
+
inserted: diff.inserted.map(item => ({
|
|
122
|
+
item,
|
|
123
|
+
order: item.currentIndex,
|
|
124
|
+
})),
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
for (const item of diff.transformed) {
|
|
128
|
+
if (!item.insert) continue;
|
|
129
|
+
|
|
130
|
+
const from = item.from;
|
|
131
|
+
item.from = {
|
|
132
|
+
...item.to,
|
|
133
|
+
current: cloner(from.current),
|
|
134
|
+
};
|
|
135
|
+
result.inserted.push({
|
|
136
|
+
item: item.from,
|
|
137
|
+
order: item.to.currentIndex,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
result.inserted.sort((a, b) => a.order - b.order);
|
|
142
|
+
|
|
143
|
+
for (const item of result.inserted) {
|
|
144
|
+
insert(item.item);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return result;
|
|
148
|
+
}
|
package/src/lib/utils/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './CanvasUtils';
|
|
2
|
-
export * from './is';
|
|
1
|
+
export * from './CanvasUtils';
|
|
2
|
+
export * from './is';
|
package/src/lib/utils/is.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create a predicate that checks if the given object is an instance of the
|
|
3
|
-
* given class.
|
|
4
|
-
*
|
|
5
|
-
* @param klass - The class to check against.
|
|
6
|
-
*/
|
|
7
|
-
export function is<T>(
|
|
8
|
-
klass: new (...args: any[]) => T,
|
|
9
|
-
): (object: any) => object is T {
|
|
10
|
-
return (object): object is T => object instanceof klass;
|
|
11
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Create a predicate that checks if the given object is an instance of the
|
|
3
|
+
* given class.
|
|
4
|
+
*
|
|
5
|
+
* @param klass - The class to check against.
|
|
6
|
+
*/
|
|
7
|
+
export function is<T>(
|
|
8
|
+
klass: new (...args: any[]) => T,
|
|
9
|
+
): (object: any) => object is T {
|
|
10
|
+
return (object): object is T => object instanceof klass;
|
|
11
|
+
}
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import type {SignalExtensions} from '@twick/core';
|
|
2
|
-
import {capitalize} from '@twick/core';
|
|
3
|
-
import type {PropertyMetadata} from '../decorators';
|
|
4
|
-
|
|
5
|
-
export function makeSignalExtensions<TSetterValue, TValue extends TSetterValue>(
|
|
6
|
-
meta: Partial<PropertyMetadata<TValue>> = {},
|
|
7
|
-
owner?: any,
|
|
8
|
-
name?: string,
|
|
9
|
-
) {
|
|
10
|
-
const extensions: Partial<SignalExtensions<TSetterValue, TValue>> = {};
|
|
11
|
-
|
|
12
|
-
if (name && owner) {
|
|
13
|
-
const setter = meta.setter ?? owner?.[`set${capitalize(name)}`];
|
|
14
|
-
if (setter) {
|
|
15
|
-
extensions.setter = setter.bind(owner);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const getter = meta.getter ?? owner?.[`get${capitalize(name)}`];
|
|
19
|
-
if (getter) {
|
|
20
|
-
extensions.getter = getter.bind(owner);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const tweener = meta.tweener ?? owner?.[`tween${capitalize(name)}`];
|
|
24
|
-
if (tweener) {
|
|
25
|
-
extensions.tweener = tweener.bind(owner);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return extensions;
|
|
30
|
-
}
|
|
1
|
+
import type {SignalExtensions} from '@twick/core';
|
|
2
|
+
import {capitalize} from '@twick/core';
|
|
3
|
+
import type {PropertyMetadata} from '../decorators';
|
|
4
|
+
|
|
5
|
+
export function makeSignalExtensions<TSetterValue, TValue extends TSetterValue>(
|
|
6
|
+
meta: Partial<PropertyMetadata<TValue>> = {},
|
|
7
|
+
owner?: any,
|
|
8
|
+
name?: string,
|
|
9
|
+
) {
|
|
10
|
+
const extensions: Partial<SignalExtensions<TSetterValue, TValue>> = {};
|
|
11
|
+
|
|
12
|
+
if (name && owner) {
|
|
13
|
+
const setter = meta.setter ?? owner?.[`set${capitalize(name)}`];
|
|
14
|
+
if (setter) {
|
|
15
|
+
extensions.setter = setter.bind(owner);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const getter = meta.getter ?? owner?.[`get${capitalize(name)}`];
|
|
19
|
+
if (getter) {
|
|
20
|
+
extensions.getter = getter.bind(owner);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const tweener = meta.tweener ?? owner?.[`tween${capitalize(name)}`];
|
|
24
|
+
if (tweener) {
|
|
25
|
+
extensions.tweener = tweener.bind(owner);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return extensions;
|
|
30
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
declare module 'mp4box';
|
|
1
|
+
declare module 'mp4box';
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
export class ImageCommunication {
|
|
2
|
-
public constructor() {
|
|
3
|
-
if (!import.meta.hot) {
|
|
4
|
-
throw new Error('FfmpegVideoFrame can only be used with HMR.');
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
public async getFrame(
|
|
9
|
-
id: string,
|
|
10
|
-
src: string,
|
|
11
|
-
time: number,
|
|
12
|
-
duration: number,
|
|
13
|
-
fps: number,
|
|
14
|
-
) {
|
|
15
|
-
const response = await fetch('/twick-ffmpeg-decoder/video-frame', {
|
|
16
|
-
method: 'POST',
|
|
17
|
-
headers: {
|
|
18
|
-
// eslint-disable-next-line
|
|
19
|
-
'Content-Type': 'application/json',
|
|
20
|
-
},
|
|
21
|
-
body: JSON.stringify({
|
|
22
|
-
id,
|
|
23
|
-
filePath: src,
|
|
24
|
-
startTime: time,
|
|
25
|
-
duration,
|
|
26
|
-
fps,
|
|
27
|
-
}),
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
if (!response.ok) {
|
|
31
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const width = parseInt(response.headers.get('X-Frame-Width') || '1080', 10);
|
|
35
|
-
const height = parseInt(
|
|
36
|
-
response.headers.get('X-Frame-Height') || '1920',
|
|
37
|
-
10,
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
41
|
-
|
|
42
|
-
const imageData = new ImageData(
|
|
43
|
-
new Uint8ClampedArray(arrayBuffer),
|
|
44
|
-
width,
|
|
45
|
-
height,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
return createImageBitmap(imageData);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
export class ImageCommunication {
|
|
2
|
+
public constructor() {
|
|
3
|
+
if (!import.meta.hot) {
|
|
4
|
+
throw new Error('FfmpegVideoFrame can only be used with HMR.');
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
public async getFrame(
|
|
9
|
+
id: string,
|
|
10
|
+
src: string,
|
|
11
|
+
time: number,
|
|
12
|
+
duration: number,
|
|
13
|
+
fps: number,
|
|
14
|
+
) {
|
|
15
|
+
const response = await fetch('/twick-ffmpeg-decoder/video-frame', {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
headers: {
|
|
18
|
+
// eslint-disable-next-line
|
|
19
|
+
'Content-Type': 'application/json',
|
|
20
|
+
},
|
|
21
|
+
body: JSON.stringify({
|
|
22
|
+
id,
|
|
23
|
+
filePath: src,
|
|
24
|
+
startTime: time,
|
|
25
|
+
duration,
|
|
26
|
+
fps,
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const width = parseInt(response.headers.get('X-Frame-Width') || '1080', 10);
|
|
35
|
+
const height = parseInt(
|
|
36
|
+
response.headers.get('X-Frame-Height') || '1920',
|
|
37
|
+
10,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
41
|
+
|
|
42
|
+
const imageData = new ImageData(
|
|
43
|
+
new Uint8ClampedArray(arrayBuffer),
|
|
44
|
+
width,
|
|
45
|
+
height,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return createImageBitmap(imageData);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import {Mp4Parser} from './parser';
|
|
2
|
-
|
|
3
|
-
// List of VideoFrameExtractors
|
|
4
|
-
const videoFrameExtractors = new Map<string, Mp4Parser>();
|
|
5
|
-
|
|
6
|
-
export async function dropExtractor(id: string, filePath: string) {
|
|
7
|
-
const extractorId = filePath + '-' + id;
|
|
8
|
-
const extractor = videoFrameExtractors.get(extractorId);
|
|
9
|
-
if (extractor) {
|
|
10
|
-
extractor.close();
|
|
11
|
-
videoFrameExtractors.delete(extractorId);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export async function getFrame(
|
|
16
|
-
id: string,
|
|
17
|
-
filePath: string,
|
|
18
|
-
time: number,
|
|
19
|
-
fps: number,
|
|
20
|
-
) {
|
|
21
|
-
// Check if we already have a VideoFrameExtractor for this video
|
|
22
|
-
const extractorId = filePath + '-' + id;
|
|
23
|
-
let extractor = videoFrameExtractors.get(extractorId);
|
|
24
|
-
|
|
25
|
-
const frameDuration = 1 / fps;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Sometimes, HTMLVideoElement.duration is not accurate, which can lead to the
|
|
29
|
-
* requested time being greater than the duration.
|
|
30
|
-
* To prevent this, we clamp the time to the duration reported by the extractor.
|
|
31
|
-
*/
|
|
32
|
-
const duration = extractor?.getDuration();
|
|
33
|
-
if (duration && time > duration) {
|
|
34
|
-
time = duration;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const isOldFrame =
|
|
38
|
-
extractor && Math.abs(time - extractor.getLastTime()) < frameDuration / 2;
|
|
39
|
-
|
|
40
|
-
// If time has not changed, return the last frame
|
|
41
|
-
if (extractor && isOldFrame) {
|
|
42
|
-
const lastFrame = extractor.getLastFrame();
|
|
43
|
-
if (!lastFrame) {
|
|
44
|
-
throw new Error('No last frame');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return lastFrame;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// If the video has skipped back we need to create a new extractor
|
|
51
|
-
if (extractor && time + frameDuration < extractor.getTime()) {
|
|
52
|
-
extractor.close();
|
|
53
|
-
videoFrameExtractors.delete(extractorId);
|
|
54
|
-
extractor = undefined;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// If the video has skipped forward we need to create a new extractor
|
|
58
|
-
if (extractor && time > extractor.getTime() + frameDuration * 1.5) {
|
|
59
|
-
extractor.close();
|
|
60
|
-
videoFrameExtractors.delete(extractorId);
|
|
61
|
-
extractor = undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!extractor) {
|
|
65
|
-
extractor = new Mp4Parser(filePath, fps, time);
|
|
66
|
-
await extractor.start();
|
|
67
|
-
videoFrameExtractors.set(extractorId, extractor);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Go to the frame that is closest to the requested time
|
|
71
|
-
return extractor.getNextFrame();
|
|
72
|
-
}
|
|
1
|
+
import {Mp4Parser} from './parser';
|
|
2
|
+
|
|
3
|
+
// List of VideoFrameExtractors
|
|
4
|
+
const videoFrameExtractors = new Map<string, Mp4Parser>();
|
|
5
|
+
|
|
6
|
+
export async function dropExtractor(id: string, filePath: string) {
|
|
7
|
+
const extractorId = filePath + '-' + id;
|
|
8
|
+
const extractor = videoFrameExtractors.get(extractorId);
|
|
9
|
+
if (extractor) {
|
|
10
|
+
extractor.close();
|
|
11
|
+
videoFrameExtractors.delete(extractorId);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function getFrame(
|
|
16
|
+
id: string,
|
|
17
|
+
filePath: string,
|
|
18
|
+
time: number,
|
|
19
|
+
fps: number,
|
|
20
|
+
) {
|
|
21
|
+
// Check if we already have a VideoFrameExtractor for this video
|
|
22
|
+
const extractorId = filePath + '-' + id;
|
|
23
|
+
let extractor = videoFrameExtractors.get(extractorId);
|
|
24
|
+
|
|
25
|
+
const frameDuration = 1 / fps;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Sometimes, HTMLVideoElement.duration is not accurate, which can lead to the
|
|
29
|
+
* requested time being greater than the duration.
|
|
30
|
+
* To prevent this, we clamp the time to the duration reported by the extractor.
|
|
31
|
+
*/
|
|
32
|
+
const duration = extractor?.getDuration();
|
|
33
|
+
if (duration && time > duration) {
|
|
34
|
+
time = duration;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const isOldFrame =
|
|
38
|
+
extractor && Math.abs(time - extractor.getLastTime()) < frameDuration / 2;
|
|
39
|
+
|
|
40
|
+
// If time has not changed, return the last frame
|
|
41
|
+
if (extractor && isOldFrame) {
|
|
42
|
+
const lastFrame = extractor.getLastFrame();
|
|
43
|
+
if (!lastFrame) {
|
|
44
|
+
throw new Error('No last frame');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return lastFrame;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// If the video has skipped back we need to create a new extractor
|
|
51
|
+
if (extractor && time + frameDuration < extractor.getTime()) {
|
|
52
|
+
extractor.close();
|
|
53
|
+
videoFrameExtractors.delete(extractorId);
|
|
54
|
+
extractor = undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// If the video has skipped forward we need to create a new extractor
|
|
58
|
+
if (extractor && time > extractor.getTime() + frameDuration * 1.5) {
|
|
59
|
+
extractor.close();
|
|
60
|
+
videoFrameExtractors.delete(extractorId);
|
|
61
|
+
extractor = undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!extractor) {
|
|
65
|
+
extractor = new Mp4Parser(filePath, fps, time);
|
|
66
|
+
await extractor.start();
|
|
67
|
+
videoFrameExtractors.set(extractorId, extractor);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Go to the frame that is closest to the requested time
|
|
71
|
+
return extractor.getNextFrame();
|
|
72
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './parser';
|
|
1
|
+
export * from './parser';
|