Skip to content

Commit

Permalink
withNewBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Oct 19, 2024
1 parent a35c1e9 commit a73f0d4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
27 changes: 7 additions & 20 deletions packages/ai/src/lib/transforms/insertAINodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
collapseSelection,
getEndPoint,
insertNodes,
withMerging,
withoutMergingHistory,
withNewBatch,
} from '@udecode/plate-common';

import { AIPlugin } from '../../react/ai/AIPlugin';
Expand All @@ -16,10 +15,10 @@ export const insertAINodes = (
editor: SlateEditor,
nodes: TDescendant[],
{
history = 'default',
splitHistory = false,
target,
}: {
history?: 'default' | 'merge' | 'withoutMerge';
splitHistory?: boolean;
target?: Path;
} = {}
) => {
Expand All @@ -38,21 +37,9 @@ export const insertAINodes = (
collapseSelection(editor, { edge: 'end' });
};

switch (history) {
case 'default': {
insert();

break;
}
case 'merge': {
withMerging(editor, insert);

break;
}
case 'withoutMerge': {
withoutMergingHistory(editor, insert);

break;
}
if (splitHistory) {
withNewBatch(editor, insert);
} else {
insert();
}
};
4 changes: 2 additions & 2 deletions packages/ai/src/react/ai-chat/transforms/acceptAIChat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { withMerging } from '@udecode/plate-common';
import { withNewBatch } from '@udecode/plate-common';
import {
type PlateEditor,
focusEditor,
Expand All @@ -12,7 +12,7 @@ import { AIPlugin } from '../../ai/AIPlugin';
export const acceptAIChat = (editor: PlateEditor) => {
const { tf } = getEditorPlugin(editor, AIPlugin);

withMerging(editor, () => {
withNewBatch(editor, () => {
tf.ai.removeMarks();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PlateEditor } from '@udecode/plate-common/react';

import { isEditorEmpty, withMerging } from '@udecode/plate-common';
import { isEditorEmpty, withNewBatch } from '@udecode/plate-common';
import { focusEditor } from '@udecode/plate-common/react';
import {
BlockSelectionPlugin,
Expand Down Expand Up @@ -31,7 +31,7 @@ export const replaceSelectionAIChat = (
editor.withoutNormalizing(() => {
removeBlockSelectionNodes(editor);

withMerging(editor, () => {
withNewBatch(editor, () => {
editor
.getTransforms(BlockSelectionPlugin)
.blockSelection.insertBlocksAndSelect(
Expand Down
12 changes: 6 additions & 6 deletions packages/ai/src/react/ai-chat/useAIChatHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const useAIChatHooks = () => {
const mode = useOption('mode');

useChatChunk({
onChunk: ({ isFirst, nodes }) => {
onChunk: ({ nodes }) => {
if (mode === 'insert' && nodes.length > 0) {
tf.ai.insertNodes(nodes, { history: isFirst ? 'default' : 'merge' });
tf.ai.insertNodes(nodes, { splitHistory: true });
}
},
onFinish: ({ content }) => {
Expand All @@ -28,11 +28,11 @@ export const useAIChatHooks = () => {
editor.undo();
editor.history.redos.pop();

setTimeout(() => {
const nodes = deserializeInlineMd(editor, content);
// setTimeout(() => {
const nodes = deserializeInlineMd(editor, content);

tf.ai.insertNodes(nodes);
}, 0);
tf.ai.insertNodes(nodes, { splitHistory: true });
// }, 0);
},
});
};
1 change: 1 addition & 0 deletions packages/slate/src/interfaces/history-editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './isHistorySaving';
export * from './withMerging';
export * from './withoutMergingHistory';
export * from './withoutSavingHistory';
export * from './withNewBatch';
4 changes: 2 additions & 2 deletions packages/slate/src/interfaces/history-editor/withNewBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { TEditor } from '../editor';
import { HistoryEditor } from '../../slate-history';

/** {@link HistoryEditor.withMerging} */
export const withMerging = (editor: TEditor, fn: () => void) =>
HistoryEditor.withMerging(editor as any, fn);
export const withNewBatch = (editor: TEditor, fn: () => void) =>
HistoryEditor.withNewBatch(editor as any, fn);

0 comments on commit a73f0d4

Please sign in to comment.