@hwp-editor/react 1.1.0 → 1.2.0

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.
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  forwardRef,
4
4
  useCallback,
5
- useEffect as useEffect3,
5
+ useEffect as useEffect4,
6
6
  useImperativeHandle,
7
7
  useMemo as useMemo3,
8
8
  useRef,
@@ -832,7 +832,7 @@ function FieldsPanel() {
832
832
  }
833
833
 
834
834
  // src/ComposePanel.tsx
835
- import { useState as useState4 } from "react";
835
+ import { useEffect as useEffect3, useState as useState4 } from "react";
836
836
 
837
837
  // src/errors.ts
838
838
  import { isHwpEngineError, isHwpErrorCode } from "@hwp-editor/core";
@@ -1018,6 +1018,17 @@ function ComposePanel(props) {
1018
1018
  const [body, setBody] = useState4("");
1019
1019
  const [busy, setBusy] = useState4(false);
1020
1020
  const [error, setError] = useState4(null);
1021
+ const { onClose } = props;
1022
+ useEffect3(() => {
1023
+ const onKeyDown = (event) => {
1024
+ if (event.key !== "Escape" || event.isComposing || event.defaultPrevented) return;
1025
+ if (busy) return;
1026
+ event.preventDefault();
1027
+ onClose();
1028
+ };
1029
+ document.addEventListener("keydown", onKeyDown);
1030
+ return () => document.removeEventListener("keydown", onKeyDown);
1031
+ }, [busy, onClose]);
1021
1032
  const compose = async () => {
1022
1033
  setBusy(true);
1023
1034
  setError(null);
@@ -1367,17 +1378,17 @@ var HwpEditor = forwardRef(
1367
1378
  style
1368
1379
  } = props;
1369
1380
  const cbRef = useRef({ onReady, onError, onChange, onDirtyChange });
1370
- useEffect3(() => {
1381
+ useEffect4(() => {
1371
1382
  cbRef.current = { onReady, onError, onChange, onDirtyChange };
1372
1383
  });
1373
- const emitReady = useCallback((document) => {
1374
- cbRef.current.onReady?.(document);
1384
+ const emitReady = useCallback((document2) => {
1385
+ cbRef.current.onReady?.(document2);
1375
1386
  }, []);
1376
1387
  const emitError = useCallback((error) => {
1377
1388
  cbRef.current.onError?.(error);
1378
1389
  }, []);
1379
- const emitChange = useCallback((document) => {
1380
- cbRef.current.onChange?.(document);
1390
+ const emitChange = useCallback((document2) => {
1391
+ cbRef.current.onChange?.(document2);
1381
1392
  }, []);
1382
1393
  const emitDirty = useCallback((dirty2) => {
1383
1394
  cbRef.current.onDirtyChange?.(dirty2);
@@ -1391,7 +1402,7 @@ var HwpEditor = forwardRef(
1391
1402
  const [loadError, setLoadError] = useState5(null);
1392
1403
  const [tab, setTab] = useState5("para");
1393
1404
  const [composing, setComposing] = useState5(false);
1394
- useEffect3(() => {
1405
+ useEffect4(() => {
1395
1406
  let cancelled = false;
1396
1407
  if (file === null) {
1397
1408
  setEnvelope(null);
@@ -1425,7 +1436,7 @@ var HwpEditor = forwardRef(
1425
1436
  };
1426
1437
  }, [engine, file, store]);
1427
1438
  const editable = !readOnly && (capabilities?.editable ?? true);
1428
- useEffect3(() => {
1439
+ useEffect4(() => {
1429
1440
  if (envelope === null || state.selection === null) return;
1430
1441
  const segment = segmentAtRef4(envelope, state.selection);
1431
1442
  if (segment === void 0) return;
@@ -1434,7 +1445,7 @@ var HwpEditor = forwardRef(
1434
1445
  );
1435
1446
  }, [envelope, state.selection]);
1436
1447
  const dirty = state.pendingOps.length > 0;
1437
- useEffect3(() => {
1448
+ useEffect4(() => {
1438
1449
  emitDirty(dirty);
1439
1450
  }, [dirty]);
1440
1451
  const applyPendingOps = useCallback(() => {
@@ -1545,6 +1556,7 @@ var HwpEditor = forwardRef(
1545
1556
  );
1546
1557
  const onKeyDown = (e) => {
1547
1558
  if (e.key === "Escape") {
1559
+ if (composing) return;
1548
1560
  store.dispatch({ type: "select", selection: null });
1549
1561
  } else if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
1550
1562
  e.preventDefault();
@@ -1684,17 +1696,17 @@ var HwpEditor = forwardRef(
1684
1696
  ComposePanel,
1685
1697
  {
1686
1698
  onClose: () => setComposing(false),
1687
- onComposed: (document) => {
1699
+ onComposed: (document2) => {
1688
1700
  setComposing(false);
1689
- emitChange(document);
1701
+ emitChange(document2);
1690
1702
  void (async () => {
1691
1703
  try {
1692
1704
  const [nextEnvelope, pages] = await Promise.all([
1693
- engine.read(document),
1694
- engine.render(document, RENDER_SVG)
1705
+ engine.read(document2),
1706
+ engine.render(document2, RENDER_SVG)
1695
1707
  ]);
1696
1708
  setEnvelope(nextEnvelope);
1697
- store.dispatch({ type: "load", document, pages });
1709
+ store.dispatch({ type: "load", document: document2, pages });
1698
1710
  } catch (e) {
1699
1711
  setLoadError(toEditorError(e));
1700
1712
  emitError(e);