@blocklet/editor 2.0.144 → 2.0.145

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.
@@ -17,7 +17,6 @@ export default function ExcalidrawComponent({ nodeKey, data }) {
17
17
  const captionButtonRef = useRef(null);
18
18
  const [isSelected, setSelected, clearSelection] = useLexicalNodeSelection(nodeKey);
19
19
  const [isResizing, setIsResizing] = useState(false);
20
- const [viewModeEnabled, setViewModeEnabled] = useState(false);
21
20
  const initialEditorEditable = useRef(editor.isEditable());
22
21
  // 兼容 excalidraw 旧版本数据 - 缺少 "elements" key, 并且是一个 array, 新版本数据是 object
23
22
  const parsed = useMemo(() => {
@@ -129,16 +128,14 @@ export default function ExcalidrawComponent({ nodeKey, data }) {
129
128
  }
130
129
  });
131
130
  };
132
- const _appState = viewModeEnabled ? { ...appState, viewModeEnabled: true } : appState;
133
131
  const handleClick = () => {
134
132
  // view mode 仅在 readonly 的 editor 中有效
135
133
  if (!initialEditorEditable.current) {
136
- setViewModeEnabled(true);
134
+ setModalOpen(true);
137
135
  }
138
136
  };
139
137
  const handleClose = () => {
140
138
  setModalOpen(false);
141
- setViewModeEnabled(false);
142
139
  };
