仲裁视频会议H5

text.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Blot, Leaf } from './abstract/blot';
  2. import LeafBlot from './abstract/leaf';
  3. import * as Registry from '../registry';
  4. class TextBlot extends LeafBlot implements Leaf {
  5. static blotName = 'text';
  6. static scope = Registry.Scope.INLINE_BLOT;
  7. public domNode!: Text;
  8. protected text: string;
  9. static create(value: string): Text {
  10. return document.createTextNode(value);
  11. }
  12. static value(domNode: Text): string {
  13. let text = domNode.data;
  14. // @ts-ignore
  15. if (text['normalize']) text = text['normalize']();
  16. return text;
  17. }
  18. constructor(node: Node) {
  19. super(node);
  20. this.text = this.statics.value(this.domNode);
  21. }
  22. deleteAt(index: number, length: number): void {
  23. this.domNode.data = this.text = this.text.slice(0, index) + this.text.slice(index + length);
  24. }
  25. index(node: Node, offset: number): number {
  26. if (this.domNode === node) {
  27. return offset;
  28. }
  29. return -1;
  30. }
  31. insertAt(index: number, value: string, def?: any): void {
  32. if (def == null) {
  33. this.text = this.text.slice(0, index) + value + this.text.slice(index);
  34. this.domNode.data = this.text;
  35. } else {
  36. super.insertAt(index, value, def);
  37. }
  38. }
  39. length(): number {
  40. return this.text.length;
  41. }
  42. optimize(context: { [key: string]: any }): void {
  43. super.optimize(context);
  44. this.text = this.statics.value(this.domNode);
  45. if (this.text.length === 0) {
  46. this.remove();
  47. } else if (this.next instanceof TextBlot && this.next.prev === this) {
  48. this.insertAt(this.length(), (<TextBlot>this.next).value());
  49. this.next.remove();
  50. }
  51. }
  52. position(index: number, inclusive: boolean = false): [Node, number] {
  53. return [this.domNode, index];
  54. }
  55. split(index: number, force: boolean = false): Blot {
  56. if (!force) {
  57. if (index === 0) return this;
  58. if (index === this.length()) return this.next;
  59. }
  60. let after = Registry.create(this.domNode.splitText(index));
  61. this.parent.insertBefore(after, this.next);
  62. this.text = this.statics.value(this.domNode);
  63. return after;
  64. }
  65. update(mutations: MutationRecord[], context: { [key: string]: any }): void {
  66. if (
  67. mutations.some(mutation => {
  68. return mutation.type === 'characterData' && mutation.target === this.domNode;
  69. })
  70. ) {
  71. this.text = this.statics.value(this.domNode);
  72. }
  73. }
  74. value(): string {
  75. return this.text;
  76. }
  77. }
  78. export default TextBlot;