|
| 1 | +# File Extensions |
| 2 | + |
| 3 | +Plugin architecture for adding custom actions to the file browser. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Extensions are placed in `src/app/extensions/` and managed via `extensions.config.ts`. |
| 8 | + |
| 9 | +## FileActionExtension |
| 10 | + |
| 11 | +```typescript |
| 12 | +interface FileActionExtension { |
| 13 | + id: string; |
| 14 | + label: string; |
| 15 | + icon: string; |
| 16 | + command: (ctx: FileActionContext) => void; |
| 17 | + parentId?: string; |
| 18 | + position?: 'start' | 'end' | number; |
| 19 | + visible?: (ctx: FileActionContext) => boolean; |
| 20 | + disabled?: (ctx: FileActionContext) => boolean; |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +### parentId |
| 25 | + |
| 26 | +If `parentId` is set, the extension appears only in that submenu (e.g., `'share'`). |
| 27 | +If not set, the extension appears in both menu and toolbar. |
| 28 | + |
| 29 | +### FileActionContext |
| 30 | + |
| 31 | +```typescript |
| 32 | +interface FileActionContext { |
| 33 | + target: FileModel | FileDetailsModel | FileFolderModel; |
| 34 | + location: 'file-list' | 'file-detail'; |
| 35 | + isViewOnly: boolean; |
| 36 | + canWrite: boolean; |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Example: Submenu Extension |
| 41 | + |
| 42 | +```typescript |
| 43 | +export function copyLinksExtensionFactory(): FileActionExtension[] { |
| 44 | + return [ |
| 45 | + { |
| 46 | + id: 'copy-link', |
| 47 | + label: 'Copy Link', |
| 48 | + icon: 'fas fa-link', |
| 49 | + command: (ctx) => { |
| 50 | + navigator.clipboard.writeText(ctx.target.links.html); |
| 51 | + }, |
| 52 | + parentId: 'share', // appears in Share submenu only |
| 53 | + position: 'end', |
| 54 | + visible: (ctx) => ctx.target.kind === 'file', |
| 55 | + disabled: (ctx) => ctx.isViewOnly, |
| 56 | + }, |
| 57 | + ]; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +> **Note:** Context menus honor `position` exactly (for example, `'start'` inserts before built-in items). Top-level toolbars append extensions after the core OSF buttons but still keep extension-to-extension ordering based on `position`. Design UX with that constraint in mind. |
| 62 | +
|
| 63 | +## Example: Menu + Toolbar Extension |
| 64 | + |
| 65 | +```typescript |
| 66 | +export function createFileExtensionFactory(config: Config): FileActionExtension[] { |
| 67 | + return [ |
| 68 | + { |
| 69 | + id: 'create-file', |
| 70 | + label: 'Create File', |
| 71 | + icon: 'fas fa-file-plus', |
| 72 | + command: (ctx) => { |
| 73 | + window.open(`${config.editorUrl}?path=${ctx.target.path}`, '_blank'); |
| 74 | + }, |
| 75 | + // no parentId: appears in both menu and toolbar |
| 76 | + position: 'end', |
| 77 | + visible: (ctx) => |
| 78 | + ctx.location === 'file-list' && |
| 79 | + ctx.target.kind === 'folder' && |
| 80 | + !ctx.isViewOnly && |
| 81 | + ctx.canWrite, |
| 82 | + }, |
| 83 | + ]; |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Configuration |
| 88 | + |
| 89 | +The extension configuration is managed via `extensions.config.ts`, which is generated at build time. |
| 90 | + |
| 91 | +### File Structure |
| 92 | + |
| 93 | +``` |
| 94 | +src/app/ |
| 95 | + extensions.config.ts # Generated (not in git) |
| 96 | + extensions.config.default.ts # Default config (in git) |
| 97 | +``` |
| 98 | + |
| 99 | +### Build-time Configuration |
| 100 | + |
| 101 | +By default, `extensions.config.default.ts` is used. To use a custom configuration: |
| 102 | + |
| 103 | +```bash |
| 104 | +EXTENSIONS_CONFIG=/path/to/my-extensions.config.ts npm run build |
| 105 | +``` |
| 106 | + |
| 107 | +The `scripts/setup-extensions.js` helper runs before `npm run build`, `npm start`, and `npm test` (see the `prebuild` / `prestart` / `pretest` npm scripts) and handles this: |
| 108 | +- If `EXTENSIONS_CONFIG` is set, copies that file |
| 109 | +- Otherwise, copies `extensions.config.default.ts` |
| 110 | + |
| 111 | +## Registration |
| 112 | + |
| 113 | +### extensions.config.ts |
| 114 | + |
| 115 | +```typescript |
| 116 | +export const extensionConfig: ExtensionConfig[] = [ |
| 117 | + { |
| 118 | + load: () => import('./extensions/copy-links'), |
| 119 | + factory: 'copyLinksExtensionFactory', |
| 120 | + enabled: true, |
| 121 | + }, |
| 122 | + { |
| 123 | + load: () => import('./extensions/onlyoffice'), |
| 124 | + factory: 'editByOnlyOfficeExtensionFactory', |
| 125 | + enabled: true, |
| 126 | + config: { editorUrl: 'https://...' }, |
| 127 | + }, |
| 128 | + { |
| 129 | + load: () => import('./extensions/onlyoffice'), |
| 130 | + factory: 'createFileExtensionFactory', |
| 131 | + enabled: true, |
| 132 | + config: { editorUrl: 'https://...' }, |
| 133 | + }, |
| 134 | +]; |
| 135 | +``` |
| 136 | + |
0 commit comments