@blocklet/pages-kit 0.4.11 → 0.4.13-beta.20250227

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.
@@ -61,20 +61,21 @@ function injectGlobalComponents() {
61
61
  '@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer: CustomComponentRenderer_1.default },
62
62
  '@blocklet/pages-kit/builtin/dayjs': dayjs,
63
63
  '@blocklet/pages-kit/builtin/utils': utils,
64
+ '@blocklet/pages-kit/builtin/stream': stream,
64
65
  '@blocklet/pages-kit/builtin/react': react,
65
66
  '@blocklet/pages-kit/builtin/emotion/css': emotionCss,
66
67
  '@blocklet/pages-kit/builtin/mui/material': muiMaterial,
67
68
  '@blocklet/pages-kit/builtin/mui/lab': muiLab,
68
69
  '@blocklet/pages-kit/builtin/react-hook-form': reactHookForm,
69
70
  '@blocklet/pages-kit/builtin/iconify/react': iconifyReact,
71
+ //
70
72
  '@blocklet/pages-kit/builtin/zustand': zustand,
71
73
  '@blocklet/pages-kit/builtin/zustand/middleware/immer': zustandMiddlewareImmer,
72
74
  '@blocklet/pages-kit/builtin/immer': immer,
73
75
  '@blocklet/pages-kit/builtin/react-wrap-balancer': reactWrapBalancer,
74
- '@blocklet/pages-kit/builtin/stream': stream,
76
+ '@blocklet/pages-kit/builtin/react-router-dom': reactRouterDOM,
75
77
  '@blocklet/pages-kit/builtin/session': session,
76
78
  '@blocklet/pages-kit/builtin/locale': locale,
77
- '@blocklet/pages-kit/builtin/react-router-dom': reactRouterDOM,
78
79
  '@blocklet/pages-kit/builtin/arcblock/ux': arcblockUx,
79
80
  '@blocklet/pages-kit/builtin/page/header': pageHeader,
80
81
  '@blocklet/pages-kit/builtin/components': components,
@@ -86,7 +87,21 @@ function injectGlobalComponents() {
86
87
  '@blocklet/pages-kit/builtin/async/ai-runtime': aiRuntime,
87
88
  },
88
89
  require(module) {
89
- return win[builtin_1.BuiltinModulesGlobalVariableName].modules[module];
90
+ var _a;
91
+ if ((_a = win[builtin_1.BuiltinModulesGlobalVariableName]) === null || _a === void 0 ? void 0 : _a.modules[module]) {
92
+ return win[builtin_1.BuiltinModulesGlobalVariableName].modules[module];
93
+ }
94
+ // Try loading from CDN or dynamic import
95
+ try {
96
+ // TODO: 需要支持动态加载
97
+ // const module = await import(/* webpackIgnore: true */ `https://cdn.jsdelivr.net/npm/${moduleName}`);
98
+ // return module;
99
+ return null;
100
+ }
101
+ catch (err) {
102
+ console.warn(`Failed to load module ${module}`, err);
103
+ return null;
104
+ }
90
105
  },
91
106
  };
92
107
  win.React = react_1.default;
@@ -23,13 +23,15 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
23
23
  const property = (_b = (_a = component === null || component === void 0 ? void 0 : component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
24
24
  if (!property)
25
25
  return [];
26
- return [property.key, parsePropertyValue(property, value, { locale, defaultLocale })];
26
+ // // if key is undefined, use id
27
+ return [property.key || property.id, parsePropertyValue(property, value, { locale, defaultLocale })];
27
28
  }));
28
29
  while (component) {
29
30
  assignNullableFields(properties, Object.fromEntries(Object.values((_a = component.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
30
31
  var _a, _b, _c, _d, _e;
31
32
  return [
32
- data.key,
33
+ // if key is undefined, use id
34
+ data.key || data.id,
33
35
  parsePropertyValue(data, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue, { locale, defaultLocale }),
34
36
  ];
35
37
  })));
@@ -12,8 +12,10 @@ import { joinURL } from 'ufo';
12
12
  const PAGES_KIT_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
13
13
  export function preloadComponents(input) {
14
14
  return __awaiter(this, void 0, void 0, function* () {
15
- var _a, _b;
16
- const prefix = ((_b = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_DID)) === null || _b === void 0 ? void 0 : _b.mountPoint) || '/';
15
+ var _a, _b, _c;
16
+ const prefix = ((_b = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_DID && i.status === 'running')) === null || _b === void 0 ? void 0 : _b.mountPoint) ||
17
+ ((_c = window === null || window === void 0 ? void 0 : window.blocklet) === null || _c === void 0 ? void 0 : _c.prefix) ||
18
+ '/';
17
19
  return axios.post(joinURL(prefix, '/api/components/preload'), input).then((res) => res.data);
18
20
  });
