|
| 1 | +--- |
| 2 | +description: Route QuickTools keys, text and commands to a plugin's own editor tab with acode.registerQuickToolsAdapter. |
| 3 | +--- |
| 4 | + |
| 5 | +# QuickTools Adapter <Badge type="tip" text="v1012+" /> |
| 6 | + |
| 7 | +QuickTools is the row of keys above the keyboard (`Ctrl`, `Tab`, arrows, brackets, undo, redo and so on). By default those keys only drive the code editor. A **QuickTools adapter** lets a [custom editor tab](../editor-components/editor-file#creating-a-custom-file-type) handle them instead, so a plugin's own editor (a spreadsheet, a canvas, a hex editor) works with QuickTools like the code editor does. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```js |
| 12 | +const dispose = acode.registerQuickToolsAdapter(tab, adapter); |
| 13 | +``` |
| 14 | + |
| 15 | +| Parameter | Type | Description | |
| 16 | +| :-------- | :--- | :---------- | |
| 17 | +| `tab` | `EditorFile` | The custom tab the adapter belongs to. | |
| 18 | +| `adapter` | `QuickToolsAdapter` | The handlers described below. | |
| 19 | + |
| 20 | +It returns a `dispose()` function that removes the adapter. The adapter is also removed automatically when the tab closes. |
| 21 | + |
| 22 | +Each tab can have only one adapter; registering a second one throws an error. |
| 23 | + |
| 24 | +## How It Works |
| 25 | + |
| 26 | +While the tab with an adapter is active: |
| 27 | + |
| 28 | +- **The adapter owns all QuickTools input for the tab.** Actions it doesn't handle are ignored. They never fall through to the code editor. |
| 29 | +- **QuickTools is shown while `getState().enabled` is `true`** and hidden otherwise. This replaces the tab's `hideQuickTools` option. |
| 30 | +- **Actions run one at a time, in order.** Each `execute()` call waits for the previous one to finish. |
| 31 | +- **Pending actions are cancelled** when the user switches tabs, when the adapter reports `enabled: false` or `busy: true`, or when a dialog or other overlay opens. The `signal` passed to `execute()` is aborted, and `cancel()` is called. |
| 32 | +- **Paste becomes text.** A paste command or `Ctrl` + `V` is read from the clipboard and delivered as a `text` action. |
| 33 | +- **Save and the Command Palette always run normally**, even with an adapter. To make **Save** work for your tab, handle the tab's [`save` event](../editor-components/editor-file#saving-a-custom-tab). |
| 34 | + |
| 35 | +## Adapter |
| 36 | + |
| 37 | +```ts |
| 38 | +interface QuickToolsAdapter { |
| 39 | + // Required |
| 40 | + getState(): { enabled: boolean; busy?: boolean }; |
| 41 | + subscribe(listener: () => void): () => void; |
| 42 | + canHandle(action: QuickToolsAction): boolean; |
| 43 | + execute(action: QuickToolsAction, options: { signal: AbortSignal }): void | Promise<void>; |
| 44 | + |
| 45 | + // Optional |
| 46 | + focus?(): void | Promise<void>; |
| 47 | + captureSelection?(): unknown | Promise<unknown>; |
| 48 | + restoreSelection?(snapshot: unknown): void | Promise<void>; |
| 49 | + cancel?(): void; |
| 50 | + onError?(error: unknown): void; |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +| Method | Description | |
| 55 | +| :----- | :---------- | |
| 56 | +| `getState()` | Current state. `enabled: false` hides QuickTools for the tab; `busy: true` pauses input while your editor is working. | |
| 57 | +| `subscribe(listener)` | Call `listener` whenever the state changes. Return a function that unsubscribes. | |
| 58 | +| `canHandle(action)` | Return `true` if the adapter can run this action right now. Other actions are dropped. **Undo** and **Redo** are shown disabled while it returns `false` for them. | |
| 59 | +| `execute(action, { signal })` | Perform the action. Check `signal.aborted` before any slow step. | |
| 60 | +| `focus()` | Give focus back to your editor after a QuickTools key is used. | |
| 61 | +| `captureSelection()` | Return a snapshot of the selection or caret before QuickTools takes focus. | |
| 62 | +| `restoreSelection(snapshot)` | Put that selection back before the next action runs. | |
| 63 | +| `cancel()` | Called when pending actions are cancelled. | |
| 64 | +| `onError(error)` | Called if `execute()`, `restoreSelection()` or `focus()` throws. | |
| 65 | + |
| 66 | +## Actions |
| 67 | + |
| 68 | +```ts |
| 69 | +type QuickToolsAction = |
| 70 | + | { type: "text"; text: string } |
| 71 | + | { |
| 72 | + type: "key"; |
| 73 | + key: string; // KeyboardEvent.key, for example "ArrowLeft", "Tab", "Escape", "a" |
| 74 | + shiftKey: boolean; |
| 75 | + ctrlKey: boolean; |
| 76 | + altKey: boolean; |
| 77 | + metaKey: boolean; |
| 78 | + } |
| 79 | + | { type: "command"; command: string } |
| 80 | + | { type: "modifier"; key: "shift" | "ctrl" | "alt" | "meta" }; |
| 81 | +``` |
| 82 | + |
| 83 | +| Type | Sent when | |
| 84 | +| :--- | :-------- | |
| 85 | +| `text` | A character key like `{`, `;` or `"` is tapped, or text is pasted. | |
| 86 | +| `key` | A key like **Tab**, **Esc** or an arrow is tapped, or the user types a key while `Ctrl`, `Shift`, `Alt` or `Meta` is active. | |
| 87 | +| `command` | A command button is tapped: `undo`, `redo`, `selectall`, `movelinesup`, `movelinesdown`, `copylinesup`, `copylinesdown` or `find` (the search button). | |
| 88 | +| `modifier` | Only checked with `canHandle()`. Return `true` to let the user turn on that modifier; Acode then sends the next typed key as a `key` action with the modifier set. | |
| 89 | + |
| 90 | +## Example |
| 91 | + |
| 92 | +A minimal custom tab with a `<textarea>` editor: |
| 93 | + |
| 94 | +```js |
| 95 | +const EditorFile = acode.require("EditorFile"); |
| 96 | + |
| 97 | +const textarea = document.createElement("textarea"); |
| 98 | +const tab = new EditorFile("Notes", { type: "custom", content: textarea }); |
| 99 | + |
| 100 | +const listeners = new Set(); |
| 101 | +let state = { enabled: true, busy: false }; |
| 102 | + |
| 103 | +function insert(text) { |
| 104 | + textarea.setRangeText(text, textarea.selectionStart, textarea.selectionEnd, "end"); |
| 105 | +} |
| 106 | + |
| 107 | +const dispose = acode.registerQuickToolsAdapter(tab, { |
| 108 | + getState: () => state, |
| 109 | + subscribe(listener) { |
| 110 | + listeners.add(listener); |
| 111 | + return () => listeners.delete(listener); |
| 112 | + }, |
| 113 | + canHandle(action) { |
| 114 | + if (action.type === "text") return true; |
| 115 | + if (action.type === "key") return ["ArrowLeft", "ArrowRight", "Tab"].includes(action.key); |
| 116 | + if (action.type === "command") return action.command === "selectall"; |
| 117 | + return false; |
| 118 | + }, |
| 119 | + execute(action, { signal }) { |
| 120 | + if (signal.aborted) return; |
| 121 | + const pos = textarea.selectionStart; |
| 122 | + if (action.type === "text") insert(action.text); |
| 123 | + else if (action.key === "Tab") insert("\t"); |
| 124 | + else if (action.key === "ArrowLeft") textarea.setSelectionRange(pos - 1, pos - 1); |
| 125 | + else if (action.key === "ArrowRight") textarea.setSelectionRange(pos + 1, pos + 1); |
| 126 | + else if (action.command === "selectall") textarea.select(); |
| 127 | + }, |
| 128 | + focus: () => textarea.focus(), |
| 129 | + captureSelection: () => [textarea.selectionStart, textarea.selectionEnd], |
| 130 | + restoreSelection: ([start, end]) => textarea.setSelectionRange(start, end), |
| 131 | +}); |
| 132 | + |
| 133 | +// Hide QuickTools while the tab is read-only, for example. |
| 134 | +function setEnabled(enabled) { |
| 135 | + state = { ...state, enabled }; |
| 136 | + for (const listener of listeners) listener(); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +Call `dispose()` in your plugin's unmount function if the tab can outlive the plugin. |
0 commit comments