{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { isShallowEqual } from '@wordpress/is-shallow-equal';\nimport type {\n\tHistoryChange as _HistoryChange,\n\tHistoryChanges as _HistoryChanges,\n\tHistoryRecord as _HistoryRecord,\n\tUndoManager as _UndoManager,\n} from './types';\n\n/**\n * Represents a single change in history.\n */\nexport type HistoryChange< T = unknown > = _HistoryChange< T >;\n\n/**\n * Represents changes for a single item.\n */\nexport type HistoryChanges< T = unknown > = _HistoryChanges< T >;\n\n/**\n * Represents a record of history changes.\n */\nexport type HistoryRecord< T = unknown > = _HistoryRecord< T >;\n\n/**\n * The undo manager interface.\n */\nexport type UndoManager< T = unknown > = _UndoManager< T >;\n\n/**\n * Merge changes for a single item into a record of changes.\n *\n * @param changes1 Previous changes\n * @param changes2 Next changes\n *\n * @return Merged changes\n */\nfunction mergeHistoryChanges< T >(\n\tchanges1: Record< string, HistoryChange< T > >,\n\tchanges2: Record< string, HistoryChange< T > >\n): Record< string, HistoryChange< T > > {\n\tconst newChanges: Record< string, HistoryChange< T > > = { ...changes1 };\n\tObject.entries( changes2 ).forEach( ( [ key, value ] ) => {\n\t\tif ( newChanges[ key ] ) {\n\t\t\tnewChanges[ key ] = { ...newChanges[ key ], to: value.to };\n\t\t} else {\n\t\t\tnewChanges[ key ] = value;\n\t\t}\n\t} );\n\n\treturn newChanges;\n}\n\n/**\n * Adds history changes for a single item into a record of changes.\n *\n * @param record  The record to merge into.\n * @param changes The changes to merge.\n */\nconst addHistoryChangesIntoRecord = < T >(\n\trecord: HistoryRecord< T >,\n\tchanges: HistoryChanges< T >\n): HistoryRecord< T > => {\n\tconst existingChangesIndex = record?.findIndex(\n\t\t( { id: recordIdentifier } ) => {\n\t\t\treturn typeof recordIdentifier === 'string'\n\t\t\t\t? recordIdentifier === changes.id\n\t\t\t\t: isShallowEqual( recordIdentifier, changes.id );\n\t\t}\n\t);\n\tconst nextRecord = [ ...record ];\n\n\tif ( existingChangesIndex !== -1 ) {\n\t\t// If the edit is already in the stack leave the initial \"from\" value.\n\t\tnextRecord[ existingChangesIndex ] = {\n\t\t\tid: changes.id,\n\t\t\tchanges: mergeHistoryChanges(\n\t\t\t\tnextRecord[ existingChangesIndex ].changes,\n\t\t\t\tchanges.changes\n\t\t\t),\n\t\t};\n\t} else {\n\t\tnextRecord.push( changes );\n\t}\n\treturn nextRecord;\n};\n\n/**\n * Creates an undo manager.\n *\n * @return Undo manager.\n */\nexport function createUndoManager< T = unknown >(): UndoManager< T > {\n\tlet history: HistoryRecord< T >[] = [];\n\tlet stagedRecord: HistoryRecord< T > = [];\n\tlet offset = 0;\n\n\tconst dropPendingRedos = (): void => {\n\t\thistory = history.slice( 0, offset || undefined );\n\t\toffset = 0;\n\t};\n\n\tconst appendStagedRecordToLatestHistoryRecord = (): void => {\n\t\tconst index = history.length === 0 ? 0 : history.length - 1;\n\t\tlet latestRecord = history[ index ] ?? [];\n\t\tstagedRecord.forEach( ( changes ) => {\n\t\t\tlatestRecord = addHistoryChangesIntoRecord( latestRecord, changes );\n\t\t} );\n\t\tstagedRecord = [];\n\t\thistory[ index ] = latestRecord;\n\t};\n\n\t/**\n\t * Checks whether a record is empty.\n\t * A record is considered empty if it the changes keep the same values.\n\t * Also updates to function values are ignored.\n\t *\n\t * @param record The record to check.\n\t * @return Whether the record is empty.\n\t */\n\tconst isRecordEmpty = ( record: HistoryRecord< T > ): boolean => {\n\t\tconst filteredRecord = record.filter( ( { changes } ) => {\n\t\t\treturn Object.values( changes ).some(\n\t\t\t\t( { from, to } ) =>\n\t\t\t\t\ttypeof from !== 'function' &&\n\t\t\t\t\ttypeof to !== 'function' &&\n\t\t\t\t\t! isShallowEqual( from, to )\n\t\t\t);\n\t\t} );\n\t\treturn ! filteredRecord.length;\n\t};\n\n\treturn {\n\t\taddRecord( record?: HistoryRecord< T >, isStaged = false ): void {\n\t\t\tconst isEmpty = ! record || isRecordEmpty( record );\n\t\t\tif ( isStaged ) {\n\t\t\t\tif ( isEmpty ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\trecord.forEach( ( changes ) => {\n\t\t\t\t\tstagedRecord = addHistoryChangesIntoRecord(\n\t\t\t\t\t\tstagedRecord,\n\t\t\t\t\t\tchanges\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\tdropPendingRedos();\n\t\t\t\tif ( stagedRecord.length ) {\n\t\t\t\t\tappendStagedRecordToLatestHistoryRecord();\n\t\t\t\t}\n\t\t\t\tif ( isEmpty ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\thistory.push( record );\n\t\t\t}\n\t\t},\n\n\t\tundo(): HistoryRecord< T > | undefined {\n\t\t\tif ( stagedRecord.length ) {\n\t\t\t\tdropPendingRedos();\n\t\t\t\tappendStagedRecordToLatestHistoryRecord();\n\t\t\t}\n\t\t\tconst undoRecord = history[ history.length - 1 + offset ];\n\t\t\tif ( ! undoRecord ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\toffset -= 1;\n\t\t\treturn undoRecord;\n\t\t},\n\n\t\tredo(): HistoryRecord< T > | undefined {\n\t\t\tconst redoRecord = history[ history.length + offset ];\n\t\t\tif ( ! redoRecord ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\toffset += 1;\n\t\t\treturn redoRecord;\n\t\t},\n\n\t\thasUndo(): boolean {\n\t\t\treturn !! history[ history.length - 1 + offset ];\n\t\t},\n\n\t\thasRedo(): boolean {\n\t\t\treturn !! history[ history.length + offset ];\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;;;;;AAoCA,SAAS,oBACR,UACA,UACuC;CACvC,MAAM,aAAmD,EAAE,GAAG,SAAS;CACvE,OAAO,QAAS,QAAS,CAAC,CAAC,SAAW,CAAE,KAAK,WAAa;EACzD,IAAK,WAAY,MAChB,WAAY,OAAQ;GAAE,GAAG,WAAY;GAAO,IAAI,MAAM;EAAG;OAEzD,WAAY,OAAQ;CAEtB,CAAE;CAEF,OAAO;AACR;;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