fbf9be56e4af8c590b1e902c7fc958fc09d5aec6cabd75b1fb290f518f82f2291327ad32c6422d65b3c5decdcfb663e986b83534eb662d8a13753f2532e7eb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { EmbedBlot, InlineBlot, Scope } from 'parchment';
  2. import Break from './break.js';
  3. import Text from './text.js';
  4. class Inline extends InlineBlot {
  5. static allowedChildren = [Inline, Break, EmbedBlot, Text];
  6. // Lower index means deeper in the DOM tree, since not found (-1) is for embeds
  7. static order = ['cursor', 'inline',
  8. // Must be lower
  9. 'link',
  10. // Chrome wants <a> to be lower
  11. 'underline', 'strike', 'italic', 'bold', 'script', 'code' // Must be higher
  12. ];
  13. static compare(self, other) {
  14. const selfIndex = Inline.order.indexOf(self);
  15. const otherIndex = Inline.order.indexOf(other);
  16. if (selfIndex >= 0 || otherIndex >= 0) {
  17. return selfIndex - otherIndex;
  18. }
  19. if (self === other) {
  20. return 0;
  21. }
  22. if (self < other) {
  23. return -1;
  24. }
  25. return 1;
  26. }
  27. formatAt(index, length, name, value) {
  28. if (Inline.compare(this.statics.blotName, name) < 0 && this.scroll.query(name, Scope.BLOT)) {
  29. const blot = this.isolate(index, length);
  30. if (value) {
  31. blot.wrap(name, value);
  32. }
  33. } else {
  34. super.formatAt(index, length, name, value);
  35. }
  36. }
  37. optimize(context) {
  38. super.optimize(context);
  39. if (this.parent instanceof Inline && Inline.compare(this.statics.blotName, this.parent.statics.blotName) > 0) {
  40. const parent = this.parent.isolate(this.offset(), this.length());
  41. // @ts-expect-error TODO: make isolate generic
  42. this.moveChildren(parent);
  43. parent.wrap(this);
  44. }
  45. }
  46. }
  47. export default Inline;
  48. //# sourceMappingURL=inline.js.map