143
140
  const containerStyles = {
144
141
  ...(containerWidth && { width: containerWidth }),
@@ -149,7 +146,8 @@ export default function ExcalidrawComponent({ nodeKey, data }) {
149
146
  if (!containerWidth && editor.getRootElement()) {
150
147
  containerStyles.width = '90%';
151
148
  }
152
- return (_jsxs(_Fragment, { children: [_jsx(ExcalidrawModal, { initialElements: elements, initialFiles: files, initialAppState: _appState, isShown: isModalOpen || viewModeEnabled, onDelete: deleteNode, onClose: handleClose, onSave: (els, aps, fls) => {
149
+ const _appState = !initialEditorEditable.current ? { ...appState, viewModeEnabled: true } : appState;
150
+ return (_jsxs(_Fragment, { children: [_jsx(ExcalidrawModal, { initialElements: elements, initialFiles: files, initialAppState: _appState, isShown: isModalOpen, readonly: !initialEditorEditable.current, onDelete: deleteNode, onClose: handleClose, onSave: (els, aps, fls) => {
153
151
  editor.setEditable(true);
154
152
  setData(els, aps, fls);
155
153
  setModalOpen(false);
@@ -37,11 +37,12 @@ type Props = {
37
37
  * Callback when the save button is clicked
38
38
  */
39
39
  onSave: (elements: ReadonlyArray<ExcalidrawElementFragment>, appState: Partial<AppState>, files: BinaryFiles) => void;
40
+ readonly?: boolean;
40
41
  };
41
42
  /**
42
43
  * @explorer-desc
43
44
  * A component which renders a modal with Excalidraw (a painting app)
44
45
  * which can be used to export an editable image
45
46
  */
46
- export default function ExcalidrawModal({ closeOnClickOutside, onSave, initialElements, initialAppState, initialFiles, initialLibraryItems, isShown, onDelete, onClose, }: Props): ReactPortal | null;
47
+ export default function ExcalidrawModal({ closeOnClickOutside, onSave, initialElements, initialAppState, initialFiles, initialLibraryItems, isShown, onDelete, onClose, readonly, }: Props): ReactPortal | null;
47
48
  export {};
@@ -12,7 +12,6 @@ import { createPortal } from 'react-dom';
12
12
  import LoadingButton from '@mui/lab/LoadingButton';
13
13
  import Button from '../../ui/Button';
14
14
  import Modal from '../../ui/Modal';
15
- import { useBeforeUnload } from '../../hooks/useBeforeUnload';
16
15
  import { uploadImageToMediaKit } from './utils';
17
16
  const safeParseJSON = (json) => {
18
17
  try {
@@ -28,7 +27,7 @@ const LIBRARY_KEY = 'excalidraw-library';
28
27
  * A component which renders a modal with Excalidraw (a painting app)
29
28
  * which can be used to export an editable image
30
29
  */
31
- export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, initialElements, initialAppState, initialFiles, initialLibraryItems = safeParseJSON(localStorage.getItem(LIBRARY_KEY))?.libraryItems, isShown = false, onDelete, onClose, }) {
30
+ export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, initialElements, initialAppState, initialFiles, initialLibraryItems = safeParseJSON(localStorage.getItem(LIBRARY_KEY))?.libraryItems, isShown = false, onDelete, onClose, readonly, }) {
32
31
  const excaliDrawModelRef = useRef(null);
33
32
  const excaliDrawSceneRef = useRef(null);
34
33
  const [discardModalOpen, setDiscardModalOpen] = useState(false);
@@ -38,7 +37,9 @@ export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, i
38
37
  const [saveText, setSaveText] = useState('Save');
39
38
  const [fullScreen, setFullScreen] = useState(false);
40
39
  const markBlockletDirtyState = () => {
41
- window.dispatchEvent(new CustomEvent('blocklet:markDirty', { detail: { key: 'excalidraw' } }));
40
+ if (!readonly) {
41
+ window.dispatchEvent(new CustomEvent('blocklet:markDirty', { detail: { key: 'excalidraw' } }));
42
+ }
42
43
  };
43
44
  const resetBlockletDirtyState = () => {
44
45
  window.dispatchEvent(new CustomEvent('blocklet:resetDirty', { detail: { key: 'excalidraw' } }));
@@ -56,7 +57,6 @@ export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, i
56
57
  return initialLibraryItems;
57
58
  },
58
59
  });
59
- useBeforeUnload(isShown);
60
60
  useEffect(() => {
61
61
  let modalOverlayElement = null;
62
62
  const clickOutsideHandler = (event) => {
@@ -112,7 +112,6 @@ export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, i
112
112
  name: appState.name,
113
113
  theme: appState.theme,
114
114
  viewBackgroundColor: appState.viewBackgroundColor,
115
- viewModeEnabled: appState.viewModeEnabled,
116
115
  zenModeEnabled: appState.zenModeEnabled,
117
116
  zoom: appState.zoom,
118
117
  }
@@ -154,7 +153,12 @@ export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, i
154
153
  }, closeOnClickOutside: false, children: ["Are you sure you want to discard the changes?", _jsxs("div", { className: "ExcalidrawModal__discardModal", children: [_jsx(Button, { onClick: () => {
155
154
  resetBlockletDirtyState();
156
155
  setDiscardModalOpen(false);
157
- onClose();
156
+ if (!initialElements?.length) {
157
+ onDelete();
158
+ }
159
+ else {
160
+ onClose();
161
+ }
158
162
  }, children: "Discard" }), ' ', _jsx(Button, { onClick: () => {
159
163
  setDiscardModalOpen(false);
160
164
  }, children: "Cancel" })] })] }));
@@ -180,5 +184,5 @@ export default function ExcalidrawModal({ closeOnClickOutside = false, onSave, i
180
184
  appState: initialAppState || { isLoading: false },
181
185
  elements: initialElements,
182
186
  files: initialFiles,
183
- } }), _jsxs("div", { className: "ExcalidrawModal__actions", children: [!fullScreen && (_jsx(LoadingButton, { className: "action-button", style: { width: 32, padding: 0, display: 'flex', alignItems: 'center' }, onClick: () => setFullScreen(true), children: _jsx("i", { className: "iconify", "data-icon": "material-symbols:fullscreen", "data-height": "18" }) })), fullScreen && (_jsx(LoadingButton, { className: "action-button", style: { width: 32, padding: 0, display: 'flex', alignItems: 'center' }, onClick: () => setFullScreen(false), children: _jsx("i", { className: "iconify", "data-icon": "material-symbols:fullscreen-exit", "data-height": "18" }) })), !initialAppState.viewModeEnabled && (_jsxs(_Fragment, { children: [_jsx(LoadingButton, { loadingPosition: "start", disabled: loading, className: "action-button", onClick: discard, children: "Discard" }), _jsx(LoadingButton, { loadingPosition: "start", loading: loading, className: "action-button", onClick: save, children: saveText })] })), initialAppState.viewModeEnabled && (_jsx(LoadingButton, { className: "action-button", onClick: () => onClose(), children: "Close" }))] })] }) }) }), document.body);
187
+ } }), _jsxs("div", { className: "ExcalidrawModal__actions", children: [!fullScreen && (_jsx(LoadingButton, { className: "action-button", style: { width: 32, padding: 0, display: 'flex', alignItems: 'center' }, onClick: () => setFullScreen(true), children: _jsx("i", { className: "iconify", "data-icon": "material-symbols:fullscreen", "data-height": "18" }) })), fullScreen && (_jsx(LoadingButton, { className: "action-button", style: { width: 32, padding: 0, display: 'flex', alignItems: 'center' }, onClick: () => setFullScreen(false), children: _jsx("i", { className: "iconify", "data-icon": "material-symbols:fullscreen-exit", "data-height": "18" }) })), !readonly && (_jsxs(_Fragment, { children: [_jsx(LoadingButton, { loadingPosition: "start", disabled: loading, className: "action-button", onClick: discard, children: "Discard" }), _jsx(LoadingButton, { loadingPosition: "start", loading: loading, className: "action-button", onClick: save, children: saveText })] })), readonly && (_jsx(LoadingButton, { className: "action-button", onClick: () => onClose(), children: "Close" }))] })] }) }) }), document.body);
184
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/editor",
3
- "version": "2.0.144",
3
+ "version": "2.0.145",
4
4
  "main": "lib/index.js",
5
5
  "scripts": {
6
6
  "dev": "npm run storybook",
@@ -40,7 +40,7 @@
40
40
  "@arcblock/ux": "^2.10.23",
41
41
  "@blocklet/embed": "^0.2.0",
42
42
  "@blocklet/pages-kit": "^0.2.399",
43
- "@blocklet/pdf": "2.0.144",
43
+ "@blocklet/pdf": "2.0.145",
44
44
  "@excalidraw/excalidraw": "^0.14.2",
45
45
  "@iconify/iconify": "^3.0.1",
46
46
  "@iconify/icons-tabler": "^1.2.95",
@@ -113,5 +113,5 @@
113
113
  "react": "*",
114
114
  "react-dom": "*"
115
115
  },
116
- "gitHead": "1f707b0d6ccaddfc2c7d57979af1514c77ec2d7a"
116
+ "gitHead": "eeac7ba668f239de905f6b1cf8c132f910d15e45"
117
117
  }