Explorar o código

Some type improvements

JC Brand hai 8 meses
pai
achega
852e226355

+ 24 - 16
src/entry.js

@@ -13,21 +13,32 @@
 // Once the rest converse.js has been loaded, window.converse will be replaced
 // with the full-fledged public API.
 
+/**
+ * @typedef {Object.<string, object>} Plugins
+ */
 const plugins = {};
 
 const converse = {
     plugins: {
-        add (name, plugin) {
+        /**
+         * Adds a Converse plugin
+         * @param {string} name The name of the plugin.
+         * @param {object} plugin The plugin object to be added.
+         * @throws {TypeError} If a plugin with the same name has already been registered.
+         */
+        add(name, plugin) {
             if (plugins[name] !== undefined) {
-                throw new TypeError(
-                    `Error: plugin with name "${name}" has already been ` + 'registered!'
-                );
+                throw new TypeError(`Error: plugin with name "${name}" has already been ` + 'registered!');
             }
             plugins[name] = plugin;
-        }
+        },
     },
 
-    initialize (settings={}) {
+    /**
+     * Initializes Converse with the provided settings.
+     * @param {object} settings
+     */
+    initialize(settings = {}) {
         return converse.load(settings).initialize(settings);
     },
 
@@ -47,23 +58,20 @@ const converse = {
      * `converse.initialize` has been called, then you need to call
      * `converse.load` first.
      *
-     * @memberOf converse
-     * @method load
-     * @param { object } settings A map of configuration-settings that are needed at load time.
-     * @example
-     * converse.load({assets_path: '/path/to/assets/'});
+     * @param {object} settings A map of configuration-settings that are needed at load time.
+     * @example converse.load({assets_path: '/path/to/assets/'});
      */
-    load (settings={}) {
+    load(settings = {}) {
         if (settings.assets_path) {
             // eslint-disable-next-line @typescript-eslint/ban-ts-comment
             // @ts-ignore
             __webpack_public_path__ = settings.assets_path; // eslint-disable-line no-undef
         }
         require('./index.js');
-        Object.keys(plugins).forEach(name => converse.plugins.add(name, plugins[name]));
+        Object.keys(plugins).forEach((name) => converse.plugins.add(name, plugins[name]));
         return converse;
-    }
-}
+    },
+};
 
 window['converse'] = converse;
 
@@ -74,7 +82,7 @@ window['converse'] = converse;
  * @event converse-loaded
  * @example window.addEventListener('converse-loaded', () => converse.initialize());
  */
-const ev = new CustomEvent('converse-loaded', {'detail': { converse }});
+const ev = new CustomEvent('converse-loaded', { 'detail': { converse } });
 window.dispatchEvent(ev);
 
 export default converse;

+ 31 - 15
src/plugins/bookmark-views/components/bookmark-form.js

@@ -1,7 +1,6 @@
 import tplMUCBookmarkForm from './templates/form.js';
 import { CustomElement } from 'shared/components/element';
-import { _converse, api } from "@converse/headless";
-
+import { _converse, api } from '@converse/headless';
 
 class MUCBookmarkForm extends CustomElement {
 
@@ -12,44 +11,61 @@ class MUCBookmarkForm extends CustomElement {
 
     static get properties () {
         return {
-            'jid': { type: String }
-        }
+            'jid': { type: String },
+        };
     }
 
-    willUpdate (changed_properties) {
+    /**
+     * @param {Map<PropertyKey, any>} changed_properties
+     * @return {void}
+     */
+    willUpdate(changed_properties) {
         const { chatboxes, bookmarks } = _converse.state;
         if (changed_properties.has('jid')) {
             this.model = chatboxes.get(this.jid);
-            this.bookmark  = bookmarks.get(this.jid);
+            this.bookmark = bookmarks.get(this.jid);
         }
     }
 
-    render () {
-        return tplMUCBookmarkForm(this)
+    render() {
+        return tplMUCBookmarkForm(this);
     }
 
+    /**
+     * @param {Event} ev
+     */
     onBookmarkFormSubmitted (ev) {
         ev.preventDefault();
         const { bookmarks } = _converse.state;
+        const form = /** @type {HTMLFormElement} */ (ev.target);
         bookmarks.createBookmark({
-            'jid': this.jid,
-            'autojoin': ev.target.querySelector('input[name="autojoin"]')?.checked || false,
-            'name': ev.target.querySelector('input[name=name]')?.value,
-            'nick': ev.target.querySelector('input[name=nick]')?.value
+            jid: this.jid,
+            autojoin: /** @type {HTMLInputElement} */ (form.querySelector('input[name="autojoin"]'))?.checked || false,
+            name: /** @type {HTMLInputElement} */ (form.querySelector('input[name=name]'))?.value,
+            nick: /** @type {HTMLInputElement} */ (form.querySelector('input[name=nick]'))?.value,
         });
         this.closeBookmarkForm(ev);
     }
 
+    /**
+     * @param {Event} ev
+     */
     removeBookmark (ev) {
         this.bookmark?.destroy();
         this.closeBookmarkForm(ev);
     }
 
+    /**
+     * @param {Event} ev
+     */
     closeBookmarkForm (ev) {
         ev.preventDefault();
-        const evt = document.createEvent('Event');
-        evt.initEvent('hide.bs.modal', true, true);
-        this.dispatchEvent(evt);
+        this.dispatchEvent(
+            new Event('hide.bs.modal', {
+                bubbles: true,
+                cancelable: true,
+            })
+        );
     }
 }
 

+ 3 - 0
src/plugins/bookmark-views/components/bookmarks-list.js

@@ -41,6 +41,9 @@ export default class BookmarksView extends CustomElement {
         return _converse.state.bookmarks && this.model ? tplBookmarksList(this) : tplSpinner();
     }
 
+    /**
+     * @param {Event} ev
+     */
     clearFilter (ev) {
         ev?.stopPropagation?.();
         this.model.set('text', '');

+ 1 - 1
src/shared/tests/mock.js

@@ -684,7 +684,7 @@ function getMockVcardFetcher (settings) {
     }
 }
 
-const theme = ['dracula', 'classic', 'concord'][Math.floor(Math.random()*3)];
+const theme = ['dracula', 'classic', 'cyberpunk'][Math.floor(Math.random()*3)];
 
 async function _initConverse (settings) {
     clearStores();

+ 29 - 9
src/types/entry.d.ts

@@ -1,9 +1,22 @@
 export default converse;
+export type Plugins = {
+    [x: string]: object;
+};
 declare namespace converse {
     namespace plugins {
-        function add(name: any, plugin: any): void;
+        /**
+         * Adds a Converse plugin
+         * @param {string} name The name of the plugin.
+         * @param {object} plugin The plugin object to be added.
+         * @throws {TypeError} If a plugin with the same name has already been registered.
+         */
+        function add(name: string, plugin: any): void;
     }
-    function initialize(settings?: {}): any;
+    /**
+     * Initializes Converse with the provided settings.
+     * @param {object} settings
+     */
+    function initialize(settings?: any): any;
     /**
      * Public API method which explicitly loads Converse and allows you the
      * possibility to pass in configuration settings which need to be defined
@@ -20,17 +33,24 @@ declare namespace converse {
      * `converse.initialize` has been called, then you need to call
      * `converse.load` first.
      *
-     * @memberOf converse
-     * @method load
-     * @param { object } settings A map of configuration-settings that are needed at load time.
-     * @example
-     * converse.load({assets_path: '/path/to/assets/'});
+     * @param {object} settings A map of configuration-settings that are needed at load time.
+     * @example converse.load({assets_path: '/path/to/assets/'});
      */
     function load(settings?: any): {
         plugins: {
-            add(name: any, plugin: any): void;
+            /**
+             * Adds a Converse plugin
+             * @param {string} name The name of the plugin.
+             * @param {object} plugin The plugin object to be added.
+             * @throws {TypeError} If a plugin with the same name has already been registered.
+             */
+            add(name: string, plugin: any): void;
         };
-        initialize(settings?: {}): any;
+        /**
+         * Initializes Converse with the provided settings.
+         * @param {object} settings
+         */
+        initialize(settings?: any): any;
         load(settings?: any): any;
     };
 }

+ 17 - 4
src/types/plugins/bookmark-views/components/bookmark-form.d.ts

@@ -6,13 +6,26 @@ declare class MUCBookmarkForm extends CustomElement {
         };
     };
     jid: any;
-    willUpdate(changed_properties: any): void;
+    /**
+     * @param {Map<PropertyKey, any>} changed_properties
+     * @return {void}
+     */
+    willUpdate(changed_properties: Map<PropertyKey, any>): void;
     model: any;
     bookmark: any;
     render(): import("lit").TemplateResult<1>;
-    onBookmarkFormSubmitted(ev: any): void;
-    removeBookmark(ev: any): void;
-    closeBookmarkForm(ev: any): void;
+    /**
+     * @param {Event} ev
+     */
+    onBookmarkFormSubmitted(ev: Event): void;
+    /**
+     * @param {Event} ev
+     */
+    removeBookmark(ev: Event): void;
+    /**
+     * @param {Event} ev
+     */
+    closeBookmarkForm(ev: Event): void;
 }
 import { CustomElement } from "shared/components/element";
 //# sourceMappingURL=bookmark-form.d.ts.map

+ 4 - 1
src/types/plugins/bookmark-views/components/bookmarks-list.d.ts

@@ -3,7 +3,10 @@ export default class BookmarksView extends CustomElement {
     liveFilter: import("lodash").DebouncedFunc<(ev: any) => false | Model>;
     model: Model;
     render(): import("lit").TemplateResult<1>;
-    clearFilter(ev: any): void;
+    /**
+     * @param {Event} ev
+     */
+    clearFilter(ev: Event): void;
 }
 import { CustomElement } from "shared/components/element.js";
 import { Model } from "@converse/skeletor";

+ 1 - 1
src/utils/html.js

@@ -296,7 +296,7 @@ function nextUntil (el, selector) {
  * Helper method that replace HTML-escaped symbols with equivalent characters
  * (e.g. transform occurrences of '&amp;' to '&')
  * @method u#unescapeHTML
- * @param { String } string - a String containing the HTML-escaped symbols.
+ * @param {String} string - a String containing the HTML-escaped symbols.
  */
 function unescapeHTML (string) {
     var div = document.createElement('div');