From 656796d18d7a89f5f9426273bc08ae31dd0939ce Mon Sep 17 00:00:00 2001 From: JYC333 <22962980+JYC333@users.noreply.github.com> Date: Mon, 9 Mar 2026 00:53:12 +0000 Subject: [PATCH] refactor: migrate relation map --- .../src/services/attribute_autocomplete.ts | 72 ++----------------- .../type_widgets/relation_map/RelationMap.tsx | 3 +- 2 files changed, 6 insertions(+), 69 deletions(-) diff --git a/apps/client/src/services/attribute_autocomplete.ts b/apps/client/src/services/attribute_autocomplete.ts index d1f29adbd1..2910074d8f 100644 --- a/apps/client/src/services/attribute_autocomplete.ts +++ b/apps/client/src/services/attribute_autocomplete.ts @@ -11,29 +11,15 @@ interface NameItem extends BaseItem { name: string; } -/** New API: pass the input element + a container for the dropdown panel */ -interface NewInitOptions { +interface InitAttributeNameOptions { /** The element where the user types */ $el: JQuery; attributeType?: AttributeType | (() => AttributeType); open: boolean; /** Called when the user selects a value or the panel closes */ onValueChange?: (value: string) => void; - /** Use the new autocomplete-core library instead of old autocomplete.js */ - useNewLib: true; -} - -/** Old API: uses legacy autocomplete.js jQuery plugin */ -interface OldInitOptions { - $el: JQuery; - attributeType?: AttributeType | (() => AttributeType); - open: boolean; -} - -type InitAttributeNameOptions = NewInitOptions | OldInitOptions; - -function isNewApi(opts: InitAttributeNameOptions): opts is NewInitOptions { - return "useNewLib" in opts && opts.useNewLib === true; + /** (Deprecated) Kept for compatibility during migration, not used anymore */ + useNewLib?: boolean; } // --------------------------------------------------------------------------- @@ -110,7 +96,7 @@ function positionPanel(panelEl: HTMLElement, inputEl: HTMLElement): void { // Attribute name autocomplete — new (autocomplete-core, headless) // --------------------------------------------------------------------------- -function initAttributeNameNew({ $el, attributeType, open, onValueChange }: NewInitOptions) { +function initAttributeNameAutocomplete({ $el, attributeType, open, onValueChange }: InitAttributeNameOptions) { const inputEl = $el[0] as HTMLInputElement; // Already initialized — just open if requested @@ -253,57 +239,7 @@ function initAttributeNameNew({ $el, attributeType, open, onValueChange }: NewIn } } -// --------------------------------------------------------------------------- -// Attribute name autocomplete — legacy (old autocomplete.js) -// --------------------------------------------------------------------------- -function initAttributeNameLegacy({ $el, attributeType, open }: OldInitOptions) { - if (!$el.hasClass("aa-input")) { - $el.autocomplete( - { - appendTo: document.querySelector("body"), - hint: false, - openOnFocus: true, - minLength: 0, - tabAutocomplete: false - }, - [ - { - displayKey: "name", - cache: false, - source: async (term, cb) => { - const type = typeof attributeType === "function" ? attributeType() : attributeType; - const names = await server.get(`attribute-names/?type=${type}&query=${encodeURIComponent(term)}`); - const result = names.map((name) => ({ name })); - cb(result); - } - } - ] - ); - - $el.on("autocomplete:opened", () => { - if ($el.attr("readonly")) { - $el.autocomplete("close"); - } - }); - } - - if (open) { - $el.autocomplete("open"); - } -} - -// --------------------------------------------------------------------------- -// Public entry point -// --------------------------------------------------------------------------- - -function initAttributeNameAutocomplete(opts: InitAttributeNameOptions) { - if (isNewApi(opts)) { - initAttributeNameNew(opts); - } else { - initAttributeNameLegacy(opts); - } -} // --------------------------------------------------------------------------- // Label value autocomplete (still using old autocomplete.js) diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx index 867fd44d39..eca3c4218d 100644 --- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx +++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx @@ -432,7 +432,8 @@ function useRelationCreation({ mapApiRef, jsPlumbApiRef }: { mapApiRef: RefObjec attribute_autocomplete.initAttributeNameAutocomplete({ $el: $answer, attributeType: "relation", - open: true + open: true, + useNewLib: true }); } });