Skip to content

Commit

Permalink
Merge branch 'main' into pro
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Oct 25, 2024
2 parents 6b9894f + 1953d41 commit 1f0ce40
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@udecode/plate-ai': patch
---

fix accept + undo
fix
12 changes: 12 additions & 0 deletions packages/ai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @udecode/plate-ai

## 39.2.7

### Patch Changes

- [`6a2b3af07b2e14a7906e838dbd843fe3490fd82c`](https://github.com/udecode/plate/commit/6a2b3af07b2e14a7906e838dbd843fe3490fd82c) by [@zbeyens](https://github.com/zbeyens) – Fix undo after accept

## 39.2.6

### Patch Changes

- [`b5dfdc1c3f4cb760685b61e3925e82ff894cf4ab`](https://github.com/udecode/plate/commit/b5dfdc1c3f4cb760685b61e3925e82ff894cf4ab) by [@zbeyens](https://github.com/zbeyens) – fix accept + undo

## 39.2.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@udecode/plate-ai",
"version": "39.2.5",
"version": "39.2.7",
"description": "Text AI plugin for Plate",
"keywords": [
"plate",
Expand Down
8 changes: 4 additions & 4 deletions packages/ai/src/lib/transforms/removeAIMarks.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Location } from 'slate';

import { type SlateEditor, getRange, removeMark } from '@udecode/plate-common';
import { type SlateEditor, unsetNodes } from '@udecode/plate-common';

import { AIPlugin } from '../../react/ai/AIPlugin';

export const removeAIMarks = (
editor: SlateEditor,
{ at = [] }: { at?: Location } = {}
) => {
removeMark(editor, {
key: AIPlugin.key,
at: getRange(editor, at),
unsetNodes(editor, AIPlugin.key, {
at,
match: (n) => (n as any)[AIPlugin.key],
});
};
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
10 changes: 7 additions & 3 deletions packages/ai/src/react/ai-chat/useAIChatHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export const useAIChatHooks = () => {

const nodes = deserializeInlineMd(editor, content);

withAIBatch(editor, () => {
tf.ai.insertNodes(nodes);
});
withAIBatch(
editor,
() => {
tf.ai.insertNodes(nodes);
},
{ split: true }
);
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import {
someNode,
} from '@udecode/plate-common';
import {
createPlateEditor,
ParagraphPlugin,
Plate,
PlateLeaf,
usePlateEditor,
} from '@udecode/plate-common/react';
import { DndPlugin } from '@udecode/plate-dnd';
import { DocxPlugin } from '@udecode/plate-docx';
Expand Down Expand Up @@ -164,7 +164,7 @@ export default function PlateEditor() {
}

export const useMyEditor = () => {
const editor = createPlateEditor({
return usePlateEditor({
plugins: [
// Nodes
HeadingPlugin,
Expand Down Expand Up @@ -450,6 +450,4 @@ export const useMyEditor = () => {
},
],
});

return editor;
};

0 comments on commit 1f0ce40

Please sign in to comment.