import { waitReactUpdate } from '@/utils';
import { CPageDataType } from '@chamn/model';
import { cloneDeep } from 'lodash-es';
import { CPlugin, CPluginCtx } from '../../core/pluginManager';
const PLUGIN_NAME = 'History';
export const HistoryPlugin: CPlugin = (ctx) => {
const CTX: CPluginCtx | null = ctx;
historyRecords: [] as CPageDataType[],
let originalPageRecord: CPageDataType | null = null;
const pageSchema = ctx.pageModel.export();
originalPageRecord = pageSchema;
dataStore.historyRecords.push(pageSchema);
const loadPage = async (page: CPageDataType) => {
CTX.pageModel.reloadPage(page);
const { currentStepIndex, historyRecords } = dataStore;
const newPage = ctx.pageModel.export();
if (currentStepIndex !== historyRecords.length - 1) {
dataStore.historyRecords = historyRecords.slice(0, currentStepIndex + 1);
dataStore.historyRecords.push(newPage);
dataStore.currentStepIndex = historyRecords.length - 1;
console.warn('plugin ctx is null, pls check it');
if (!originalPageRecord) {
dataStore.historyRecords = [];
loadPage(originalPageRecord);
const { currentStepIndex, historyRecords } = dataStore;
if (!resObj.canGoPreStep()) {
const newIndex = currentStepIndex - 1;
dataStore.currentStepIndex = newIndex;
const page = cloneDeep(historyRecords[newIndex]);
if (!resObj.canGoNextStep()) {
const { currentStepIndex, historyRecords } = dataStore;
const newIndex = currentStepIndex + 1;
dataStore.currentStepIndex = newIndex;
const page = cloneDeep(historyRecords[newIndex]);
const { currentStepIndex } = dataStore;
if (currentStepIndex <= 0) {
const { currentStepIndex, historyRecords } = dataStore;
if (currentStepIndex >= historyRecords.length - 1) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
ctx.pageModel.emitter.on('onNodeChange', () => {
ctx.pageModel.emitter.on('onPageChange', () => {
// !!! 必须调用,通知 engine,插件初始化完成,可以被消费
console.log('destroy', ctx);