Skip to content

Commit

Permalink
feat: added create with ai
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Nov 22, 2023
1 parent b48fd78 commit 4c232a5
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/components/context-menu/ContextMenuList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export default defineComponent({
background-color: var(--cl-primary-plain-color);
}
&.disabled {
cursor: not-allowed;
opacity: 0.5;
}
.title {
white-space: nowrap;
overflow: hidden;
Expand Down
12 changes: 10 additions & 2 deletions src/components/file/FileEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
@node-db-click="onNavItemDbClick"
@node-drop="onNavItemDrop"
@ctx-menu-new-file="onContextMenuNewFile"
@ctx-menu-new-file-with-ai="onContextMenuNewFileWithAi"
@ctx-menu-new-directory="onContextMenuNewDirectory"
@ctx-menu-rename="onContextMenuRename"
@ctx-menu-clone="onContextMenuClone"
Expand Down Expand Up @@ -191,6 +192,7 @@ export default defineComponent({
'node-drop',
'save-file',
'ctx-menu-new-file',
'ctx-menu-new-file-with-ai',
'ctx-menu-new-directory',
'ctx-menu-rename',
'ctx-menu-clone',
Expand Down Expand Up @@ -459,6 +461,11 @@ export default defineComponent({
sendEvent('click_file_editor_nav_menu_item_context_menu_new_file');
};
const onContextMenuNewFileWithAi = (item: FileNavItem) => {
console.debug(item);
emit('ctx-menu-new-file-with-ai', item);
};
const onContextMenuNewDirectory = (item: FileNavItem, name: string) => {
emit('ctx-menu-new-directory', item, name);
Expand Down Expand Up @@ -676,8 +683,8 @@ export default defineComponent({
codeMirrorTemplateEditor.setOption('mode', 'text/x-python');
};
const onCreateWithAi = (name: string, sourceCode: string) => {
emit('create-with-ai', name, sourceCode);
const onCreateWithAi = (name: string, sourceCode: string, item?: FileNavItem) => {
emit('create-with-ai', name, sourceCode, item);
};
onMounted(initEditor);
Expand Down Expand Up @@ -709,6 +716,7 @@ export default defineComponent({
onNavItemDbClick,
onNavItemDrop,
onContextMenuNewFile,
onContextMenuNewFileWithAi,
onContextMenuNewDirectory,
onContextMenuRename,
onContextMenuClone,
Expand Down
20 changes: 5 additions & 15 deletions src/components/file/FileEditorCreateWithAiDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</el-select>
</el-form-item>
<el-form-item :label="t('components.file.editor.createWithAi.form.prompt')" prop="prompt" required>
<el-input v-model="form.prompt" :placeholder="t('components.file.editor.createWithAi.form.prompt')"/>
<el-input v-model="form.prompt" type="textarea" :placeholder="t('components.file.editor.createWithAi.form.prompt')"/>
</el-form-item>
</el-form>
<template #footer>
Expand All @@ -50,16 +50,11 @@
</template>

<script lang="ts">
import {computed, defineComponent, onBeforeMount, readonly, ref} from 'vue';
import {computed, defineComponent, ref} from 'vue';
import {useStore} from 'vuex';
import {plainClone} from '@/utils/object';
import {getOptionDefinition, getThemes} from '@/utils/codemirror';
import {onBeforeRouteLeave} from 'vue-router';
import {useI18n} from 'vue-i18n';
import {sendEvent} from '@/admin/umeng';
import useRequest from "@/services/request";
import useSpiderService from "@/services/spider/spiderService";
import useSpiderDetail from "@/views/spider/detail/useSpiderDetail";
import {ElMessage} from "element-plus";
const {
Expand All @@ -83,13 +78,6 @@ export default defineComponent({
return editorCreateWithAiDialogVisible;
});
const {
activeId,
} = useSpiderDetail();
// spider id
const id = computed<string>(() => activeId.value);
const form = ref({
fileName: '',
url: '',
Expand Down Expand Up @@ -143,6 +131,8 @@ export default defineComponent({
}
});
const item = computed(() => file.editorFileNavItem);
const onClose = () => {
store.commit(`${storeNamespace}/setEditorCreateWithAiDialogVisible`, false);
};
Expand All @@ -161,7 +151,7 @@ export default defineComponent({
} as any);
const sourceCode = res.data?.source_code || res.data?.output.source_code;
store.commit(`${storeNamespace}/setEditorCreateWithAiDialogVisible`, false);
emit('create', form.value.fileName, sourceCode);
emit('create', form.value.fileName, sourceCode, item.value);
} catch (e: any) {
ElMessage.error(e.message)
} finally {
Expand Down
8 changes: 8 additions & 0 deletions src/components/file/FileEditorNavMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@delete="onNodeContextMenuDelete(data)"
@rename="onNodeContextMenuRename(data)"
@new-file="onNodeContextMenuNewFile(data)"
@new-file-with-ai="onNodeContextMenuNewFileWithAi(data)"
@new-directory="onNodeContextMenuNewDirectory(data)"
>
<div
Expand Down Expand Up @@ -113,6 +114,7 @@ export default defineComponent({
'node-db-click',
'node-drop',
'ctx-menu-new-file',
'ctx-menu-new-file-with-ai',
'ctx-menu-new-directory',
'ctx-menu-rename',
'ctx-menu-clone',
Expand Down Expand Up @@ -231,6 +233,11 @@ export default defineComponent({
emit('ctx-menu-new-file', item, res.value);
};
const onNodeContextMenuNewFileWithAi = async (item: FileNavItem) => {
console.debug(item);
emit('ctx-menu-new-file-with-ai', item);
};
const onNodeContextMenuNewDirectory = async (item: FileNavItem) => {
const res = await ElMessageBox.prompt(
t('components.file.editor.messageBox.prompt.newDirectory'),
Expand Down Expand Up @@ -414,6 +421,7 @@ export default defineComponent({
onNodeContextMenuShow,
onNodeContextMenuHide,
onNodeContextMenuNewFile,
onNodeContextMenuNewFileWithAi,
onNodeContextMenuNewDirectory,
onNodeContextMenuRename,
onNodeContextMenuClone,
Expand Down
20 changes: 17 additions & 3 deletions src/components/file/FileEditorNavMenuContextMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,45 @@

<script lang="ts">
import {computed, defineComponent} from 'vue';
import ContextMenu, {contextMenuDefaultProps} from '@/components/context-menu/ContextMenu.vue';
import ContextMenuList from '@/components/context-menu/ContextMenuList.vue';
import {contextMenuDefaultProps} from '@/components/context-menu/ContextMenu.vue';
import {useI18n} from 'vue-i18n';
import {useStore} from "vuex";
export default defineComponent({
name: 'FileEditorNavMenuContextMenu',
props: contextMenuDefaultProps,
emits: [
'hide',
'new-file',
'new-file-with-ai',
'new-directory',
'rename',
'clone',
'delete',
],
setup(props, {emit}) {
setup(_, {emit}) {
const {t} = useI18n();
const store = useStore();
const {
common: commonState
} = store.state as RootStoreState;
const systemInfo = computed<SystemInfo>(() => commonState.systemInfo || {});
const items = computed<ContextMenuItem[]>(() => [
{
title: t('components.file.editor.navMenu.newFile'),
icon: ['fa', 'file-alt'],
className: 'new-file',
action: () => emit('new-file'),
},
{
title: t('components.file.editor.navMenu.newFileWithAi'),
icon: ['fa', 'robot'],
className: 'new-file-with-ai' + (systemInfo.value.edition === 'global.edition.community' ? ' disabled' : ''),
action: () => emit('new-file-with-ai'),
},
{
title: t('components.file.editor.navMenu.newDirectory'),
icon: ['fa', 'folder-plus'],
Expand Down
1 change: 1 addition & 0 deletions src/i18n/lang/en/components/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const file: LComponentsFile = {
},
navMenu: {
newFile: 'New File',
newFileWithAi: 'New File with AI',
newDirectory: 'New Directory',
rename: 'Rename',
duplicate: 'Duplicate',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/lang/zh/components/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const file: LComponentsFile = {
},
navMenu: {
newFile: '新建文件',
newFileWithAi: '用 AI 新建文件',
newDirectory: '新建目录',
rename: '重命名',
duplicate: '复制',
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/i18n/components/file.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface LComponentsFile {
};
navMenu: {
newFile: string;
newFileWithAi: string;
newDirectory: string;
rename: string;
duplicate: string;
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/store/modules/file.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare global {
editorOptions: FileEditorConfiguration;
editorSettingsDialogVisible: boolean;
editorCreateWithAiDialogVisible: boolean;
editorFileNavItem?: FileNavItem;
}

// interface FileStoreGetters extends GetterTree<FileStoreState, RootStoreState> {
Expand All @@ -21,5 +22,7 @@ declare global {
resetEditorOptions: StoreMutation<FileStoreState>;
setEditorSettingsDialogVisible: StoreMutation<FileStoreState, boolean>;
setEditorCreateWithAiDialogVisible: StoreMutation<FileStoreState, boolean>;
resetEditorFileNavItem: StoreMutation<FileStoreState>;
setEditorFileNavItem: StoreMutation<FileStoreState, FileNavItem>;
}
}
7 changes: 7 additions & 0 deletions src/store/modules/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default {
editorOptions: getDefaultEditorOptions(),
editorSettingsDialogVisible: false,
editorCreateWithAiDialogVisible: false,
editorFileNavItem: undefined,
},
mutations: {
setEditorOptions: (state: FileStoreState, options: EditorConfiguration) => {
Expand All @@ -66,6 +67,12 @@ export default {
setEditorCreateWithAiDialogVisible: (state: FileStoreState, value: boolean) => {
state.editorCreateWithAiDialogVisible = value;
},
resetEditorFileNavItem: (state: FileStoreState) => {
state.editorFileNavItem = undefined;
},
setEditorFileNavItem: (state: FileStoreState, value: FileNavItem) => {
state.editorFileNavItem = value;
},
},
actions: {}
} as FileStoreModule;
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default defineComponent({
};
const onOpenCreateWithAi = async () => {
store.commit('file/resetEditorFileNavItem');
store.commit(`file/setEditorCreateWithAiDialogVisible`, true);
};
Expand Down
15 changes: 13 additions & 2 deletions src/views/spider/detail/tabs/SpiderDetailTabFiles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@node-db-click="onNavItemDbClick"
@node-drop="onNavItemDrop"
@ctx-menu-new-file="onContextMenuNewFile"
@ctx-menu-new-file-with-ai="onContextMenuNewFileWithAi"
@ctx-menu-new-directory="onContextMenuNewDirectory"
@ctx-menu-rename="onContextMenuRename"
@ctx-menu-clone="onContextMenuClone"
Expand Down Expand Up @@ -134,6 +135,12 @@ export default defineComponent({
await openFile(path);
};
const onContextMenuNewFileWithAi = async (item: FileNavItem) => {
console.debug(item);
store.commit('file/setEditorFileNavItem', item);
store.commit(`file/setEditorCreateWithAiDialogVisible`, true);
};
const onContextMenuNewDirectory = async (item: FileNavItem, name: string) => {
if (!item.path) return;
const path = getPath(item, name);
Expand Down Expand Up @@ -173,8 +180,11 @@ export default defineComponent({
await listRootDir(id.value);
};
const onCreateWithAi = async (name: string, sourceCode: string) => {
const path = `/${name}`;
const onCreateWithAi = async (name: string, sourceCode: string, item?: FileNavItem) => {
let path = `/${name}`;
if (item) {
path = getPath(item, name);
}
await saveFile(id.value, path, sourceCode);
await listRootDir(id.value);
await openFile(path);
Expand Down Expand Up @@ -215,6 +225,7 @@ export default defineComponent({
onNavItemDbClick,
onNavItemDrop,
onContextMenuNewFile,
onContextMenuNewFileWithAi,
onContextMenuNewDirectory,
onContextMenuRename,
onContextMenuClone,
Expand Down

0 comments on commit 4c232a5

Please sign in to comment.