@blocklet/pages-kit 0.5.17 → 0.5.19

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.
@@ -75,4 +75,5 @@ exports.BuiltinModules = {
75
75
  '@blocklet/pages-kit/builtin/uploader': {},
76
76
  '@blocklet/pages-kit/builtin/color-picker': {},
77
77
  '@blocklet/pages-kit/builtin/markdown/index': {},
78
+ '@blocklet/pages-kit/builtin/event-bus': {},
78
79
  };
@@ -39,6 +39,7 @@ const colorPicker = __importStar(require("../builtin/color-picker"));
39
39
  const components = __importStar(require("../builtin/components"));
40
40
  const dayjs = __importStar(require("../builtin/dayjs"));
41
41
  const emotionCss = __importStar(require("../builtin/emotion/css"));
42
+ const eventBus = __importStar(require("../builtin/event-bus"));
42
43
  const iconifyReact = __importStar(require("../builtin/iconify/react"));
43
44
  const immer = __importStar(require("../builtin/immer"));
44
45
  const locale = __importStar(require("../builtin/locale"));
@@ -108,6 +109,7 @@ function injectGlobalComponents() {
108
109
  '@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
109
110
  '@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
110
111
  '@blocklet/pages-kit/builtin/async/ai-runtime': Promise.resolve().then(() => __importStar(require('../builtin/async/ai-runtime'))),
112
+ '@blocklet/pages-kit/builtin/event-bus': eventBus,
111
113
  };
112
114
  // set global variable
113
115
  win[builtin_1.BuiltinModulesGlobalVariableName] = {
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.initDynamicParsePropertyValueHandlers = exports.RenderNestedComponent = void 0;
29
+ exports.initDynamicParsePropertyValueHandlers = exports.RenderNestedComponent = exports.sanitize = void 0;
30
30
  exports.componentUMDName = componentUMDName;
31
31
  exports.mergeComponent = mergeComponent;
32
32
  exports.safeJSONParse = safeJSONParse;
@@ -34,9 +34,11 @@ exports.safeYamlParse = safeYamlParse;
34
34
  exports.parsePropertyValue = parsePropertyValue;
35
35
  exports.assignNullableFields = assignNullableFields;
36
36
  exports.getComponentDependencies = getComponentDependencies;
37
+ const xss_1 = require("@blocklet/xss");
37
38
  const snakeCase_1 = __importDefault(require("lodash/snakeCase"));
38
39
  const yaml = __importStar(require("yaml"));
39
40
  const common_1 = require("./common");
41
+ exports.sanitize = (0, xss_1.initSanitize)();
40
42
  function componentUMDName({ componentId }) {
41
43
  return `PagesCustomComponent${(0, snakeCase_1.default)(componentId)}`;
42
44
  }
@@ -129,12 +131,13 @@ const initDynamicParsePropertyValueHandlers = () => {
129
131
  };
130
132
  exports.initDynamicParsePropertyValueHandlers = initDynamicParsePropertyValueHandlers;
131
133
  (0, exports.initDynamicParsePropertyValueHandlers)();
132
- function parsePropertyValue(property, value, { locale, defaultLocale, propertyHandlers, }) {
134
+ function parsePropertyValue(property, inputValue, { locale, defaultLocale, propertyHandlers, }) {
133
135
  // 混合动态和自定义的属性处理函数,这个需要放在最前面,因为有可能存在覆盖下面的处理
134
136
  const mixedPropertyHandlers = {
135
137
  ...(dynamicPropertyHandlers ?? {}),
136
138
  ...(propertyHandlers ?? {}),
137
139
  };
140
+ const value = inputValue;
138
141
  if (mixedPropertyHandlers && property.type && typeof mixedPropertyHandlers[property.type] === 'function') {
139
142
  try {
140
143
  const handler = mixedPropertyHandlers[property.type];
@@ -0,0 +1,35 @@
1
+ import mitt from 'mitt';
2
+ import { useEffect, useCallback } from 'react';
3
+ export const eventBus = mitt();
4
+ export const useEventBus = (eventName, eventCallBack, deps = []) => {
5
+ // 使用 useCallback 包装 emit 函数避免重渲染
6
+ const emit = useCallback((name, data) => {
7
+ eventBus.emit(name, data);
8
+ }, []);
9
+ useEffect(() => {
10
+ if (eventName && eventCallBack) {
11
+ // 包装回调以添加错误处理
12
+ const wrappedCallback = (data) => {
13
+ try {
14
+ eventCallBack(data);
15
+ }
16
+ catch (error) {
17
+ console.error(`Error in event handler for "${eventName}":`, error);
18
+ }
19
+ };
20
+ eventBus.on(eventName, wrappedCallback);
21
+ return () => {
22
+ eventBus.off(eventName, wrappedCallback);
23
+ };
24
+ }
25
+ return () => { };
26
+ }, [eventName, eventCallBack, ...deps]);
27
+ return {
28
+ emit,
29
+ // 提供更多控制功能
30
+ off: eventBus.off,
31
+ on: eventBus.on,
32
+ all: eventBus.all,
33
+ };
34
+ };
35
+ export default useEventBus;