@defold-typescript/types 0.21.0 → 0.22.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/index.d.ts +5 -0
- package/package.json +1 -1
- package/src/editor.ts +75 -0
- package/src/index.ts +5 -0
package/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/editor.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Editor scripts are loaded by the Defold *editor*, not the runtime engine: the
|
|
2
|
+
// editor `require`s the emitted chunk and reads the hooks table it returns. That
|
|
3
|
+
// makes them a fourth, disjoint script kind — lowered to a chunk-level
|
|
4
|
+
// `return <hooks table>` rather than the runtime kinds' flat top-level globals.
|
|
5
|
+
//
|
|
6
|
+
// This is the keystone surface only. The full typed `editor.*` global
|
|
7
|
+
// (`get`/`transact`/`command` + the editor-VM `http`/`json`/`zip`) and the
|
|
8
|
+
// per-kind API walls are a later slice, so a command's `run`/`active` receive a
|
|
9
|
+
// loosely-typed opts bag for now.
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A single command an editor script contributes: a label, the editor UI
|
|
13
|
+
* locations it appears in (e.g. `"Edit"`, `"Assets"`, `"Outline"`, `"View"`),
|
|
14
|
+
* and optional `active`/`run` hooks the editor calls with a command-context bag.
|
|
15
|
+
*/
|
|
16
|
+
export interface EditorCommand {
|
|
17
|
+
/** Menu/label text shown for the command. */
|
|
18
|
+
label: string;
|
|
19
|
+
/** Editor UI locations the command is offered in. */
|
|
20
|
+
locations: string[];
|
|
21
|
+
/**
|
|
22
|
+
* Declares the command's context arguments; the editor passes the resolved
|
|
23
|
+
* values to `active`/`run`. Loosely typed until the `editor.*` slice lands.
|
|
24
|
+
*/
|
|
25
|
+
query?: Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Called to decide whether the command is currently enabled. Omit to always
|
|
28
|
+
* enable. The opts bag is loosely typed until the `editor.*` slice lands.
|
|
29
|
+
*/
|
|
30
|
+
active?: (opts: Record<string, unknown>) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Called when the command is invoked. The opts bag is loosely typed until the
|
|
33
|
+
* `editor.*` slice lands.
|
|
34
|
+
*/
|
|
35
|
+
run?: (opts: Record<string, unknown>) => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The hooks table an editor script returns. Every hook is optional; the editor
|
|
40
|
+
* calls the ones present. Only the keystone hooks are typed here.
|
|
41
|
+
*/
|
|
42
|
+
export interface EditorScriptModule {
|
|
43
|
+
/** Returns the commands this script contributes to the editor. */
|
|
44
|
+
get_commands?: () => EditorCommand[];
|
|
45
|
+
/** Returns language-server descriptors this script contributes. */
|
|
46
|
+
get_language_servers?: () => unknown[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Type an editor script's hooks table. At runtime this is an identity function —
|
|
51
|
+
* it returns `module` unchanged; its only job is typing. The transpiler's
|
|
52
|
+
* `editor-script-erasure` pass rewrites the top-level `export default
|
|
53
|
+
* defineEditorScript({...})` into a chunk-level `return { ... }` (the shape the
|
|
54
|
+
* editor loads) and erases this import — zero runtime cost.
|
|
55
|
+
*
|
|
56
|
+
* @param module - the editor-script hooks table to type and return.
|
|
57
|
+
* @returns the same `module` object, now typed (identity at runtime).
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* export default defineEditorScript({
|
|
61
|
+
* get_commands: () => [
|
|
62
|
+
* { label: "Say Hi", locations: ["Edit"], run: () => print("hi") },
|
|
63
|
+
* ],
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function defineEditorScript<T extends EditorScriptModule>(
|
|
68
|
+
// Intersecting the non-module keys with `never` rejects an unknown hook key on
|
|
69
|
+
// a fresh object literal, while the `T` return keeps the call an identity over
|
|
70
|
+
// its exact argument type (a bare `<T extends ...>` would silently absorb the
|
|
71
|
+
// extra key into `T` and accept it).
|
|
72
|
+
module: T & Record<Exclude<keyof T, keyof EditorScriptModule>, never>,
|
|
73
|
+
): T {
|
|
74
|
+
return module;
|
|
75
|
+
}
|
package/src/index.ts
CHANGED