Skip to content

Commit c90fb52

Browse files
committed
file refactors: split into more files
1 parent bf812fa commit c90fb52

8 files changed

Lines changed: 112 additions & 117 deletions

File tree

Lines changed: 7 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
// This example is for an Editor with `ReactEditor` and `HistoryEditor`
2-
import {
3-
BaseEditor,
4-
Descendant,
5-
createEditor,
6-
Element,
7-
Transforms,
8-
Editor,
9-
Node,
10-
Path,
11-
} from "slate";
12-
import {
13-
Editable,
14-
ReactEditor,
15-
RenderElementProps,
16-
RenderLeafProps,
17-
Slate,
18-
withReact,
19-
} from "slate-react";
20-
import { HistoryEditor } from "slate-history";
21-
import { useCallback, useEffect, useRef, useState } from "react";
22-
import { editorToText } from "../../lib/editorToText";
2+
import { Descendant } from "slate";
3+
import { Editable, Slate } from "slate-react";
4+
import { useCallback, useEffect, useRef } from "react";
235
import { useAutocomplete } from "../../hooks/useAutocomplete";
246
import { clearAutocompletionsFromEditor } from "../../lib/slatejs-edits/clear-autocompletions";
257
import { addAutocompletionsToEditor } from "../../lib/slatejs-edits/add-autocompletions";
8+
import { useCopilotTextareaEditor } from "../../hooks/useCopilotTextareaEditor";
9+
import { renderElement } from "./render-element";
2610

