@motionscript/code 0.1.0-alpha.3 → 0.1.0-alpha.5

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.
Files changed (59) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +161 -108
  5. package/dist/browser/THIRD_PARTY_LICENSES.txt +431 -0
  6. package/dist/browser/index.js +7 -6
  7. package/dist/browser/index.js.map +3 -3
  8. package/dist/browser/manifest.json +84 -2
  9. package/dist/code-range.d.ts +6 -0
  10. package/dist/code-range.d.ts.map +1 -1
  11. package/dist/code-range.js +8 -0
  12. package/dist/code-range.js.map +1 -1
  13. package/dist/diff.d.ts +14 -0
  14. package/dist/diff.d.ts.map +1 -1
  15. package/dist/diff.js +134 -2
  16. package/dist/diff.js.map +1 -1
  17. package/dist/engine.d.ts +1 -1
  18. package/dist/engine.d.ts.map +1 -1
  19. package/dist/engine.js +1 -1
  20. package/dist/engine.js.map +1 -1
  21. package/dist/highlight.d.ts +10 -2
  22. package/dist/highlight.d.ts.map +1 -1
  23. package/dist/highlight.js +40 -6
  24. package/dist/highlight.js.map +1 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/layout.js.map +1 -1
  27. package/dist/node.d.ts +42 -7
  28. package/dist/node.d.ts.map +1 -1
  29. package/dist/node.js +174 -47
  30. package/dist/node.js.map +1 -1
  31. package/dist/nodes.js.map +1 -1
  32. package/dist/render.d.ts +6 -1
  33. package/dist/render.d.ts.map +1 -1
  34. package/dist/render.js +59 -0
  35. package/dist/render.js.map +1 -1
  36. package/dist/style.d.ts +3 -3
  37. package/dist/style.d.ts.map +1 -1
  38. package/dist/style.js +10 -6
  39. package/dist/style.js.map +1 -1
  40. package/dist/tokens.d.ts.map +1 -1
  41. package/dist/tokens.js +5 -1
  42. package/dist/tokens.js.map +1 -1
  43. package/dist/transitions.d.ts +19 -1
  44. package/dist/transitions.d.ts.map +1 -1
  45. package/dist/transitions.js.map +1 -1
  46. package/package.json +7 -5
  47. package/registry.json +6 -6
  48. package/src/code-range.ts +165 -156
  49. package/src/diff.ts +351 -216
  50. package/src/engine.ts +21 -21
  51. package/src/highlight.ts +340 -291
  52. package/src/index.ts +3 -3
  53. package/src/layout.ts +157 -157
  54. package/src/node.ts +1038 -904
  55. package/src/nodes.ts +18 -18
  56. package/src/render.ts +312 -247
  57. package/src/style.ts +255 -251
  58. package/src/tokens.ts +79 -75
  59. package/src/transitions.ts +204 -184
