@canvas-commons/2d 0.2.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 +22 -0
- package/editor/index.css +40 -0
- package/editor/index.js +525 -0
- package/editor/index.js.map +1 -0
- package/lib/index-OvpZ-GsA.d.ts +5673 -0
- package/lib/index-OvpZ-GsA.d.ts.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +10326 -0
- package/lib/index.js.map +1 -0
- package/lib/jsx-dev-runtime.d.ts +2 -0
- package/lib/jsx-dev-runtime.js +2 -0
- package/lib/jsx-runtime.d.ts +2 -0
- package/lib/jsx-runtime.js +28 -0
- package/lib/jsx-runtime.js.map +1 -0
- package/package.json +79 -0
- package/src/editor/NodeInspectorConfig.tsx +76 -0
- package/src/editor/PreviewOverlayConfig.tsx +65 -0
- package/src/editor/Provider.tsx +109 -0
- package/src/editor/SceneGraphTabConfig.tsx +87 -0
- package/src/editor/icons/CircleIcon.tsx +7 -0
- package/src/editor/icons/CodeIcon.tsx +8 -0
- package/src/editor/icons/CurveIcon.tsx +7 -0
- package/src/editor/icons/GridIcon.tsx +7 -0
- package/src/editor/icons/IconMap.ts +35 -0
- package/src/editor/icons/ImgIcon.tsx +8 -0
- package/src/editor/icons/LayoutIcon.tsx +9 -0
- package/src/editor/icons/LineIcon.tsx +7 -0
- package/src/editor/icons/NodeIcon.tsx +7 -0
- package/src/editor/icons/RayIcon.tsx +7 -0
- package/src/editor/icons/RectIcon.tsx +7 -0
- package/src/editor/icons/ShapeIcon.tsx +7 -0
- package/src/editor/icons/TxtIcon.tsx +8 -0
- package/src/editor/icons/VideoIcon.tsx +7 -0
- package/src/editor/icons/View2DIcon.tsx +10 -0
- package/src/editor/index.css +0 -0
- package/src/editor/index.ts +19 -0
- package/src/editor/shortcuts.ts +27 -0
- package/src/editor/tree/DetachedRoot.tsx +27 -0
- package/src/editor/tree/NodeElement.tsx +72 -0
- package/src/editor/tree/TreeElement.tsx +70 -0
- package/src/editor/tree/TreeRoot.tsx +10 -0
- package/src/editor/tree/ViewRoot.tsx +20 -0
- package/src/editor/tree/index.module.scss +45 -0
- package/src/editor/tree/index.ts +4 -0
- package/src/editor/tree/navigation.ts +145 -0
- package/src/editor/tsconfig.build.json +5 -0
- package/src/editor/tsconfig.json +12 -0
- package/src/editor/tsdoc.json +4 -0
- package/src/editor/utils/SignalSet.ts +37 -0
- package/src/editor/utils/index.ts +1 -0
- package/src/editor/vite-env.d.ts +1 -0
- package/src/lib/code/CodeCursor.ts +468 -0
- package/src/lib/code/CodeDiffer.ts +77 -0
- package/src/lib/code/CodeFragment.ts +96 -0
- package/src/lib/code/CodeHighlighter.ts +73 -0
- package/src/lib/code/CodeMetrics.ts +47 -0
- package/src/lib/code/CodeRange.test.ts +113 -0
- package/src/lib/code/CodeRange.ts +222 -0
- package/src/lib/code/CodeScope.ts +100 -0
- package/src/lib/code/CodeSelection.ts +28 -0
- package/src/lib/code/CodeSignal.ts +348 -0
- package/src/lib/code/CodeTokenizer.ts +54 -0
- package/src/lib/code/DefaultHighlightStyle.ts +98 -0
- package/src/lib/code/LezerHighlighter.ts +113 -0
- package/src/lib/code/diff.test.ts +311 -0
- package/src/lib/code/diff.ts +319 -0
- package/src/lib/code/extractRange.ts +125 -0
- package/src/lib/code/index.ts +13 -0
- package/src/lib/components/Bezier.ts +103 -0
- package/src/lib/components/Camera.ts +401 -0
- package/src/lib/components/Circle.ts +310 -0
- package/src/lib/components/Code.ts +532 -0
- package/src/lib/components/CubicBezier.ts +115 -0
- package/src/lib/components/Curve.ts +460 -0
- package/src/lib/components/Grid.ts +134 -0
- package/src/lib/components/Icon.ts +153 -0
- package/src/lib/components/Img.ts +328 -0
- package/src/lib/components/Knot.ts +156 -0
- package/src/lib/components/Latex.ts +537 -0
- package/src/lib/components/Layout.ts +1101 -0
- package/src/lib/components/Line.ts +394 -0
- package/src/lib/components/Node.ts +1941 -0
- package/src/lib/components/Path.ts +132 -0
- package/src/lib/components/Polygon.ts +266 -0
- package/src/lib/components/QuadBezier.ts +103 -0
- package/src/lib/components/Ray.ts +126 -0
- package/src/lib/components/Rect.ts +219 -0
- package/src/lib/components/SVG.ts +853 -0
- package/src/lib/components/Shape.ts +322 -0
- package/src/lib/components/Spline.ts +318 -0
- package/src/lib/components/Txt.test.tsx +81 -0
- package/src/lib/components/Txt.ts +204 -0
- package/src/lib/components/TxtLeaf.ts +210 -0
- package/src/lib/components/Video.ts +368 -0
- package/src/lib/components/View2D.ts +85 -0
- package/src/lib/components/__logs__/image-without-source.ts +18 -0
- package/src/lib/components/__logs__/line-without-points.ts +31 -0
- package/src/lib/components/__logs__/reactive-playback-rate.ts +22 -0
- package/src/lib/components/__logs__/spline-with-insufficient-knots.ts +25 -0
- package/src/lib/components/__tests__/Camera.test.tsx +73 -0
- package/src/lib/components/__tests__/children.test.tsx +142 -0
- package/src/lib/components/__tests__/clone.test.tsx +126 -0
- package/src/lib/components/__tests__/fontInheritance.test.tsx +102 -0
- package/src/lib/components/__tests__/generatorTest.ts +27 -0
- package/src/lib/components/__tests__/mockScene2D.ts +50 -0
- package/src/lib/components/__tests__/query.test.tsx +122 -0
- package/src/lib/components/__tests__/state.test.tsx +60 -0
- package/src/lib/components/index.ts +26 -0
- package/src/lib/components/types.ts +35 -0
- package/src/lib/curves/ArcSegment.ts +169 -0
- package/src/lib/curves/CircleSegment.ts +99 -0
- package/src/lib/curves/CubicBezierSegment.ts +80 -0
- package/src/lib/curves/CurveDrawingInfo.ts +11 -0
- package/src/lib/curves/CurvePoint.ts +15 -0
- package/src/lib/curves/CurveProfile.ts +28 -0
- package/src/lib/curves/KnotInfo.ts +10 -0
- package/src/lib/curves/LineSegment.ts +75 -0
- package/src/lib/curves/Polynomial.ts +355 -0
- package/src/lib/curves/Polynomial2D.ts +62 -0
- package/src/lib/curves/PolynomialSegment.ts +151 -0
- package/src/lib/curves/QuadBezierSegment.ts +66 -0
- package/src/lib/curves/Segment.ts +31 -0
- package/src/lib/curves/UniformPolynomialCurveSampler.ts +93 -0
- package/src/lib/curves/createCurveProfileLerp.ts +471 -0
- package/src/lib/curves/getBezierSplineProfile.ts +227 -0
- package/src/lib/curves/getCircleProfile.ts +86 -0
- package/src/lib/curves/getPathProfile.ts +177 -0
- package/src/lib/curves/getPointAtDistance.ts +21 -0
- package/src/lib/curves/getPolylineProfile.test.ts +21 -0
- package/src/lib/curves/getPolylineProfile.ts +88 -0
- package/src/lib/curves/getRectProfile.ts +138 -0
- package/src/lib/curves/index.ts +16 -0
- package/src/lib/decorators/canvasStyleSignal.ts +15 -0
- package/src/lib/decorators/colorSignal.ts +9 -0
- package/src/lib/decorators/compound.ts +85 -0
- package/src/lib/decorators/computed.ts +18 -0
- package/src/lib/decorators/defaultStyle.ts +15 -0
- package/src/lib/decorators/filtersSignal.ts +133 -0
- package/src/lib/decorators/index.ts +10 -0
- package/src/lib/decorators/initializers.ts +32 -0
- package/src/lib/decorators/nodeName.ts +13 -0
- package/src/lib/decorators/signal.test.ts +89 -0
- package/src/lib/decorators/signal.ts +348 -0
- package/src/lib/decorators/spacingSignal.ts +15 -0
- package/src/lib/decorators/transformSignals.test.ts +858 -0
- package/src/lib/decorators/transformSignals.ts +1633 -0
- package/src/lib/decorators/vector2Signal.ts +35 -0
- package/src/lib/globals.d.ts +2 -0
- package/src/lib/index.ts +9 -0
- package/src/lib/jsx-dev-runtime.ts +2 -0
- package/src/lib/jsx-runtime.ts +45 -0
- package/src/lib/morphers/PathMorpher.ts +8 -0
- package/src/lib/morphers/defaultMorpher.ts +43 -0
- package/src/lib/morphers/index.ts +4 -0
- package/src/lib/morphers/manimMorpher.ts +658 -0
- package/src/lib/parse-svg-path.d.ts +14 -0
- package/src/lib/partials/Filter.ts +185 -0
- package/src/lib/partials/Gradient.ts +103 -0
- package/src/lib/partials/Pattern.ts +35 -0
- package/src/lib/partials/RoughConfig.ts +180 -0
- package/src/lib/partials/ShaderConfig.ts +122 -0
- package/src/lib/partials/index.ts +5 -0
- package/src/lib/partials/types.ts +58 -0
- package/src/lib/scenes/Scene2D.ts +167 -0
- package/src/lib/scenes/index.ts +3 -0
- package/src/lib/scenes/makeScene2D.ts +19 -0
- package/src/lib/scenes/useScene2D.ts +6 -0
- package/src/lib/tsconfig.build.json +5 -0
- package/src/lib/tsconfig.json +11 -0
- package/src/lib/tsdoc.json +4 -0
- package/src/lib/utils/CanvasUtils.ts +304 -0
- package/src/lib/utils/PathDataBuilder.ts +320 -0
- package/src/lib/utils/diff.test.ts +453 -0
- package/src/lib/utils/diff.ts +148 -0
- package/src/lib/utils/index.ts +5 -0
- package/src/lib/utils/is.ts +11 -0
- package/src/lib/utils/makeSignalExtensions.ts +29 -0
- package/src/lib/utils/rough.ts +513 -0
- package/src/lib/utils/withDefaults.tsx +26 -0
- package/src/tsconfig.base.json +18 -0
- package/src/tsconfig.build.json +8 -0
- package/src/tsconfig.json +5 -0
- package/tsconfig.project.json +7 -0
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BBox,
|
|
3
|
+
createSignal,
|
|
4
|
+
experimentalLog,
|
|
5
|
+
map,
|
|
6
|
+
SerializedVector2,
|
|
7
|
+
Signal,
|
|
8
|
+
SignalValue,
|
|
9
|
+
SimpleSignal,
|
|
10
|
+
ThreadGenerator,
|
|
11
|
+
TimingFunction,
|
|
12
|
+
unwrap,
|
|
13
|
+
useLogger,
|
|
14
|
+
useScene,
|
|
15
|
+
Vector2,
|
|
16
|
+
} from '@canvas-commons/core';
|
|
17
|
+
import {
|
|
18
|
+
CodeCursor,
|
|
19
|
+
CodeFragmentDrawingInfo,
|
|
20
|
+
CodeHighlighter,
|
|
21
|
+
CodePoint,
|
|
22
|
+
CodeRange,
|
|
23
|
+
CodeSelection,
|
|
24
|
+
CodeSignal,
|
|
25
|
+
codeSignal,
|
|
26
|
+
CodeSignalContext,
|
|
27
|
+
findAllCodeRanges,
|
|
28
|
+
isPointInCodeSelection,
|
|
29
|
+
lines,
|
|
30
|
+
parseCodeSelection,
|
|
31
|
+
PossibleCodeScope,
|
|
32
|
+
PossibleCodeSelection,
|
|
33
|
+
resolveScope,
|
|
34
|
+
} from '../code';
|
|
35
|
+
import {computed, initial, nodeName, parser, signal} from '../decorators';
|
|
36
|
+
import {DesiredLength} from '../partials';
|
|
37
|
+
import {Shape, ShapeProps} from './Shape';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @experimental
|
|
41
|
+
*/
|
|
42
|
+
export interface DrawTokenHook {
|
|
43
|
+
(
|
|
44
|
+
ctx: CanvasRenderingContext2D,
|
|
45
|
+
text: string,
|
|
46
|
+
position: Vector2,
|
|
47
|
+
color: string,
|
|
48
|
+
selection: number,
|
|
49
|
+
): void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Describes custom drawing logic used by the Code node.
|
|
54
|
+
*
|
|
55
|
+
* @experimental
|
|
56
|
+
*/
|
|
57
|
+
export interface DrawHooks {
|
|
58
|
+
/**
|
|
59
|
+
* Custom drawing logic for individual code tokens.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* token(ctx, text, position, color, selection) {
|
|
64
|
+
* const blur = map(3, 0, selection);
|
|
65
|
+
* const alpha = map(0.5, 1, selection);
|
|
66
|
+
* ctx.globalAlpha *= alpha;
|
|
67
|
+
* ctx.filter = `blur(${blur}px)`;
|
|
68
|
+
* ctx.fillStyle = color;
|
|
69
|
+
* ctx.fillText(text, position.x, position.y);
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
token: DrawTokenHook;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface CodeProps extends ShapeProps {
|
|
77
|
+
/**
|
|
78
|
+
* {@inheritDoc Code.highlighter}
|
|
79
|
+
*/
|
|
80
|
+
highlighter?: SignalValue<CodeHighlighter | null>;
|
|
81
|
+
/**
|
|
82
|
+
* {@inheritDoc Code.code}
|
|
83
|
+
*/
|
|
84
|
+
code?: SignalValue<PossibleCodeScope>;
|
|
85
|
+
/**
|
|
86
|
+
* {@inheritDoc Code.selection}
|
|
87
|
+
*/
|
|
88
|
+
selection?: SignalValue<PossibleCodeSelection>;
|
|
89
|
+
/**
|
|
90
|
+
* {@inheritDoc Code.drawHooks}
|
|
91
|
+
*/
|
|
92
|
+
drawHooks?: SignalValue<DrawHooks>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A node for displaying and animating code.
|
|
97
|
+
*
|
|
98
|
+
* @preview
|
|
99
|
+
* ```tsx editor
|
|
100
|
+
* import {Code, makeScene2D} from '@canvas-commons/2d';
|
|
101
|
+
* import {createRef} from '@canvas-commons/core';
|
|
102
|
+
*
|
|
103
|
+
* export default makeScene2D(function* (view) {
|
|
104
|
+
* const code = createRef<Code>();
|
|
105
|
+
*
|
|
106
|
+
* view.add(
|
|
107
|
+
* <Code
|
|
108
|
+
* ref={code}
|
|
109
|
+
* anchor={-1}
|
|
110
|
+
* position={view.size().scale(-0.5).add(60)}
|
|
111
|
+
* fontFamily={'JetBrains Mono, monospace'}
|
|
112
|
+
* fontSize={36}
|
|
113
|
+
* code={`\
|
|
114
|
+
* function hello() {
|
|
115
|
+
* console.log('Hello');
|
|
116
|
+
* }`}
|
|
117
|
+
* />,
|
|
118
|
+
* );
|
|
119
|
+
*
|
|
120
|
+
* yield* code()
|
|
121
|
+
* .code(
|
|
122
|
+
* `\
|
|
123
|
+
* function hello() {
|
|
124
|
+
* console.warn('Hello World');
|
|
125
|
+
* }`,
|
|
126
|
+
* 1,
|
|
127
|
+
* )
|
|
128
|
+
* .wait(0.5)
|
|
129
|
+
* .back(1)
|
|
130
|
+
* .wait(0.5);
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
@nodeName('Code')
|
|
135
|
+
export class Code extends Shape {
|
|
136
|
+
/**
|
|
137
|
+
* Create a standalone code signal.
|
|
138
|
+
*
|
|
139
|
+
* @param initial - The initial code.
|
|
140
|
+
* @param highlighter - Custom highlighter to use.
|
|
141
|
+
*/
|
|
142
|
+
public static createSignal(
|
|
143
|
+
initial: SignalValue<PossibleCodeScope>,
|
|
144
|
+
highlighter?: SignalValue<CodeHighlighter>,
|
|
145
|
+
): CodeSignal<void> {
|
|
146
|
+
return new CodeSignalContext<void>(
|
|
147
|
+
initial,
|
|
148
|
+
undefined,
|
|
149
|
+
highlighter,
|
|
150
|
+
).toSignal();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public static defaultHighlighter: CodeHighlighter | null = null;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The code highlighter to use for this code node.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* Defaults to a shared {@link code.LezerHighlighter}.
|
|
160
|
+
*/
|
|
161
|
+
@initial(() => Code.defaultHighlighter)
|
|
162
|
+
@signal()
|
|
163
|
+
declare public readonly highlighter: SimpleSignal<
|
|
164
|
+
CodeHighlighter | null,
|
|
165
|
+
this
|
|
166
|
+
>;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The code to display.
|
|
170
|
+
*/
|
|
171
|
+
@codeSignal()
|
|
172
|
+
declare public readonly code: CodeSignal<this>;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Custom drawing logic for the code.
|
|
176
|
+
*
|
|
177
|
+
* @remarks
|
|
178
|
+
* Check out {@link DrawHooks} for available render hooks.
|
|
179
|
+
*
|
|
180
|
+
* @experimental
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* Make the unselected code blurry and transparent:
|
|
184
|
+
* ```tsx
|
|
185
|
+
* <Code
|
|
186
|
+
* drawHooks={{
|
|
187
|
+
* token(ctx, text, position, color, selection) {
|
|
188
|
+
* const blur = map(3, 0, selection);
|
|
189
|
+
* const alpha = map(0.5, 1, selection);
|
|
190
|
+
* ctx.globalAlpha *= alpha;
|
|
191
|
+
* ctx.filter = `blur(${blur}px)`;
|
|
192
|
+
* ctx.fillStyle = color;
|
|
193
|
+
* ctx.fillText(text, position.x, position.y);
|
|
194
|
+
* },
|
|
195
|
+
* }}
|
|
196
|
+
* // ...
|
|
197
|
+
* />
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
@initial<DrawHooks>({
|
|
201
|
+
token(ctx, text, position, color, selection) {
|
|
202
|
+
ctx.fillStyle = color;
|
|
203
|
+
ctx.globalAlpha *= map(0.2, 1, selection);
|
|
204
|
+
ctx.fillText(text, position.x, position.y);
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
@signal()
|
|
208
|
+
declare public readonly drawHooks: SimpleSignal<DrawHooks, this>;
|
|
209
|
+
|
|
210
|
+
protected setDrawHooks(value: DrawHooks) {
|
|
211
|
+
if (
|
|
212
|
+
!useScene().experimentalFeatures &&
|
|
213
|
+
value !== this.drawHooks.context.getInitial()
|
|
214
|
+
) {
|
|
215
|
+
useLogger().log({
|
|
216
|
+
...experimentalLog(`Code uses experimental draw hooks.`),
|
|
217
|
+
inspect: this.key,
|
|
218
|
+
});
|
|
219
|
+
} else {
|
|
220
|
+
this.drawHooks.context.setter(value);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The currently selected code range.
|
|
226
|
+
*
|
|
227
|
+
* @remarks
|
|
228
|
+
* Either a single {@link code.CodeRange} or an array of them
|
|
229
|
+
* describing which parts of the code should be visually emphasized.
|
|
230
|
+
*
|
|
231
|
+
* You can use {@link code.word} and
|
|
232
|
+
* {@link code.lines} to quickly create ranges.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* The following will select the word "console" in the code.
|
|
236
|
+
* Both lines and columns are 0-based. So it will select a 7-character-long
|
|
237
|
+
* (`7`) word in the second line (`1`) starting at the third character (`2`).
|
|
238
|
+
* ```tsx
|
|
239
|
+
* <Code
|
|
240
|
+
* selection={word(1, 2, 7)}
|
|
241
|
+
* code={`\
|
|
242
|
+
* function hello() => {
|
|
243
|
+
* console.log('Hello');
|
|
244
|
+
* }`}
|
|
245
|
+
* // ...
|
|
246
|
+
* />
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
@initial(lines(0, Infinity))
|
|
250
|
+
@parser(parseCodeSelection)
|
|
251
|
+
@signal()
|
|
252
|
+
declare public readonly selection: Signal<
|
|
253
|
+
PossibleCodeSelection,
|
|
254
|
+
CodeSelection,
|
|
255
|
+
this
|
|
256
|
+
>;
|
|
257
|
+
public oldSelection: CodeSelection | null = null;
|
|
258
|
+
public selectionProgress = createSignal<number | null>(null);
|
|
259
|
+
protected *tweenSelection(
|
|
260
|
+
value: CodeRange[],
|
|
261
|
+
duration: number,
|
|
262
|
+
timingFunction: TimingFunction,
|
|
263
|
+
): ThreadGenerator {
|
|
264
|
+
this.oldSelection = this.selection();
|
|
265
|
+
this.selection(value);
|
|
266
|
+
this.selectionProgress(0);
|
|
267
|
+
yield* this.selectionProgress(1, duration, timingFunction);
|
|
268
|
+
this.selectionProgress(null);
|
|
269
|
+
this.oldSelection = null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Get the currently displayed code as a string.
|
|
274
|
+
*/
|
|
275
|
+
@computed()
|
|
276
|
+
public parsed(): string {
|
|
277
|
+
return resolveScope(this.code(), scope => unwrap(scope.progress) > 0.5);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
@computed()
|
|
281
|
+
public highlighterCache() {
|
|
282
|
+
const highlighter = this.highlighter();
|
|
283
|
+
if (!highlighter || !highlighter.initialize()) return null;
|
|
284
|
+
const code = this.code();
|
|
285
|
+
const before = resolveScope(code, false);
|
|
286
|
+
const after = resolveScope(code, true);
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
before: highlighter.prepare(before),
|
|
290
|
+
after: highlighter.prepare(after),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private cursorCache: CodeCursor | undefined;
|
|
295
|
+
private get cursor() {
|
|
296
|
+
this.cursorCache ??= new CodeCursor(this);
|
|
297
|
+
return this.cursorCache;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
public constructor(props: CodeProps) {
|
|
301
|
+
super({
|
|
302
|
+
fontFamily: 'monospace',
|
|
303
|
+
highlighter: Code.defaultHighlighter,
|
|
304
|
+
...props,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Create a child code signal.
|
|
310
|
+
*
|
|
311
|
+
* @param initial - The initial code.
|
|
312
|
+
*/
|
|
313
|
+
public createSignal(
|
|
314
|
+
initial: SignalValue<PossibleCodeScope>,
|
|
315
|
+
): CodeSignal<this> {
|
|
316
|
+
return new CodeSignalContext<this>(
|
|
317
|
+
initial,
|
|
318
|
+
this,
|
|
319
|
+
this.highlighter,
|
|
320
|
+
).toSignal();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Find all code ranges that match the given pattern.
|
|
325
|
+
*
|
|
326
|
+
* @param pattern - Either a string or a regular expression to search for.
|
|
327
|
+
* When a string is passed, it looks for **exact matches**.
|
|
328
|
+
* When a RegExp object is passed, it will match against it.
|
|
329
|
+
*/
|
|
330
|
+
public findAllRanges(pattern: string | RegExp): CodeRange[] {
|
|
331
|
+
return findAllCodeRanges(this.parsed(), pattern);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Find the first code range that matches the given pattern.
|
|
336
|
+
*
|
|
337
|
+
* @param pattern - Either a string or a regular expression to search for.
|
|
338
|
+
* When a string is passed, it looks for **exact matches**.
|
|
339
|
+
* When a RegExp object is passed, it will match against it.
|
|
340
|
+
*/
|
|
341
|
+
public findFirstRange(pattern: string | RegExp): CodeRange {
|
|
342
|
+
return (
|
|
343
|
+
findAllCodeRanges(this.parsed(), pattern, 1)[0] ?? [
|
|
344
|
+
[0, 0],
|
|
345
|
+
[0, 0],
|
|
346
|
+
]
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Find the last code range that matches the given pattern.
|
|
352
|
+
*
|
|
353
|
+
* @param pattern - Either a string or a regular expression to search for.
|
|
354
|
+
* When a string is passed, it looks for **exact matches**.
|
|
355
|
+
* When a RegExp object is passed, it will match against it.
|
|
356
|
+
*/
|
|
357
|
+
public findLastRange(pattern: string | RegExp): CodeRange {
|
|
358
|
+
return (
|
|
359
|
+
findAllCodeRanges(this.parsed(), pattern).at(-1) ?? [
|
|
360
|
+
[0, 0],
|
|
361
|
+
[0, 0],
|
|
362
|
+
]
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Return the bounding box of the given point (character) in the code.
|
|
368
|
+
*
|
|
369
|
+
* @remarks
|
|
370
|
+
* The returned bound box is in local space of the `Code` node.
|
|
371
|
+
*
|
|
372
|
+
* @param point - The point to get the bounding box for.
|
|
373
|
+
*/
|
|
374
|
+
public getPointBBox(point: CodePoint): BBox {
|
|
375
|
+
const [line, column] = point;
|
|
376
|
+
const drawingInfo = this.drawingInfo();
|
|
377
|
+
let match: CodeFragmentDrawingInfo | undefined;
|
|
378
|
+
for (const info of drawingInfo.fragments) {
|
|
379
|
+
if (info.cursor.y < line) {
|
|
380
|
+
match = info;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (info.cursor.y === line && info.cursor.x < column) {
|
|
385
|
+
match = info;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (!match) return new BBox();
|
|
393
|
+
|
|
394
|
+
const size = this.computedSize();
|
|
395
|
+
return new BBox(
|
|
396
|
+
match.position
|
|
397
|
+
.sub(size.scale(0.5))
|
|
398
|
+
.addX(match.characterSize.x * (column - match.cursor.x)),
|
|
399
|
+
match.characterSize,
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Return bounding boxes of all characters in the selection.
|
|
405
|
+
*
|
|
406
|
+
* @remarks
|
|
407
|
+
* The returned bounding boxes are in local space of the `Code` node.
|
|
408
|
+
* Each line of code has a separate bounding box.
|
|
409
|
+
*
|
|
410
|
+
* @param selection - The selection to get the bounding boxes for.
|
|
411
|
+
*/
|
|
412
|
+
public getSelectionBBox(selection: PossibleCodeSelection): BBox[] {
|
|
413
|
+
const size = this.computedSize();
|
|
414
|
+
const range = parseCodeSelection(selection);
|
|
415
|
+
const drawingInfo = this.drawingInfo();
|
|
416
|
+
const bboxes: BBox[] = [];
|
|
417
|
+
|
|
418
|
+
let current: BBox | null = null;
|
|
419
|
+
let line = 0;
|
|
420
|
+
let column = 0;
|
|
421
|
+
for (const info of drawingInfo.fragments) {
|
|
422
|
+
if (info.cursor.y !== line) {
|
|
423
|
+
line = info.cursor.y;
|
|
424
|
+
if (current) {
|
|
425
|
+
bboxes.push(current);
|
|
426
|
+
current = null;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
column = info.cursor.x;
|
|
431
|
+
for (let i = 0; i < info.text.length; i++) {
|
|
432
|
+
if (isPointInCodeSelection([line, column], range)) {
|
|
433
|
+
const bbox = new BBox(
|
|
434
|
+
info.position
|
|
435
|
+
.sub(size.scale(0.5))
|
|
436
|
+
.addX(info.characterSize.x * (column - info.cursor.x)),
|
|
437
|
+
info.characterSize,
|
|
438
|
+
);
|
|
439
|
+
if (!current) {
|
|
440
|
+
current = bbox;
|
|
441
|
+
} else {
|
|
442
|
+
current = current.union(bbox);
|
|
443
|
+
}
|
|
444
|
+
} else if (current) {
|
|
445
|
+
bboxes.push(current);
|
|
446
|
+
current = null;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
column++;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (current) {
|
|
454
|
+
bboxes.push(current);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return bboxes;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
@computed()
|
|
461
|
+
protected drawingInfo() {
|
|
462
|
+
this.requestFontUpdate();
|
|
463
|
+
const context = this.cacheCanvas();
|
|
464
|
+
const code = this.code();
|
|
465
|
+
|
|
466
|
+
context.save();
|
|
467
|
+
this.applyStyle(context);
|
|
468
|
+
this.applyText(context);
|
|
469
|
+
this.cursor.setupDraw(context);
|
|
470
|
+
this.cursor.drawScope(code);
|
|
471
|
+
const info = this.cursor.getDrawingInfo();
|
|
472
|
+
context.restore();
|
|
473
|
+
|
|
474
|
+
return info;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
protected override desiredSize(): SerializedVector2<DesiredLength> {
|
|
478
|
+
this.requestFontUpdate();
|
|
479
|
+
const context = this.cacheCanvas();
|
|
480
|
+
const code = this.code();
|
|
481
|
+
|
|
482
|
+
context.save();
|
|
483
|
+
this.applyStyle(context);
|
|
484
|
+
this.applyText(context);
|
|
485
|
+
this.cursor.setupMeasure(context);
|
|
486
|
+
this.cursor.measureSize(code);
|
|
487
|
+
const size = this.cursor.getSize();
|
|
488
|
+
context.restore();
|
|
489
|
+
|
|
490
|
+
return size;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
protected override draw(context: CanvasRenderingContext2D): void {
|
|
494
|
+
this.requestFontUpdate();
|
|
495
|
+
this.applyStyle(context);
|
|
496
|
+
this.applyText(context);
|
|
497
|
+
const size = this.computedSize();
|
|
498
|
+
const drawingInfo = this.drawingInfo();
|
|
499
|
+
|
|
500
|
+
context.save();
|
|
501
|
+
context.translate(
|
|
502
|
+
-size.width / 2,
|
|
503
|
+
-size.height / 2 + drawingInfo.verticalOffset,
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
const drawHooks = this.drawHooks();
|
|
507
|
+
for (const info of drawingInfo.fragments) {
|
|
508
|
+
context.save();
|
|
509
|
+
context.globalAlpha *= info.alpha;
|
|
510
|
+
drawHooks.token(context, info.text, info.position, info.fill, info.time);
|
|
511
|
+
context.restore();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
context.restore();
|
|
515
|
+
|
|
516
|
+
this.drawChildren(context);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
protected override applyText(context: CanvasRenderingContext2D) {
|
|
520
|
+
super.applyText(context);
|
|
521
|
+
context.font = this.styles.font;
|
|
522
|
+
context.textBaseline = 'top';
|
|
523
|
+
if ('letterSpacing' in context) {
|
|
524
|
+
context.letterSpacing = this.styles.letterSpacing;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
protected override collectAsyncResources(): void {
|
|
529
|
+
super.collectAsyncResources();
|
|
530
|
+
this.highlighter()?.initialize();
|
|
531
|
+
}
|
|
532
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PossibleVector2,
|
|
3
|
+
SignalValue,
|
|
4
|
+
Vector2Signal,
|
|
5
|
+
} from '@canvas-commons/core';
|
|
6
|
+
import {CubicBezierSegment} from '../curves';
|
|
7
|
+
import {PolynomialSegment} from '../curves/PolynomialSegment';
|
|
8
|
+
import {computed, vector2Signal} from '../decorators';
|
|
9
|
+
import {bezierCurveTo, lineTo, moveTo} from '../utils';
|
|
10
|
+
import {Bezier, BezierOverlayInfo} from './Bezier';
|
|
11
|
+
import {CurveProps} from './Curve';
|
|
12
|
+
|
|
13
|
+
export interface CubicBezierProps extends CurveProps {
|
|
14
|
+
p0?: SignalValue<PossibleVector2>;
|
|
15
|
+
p0X?: SignalValue<number>;
|
|
16
|
+
p0Y?: SignalValue<number>;
|
|
17
|
+
|
|
18
|
+
p1?: SignalValue<PossibleVector2>;
|
|
19
|
+
p1X?: SignalValue<number>;
|
|
20
|
+
p1Y?: SignalValue<number>;
|
|
21
|
+
|
|
22
|
+
p2?: SignalValue<PossibleVector2>;
|
|
23
|
+
p2X?: SignalValue<number>;
|
|
24
|
+
p2Y?: SignalValue<number>;
|
|
25
|
+
|
|
26
|
+
p3?: SignalValue<PossibleVector2>;
|
|
27
|
+
p3X?: SignalValue<number>;
|
|
28
|
+
p3Y?: SignalValue<number>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A node for drawing a cubic Bézier curve.
|
|
33
|
+
*
|
|
34
|
+
* @preview
|
|
35
|
+
* ```tsx editor
|
|
36
|
+
* import {makeScene2D, CubicBezier} from '@canvas-commons/2d';
|
|
37
|
+
* import {createRef} from '@canvas-commons/core';
|
|
38
|
+
*
|
|
39
|
+
* export default makeScene2D(function* (view) {
|
|
40
|
+
* const bezier = createRef<CubicBezier>();
|
|
41
|
+
*
|
|
42
|
+
* view.add(
|
|
43
|
+
* <CubicBezier
|
|
44
|
+
* ref={bezier}
|
|
45
|
+
* lineWidth={4}
|
|
46
|
+
* stroke={'lightseagreen'}
|
|
47
|
+
* p0={[-200, -100]}
|
|
48
|
+
* p1={[100, -100]}
|
|
49
|
+
* p2={[-100, 100]}
|
|
50
|
+
* p3={[200, 100]}
|
|
51
|
+
* end={0}
|
|
52
|
+
* />
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* yield* bezier().end(1, 1);
|
|
56
|
+
* yield* bezier().start(1, 1).to(0, 1);
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export class CubicBezier extends Bezier {
|
|
61
|
+
/**
|
|
62
|
+
* The start point of the Bézier curve.
|
|
63
|
+
*/
|
|
64
|
+
@vector2Signal('p0')
|
|
65
|
+
declare public readonly p0: Vector2Signal<this>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The first control point of the Bézier curve.
|
|
69
|
+
*/
|
|
70
|
+
@vector2Signal('p1')
|
|
71
|
+
declare public readonly p1: Vector2Signal<this>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The second control point of the Bézier curve.
|
|
75
|
+
*/
|
|
76
|
+
@vector2Signal('p2')
|
|
77
|
+
declare public readonly p2: Vector2Signal<this>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The end point of the Bézier curve.
|
|
81
|
+
*/
|
|
82
|
+
@vector2Signal('p3')
|
|
83
|
+
declare public readonly p3: Vector2Signal<this>;
|
|
84
|
+
|
|
85
|
+
public constructor(props: CubicBezierProps) {
|
|
86
|
+
super(props);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@computed()
|
|
90
|
+
protected segment(): PolynomialSegment {
|
|
91
|
+
return new CubicBezierSegment(this.p0(), this.p1(), this.p2(), this.p3());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
protected overlayInfo(matrix: DOMMatrix): BezierOverlayInfo {
|
|
95
|
+
const [p0, p1, p2, p3] = this.segment().transformPoints(matrix);
|
|
96
|
+
|
|
97
|
+
const curvePath = new Path2D();
|
|
98
|
+
moveTo(curvePath, p0);
|
|
99
|
+
bezierCurveTo(curvePath, p1, p2, p3);
|
|
100
|
+
|
|
101
|
+
const handleLinesPath = new Path2D();
|
|
102
|
+
moveTo(handleLinesPath, p0);
|
|
103
|
+
lineTo(handleLinesPath, p1);
|
|
104
|
+
moveTo(handleLinesPath, p2);
|
|
105
|
+
lineTo(handleLinesPath, p3);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
curve: curvePath,
|
|
109
|
+
startPoint: p0,
|
|
110
|
+
endPoint: p3,
|
|
111
|
+
controlPoints: [p1, p2],
|
|
112
|
+
handleLines: handleLinesPath,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|