2711
export interface AutocompleteConfig {
2812
autocomplete: (
@@ -41,32 +25,6 @@ export interface CopilotTextareaProps {
4125
autocompleteConfig: AutocompleteConfig;
4226
}
4327

44-
export type CustomEditor = BaseEditor & ReactEditor & HistoryEditor;
45-
46-
export type ParagraphElement = {
47-
type: "paragraph";
48-
children: CustomText[];
49-
};
50-
51-
export type SuggestionElement = {
52-
type: "suggestion";
53-
inline: boolean;
54-
content: string;
55-
children: CustomText[];
56-
};
57-
58-
export type CustomElement = ParagraphElement | SuggestionElement;
59-
export type SuggestionAwareText = { text: string };
60-
export type CustomText = SuggestionAwareText;
61-
62-
declare module "slate" {
63-
interface CustomTypes {
64-
Editor: CustomEditor;
65-
Element: CustomElement;
66-
Text: CustomText;
67-
}
68-
}
69-
7028
export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
7129
const initialValue: Descendant[] = [
7230
{
@@ -75,48 +33,15 @@ export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
7533
},
7634
];
7735

78-
const [editor] = useState(() => {
79-
const editor = withReact(createEditor());
80-
81-
const { isVoid } = editor;
82-
editor.isVoid = (element) => {
83-
switch (element.type) {
84-
case "suggestion":
85-
return true;
86-
default:
87-
return isVoid(element);
88-
}
89-
};
90-
91-
const { markableVoid } = editor;
92-
editor.markableVoid = (element) => {
93-
switch (element.type) {
94-
case "suggestion":
95-
return true;
96-
default:
97-
return markableVoid(element);
98-
}
99-
};
100-
101-
const { isInline } = editor;
102-
editor.isInline = (element) => {
103-
switch (element.type) {
104-
case "suggestion":
105-
return element.inline;
106-
default:
107-
return isInline(element);
108-
}
109-
};
110-
111-
return editor;
112-
});
36+
const editor = useCopilotTextareaEditor();
11337

11438
const renderElementMemoized = useCallback(renderElement, []);
11539
const {
11640
currentAutocompleteSuggestion,
11741
onChangeHandler: onChangeHandlerForAutocomplete,
11842
} = useAutocomplete(props.autocompleteConfig);
11943

44+
// sync autosuggestions state with the editor
12045
useEffect(() => {
12146
clearAutocompletionsFromEditor(editor);
12247
if (currentAutocompleteSuggestion) {
@@ -144,31 +69,3 @@ export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
14469
</Slate>
14570
);
14671
}
147-
148-
function renderElement(props: RenderElementProps) {
149-
switch (props.element.type) {
150-
case "paragraph":
151-
return <DefaultElement {...props} />;
152-
case "suggestion":
153-
return <SuggestionElement {...props} />;
154-
}
155-
}
156-
157-
const DefaultElement = (props: RenderElementProps) => {
158-
return <div {...props.attributes}>{props.children}</div>;
159-
};
160-
161-
const SuggestionElement = (props: RenderElementProps) => {
162-
return (
163-
<span
164-
{...props.attributes}
165-
style={{
166-
fontStyle: "italic",
167-
color: "gray",
168-
}}
169-
contentEditable={false}
170-
>
171-
{props.element.type === "suggestion" && props.element.content}
172-
</span>
173-
);
174-
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { RenderElementProps } from "slate-react";
2+
3+
export function renderElement(props: RenderElementProps) {
4+
switch (props.element.type) {
5+
case "paragraph":
6+
return <DefaultElement {...props} />;
7+
case "suggestion":
8+
return <SuggestionElement {...props} />;
9+
}
10+
}
11+
const DefaultElement = (props: RenderElementProps) => {
12+
return <div {...props.attributes}>{props.children}</div>;
13+
};
14+
const SuggestionElement = (props: RenderElementProps) => {
15+
return (
16+
<span
17+
{...props.attributes}
18+
style={{
19+
fontStyle: "italic",
20+
color: "gray",
21+
}}
22+
contentEditable={false}
23+
>
24+
{props.element.type === "suggestion" && props.element.content}
25+
</span>
26+
);
27+
};

packages/react-textarea/src/hooks/useAutocomplete.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useCallback, useMemo, useRef, useState } from "react";
2-
import {
3-
AutocompleteConfig,
4-
CustomEditor,
5-
} from "../components/copilot-textarea/copilot-textarea";
2+
import { AutocompleteConfig } from "../components/copilot-textarea/copilot-textarea";
3+
import { CustomEditor } from "../types/custom-editor";
64
import { BasePoint, Descendant, Transforms } from "slate";
75
import { Debouncer } from "../lib/debouncer";
86
import { getTextAroundCursor } from "../lib/getTextAroundCursor";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createEditor } from "slate";
2+
import { withReact } from "slate-react";
3+
import { useState } from "react";
4+
import { CustomEditor } from "../types/custom-editor";
5+
6+
export function useCopilotTextareaEditor(): CustomEditor {
7+
const [editor] = useState(() => {
8+
const editor = withReact(createEditor());
9+
10+
const { isVoid } = editor;
11+
editor.isVoid = (element) => {
12+
switch (element.type) {
13+
case "suggestion":
14+
return true;
15+
default:
16+
return isVoid(element);
17+
}
18+
};
19+
20+
const { markableVoid } = editor;
21+
editor.markableVoid = (element) => {
22+
switch (element.type) {
23+
case "suggestion":
24+
return true;
25+
default:
26+
return markableVoid(element);
27+
}
28+
};
29+
30+
const { isInline } = editor;
31+
editor.isInline = (element) => {
32+
switch (element.type) {
33+
case "suggestion":
34+
return element.inline;
35+
default:
36+
return isInline(element);
37+
}
38+
};
39+
40+
return editor;
41+
});
42+
43+
return editor;
44+
}

packages/react-textarea/src/lib/editorToText.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BaseEditor, Descendant, Element } from "slate";
22
import { HistoryEditor } from "slate-history";
33
import { ReactEditor } from "slate-react";
4-
import { SuggestionAwareText } from "../components/copilot-textarea/copilot-textarea";
4+
import { SuggestionAwareText } from "../types/custom-editor";
55

66
function nodeChildrenToTextComponents(
77
editor: BaseEditor & ReactEditor & HistoryEditor,

packages/react-textarea/src/lib/slatejs-edits/add-autocompletions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BasePoint, Transforms } from "slate";
2-
import { CustomEditor } from "../../components/copilot-textarea/copilot-textarea";
2+
import { CustomEditor } from "../../types/custom-editor";
33

44
export function addAutocompletionsToEditor(
55
editor: CustomEditor,

packages/react-textarea/src/lib/slatejs-edits/clear-autocompletions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Path, Node, Element, Transforms } from "slate";
2-
import { CustomEditor } from "../../components/copilot-textarea/copilot-textarea";
2+
import { CustomEditor } from "../../types/custom-editor";
33

44
export function clearAutocompletionsFromEditor(editor: CustomEditor) {
55
// clear previous suggestion
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { BaseEditor } from "slate";
2+
import { ReactEditor } from "slate-react";
3+
import { HistoryEditor } from "slate-history";
4+
5+
export type CustomEditor = BaseEditor & ReactEditor & HistoryEditor;
6+
7+
export type ParagraphElement = {
8+
type: "paragraph";
9+
children: CustomText[];
10+
};
11+
12+
export type SuggestionElement = {
13+
type: "suggestion";
14+
inline: boolean;
15+
content: string;
16+
children: CustomText[];
17+
};
18+
19+
export type CustomElement = ParagraphElement | SuggestionElement;
20+
export type SuggestionAwareText = { text: string };
21+
export type CustomText = SuggestionAwareText;
22+
23+
declare module "slate" {
24+
interface CustomTypes {
25+
Editor: CustomEditor;
26+
Element: CustomElement;
27+
Text: CustomText;
28+
}
29+
}

0 commit comments

Comments
 (0)