@@ -13,7 +13,7 @@ function isNodeWithSize(node: LexicalNode): node is NodeHasSize&LexicalNode {
|
13 | 13 | class NodeResizer {
|
14 | 14 | protected context: EditorUiContext;
|
15 | 15 | protected resizerDOM: HTMLElement|null = null;
|
16 |
| -protected targetDOM: HTMLElement|null = null; |
| 16 | +protected targetNode: LexicalNode|null = null; |
17 | 17 | protected scrollContainer: HTMLElement;
|
18 | 18 |
|
19 | 19 | protected mouseTracker: MouseDragTracker|null = null;
|
@@ -38,20 +38,27 @@ class NodeResizer {
|
38 | 38 |
|
39 | 39 | if (nodes.length === 1 && isNodeWithSize(nodes[0])) {
|
40 | 40 | const node = nodes[0];
|
41 |
| -const nodeKey = node.getKey(); |
42 |
| -let nodeDOM = this.context.editor.getElementByKey(nodeKey); |
43 |
| - |
44 |
| -if (nodeDOM && nodeDOM.nodeName === 'SPAN') { |
45 |
| -nodeDOM = nodeDOM.firstElementChild as HTMLElement; |
46 |
| -} |
| 41 | +let nodeDOM = this.getTargetDOM(node) |
47 | 42 |
|
48 | 43 | if (nodeDOM) {
|
49 | 44 | this.showForNode(node, nodeDOM);
|
50 | 45 | }
|
51 | 46 | }
|
52 | 47 | }
|
53 | 48 |
|
54 |
| -onTargetDOMLoad(): void { |
| 49 | +protected getTargetDOM(targetNode: LexicalNode|null): HTMLElement|null { |
| 50 | +if (targetNode == null) { |
| 51 | +return null; |
| 52 | +} |
| 53 | + |
| 54 | +let nodeDOM = this.context.editor.getElementByKey(targetNode.__key) |
| 55 | +if (nodeDOM && nodeDOM.nodeName === 'SPAN') { |
| 56 | +nodeDOM = nodeDOM.firstElementChild as HTMLElement; |
| 57 | +} |
| 58 | +return nodeDOM; |
| 59 | +} |
| 60 | + |
| 61 | +protected onTargetDOMLoad(): void { |
55 | 62 | this.updateResizerPosition();
|
56 | 63 | }
|
57 | 64 |
|
@@ -62,7 +69,7 @@ class NodeResizer {
|
62 | 69 |
|
63 | 70 | protected showForNode(node: NodeHasSize&LexicalNode, targetDOM: HTMLElement) {
|
64 | 71 | this.resizerDOM = this.buildDOM();
|
65 |
| -this.targetDOM = targetDOM; |
| 72 | +this.targetNode = node; |
66 | 73 |
|
67 | 74 | let ghost = el('span', {class: 'editor-node-resizer-ghost'});
|
68 | 75 | if ($isImageNode(node)) {
|
@@ -83,12 +90,13 @@ class NodeResizer {
|
83 | 90 | }
|
84 | 91 |
|
85 | 92 | protected updateResizerPosition() {
|
86 |
| -if (!this.resizerDOM || !this.targetDOM) { |
| 93 | +const targetDOM = this.getTargetDOM(this.targetNode); |
| 94 | +if (!this.resizerDOM || !targetDOM) { |
87 | 95 | return;
|
88 | 96 | }
|
89 | 97 |
|
90 | 98 | const scrollAreaRect = this.scrollContainer.getBoundingClientRect();
|
91 |
| -const nodeRect = this.targetDOM.getBoundingClientRect(); |
| 99 | +const nodeRect = targetDOM.getBoundingClientRect(); |
92 | 100 | const top = nodeRect.top - (scrollAreaRect.top - this.scrollContainer.scrollTop);
|
93 | 101 | const left = nodeRect.left - scrollAreaRect.left;
|
94 | 102 |
|
@@ -110,7 +118,7 @@ class NodeResizer {
|
110 | 118 | protected hide() {
|
111 | 119 | this.mouseTracker?.teardown();
|
112 | 120 | this.resizerDOM?.remove();
|
113 |
| -this.targetDOM = null; |
| 121 | +this.targetNode = null; |
114 | 122 | this.activeSelection = '';
|
115 | 123 | this.loadAbortController.abort();
|
116 | 124 | }
|
@@ -126,7 +134,7 @@ class NodeResizer {
|
126 | 134 | }, handleElems);
|
127 | 135 | }
|
128 | 136 |
|
129 |
| -setupTracker(container: HTMLElement, node: NodeHasSize, nodeDOM: HTMLElement): MouseDragTracker { |
| 137 | +setupTracker(container: HTMLElement, node: NodeHasSize&LexicalNode, nodeDOM: HTMLElement): MouseDragTracker { |
130 | 138 | let startingWidth: number = 0;
|
131 | 139 | let startingHeight: number = 0;
|
132 | 140 | let startingRatio: number = 0;
|
@@ -179,10 +187,13 @@ class NodeResizer {
|
179 | 187 | _this.context.editor.update(() => {
|
180 | 188 | node.setWidth(size.width);
|
181 | 189 | node.setHeight(hasHeight ? size.height : 0);
|
182 |
| -_this.context.manager.triggerLayoutUpdate(); |
183 |
| -requestAnimationFrame(() => { |
184 |
| -_this.updateResizerPosition(); |
185 |
| -}) |
| 190 | +}, { |
| 191 | +onUpdate: () => { |
| 192 | +requestAnimationFrame(() => { |
| 193 | +_this.context.manager.triggerLayoutUpdate(); |
| 194 | +_this.updateResizerPosition(); |
| 195 | +}); |
| 196 | +} |
186 | 197 | });
|
187 | 198 | _this.resizerDOM?.classList.remove('active');
|
188 | 199 | }
|
|
0 commit comments