{"version":3,"sources":["../src/components/base-ai-textarea/track-cursor-moved-since-last-text-change.tsx"],"sourcesContent":["import { useEffect, useRef } from \"react\";\nimport { BaseSelection } from \"slate\";\nimport { useSlateSelector } from \"slate-react\";\nimport { Range } from \"slate\";\nimport { editorToText } from \"../../lib/editor-to-text\";\n\ninterface TrackerTextEditedSinceLastCursorMovementProps {\n  setCursorMovedSinceLastTextChange: (value: boolean) => void;\n}\nexport function TrackerTextEditedSinceLastCursorMovement(\n  props: TrackerTextEditedSinceLastCursorMovementProps,\n) {\n  const cursorState: RelevantEditorState = useSlateSelector((state) => ({\n    selection: state.selection,\n    text: editorToText(state),\n  }));\n\n  const previousState = usePrevious(cursorState);\n\n  useEffect(() => {\n    if (!previousState) {\n      return;\n    }\n\n    if (cursorChangedWithoutTextChanged(previousState, cursorState)) {\n      props.setCursorMovedSinceLastTextChange(true);\n    }\n  }, [props.setCursorMovedSinceLastTextChange, cursorState]);\n\n  return <></>;\n}\n\ntype RelevantEditorState = {\n  selection: BaseSelection;\n  text: string;\n};\n\nconst cursorChangedWithoutTextChanged = (\n  prev: RelevantEditorState,\n  next: RelevantEditorState,\n): boolean => {\n  // Check if the selection has changed\n  const isSelectionChanged = !isSelectionEqual(prev.selection, next.selection);\n\n  // Check if the text content remains the same\n  const isTextSame = prev.text === next.text;\n\n  return isSelectionChanged && isTextSame;\n};\n\nconst isSelectionEqual = (a: BaseSelection, b: BaseSelection) => {\n  if (!a && !b) return true;\n  if (!a || !b) return false;\n  return Range.equals(a, b);\n};\n\n/**\n * Easily keep track of the *previous* value of a variable.\n *\n * Example:\n * ```\n * const [count, setCount] = useState(0);\n * const prevCount = usePrevious(count);\n *\n * useEffect(() => {\n *  if (count > prevCount) {\n *   console.log('Now I know that count is bigger than before');\n * }\n * }, [count, prevCount]);\n * ```\n */\nfunction usePrevious<T>(value: T): T | undefined {\n  const ref = useRef<T>();\n\n  useEffect(() => {\n    ref.current = value;\n  });\n\n  return ref.current;\n}\n"],"mappings":";;;;;AAAA,SAAS,WAAW,cAAc;AAElC,SAAS,wBAAwB;AACjC,SAAS,aAAa;AA0Bb;AApBF,SAAS,yCACd,OACA;AACA,QAAM,cAAmC,iBAAiB,CAAC,WAAW;AAAA,IACpE,WAAW,MAAM;AAAA,IACjB,MAAM,aAAa,KAAK;AAAA,EAC1B,EAAE;AAEF,QAAM,gBAAgB,YAAY,WAAW;AAE7C,YAAU,MAAM;AACd,QAAI,CAAC,eAAe;AAClB;AAAA,IACF;AAEA,QAAI,gCAAgC,eAAe,WAAW,GAAG;AAC/D,YAAM,kCAAkC,IAAI;AAAA,IAC9C;AAAA,EACF,GAAG,CAAC,MAAM,mCAAmC,WAAW,CAAC;AAEzD,SAAO,gCAAE;AACX;AAOA,IAAM,kCAAkC,CACtC,MACA,SACY;AAEZ,QAAM,qBAAqB,CAAC,iBAAiB,KAAK,WAAW,KAAK,SAAS;AAG3E,QAAM,aAAa,KAAK,SAAS,KAAK;AAEtC,SAAO,sBAAsB;AAC/B;AAEA,IAAM,mBAAmB,CAAC,GAAkB,MAAqB;AAC/D,MAAI,CAAC,KAAK,CAAC;AAAG,WAAO;AACrB,MAAI,CAAC,KAAK,CAAC;AAAG,WAAO;AACrB,SAAO,MAAM,OAAO,GAAG,CAAC;AAC1B;AAiBA,SAAS,YAAe,OAAyB;AAC/C,QAAM,MAAM,OAAU;AAEtB,YAAU,MAAM;AACd,QAAI,UAAU;AAAA,EAChB,CAAC;AAED,SAAO,IAAI;AACb;","names":[]}