|
| 1 | +<!-- Generated by documentation.js. Update this documentation by updating the source code. --> |
| 2 | + |
| 3 | +## features/NewFileContentManager |
| 4 | + |
| 5 | +NewFileContentManager provides support to add default template content when a new/empty file is created. |
| 6 | +Extensions can register to provide content with `NewFileContentManager.registerContentProvider` API. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +Let's say whenever a user creates a new js file, we have to prefill the contents to "console.log("hello")" |
| 11 | + |
| 12 | +```js |
| 13 | +const NewFileContentManager = brackets.getModule("features/NewFileContentManager"); |
| 14 | +// replace `js` with language ID(Eg. javascript) if you want to restrict the preview to js files only. use `all` for |
| 15 | +// all languages. |
| 16 | +NewFileContentManager.registerContentProvider(exports, ["js"], 1); |
| 17 | + |
| 18 | +// provide a helpful name for the ContentProvider. This will be useful if you have to debug. |
| 19 | +exports.CONTENT_PROVIDER_NAME = "extension.someName"; |
| 20 | +// now implement the getContent function that will be invoked when ever creates a new empty file. |
| 21 | +exports.getContent = function(fullPath) { |
| 22 | + return new Promise((resolve, reject)=>{ |
| 23 | + resolve("sample content"); |
| 24 | + }); |
| 25 | + }; |
| 26 | +``` |
| 27 | + |
| 28 | +## API |
| 29 | + |
| 30 | +### registerContentProvider |
| 31 | + |
| 32 | +Register a Content provider with this api. |
| 33 | + |
| 34 | +```js |
| 35 | +// syntax |
| 36 | +NewFileContentManager.registerContentProvider(provider, supportedLanguages, priority); |
| 37 | +``` |
| 38 | + |
| 39 | +The API requires three parameters: |
| 40 | + |
| 41 | +1. `provider`: must implement a `getContent` function which will be invoked to get the content. See API doc below. |
| 42 | +2. `supportedLanguages`: An array of languages that the provider supports. If `["all"]` is supplied, then the |
| 43 | + provider will be invoked for all languages. Restrict to specific languages: Eg: `["javascript", "html", "php"]` |
| 44 | +3. `priority`: Contents provided hy providers with higher priority will win if there are more than |
| 45 | + one provider registered for the language. Default is 0. |
| 46 | + |
| 47 | +```js |
| 48 | +// to register a provider that will be invoked for all languages. where provider is any object that implements |
| 49 | +// a getContent function |
| 50 | +NewFileContentManager.registerContentProvider(provider, ["all"]); |
| 51 | + |
| 52 | +// to register a provider that will be invoked for specific languages |
| 53 | +NewFileContentManager.registerContentProvider(provider, ["javascript", "html", "php"]); |
| 54 | +``` |
| 55 | + |
| 56 | +### removeContentProvider |
| 57 | + |
| 58 | +Removes a registered content provider. The API takes the same arguments as `registerContentProvider`. |
| 59 | + |
| 60 | +```js |
| 61 | +// syntax |
| 62 | +NewFileContentManager.removeContentProvider(provider, supportedLanguages); |
| 63 | +// Example |
| 64 | +NewFileContentManager.removeContentProvider(provider, ["javascript", "html"]); |
| 65 | +``` |
| 66 | + |
| 67 | +### provider.getContent |
| 68 | + |
| 69 | +Each provider must implement the `getContent` function that returns a promise. The promise either resolves with |
| 70 | +the content text or rejects if there is no content made available by the provider. |
| 71 | + |
| 72 | +```js |
| 73 | +exports.CONTENT_PROVIDER_NAME = "extension.someName"; // for debugging |
| 74 | +// function signature |
| 75 | +exports.getContent = function(fullPath) { |
| 76 | + return new Promise((resolve, reject)=>{ |
| 77 | + resolve("sample content"); |
| 78 | + }); |
| 79 | + }; |
| 80 | +``` |
| 81 | + |
| 82 | +#### parameters |
| 83 | + |
| 84 | +The function will be called with the path of the file that needs the content. |
| 85 | + |
| 86 | +1. `fullPath` - string path |
| 87 | + |
| 88 | +#### return types |
| 89 | + |
| 90 | +A promise that resolves with the content text or rejects if there is no content made available by the provider. |
| 91 | + |
| 92 | +## getInitialContentForFile |
| 93 | + |
| 94 | +Returns a promise that resolves to the default text content of the given file after querying |
| 95 | +all the content providers. If no text is returned by any providers, it will return an empty string "". |
| 96 | + |
| 97 | +### Parameters |
| 98 | + |
| 99 | +* `fullPath` |
| 100 | + |
| 101 | +Returns **[Promise][1]<[string][2]>** The text contents |
| 102 | + |
| 103 | +[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise |
| 104 | + |
| 105 | +[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String |
0 commit comments