buttonrig 1.0.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/package.json +17 -0
- package/src/main.ts +86 -0
- package/src/types.ts +117 -0
- package/tsconfig.json +43 -0
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "buttonrig",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "API for your plugin to work with the Button Rig App.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/main.js",
|
|
7
|
+
"types": "dist/main.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "npx tsc"
|
|
10
|
+
},
|
|
11
|
+
"author": "Button Rig",
|
|
12
|
+
"license": "Apache 2.0",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "^24.9.1",
|
|
15
|
+
"typescript": "^5.9.3"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {
|
|
2
|
+
newMessage,
|
|
3
|
+
type RxPayload,
|
|
4
|
+
type TxPayload,
|
|
5
|
+
ErrorPayload,
|
|
6
|
+
newSaveHandlerArgs,
|
|
7
|
+
RxLoadHandlerArgsPayload,
|
|
8
|
+
RxFilePickPayload,
|
|
9
|
+
RxFolderPickPayload,
|
|
10
|
+
RxFilesPickPayload,
|
|
11
|
+
} from "./types.js";
|
|
12
|
+
|
|
13
|
+
export function saveHandlerArgs(args: string[] | ErrorPayload) {
|
|
14
|
+
let txPayload: TxPayload;
|
|
15
|
+
if (args instanceof ErrorPayload) {
|
|
16
|
+
txPayload = args;
|
|
17
|
+
} else {
|
|
18
|
+
txPayload = newSaveHandlerArgs(args);
|
|
19
|
+
}
|
|
20
|
+
postMessage(txPayload);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function loadHandlerArgs(fn: (handlerArgs: string[]) => void) {
|
|
24
|
+
addEventListener("loadHandlerArgs", (payload) => {
|
|
25
|
+
let loadHandlerArgsPayload = payload as RxLoadHandlerArgsPayload;
|
|
26
|
+
fn(loadHandlerArgsPayload.loadHandlerArgs.handlerArgs);
|
|
27
|
+
});
|
|
28
|
+
postMessage("readyToReceive");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function pickFile(extensions: string[]): Promise<string | null> {
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
postMessage({
|
|
34
|
+
pickFile: {
|
|
35
|
+
extensions,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
addEventListener("filePick", (payload) => {
|
|
39
|
+
let filePickPayload = payload as RxFilePickPayload;
|
|
40
|
+
resolve(filePickPayload.filePick.file);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function pickFiles(extensions: string[]): Promise<string[]> {
|
|
46
|
+
return new Promise((resolve) => {
|
|
47
|
+
postMessage({
|
|
48
|
+
pickFiles: {
|
|
49
|
+
extensions,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
addEventListener("filesPick", (payload) => {
|
|
53
|
+
let filesPickPayload = payload as RxFilesPickPayload;
|
|
54
|
+
resolve(filesPickPayload.filesPick.files);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function pickFolder(): Promise<string | null> {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
postMessage("pickFolder");
|
|
62
|
+
addEventListener("folderPick", (payload) => {
|
|
63
|
+
let folderPickPayload = payload as RxFolderPickPayload;
|
|
64
|
+
resolve(folderPickPayload.folderPick.folder);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function addEventListener(eventType: string, fn: (payload: RxPayload) => void) {
|
|
70
|
+
window.addEventListener("message", (event) => {
|
|
71
|
+
if (
|
|
72
|
+
!(
|
|
73
|
+
event.data.event == eventType ||
|
|
74
|
+
Object.keys(event.data.event)[0] == eventType
|
|
75
|
+
)
|
|
76
|
+
) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
fn(event.data.event as RxPayload);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function postMessage(payload: TxPayload) {
|
|
84
|
+
let message = newMessage(payload);
|
|
85
|
+
window.parent.postMessage(message, "*");
|
|
86
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
export class ErrorPayload {
|
|
2
|
+
error: {
|
|
3
|
+
message: string | null;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
constructor(message: string | null) {
|
|
7
|
+
this.error = {
|
|
8
|
+
message,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Message {
|
|
14
|
+
buttonPluginActionId: string;
|
|
15
|
+
payload: TxPayload;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function newMessage(payload: TxPayload): Message {
|
|
19
|
+
return {
|
|
20
|
+
buttonPluginActionId: window.name,
|
|
21
|
+
payload: payload,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type TxPayload =
|
|
26
|
+
| TxPickFilePayload
|
|
27
|
+
| TxPickFilesPayload
|
|
28
|
+
| TxPickFolder
|
|
29
|
+
| TxReadyToReceive
|
|
30
|
+
| TxSaveHandlerArgsPayload
|
|
31
|
+
| ErrorPayload;
|
|
32
|
+
|
|
33
|
+
export interface TxPickFilePayload {
|
|
34
|
+
pickFile: {
|
|
35
|
+
extensions: string[];
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface TxPickFilesPayload {
|
|
40
|
+
pickFiles: {
|
|
41
|
+
extensions: string[];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function newSaveHandlerArgs(
|
|
46
|
+
handlerArgs: string[]
|
|
47
|
+
): TxSaveHandlerArgsPayload {
|
|
48
|
+
return {
|
|
49
|
+
saveHandlerArgs: {
|
|
50
|
+
handlerArgs,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type RxPayload =
|
|
56
|
+
| RxFilePickPayload
|
|
57
|
+
| RxFilesPickPayload
|
|
58
|
+
| RxFolderPickPayload
|
|
59
|
+
| RxLoadHandlerArgsPayload
|
|
60
|
+
| null;
|
|
61
|
+
|
|
62
|
+
export class RxLoadHandlerArgsPayload {
|
|
63
|
+
loadHandlerArgs: {
|
|
64
|
+
handlerArgs: string[];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
constructor(handlerArgs: string[]) {
|
|
68
|
+
this.loadHandlerArgs = {
|
|
69
|
+
handlerArgs,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class RxFilePickPayload {
|
|
75
|
+
filePick: {
|
|
76
|
+
file: string | null;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
constructor() {
|
|
80
|
+
this.filePick = {
|
|
81
|
+
file: null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class RxFilesPickPayload {
|
|
87
|
+
filesPick: {
|
|
88
|
+
files: string[];
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
constructor() {
|
|
92
|
+
this.filesPick = {
|
|
93
|
+
files: [],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class RxFolderPickPayload {
|
|
99
|
+
folderPick: {
|
|
100
|
+
folder: string | null;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
constructor() {
|
|
104
|
+
this.folderPick = {
|
|
105
|
+
folder: null,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface TxSaveHandlerArgsPayload {
|
|
111
|
+
saveHandlerArgs: {
|
|
112
|
+
handlerArgs: string[];
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type TxReadyToReceive = "readyToReceive";
|
|
117
|
+
export type TxPickFolder = "pickFolder";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// File Layout
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
|
|
7
|
+
// Environment Settings
|
|
8
|
+
// See also https://aka.ms/tsconfig/module
|
|
9
|
+
"module": "nodenext",
|
|
10
|
+
"target": "esnext",
|
|
11
|
+
"types": [],
|
|
12
|
+
// For nodejs:
|
|
13
|
+
// "lib": ["esnext"],
|
|
14
|
+
// "types": ["node"],
|
|
15
|
+
// and npm install -D @types/node
|
|
16
|
+
|
|
17
|
+
// Other Outputs
|
|
18
|
+
"sourceMap": true,
|
|
19
|
+
"declaration": true,
|
|
20
|
+
"declarationMap": true,
|
|
21
|
+
|
|
22
|
+
// Stricter Typechecking Options
|
|
23
|
+
"noUncheckedIndexedAccess": true,
|
|
24
|
+
"exactOptionalPropertyTypes": true,
|
|
25
|
+
|
|
26
|
+
// Style Options
|
|
27
|
+
// "noImplicitReturns": true,
|
|
28
|
+
// "noImplicitOverride": true,
|
|
29
|
+
// "noUnusedLocals": true,
|
|
30
|
+
// "noUnusedParameters": true,
|
|
31
|
+
// "noFallthroughCasesInSwitch": true,
|
|
32
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
33
|
+
|
|
34
|
+
// Recommended Options
|
|
35
|
+
"strict": true,
|
|
36
|
+
"jsx": "react-jsx",
|
|
37
|
+
"verbatimModuleSyntax": true,
|
|
38
|
+
"isolatedModules": true,
|
|
39
|
+
"noUncheckedSideEffectImports": true,
|
|
40
|
+
"moduleDetection": "force",
|
|
41
|
+
"skipLibCheck": true
|
|
42
|
+
}
|
|
43
|
+
}
|