19
21
  }
@@ -25,7 +25,7 @@ export default function CustomComponentRenderer(_a) {
25
25
  var { dev } = _a, props = __rest(_a, ["dev"]);
26
26
  const inheritedDev = useDev();
27
27
  const BuiltinComponent = BuiltinComponents[props.componentId];
28
- return (_jsx(ErrorBoundary, { FallbackComponent: (props === null || props === void 0 ? void 0 : props.fallbackRender) || ErrorView, resetKeys: [Date.now()], children: _jsx(DevProvider, { dev: dev !== null && dev !== void 0 ? dev : inheritedDev, children: BuiltinComponent ? (_jsx(BuiltinComponent, Object.assign({}, props))) : (_jsx(ComponentRenderer, Object.assign({}, props, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId }))) }) }));
28
+ return (_jsx(ErrorBoundary, { FallbackComponent: (props === null || props === void 0 ? void 0 : props.fallbackRender) || ErrorView, resetKeys: [Date.now()], children: _jsx(DevProvider, { dev: dev !== null && dev !== void 0 ? dev : inheritedDev, children: BuiltinComponent ? (_jsx(BuiltinComponent, Object.assign({}, props))) : (_jsx(ComponentRenderer, Object.assign({}, props, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId, renderType: props.renderType }))) }) }));
29
29
  }
