formula.js 881 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Embed from '../blots/embed';
  2. import Quill from '../core/quill';
  3. import Module from '../core/module';
  4. class FormulaBlot extends Embed {
  5. static create(value) {
  6. let node = super.create(value);
  7. if (typeof value === 'string') {
  8. window.katex.render(value, node, {
  9. throwOnError: false,
  10. errorColor: '#f00'
  11. });
  12. node.setAttribute('data-value', value);
  13. }
  14. return node;
  15. }
  16. static value(domNode) {
  17. return domNode.getAttribute('data-value');
  18. }
  19. }
  20. FormulaBlot.blotName = 'formula';
  21. FormulaBlot.className = 'ql-formula';
  22. FormulaBlot.tagName = 'SPAN';
  23. class Formula extends Module {
  24. static register() {
  25. Quill.register(FormulaBlot, true);
  26. }
  27. constructor() {
  28. super();
  29. if (window.katex == null) {
  30. throw new Error('Formula module requires KaTeX.');
  31. }
  32. }
  33. }
  34. export { FormulaBlot, Formula as default };