@blocklet/editor 1.6.241 → 1.6.243
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.
- package/lib/ext/AIPlugin/index.d.ts +0 -1
- package/lib/ext/AIPlugin/index.js +6 -21
- package/lib/ext/Aide/context.d.ts +2 -3
- package/lib/ext/Aide/context.js +5 -7
- package/lib/ext/Aide/hooks.d.ts +7 -0
- package/lib/ext/Aide/hooks.js +14 -0
- package/lib/main/themes/customTheme.js +2 -2
- package/package.json +3 -3
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { LexicalCommand } from 'lexical';
|
|
2
2
|
import { Mode } from '../Aide/types';
|
|
3
3
|
export declare const ACTIVATE_AI_COMMAND: LexicalCommand<Mode>;
|
|
4
|
-
export declare const AI_AVAILABLE_STATE_COMMAND: LexicalCommand<boolean>;
|
|
5
4
|
export declare const AI_TEMPLATES_AVAILABLE_COMMAND: LexicalCommand<boolean>;
|
|
6
5
|
export declare function AideFloatingToolbarButton({ onClick }: {
|
|
7
6
|
onClick?: () => void;
|
|
@@ -12,22 +12,14 @@ import { Aide, AideProvider, useAideContext } from '../Aide';
|
|
|
12
12
|
import TextGeneration from './TextGeneration';
|
|
13
13
|
import { replace, insertBelow, copy } from './utils';
|
|
14
14
|
import AIKitIcon from './AIIcon';
|
|
15
|
+
import { useAIAvailable } from '../Aide/hooks';
|
|
15
16
|
export const ACTIVATE_AI_COMMAND = createCommand('ACTIVATE_AI_COMMAND');
|
|
16
|
-
// AI 可用性状态变化时通过该命令与 editor 其它插件通讯
|
|
17
|
-
export const AI_AVAILABLE_STATE_COMMAND = createCommand('AI_AVAILABLE_STATE_COMMAND');
|
|
18
17
|
// AI external templates 可用时通过该命令与 editor 其它插件通讯
|
|
19
18
|
export const AI_TEMPLATES_AVAILABLE_COMMAND = createCommand('AI_TEMPLATES_AVAILABLE_COMMAND');
|
|
20
|
-
const globalStates = {
|
|
19
|
+
const globalStates = { templatesAvailable: false };
|
|
21
20
|
export function AideFloatingToolbarButton({ onClick }) {
|
|
22
21
|
const [editor] = useLexicalComposerContext();
|
|
23
|
-
const
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
return editor.registerCommand(AI_AVAILABLE_STATE_COMMAND, (payload) => {
|
|
26
|
-
setAvailable(payload);
|
|
27
|
-
globalStates.available = payload;
|
|
28
|
-
return false;
|
|
29
|
-
}, COMMAND_PRIORITY_NORMAL);
|
|
30
|
-
}, [editor]);
|
|
22
|
+
const { available } = useAIAvailable();
|
|
31
23
|
return (_jsx("button", { disabled: !available, onClick: () => {
|
|
32
24
|
editor.dispatchCommand(ACTIVATE_AI_COMMAND, 'normal');
|
|
33
25
|
onClick?.();
|
|
@@ -35,14 +27,10 @@ export function AideFloatingToolbarButton({ onClick }) {
|
|
|
35
27
|
}
|
|
36
28
|
export function AideToolbarButton() {
|
|
37
29
|
const [editor] = useLexicalComposerContext();
|
|
38
|
-
const
|
|
30
|
+
const { available } = useAIAvailable();
|
|
39
31
|
const [templatesAvailable, setTemplatesAvailable] = useState(globalStates.templatesAvailable);
|
|
40
32
|
useEffect(() => {
|
|
41
|
-
return mergeRegister(editor.registerCommand(
|
|
42
|
-
setAvailable(payload);
|
|
43
|
-
globalStates.available = payload;
|
|
44
|
-
return false;
|
|
45
|
-
}, COMMAND_PRIORITY_NORMAL), editor.registerCommand(AI_TEMPLATES_AVAILABLE_COMMAND, (payload) => {
|
|
33
|
+
return mergeRegister(editor.registerCommand(AI_TEMPLATES_AVAILABLE_COMMAND, (payload) => {
|
|
46
34
|
setTemplatesAvailable(payload);
|
|
47
35
|
globalStates.templatesAvailable = payload;
|
|
48
36
|
return false;
|
|
@@ -138,9 +126,6 @@ function AidePlugin() {
|
|
|
138
126
|
const [editor] = useLexicalComposerContext();
|
|
139
127
|
const { available, status, externalTemplates, activateAI } = useAideContext();
|
|
140
128
|
const selectionRef = useRef(null);
|
|
141
|
-
useEffect(() => {
|
|
142
|
-
editor.dispatchCommand(AI_AVAILABLE_STATE_COMMAND, available);
|
|
143
|
-
}, [available]);
|
|
144
129
|
useEffect(() => {
|
|
145
130
|
editor.dispatchCommand(AI_TEMPLATES_AVAILABLE_COMMAND, externalTemplates.templates.length > 0);
|
|
146
131
|
}, [externalTemplates]);
|
|
@@ -189,7 +174,7 @@ export default function AidePluginWrapper() {
|
|
|
189
174
|
const _copy = (output) => {
|
|
190
175
|
copy(output);
|
|
191
176
|
};
|
|
192
|
-
return (_jsx(AideProvider, {
|
|
177
|
+
return (_jsx(AideProvider, { completions: AI.completions, replaceSelection: _replaceSelection, copy: _copy, insertBelow: _insertBelow, children: _jsx(AidePlugin, {}) }));
|
|
193
178
|
}
|
|
194
179
|
return null;
|
|
195
180
|
}
|
|
@@ -4,13 +4,11 @@ import { Template } from './types';
|
|
|
4
4
|
interface AideProviderProps {
|
|
5
5
|
children: ReactNode;
|
|
6
6
|
completions: Completions;
|
|
7
|
-
checkAvailable: () => Promise<boolean> | boolean;
|
|
8
7
|
replaceSelection?: (selection: any, output: string) => void;
|
|
9
8
|
insertBelow?: (selection: any, output: string) => void;
|
|
10
9
|
copy?: (output: string) => void;
|
|
11
10
|
}
|
|
12
11
|
interface AideContextState {
|
|
13
|
-
available: boolean;
|
|
14
12
|
status: Status;
|
|
15
13
|
mode: Mode;
|
|
16
14
|
templates: Template[];
|
|
@@ -27,6 +25,7 @@ interface AideContextState {
|
|
|
27
25
|
output: string;
|
|
28
26
|
}
|
|
29
27
|
interface AideContextType extends AideContextState, Pick<AideProviderProps, 'replaceSelection' | 'insertBelow' | 'completions' | 'copy'> {
|
|
28
|
+
available: boolean;
|
|
30
29
|
filteredTemplates: Template[];
|
|
31
30
|
activeBuiltinTemplate?: Template;
|
|
32
31
|
activateAI: ({ mode, selectionText, selection, }: {
|
|
@@ -44,5 +43,5 @@ interface AideContextType extends AideContextState, Pick<AideProviderProps, 'rep
|
|
|
44
43
|
}
|
|
45
44
|
export declare const AideContext: import("react").Context<AideContextType>;
|
|
46
45
|
export declare const useAideContext: () => AideContextType;
|
|
47
|
-
export declare function AideProvider({
|
|
46
|
+
export declare function AideProvider({ completions, replaceSelection, insertBelow, copy, children }: AideProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
48
47
|
export {};
|
package/lib/ext/Aide/context.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { createContext, useCallback, useContext, useMemo, useRef } from 'react';
|
|
3
|
-
import { useGetState
|
|
3
|
+
import { useGetState } from 'ahooks';
|
|
4
4
|
import builtinTemplates from './builtin-templates';
|
|
5
5
|
import { Template } from './types';
|
|
6
|
+
import { useAIAvailable } from './hooks';
|
|
6
7
|
export const AideContext = createContext({});
|
|
7
8
|
export const useAideContext = () => useContext(AideContext);
|
|
8
|
-
export function AideProvider({
|
|
9
|
+
export function AideProvider({ completions, replaceSelection, insertBelow, copy, children }) {
|
|
9
10
|
const [state, setState, getState] = useGetState({
|
|
10
|
-
available: false,
|
|
11
11
|
templates: builtinTemplates,
|
|
12
12
|
externalTemplates: { templates: [], hasMore: true, tags: [], tag: undefined, loading: false },
|
|
13
13
|
mode: 'normal',
|
|
@@ -41,10 +41,7 @@ export function AideProvider({ checkAvailable, completions, replaceSelection, in
|
|
|
41
41
|
};
|
|
42
42
|
// @deprecated
|
|
43
43
|
const loadMoreTemplates = async () => { };
|
|
44
|
-
|
|
45
|
-
const available = await checkAvailable();
|
|
46
|
-
setState((prev) => ({ ...prev, available }));
|
|
47
|
-
}, { cacheKey: 'editor-aide-available' });
|
|
44
|
+
const { available } = useAIAvailable();
|
|
48
45
|
const activateAI = useCallback(
|
|
49
46
|
// eslint-disable-next-line require-await
|
|
50
47
|
async ({ mode = 'normal', selectionText, selection }) => {
|
|
@@ -98,6 +95,7 @@ export function AideProvider({ checkAvailable, completions, replaceSelection, in
|
|
|
98
95
|
const stop = () => setState((prev) => ({ ...prev, status: 'activated' }));
|
|
99
96
|
const value = useMemo(() => ({
|
|
100
97
|
...state,
|
|
98
|
+
available,
|
|
101
99
|
filteredTemplates,
|
|
102
100
|
activeBuiltinTemplate,
|
|
103
101
|
activateAI,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useRequest } from 'ahooks';
|
|
2
|
+
import { useEditorConfig } from '../../config';
|
|
3
|
+
/**
|
|
4
|
+
* 检查 ai 是否可用
|
|
5
|
+
* 注意: 该 hook 可适用于无法使用 useAideContext 的场景, 比如 editor toolbar button 中
|
|
6
|
+
*/
|
|
7
|
+
export function useAIAvailable() {
|
|
8
|
+
const { AI } = useEditorConfig();
|
|
9
|
+
const { data } = useRequest(async () => {
|
|
10
|
+
const available = await AI?.checkAvailable();
|
|
11
|
+
return available;
|
|
12
|
+
}, { cacheKey: 'editor-aide-available', staleTime: -1 });
|
|
13
|
+
return { available: !!data };
|
|
14
|
+
}
|
|
@@ -146,7 +146,7 @@ const createCustomTheme = (theme) => {
|
|
|
146
146
|
padding-left: 0.25em;
|
|
147
147
|
`, theme.typography.body1, { lineHeight: 1.5 });
|
|
148
148
|
const listItemChecked = css `
|
|
149
|
-
margin-left: 0;
|
|
149
|
+
margin-left: 0 !important;
|
|
150
150
|
margin-right: 8px;
|
|
151
151
|
padding-left: 24px;
|
|
152
152
|
padding-right: 24px;
|
|
@@ -163,7 +163,7 @@ const createCustomTheme = (theme) => {
|
|
|
163
163
|
}
|
|
164
164
|
`;
|
|
165
165
|
const listItemUnchecked = css `
|
|
166
|
-
margin-left: 0;
|
|
166
|
+
margin-left: 0 !important;
|
|
167
167
|
margin-right: 8px;
|
|
168
168
|
padding-left: 24px;
|
|
169
169
|
padding-right: 24px;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/editor",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.243",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "npm run storybook",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@arcblock/ux": "^2.9.77",
|
|
41
41
|
"@blocklet/embed": "^0.1.11",
|
|
42
42
|
"@blocklet/pages-kit": "^0.2.302",
|
|
43
|
-
"@blocklet/pdf": "1.6.
|
|
43
|
+
"@blocklet/pdf": "1.6.243",
|
|
44
44
|
"@excalidraw/excalidraw": "^0.14.2",
|
|
45
45
|
"@iconify/iconify": "^3.0.1",
|
|
46
46
|
"@iconify/icons-tabler": "^1.2.95",
|
|
@@ -110,5 +110,5 @@
|
|
|
110
110
|
"react": "*",
|
|
111
111
|
"react-dom": "*"
|
|
112
112
|
},
|
|
113
|
-
"gitHead": "
|
|
113
|
+
"gitHead": "8b98e3ebd3806ae3808162de2a50541e9b8cbc98"
|
|
114
114
|
}
|