Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Oct 24, 2024
1 parent 2e9ced7 commit 6a2b3af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-zebras-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-ai': patch
---

Fix undo after accept
13 changes: 8 additions & 5 deletions packages/ai/src/lib/transforms/withAIBatch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
type History,
type SlateEditor,
withMerging,
withNewBatch,
} from '@udecode/plate-common';

export type AIBatch = History['undos'][number] & { ai?: boolean };

export const withAIBatch = (
editor: SlateEditor,
fn: () => void,
Expand All @@ -13,15 +16,15 @@ export const withAIBatch = (
split?: boolean;
} = {}
) => {
if (!split && (editor.history.undos?.at(-1) as any)?.ai) {
const lastBatch = editor.history.undos?.at(-1) as AIBatch | undefined;

if (!split && lastBatch?.ai) {
withMerging(editor, fn);
} else {
withNewBatch(editor, fn);

const batch = editor.history.undos?.at(-1) as any;

if (batch) {
batch.ai = true;
if (lastBatch) {
lastBatch.ai = true;
}
}
};
8 changes: 8 additions & 0 deletions packages/ai/src/react/ai-chat/AIChatPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from '@udecode/plate-common/react';
import { BlockSelectionPlugin } from '@udecode/plate-selection/react';

import type { AIBatch } from '../../lib';

import { AIPlugin } from '../ai/AIPlugin';
import { acceptAIChat } from './transforms/acceptAIChat';
import { insertBelowAIChat } from './transforms/insertBelowAIChat';
Expand Down Expand Up @@ -139,6 +141,12 @@ export const AIChatPlugin = createTPlatePlugin<AIChatPluginConfig>({
} else {
focusEditor(editor);
}

const lastBatch = editor.history.undos.at(-1) as AIBatch;

if (lastBatch?.ai) {
delete lastBatch.ai;
}
},
show: () => {
api.aiChat.reset();
Expand Down

0 comments on commit 6a2b3af

Please sign in to comment.