color.js 682 B

1234567891011121314151617181920212223
  1. import { ClassAttributor, Scope, StyleAttributor } from 'parchment';
  2. class ColorAttributor extends StyleAttributor {
  3. value(domNode) {
  4. let value = super.value(domNode);
  5. if (!value.startsWith('rgb(')) return value;
  6. value = value.replace(/^[^\d]+/, '').replace(/[^\d]+$/, '');
  7. const hex = value
  8. .split(',')
  9. .map(component => `00${parseInt(component, 10).toString(16)}`.slice(-2))
  10. .join('');
  11. return `#${hex}`;
  12. }
  13. }
  14. const ColorClass = new ClassAttributor('color', 'ql-color', {
  15. scope: Scope.INLINE,
  16. });
  17. const ColorStyle = new ColorAttributor('color', 'color', {
  18. scope: Scope.INLINE,
  19. });
  20. export { ColorAttributor, ColorClass, ColorStyle };