zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Blot } from './abstract/blot';
import ContainerBlot from './abstract/container';
import LinkedList from '../collection/linked-list';
import * as Registry from '../registry';
 
const OBSERVER_CONFIG = {
  attributes: true,
  characterData: true,
  characterDataOldValue: true,
  childList: true,
  subtree: true,
};
 
const MAX_OPTIMIZE_ITERATIONS = 100;
 
class ScrollBlot extends ContainerBlot {
  static blotName = 'scroll';
  static defaultChild = 'block';
  static scope = Registry.Scope.BLOCK_BLOT;
  static tagName = 'DIV';
 
  observer: MutationObserver;
 
  constructor(node: HTMLDivElement) {
    super(node);
    this.scroll = this;
    this.observer = new MutationObserver((mutations: MutationRecord[]) => {
      this.update(mutations);
    });
    this.observer.observe(this.domNode, OBSERVER_CONFIG);
    this.attach();
  }
 
  detach() {
    super.detach();
    this.observer.disconnect();
  }
 
  deleteAt(index: number, length: number): void {
    this.update();
    if (index === 0 && length === this.length()) {
      this.children.forEach(function(child) {
        child.remove();
      });
    } else {
      super.deleteAt(index, length);
    }
  }
 
  formatAt(index: number, length: number, name: string, value: any): void {
    this.update();
    super.formatAt(index, length, name, value);
  }
 
  insertAt(index: number, value: string, def?: any): void {
    this.update();
    super.insertAt(index, value, def);
  }
 
  optimize(context: { [key: string]: any }): void;
  optimize(mutations: MutationRecord[], context: { [key: string]: any }): void;
  optimize(mutations: any = [], context: any = {}): void {
    super.optimize(context);
    // We must modify mutations directly, cannot make copy and then modify
    let records = [].slice.call(this.observer.takeRecords());
    // Array.push currently seems to be implemented by a non-tail recursive function
    // so we cannot just mutations.push.apply(mutations, this.observer.takeRecords());
    while (records.length > 0) mutations.push(records.pop());
    // TODO use WeakMap
    let mark = (blot: Blot | null, markParent: boolean = true) => {
      if (blot == null || blot === this) return;
      if (blot.domNode.parentNode == null) return;
      // @ts-ignore
      if (blot.domNode[Registry.DATA_KEY].mutations == null) {
        // @ts-ignore
        blot.domNode[Registry.DATA_KEY].mutations = [];
      }
      if (markParent) mark(blot.parent);
    };
    let optimize = function(blot: Blot) {
      // Post-order traversal
      if (
        // @ts-ignore
        blot.domNode[Registry.DATA_KEY] == null ||
        // @ts-ignore
        blot.domNode[Registry.DATA_KEY].mutations == null
      ) {
        return;
      }
      if (blot instanceof ContainerBlot) {
        blot.children.forEach(optimize);
      }
      blot.optimize(context);
    };
    let remaining = mutations;
    for (let i = 0; remaining.length > 0; i += 1) {
      if (i >= MAX_OPTIMIZE_ITERATIONS) {
        throw new Error('[Parchment] Maximum optimize iterations reached');
      }
      remaining.forEach(function(mutation: MutationRecord) {
        let blot = Registry.find(mutation.target, true);
        if (blot == null) return;
        if (blot.domNode === mutation.target) {
          if (mutation.type === 'childList') {
            mark(Registry.find(mutation.previousSibling, false));
            [].forEach.call(mutation.addedNodes, function(node: Node) {
              let child = Registry.find(node, false);
              mark(child, false);
              if (child instanceof ContainerBlot) {
                child.children.forEach(function(grandChild: Blot) {
                  mark(grandChild, false);
                });
              }
            });
          } else if (mutation.type === 'attributes') {
            mark(blot.prev);
          }
        }
        mark(blot);
      });
      this.children.forEach(optimize);
      remaining = [].slice.call(this.observer.takeRecords());
      records = remaining.slice();
      while (records.length > 0) mutations.push(records.pop());
    }
  }
 
  update(mutations?: MutationRecord[], context: { [key: string]: any } = {}): void {
    mutations = mutations || this.observer.takeRecords();
    // TODO use WeakMap
    mutations
      .map(function(mutation: MutationRecord) {
        let blot = Registry.find(mutation.target, true);
        if (blot == null) return null;
        // @ts-ignore
        if (blot.domNode[Registry.DATA_KEY].mutations == null) {
          // @ts-ignore
          blot.domNode[Registry.DATA_KEY].mutations = [mutation];
          return blot;
        } else {
          // @ts-ignore
          blot.domNode[Registry.DATA_KEY].mutations.push(mutation);
          return null;
        }
      })
      .forEach((blot: Blot | null) => {
        if (
          blot == null ||
          blot === this ||
          //@ts-ignore
          blot.domNode[Registry.DATA_KEY] == null
        )
          return;
        // @ts-ignore
        blot.update(blot.domNode[Registry.DATA_KEY].mutations || [], context);
      });
    // @ts-ignore
    if (this.domNode[Registry.DATA_KEY].mutations != null) {
      // @ts-ignore
      super.update(this.domNode[Registry.DATA_KEY].mutations, context);
    }
    this.optimize(mutations, context);
  }
}
 
export default ScrollBlot;