30
30
  const BuiltinComponents = {
31
31
  'blocklet-react-component': BlockletReactComponentRenderer,
@@ -35,20 +35,25 @@ function ErrorView({ error }) {
35
35
  }
36
36
  function ComponentRenderer(_a) {
37
37
  var _b;
38
- var { renderCount = 0, blockletId, blockletTitle, componentName } = _a, props = __rest(_a, ["renderCount", "blockletId", "blockletTitle", "componentName"]);
38
+ var { renderCount = 0, blockletId, blockletTitle, componentName, renderType = 'view' } = _a, props = __rest(_a, ["renderCount", "blockletId", "blockletTitle", "componentName", "renderType"]);
39
39
  const dev = useDev();
40
40
  if (renderCount > MAXIMUM_RENDER_STACK_SIZE) {
41
41
  throw new Error('Maximum render stack size exceeded');
42
42
  }
43
43
  const component = useComponent(Object.assign(Object.assign({}, props), { dev }));
44
+ const ctx = useCustomComponentRenderer();
44
45
  if (component === null || component === void 0 ? void 0 : component.error) {
45
- // throw to outer error boundary
46
46
  throw component.error;
47
47
  }
48
- const ctx = useCustomComponentRenderer();
49
- if (component === null || component === void 0 ? void 0 : component.Component) {
50
- const children = (_jsx(Renderer, Object.assign({ renderCount: renderCount + 1 }, props, { Component: component.Component, props: Object.assign(Object.assign({}, component.props), props.props) })));
51
- return (ctx === null || ctx === void 0 ? void 0 : ctx.customRendererComponent) ? (_jsx(ctx.customRendererComponent, { Component: component.Component, children: children })) : (children);
48
+ const renderComponentChildren = (Component) => {
49
+ const children = (_jsx(Renderer, Object.assign({ renderCount: renderCount + 1 }, props, { Component: Component, props: Object.assign(Object.assign({}, component === null || component === void 0 ? void 0 : component.props), props.props) })));
50
+ return (ctx === null || ctx === void 0 ? void 0 : ctx.customRendererComponent) ? (_jsx(ctx.customRendererComponent, { Component: Component, children: children })) : (children);
51
+ };
52
+ if ((component === null || component === void 0 ? void 0 : component.Component) && renderType === 'view') {
53
+ return renderComponentChildren(component.Component);
54
+ }
55
+ if ((component === null || component === void 0 ? void 0 : component.EditComponent) && renderType === 'setting') {
56
+ return renderComponentChildren(component.EditComponent);
52
57
  }
53
58
  // if the component is not in the dev.components, it means the component is not loaded
54
59
  if ((dev === null || dev === void 0 ? void 0 : dev.mode) === 'draft' && dev && ((_b = dev.components) === null || _b === void 0 ? void 0 : _b[props.componentId]) === undefined) {
@@ -24,6 +24,7 @@ import { isEmpty } from 'lodash';
24
24
  import set from 'lodash/set';
25
25
  import { useDeferredValue, useEffect, useRef, useState } from 'react';
26
26
  import { Helmet } from 'react-helmet';
27
+ import { joinURL } from 'ufo';
27
28
  import { create } from 'zustand';
28
29
  import { immer } from 'zustand/middleware/immer';
29
30
  import { preloadComponents } from '../../api';
@@ -160,6 +161,7 @@ export function useComponent({ instanceId, componentId, properties, locale, dev
160
161
  const componentProperties = (_a = transpile === null || transpile === void 0 ? void 0 : transpile.componentProperties) !== null && _a !== void 0 ? _a : preload === null || preload === void 0 ? void 0 : preload.componentProperties;
161
162
  return {
162
163
  Component: (_b = transpile === null || transpile === void 0 ? void 0 : transpile.Component) !== null && _b !== void 0 ? _b : preload === null || preload === void 0 ? void 0 : preload.Component,
164
+ EditComponent: transpile === null || transpile === void 0 ? void 0 : transpile.EditComponent,
163
165
  props: Object.assign(Object.assign({}, ((_c = transpile === null || transpile === void 0 ? void 0 : transpile.props) !== null && _c !== void 0 ? _c : preload === null || preload === void 0 ? void 0 : preload.props)), Object.fromEntries(Object.entries(properties !== null && properties !== void 0 ? properties : {})
164
166
  .map(([key, { value }]) => {
165
167
  var _a;
@@ -167,7 +169,8 @@ export function useComponent({ instanceId, componentId, properties, locale, dev
167
169
  if (!property)
168
170
  return undefined;
169
171
  const v = parsePropertyValue(property, value, { locale, defaultLocale: dev === null || dev === void 0 ? void 0 : dev.defaultLocale });
170
- return [property.key, v];
172
+ // if key is undefined, use id
173
+ return [property.key || property.id, v];
171
174
  })
172
175
  .filter((i) => !!i))),
173
176
  error: transpile === null || transpile === void 0 ? void 0 : transpile.error,
@@ -202,7 +205,7 @@ function usePreloadComponent({ instanceId, componentId, properties, locale, dev
202
205
  }
203
206
  function useTranspileComponent({ componentId, locale, properties, dev: { defaultLocale, components } = {}, }) {
204
207
  var _a, _b;
205
- const [{ error, Component, propertiesFromCode }, setComponent] = useState({});
208
+ const [{ error, Component, EditComponent, propertiesFromCode }, setComponent] = useState({});
206
209
  const component = mergeComponent({
207
210
  componentId,
208
211
  getComponent: (componentId) => {
@@ -224,7 +227,7 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
224
227
  .then((m) => {
225
228
  // get properties from code
226
229
  const propertiesFromCode = getPropertiesFromCode(m);
227
- setComponent({ Component: m.default, propertiesFromCode });
230
+ setComponent({ Component: m.default, EditComponent: m.EditComponent || undefined, propertiesFromCode });
228
231
  })
229
232
  .catch((error) => {
230
233
  setComponent({ error });
@@ -234,12 +237,32 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
234
237
  return {
235
238
  error,
236
239
  Component,
240
+ EditComponent,
237
241
  props: component === null || component === void 0 ? void 0 : component.properties,
238
242
  componentProperties: (_b = (_a = components === null || components === void 0 ? void 0 : components[componentId]) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.properties,
239
243
  };
240
244
  }
241
245
  export function transpileAndLoadScript(script) {
242
246
  return __awaiter(this, void 0, void 0, function* () {
247
+ var _a, _b;
248
+ // @ts-ignore
249
+ if (import.meta.env.DEV && ((_a = window.__BLOCK_STUDIO__) === null || _a === void 0 ? void 0 : _a.getCodeImportPath)) {
250
+ try {
251
+ // @ts-ignore
252
+ const filePath = joinURL((_b = window.__BLOCK_STUDIO__) === null || _b === void 0 ? void 0 : _b.getCodeImportPath(script));
253
+ if (filePath) {
254
+ // eslint-disable-next-line no-console
255
+ console.info('get code import path from block studio:', filePath);
256
+ const m = yield import(/* @vite-ignore */ filePath).catch(() => null);
257
+ if (m) {
258
+ return m;
259
+ }
260
+ }
261
+ }
262
+ catch (e) {
263
+ // ignore error
264
+ }
265
+ }
243
266
  const [ts, { createBuiltinModuleTransformer }] = yield Promise.all([
244
267
  import('typescript').then((m) => m.default || m),
245
268
  import('../../utils/typescript/builtin-module-transformer'),
@@ -250,7 +273,13 @@ export function transpileAndLoadScript(script) {
250
273
  before: [createBuiltinModuleTransformer(ts)],
251
274
  },
252
275
  }).outputText;
276
+ // 降级到原有的处理方式
253
277
  const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
254
- return import(/* @vite-ignore */ url);
278
+ try {
279
+ return yield import(/* @vite-ignore */ url);
280
+ }
281
+ finally {
282
+ URL.revokeObjectURL(url);
283
+ }
255
284
  });
256
285
  }