author: Chenzhu-Xie name: Library/xczphysics/CONFIG/Picker/Table tags: meta/library
table 拿不到 snippet:
link 只能拿到含有 table 中某一含有 link 的 行 的 snippet
[ ] 或许 不采用 index.tag 中的 table 来获取 table
${query[
from index.tag "link"
where page == _CTX.currentPage.name
](
from index.tag "link"
where page == _CTX.currentPage.name
)} 放在 Language/Input Method/冰雪清韵 发现...
- table 不属于 link,所以... 无法使用 link 的 snippet
#💡 似乎 Picker 可以不只是 1 维的 List,也可以是 2 维的表: 比如:表的每一行 不作为一个 snippet 整体,塞进 picker 的一个 sel 而是:表的每一行 按其原有的 列单元格,作为 picker 的一个 selection 这样一来,就可以像 CONFIG/Widget/Journal/Calendar#Example:|日历 那样 ←→↑↓ 地 选择 棋盘方格 i,j 了
command.define {
name = "Navigate: Table Picker",
key = "Ctrl-Shift-t",
priority = 1,
run = function()
local tables = getTables()
if not tables or #tables == 0 then
editor.flashNotification("No tables found.")
return
end
local sel = editor.filterBox("🤏 Pick", tables, "Select a Table...", "🗓️ Table position")
if not sel then return end
editor.navigate(sel.ref)
editor.invokeCommand("Navigate: Center Cursor")
end
}
function navigateToPos(ref, pos)
if ref then
editor.navigate(ref)
if pos then
editor.moveCursor(tonumber(pos), true)
end
return true
end
return false
end
command.define {
name = "Navigate: Table Picker",
key = "Ctrl-Shift-t",
priority = 1,
run = function()
local tables = getTables()
if not tables or #tables == 0 then
editor.flashNotification("No tables found.")
return
end
local items = {}
for _, r in ipairs(tables) do
table.insert(items, {
name = string.format("%s @ %d", r.page, r.pos),
-- description = string.format("%s @ %d", r.page, r.pos),
ref = r.ref,
page = r.page,
pos = r.pos
})
end
local sel = editor.filterBox("Jump to", items, "Select a Table...", "Page @ Pos where the Table locates")
if not sel then return end
if not navigateToPos(sel.ref, sel.pos) then
editor.flashNotification("Failed to navigate to selected table.")
end
end
}
${query[from index.tag "table"
order by _.tableref
](from index.tag "table"
order by _.tableref
)}
${getTables()}
function getTables()
local rows = query[
from index.tag "table"
select {
name = string.format("%s @ %d", _.page, _.pos),
ref = _.ref,
tableref = _.tableref,
}
order by _.page, _.pos
](
from index.tag "table"
select {
name = string.format("%s @ %d", _.page, _.pos),
ref = _.ref,
tableref = _.tableref,
}
order by _.page, _.pos
)
local out, seen = {}, {}
for _, r in ipairs(rows) do
local key = r.tableref
if not seen[key] then
seen[key] = true
table.insert(out, r)
end
end
return out
end
function getTables()
local rows = query[
from index.tag "table"
select {
ref = _.ref,
tableref = _.tableref,
page = _.page,
pos = _.pos,
}
order by _.page, _.pos
](
from index.tag "table"
select {
ref = _.ref,
tableref = _.tableref,
page = _.page,
pos = _.pos,
}
order by _.page, _.pos
)
local out, seen = {}, {}
for _, r in ipairs(rows) do
local key = r.tableref
if not seen[key] then
seen[key] = true
table.insert(out, r)
end
end
return out
end
function getTables()
local rows = query[
from index.tag "table"
select {
ref = _.ref,
tableref = _.tableref,
page = _.page,
pos = _.pos,
}
order by _.tableref, _.pos
](
from index.tag "table"
select {
ref = _.ref,
tableref = _.tableref,
page = _.page,
pos = _.pos,
}
order by _.tableref, _.pos
)
local bestByRef = {}
for _, r in ipairs(rows) do
local key = r.tableref
local cur = bestByRef[key]
if not cur or (r.pos or math.huge) < (cur.pos or math.huge) then
bestByRef[key] = r
end
end
local out = {}
for _, r in pairs(bestByRef) do
table.insert(out, r)
end
return query[
from out
order by _.page, _.pos
](
from out
order by _.page, _.pos
)
end