Skip to content

Commit 1e503b0

Browse files
committed
refactor: clean up old autocomplete implementation
1 parent b886a64 commit 1e503b0

4 files changed

Lines changed: 14 additions & 26 deletions

File tree

.agents/migration_plan_autocomplete.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,22 @@
4545

4646
### Step 1: 迁移属性名称自动补全 ✅ 完成
4747
**文件变更:**
48-
- `apps/client/src/services/attribute_autocomplete.ts``initAttributeNameAutocomplete()` 支持双 API:传 `container` 走新库,传 `$el` 走旧库
49-
- `apps/client/src/widgets/attribute_widgets/attribute_detail.ts`属性名称输入框从 `<input>` 改为 `<div>` 容器,值读写改用 `getInputValue()/setInputValue()`
50-
- `apps/client/src/stylesheets/style.css`添加新库 CSS(隐藏搜索图标、样式对齐)
51-
- `RelationMap.tsx`**不变**,继续用旧 `$el` API
48+
- `apps/client/src/services/attribute_autocomplete.ts``initAttributeNameAutocomplete()` 完全使用 **Headless API (`@algolia/autocomplete-core`)** 重写,移除遗留的 jQuery autocomplete 调用。
49+
- `apps/client/src/widgets/attribute_widgets/attribute_detail.ts`维持原有 `<input>` 模型不变,仅需增加 `onValueChange` 处理回调。
50+
- `apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx`维持原有回调逻辑,新旧无感替换。
51+
- `apps/client/src/stylesheets/style.css`增加自定义 Headless 渲染面板样式 (`.aa-core-panel``.aa-core-list` 等)。
5252

53-
**说明**
54-
`initAttributeNameAutocomplete()` 同时支持新旧两种调用方式(overloaded interface),让消费者可以逐步迁移:
55-
- `attribute_detail.ts``{ container }` → 使用 `autocomplete-js`
56-
- `RelationMap.tsx``{ $el }` → 使用旧 `autocomplete.js`
57-
- RelationMap 的迁移推迟到后续步骤(因为它的 prompt dialog 由 React 管理 input)
53+
**架构说明**
54+
由于 Trilium 依赖同一页面同时运行多个 autocomplete 生命周期(边栏属性列表,底部编辑器等),原生 `@algolia/autocomplete-js` 会因为单例 DOM 冲突强行报错 "doesn't support multiple instances running at the same time"。
55+
解决方案是退化使用纯状态机的 `@algolia/autocomplete-core`,自己进行 DOM 劫持与面板渲染。
56+
- `requestAnimationFrame`:针对下拉层自动跟踪光标位置,适配面板的高频大小变化
57+
- 事件阻断:拦截了选择时候的 `Enter` 返回键事件气泡,避免误触外层 Dialog 销毁。
5858

5959
**验证方式:**
60-
- ⬜ 打开一个笔记 → 点击属性面板 → 点击属性名称输入框 → 应能看到自动补全的属性名称列表
61-
- ⬜ 选择一个名称后,值应正确填入
62-
- ⬜ 关系图创建关系时,关系名输入框的自动补全仍正常(旧库路径)
60+
- ✅ 打开一个笔记 → 点击属性面板弹出 "Label detail" → 输入属性名称时正常显示下拉自动补全框
61+
- ✅ 放大/缩小/变形整个面板,下拉菜单粘连位置准确
62+
- ✅ 键盘上下方向键可高亮,按 Enter 可选中当前项填充,且对话框不关闭
63+
- ✅ 关系图 (Relation map) 创建关系时,关系名输入框的自动补全同样工作正常
6364

6465
---
6566

apps/client/src/services/attribute_autocomplete.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ interface InitAttributeNameOptions {
1818
open: boolean;
1919
/** Called when the user selects a value or the panel closes */
2020
onValueChange?: (value: string) => void;
21-
/** (Deprecated) Kept for compatibility during migration, not used anymore */
22-
useNewLib?: boolean;
2321
}
2422

2523
// ---------------------------------------------------------------------------
@@ -34,15 +32,6 @@ interface ManagedInstance {
3432

3533
const instanceMap = new WeakMap<HTMLElement, ManagedInstance>();
3634

37-
function destroyInstance(el: HTMLElement): void {
38-
const inst = instanceMap.get(el);
39-
if (inst) {
40-
inst.cleanup();
41-
inst.panelEl.remove();
42-
instanceMap.delete(el);
43-
}
44-
}
45-
4635
// ---------------------------------------------------------------------------
4736
// Dropdown panel DOM helpers
4837
// ---------------------------------------------------------------------------

apps/client/src/widgets/attribute_widgets/attribute_detail.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
379379
$el: this.$inputName,
380380
attributeType: () => (["relation", "relation-definition"].includes(this.attrType || "") ? "relation" : "label"),
381381
open: true,
382-
useNewLib: true,
383382
onValueChange: () => this.userEditedAttribute(),
384383
});
385384
});

apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ function useRelationCreation({ mapApiRef, jsPlumbApiRef }: { mapApiRef: RefObjec
432432
attribute_autocomplete.initAttributeNameAutocomplete({
433433
$el: $answer,
434434
attributeType: "relation",
435-
open: true,
436-
useNewLib: true
435+
open: true
437436
});
438437
}
439438
});

0 commit comments

Comments
 (0)