{"version":3,"file":"timelineEditGesture.mjs","names":[],"sources":["../../../src/hooks/editing/timelineEditGesture.ts"],"sourcesContent":["import { timelineCommandFail, timelineCommandOk } from '@techsquidtv/canvas-timeline-core';\nimport type {\n  TimelineCommandResult,\n  TimelineEditCommand,\n  TimelineEditPreview,\n  TimelineEngine,\n} from '@techsquidtv/canvas-timeline-core';\n\n/** Owns one gesture's preview, including cancellation before its first move. */\nexport class TimelineEditGesture {\n  preview: TimelineEditPreview | null = null;\n  private current = true;\n  private publishing = false;\n  private readonly unsubscribe: () => void;\n\n  constructor(private readonly engine: TimelineEngine) {\n    this.unsubscribe = engine.on('edit:preview', () => {\n      if (!this.publishing) {\n        this.current = false;\n      }\n    });\n  }\n\n  isCurrent() {\n    return this.current && this.engine.getEditPreview() === this.preview;\n  }\n\n  publish(command: TimelineEditCommand) {\n    this.publishing = true;\n    try {\n      this.preview = this.engine.previewEdit(command);\n      return this.preview;\n    } finally {\n      this.publishing = false;\n    }\n  }\n\n  /** Commits only this gesture's preview, then clears its feedback. */\n  commit(): TimelineCommandResult {\n    const current = this.isCurrent();\n    const preview = this.preview;\n    this.release();\n    if (!current) {\n      return timelineCommandFail('unsupported', 'The gesture preview was replaced.');\n    }\n    if (!preview) {\n      this.engine.cancelEdit();\n      return timelineCommandOk();\n    }\n    const result = this.engine.commitEdit(preview.command);\n    this.engine.cancelEdit();\n    return result.committed\n      ? timelineCommandOk()\n      : timelineCommandFail(result.preview.reason ?? 'unsupported', result.preview.message);\n  }\n\n  cancel() {\n    const current = this.isCurrent();\n    this.release();\n    if (current) {\n      this.engine.cancelEdit();\n    }\n    return current;\n  }\n\n  release() {\n    this.unsubscribe();\n    this.current = false;\n  }\n}\n"],"mappings":";;;AASA,IAAa,sBAAb,MAAiC;CAM/B,YAAY,QAAyC;EAAxB,KAAA,SAAA;EALS,KAAA,UAAA;EACpB,KAAA,UAAA;EACG,KAAA,aAAA;EAInB,KAAK,cAAc,OAAO,GAAG,sBAAsB;GACjD,IAAI,CAAC,KAAK,YACR,KAAK,UAAU;EAEnB,CAAC;CACH;CAEA,YAAY;EACV,OAAO,KAAK,WAAW,KAAK,OAAO,eAAe,MAAM,KAAK;CAC/D;CAEA,QAAQ,SAA8B;EACpC,KAAK,aAAa;EAClB,IAAI;GACF,KAAK,UAAU,KAAK,OAAO,YAAY,OAAO;GAC9C,OAAO,KAAK;EACd,UAAU;GACR,KAAK,aAAa;EACpB;CACF;;CAGA,SAAgC;EAC9B,MAAM,UAAU,KAAK,UAAU;EAC/B,MAAM,UAAU,KAAK;EACrB,KAAK,QAAQ;EACb,IAAI,CAAC,SACH,OAAO,oBAAoB,eAAe,mCAAmC;EAE/E,IAAI,CAAC,SAAS;GACZ,KAAK,OAAO,WAAW;GACvB,OAAO,kBAAkB;EAC3B;EACA,MAAM,SAAS,KAAK,OAAO,WAAW,QAAQ,OAAO;EACrD,KAAK,OAAO,WAAW;EACvB,OAAO,OAAO,YACV,kBAAkB,IAClB,oBAAoB,OAAO,QAAQ,UAAU,eAAe,OAAO,QAAQ,OAAO;CACxF;CAEA,SAAS;EACP,MAAM,UAAU,KAAK,UAAU;EAC/B,KAAK,QAAQ;EACb,IAAI,SACF,KAAK,OAAO,WAAW;EAEzB,OAAO;CACT;CAEA,UAAU;EACR,KAAK,YAAY;EACjB,KAAK,UAAU;CACjB;AACF"}