@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.
@@ -18,8 +18,10 @@ const ufo_1 = require("ufo");
18
18
  const PAGES_KIT_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
19
19
  function preloadComponents(input) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
- var _a, _b;
22
- 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) || '/';
21
+ var _a, _b, _c;
22
+ 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) ||
23
+ ((_c = window === null || window === void 0 ? void 0 : window.blocklet) === null || _c === void 0 ? void 0 : _c.prefix) ||
24
+ '/';
23
25
  return axios_1.default.post((0, ufo_1.joinURL)(prefix, '/api/components/preload'), input).then((res) => res.data);
24
26
  });
25
27
  }
@@ -45,7 +45,7 @@ function CustomComponentRenderer(_a) {
45
45
  var { dev } = _a, props = __rest(_a, ["dev"]);
46
46
  const inheritedDev = (0, DevProvider_1.useDev)();
47
47
  const BuiltinComponent = BuiltinComponents[props.componentId];
48
- return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { FallbackComponent: (props === null || props === void 0 ? void 0 : props.fallbackRender) || ErrorView, resetKeys: [Date.now()], children: (0, jsx_runtime_1.jsx)(DevProvider_1.DevProvider, { dev: dev !== null && dev !== void 0 ? dev : inheritedDev, children: BuiltinComponent ? ((0, jsx_runtime_1.jsx)(BuiltinComponent, Object.assign({}, props))) : ((0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({}, props, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId }))) }) }));
48
+ return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { FallbackComponent: (props === null || props === void 0 ? void 0 : props.fallbackRender) || ErrorView, resetKeys: [Date.now()], children: (0, jsx_runtime_1.jsx)(DevProvider_1.DevProvider, { dev: dev !== null && dev !== void 0 ? dev : inheritedDev, children: BuiltinComponent ? ((0, jsx_runtime_1.jsx)(BuiltinComponent, Object.assign({}, props))) : ((0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({}, props, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId, renderType: props.renderType }))) }) }));
49
49
  }
50
50
  const BuiltinComponents = {
51
51
  'blocklet-react-component': BlockletReactComponentRenderer_1.default,
@@ -55,20 +55,25 @@ function ErrorView({ error }) {
55
55
  }
56
56
  function ComponentRenderer(_a) {
57
57
  var _b;
58
- var { renderCount = 0, blockletId, blockletTitle, componentName } = _a, props = __rest(_a, ["renderCount", "blockletId", "blockletTitle", "componentName"]);
58
+ var { renderCount = 0, blockletId, blockletTitle, componentName, renderType = 'view' } = _a, props = __rest(_a, ["renderCount", "blockletId", "blockletTitle", "componentName", "renderType"]);
59
59
  const dev = (0, DevProvider_1.useDev)();
60
60
  if (renderCount > MAXIMUM_RENDER_STACK_SIZE) {
61
61
  throw new Error('Maximum render stack size exceeded');
62
62
  }
63
63
  const component = (0, state_1.useComponent)(Object.assign(Object.assign({}, props), { dev }));
64
+ const ctx = (0, context_1.useCustomComponentRenderer)();
64
65
  if (component === null || component === void 0 ? void 0 : component.error) {
65
- // throw to outer error boundary
66
66
  throw component.error;
67
67
  }
68
- const ctx = (0, context_1.useCustomComponentRenderer)();
69
- if (component === null || component === void 0 ? void 0 : component.Component) {
70
- const children = ((0, jsx_runtime_1.jsx)(Renderer, Object.assign({ renderCount: renderCount + 1 }, props, { Component: component.Component, props: Object.assign(Object.assign({}, component.props), props.props) })));
71
- return (ctx === null || ctx === void 0 ? void 0 : ctx.customRendererComponent) ? ((0, jsx_runtime_1.jsx)(ctx.customRendererComponent, { Component: component.Component, children: children })) : (children);
68
+ const renderComponentChildren = (Component) => {
69
+ const children = ((0, jsx_runtime_1.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) })));
70
+ return (ctx === null || ctx === void 0 ? void 0 : ctx.customRendererComponent) ? ((0, jsx_runtime_1.jsx)(ctx.customRendererComponent, { Component: Component, children: children })) : (children);
71
+ };
72
+ if ((component === null || component === void 0 ? void 0 : component.Component) && renderType === 'view') {
73
+ return renderComponentChildren(component.Component);
74
+ }
75
+ if ((component === null || component === void 0 ? void 0 : component.EditComponent) && renderType === 'setting') {
76
+ return renderComponentChildren(component.EditComponent);
72
77
  }
73
78
  // if the component is not in the dev.components, it means the component is not loaded
