|
| 1 | +# File Extensions |
| 2 | + |
| 3 | +Plugin architecture for adding custom actions to the file browser. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Extensions can be placed internally (`src/app/extensions/`) or externally (npm package). Both are managed via `extensions.config.ts`. |
| 8 | + |
| 9 | +| Location | Example | |
| 10 | +|----------|---------| |
| 11 | +| `src/app/extensions/` | Copy Links | |
| 12 | +| npm package | [osf-extension-onlyoffice](https://github.com/yacchin1205/osf-extension-onlyoffice) (experimental) | |
| 13 | + |
| 14 | +> **Note:** `osf-extension-onlyoffice` is currently experimental and used for testing the plugin architecture. |
| 15 | +
|
| 16 | +## FileActionExtension |
| 17 | + |
| 18 | +```typescript |
| 19 | +interface FileActionExtension { |
| 20 | + id: string; |
| 21 | + label: string; |
| 22 | + icon: string; |
| 23 | + command: (ctx: FileActionContext) => void; |
| 24 | + parentId?: string; |
| 25 | + position?: 'start' | 'end' | number; |
| 26 | + visible?: (ctx: FileActionContext) => boolean; |
| 27 | + disabled?: (ctx: FileActionContext) => boolean; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### parentId |
| 32 | + |
| 33 | +If `parentId` is set, the extension appears only in that submenu (e.g., `'share'`). |
| 34 | +If not set, the extension appears in both menu and toolbar. |
| 35 | + |
| 36 | +### FileActionContext |
| 37 | + |
| 38 | +```typescript |
| 39 | +interface FileActionContext { |
| 40 | + target: FileModel | FileDetailsModel | FileFolderModel; |
| 41 | + location: 'file-list' | 'file-detail'; |
| 42 | + isViewOnly: boolean; |
| 43 | + canWrite: boolean; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Example: Submenu Extension |
| 48 | + |
| 49 | +```typescript |
| 50 | +export function copyLinksExtensionFactory(): FileActionExtension[] { |
| 51 | + return [ |
| 52 | + { |
| 53 | + id: 'copy-link', |
| 54 | + label: 'Copy Link', |
| 55 | + icon: 'fas fa-link', |
| 56 | + command: (ctx) => { |
| 57 | + navigator.clipboard.writeText(ctx.target.links.html); |
| 58 | + }, |
| 59 | + parentId: 'share', // appears in Share submenu only |
| 60 | + position: 'end', |
| 61 | + visible: (ctx) => ctx.target.kind === 'file', |
| 62 | + disabled: (ctx) => ctx.isViewOnly, |
| 63 | + }, |
| 64 | + ]; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +> **Heads up:** 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. |
| 69 | +
|
| 70 | +## Example: Menu + Toolbar Extension |
| 71 | + |
| 72 | +```typescript |
| 73 | +export function createFileExtensionFactory(config: Config): FileActionExtension[] { |
| 74 | + return [ |
| 75 | + { |
| 76 | + id: 'create-file', |
| 77 | + label: 'Create File', |
| 78 | + icon: 'fas fa-file-plus', |
| 79 | + command: (ctx) => { |
| 80 | + window.open(`${config.editorUrl}?path=${ctx.target.path}`, '_blank'); |
| 81 | + }, |
| 82 | + // no parentId: appears in both menu and toolbar |
| 83 | + position: 'end', |
| 84 | + visible: (ctx) => |
| 85 | + ctx.location === 'file-list' && |
| 86 | + ctx.target.kind === 'folder' && |
| 87 | + !ctx.isViewOnly && |
| 88 | + ctx.canWrite, |
| 89 | + }, |
| 90 | + ]; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Configuration |
| 95 | + |
| 96 | +The extension configuration is managed via `extensions.config.ts`, which is generated at build time. |
| 97 | + |
| 98 | +### File Structure |
| 99 | + |
| 100 | +``` |
| 101 | +src/app/ |
| 102 | + extensions.config.ts # Generated (not in git) |
| 103 | + extensions.config.default.ts # Default config (in git) |
| 104 | +``` |
| 105 | + |
| 106 | +### Build-time Configuration |
| 107 | + |
| 108 | +By default, `extensions.config.default.ts` is used. To use a custom configuration: |
| 109 | + |
| 110 | +```bash |
| 111 | +EXTENSIONS_CONFIG=/path/to/my-extensions.config.ts npm run build |
| 112 | +``` |
| 113 | + |
| 114 | +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: |
| 115 | +- If `EXTENSIONS_CONFIG` is set, copies that file |
| 116 | +- Otherwise, copies `extensions.config.default.ts` |
| 117 | + |
| 118 | +## Registration |
| 119 | + |
| 120 | +### extensions.config.ts |
| 121 | + |
| 122 | +```typescript |
| 123 | +export const extensionConfig: ExtensionConfig[] = [ |
| 124 | + // Internal |
| 125 | + { |
| 126 | + load: () => import('./extensions/copy-links'), |
| 127 | + factory: 'copyLinksExtensionFactory', |
| 128 | + enabled: true, |
| 129 | + }, |
| 130 | + // External |
| 131 | + { |
| 132 | + load: () => import('@nii/osf-extension-onlyoffice'), |
| 133 | + factory: 'editWithOnlyOfficeExtensionFactory', |
| 134 | + enabled: true, |
| 135 | + config: { editorUrl: 'https://...' }, |
| 136 | + }, |
| 137 | + { |
| 138 | + load: () => import('@nii/osf-extension-onlyoffice'), |
| 139 | + factory: 'createFileExtensionFactory', |
| 140 | + enabled: true, |
| 141 | + config: { editorUrl: 'https://...' }, |
| 142 | + }, |
| 143 | +]; |
| 144 | +``` |
| 145 | + |
| 146 | +## Creating an External Plugin |
| 147 | + |
| 148 | +``` |
| 149 | +my-extension/ |
| 150 | +├── package.json |
| 151 | +├── tsconfig.json |
| 152 | +└── src/ |
| 153 | + ├── index.ts # re-export |
| 154 | + ├── types.ts # type definitions |
| 155 | + └── my-feature.ts # factory function |
| 156 | +``` |
| 157 | + |
| 158 | +### types.ts |
| 159 | + |
| 160 | +```typescript |
| 161 | +export interface FileActionContext { |
| 162 | + target: { id: string; name: string; kind: string; path?: string; links: { html: string } }; |
| 163 | + location: 'file-list' | 'file-detail'; |
| 164 | + isViewOnly: boolean; |
| 165 | + canWrite: boolean; |
| 166 | +} |
| 167 | + |
| 168 | +export interface FileActionExtension { |
| 169 | + id: string; |
| 170 | + label: string; |
| 171 | + icon: string; |
| 172 | + command: (ctx: FileActionContext) => void; |
| 173 | + parentId?: string; |
| 174 | + position?: 'start' | 'end' | number; |
| 175 | + visible?: (ctx: FileActionContext) => boolean; |
| 176 | + disabled?: (ctx: FileActionContext) => boolean; |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +The `config` parameter in factory functions is passed from `extensions.config.ts`. |
| 181 | + |
| 182 | +## Developing External Plugins with npm link |
| 183 | + |
| 184 | +To test an external plugin locally without publishing to npm: |
| 185 | + |
| 186 | +```bash |
| 187 | +# 1. In the extension directory, build and create a global link |
| 188 | +cd ../osf-extension-onlyoffice |
| 189 | +npm run build |
| 190 | +npm link |
| 191 | + |
| 192 | +# 2. In angular-osf, link the package |
| 193 | +cd ../angular-osf |
| 194 | +npm link @nii/osf-extension-onlyoffice |
| 195 | + |
| 196 | +# 3. Add to extensions.config.ts and start |
| 197 | +npm start |
| 198 | +``` |
| 199 | + |
| 200 | +After making changes to the extension: |
| 201 | + |
| 202 | +```bash |
| 203 | +# Rebuild the extension |
| 204 | +cd ../osf-extension-onlyoffice |
| 205 | +npm run build |
| 206 | + |
| 207 | +# Restart angular-osf to pick up changes |
| 208 | +``` |
| 209 | + |
| 210 | +To unlink: |
| 211 | + |
| 212 | +```bash |
| 213 | +cd ../angular-osf |
| 214 | +npm unlink @nii/osf-extension-onlyoffice |
| 215 | +``` |
0 commit comments