-
Notifications
You must be signed in to change notification settings - Fork 4.7k
chore: add typedefs for registerBlockType #18257
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ gutenberg.zip | |
| phpcs.xml | ||
| yarn.lock | ||
| /wordpress | ||
| .vscode | ||
|
|
||
| playground/dist | ||
| .cache | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,101 @@ import { select, dispatch } from '@wordpress/data'; | |||||||||||||||||||||||
| import { isValidIcon, normalizeIconObject } from './utils'; | ||||||||||||||||||||||||
| import { DEPRECATED_ENTRY_KEYS } from './constants'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * TODO: Editor block attributes object. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @typedef {Object} WPBlockAttributeOptions | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @property {WPBlockAttributeType} type ... | ||||||||||||||||||||||||
| * @property {string} source ... | ||||||||||||||||||||||||
| * @property {string} selector ... | ||||||||||||||||||||||||
| * @property {string} attribute ... | ||||||||||||||||||||||||
| * @property {string} meta Attributes may be obtained from a post’s meta rather than from the block’s representation in saved post content. For this, an attribute is required to specify its corresponding meta key under the meta key | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| * @property {string} meta Attributes may be obtained from a post’s meta rather than from the block’s representation in saved post content. For this, an attribute is required to specify its corresponding meta key under the meta key | |
| * @property {string} meta Attributes may be obtained from post | |
| * meta rather than from the block’s | |
| * representation in saved post content. | |
| * For this, an attribute is required to | |
| * specify its corresponding meta key | |
| * under the meta key. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted, thanks! This entire block is marked as TODO but I will format it accordingly after adding all of the descriptions and correct types.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this type valid? I haven't seen any written like this before.
There are other variants of this that are valid so this may be a matter of style...
Examples:
// Using a `Record` type
/**
* @typedef {Record<string, WPBlockAtributeOptions>} WPBlockAttributes
*/
// Using an indexed type
/**
* @typedef { {[k: string]: WPBlockAttributeOptions} } WPBlockAttributes
*/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://jsdoc.app/tags-type.html > third row under syntax examples uses the same syntax. I don't have a style preference though so if we're using a different syntax elsewhere I am happy to conform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it to be pretty intuitive at it's proposed in this pull request, and it's a syntax I'm familiar with using in my own projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some prior art (omitting ., which upon reflection I am uncertain whether to be valid):
gutenberg/bin/packages/build-worker.js
Line 83 in 711f1f7
| * @type {Object<string,Function>} |
| * @param {Object<string,Object>} state Current state. |
| * @return {Object<string,number>} Column widths. |
| * @return {Object<string,number>} Redistributed column widths. |
gutenberg/packages/blocks/src/api/serializer.js
Lines 147 to 150 in 711f1f7
| * @param {Object<string,*>} blockType Block type. | |
| * @param {Object<string,*>} attributes Attributes from in-memory block data. | |
| * | |
| * @return {Object<string,*>} Subset of attributes for comment serialization. |
| * @param {Object<string,string>} eventTypesToHandlers Object with keys of DOM |
| * @type {Object<string,string>} |
| * @type {Object<string,MediaQueryList>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Looks good to me assuming the typescript compiler is cool with that.
(also I agree no dot is better)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a reference for this syntax of defining options of a set that you can share?
I can't find anything about it on the @type documentation, and my experience with similar options like @enum are they aren't quite what we're looking for for what you're expressing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to dig for it, but this came from jsdoc/jsdoc#629 (comment). So it's not documented as the "official" solution but it is parsed correctly, and this seemed more appropriate than an enum here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me!
The only thing I might suggest is that if a block is to be registered with a single category, it would make sense that this be singular "Category" as well, vs. "Categories"? Similar to WPBlockAttributeType and WPBlockAttributeSource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is valid... String union type 👍
The surrounding parens aren't required though. But that's a matter of style.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that this is marked todo, but see here for what I'd recommend typing this as....
(same for save below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I should say that I do understand that generics aren't as well supported in JSDoc, but ComponentType<any> should work at the very least!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Historically we've advocated that this be included in a developer's own global
.gitignoreto avoid a proliferation of user-specific entries in this file, but this has definitely come up on multiple occasions and I'm curious to potentially consider again (for developer experience' sake) whether to make exceptions at least for the most common cases.cc @gziolo
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit outside the scope of what you're trying to accomplish here, and not of huge consequence one way or the other, but perhaps a topic worth covering in tomorrow's weekly JavaScript chat in Slack.
Edit: I added a topic to the agenda