Class: HistoryRecordingService

history.HistoryRecordingService(undoManager)

History recording service.

A self-contained service interface for recording history. Once injected, no other dependencies or globals are required (example: UndoManager, command types, etc.). Easy to mock for unit tests. Built on top of history classes in history.js.

There is a simple start/end interface for batch commands.

HistoryRecordingService.NO_HISTORY is a singleton that can be passed in to functions that record history. This helps when the caller requires that no history be recorded.

The following will record history: insert, batch, insert.

Constructor

new HistoryRecordingService(undoManager)

Parameters:
Name Type Description
undoManager history.UndoManager | null

The undo manager. A value of null is valid for cases where no history recording is required. See singleton: module:history.HistoryRecordingService.HistoryRecordingService.NO_HISTORY

Source:
Examples
hrService = new HistoryRecordingService(this.undoMgr);
hrService.insertElement(elem, text); // add simple command to history.
hrService.startBatchCommand('create two elements');
hrService.changeElement(elem, attrs, text); // add to batchCommand
hrService.changeElement(elem, attrs2, text); // add to batchCommand
hrService.endBatchCommand(); // add batch command with two change commands to history.
hrService.insertElement(elem, text); // add simple command to history.
// Note that all functions return this, so commands can be chained, like so:
hrService
  .startBatchCommand('create two elements')
  .insertElement(elem, text)
  .changeElement(elem, attrs, text)
  .endBatchCommand();

Members

(static) HistoryRecordingService.NO_HISTORY

Properties:
Name Type Description
NO_HISTORY module:history.HistoryRecordingService

Singleton that can be passed to functions that record history, but the caller requires that no history be recorded.

Source:

Methods

changeElement(elem, attrs, textopt) → {module:history.HistoryRecordingService}

Add a ChangeElementCommand to the history or current batch command.

Parameters:
Name Type Attributes Description
elem Element

The DOM element that was changed

attrs module:history.CommandAttributes

An object with the attributes to be changed and the values they had before the change

text string <optional>

An optional string visible to user related to this change

Source:
Returns:
Type
module:history.HistoryRecordingService

endBatchCommand() → {module:history.HistoryRecordingService}

End a batch command and add it to the history or a parent batch command.

Source:
Returns:
Type
module:history.HistoryRecordingService

insertElement(elem, textopt) → {module:history.HistoryRecordingService}

Add an InsertElementCommand to the history or current batch command.

Parameters:
Name Type Attributes Description
elem Element

The DOM element that was added

text string <optional>

An optional string visible to user related to this change

Source:
Returns:
Type
module:history.HistoryRecordingService

moveElement(elem, oldNextSibling, oldParent, textopt) → {module:history.HistoryRecordingService}

Add a MoveElementCommand to the history or current batch command.

Parameters:
Name Type Attributes Description
elem Element

The DOM element that was moved

oldNextSibling Element

The element's next sibling before it was moved

oldParent Element

The element's parent before it was moved

text string <optional>

An optional string visible to user related to this change

Source:
Returns:
Type
module:history.HistoryRecordingService

removeElement(elem, oldNextSibling, oldParent, textopt) → {module:history.HistoryRecordingService}

Add a RemoveElementCommand to the history or current batch command.

Parameters:
Name Type Attributes Description
elem Element

The DOM element that was removed

oldNextSibling Element

The element's next sibling before it was removed

oldParent Element

The element's parent before it was removed

text string <optional>

An optional string visible to user related to this change

Source:
Returns:
Type
module:history.HistoryRecordingService

startBatchCommand(text) → {module:history.HistoryRecordingService}

Start a batch command so multiple commands can recorded as a single history command. Requires a corresponding call to endBatchCommand. Start and end commands can be nested.

Parameters:
Name Type Description
text string

Optional string describing the batch command.

Source:
Returns:
Type
module:history.HistoryRecordingService