@lingjingai/script-editor 0.1.13 → 0.1.15

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
@@ -2015,6 +2015,7 @@ function getActorCueVar(actorId, tone = "dialogue") {
2015
2015
  return `var(--lj-se-actor-cue-${idx})`;
2016
2016
  }
2017
2017
  var EditingSignalContext = createContext(null);
2018
+ var LIVE_COMMIT_MS = 400;
2018
2019
  function caretOffsetFromPoint(x, y) {
2019
2020
  const d = document;
2020
2021
  if (typeof d.caretRangeFromPoint === "function") {
@@ -2044,6 +2045,34 @@ function EditableText({
2044
2045
  const inputRef = useRef(null);
2045
2046
  const committedRef = useRef(value);
2046
2047
  const editingSignal = useContext(EditingSignalContext);
2048
+ const liveTimerRef = useRef(null);
2049
+ const composingRef = useRef(false);
2050
+ const draftRef = useRef(value);
2051
+ const onSaveRef = useRef(onSave);
2052
+ onSaveRef.current = onSave;
2053
+ const clearLiveTimer = () => {
2054
+ if (liveTimerRef.current) {
2055
+ clearTimeout(liveTimerRef.current);
2056
+ liveTimerRef.current = null;
2057
+ }
2058
+ };
2059
+ const flushLive = () => {
2060
+ clearLiveTimer();
2061
+ const next = draftRef.current;
2062
+ if (next !== committedRef.current) {
2063
+ committedRef.current = next;
2064
+ onSaveRef.current(next);
2065
+ }
2066
+ };
2067
+ const scheduleLive = () => {
2068
+ clearLiveTimer();
2069
+ liveTimerRef.current = setTimeout(() => {
2070
+ liveTimerRef.current = null;
2071
+ if (composingRef.current) return;
2072
+ flushLive();
2073
+ }, LIVE_COMMIT_MS);
2074
+ };
2075
+ useEffect(() => () => flushLive(), []);
2047
2076
  useEffect(() => {
2048
2077
  if (!editing || !editingSignal) return;
2049
2078
  editingSignal.inc();
@@ -2079,9 +2108,11 @@ function EditableText({
2079
2108
  if (sel && !sel.isCollapsed) return;
2080
2109
  caretRef.current = caretOffsetFromPoint(e.clientX, e.clientY);
2081
2110
  setDraft(committedRef.current);
2111
+ draftRef.current = committedRef.current;
2082
2112
  setEditing(true);
2083
2113
  }
2084
2114
  function commit() {
2115
+ clearLiveTimer();
2085
2116
  setEditing(false);
2086
2117
  onEditEnd?.();
2087
2118
  if (draft !== committedRef.current) {
@@ -2090,7 +2121,9 @@ function EditableText({
2090
2121
  }
2091
2122
  }
2092
2123
  function cancel() {
2124
+ clearLiveTimer();
2093
2125
  setDraft(committedRef.current);
2126
+ draftRef.current = committedRef.current;
2094
2127
  setEditing(false);
2095
2128
  onEditEnd?.();
2096
2129
  }
@@ -2110,8 +2143,20 @@ function EditableText({
2110
2143
  value: draft,
2111
2144
  className: "lj-se-editing" + (className ? " " + className : ""),
2112
2145
  onChange: (e) => {
2113
- setDraft(e.target.value);
2146
+ const v = e.target.value;
2147
+ setDraft(v);
2148
+ draftRef.current = v;
2114
2149
  if (multiline) autoSize(e.target);
2150
+ if (!composingRef.current) scheduleLive();
2151
+ },
2152
+ onCompositionStart: () => {
2153
+ composingRef.current = true;
2154
+ clearLiveTimer();
2155
+ },
2156
+ onCompositionEnd: (e) => {
2157
+ composingRef.current = false;
2158
+ draftRef.current = e.currentTarget.value;
2159
+ scheduleLive();
2115
2160
  },
2116
2161
  onBlur: commit,
2117
2162
  onKeyDown: (e) => {