inline.js 1.5 KB

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