Skip to content

style_map components should be given data on render #87

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

Closed
thibaudcolas opened this issue Jan 25, 2018 · 0 comments
Closed

style_map components should be given data on render #87

thibaudcolas opened this issue Jan 25, 2018 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Milestone

Comments

@thibaudcolas
Copy link
Collaborator

At the moment, style_map components do not receive any data beyond the text to style (as props['children]).

def render_styles(self, text_node):
node = text_node
if not self.is_empty():
# Nest the tags.
for s in sorted(self.styles, reverse=True):
opt = Options.for_style(self.style_map, s)
node = DOM.create_element(opt.element, opt.props, node)

This is ok for common use cases (BOLD, ITALIC, etc), but it makes the style_map fallback rather useless – there is no way to know what style needs the fallback, or have any other information about the context to adjust the fallback behavior.

Here's what the block_map fallback has access to for comparison:

props['block'] = {
        'type': type_,
        'depth': depth,
        'data': data,
}

In retrospect I think this could've been all of the block's attributes, not just a cherry-picked shortlist, so for inline styles we could pass the following exhaustive props:

{
    # The style range to render.
    "range": 
        "offset": 10,
        "length": 17,
        "style": "BOLD"
    },
    # The full block data, eg.
    "block": {
            "key": "t7k7",
            "text": "Unstyled test test test test test",
            "type": "unstyled",
            "depth": 0,
            "inlineStyleRanges": [
                {
                    "offset": 10,
                    "length": 17,
                    "style": "BOLD"
                }
            ],
            "entityRanges": [
                {
                    "offset": 0,
                    "length": 4,
                    "key": 6
                }
            ],
            "data": {}
        },
}

Here's the approximative change:

-    def render_styles(self, text_node):
+    def render_styles(self, text_node, block):
        node = text_node
        if not self.is_empty():
            # Nest the tags.
            for s in sorted(self.styles, reverse=True):
                opt = Options.for_style(self.style_map, s)
+                props['block'] = block
+                props['range'] = s
                node = DOM.create_element(opt.element, opt.props, node)

        return node

Ideally I'd like entities and blocks to also be given more data (enough data to recreate the whole ContentState, thus making the exporter usable to create content migrations), but that's a separate issue.

@thibaudcolas thibaudcolas added enhancement New feature or request help wanted Extra attention is needed labels Jan 25, 2018
@thibaudcolas thibaudcolas added this to the Nice to have milestone Jan 25, 2018
@thibaudcolas thibaudcolas added the good first issue Good for newcomers label Jan 25, 2018
thibaudcolas added a commit that referenced this issue Mar 1, 2018
…s` list, and current style type as `inline_style_range.style`. Fix #87
thibaudcolas added a commit that referenced this issue Mar 1, 2018
…s` list, and current style type as `inline_style_range.style`. Fix #87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant