Skip to content

Commit

Permalink
Merge pull request #3644 from udecode/ai-release
Browse files Browse the repository at this point in the history
Ai packages
  • Loading branch information
zbeyens authored Oct 19, 2024
2 parents 1ce4126 + a73f0d4 commit 159f671
Show file tree
Hide file tree
Showing 93 changed files with 2,417 additions and 1,342 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-mice-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-markdown': patch
---

New `deserializeInlineMd` `serializeInlineMd` `stripMarkdownBlocks` `stripMarkdownInline`
7 changes: 7 additions & 0 deletions .changeset/eight-gorillas-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@udecode/plate-selection': patch
---

BlockSelectionPlugin: New `tf.setBlockSelectionIndent` `tf.insertBlocksAndSelect`

BlockMenuPlugin: Now when the left mouse button is clicked and the menu is open, the default event will not be prevented.
5 changes: 5 additions & 0 deletions .changeset/fresh-spoons-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-math': patch
---

New `editor.tf.insert.equation` `editor.tf.insert.inlineEquation`
5 changes: 5 additions & 0 deletions .changeset/new-socks-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-callout': patch
---

New `editor.tf.insert.callout`
6 changes: 6 additions & 0 deletions .changeset/small-months-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@udecode/plate-media': patch
---

New `editor.tf.insert.audioPlaceholder` `editor.tf.insert.filePlaceholder` `editor.tf.insert.imagePlaceholder` `editor.tf.insert.videoPlaceholder`

5 changes: 5 additions & 0 deletions .changeset/sour-cobras-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-ai': patch
---

`CopilotPlugin`, `AIPlugin` is ready from this version.
8 changes: 7 additions & 1 deletion packages/ai/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
WIP
# Plate ai

Visit https://platejs.org/docs/ai to view the documentation.

## License

[MIT](../../LICENSE)
1 change: 1 addition & 0 deletions packages/ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@udecode/plate-combobox": "39.0.0",
"@udecode/plate-markdown": "39.1.5",
"@udecode/plate-selection": "39.1.4",
"ai": "^3.4.10",
"lodash": "^4.17.21"
},
"peerDependencies": {
Expand Down
53 changes: 37 additions & 16 deletions packages/ai/src/lib/BaseAIPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
import type { TriggerComboboxPluginOptions } from '@udecode/plate-combobox';

import {
type OmitFirst,
type PluginConfig,
type SlateEditor,
type TNodeEntry,
bindFirst,
createTSlatePlugin,
} from '@udecode/plate-common';

import { withTriggerAIMenu } from './withTriggerAIMenu';
import { removeAIMarks } from './transforms';
import { insertAINodes } from './transforms/insertAINodes';
import { removeAINodes } from './transforms/removeAINodes';

export type BaseAIOptions = {
onOpenAI?: (editor: SlateEditor, nodeEntry: TNodeEntry) => void;
} & TriggerComboboxPluginOptions;
type BaseAIOptions = {};
type BaseAITransforms = {
insertNodes: OmitFirst<typeof insertAINodes>;
removeMarks: OmitFirst<typeof removeAIMarks>;
removeNodes: OmitFirst<typeof removeAINodes>;
};

export type BaseAIPluginConfig = PluginConfig<'ai', BaseAIOptions>;
export type BaseAIPluginConfig = PluginConfig<
'ai',
BaseAIOptions,
{},
{ ai: BaseAITransforms }
>;

export const BaseAIPlugin = createTSlatePlugin({
key: 'ai',
extendEditor: withTriggerAIMenu,
options: {
scrollContainerSelector: '#scroll_container',
trigger: ' ',
triggerPreviousCharPattern: /^\s?$/,
},
});
node: { isLeaf: true },
})
.extendTransforms(({ editor }) => ({
insertNodes: bindFirst(insertAINodes, editor),
removeMarks: bindFirst(removeAIMarks, editor),
removeNodes: bindFirst(removeAINodes, editor),
}))
.extend({
extendEditor: ({ editor }) => {
const { apply } = editor;

editor.apply = (op) => {
// console.log('🚀 ~ editor.apply= ~ op:', op);
// console.log('history', editor.history.undos);
apply(op);
};

return editor;
},
});
2 changes: 1 addition & 1 deletion packages/ai/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*/

export * from './BaseAIPlugin';
export * from './withTriggerAIMenu';
export * from './transforms/index';
7 changes: 7 additions & 0 deletions packages/ai/src/lib/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @file Automatically generated by barrelsby.
*/

export * from './insertAINodes';
export * from './removeAIMarks';
export * from './removeAINodes';
45 changes: 45 additions & 0 deletions packages/ai/src/lib/transforms/insertAINodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Path } from 'slate';

import {
type SlateEditor,
type TDescendant,
collapseSelection,
getEndPoint,
insertNodes,
withNewBatch,
} from '@udecode/plate-common';

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

export const insertAINodes = (
editor: SlateEditor,
nodes: TDescendant[],
{
splitHistory = false,
target,
}: {
splitHistory?: boolean;
target?: Path;
} = {}
) => {
if (!target && !editor.selection?.focus.path) return;

const insert = () => {
const aiNodes = nodes.map((node) => ({
...node,
[AIPlugin.key]: true,
}));

insertNodes(editor, aiNodes, {
at: getEndPoint(editor, target || editor.selection!.focus.path),
select: true,
});
collapseSelection(editor, { edge: 'end' });
};

if (splitHistory) {
withNewBatch(editor, insert);
} else {
insert();
}
};
15 changes: 15 additions & 0 deletions packages/ai/src/lib/transforms/removeAIMarks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Location } from 'slate';

import { type SlateEditor, getRange, removeMark } 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),
});
};
15 changes: 15 additions & 0 deletions packages/ai/src/lib/transforms/removeAINodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Path } from 'slate';

import { type SlateEditor, isText, removeNodes } from '@udecode/plate-common';

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

export const removeAINodes = (
editor: SlateEditor,
{ at = [] }: { at?: Path } = {}
) => {
removeNodes(editor, {
at,
match: (n) => isText(n) && !!n[AIPlugin.key],
});
};
75 changes: 0 additions & 75 deletions packages/ai/src/lib/withTriggerAIMenu.ts

This file was deleted.

Loading

0 comments on commit 159f671

Please sign in to comment.