-
Notifications
You must be signed in to change notification settings - Fork 808
Add Custom File Type Handler API #1262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bajrangCoder
merged 2 commits into
Acode-Foundation:main
from
bajrangCoder:feature/custom-file-handlers
Apr 16, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * @typedef {Object} FileTypeHandler | ||
| * @property {string} id - Unique identifier for the handler | ||
| * @property {string[]} extensions - File extensions this handler supports (without dots) | ||
| * @property {function} handleFile - Function that handles the file | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {Object} FileInfo | ||
| * @property {string} name - File name | ||
| * @property {string} uri - File URI | ||
| * @property {Object} stats - File stats | ||
| * @property {boolean} readOnly - Whether the file is read-only | ||
| * @property {Object} options - Additional options passed during file open | ||
| */ | ||
|
|
||
| class FileTypeHandlerRegistry { | ||
| #handlers = new Map(); | ||
|
|
||
| /** | ||
| * Register a file type handler | ||
| * @param {string} id - Unique identifier for the handler | ||
| * @param {Object} options - Handler options | ||
| * @param {string[]} options.extensions - File extensions to handle (without dots) | ||
| * @param {function(FileInfo): Promise<void>} options.handleFile - Async function to handle the file | ||
| * @throws {Error} If id is already registered or required options are missing | ||
| */ | ||
| registerFileHandler(id, { extensions, handleFile }) { | ||
| if (this.#handlers.has(id)) { | ||
| throw new Error(`Handler with id '${id}' is already registered`); | ||
| } | ||
|
|
||
| if (!extensions?.length) { | ||
| throw new Error("extensions array is required"); | ||
| } | ||
|
|
||
| if (typeof handleFile !== "function") { | ||
| throw new Error("handleFile function is required"); | ||
| } | ||
|
|
||
| // Normalize extensions (remove dots if present, convert to lowercase) | ||
| const normalizedExts = extensions.map((ext) => | ||
| ext.toLowerCase().replace(/^\./, ""), | ||
| ); | ||
|
|
||
| this.#handlers.set(id, { | ||
| extensions: normalizedExts, | ||
| handleFile, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Unregister a file type handler | ||
| * @param {string} id - The handler id to remove | ||
| */ | ||
| unregisterFileHandler(id) { | ||
| this.#handlers.delete(id); | ||
| } | ||
|
|
||
| /** | ||
| * Get a file handler for a given filename | ||
| * @param {string} filename | ||
| * @returns {Object|null} The matching handler or null if none found | ||
| */ | ||
| getFileHandler(filename) { | ||
| const ext = filename.split(".").pop().toLowerCase(); | ||
|
|
||
| for (const [id, handler] of this.#handlers) { | ||
| if ( | ||
| handler.extensions.includes(ext) || | ||
| handler.extensions.includes("*") | ||
| ) { | ||
| return { | ||
| id, | ||
| ...handler, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered handlers | ||
| * @returns {Map} Map of all registered handlers | ||
| */ | ||
| getHandlers() { | ||
| return new Map(this.#handlers); | ||
| } | ||
| } | ||
|
|
||
| export const fileTypeHandler = new FileTypeHandlerRegistry(); | ||
| export default fileTypeHandler; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.