@@ -158,6 +158,10 @@ export default defineComponent({
158158 const isSaving = ref (false );
159159 const errorMessage = ref (' ' );
160160
161+ // 本地新增的分类 (尚未保存到服务器的)
162+ const localCategories = ref <string []>([]);
163+ const localSubCategories = ref <{[category : string ]: string []}>({});
164+
161165 // 新提示词数据
162166 const newPrompt = ref <NewPromptData >({
163167 text: ' ' ,
@@ -169,20 +173,34 @@ export default defineComponent({
169173
170174 // 一级分类列表
171175 const categories = computed (() => {
172- const categorySet = new Set (props .promptLibraryData .map (item => item .category ));
176+ // 获取已有分类
177+ const categorySet = new Set ([
178+ ... props .promptLibraryData .map (item => item .category ),
179+ ... localCategories .value
180+ ]);
181+
173182 return Array .from (categorySet ).sort ();
174183 });
175184
176185 // 二级分类列表 (根据选择的一级分类筛选)
177186 const subCategories = computed (() => {
178- let items = props . promptLibraryData ;
187+ let subCategorySet = new Set < string >() ;
179188
180- // 如果在模态框中选择了一级分类 ,则按该分类筛选
189+ // 如果选择了一级分类 ,则按该分类筛选
181190 if (newPrompt .value .category ) {
182- items = items .filter (item => item .category === newPrompt .value .category );
191+ // 从现有数据中获取二级分类
192+ const filteredItems = props .promptLibraryData .filter (
193+ item => item .category === newPrompt .value .category
194+ );
195+ filteredItems .forEach (item => {
196+ if (item .subCategory ) subCategorySet .add (item .subCategory );
197+ });
198+
199+ // 加入本地新增的二级分类
200+ const localSubs = localSubCategories .value [newPrompt .value .category ] || [];
201+ localSubs .forEach (sub => subCategorySet .add (sub ));
183202 }
184203
185- const subCategorySet = new Set (items .map (item => item .subCategory ));
186204 return Array .from (subCategorySet ).sort ();
187205 });
188206
@@ -210,6 +228,8 @@ export default defineComponent({
210228 newCategory .value = ' ' ;
211229 newSubCategory .value = ' ' ;
212230 errorMessage .value = ' ' ;
231+
232+ // 注意:不重置localCategories和localSubCategories,因为需要保留用户创建的分类
213233 };
214234
215235 // 添加新一级分类
@@ -222,6 +242,9 @@ export default defineComponent({
222242 return ;
223243 }
224244
245+ // 添加到本地分类列表
246+ localCategories .value .push (newCategory .value .trim ());
247+
225248 // 设置新分类
226249 newPrompt .value .category = newCategory .value .trim ();
227250 showAddCategory .value = false ;
@@ -230,14 +253,22 @@ export default defineComponent({
230253
231254 // 添加新二级分类
232255 const addNewSubCategory = () => {
233- if (newSubCategory .value .trim () === ' ' ) return ;
256+ if (newSubCategory .value .trim () === ' ' || ! newPrompt . value . category ) return ;
234257
235258 // 检查是否已存在
236259 if (subCategories .value .includes (newSubCategory .value .trim ())) {
237260 alert (' 该二级分类已存在' );
238261 return ;
239262 }
240263
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+
241272 // 设置新二级分类
242273 newPrompt .value .subCategory = newSubCategory .value .trim ();
243274 showAddSubCategory .value = false ;
@@ -346,6 +377,8 @@ export default defineComponent({
346377 subCategories ,
347378 canSaveToLibrary ,
348379 errorMessage ,
380+ localCategories ,
381+ localSubCategories ,
349382
350383 // 方法
351384 addNewCategory ,
0 commit comments