File's Link Picker

File’s Link Picker

Alt-f here we go

Realization

first def a global func getFileLinks()

function getFileLinks()
  return query[[
    from index.tag "link"
    where _.toFile
    select{
      ref = _.ref,
      snippet = _.snippet,
      page = _.page,
      pos = _.pos,
    }
    order by _.page, _.pos
  ]]
end

then create the File Link Picker

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: File Link Picker",
  key = "Alt-f",
  priority = 1,
  run = function()
    local tables = getFileLinks()
    if not tables or #tables == 0 then
      editor.flashNotification("No File Links found.")
      return
    end

    local items = {}
    for _, r in ipairs(tables) do
      table.insert(items, {
        name = r.snippet,
        description = string.format("%s @ %d", r.page, r.pos),
        ref = r.ref,
        page = r.page,
        pos = r.pos
      })
    end
    
    local sel = editor.filterBox("🔍 Select", items, "Choose a File Link...", "a File Link to GoTo")
    if not sel then return end

    if not navigateToPos(sel.ref, sel.pos) then
      editor.flashNotification("Failed to navigate to selected File Link.")
    end
  end
}

My attempt to locate online images like ![](https://aituyaa.com/assets/Pasted%20image%2020250110153326.png) in my SB instance ended up producing this ![[abcdefg.suffix]] along with [[abcdefg.suffix]] picker unintentionally.

My original goal was an online-image picker, but such images are not regarded as indexable objects.

This suggests we may convert these !()[abcdefg.suffix]s into objects, or perhaps objectify everything (extending the built-in tags list)? and may further require some real SQL support.

For now, one expedient method is to add a label/anchor (or any built-in tag) to/alongside each online image link !()[abcdefg.suffix] and rely on the Anchor/Label Picker to query them, though this solution feels inelegant despite its generality.

this ![[name.suffix]] Picker is slightly different from the built-in Document Picker

cleaned up

command.define {
  name = "Navigate: File Link Picker",
  key = "Alt-f",
  priority = 1,
  run = function()
    local FileLinks = getFileLinks()
    if not FileLinks or #FileLinks == 0 then
      editor.flashNotification("No File Links found.")
      return
    end
    
    local sel = editor.filterBox("🔍 Select", FileLinks, "Choose a File Link...", "a File Link to GoTo")
    if not sel then return end
    editor.navigate(sel.ref)
    editor.invokeCommand("Navigate: Center Cursor")
  end
}

function getFileLinks()
  return query[[
    from index.tag "link"
    where _.toFile
    select{
      name = _.snippet,
      description = string.format("%s @ %d", _.page, _.pos),
      ref = _.ref,
    }
    order by _.page, _.pos
  ]]
end