@motion-script/code 0.1.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.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Describes the result of a highlight operation.
3
+ */
4
+ export interface HighlightResult {
5
+ /**
6
+ * The color of the code at the given index.
7
+ */
8
+ color: string | null;
9
+ /**
10
+ * The number of characters to skip ahead.
11
+ *
12
+ * @remarks
13
+ * This should be used to skip to the end of the currently highlighted token.
14
+ * The returned style will be used for the skipped characters, and they will
15
+ * be drawn as one continuous string keeping emojis and ligatures intact.
16
+ *
17
+ * The returned value is the number of characters to skip ahead, not the
18
+ * index of the end of the token.
19
+ */
20
+ skipAhead: number;
21
+ }
22
+ /**
23
+ * Describes custom highlighters used by the Code node.
24
+ *
25
+ * @typeParam T - The type of the cache used by the highlighter.
26
+ */
27
+ export interface CodeHighlighter<T = unknown> {
28
+ /**
29
+ * Initializes the highlighter.
30
+ *
31
+ * @remarks
32
+ * This method is called when collecting async resources for the node.
33
+ * It can be called multiple times so caching the initialization is
34
+ * recommended.
35
+ *
36
+ * If initialization is asynchronous, a promise should be registered using
37
+ * {@link DependencyContext.collectPromise} and the value of `false` should
38
+ * be returned. The hook will be called again when the promise resolves.
39
+ * This process can be repeated until the value of `true` is returned which
40
+ * will mark the highlighter as ready.
41
+ */
42
+ initialize(): boolean;
43
+ /**
44
+ * Prepares the code for highlighting.
45
+ *
46
+ * @remarks
47
+ * This method is called each time the code changes. It can be used to do
48
+ * any preprocessing of the code before highlighting. The result of this
49
+ * method is cached and passed to {@link highlight} when the code is
50
+ * highlighted.
51
+ *
52
+ * @param code - The code to prepare.
53
+ */
54
+ prepare(code: string): T;
55
+ /**
56
+ * Highlights the code at the given index.
57
+ *
58
+ * @param index - The index of the code to highlight.
59
+ * @param cache - The result of {@link prepare}.
60
+ */
61
+ highlight(index: number, cache: T): HighlightResult;
62
+ /**
63
+ * Tokenize the code.
64
+ *
65
+ * @param code - The code to tokenize.
66
+ */
67
+ tokenize(code: string): string[];
68
+ }
69
+ //# sourceMappingURL=highlighter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"highlighter.d.ts","sourceRoot":"","sources":["../src/highlighter.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC5B;;OAEG;IACH,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAErB;;;;;;;;;;OAUG;IACH,SAAS,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,OAAO;IACxC;;;;;;;;;;;;;OAaG;IACH,UAAU,IAAI,OAAO,CAAC;IAEtB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC;IAEzB;;;;;OAKG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,eAAe,CAAC;IAEpD;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACpC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=highlighter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"highlighter.js","sourceRoot":"","sources":["../src/highlighter.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ export * from './node';
2
+ export * from './props';
3
+ export { word, lines, range } from './code-range';
4
+ export type { CodeRange } from './code-range';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './node';
2
+ export * from './props';
3
+ export { word, lines, range } from './code-range';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC"}
package/dist/node.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ import type { CodeProps } from "./props";
2
+ import { CodeRange } from "./code-range";
3
+ import { RenderContext, EaseFunction, FrameGenerator, NodeConfig, Size2D, SizeConstraints, Node, MeasureScope, AssetTracker, PaddingResolved } from "@motion-script/core";
4
+ export declare function initSyntaxHighlighter(themes?: string[], langs?: string[]): Promise<void>;
5
+ export declare class Code extends Node<CodeProps> {
6
+ readonly code: string;
7
+ readonly language: string;
8
+ readonly fontFamily: string;
9
+ readonly theme: string;
10
+ readonly fontSize: number;
11
+ readonly lineHeight: number;
12
+ readonly letterSpacing: number;
13
+ readonly showLineNumbers: boolean;
14
+ readonly lineNumberGap: number;
15
+ readonly padding: PaddingResolved;
16
+ private tokenLines;
17
+ private tokenized;
18
+ private transitions;
19
+ private highlightDimOpacity;
20
+ private highlightedIds;
21
+ constructor(props: NodeConfig<Code, CodeProps>);
22
+ set(props: {
23
+ [K in keyof CodeProps]?: CodeProps[K] | (() => CodeProps[K]);
24
+ }): void;
25
+ private tokenize;
26
+ prepare(storage: AssetTracker): void;
27
+ private tokenAdvance;
28
+ private gutterGap;
29
+ private gutterWidth;
30
+ measure(constraints: SizeConstraints, scope: MeasureScope): Partial<Size2D>;
31
+ onRender(ctx: RenderContext): void;
32
+ append(code: string, duration: number, easing?: EaseFunction): FrameGenerator;
33
+ prepend(code: string, duration: number, easing?: EaseFunction): FrameGenerator;
34
+ /**
35
+ * Highlight a range of code: tokens within the range stay at opacity 1,
36
+ * tokens outside dim to `opacity`. Persistent — call resetHighlight() to
37
+ * undo, or call highlight() again with a different range to cross-fade.
38
+ */
39
+ highlight(codeRange: CodeRange, duration?: number, easing?: EaseFunction, opacity?: number): FrameGenerator;
40
+ /**
41
+ * Fade all dimmed tokens back to opacity 1 and clear the persistent
42
+ * highlight state.
43
+ */
44
+ resetHighlight(duration?: number, easing?: EaseFunction): FrameGenerator;
45
+ /**
46
+ * Replace the tokens in `codeRange` with `next`, cross-fading widths and
47
+ * opacities.
48
+ */
49
+ replace(codeRange: CodeRange, next: string, duration: number, easing?: EaseFunction): FrameGenerator;
50
+ /**
51
+ * Insert `code` at the given (line, col). Both are 1-indexed; col is the
52
+ * column BEFORE which the new content is inserted (col=1 means start of
53
+ * line). If `code` contains newlines, new lines are created in the middle
54
+ * of the existing line.
55
+ */
56
+ insert(position: [number, number], code: string, duration: number, easing?: EaseFunction): FrameGenerator;
57
+ /**
58
+ * Remove the tokens in `codeRange`. If the range spans whole lines, those
59
+ * lines collapse their height; partial line ranges only remove tokens
60
+ * (the surrounding text reflows).
61
+ */
62
+ remove(codeRange: CodeRange, duration: number, easing?: EaseFunction): FrameGenerator;
63
+ /**
64
+ * Find every range matching the literal string `text` in the current
65
+ * source. Multi-line matches are supported.
66
+ */
67
+ findAllRanges(text: string): CodeRange[];
68
+ /**
69
+ * Find the `index`th range matching `text`. Returns null if not found.
70
+ */
71
+ findRangeAt(text: string, index: number): CodeRange | null;
72
+ /**
73
+ * Find the first range matching `text`. Returns null if not found.
74
+ */
75
+ findFirstRange(text: string): CodeRange | null;
76
+ private runTransition;
77
+ private joinedSource;
78
+ private lineLengths;
79
+ /**
80
+ * Resolve a CodeRange to the set of token ids whose content overlaps the
81
+ * range. Tokens that partially overlap are included.
82
+ */
83
+ private tokenIdsInRange;
84
+ /**
85
+ * Resolve a CodeRange to a structural (fromLine, fromIdx)..(toLine, toIdx)
86
+ * token span. Snaps to whole tokens (any token that overlaps the range is
87
+ * included). Returns null if no tokens overlap.
88
+ */
89
+ private rangeToTokenSpan;
90
+ protected drawSelf(draw: RenderContext): void;
91
+ private isHighlightActive;
92
+ private lineHighlightOpacity;
93
+ private resolveTokenStates;
94
+ private resolveLineHeightScales;
95
+ }
96
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,SAAS,EAAsB,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,cAAc,EAAE,UAAU,EAAc,MAAM,EAAE,eAAe,EAAE,IAAI,EAAS,YAAY,EAAE,YAAY,EAAE,eAAe,EAA4D,MAAM,qBAAqB,CAAC;AAoBvP,wBAAsB,qBAAqB,CACvC,MAAM,GAAE,MAAM,EAAoB,EAClC,KAAK,GAAE,MAAM,EAAmD,iBAGnE;AAkFD,qBAAa,IAAK,SAAQ,IAAI,CAAC,SAAS,CAAC;IAGrC,SAA4C,IAAI,EAAE,MAAM,CAAC;IACzD,SAAsD,QAAQ,EAAE,MAAM,CAAC;IACvE,SAA0D,UAAU,EAAE,MAAM,CAAC;IAC7E,SAAuD,KAAK,EAAE,MAAM,CAAC;IACrE,SAA4C,QAAQ,EAAE,MAAM,CAAC;IAC7D,SAA6C,UAAU,EAAE,MAAM,CAAC;IAMhE,SAA6C,aAAa,EAAE,MAAM,CAAC;IACnE,SAA+C,eAAe,EAAE,OAAO,CAAC;IAIxE,SAA2C,aAAa,EAAE,MAAM,CAAC;IACjE,SAAyF,OAAO,EAAE,eAAe,CAAC;IAElH,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,SAAS,CAAkB;IAEnC,OAAO,CAAC,WAAW,CAAoB;IAIvC,OAAO,CAAC,mBAAmB,CAAuB;IAClD,OAAO,CAAC,cAAc,CAA0B;gBAEpC,KAAK,EAAE,UAAU,CAAC,IAAI,EAAE,SA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