@platejs/ai 51.1.2 → 52.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ import { KEYS, TextApi, bindFirst, createTSlatePlugin, getPluginType } from "platejs";
2
+ import { getTransientSuggestionKey } from "@platejs/suggestion";
3
+ import { isSelecting } from "@platejs/selection";
4
+
5
+ //#region src/lib/transforms/insertAINodes.ts
6
+ const insertAINodes = (editor, nodes, { target } = {}) => {
7
+ if (!target && !editor.selection?.focus.path) return;
8
+ const aiNodes = nodes.map((node) => ({
9
+ ...node,
10
+ ai: true
11
+ }));
12
+ editor.tf.withoutNormalizing(() => {
13
+ editor.tf.insertNodes(aiNodes, {
14
+ at: editor.api.end(target || editor.selection.focus.path),
15
+ select: true
16
+ });
17
+ editor.tf.collapse({ edge: "end" });
18
+ });
19
+ };
20
+
21
+ //#endregion
22
+ //#region src/lib/transforms/removeAIMarks.ts
23
+ const removeAIMarks = (editor, { at = [] } = {}) => {
24
+ const nodeType = getPluginType(editor, KEYS.ai);
25
+ editor.tf.unsetNodes(nodeType, {
26
+ at,
27
+ match: (n) => n[nodeType]
28
+ });
29
+ };
30
+
31
+ //#endregion
32
+ //#region src/lib/transforms/removeAINodes.ts
33
+ const removeAINodes = (editor, { at = [] } = {}) => {
34
+ editor.tf.removeNodes({
35
+ at,
36
+ match: (n) => TextApi.isText(n) && !!n.ai
37
+ });
38
+ };
39
+
40
+ //#endregion
41
+ //#region src/lib/transforms/undoAI.ts
42
+ const undoAI = (editor) => {
43
+ const hasAINodeOrAISuggestion = editor.api.some({
44
+ at: [],
45
+ match: (n) => !!n.ai
46
+ }) || editor.api.some({
47
+ at: [],
48
+ match: (n) => !!n[getTransientSuggestionKey()]
49
+ });
50
+ if (editor.history.undos.at(-1)?.ai && hasAINodeOrAISuggestion) {
51
+ editor.undo();
52
+ editor.history.redos.pop();
53
+ }
54
+ };
55
+
56
+ //#endregion
57
+ //#region src/lib/transforms/withAIBatch.ts
58
+ const withAIBatch = (editor, fn, { split } = {}) => {
59
+ if (split) editor.tf.withNewBatch(fn);
60
+ else editor.tf.withMerging(fn);
61
+ const lastBatch = editor.history.undos?.at(-1);
62
+ if (lastBatch) lastBatch.ai = true;
63
+ };
64
+
65
+ //#endregion
66
+ //#region src/lib/BaseAIPlugin.ts
67
+ const BaseAIPlugin = createTSlatePlugin({
68
+ key: KEYS.ai,
69
+ node: {
70
+ isDecoration: false,
71
+ isLeaf: true
72
+ }
73
+ }).extendTransforms(({ editor }) => ({
74
+ insertNodes: bindFirst(insertAINodes, editor),
75
+ removeMarks: bindFirst(removeAIMarks, editor),
76
+ removeNodes: bindFirst(removeAINodes, editor),
77
+ undo: bindFirst(undoAI, editor)
78
+ }));
79
+
80
+ //#endregion
81
+ //#region src/lib/utils/getEditorPrompt.ts
82
+ const createPromptFromConfig = (config, params) => {
83
+ const { isBlockSelecting, isSelecting: isSelecting$1 } = params;
84
+ if (isBlockSelecting && config.blockSelecting) return config.blockSelecting ?? config.default;
85
+ if (isSelecting$1 && config.selecting) return config.selecting ?? config.default;
86
+ return config.default;
87
+ };
88
+ const getEditorPrompt = (editor, { prompt = "" }) => {
89
+ const params = {
90
+ editor,
91
+ isBlockSelecting: editor.getOption({ key: KEYS.blockSelection }, "isSelectingSome"),
92
+ isSelecting: isSelecting(editor)
93
+ };
94
+ let promptText = "";
95
+ if (typeof prompt === "function") promptText = prompt(params);
96
+ else if (typeof prompt === "object") promptText = createPromptFromConfig(prompt, params);
97
+ else promptText = prompt;
98
+ return promptText;
99
+ };
100
+
101
+ //#endregion
102
+ export { removeAINodes as a, undoAI as i, BaseAIPlugin as n, removeAIMarks as o, withAIBatch as r, insertAINodes as s, getEditorPrompt as t };
@@ -0,0 +1,96 @@
1
+ import * as platejs0 from "platejs";
2
+ import { Descendant, History, OmitFirst, Path, PluginConfig, SlateEditor, TLocation } from "platejs";
3
+
4
+ //#region src/lib/transforms/insertAINodes.d.ts
5
+ declare const insertAINodes: (editor: SlateEditor, nodes: Descendant[], {
6
+ target
7
+ }?: {
8
+ target?: Path;
9
+ }) => void;
10
+ //#endregion
11
+ //#region src/lib/transforms/removeAIMarks.d.ts
12
+ declare const removeAIMarks: (editor: SlateEditor, {
13
+ at
14
+ }?: {
15
+ at?: TLocation;
16
+ }) => void;
17
+ //#endregion
18
+ //#region src/lib/transforms/removeAINodes.d.ts
19
+ declare const removeAINodes: (editor: SlateEditor, {
20
+ at
21
+ }?: {
22
+ at?: Path;
23
+ }) => void;
24
+ //#endregion
25
+ //#region src/lib/transforms/undoAI.d.ts
26
+ declare const undoAI: (editor: SlateEditor) => void;
27
+ //#endregion
28
+ //#region src/lib/transforms/withAIBatch.d.ts
29
+ type AIBatch = History['undos'][number] & {
30
+ ai?: boolean;
31
+ };
32
+ declare const withAIBatch: (editor: SlateEditor, fn: () => void, {
33
+ split
34
+ }?: {
35
+ split?: boolean;
36
+ }) => void;
37
+ //#endregion
38
+ //#region src/lib/BaseAIPlugin.d.ts
39
+ type BaseAIPluginConfig = PluginConfig<'ai', {}, {}, {
40
+ ai: {
41
+ insertNodes: OmitFirst<typeof insertAINodes>;
42
+ removeMarks: OmitFirst<typeof removeAIMarks>;
43
+ removeNodes: OmitFirst<typeof removeAINodes>;
44
+ };
45
+ }>;
46
+ declare const BaseAIPlugin: platejs0.SlatePlugin<PluginConfig<any, {}, {}, Record<any, {
47
+ insertNodes: (nodes: platejs0.Descendant[], args_1?: {
48
+ target?: platejs0.Path;
49
+ } | undefined) => void;
50
+ removeMarks: (args_0?: {
51
+ at?: platejs0.TLocation;
52
+ } | undefined) => void;
53
+ removeNodes: (args_0?: {
54
+ at?: platejs0.Path;
55
+ } | undefined) => void;
56
+ undo: () => void;
57
+ }>, {}>>;
58
+ //#endregion
59
+ //#region src/lib/types.d.ts
60
+ type AIToolName = 'comment' | 'edit' | 'generate' | null;
61
+ type AIMode = 'chat' | 'insert';
62
+ //#endregion
63
+ //#region src/lib/utils/getEditorPrompt.d.ts
64
+ type EditorPrompt = ((params: EditorPromptParams) => string) | PromptConfig | string;
65
+ type EditorPromptParams = {
66
+ editor: SlateEditor;
67
+ isBlockSelecting: boolean;
68
+ isSelecting: boolean;
69
+ };
70
+ type PromptConfig = {
71
+ default: string;
72
+ blockSelecting?: string;
73
+ selecting?: string;
74
+ };
75
+ declare const getEditorPrompt: (editor: SlateEditor, {
76
+ prompt
77
+ }: {
78
+ prompt?: EditorPrompt;
79
+ }) => string;
80
+ //#endregion
81
+ //#region src/lib/utils/replacePlaceholders.d.ts
82
+ type MarkdownType = 'block' | 'blockSelection' | 'blockSelectionWithBlockId' | 'blockWithBlockId' | 'editor' | 'editorWithBlockId';
83
+ declare const replacePlaceholders: (editor: SlateEditor, text: string, {
84
+ prompt
85
+ }?: {
86
+ prompt?: string;
87
+ }) => string;
88
+ //#endregion
89
+ //#region src/lib/utils/getMarkdown.d.ts
90
+ declare const getMarkdown: (editor: SlateEditor, {
91
+ type
92
+ }: {
93
+ type: MarkdownType;
94
+ }) => string;
95
+ //#endregion
96
+ export { insertAINodes as _, EditorPromptParams as a, AIMode as c, BaseAIPluginConfig as d, AIBatch as f, removeAIMarks as g, removeAINodes as h, EditorPrompt as i, AIToolName as l, undoAI as m, MarkdownType as n, PromptConfig as o, withAIBatch as p, replacePlaceholders as r, getEditorPrompt as s, getMarkdown as t, BaseAIPlugin as u };
package/dist/index.d.ts CHANGED
@@ -1,22 +1,2 @@
1
- export { b as AIMode, A as AIToolName, a as BaseAIPlugin, B as BaseAIPluginConfig, E as EditorPrompt, d as EditorPromptParams, P as PromptConfig, g as getEditorPrompt, i as insertAINodes, r as removeAIMarks, c as removeAINodes } from './getEditorPrompt-C6prTxft.js';
2
- import { SlateEditor, History } from 'platejs';
3
-
4
- declare const undoAI: (editor: SlateEditor) => void;
5
-
6
- type AIBatch = History['undos'][number] & {
7
- ai?: boolean;
8
- };
9
- declare const withAIBatch: (editor: SlateEditor, fn: () => void, { split, }?: {
10
- split?: boolean;
11
- }) => void;
12
-
13
- type MarkdownType = 'block' | 'blockSelection' | 'blockSelectionWithBlockId' | 'blockWithBlockId' | 'editor' | 'editorWithBlockId';
14
- declare const replacePlaceholders: (editor: SlateEditor, text: string, { prompt, }?: {
15
- prompt?: string;
16
- }) => string;
17
-
18
- declare const getMarkdown: (editor: SlateEditor, { type, }: {
19
- type: MarkdownType;
20
- }) => string;
21
-
22
- export { type AIBatch, type MarkdownType, getMarkdown, replacePlaceholders, undoAI, withAIBatch };
1
+ import { _ as insertAINodes, a as EditorPromptParams, c as AIMode, d as BaseAIPluginConfig, f as AIBatch, g as removeAIMarks, h as removeAINodes, i as EditorPrompt, l as AIToolName, m as undoAI, n as MarkdownType, o as PromptConfig, p as withAIBatch, r as replacePlaceholders, s as getEditorPrompt, t as getMarkdown, u as BaseAIPlugin } from "./index-CyVv31EV.js";
2
+ export { AIBatch, AIMode, AIToolName, BaseAIPlugin, BaseAIPluginConfig, EditorPrompt, EditorPromptParams, MarkdownType, PromptConfig, getEditorPrompt, getMarkdown, insertAINodes, removeAIMarks, removeAINodes, replacePlaceholders, undoAI, withAIBatch };
package/dist/index.js CHANGED
@@ -1,224 +1,47 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- BaseAIPlugin: () => BaseAIPlugin,
24
- getEditorPrompt: () => getEditorPrompt,
25
- getMarkdown: () => getMarkdown,
26
- insertAINodes: () => insertAINodes,
27
- removeAIMarks: () => removeAIMarks,
28
- removeAINodes: () => removeAINodes,
29
- replacePlaceholders: () => replacePlaceholders,
30
- undoAI: () => undoAI,
31
- withAIBatch: () => withAIBatch
32
- });
33
- module.exports = __toCommonJS(index_exports);
34
-
35
- // src/lib/BaseAIPlugin.ts
36
- var import_platejs3 = require("platejs");
37
-
38
- // src/lib/transforms/insertAINodes.ts
39
- var insertAINodes = (editor, nodes, {
40
- target
41
- } = {}) => {
42
- if (!target && !editor.selection?.focus.path) return;
43
- const aiNodes = nodes.map((node) => ({
44
- ...node,
45
- ai: true
46
- }));
47
- editor.tf.withoutNormalizing(() => {
48
- editor.tf.insertNodes(aiNodes, {
49
- at: editor.api.end(target || editor.selection.focus.path),
50
- select: true
51
- });
52
- editor.tf.collapse({ edge: "end" });
53
- });
54
- };
55
-
56
- // src/lib/transforms/removeAIMarks.ts
57
- var import_platejs = require("platejs");
58
- var removeAIMarks = (editor, { at = [] } = {}) => {
59
- const nodeType = (0, import_platejs.getPluginType)(editor, import_platejs.KEYS.ai);
60
- editor.tf.unsetNodes(nodeType, {
61
- at,
62
- match: (n) => n[nodeType]
63
- });
64
- };
65
-
66
- // src/lib/transforms/removeAINodes.ts
67
- var import_platejs2 = require("platejs");
68
- var removeAINodes = (editor, { at = [] } = {}) => {
69
- editor.tf.removeNodes({
70
- at,
71
- match: (n) => import_platejs2.TextApi.isText(n) && !!n.ai
72
- });
73
- };
74
-
75
- // src/lib/transforms/undoAI.ts
76
- var import_suggestion = require("@platejs/suggestion");
77
- var undoAI = (editor) => {
78
- const hasAINodeOrAISuggestion = editor.api.some({
79
- at: [],
80
- match: (n) => !!n.ai
81
- }) || editor.api.some({
82
- at: [],
83
- match: (n) => !!n[(0, import_suggestion.getTransientSuggestionKey)()]
84
- });
85
- if (editor.history.undos.at(-1)?.ai && hasAINodeOrAISuggestion) {
86
- editor.undo();
87
- editor.history.redos.pop();
88
- }
89
- };
90
-
91
- // src/lib/transforms/withAIBatch.ts
92
- var withAIBatch = (editor, fn, {
93
- split
94
- } = {}) => {
95
- if (split) {
96
- editor.tf.withNewBatch(fn);
97
- } else {
98
- editor.tf.withMerging(fn);
99
- }
100
- const lastBatch = editor.history.undos?.at(-1);
101
- if (lastBatch) {
102
- lastBatch.ai = true;
103
- }
104
- };
105
-
106
- // src/lib/BaseAIPlugin.ts
107
- var BaseAIPlugin = (0, import_platejs3.createTSlatePlugin)({
108
- key: import_platejs3.KEYS.ai,
109
- node: { isDecoration: false, isLeaf: true }
110
- }).extendTransforms(({ editor }) => ({
111
- insertNodes: (0, import_platejs3.bindFirst)(insertAINodes, editor),
112
- removeMarks: (0, import_platejs3.bindFirst)(removeAIMarks, editor),
113
- removeNodes: (0, import_platejs3.bindFirst)(removeAINodes, editor),
114
- undo: (0, import_platejs3.bindFirst)(undoAI, editor)
115
- }));
116
-
117
- // src/lib/utils/getEditorPrompt.ts
118
- var import_selection = require("@platejs/selection");
119
- var import_platejs4 = require("platejs");
120
- var createPromptFromConfig = (config, params) => {
121
- const { isBlockSelecting, isSelecting: isSelecting2 } = params;
122
- if (isBlockSelecting && config.blockSelecting) {
123
- return config.blockSelecting ?? config.default;
124
- }
125
- if (isSelecting2 && config.selecting) {
126
- return config.selecting ?? config.default;
127
- }
128
- return config.default;
129
- };
130
- var getEditorPrompt = (editor, {
131
- prompt = ""
132
- }) => {
133
- const params = {
134
- editor,
135
- isBlockSelecting: editor.getOption(
136
- { key: import_platejs4.KEYS.blockSelection },
137
- "isSelectingSome"
138
- ),
139
- isSelecting: (0, import_selection.isSelecting)(editor)
140
- };
141
- let promptText = "";
142
- if (typeof prompt === "function") {
143
- promptText = prompt(params);
144
- } else if (typeof prompt === "object") {
145
- promptText = createPromptFromConfig(prompt, params);
146
- } else {
147
- promptText = prompt;
148
- }
149
- return promptText;
150
- };
151
-
152
- // src/lib/utils/getMarkdown.ts
153
- var import_markdown = require("@platejs/markdown");
154
- var import_platejs5 = require("platejs");
155
- var getMarkdown = (editor, {
156
- type
157
- }) => {
158
- if (type === "editor" || type === "editorWithBlockId") {
159
- return (0, import_markdown.serializeMd)(editor, {
160
- withBlockId: type === "editorWithBlockId"
161
- });
162
- }
163
- if (type === "block" || type === "blockWithBlockId") {
164
- const blocks = editor.api.blocks({ mode: "highest" }).map(([node]) => node);
165
- return (0, import_markdown.serializeMd)(editor, {
166
- value: blocks,
167
- withBlockId: type === "blockWithBlockId"
168
- });
169
- }
170
- if (type === "blockSelection" || type === "blockSelectionWithBlockId") {
171
- const fragment = editor.api.fragment();
172
- if (fragment.length === 1) {
173
- const modifiedFragment = [
174
- {
175
- children: fragment[0].children,
176
- type: import_platejs5.KEYS.p
177
- }
178
- ];
179
- return (0, import_markdown.serializeMd)(editor, {
180
- value: modifiedFragment,
181
- withBlockId: type === "blockSelectionWithBlockId"
182
- });
183
- }
184
- return (0, import_markdown.serializeMd)(editor, {
185
- value: fragment,
186
- withBlockId: type === "blockSelectionWithBlockId"
187
- });
188
- }
189
- return "";
190
- };
191
-
192
- // src/lib/utils/replacePlaceholders.ts
193
- var replacePlaceholders = (editor, text, {
194
- prompt
195
- } = {}) => {
196
- let result = text.replace("{prompt}", prompt || "");
197
- const placeholders = {
198
- "{blockSelectionWithBlockId}": "blockSelectionWithBlockId",
199
- "{blockSelection}": "blockSelection",
200
- "{blockWithBlockId}": "blockWithBlockId",
201
- "{block}": "block",
202
- "{editorWithBlockId}": "editorWithBlockId",
203
- "{editor}": "editor"
204
- };
205
- Object.entries(placeholders).forEach(([placeholder, type]) => {
206
- if (result.includes(placeholder)) {
207
- result = result.replace(placeholder, getMarkdown(editor, { type }));
208
- }
209
- });
210
- return result;
211
- };
212
- // Annotate the CommonJS export names for ESM import in node:
213
- 0 && (module.exports = {
214
- BaseAIPlugin,
215
- getEditorPrompt,
216
- getMarkdown,
217
- insertAINodes,
218
- removeAIMarks,
219
- removeAINodes,
220
- replacePlaceholders,
221
- undoAI,
222
- withAIBatch
223
- });
224
- //# sourceMappingURL=index.js.map
1
+ import { a as removeAINodes, i as undoAI, n as BaseAIPlugin, o as removeAIMarks, r as withAIBatch, s as insertAINodes, t as getEditorPrompt } from "./getEditorPrompt-BnVrgUl3.js";
2
+ import { KEYS } from "platejs";
3
+ import { serializeMd } from "@platejs/markdown";
4
+
5
+ //#region src/lib/utils/getMarkdown.ts
6
+ const getMarkdown = (editor, { type }) => {
7
+ if (type === "editor" || type === "editorWithBlockId") return serializeMd(editor, { withBlockId: type === "editorWithBlockId" });
8
+ if (type === "block" || type === "blockWithBlockId") return serializeMd(editor, {
9
+ value: editor.api.blocks({ mode: "highest" }).map(([node]) => node),
10
+ withBlockId: type === "blockWithBlockId"
11
+ });
12
+ if (type === "blockSelection" || type === "blockSelectionWithBlockId") {
13
+ const fragment = editor.api.fragment();
14
+ if (fragment.length === 1) return serializeMd(editor, {
15
+ value: [{
16
+ children: fragment[0].children,
17
+ type: KEYS.p
18
+ }],
19
+ withBlockId: type === "blockSelectionWithBlockId"
20
+ });
21
+ return serializeMd(editor, {
22
+ value: fragment,
23
+ withBlockId: type === "blockSelectionWithBlockId"
24
+ });
25
+ }
26
+ return "";
27
+ };
28
+
29
+ //#endregion
30
+ //#region src/lib/utils/replacePlaceholders.ts
31
+ const replacePlaceholders = (editor, text, { prompt } = {}) => {
32
+ let result = text.replace("{prompt}", prompt || "");
33
+ Object.entries({
34
+ "{blockSelectionWithBlockId}": "blockSelectionWithBlockId",
35
+ "{blockSelection}": "blockSelection",
36
+ "{blockWithBlockId}": "blockWithBlockId",
37
+ "{block}": "block",
38
+ "{editorWithBlockId}": "editorWithBlockId",
39
+ "{editor}": "editor"
40
+ }).forEach(([placeholder, type]) => {
41
+ if (result.includes(placeholder)) result = result.replace(placeholder, getMarkdown(editor, { type }));
42
+ });
43
+ return result;
44
+ };
45
+
46
+ //#endregion
47
+ export { BaseAIPlugin, getEditorPrompt, getMarkdown, insertAINodes, removeAIMarks, removeAINodes, replacePlaceholders, undoAI, withAIBatch };