Codemirror-CSS-color-picker
Codemirror-CSS-color-picker copied to clipboard
Extending to support languages other than CSS?
A lot of visualisations are created in languages such as JavaScript, Python, Julia, or R. The visualisation libraries usually just use the same color syntax as CSS, often because they do produce CSS (along some HTML or SVG) in the end.
I would be interested in extending this extension to work in strings in these languages. The recognition of a string as colour would happen only if the string contains a valid color, so "#cccccc"
and 'red'
would pass but "green" in "I like green apples"
would not be detected.
This boils down to the enter
function which walks the syntax tree to discover colors:
https://github.com/replit/Codemirror-CSS-color-picker/blob/3b52d6733437058383e47fce7bcac2e4ed6eb91c/src/index.ts#L39-L46
There are two ways I see this could happen:
- (a) the
enter
function could be modified to accept the strings containing only colour; there is a fewtypeName
in the default CodeMirror modes for "string" depending on the languages, but this is a limited set - (b) a new callable plugin could be created, allowing the user to pass a custom implementation of the
enter()
function; in that case I would also like to export the utility functionstoFullHex
,hslToHex
,rgbToHex
andnamedColors
which are used inenter()
Are you interested in reviewing a contribution enabling such usage? Do you prefer option (a) or (b) or do you have a completely different idea?