package/README.md CHANGED
@@ -1,108 +1,161 @@
1
- # @motionscript/code
2
-
3
- Animated, syntax-highlighted code listings for [Motion Script](https://motionscript.dev).
4
- The `Code` node highlights source with [Lezer](https://lezer.codemirror.net)
5
- grammars and animates edits at the token level: each edit is diffed against the
6
- listing it becomes, so tokens that survive travel to their new columns, removed
7
- ones fade out and new ones fade in, rather than the listing snapping.
8
-
9
- Hand `Code` to the engine through your project entry's `nodes`, and any
10
- document that engine opens can name it as `code`:
11
-
12
- ```ts
13
- // src/project.ts
14
- import { Code } from '@motionscript/code';
15
-
16
- export const nodes = [Code];
17
- ```
18
-
19
- ```ts
20
- import type { CompositionDocument } from '@motionscript/sdk';
21
- import { lines } from '@motionscript/code';
22
-
23
- export default {
24
- kind: 'composition',
25
- nodes: [
26
- { id: 'listing', type: 'code', parentId: null, layerOrder: 0,
27
- props: { code: 'function greet(name) {\n return "hi";\n}', language: 'typescript',
28
- theme: 'github-dark', fontSize: 28 } },
29
- ],
30
- commands: [
31
- { id: 'call', type: 'append', targetId: 'listing', at: 0, duration: 0.6,
32
- args: { code: '\ngreet("world");' } },
33
- { id: 'focus', type: 'highlight', targetId: 'listing', at: 0.8, duration: 0.4,
34
- args: { target: lines(2) } },
35
- ],
36
- } satisfies CompositionDocument;
37
- ```
38
-
39
- ## What's in here
40
-
41
- - **`Code`** node, a hugging block of highlighted source. Set `code`,
42
- `language` and `theme` to choose what is shown; tune `fontSize`,
43
- `fontFamily`, `lineHeight`, `letterSpacing`, `showLineNumbers`,
44
- `lineNumberGap` and `padding` for layout.
45
- - **Token-level commands**: `append`/`prepend` (lines at the end or start),
46
- `insert` (at a `[line, col]` position), `remove`, `replace`, and
47
- `highlight`/`resetHighlight` (dim everything outside a range, then restore
48
- it). A `to` command on `code` diffs the whole listing the same way.
49
- - **Range helpers**: `word(line, col, length)`, `lines(from, to)` and the
50
- chainable `CodeRanges` build the ranges those commands take.
51
- - **Themes**: `github-dark`, `github-light`, `vscode-dark`, `vscode-light`
52
- and a default, plus `registerCodeTheme` for your own.
53
- `initSyntaxHighlighter(themes, langs)` preloads grammars so the first frame
54
- renders fully highlighted.
55
-
56
- Every token keeps a stable identity across edits, so concurrent animations at
57
- different positions do not clobber each other while one is mid-flight.
58
-
59
- This package is written against [`@motionscript/sdk`](../../sdk), which it
60
- takes as a peer dependency, and renders through whatever backend draws the
61
- scene, such as [`@motionscript/browser`](../../browser).
62
-
63
- ## The monospaced face is yours to supply
64
-
65
- `Code` measures and draws every token in its `fontFamily`, `Fira Mono` by
66
- default, and lines its columns up on that face's advances. The package ships no
67
- font file and names no URL: list a monospaced face under that family in the
68
- engine's asset manifest, alongside the node itself.
69
-
70
- ```ts
71
- const engine = await createBrowserEngine({
72
- wasmUrl,
73
- nodes: [Code],
74
- manifest: {
75
- image: {}, video: {}, audio: {},
76
- font: {
77
- 'Fira Mono@400': {
78
- fontFamily: 'Fira Mono', fontWeight: 400,
79
- src: '/fonts/FiraMono-Regular.ttf', sizeBytes: 0,
80
- },
81
- },
82
- },
83
- });
84
- ```
85
-
86
- A family the manifest does not list is never loaded. The loader warns once, and
87
- the listing draws no text, or shapes in whichever other face is registered. To
88
- use another face, set `fontFamily` on the node and list that family instead.
89
-
90
- ## Usage
91
-
92
- ```bash
93
- npm install @motionscript/code @motionscript/sdk
94
- ```
95
-
96
- Or copy the node's source into your project with `ms add code`
97
- (see [`@motionscript/cli`](../../cli)). See the
98
- [docs](https://motionscript.dev/docs) for the full feature set and API
99
- reference.
100
-
101
- ## Development
102
-
103
- From the monorepo root:
104
-
105
- ```bash
106
- pnpm --filter @motionscript/code build
107
- pnpm --filter @motionscript/code test
108
- ```
1
+ # @motionscript/code
2
+
3
+ Animated, syntax-highlighted code listings for [Motion Script](https://motionscript.dev).
4
+ The `Code` node highlights source with [Lezer](https://lezer.codemirror.net)
5
+ grammars and animates edits at the token level: each edit is diffed against the
6
+ listing it becomes, so tokens that survive travel to their new columns, removed
7
+ ones fade out and new ones fade in, rather than the listing snapping.
8
+
9
+ Hand `Code` to the engine through your project entry's `nodes`, and any
10
+ document that engine opens can name it as `code`:
11
+
12
+ ```ts
13
+ // src/project.ts
14
+ import { Code } from '@motionscript/code';
15
+
16
+ export const nodes = [Code];
17
+ ```
18
+
19
+ ```ts
20
+ import type { CompositionDocument } from '@motionscript/sdk';
21
+ import { lines } from '@motionscript/code';
22
+
23
+ export default {
24
+ kind: 'composition',
25
+ nodes: [
26
+ { id: 'listing', type: 'code', parentId: null, layerOrder: 0,
27
+ props: { code: 'function greet(name) {\n return "hi";\n}', language: 'typescript',
28
+ theme: 'github-dark', fontSize: 28 } },
29
+ ],
30
+ commands: [
31
+ { id: 'call', type: 'append', targetId: 'listing', at: 0, duration: 0.6,
32
+ args: { code: '\ngreet("world");' } },
33
+ { id: 'focus', type: 'highlight', targetId: 'listing', at: 0.8, duration: 0.4,
34
+ args: { target: lines(2) } },
35
+ ],
36
+ } satisfies CompositionDocument;
37
+ ```
38
+
39
+ ## What's in here
40
+
41
+ - **`Code`** node, a hugging block of highlighted source. Set `code`,
42
+ `language` and `theme` to choose what is shown; tune `fontSize`,
43
+ `fontFamily`, `lineHeight`, `letterSpacing`, `showLineNumbers`,
44
+ `lineNumberGap` and `padding` for layout.
45
+ - **Token-level commands**: `append`/`prepend` (lines at the end or start),
46
+ `insert` (at a `[line, col]` position), `remove`, `replace`,
47
+ `highlight`/`resetHighlight` (dim everything outside a range, then restore
48
+ it), and `highlightRows`/`resetRowHighlight` (a coloured band behind whole
49
+ lines, which slides onto the next rows it is given and follows its lines
50
+ through an edit). A `to` command on `code` diffs the whole listing the same way.
51
+ - **Range helpers**: `word(line, col, length)`, `lines(from, to)` and the
52
+ chainable `CodeRanges` build the ranges those commands take.
53
+ - **Themes**: `github-dark`, `github-light`, `vscode-dark`, `vscode-light`
54
+ and a default, plus `registerCodeTheme` for your own.
55
+ `initSyntaxHighlighter(themes, langs)` preloads grammars so the first frame
56
+ renders fully highlighted.
57
+
58
+ Every token keeps a stable identity across edits, so concurrent animations at
59
+ different positions do not clobber each other while one is mid-flight.
60
+
61
+ This package is written against [`@motionscript/sdk`](https://github.com/motionscript-dev/motionscript/tree/main/packages/sdk), which it
62
+ takes as a peer dependency, and renders through whatever backend draws the
63
+ scene, such as [`@motionscript/browser`](https://github.com/motionscript-dev/motionscript/tree/main/packages/browser).
64
+
65
+ ## Languages, themes and line endings
66
+
67
+ - **Languages**: `typescript` (`ts`), `tsx`, `javascript` (`js`), `jsx`, `python`
68
+ (`py`), `json`, `html`, `css`, `rust`, `go`, `java`, `cpp` (`c++`), `php`,
69
+ `markdown` (`md`), `xml` and `yaml` (`yml`). `shell` is an alias of
70
+ `javascript`, because no shell grammar is bundled. Names are case-insensitive.
71
+ - **An unknown language, a grammar whose import is rejected and an unknown theme
72
+ name** each fall back (plain text, plain text and the default theme) and log one
73
+ `[Code]` warning per name for the life of the process. `''`, `text` and
74
+ `plaintext` mean plain text on purpose and say nothing.
75
+ - **`language` and `theme` can be tweened or bound.** The listing is re-coloured
76
+ from the props on the next frame, keeping the token identities a highlight
77
+ already names.
78
+ - **`\r\n` and a lone `\r` are line breaks**, in `code` and in the text a command
79
+ brings in; the node's `code` reads back with `\n` after its first command.
80
+ - **Edits are diffed within a budget.** Lines are paired by the same text at the
81
+ ends of the listing and by lines that occur once on both sides, so an edit
82
+ to a long listing costs in proportion to what changed. Only a run of changed
83
+ lines between two such anchors is compared pairwise, up to
84
+ `LINE_ALIGN_CELLS` (40,000) line pairs per run and 160,000 per edit; beyond
85
+ that its lines leave and enter as whole rows instead of morphing into each
86
+ other.
87
+ - **A command on a node that is not in a tree** runs its duration and writes
88
+ nothing. A document cannot reach this, because the validator rejects a command
89
+ that starts before its target is present.
90
+
91
+ ## Ranges, queries and what a copy holds
92
+
93
+ - **Ranges are 1-indexed** in lines and columns. `endLine`/`endCol` may be
94
+ `Infinity` (to the end of the line or document); a range that went through
95
+ JSON holds `null` there and reads the same. Positions clamp, so no command
96
+ throws for a bad range, and a `CodeRanges` chain serialises as its array of
97
+ ranges.
98
+ - **`findAllRanges(text)`, `findRangeAt(text, index)` and `findFirstRange(text)`**
99
+ on a `Code` return the literal matches in its current source as ranges.
100
+ - **`remove` is the document key; the method is `erase`**, because `Node.remove`
101
+ already takes a child out of the tree.
102
+ - **A `Code` takes no children and has no `fill`, `stroke` or `shadow`**; a panel
103
+ behind it is a parent `Rect`. It also refuses the ambient text style
104
+ (`<DefaultTextStyle>`): its columns are measured against its own face, so an
105
+ inherited family or weight would shape glyphs the geometry never measured.
106
+ - **`diffStrategy` and `phaseStrategy`** (`CodeStrategies`) replace how an edit is
107
+ paired and how its out/move/in windows are timed. They are constructor options,
108
+ not props, so a document cannot set them.
109
+ - **Highlighter state is per process.** Loaded grammars, registered themes and
110
+ token ids are shared by every `Code` in it.
111
+ - **`ms add code` copies only `node.ts`** (`registry.json`). The highlighter,
112
+ diff, layout and drawing stay in this package behind `src/engine.ts`, which the
113
+ copy imports from `@motionscript/code`; the grammars are dynamic imports, so a
114
+ host's bundler must resolve `@lezer/*` from where the package is installed.
115
+
116
+ ## The monospaced face is yours to supply
117
+
118
+ `Code` measures and draws every token in its `fontFamily`, `Fira Mono` by
119
+ default, and lines its columns up on that face's advances. The package ships no
120
+ font file and names no URL: list a monospaced face under that family in the
121
+ engine's asset manifest, alongside the node itself.
122
+
123
+ ```ts
124
+ const engine = await createBrowserEngine({
125
+ wasmUrl,
126
+ nodes: [Code],
127
+ manifest: {
128
+ image: {}, video: {}, audio: {},
129
+ font: {
130
+ 'Fira Mono@400': {
131
+ fontFamily: 'Fira Mono', fontWeight: 400,
132
+ src: '/fonts/FiraMono-Regular.ttf', sizeBytes: 0,
133
+ },
134
+ },
135
+ },
136
+ });
137
+ ```
138
+
139
+ A family the manifest does not list is never loaded. The loader warns once, and
140
+ the listing draws no text, or shapes in whichever other face is registered. To
141
+ use another face, set `fontFamily` on the node and list that family instead.
142
+
143
+ ## Usage
144
+
145
+ ```bash
146
+ npm install @motionscript/code @motionscript/sdk
147
+ ```
148
+
149
+ Or copy the node's source into your project with `ms add code`
150
+ (see [`@motionscript/cli`](https://github.com/motionscript-dev/motionscript/tree/main/packages/cli)). See the
151
+ [docs](https://motionscript.dev/docs) for the full feature set and API
152
+ reference.
153
+
154
+ ## Development
155
+
156
+ From the monorepo root:
157
+
158
+ ```bash
159
+ pnpm --filter @motionscript/code build
160
+ pnpm --filter @motionscript/code test
161
+ ```