inline.js 1.4 KB

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