74
79
  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) {
@@ -56,6 +56,7 @@ const lodash_1 = require("lodash");
56
56
  const set_1 = __importDefault(require("lodash/set"));
57
57
  const react_1 = require("react");
58
58
  const react_helmet_1 = require("react-helmet");
59
+ const ufo_1 = require("ufo");
59
60
  const zustand_1 = require("zustand");
60
61
  const immer_1 = require("zustand/middleware/immer");
61
62
  const api_1 = require("../../api");
@@ -193,6 +194,7 @@ function useComponent({ instanceId, componentId, properties, locale, dev }) {
193
194
  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;
194
195
  return {
195
196
  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,
197
+ EditComponent: transpile === null || transpile === void 0 ? void 0 : transpile.EditComponent,
196
198
  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 : {})
197
199
  .map(([key, { value }]) => {
198
200
  var _a;
@@ -200,7 +202,8 @@ function useComponent({ instanceId, componentId, properties, locale, dev }) {
200
202
  if (!property)
201
203
  return undefined;
202
204
  const v = (0, property_1.parsePropertyValue)(property, value, { locale, defaultLocale: dev === null || dev === void 0 ? void 0 : dev.defaultLocale });
203
- return [property.key, v];
205
+ // if key is undefined, use id
206
+ return [property.key || property.id, v];
204
207
  })
205
208
  .filter((i) => !!i))),
206
209
  error: transpile === null || transpile === void 0 ? void 0 : transpile.error,
@@ -235,7 +238,7 @@ function usePreloadComponent({ instanceId, componentId, properties, locale, dev
235
238
  }
236
239
  function useTranspileComponent({ componentId, locale, properties, dev: { defaultLocale, components } = {}, }) {
237
240
  var _a, _b;
238
- const [{ error, Component, propertiesFromCode }, setComponent] = (0, react_1.useState)({});
241
+ const [{ error, Component, EditComponent, propertiesFromCode }, setComponent] = (0, react_1.useState)({});
239
242
  const component = (0, property_1.mergeComponent)({
240
243
  componentId,
241
244
  getComponent: (componentId) => {
@@ -257,7 +260,7 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
257
260
  .then((m) => {
258
261
  // get properties from code
259
262
  const propertiesFromCode = getPropertiesFromCode(m);
260
- setComponent({ Component: m.default, propertiesFromCode });
263
+ setComponent({ Component: m.default, EditComponent: m.EditComponent || undefined, propertiesFromCode });
261
264
  })
262
265
  .catch((error) => {
263
266
  setComponent({ error });
@@ -267,12 +270,32 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
267
270
  return {
268
271
  error,
269
272
  Component,
273
+ EditComponent,
270
274
  props: component === null || component === void 0 ? void 0 : component.properties,
271
275
  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,
272
276
  };
273
277
  }
274
278
  function transpileAndLoadScript(script) {
275
279
  return __awaiter(this, void 0, void 0, function* () {
280
+ var _a, _b;
281
+ // @ts-ignore
282
+ if (import.meta.env.DEV && ((_a = window.__BLOCK_STUDIO__) === null || _a === void 0 ? void 0 : _a.getCodeImportPath)) {
283
+ try {
284
+ // @ts-ignore
285
+ const filePath = (0, ufo_1.joinURL)((_b = window.__BLOCK_STUDIO__) === null || _b === void 0 ? void 0 : _b.getCodeImportPath(script));
286
+ if (filePath) {
287
+ // eslint-disable-next-line no-console
288
+ console.info('get code import path from block studio:', filePath);
289
+ const m = yield Promise.resolve(`${filePath}`).then(s => __importStar(require(s))).catch(() => null);
290
+ if (m) {
291
+ return m;
292
+ }
293
+ }
294
+ }
295
+ catch (e) {
296
+ // ignore error
297
+ }
298
+ }
276
299
  const [ts, { createBuiltinModuleTransformer }] = yield Promise.all([
277
300
  Promise.resolve().then(() => __importStar(require('typescript'))).then((m) => m.default || m),
278
301
  Promise.resolve().then(() => __importStar(require('../../utils/typescript/builtin-module-transformer'))),
@@ -283,7 +306,13 @@ function transpileAndLoadScript(script) {
283
306
  before: [createBuiltinModuleTransformer(ts)],
284
307
  },
285
308
  }).outputText;
309
+ // 降级到原有的处理方式
286
310
  const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
287
- return Promise.resolve(`${url}`).then(s => __importStar(require(s)));
311
+ try {
312
+ return yield Promise.resolve(`${url}`).then(s => __importStar(require(s)));
313
+ }
314
+ finally {
315
+ URL.revokeObjectURL(url);
316
+ }
288
317
  });
289
318
  }