@blocklet/editor 2.1.82 → 2.1.84

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.
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
3
3
  import { Box, Button, Divider, IconButton, Popover, useMediaQuery } from '@mui/material';
4
- import { useEffect, useState } from 'react';
4
+ import { useEffect, useMemo, useState } from 'react';
5
5
  import SettingsOutlinedIcon from '@mui/icons-material/SettingsOutlined';
6
6
  import { CustomComponentRenderer, customComponentStates } from '@blocklet/pages-kit/components';
7
7
  import { isBlockletRunning } from '../utils';
@@ -28,20 +28,38 @@ export function PagesKitComponentRenderer({ id, name, properties, onPropertiesCh
28
28
  onPropertiesChange(pendingProperties);
29
29
  setAnchorEl(null);
30
30
  };
31
+ const instanceId = useMemo(() => `${id}-${hashCode(JSON.stringify(pendingProperties))}`, [id, pendingProperties]);
31
32
  useEffect(() => {
32
33
  const load = async () => {
33
34
  const state = customComponentStates().getState();
34
35
  await state.loadComponents({
35
36
  mode,
36
37
  locale,
37
- instances: [{ id, component: { name } }],
38
+ instances: [
39
+ {
40
+ id: instanceId,
41
+ component: { id },
42
+ // TODO wait pages kit package update, remove this
43
+ // @ts-ignore
44
+ useCache: true,
45
+ // @ts-ignore
46
+ cacheDuration: 3600 * 2,
47
+ properties: Object.entries(pendingProperties?.[locale] || {}).reduce((props, [key, value]) => {
48
+ props[key] = { value };
49
+ if (typeof value === 'object') {
50
+ props[key].value = JSON.stringify(value);
51
+ }
52
+ return props;
53
+ }, {}),
54
+ },
55
+ ],
38
56
  });
39
57
  setLoading(false);
40
58
  };
41
59
  if (isBlockletRunning('pages-kit')) {
42
60
  load();
43
61
  }
44
- }, [id, name]);
62
+ }, [id, name, pendingProperties]);
45
63
  const component = customComponentStates().getState()?.state?.components?.[id]?.component;
46
64
  if (loading || !component) {
47
65
  return null;
@@ -50,7 +68,7 @@ export function PagesKitComponentRenderer({ id, name, properties, onPropertiesCh
50
68
  const propDefinitions = Object.values(component.properties || {})
51
69
  .map((x) => x.data)
52
70
  .filter((x) => !!x.key && isValidPropertyType(x.type));
53
- return (_jsxs(Box, { sx: { position: 'relative', ...(isEditable && { mt: 2 }) }, children: [_jsx(CustomComponentRenderer, { componentId: id, locale: locale, dev: { mode }, props: properties?.[locale] }), isEditable && !!propDefinitions.length && upMd && (_jsxs(_Fragment, { children: [_jsx(IconButton, { sx: { position: 'absolute', right: 0, top: -32 }, onClick: handleClick, children: _jsx(SettingsOutlinedIcon, {}) }), _jsx(Popover, { open: open, anchorEl: anchorEl, onClose: () => setAnchorEl(null), anchorOrigin: {
71
+ return (_jsxs(Box, { sx: { position: 'relative', ...(isEditable && { mt: 2 }) }, children: [_jsx(CustomComponentRenderer, { instanceId: instanceId, componentId: id, locale: locale, dev: { mode } }), isEditable && !!propDefinitions.length && upMd && (_jsxs(_Fragment, { children: [_jsx(IconButton, { sx: { position: 'absolute', right: 0, top: -32 }, onClick: handleClick, children: _jsx(SettingsOutlinedIcon, {}) }), _jsx(Popover, { open: open, anchorEl: anchorEl, onClose: () => setAnchorEl(null), anchorOrigin: {
54
72
  vertical: 'bottom',
55
73
  horizontal: 'right',
56
74
  }, transformOrigin: {
@@ -66,10 +84,19 @@ export function PagesKitComponentRenderer({ id, name, properties, onPropertiesCh
66
84
  }, children: [_jsx(Box, { component: "span", className: "iconify", "data-icon": "tabler:settings-2", sx: { fontSize: 20 } }), _jsx("span", { children: "Properties" })] }) }), _jsx(Divider, { sx: { mt: 0.75, mb: 2 } }), propDefinitions.map((x) => {
67
85
  // @ts-ignore
68
86
  const multiline = !!x.multiline;
69
- return (_jsx(Box, { sx: { mt: 2 }, children: _jsx(PropertyField, { label: getPropertyLabel({ locale, locales: x.locales }), type: x.type, value: pendingProperties[locale]?.[x.key] ??
87
+ return (_jsx(Box, { sx: { mt: 2 }, children: _jsx(PropertyField, { label: getPropertyLabel({ locale, locales: x.locales }), type: x.type, value: pendingProperties[locale]?.[x.id] ??
70
88
  getLocalizedValue({ key: 'defaultValue', locale, data: x.locales }), onChange: (v) => setPendingProperties({
71
89
  ...pendingProperties,
72
- [locale]: { ...pendingProperties[locale], [x.key]: v },
73
- }), multiline: multiline }) }, x.key));
90
+ [locale]: { ...pendingProperties[locale], [x.id]: v },
91
+ }), multiline: multiline }) }, x.id));
74
92
  }), _jsxs(Box, { sx: { display: 'flex', justifyContent: 'flex-end', gap: 1, py: 2 }, children: [_jsx(Button, { color: "inherit", onClick: handleCancel, children: "Cancel" }), _jsx(Button, { color: "primary", variant: "contained", onClick: handleSave, children: "Save" })] })] }) })] }))] }));
75
93
  }
94
+ function hashCode(str) {
95
+ let hash = 0;
96
+ for (let i = 0; i < str.length; i++) {
97
+ const char = str.charCodeAt(i);
98
+ hash = (hash << 5) - hash + char;
99
+ hash &= hash; // Convert to 32-bit integer
100
+ }
101
+ return Math.abs(hash).toString(36); // 转换为 36 进制,使 id 更短
102
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/editor",
3
- "version": "2.1.82",
3
+ "version": "2.1.84",
4
4
  "main": "lib/index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -65,7 +65,7 @@
65
65
  "ufo": "^1.5.4",
66
66
  "url-join": "^4.0.1",
67
67
  "zustand": "^4.5.5",
68
- "@blocklet/pdf": "^2.1.82"
68
+ "@blocklet/pdf": "^2.1.84"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@babel/core": "^7.25.2",