toMap method

Future<Map> toMap()

Converts the state history to a Map.

Returns a Map representing the state history of the editor, including layers, filters, stickers, and other configurations.

Implementation

Future<Map> toMap() async {
  _contentRecorderCtrl = ContentRecorderController(
    configs: const ProImageEditorConfigs(
      imageGenerationConfigs: ImageGeneratioConfigs(
          outputFormat: OutputFormat.png,
          processorConfigs:
              ProcessorConfigs(processorMode: ProcessorMode.minimum)),
    ),
  );

  List history = [];
  List<Uint8List> stickers = [];
  List<EditorStateHistory> changes = List.from(stateHistory);

  if (changes.isNotEmpty) changes.removeAt(0);

  /// Choose history span
  switch (_configs.historySpan) {
    case ExportHistorySpan.current:
      changes = [changes[_editorPosition - 1]];
      break;
    case ExportHistorySpan.currentAndBackward:
      changes.removeRange(_editorPosition, changes.length);
      break;
    case ExportHistorySpan.currentAndForward:
      changes.removeRange(0, _editorPosition - 1);
      break;
    case ExportHistorySpan.all:
      break;
  }

  /// Build Layers and filters
  for (EditorStateHistory element in changes) {
    List layers = [];

    await _convertLayers(
      element: element,
      layers: layers,
      stickers: stickers,
      imageInfos: imageInfos,
    );

    Map transformConfigsMap = element.transformConfigs.toMap();
    history.add({
      if (layers.isNotEmpty) 'layers': layers,
      if (_configs.exportFilter && element.filters.isNotEmpty)
        'filters': element.filters,
      'blur': element.blur,
      if (transformConfigsMap.isNotEmpty) 'transform': transformConfigsMap,
    });
  }
  await _contentRecorderCtrl.destroy();

  return {
    'version': ExportImportVersion.version_2_0_0,
    'position': _configs.historySpan == ExportHistorySpan.current ||
            _configs.historySpan == ExportHistorySpan.currentAndForward
        ? 0
        : _editorPosition - 1,
    if (history.isNotEmpty) 'history': history,
    if (stickers.isNotEmpty) 'stickers': stickers,
    'imgSize': {
      'width': _imgSize.width,
      'height': _imgSize.height,
    },
  };
}