@ohos-ports/braneframe-plugin-script 0.0.0-stage → 0.6.7-beta.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 +8 -0
- package/README.md +3 -2
- package/dist/lib/browser/chunk-HAIUBDFW.mjs +21 -0
- package/dist/lib/browser/chunk-HAIUBDFW.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +670 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/meta.mjs +9 -0
- package/dist/lib/browser/meta.mjs.map +7 -0
- package/dist/types/src/ScriptPlugin.d.ts +7 -0
- package/dist/types/src/ScriptPlugin.d.ts.map +1 -0
- package/dist/types/src/compiler/compiler.d.ts +39 -0
- package/dist/types/src/compiler/compiler.d.ts.map +1 -0
- package/dist/types/src/compiler/compiler.test.d.ts +2 -0
- package/dist/types/src/compiler/compiler.test.d.ts.map +1 -0
- package/dist/types/src/compiler/index.d.ts +2 -0
- package/dist/types/src/compiler/index.d.ts.map +1 -0
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts +12 -0
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -0
- package/dist/types/src/components/FrameContainer/frame.d.ts +2 -0
- package/dist/types/src/components/FrameContainer/frame.d.ts.map +1 -0
- package/dist/types/src/components/FrameContainer/index.d.ts +2 -0
- package/dist/types/src/components/FrameContainer/index.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock.d.ts +16 -0
- package/dist/types/src/components/ScriptBlock.d.ts.map +1 -0
- package/dist/types/src/components/ScriptBlock.stories.d.ts +14 -0
- package/dist/types/src/components/ScriptBlock.stories.d.ts.map +1 -0
- package/dist/types/src/components/ScriptEditor/ScriptEditor.d.ts +10 -0
- package/dist/types/src/components/ScriptEditor/ScriptEditor.d.ts.map +1 -0
- package/dist/types/src/components/ScriptEditor/ScriptEditor.stories.d.ts +13 -0
- package/dist/types/src/components/ScriptEditor/ScriptEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/ScriptEditor/index.d.ts +2 -0
- package/dist/types/src/components/ScriptEditor/index.d.ts.map +1 -0
- package/dist/types/src/components/Splitter/Splitter.d.ts +15 -0
- package/dist/types/src/components/Splitter/Splitter.d.ts.map +1 -0
- package/dist/types/src/components/Splitter/Splitter.stories.d.ts +13 -0
- package/dist/types/src/components/Splitter/Splitter.stories.d.ts.map +1 -0
- package/dist/types/src/components/Splitter/index.d.ts +2 -0
- package/dist/types/src/components/Splitter/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +5 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +7 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/meta.d.ts +15 -0
- package/dist/types/src/meta.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +12 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +8 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/package.json +97 -4
- package/src/ScriptPlugin.tsx +176 -0
- package/src/compiler/compiler.test.ts +17 -0
- package/src/compiler/compiler.ts +210 -0
- package/src/compiler/index.ts +5 -0
- package/src/components/FrameContainer/FrameContainer.tsx +82 -0
- package/src/components/FrameContainer/frame.html +25 -0
- package/src/components/FrameContainer/frame.tsx +51 -0
- package/src/components/FrameContainer/index.ts +5 -0
- package/src/components/ScriptBlock.stories.tsx +61 -0
- package/src/components/ScriptBlock.tsx +129 -0
- package/src/components/ScriptEditor/ScriptEditor.stories.tsx +65 -0
- package/src/components/ScriptEditor/ScriptEditor.tsx +52 -0
- package/src/components/ScriptEditor/index.ts +5 -0
- package/src/components/ScriptEditor/themes/GitHubLight.json +348 -0
- package/src/components/ScriptEditor/themes/XCode.json +98 -0
- package/src/components/Splitter/Splitter.stories.tsx +37 -0
- package/src/components/Splitter/Splitter.tsx +52 -0
- package/src/components/Splitter/index.ts +5 -0
- package/src/components/index.ts +8 -0
- package/src/index.ts +13 -0
- package/src/meta.tsx +19 -0
- package/src/translations.ts +18 -0
- package/src/types.tsx +27 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCRIPT_PLUGIN,
|
|
3
|
+
meta_default
|
|
4
|
+
} from "./chunk-HAIUBDFW.mjs";
|
|
5
|
+
|
|
6
|
+
// packages/apps/plugins/plugin-script/src/ScriptPlugin.tsx
|
|
7
|
+
import { Code as Code2 } from "@phosphor-icons/react";
|
|
8
|
+
import React5, { useMemo as useMemo2 } from "react";
|
|
9
|
+
import { parseClientPlugin } from "@ohos-ports/braneframe-plugin-client";
|
|
10
|
+
import { createExtension, isActionGroup } from "@braneframe/plugin-graph";
|
|
11
|
+
import { SpaceAction } from "@ohos-ports/braneframe-plugin-space";
|
|
12
|
+
import { ScriptType, TextType } from "@ohos-ports/braneframe-types";
|
|
13
|
+
import { parseIntentPlugin, resolvePlugin, NavigationAction } from "@dxos/app-framework";
|
|
14
|
+
import { create } from "@dxos/echo-schema";
|
|
15
|
+
import { createDocAccessor } from "@dxos/react-client/echo";
|
|
16
|
+
import { Main } from "@dxos/react-ui";
|
|
17
|
+
import { baseSurface as baseSurface2, bottombarBlockPaddingEnd, fixedInsetFlexLayout, topbarBlockPaddingStart } from "@ohos-ports/dxos-react-ui-theme";
|
|
18
|
+
|
|
19
|
+
// packages/apps/plugins/plugin-script/src/components/FrameContainer/FrameContainer.tsx
|
|
20
|
+
import React, { useEffect, useRef } from "react";
|
|
21
|
+
import { clientServiceBundle } from "@dxos/client-protocol";
|
|
22
|
+
import { log } from "@dxos/log";
|
|
23
|
+
import { useClient } from "@dxos/react-client";
|
|
24
|
+
import { baseSurface, mx } from "@ohos-ports/dxos-react-ui-theme";
|
|
25
|
+
import { createProtoRpcPeer } from "@dxos/rpc";
|
|
26
|
+
import { createIFramePort } from "@dxos/rpc-tunnel";
|
|
27
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/apps/plugins/plugin-script/src/components/FrameContainer/FrameContainer.tsx";
|
|
28
|
+
var FrameContainer = ({ containerUrl, result, debug = true }) => {
|
|
29
|
+
const client = useClient();
|
|
30
|
+
const iframeRef = useRef(null);
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (iframeRef.current) {
|
|
33
|
+
const rpc = createProtoRpcPeer({
|
|
34
|
+
exposed: clientServiceBundle,
|
|
35
|
+
handlers: client._services.services,
|
|
36
|
+
port: createIFramePort({
|
|
37
|
+
channel: "frame",
|
|
38
|
+
iframe: iframeRef.current,
|
|
39
|
+
origin: "*"
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
rpc.open().catch((e) => log.catch(e, void 0, {
|
|
43
|
+
F: __dxlog_file,
|
|
44
|
+
L: 41,
|
|
45
|
+
S: void 0,
|
|
46
|
+
C: (f, a) => f(...a)
|
|
47
|
+
}));
|
|
48
|
+
return () => {
|
|
49
|
+
rpc.close().catch((e) => log.catch(e, void 0, {
|
|
50
|
+
F: __dxlog_file,
|
|
51
|
+
L: 43,
|
|
52
|
+
S: void 0,
|
|
53
|
+
C: (f, a) => f(...a)
|
|
54
|
+
}));
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}, [
|
|
58
|
+
iframeRef
|
|
59
|
+
]);
|
|
60
|
+
const sourceHash = Buffer.from(result.sourceHash).toString("hex");
|
|
61
|
+
const src = result.bundle && `${containerUrl}?ts=${sourceHash}#code=${encodeURIComponent(result.bundle)}`;
|
|
62
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("iframe", {
|
|
63
|
+
ref: iframeRef,
|
|
64
|
+
sandbox: "allow-scripts",
|
|
65
|
+
src,
|
|
66
|
+
style: {
|
|
67
|
+
width: "100%",
|
|
68
|
+
height: "100%"
|
|
69
|
+
}
|
|
70
|
+
}), debug && /* @__PURE__ */ React.createElement("div", {
|
|
71
|
+
className: "relative"
|
|
72
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
73
|
+
className: mx(baseSurface, "flex absolute right-2 bottom-2 w-[400px] h-[200px] ring rounded", "z-[100] overflow-x-hidden overflow-y-auto")
|
|
74
|
+
}, /* @__PURE__ */ React.createElement("pre", {
|
|
75
|
+
className: "text-xs whitespace-break-spaces break-all p-2"
|
|
76
|
+
}, JSON.stringify({
|
|
77
|
+
timestamp: result.timestamp,
|
|
78
|
+
sourceHash,
|
|
79
|
+
error: result.error,
|
|
80
|
+
src
|
|
81
|
+
}, void 0, 2)))));
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// packages/apps/plugins/plugin-script/src/components/ScriptBlock.tsx
|
|
85
|
+
import { Check, Play, Warning } from "@phosphor-icons/react";
|
|
86
|
+
import esbuildWasmURL from "esbuild-wasm/esbuild.wasm?url";
|
|
87
|
+
import React4, { useCallback, useEffect as useEffect2, useMemo, useState } from "react";
|
|
88
|
+
import { DocAccessor as DocAccessor2 } from "@dxos/react-client/echo";
|
|
89
|
+
import { DensityProvider, useThemeContext, Toolbar as Toolbar2, Button } from "@dxos/react-ui";
|
|
90
|
+
import { mx as mx4, getSize as getSize2 } from "@ohos-ports/dxos-react-ui-theme";
|
|
91
|
+
|
|
92
|
+
// packages/apps/plugins/plugin-script/src/components/ScriptEditor/ScriptEditor.tsx
|
|
93
|
+
import { autocompletion, completionKeymap } from "@codemirror/autocomplete";
|
|
94
|
+
import { javascript } from "@codemirror/lang-javascript";
|
|
95
|
+
import { EditorView, keymap, lineNumbers } from "@codemirror/view";
|
|
96
|
+
import { minimalSetup } from "codemirror";
|
|
97
|
+
import React2 from "react";
|
|
98
|
+
import { DocAccessor } from "@dxos/react-client/echo";
|
|
99
|
+
import { automerge, defaultTheme, useTextEditor } from "@dxos/react-ui-editor";
|
|
100
|
+
import { focusRing, mx as mx2 } from "@ohos-ports/dxos-react-ui-theme";
|
|
101
|
+
var ScriptEditor = ({ source, themeMode, className }) => {
|
|
102
|
+
const { parentRef, focusAttributes } = useTextEditor(() => ({
|
|
103
|
+
initialValue: DocAccessor.getValue(source),
|
|
104
|
+
extensions: [
|
|
105
|
+
// TODO(burdon): Use basic set-up (e.g., bracket matching).
|
|
106
|
+
// TODO(burdon): Use this in text editor (cancels highlight current line)
|
|
107
|
+
// basicSetup,
|
|
108
|
+
minimalSetup,
|
|
109
|
+
lineNumbers(),
|
|
110
|
+
// TODO(burdon): Syntax highlighting theme.
|
|
111
|
+
javascript({
|
|
112
|
+
typescript: true
|
|
113
|
+
}),
|
|
114
|
+
autocompletion({
|
|
115
|
+
activateOnTyping: false
|
|
116
|
+
}),
|
|
117
|
+
keymap.of([
|
|
118
|
+
...completionKeymap
|
|
119
|
+
]),
|
|
120
|
+
EditorView.baseTheme(defaultTheme),
|
|
121
|
+
EditorView.darkTheme.of(themeMode === "dark"),
|
|
122
|
+
EditorView.contentAttributes.of({
|
|
123
|
+
class: "!px-2"
|
|
124
|
+
}),
|
|
125
|
+
// TODO(burdon): Presence.
|
|
126
|
+
automerge(source)
|
|
127
|
+
]
|
|
128
|
+
}), [
|
|
129
|
+
source,
|
|
130
|
+
themeMode
|
|
131
|
+
]);
|
|
132
|
+
return /* @__PURE__ */ React2.createElement("div", {
|
|
133
|
+
...focusAttributes,
|
|
134
|
+
ref: parentRef,
|
|
135
|
+
className: mx2("flex grow rounded-sm", focusRing, className)
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// packages/apps/plugins/plugin-script/src/components/Splitter/Splitter.tsx
|
|
140
|
+
import { Code, SquareSplitHorizontal, Eye } from "@phosphor-icons/react";
|
|
141
|
+
import React3 from "react";
|
|
142
|
+
import { ToggleGroup, ToggleGroupItem, Toolbar } from "@dxos/react-ui";
|
|
143
|
+
import { getSize, mx as mx3 } from "@ohos-ports/dxos-react-ui-theme";
|
|
144
|
+
var Splitter = ({ view = "split", className, children = [] }) => {
|
|
145
|
+
const [left, right] = children;
|
|
146
|
+
return /* @__PURE__ */ React3.createElement("div", {
|
|
147
|
+
className: mx3("flex overflow-hidden grow", className)
|
|
148
|
+
}, left && view !== "preview" && /* @__PURE__ */ React3.createElement("div", {
|
|
149
|
+
className: "flex flex-1 shrink-0 overflow-hidden"
|
|
150
|
+
}, left), right && view !== "editor" && /* @__PURE__ */ React3.createElement("div", {
|
|
151
|
+
className: "flex flex-1 shrink-0 overflow-hidden"
|
|
152
|
+
}, right));
|
|
153
|
+
};
|
|
154
|
+
var SplitterSelector = ({ view, onChange, className }) => {
|
|
155
|
+
return /* @__PURE__ */ React3.createElement(Toolbar.Root, {
|
|
156
|
+
classNames: className
|
|
157
|
+
}, /* @__PURE__ */ React3.createElement(ToggleGroup, {
|
|
158
|
+
type: "single",
|
|
159
|
+
value: view,
|
|
160
|
+
onValueChange: (value) => onChange?.(value)
|
|
161
|
+
}, /* @__PURE__ */ React3.createElement(ToggleGroupItem, {
|
|
162
|
+
value: "editor"
|
|
163
|
+
}, /* @__PURE__ */ React3.createElement(Code, {
|
|
164
|
+
className: getSize(5)
|
|
165
|
+
})), /* @__PURE__ */ React3.createElement(ToggleGroupItem, {
|
|
166
|
+
value: "split"
|
|
167
|
+
}, /* @__PURE__ */ React3.createElement(SquareSplitHorizontal, {
|
|
168
|
+
className: getSize(5)
|
|
169
|
+
})), /* @__PURE__ */ React3.createElement(ToggleGroupItem, {
|
|
170
|
+
value: "preview"
|
|
171
|
+
}, /* @__PURE__ */ React3.createElement(Eye, {
|
|
172
|
+
className: getSize(5)
|
|
173
|
+
}))));
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// packages/apps/plugins/plugin-script/src/compiler/compiler.ts
|
|
177
|
+
import { build, initialize } from "esbuild-wasm";
|
|
178
|
+
import { subtleCrypto } from "@dxos/crypto";
|
|
179
|
+
import { invariant } from "@dxos/invariant";
|
|
180
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/apps/plugins/plugin-script/src/compiler/compiler.ts";
|
|
181
|
+
var initialized;
|
|
182
|
+
var initializeCompiler = async (options) => {
|
|
183
|
+
await (initialized ??= initialize({
|
|
184
|
+
wasmURL: options.wasmURL
|
|
185
|
+
}));
|
|
186
|
+
};
|
|
187
|
+
var Compiler = class {
|
|
188
|
+
constructor(_options) {
|
|
189
|
+
this._options = _options;
|
|
190
|
+
}
|
|
191
|
+
async compile(source) {
|
|
192
|
+
const { providedModules, ...options } = this._options;
|
|
193
|
+
const createResult = async (result) => {
|
|
194
|
+
return {
|
|
195
|
+
timestamp: Date.now(),
|
|
196
|
+
sourceHash: Buffer.from(await subtleCrypto.digest("SHA-256", Buffer.from(source))),
|
|
197
|
+
...result
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
if (this._options.platform === "browser") {
|
|
201
|
+
invariant(initialized, "Compiler not initialized.", {
|
|
202
|
+
F: __dxlog_file2,
|
|
203
|
+
L: 55,
|
|
204
|
+
S: this,
|
|
205
|
+
A: [
|
|
206
|
+
"initialized",
|
|
207
|
+
"'Compiler not initialized.'"
|
|
208
|
+
]
|
|
209
|
+
});
|
|
210
|
+
await initialized;
|
|
211
|
+
}
|
|
212
|
+
const imports = analyzeSourceFileImports(source);
|
|
213
|
+
try {
|
|
214
|
+
const result = await build({
|
|
215
|
+
...options,
|
|
216
|
+
metafile: true,
|
|
217
|
+
write: false,
|
|
218
|
+
entryPoints: [
|
|
219
|
+
"memory:main.tsx"
|
|
220
|
+
],
|
|
221
|
+
bundle: true,
|
|
222
|
+
format: "esm",
|
|
223
|
+
plugins: [
|
|
224
|
+
{
|
|
225
|
+
name: "memory",
|
|
226
|
+
setup: (build2) => {
|
|
227
|
+
build2.onResolve({
|
|
228
|
+
filter: /^memory:/
|
|
229
|
+
}, ({ path }) => {
|
|
230
|
+
return {
|
|
231
|
+
path: path.split(":")[1],
|
|
232
|
+
namespace: "memory"
|
|
233
|
+
};
|
|
234
|
+
});
|
|
235
|
+
build2.onLoad({
|
|
236
|
+
filter: /.*/,
|
|
237
|
+
namespace: "memory"
|
|
238
|
+
}, ({ path }) => {
|
|
239
|
+
const imports2 = [
|
|
240
|
+
"import React from 'react';"
|
|
241
|
+
];
|
|
242
|
+
if (path === "main.tsx") {
|
|
243
|
+
return {
|
|
244
|
+
contents: [
|
|
245
|
+
...imports2,
|
|
246
|
+
source
|
|
247
|
+
].join("\n"),
|
|
248
|
+
loader: "tsx"
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
for (const module of providedModules) {
|
|
253
|
+
build2.onResolve({
|
|
254
|
+
filter: new RegExp(`^${module}$`)
|
|
255
|
+
}, ({ path }) => {
|
|
256
|
+
return {
|
|
257
|
+
path,
|
|
258
|
+
namespace: "injected-module"
|
|
259
|
+
};
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
build2.onLoad({
|
|
263
|
+
filter: /.*/,
|
|
264
|
+
namespace: "injected-module"
|
|
265
|
+
}, ({ path }) => {
|
|
266
|
+
const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];
|
|
267
|
+
return {
|
|
268
|
+
contents: `
|
|
269
|
+
const { ${namedImports.join(",")} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];
|
|
270
|
+
export { ${namedImports.join(",")} };
|
|
271
|
+
export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;
|
|
272
|
+
`,
|
|
273
|
+
loader: "tsx"
|
|
274
|
+
};
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
]
|
|
279
|
+
});
|
|
280
|
+
return await createResult({
|
|
281
|
+
imports: this.analyzeImports(result),
|
|
282
|
+
bundle: result.outputFiles[0].text
|
|
283
|
+
});
|
|
284
|
+
} catch (err) {
|
|
285
|
+
return await createResult({
|
|
286
|
+
error: err
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
|
|
291
|
+
analyzeImports(result) {
|
|
292
|
+
invariant(result.outputFiles, void 0, {
|
|
293
|
+
F: __dxlog_file2,
|
|
294
|
+
L: 121,
|
|
295
|
+
S: this,
|
|
296
|
+
A: [
|
|
297
|
+
"result.outputFiles",
|
|
298
|
+
""
|
|
299
|
+
]
|
|
300
|
+
});
|
|
301
|
+
const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
|
|
302
|
+
return Object.values(result.metafile.outputs)[0].imports.map((entry) => {
|
|
303
|
+
const namedImports = [];
|
|
304
|
+
const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
|
|
305
|
+
if (parsedImport?.[2]) {
|
|
306
|
+
NAMED_IMPORTS_REGEX.lastIndex = 0;
|
|
307
|
+
const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
|
|
308
|
+
if (namedImportsMatch) {
|
|
309
|
+
namedImportsMatch[1].split(",").forEach((importName) => {
|
|
310
|
+
namedImports.push(importName.trim());
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
moduleUrl: entry.path,
|
|
316
|
+
defaultImport: !!parsedImport?.[1],
|
|
317
|
+
namedImports
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
analyzeSourceFileImports(code) {
|
|
322
|
+
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
323
|
+
return parsedImports.map((capture) => {
|
|
324
|
+
return {
|
|
325
|
+
defaultImportName: capture[1],
|
|
326
|
+
namedImports: capture[2]?.split(",").map((importName) => importName.trim()),
|
|
327
|
+
wildcardImportName: capture[3],
|
|
328
|
+
moduleIdentifier: capture[4],
|
|
329
|
+
quotes: capture[5]
|
|
330
|
+
};
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
var IMPORT_REGEX = /import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/gm;
|
|
335
|
+
var NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
|
|
336
|
+
var allMatches = (regex, str) => {
|
|
337
|
+
regex.lastIndex = 0;
|
|
338
|
+
let match;
|
|
339
|
+
const matches = [];
|
|
340
|
+
while (match = regex.exec(str)) {
|
|
341
|
+
matches.push(match);
|
|
342
|
+
}
|
|
343
|
+
return matches;
|
|
344
|
+
};
|
|
345
|
+
var analyzeSourceFileImports = (code) => {
|
|
346
|
+
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
347
|
+
return parsedImports.map((capture) => {
|
|
348
|
+
return {
|
|
349
|
+
defaultImportName: capture[1],
|
|
350
|
+
namedImports: capture[2]?.trim().slice(1, -1).split(",").map((importName) => importName.trim()),
|
|
351
|
+
wildcardImportName: capture[3],
|
|
352
|
+
moduleIdentifier: capture[4],
|
|
353
|
+
quotes: capture[5]
|
|
354
|
+
};
|
|
355
|
+
});
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
// packages/apps/plugins/plugin-script/src/components/ScriptBlock.tsx
|
|
359
|
+
var PROVIDED_MODULES = [
|
|
360
|
+
"react",
|
|
361
|
+
"react-dom/client",
|
|
362
|
+
"@dxos/client",
|
|
363
|
+
"@dxos/react-client",
|
|
364
|
+
"@dxos/react-client/echo",
|
|
365
|
+
"@ohos-ports/braneframe-plugin-explorer",
|
|
366
|
+
"@ohos-ports/braneframe-types"
|
|
367
|
+
];
|
|
368
|
+
var ScriptBlock = ({ id, source, view: controlledView, hideSelector, classes, containerUrl }) => {
|
|
369
|
+
const { themeMode } = useThemeContext();
|
|
370
|
+
const [view, setView] = useState(controlledView ?? "editor");
|
|
371
|
+
useEffect2(() => handleSetView(controlledView ?? "editor"), [
|
|
372
|
+
controlledView
|
|
373
|
+
]);
|
|
374
|
+
const [result, setResult] = useState();
|
|
375
|
+
const compiler = useMemo(() => new Compiler({
|
|
376
|
+
platform: "browser",
|
|
377
|
+
providedModules: PROVIDED_MODULES
|
|
378
|
+
}), []);
|
|
379
|
+
useEffect2(() => {
|
|
380
|
+
void initializeCompiler({
|
|
381
|
+
wasmURL: esbuildWasmURL
|
|
382
|
+
});
|
|
383
|
+
}, []);
|
|
384
|
+
useEffect2(() => {
|
|
385
|
+
const t = setTimeout(async () => {
|
|
386
|
+
const result2 = await compiler.compile(DocAccessor2.getValue(source));
|
|
387
|
+
setResult(result2);
|
|
388
|
+
});
|
|
389
|
+
return () => clearTimeout(t);
|
|
390
|
+
}, [
|
|
391
|
+
source,
|
|
392
|
+
id
|
|
393
|
+
]);
|
|
394
|
+
const handleSetView = useCallback((view2) => {
|
|
395
|
+
setView(view2);
|
|
396
|
+
if (!result && view2 !== "editor") {
|
|
397
|
+
void handleExec(false);
|
|
398
|
+
}
|
|
399
|
+
}, [
|
|
400
|
+
result
|
|
401
|
+
]);
|
|
402
|
+
const handleExec = useCallback(async (auto = true) => {
|
|
403
|
+
const result2 = await compiler.compile(DocAccessor2.getValue(source));
|
|
404
|
+
setResult(result2);
|
|
405
|
+
if (auto && view === "editor") {
|
|
406
|
+
setView("preview");
|
|
407
|
+
}
|
|
408
|
+
}, [
|
|
409
|
+
source,
|
|
410
|
+
view
|
|
411
|
+
]);
|
|
412
|
+
if (!source) {
|
|
413
|
+
return null;
|
|
414
|
+
}
|
|
415
|
+
return /* @__PURE__ */ React4.createElement("div", {
|
|
416
|
+
className: mx4("flex flex-col grow overflow-hidden", classes?.root)
|
|
417
|
+
}, !hideSelector && /* @__PURE__ */ React4.createElement(DensityProvider, {
|
|
418
|
+
density: "fine"
|
|
419
|
+
}, /* @__PURE__ */ React4.createElement(Toolbar2.Root, {
|
|
420
|
+
classNames: mx4("mb-2", classes?.toolbar)
|
|
421
|
+
}, /* @__PURE__ */ React4.createElement(SplitterSelector, {
|
|
422
|
+
view,
|
|
423
|
+
onChange: handleSetView
|
|
424
|
+
}), /* @__PURE__ */ React4.createElement("div", {
|
|
425
|
+
className: "grow"
|
|
426
|
+
}), result?.bundle && !result?.error && /* @__PURE__ */ React4.createElement("div", {
|
|
427
|
+
title: String(result.error)
|
|
428
|
+
}, /* @__PURE__ */ React4.createElement(Check, {
|
|
429
|
+
className: mx4(getSize2(5), "text-green-500")
|
|
430
|
+
})), result?.error && /* @__PURE__ */ React4.createElement("div", {
|
|
431
|
+
title: String(result.error)
|
|
432
|
+
}, /* @__PURE__ */ React4.createElement(Warning, {
|
|
433
|
+
className: mx4(getSize2(5), "text-orange-500")
|
|
434
|
+
})), /* @__PURE__ */ React4.createElement(Button, {
|
|
435
|
+
variant: "ghost",
|
|
436
|
+
onClick: () => handleExec()
|
|
437
|
+
}, /* @__PURE__ */ React4.createElement(Play, {
|
|
438
|
+
className: getSize2(5)
|
|
439
|
+
})))), /* @__PURE__ */ React4.createElement(Splitter, {
|
|
440
|
+
view
|
|
441
|
+
}, /* @__PURE__ */ React4.createElement(ScriptEditor, {
|
|
442
|
+
source,
|
|
443
|
+
themeMode
|
|
444
|
+
}), result && /* @__PURE__ */ React4.createElement(FrameContainer, {
|
|
445
|
+
key: id,
|
|
446
|
+
result,
|
|
447
|
+
containerUrl
|
|
448
|
+
})));
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// packages/apps/plugins/plugin-script/src/translations.ts
|
|
452
|
+
var translations_default = [
|
|
453
|
+
{
|
|
454
|
+
"en-US": {
|
|
455
|
+
[SCRIPT_PLUGIN]: {
|
|
456
|
+
"plugin name": "Scripts",
|
|
457
|
+
"object title placeholder": "New script",
|
|
458
|
+
"create object label": "Create script",
|
|
459
|
+
"create stack section label": "Create script"
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
];
|
|
464
|
+
|
|
465
|
+
// packages/apps/plugins/plugin-script/src/types.tsx
|
|
466
|
+
var ScriptAction;
|
|
467
|
+
(function(ScriptAction2) {
|
|
468
|
+
ScriptAction2[ScriptAction2["CREATE"] = `${SCRIPT_PLUGIN}/create`] = "CREATE";
|
|
469
|
+
})(ScriptAction || (ScriptAction = {}));
|
|
470
|
+
|
|
471
|
+
// packages/apps/plugins/plugin-script/src/ScriptPlugin.tsx
|
|
472
|
+
var ScriptPlugin = ({ containerUrl }) => {
|
|
473
|
+
return {
|
|
474
|
+
meta: meta_default,
|
|
475
|
+
provides: {
|
|
476
|
+
metadata: {
|
|
477
|
+
records: {
|
|
478
|
+
[ScriptType.typename]: {
|
|
479
|
+
placeholder: [
|
|
480
|
+
"object title placeholder",
|
|
481
|
+
{
|
|
482
|
+
ns: SCRIPT_PLUGIN
|
|
483
|
+
}
|
|
484
|
+
],
|
|
485
|
+
icon: (props) => /* @__PURE__ */ React5.createElement(Code2, props),
|
|
486
|
+
iconSymbol: "ph--code--regular"
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
translations: translations_default,
|
|
491
|
+
echo: {
|
|
492
|
+
schema: [
|
|
493
|
+
ScriptType
|
|
494
|
+
]
|
|
495
|
+
},
|
|
496
|
+
graph: {
|
|
497
|
+
builder: (plugins) => {
|
|
498
|
+
const client = resolvePlugin(plugins, parseClientPlugin)?.provides.client;
|
|
499
|
+
const dispatch = resolvePlugin(plugins, parseIntentPlugin)?.provides.intent.dispatch;
|
|
500
|
+
if (!client || !dispatch) {
|
|
501
|
+
return [];
|
|
502
|
+
}
|
|
503
|
+
return createExtension({
|
|
504
|
+
id: ScriptAction.CREATE,
|
|
505
|
+
filter: (node) => isActionGroup(node) && node.id.startsWith(SpaceAction.ADD_OBJECT),
|
|
506
|
+
actions: ({ node }) => {
|
|
507
|
+
const id = node.id.split("/").at(-1);
|
|
508
|
+
const [spaceId, objectId] = id?.split(":") ?? [];
|
|
509
|
+
const space = client.spaces.get().find((space2) => space2.id === spaceId);
|
|
510
|
+
const object = objectId && space?.db.getObjectById(objectId);
|
|
511
|
+
const target = objectId ? object : space;
|
|
512
|
+
if (!target) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
return [
|
|
516
|
+
{
|
|
517
|
+
id: `${SCRIPT_PLUGIN}/create/${node.id}`,
|
|
518
|
+
data: async () => {
|
|
519
|
+
await dispatch([
|
|
520
|
+
{
|
|
521
|
+
plugin: SCRIPT_PLUGIN,
|
|
522
|
+
action: ScriptAction.CREATE
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
action: SpaceAction.ADD_OBJECT,
|
|
526
|
+
data: {
|
|
527
|
+
target
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
action: NavigationAction.OPEN
|
|
532
|
+
}
|
|
533
|
+
]);
|
|
534
|
+
},
|
|
535
|
+
properties: {
|
|
536
|
+
label: [
|
|
537
|
+
"create object label",
|
|
538
|
+
{
|
|
539
|
+
ns: SCRIPT_PLUGIN
|
|
540
|
+
}
|
|
541
|
+
],
|
|
542
|
+
icon: (props) => /* @__PURE__ */ React5.createElement(Code2, props),
|
|
543
|
+
iconSymbol: "ph--code--regular",
|
|
544
|
+
testId: "scriptPlugin.createObject"
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
];
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
},
|
|
552
|
+
stack: {
|
|
553
|
+
creators: [
|
|
554
|
+
{
|
|
555
|
+
id: "create-stack-section-script",
|
|
556
|
+
testId: "scriptPlugin.createSectionSpaceScript",
|
|
557
|
+
label: [
|
|
558
|
+
"create stack section label",
|
|
559
|
+
{
|
|
560
|
+
ns: SCRIPT_PLUGIN
|
|
561
|
+
}
|
|
562
|
+
],
|
|
563
|
+
icon: (props) => /* @__PURE__ */ React5.createElement(Code2, props),
|
|
564
|
+
intent: {
|
|
565
|
+
plugin: SCRIPT_PLUGIN,
|
|
566
|
+
action: ScriptAction.CREATE
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
]
|
|
570
|
+
},
|
|
571
|
+
surface: {
|
|
572
|
+
component: ({ data, role }) => {
|
|
573
|
+
switch (role) {
|
|
574
|
+
case "main":
|
|
575
|
+
return data.active instanceof ScriptType ? /* @__PURE__ */ React5.createElement(Main.Content, {
|
|
576
|
+
classNames: [
|
|
577
|
+
baseSurface2,
|
|
578
|
+
fixedInsetFlexLayout,
|
|
579
|
+
topbarBlockPaddingStart,
|
|
580
|
+
bottombarBlockPaddingEnd
|
|
581
|
+
]
|
|
582
|
+
}, /* @__PURE__ */ React5.createElement(ScriptBlockWrapper, {
|
|
583
|
+
// prettier-ignore
|
|
584
|
+
script: data.active,
|
|
585
|
+
containerUrl,
|
|
586
|
+
classes: {
|
|
587
|
+
toolbar: "px-1"
|
|
588
|
+
}
|
|
589
|
+
})) : null;
|
|
590
|
+
case "slide":
|
|
591
|
+
return data.slide instanceof ScriptType ? /* @__PURE__ */ React5.createElement(ScriptBlockWrapper, {
|
|
592
|
+
// prettier-ignore
|
|
593
|
+
script: data.slide,
|
|
594
|
+
containerUrl,
|
|
595
|
+
classes: {
|
|
596
|
+
toolbar: "p-24"
|
|
597
|
+
},
|
|
598
|
+
view: "preview",
|
|
599
|
+
hideSelector: true
|
|
600
|
+
}) : null;
|
|
601
|
+
case "section":
|
|
602
|
+
return data.object instanceof ScriptType ? /* @__PURE__ */ React5.createElement(ScriptBlockWrapper, {
|
|
603
|
+
// prettier-ignore
|
|
604
|
+
script: data.object,
|
|
605
|
+
containerUrl,
|
|
606
|
+
classes: {
|
|
607
|
+
root: "h-[400px] py-2"
|
|
608
|
+
}
|
|
609
|
+
}) : null;
|
|
610
|
+
}
|
|
611
|
+
return null;
|
|
612
|
+
}
|
|
613
|
+
},
|
|
614
|
+
intent: {
|
|
615
|
+
resolver: (intent, plugins) => {
|
|
616
|
+
switch (intent.action) {
|
|
617
|
+
case ScriptAction.CREATE: {
|
|
618
|
+
return {
|
|
619
|
+
data: create(ScriptType, {
|
|
620
|
+
source: create(TextType, {
|
|
621
|
+
content: example
|
|
622
|
+
})
|
|
623
|
+
})
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
};
|
|
632
|
+
var ScriptBlockWrapper = ({ script, ...props }) => {
|
|
633
|
+
const source = useMemo2(() => script.source && createDocAccessor(script.source, [
|
|
634
|
+
"content"
|
|
635
|
+
]), [
|
|
636
|
+
script.source
|
|
637
|
+
]);
|
|
638
|
+
return source ? /* @__PURE__ */ React5.createElement(ScriptBlock, {
|
|
639
|
+
id: script.id,
|
|
640
|
+
source,
|
|
641
|
+
...props
|
|
642
|
+
}) : null;
|
|
643
|
+
};
|
|
644
|
+
var example = [
|
|
645
|
+
"import { Filter, useQuery, useSpaces} from '@dxos/react-client/echo';",
|
|
646
|
+
"import { Chart } from '@ohos-ports/braneframe-plugin-explorer';",
|
|
647
|
+
"",
|
|
648
|
+
"export default () => {",
|
|
649
|
+
" const spaces = useSpaces();",
|
|
650
|
+
" const space = spaces[1];",
|
|
651
|
+
" const objects = useQuery(space, Filter.typename('example.com/type/contact'));",
|
|
652
|
+
" return <Chart items={objects} accessor={object => ({ x: object.lat, y: object.lng })} />",
|
|
653
|
+
"}"
|
|
654
|
+
].join("\n");
|
|
655
|
+
|
|
656
|
+
// packages/apps/plugins/plugin-script/src/index.ts
|
|
657
|
+
var src_default = ScriptPlugin;
|
|
658
|
+
export {
|
|
659
|
+
Compiler,
|
|
660
|
+
FrameContainer,
|
|
661
|
+
ScriptAction,
|
|
662
|
+
ScriptBlock,
|
|
663
|
+
ScriptEditor,
|
|
664
|
+
ScriptPlugin,
|
|
665
|
+
Splitter,
|
|
666
|
+
SplitterSelector,
|
|
667
|
+
src_default as default,
|
|
668
|
+
initializeCompiler
|
|
669
|
+
};
|
|
670
|
+
//# sourceMappingURL=index.mjs.map
|