@@ -143,6 +143,10 @@ export default defineComponent({
143143 promptLibraryData: {
144144 type: Array as () => PromptLibraryItem [],
145145 default : () => []
146+ },
147+ selectedPrompt: {
148+ type: Object as () => PromptLibraryItem | null ,
149+ default: null
146150 }
147151 },
148152
@@ -158,10 +162,6 @@ export default defineComponent({
158162 const isSaving = ref (false );
159163 const errorMessage = ref (' ' );
160164
161- // 本地新增的分类 (尚未保存到服务器的)
162- const localCategories = ref <string []>([]);
163- const localSubCategories = ref <{[category : string ]: string []}>({});
164-
165165 // 新提示词数据
166166 const newPrompt = ref <NewPromptData >({
167167 text: ' ' ,
@@ -173,12 +173,8 @@ export default defineComponent({
173173
174174 // 一级分类列表
175175 const categories = computed (() => {
176- // 获取已有分类
177- const categorySet = new Set ([
178- ... props .promptLibraryData .map (item => item .category ),
179- ... localCategories .value
180- ]);
181-
176+ // 直接从props中获取分类
177+ const categorySet = new Set (props .promptLibraryData .map (item => item .category ));
182178 return Array .from (categorySet ).sort ();
183179 });
184180
@@ -195,10 +191,6 @@ export default defineComponent({
195191 filteredItems .forEach (item => {
196192 if (item .subCategory ) subCategorySet .add (item .subCategory );
197193 });
198-
199- // 加入本地新增的二级分类
200- const localSubs = localSubCategories .value [newPrompt .value .category ] || [];
201- localSubs .forEach (sub => subCategorySet .add (sub ));
202194 }
203195
204196 return Array .from (subCategorySet ).sort ();
@@ -228,8 +220,6 @@ export default defineComponent({
228220 newCategory .value = ' ' ;
229221 newSubCategory .value = ' ' ;
230222 errorMessage .value = ' ' ;
231-
232- // 注意:不重置localCategories和localSubCategories,因为需要保留用户创建的分类
233223 };
234224
235225 // 添加新一级分类
@@ -242,9 +232,6 @@ export default defineComponent({
242232 return ;
243233 }
244234
245- // 添加到本地分类列表
246- localCategories .value .push (newCategory .value .trim ());
247-
248235 // 设置新分类
249236 newPrompt .value .category = newCategory .value .trim ();
250237 showAddCategory .value = false ;
@@ -261,14 +248,6 @@ export default defineComponent({
261248 return ;
262249 }
263250
264- // 初始化分类的子分类数组(如果不存在)
265- if (! localSubCategories .value [newPrompt .value .category ]) {
266- localSubCategories .value [newPrompt .value .category ] = [];
267- }
268-
269- // 添加到本地二级分类列表
270- localSubCategories .value [newPrompt .value .category ].push (newSubCategory .value .trim ());
271-
272251 // 设置新二级分类
273252 newPrompt .value .subCategory = newSubCategory .value .trim ();
274253 showAddSubCategory .value = false ;
@@ -319,6 +298,23 @@ export default defineComponent({
319298 }
320299 };
321300
301+ // 监听选中的提示词变化
302+ watch (() => props .selectedPrompt , (selected ) => {
303+ if (selected ) {
304+ // 判断是否为英文
305+ const isEnglish = / ^ [a-zA-Z0-9 \s \-_ ,. ] + $ / .test (selected .text );
306+
307+ // 如果有选中的提示词,填充到表单
308+ newPrompt .value = {
309+ text: selected .text ,
310+ translated: isEnglish ? selected .chinese : selected .english ,
311+ category: selected .category ,
312+ subCategory: selected .subCategory ,
313+ isEnglish: isEnglish
314+ };
315+ }
316+ }, { immediate: true });
317+
322318 // 保存到提示词库
323319 const saveToLibrary = async () => {
324320 if (! canSaveToLibrary .value ) return ;
@@ -335,6 +331,11 @@ export default defineComponent({
335331 subCategory: newPrompt .value .subCategory || ' 默认' // 未选择时使用默认分类
336332 };
337333
334+ // 如果是在编辑已有提示词,添加ID
335+ if (props .selectedPrompt ) {
336+ (newItem as any ).id = props .selectedPrompt .id ;
337+ }
338+
338339 // 保存到后端
339340 const savedItem = await PromptsAPI .savePromptToLibrary (newItem );
340341
@@ -345,7 +346,7 @@ export default defineComponent({
345346 emit (' saved' , savedItem );
346347
347348 // 显示成功消息
348- alert (' 提示词已成功添加到提示词库 ' );
349+ alert (' 提示词已成功 ' + ( props . selectedPrompt ? ' 更新 ' : ' 添加 ' ) + ' 到提示词库 ' );
349350 } catch (error ) {
350351 console .error (' 保存提示词失败:' , error );
351352 errorMessage .value = ' 保存提示词失败,请重试' ;
@@ -377,8 +378,6 @@ export default defineComponent({
377378 subCategories ,
378379 canSaveToLibrary ,
379380 errorMessage ,
380- localCategories ,
381- localSubCategories ,
382381
383382 // 方法
384383 addNewCategory ,
0 commit comments