feat(attrs): design + demos for token-based text attributes#10
Draft
jitendra-jimdo wants to merge 1 commit into
Draft
feat(attrs): design + demos for token-based text attributes#10jitendra-jimdo wants to merge 1 commit into
jitendra-jimdo wants to merge 1 commit into
Conversation
Add an engineering reference under docs/ and two runtime-only demos
that prototype a generalised text-attribute mechanism: semantic data
attributes (data-font, data-color, data-underline) backed by a fixed
vocabulary defined by the host theme.
No source changes to src/ or dist/ in this PR. The demos work against
the shipped dist/trix.js via three runtime monkey-patches:
- HTMLSanitizer.sanitize: allow data-* attribute names through
- PieceView.createElement: honour a new `attributeName` config key
- Trix.config.textAttributes: register font/color/underline
The follow-up PR will bake these into source (3 files, ~15 lines),
rebuild dist/, and cut a release for consumption from the CMS.
Files
docs/ overview, build/test, architecture,
attribute system, feature proposal
color-demo.html single-attribute (color) runtime demo
tokens-demo.html three-dimension (font/color/underline) demo
.gitignore ignore /vendor/bundle and /.bundle
mise.toml pin Ruby 2.6.10 for the build toolchain
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
docs/mapping the editor's build, architecture, attribute system, and a feature proposal — written from a fresh read of the codebase after the 6-year hiatus.data-font,data-color,data-underline) without modifyingsrc/ordist/.mise.tomland ignores/vendor/bundle+/.bundleso the local Ruby toolchain stops fighting the macOS system Ruby.What this PR is not
This PR makes no changes to
src/ordist/. It's design + demos. The demos work against the shippeddist/trix.jsvia three runtime monkey-patches living inside the demo HTML — not the editor itself. The follow-up PR will land the source changes.The design
Three independent text attributes, each with its own value vocabulary defined by the host theme:
fontdata-fontheadline,body,accentcolordata-colorprimary,secondary,shade1,shade2underlinedata-underlinepattern1…pattern5Dimensions are orthogonal — a single piece can carry any combination. HTML output for a piece with
{font: "headline", color: "primary", bold: true}:The token-to-style mapping lives in CSS at the consumer (CMS theme), keeping the editor agnostic.
How the demos work (the three runtime patches)
Inside each demo's
<script>block, before<trix-editor>is parsed:Trix.HTMLSanitizer.sanitizeis wrapped to append the newdata-*attribute names toallowedAttributes(default allow-list isstyle href src width height class targetplusdata-trix-*).Trix.PieceView.prototype.createElementis wrapped to honour a customattributeNameconfig key. After the stock function handlestagName/styleProperty/style, our wrapper callssetAttribute(config.attributeName, value)so the token becomes an HTML attribute on the rendered element.Trix.config.textAttributes.{font,color,underline}is registered, each with{attributeName, inheritable: true, parser}. The parser usesTrix.findClosestElementFromNodeto walk up to the matching[data-*]ancestor — same shape ashref's parser.Roadmap to a release
When the CLI Tools toolchain is fixed locally (or built elsewhere), the source-side changes are mechanical:
src/trix/config/text_attributes.coffee— registerfont/color/underline(~15 lines).src/trix/views/piece_view.coffee— extendcreateElementwith theattributeNamebranch (~3 lines).src/trix/models/html_sanitizer.coffee— extendDEFAULT_ALLOWED_ATTRIBUTES(one line).test/src/unit/html_parser_test.coffeeandtest/src/system/text_formatting_test.coffee.bin/blade build→ regeneratedist/.package.jsonto0.13.0,npm publish.workspace/apps/cms/components/editors/TextEditor/TrixTextEditor/index.tsx.docs/05-color-feature-proposal.mdhas the full reasoning for the single-colour version of this; it should be either generalised or replaced with a "tokens" version when the source PR lands.Test plan
color-demo.htmlin a browser served from the repo root — pick a colour token, seedata-color="..."in the serialized HTML pane on the right.tokens-demo.html— combine font + colour + underline on the same selection, confirm they stack on a single element.tokens-demo.htmlclick Reload from serialized HTML — editor re-parses its own output, all tokens survive.<strong data-color="...">…</strong>round-trips.Cmd+Zreverses the last token change in one step.To serve locally without the Ruby build:
🤖 Generated with Claude Code