small fix

This commit is contained in:
rulingcom 2025-06-20 18:00:22 +08:00
parent b160624e4e
commit 004a2023a6
2 changed files with 65 additions and 50 deletions

View File

@ -16,14 +16,20 @@ export class JsmindSearch {
* @param {Function} searchAPI - 遠程搜尋 API 函式 (Remote search API function)
* @param {string} tableUID
*/
constructor(jm, searchAPI, tableUID) {
this.jm = jm
this.searchAPI = searchAPI
this.container = document.getElementById(jm.options.container)
this.suggestionBox = null
this.tableUID = tableUID
this.init()
}
constructor(jm, searchAPI, tableUID) {
this.jm = jm
this.searchAPI = searchAPI
this.container = document.getElementById(jm.options.container)
this.suggestionBox = null
this.tableUID = tableUID
// 新增記錄節點與事件 handler
this.currentNode = null
this._keydownHandler = null
this._inputHandler = null
this.init()
}
/**
* 初始化搜尋事件
@ -59,41 +65,49 @@ export class JsmindSearch {
* Start handling search
* @param {Object} node - 當前選中節點 (Selected node)
*/
handleSearch(node) {
const inputField = document.querySelector(`.${EDITOR_CLASS}`)
if (!inputField) return
handleSearch(node) {
const inputField = document.querySelector(`.${EDITOR_CLASS}`)
if (!inputField) return
// 確保不會重複綁定 input 事件
// Ensure input event is not bound multiple times
inputField.removeEventListener('input', this.onInput)
inputField.addEventListener('input', this.onInput.bind(this, node))
inputField.removeEventListener('keydown', this.onKeyDown)
inputField.addEventListener('keydown', this.onKeyDown.bind(this, node))
}
// 記住目前的 node
this.currentNode = node
// 清除之前的 handler
if (this._keydownHandler) inputField.removeEventListener('keydown', this._keydownHandler)
if (this._inputHandler) inputField.removeEventListener('input', this._inputHandler)
// 新綁定 handler
this._keydownHandler = this.onKeyDown.bind(this)
this._inputHandler = this.onInput.bind(this)
inputField.addEventListener('keydown', this._keydownHandler)
inputField.addEventListener('input', this._inputHandler)
}
/**
* 處理 Enter 鍵完成輸入
* Handle Enter key to finalize input
* @param {Object} node - 當前節點
* @param {KeyboardEvent} e - 鍵盤事件
*/
onKeyDown(node, e) {
if (e.key === 'Enter') {
e.preventDefault()
const input = e.target.value.trim()
if (input) {
// 更新節點文字
node.data.text = input
this.jm.end_edit()
this.jm.update_node(node.id, input)
onKeyDown(e) {
if (e.key === 'Enter') {
e.preventDefault()
// 隱藏 suggestion box避免未選建議但仍留下
if (this.suggestionBox) {
this.suggestionBox.style.display = 'none'
}
}
const input = e.target.value.trim()
const node = this.currentNode
if (input && node) {
node.data.text = input
this.jm.end_edit()
this.jm.update_node(node.id, input)
if (this.suggestionBox) {
this.suggestionBox.style.display = 'none'
}
this.currentNode = null // 清除參考
}
}
}
/**
* 處理使用者輸入
@ -101,18 +115,17 @@ export class JsmindSearch {
* @param {Object} node - 當前選中節點 (Selected node)
* @param {Event} e - 輸入事件 (Input event)
*/
async onInput(node, e) {
const query = e.target.value.trim()
if (!query) return
await new Promise(resolve => setTimeout(resolve, 500));
try {
const results = await this.searchAPI(query, this.tableUID)
this.showSuggestion(node, e.target, results)
} catch (error) {
// Search API error handling
console.error('搜尋 API 錯誤:', error)
}
async onInput(e) {
const query = e.target.value.trim()
if (!query) return
await new Promise((resolve) => setTimeout(resolve, 500))
try {
const results = await this.searchAPI(query, this.tableUID)
this.showSuggestion(this.currentNode, e.target, results)
} catch (error) {
console.error('搜尋 API 錯誤:', error)
}
}
/**
* 顯示搜尋建議框

View File

@ -111,14 +111,16 @@ wb.add_worksheet(name: "Structure") do |sheet|
when "file"
file_links = []
locale = I18n.locale.to_s
column.column_entry_files.desc(:sort_number).each do |entry_file|
next unless entry_file.choose_lang_display(locale)
file_links << (url + entry_file.get_link)
end
row << file_links.join("\r\n")
if !column.nil?
column.column_entry_files.desc(:sort_number).each do |entry_file|
next unless entry_file.choose_lang_display(locale)
file_links << (url + entry_file.get_link)
end
end
row << file_links.join(";")
end
end
row << entry.table_tags.pluck("title").map { |t| "#{t}" }.join(", ")
row << entry.table_tags.pluck("title").map { |t| "#{t}" }.join("; ")
row << entry.get_related_entries_uid
sheet.add_row row, style: wrap
end