{"version":3,"sources":["../src/lib/slatejs-edits/with-partial-history.ts"],"sourcesContent":["import { Editor, Operation, Path, Range, Transforms } from \"slate\";\nimport { HistoryEditor } from \"slate-history\";\n\n// Copy-pasted from `https://github.com/ianstormtaylor/slate/blob/main/packages/slate-history/src/with-history.ts`\n// With one exception: the `shouldSave` function is passed in as an argument to `withPartialHistory` instead of being hardcoded\nexport type ShouldSaveToHistory = (op: Operation, prev: Operation | undefined) => boolean;\n\nexport const withPartialHistory = <T extends Editor>(\n  editor: T,\n  shouldSave: ShouldSaveToHistory,\n) => {\n  const e = editor as T & HistoryEditor;\n  const { apply } = e;\n  e.history = { undos: [], redos: [] };\n\n  e.redo = () => {\n    const { history } = e;\n    const { redos } = history;\n\n    if (redos.length > 0) {\n      const batch = redos[redos.length - 1];\n\n      if (batch.selectionBefore) {\n        Transforms.setSelection(e, batch.selectionBefore);\n      }\n\n      HistoryEditor.withoutSaving(e, () => {\n        Editor.withoutNormalizing(e, () => {\n          for (const op of batch.operations) {\n            e.apply(op);\n          }\n        });\n      });\n\n      history.redos.pop();\n      e.writeHistory(\"undos\", batch);\n    }\n  };\n\n  e.undo = () => {\n    const { history } = e;\n    const { undos } = history;\n\n    if (undos.length > 0) {\n      const batch = undos[undos.length - 1];\n\n      HistoryEditor.withoutSaving(e, () => {\n        Editor.withoutNormalizing(e, () => {\n          const inverseOps = batch.operations.map(Operation.inverse).reverse();\n\n          for (const op of inverseOps) {\n            e.apply(op);\n          }\n          if (batch.selectionBefore) {\n            Transforms.setSelection(e, batch.selectionBefore);\n          }\n        });\n      });\n\n      e.writeHistory(\"redos\", batch);\n      history.undos.pop();\n    }\n  };\n\n  e.apply = (op: Operation) => {\n    const { operations, history } = e;\n    const { undos } = history;\n    const lastBatch = undos[undos.length - 1];\n    const lastOp = lastBatch && lastBatch.operations[lastBatch.operations.length - 1];\n    let save = HistoryEditor.isSaving(e);\n    let merge = HistoryEditor.isMerging(e);\n\n    if (save == null) {\n      save = shouldSave(op, lastOp);\n    }\n\n    if (save) {\n      if (merge == null) {\n        if (lastBatch == null) {\n          merge = false;\n        } else if (operations.length !== 0) {\n          merge = true;\n        } else {\n          merge = shouldMerge(op, lastOp);\n        }\n      }\n\n      if (lastBatch && merge) {\n        lastBatch.operations.push(op);\n      } else {\n        const batch = {\n          operations: [op],\n          selectionBefore: e.selection,\n        };\n        e.writeHistory(\"undos\", batch);\n      }\n\n      while (undos.length > 100) {\n        undos.shift();\n      }\n\n      history.redos = [];\n    }\n\n    apply(op);\n  };\n\n  e.writeHistory = (stack: \"undos\" | \"redos\", batch: any) => {\n    e.history[stack].push(batch);\n  };\n\n  return e;\n};\n\n/**\n * Check whether to merge an operation into the previous operation.\n */\n\nconst shouldMerge = (op: Operation, prev: Operation | undefined): boolean => {\n  if (\n    prev &&\n    op.type === \"insert_text\" &&\n    prev.type === \"insert_text\" &&\n    op.offset === prev.offset + prev.text.length &&\n    Path.equals(op.path, prev.path)\n  ) {\n    return true;\n  }\n\n  if (\n    prev &&\n    op.type === \"remove_text\" &&\n    prev.type === \"remove_text\" &&\n    op.offset + op.text.length === prev.offset &&\n    Path.equals(op.path, prev.path)\n  ) {\n    return true;\n  }\n\n  return false;\n};\n\nexport const defaultShouldSave = (op: Operation, prev: Operation | undefined): boolean => {\n  if (op.type === \"set_selection\") {\n    return false;\n  }\n\n  return true;\n};\n"],"mappings":";AAAA,SAAS,QAAQ,WAAW,MAAa,kBAAkB;AAC3D,SAAS,qBAAqB;AAMvB,IAAM,qBAAqB,CAChC,QACA,eACG;AACH,QAAM,IAAI;AACV,QAAM,EAAE,MAAM,IAAI;AAClB,IAAE,UAAU,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,EAAE;AAEnC,IAAE,OAAO,MAAM;AACb,UAAM,EAAE,QAAQ,IAAI;AACpB,UAAM,EAAE,MAAM,IAAI;AAElB,QAAI,MAAM,SAAS,GAAG;AACpB,YAAM,QAAQ,MAAM,MAAM,SAAS,CAAC;AAEpC,UAAI,MAAM,iBAAi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