silverbullet/website/API/editor.md

193 lines
3.4 KiB
Markdown
Raw Normal View History

The Editor API provides functions for interacting with the editor interface.
2025-01-17 03:41:52 +08:00
### editor.getCurrentPage()
2025-01-17 03:41:52 +08:00
Returns the name of the page currently open in the editor.
Example: ${editor.getCurrentPage()}
2025-01-17 03:41:52 +08:00
### editor.getCurrentPageMeta()
2025-01-17 03:41:52 +08:00
Returns the meta data of the page currently open in the editor.
Example:
${editor.getCurrentPageMeta()}
2025-01-17 03:41:52 +08:00
### editor.getText()
2025-01-17 03:41:52 +08:00
Returns the full text of the currently open page.
Example:
```lua
local text = editor.getText()
2025-01-17 03:41:52 +08:00
print("Document length: " .. #text)
```
### editor.setText(text, isolateHistory)
2025-01-17 03:41:52 +08:00
Updates the editor text while preserving cursor location.
Example:
```lua
local text = editor.getText()
editor.setText(text:upper(), false) -- Convert to uppercase
2025-01-17 03:41:52 +08:00
```
### editor.insertAtPos(text, pos)
2025-01-17 03:41:52 +08:00
Insert text at the specified position.
Example:
```lua
editor.insertAtPos("Hello!", 0) -- Insert at beginning
2025-01-17 03:41:52 +08:00
```
### editor.replaceRange(from, to, text)
2025-01-17 03:41:52 +08:00
Replace text in the specified range.
Example:
```lua
editor.replaceRange(0, 5, "New text")
2025-01-17 03:41:52 +08:00
```
### editor.insertAtCursor(text, scrollIntoView?)
2025-01-17 03:41:52 +08:00
Insert text at the current cursor position.
Example:
```lua
editor.insertAtCursor("Inserted at cursor")
2025-01-17 03:41:52 +08:00
```
### editor.getCursor()
2025-01-17 03:41:52 +08:00
Returns the cursor position as character offset.
Example:
```lua
local pos = editor.getCursor()
2025-01-17 03:41:52 +08:00
print("Cursor at position: " .. pos)
```
### editor.getSelection()
2025-01-17 03:41:52 +08:00
Returns the current selection range.
Example:
```lua
local sel = editor.getSelection()
2025-01-17 03:41:52 +08:00
print("Selection from " .. sel.from .. " to " .. sel.to)
```
### editor.setSelection(from, to)
2025-01-17 03:41:52 +08:00
Sets the current selection range.
Example:
```lua
editor.setSelection(0, 10) -- Select first 10 characters
2025-01-17 03:41:52 +08:00
```
### editor.moveCursor(pos, center)
2025-01-17 03:41:52 +08:00
Move the cursor to a specific position.
Example:
```lua
editor.moveCursor(0, true) -- Move to start and center
2025-01-17 03:41:52 +08:00
```
### editor.moveCursorToLine(line, column, center)
2025-01-17 03:41:52 +08:00
Move the cursor to a specific line and column.
Example:
```lua
editor.moveCursorToLine(1, 1, true) -- Move to start of first line
2025-01-17 03:41:52 +08:00
```
### editor.openPageNavigator(mode)
2025-01-17 03:41:52 +08:00
Opens the page navigator.
Example:
```lua
editor.openPageNavigator("page")
2025-01-17 03:41:52 +08:00
```
### editor.openCommandPalette()
2025-01-17 03:41:52 +08:00
Opens the command palette.
Example:
```lua
editor.openCommandPalette()
2025-01-17 03:41:52 +08:00
```
### editor.showPanel(id, mode, html, script)
2025-01-17 03:41:52 +08:00
Shows a panel in the editor.
Example:
```lua
editor.showPanel("rhs", 1, "<h1>Hello</h1>")
2025-01-17 03:41:52 +08:00
```
### editor.hidePanel(id)
2025-01-17 03:41:52 +08:00
Hides a panel in the editor.
Example:
```lua
editor.hidePanel("rhs")
2025-01-17 03:41:52 +08:00
```
### editor.flashNotification(message, type)
2025-01-17 03:41:52 +08:00
Shows a flash notification.
Example:
```lua
editor.flashNotification("Operation completed", "info")
2025-01-17 03:41:52 +08:00
```
### editor.downloadFile(filename, dataUrl)
2025-01-17 03:41:52 +08:00
Triggers a file download in the browser.
Example:
```lua
editor.downloadFile("test.txt", "data:text/plain;base64,SGVsbG8=")
2025-01-17 03:41:52 +08:00
```
### editor.uploadFile(accept, capture)
2025-01-17 03:41:52 +08:00
Opens a file upload dialog.
Example:
```lua
local file = editor.uploadFile(".txt", nil)
2025-01-17 03:41:52 +08:00
print("Uploaded: " .. file.name)
```
### editor.copyToClipboard(data)
2025-01-17 03:41:52 +08:00
Copies data to the clipboard.
Example:
```lua
editor.copyToClipboard("Copied text")
2025-01-17 03:41:52 +08:00
```
### editor.toggleFold()
2025-01-17 03:41:52 +08:00
Toggles code folding at the current position.
Example:
```lua
editor.toggleFold()
2025-01-17 03:41:52 +08:00
```
### editor.foldAll()
2025-01-17 03:41:52 +08:00
Folds all foldable regions.
Example:
```lua
editor.foldAll()
2025-01-17 03:41:52 +08:00
```
### editor.unfoldAll()
2025-01-17 03:41:52 +08:00
Unfolds all folded regions.
Example:
```lua
editor.unfoldAll()
2025-01-17 03:41:52 +08:00
```
### editor.openSearchPanel()
2025-01-17 03:41:52 +08:00
Opens the editor's search panel.
Example:
```lua
editor.openSearchPanel()