New concept: block attribute sets #38191
Replies: 2 comments 4 replies
-
cc: @mtias @youknowriad |
Beta Was this translation helpful? Give feedback.
-
I think the idea has value, bringing more semantics to the block API can be useful in a lot of situations. That said, I don't think the proposed approach is the right one, I think we should favor serializable APIs in favor of functions. The server might also need to know about this information and declaring this as a JS function forbids that entirely. So I'm actually more in favor of the opposite direction (assign a "role" to an attribute), in fact we already have such attribute today but it's marked unstable I think what I'm missing before stabilizing and expanding that API is to actually use it more, expand the use-cases for it and once the dust settles we can make it stable as it would have proven its value. |
Beta Was this translation helpful? Give feedback.
-
In this discussion I'd like to introduce and share design work on a new concept to the block API, what I'm currently calling "attribute sets." Attribute sets give block authors a way to classify relevance of specific block attributes in different contexts to let the editor make more informed decisions in those contexts.
Related
Description and motivation
Let us consider different contexts that exist while editing a document:
These contexts provide a basis for classifying block attributes so that we don't have to treat every block attribute in the same way. These classifications can provide new and enhanced functionality such as:
Design
It's tempting to look at these use-cases and build the inverse of what is proposed here:
classifyAttributes
where we specify which context each attribute applies to. However, the categories overlap and so it's not appropriate to force a 1:1 mapping. It's also more burdensome to classify attributes into contexts than to report membership into a context of the set of attributes.When registering a block with
registerBlockType
we can add a new optional setting,attributeSets( context, attributes )
, giving block authors the ability to perform the classification and to do so dynamically based not only on the names of the attributes but also their values.Return type
The inputs to this function don't need to be that complicated in order to provide a useful and easy API. The return type, however, will determine how confusing it is to use. There are different answers we might want to give to the question:
Some of these questions need more consideration. It's my believe right now that we will likely want to allow people to specify a list of attribute names to include, provide a full attribute set to use instead of the block attributes (which could include or exclude specific attributes or transform their values), and accept returning
undefined
to accept a default behavior.API injection
It's also possible given the likelihood of only needing minor adjustments to the attribute sets in normal practice that instead of returning an indicator for which attributes are relevant to a context that we provide a little imperative API instead.
The imperative approach has some strong disadvantages by its nature but it may have a stronger match to the mental model of marking attributes and remove some complexity around making a sensible DSL for including and excluding at the same time (c.f. the same challenge with
.gitignore
files).Performance considerations
There's a potential blow-up if we don't properly design how often we classify attributes. For the most part classification should only need to occur after human-initiated actions like undo/redo or copy/paste. It's also possible however that every change in the editor could trigger classification due to the saving and serialization process.
One pre-emotive way to mitigate the issue would be to only allow classifying attributes by their name and not by their value. There's a strong disadvantage to this as it would prevent solving one of the motivating cases for the feature: don't undo an image's source the local data URI generated when uploading the image (it would be wise for blocks not to use their attributes for data like that, but many do in practice, making classification a practical value-add).
Another way would be to simply watch the metrics and see how it works in practice. Thankfully most or all of the performance issues should be addressable on the framework side and I don't see code in the block implementations causing that much trouble. If we maintain our contract in the framework then we can iterate on the algorithm as needed.
Next steps
If you have thoughts on this design I'd love to hear your feedback. There are potentially many more contexts where attribute classification could be useful. We can imagine core- or plugin-defined contexts that blocks could opt into simply by being aware of their existence:
grammar-check
,readable
, etc…If you know of issues that are related to this please link them. It would be good to draw a wide net to see how people are currently trying to solve these problems and let that inform how we design a solution in the block API.
Thank you for your time reading through this and your thoughts!
Beta Was this translation helpful? Give feedback.
All reactions