|
| 1 | +import { Panel, Widget } from "@lumino/widgets"; |
| 2 | +import type { CellDiffWidget } from "./"; |
| 3 | + |
| 4 | +import foldDown from "./fold-down.svg"; |
| 5 | +import foldUp from "./fold-up.svg"; |
| 6 | +import fold from "./fold.svg"; |
| 7 | + |
| 8 | +class LinkedListCell extends Panel { |
| 9 | + _next: LinkedListCell | LazyDisplayLinkedListCell | null; |
| 10 | + _prev: LinkedListCell | LazyDisplayLinkedListCell | null; |
| 11 | + renderFunc: () => CellDiffWidget; |
| 12 | + _displayed: boolean; |
| 13 | + lazy: boolean; |
| 14 | + |
| 15 | + constructor(renderFunc: () => CellDiffWidget) { |
| 16 | + super(); |
| 17 | + this._next = null; |
| 18 | + this._prev = null; |
| 19 | + this.renderFunc = renderFunc; |
| 20 | + this._displayed = true; |
| 21 | + this.renderCell(); |
| 22 | + this.addClass("linked-cell"); |
| 23 | + this.lazy = false; |
| 24 | + } |
| 25 | + |
| 26 | + protected renderCell() { |
| 27 | + this.addWidget(this.renderFunc()); |
| 28 | + this._displayed = true; |
| 29 | + } |
| 30 | + |
| 31 | + get next(): LinkedListCell | LazyDisplayLinkedListCell | null { |
| 32 | + return this._next; |
| 33 | + } |
| 34 | + |
| 35 | + set next(nextCell: LinkedListCell | LazyDisplayLinkedListCell | null) { |
| 36 | + this._next = nextCell; |
| 37 | + if (nextCell === null) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (nextCell.lazy) { |
| 42 | + console.log("call expandDown"); |
| 43 | + nextCell.expandDown(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + get prev(): LinkedListCell | LazyDisplayLinkedListCell | null { |
| 48 | + return this._prev; |
| 49 | + } |
| 50 | + |
| 51 | + set prev(prevCell: LinkedListCell | LazyDisplayLinkedListCell | null) { |
| 52 | + this._prev = prevCell; |
| 53 | + if (prevCell === null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + prevCell.next = this; |
| 57 | + if (prevCell.lazy) { |
| 58 | + prevCell.expandUp(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + get displayed(): boolean { |
| 63 | + return this._displayed; |
| 64 | + } |
| 65 | + |
| 66 | + expandUp(): void { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + expandDown(): void { |
| 71 | + return; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class LazyDisplayLinkedListCell extends LinkedListCell { |
| 76 | + expandButton: HTMLDivElement; |
| 77 | + expandButtonDisplayed: boolean; |
| 78 | + |
| 79 | + // Path: packages/nbdime/src/diff/widget/wrapper_cells.ts |
| 80 | + constructor(renderFunc: () => CellDiffWidget) { |
| 81 | + super(renderFunc); |
| 82 | + this.expandButton = document.createElement("div"); |
| 83 | + this.expandButton.className = "jp-expand-output-wrapper"; |
| 84 | + this.expandButtonDisplayed = false; |
| 85 | + this.addClass("lazy-linked-cell"); |
| 86 | + this.lazy = true; |
| 87 | + } |
| 88 | + |
| 89 | + set prev(prevCell: LinkedListCell | LazyDisplayLinkedListCell) { |
| 90 | + this._prev = prevCell; |
| 91 | + prevCell.next = this; |
| 92 | + } |
| 93 | + |
| 94 | + set next(nextCell: LinkedListCell | LazyDisplayLinkedListCell) { |
| 95 | + this._next = nextCell; |
| 96 | + } |
| 97 | + |
| 98 | + protected renderCell() { |
| 99 | + this._displayed = false; |
| 100 | + } |
| 101 | + |
| 102 | + expandUp(): void { |
| 103 | + if (this._displayed) { |
| 104 | + return; |
| 105 | + } |
| 106 | + if (this.expandButtonDisplayed) { |
| 107 | + this._setupFoldButton(); |
| 108 | + } else { |
| 109 | + this._setupExpandUpButton(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + expandDown(): void { |
| 114 | + if (this._displayed) { |
| 115 | + return; |
| 116 | + } |
| 117 | + if (this.expandButtonDisplayed) { |
| 118 | + this._setupFoldButton(); |
| 119 | + } else { |
| 120 | + this._setupExpandDownButton(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + _setupFoldButton() { |
| 125 | + this.expandButton.innerHTML = ""; |
| 126 | + const button = this.createButton("Fold"); |
| 127 | + button.onclick = (e) => { |
| 128 | + e.preventDefault(); |
| 129 | + this.showLazyCellUp(); |
| 130 | + }; |
| 131 | + this.expandButton.appendChild(button); |
| 132 | + const widget = new Widget({ node: this.expandButton }); |
| 133 | + this.addWidget(widget); |
| 134 | + } |
| 135 | + |
| 136 | + _setupExpandUpButton() { |
| 137 | + const button = this.createButton("Up"); |
| 138 | + button.onclick = (e) => { |
| 139 | + e.preventDefault(); |
| 140 | + this.showLazyCellUp(); |
| 141 | + }; |
| 142 | + this.expandButton.appendChild(button); |
| 143 | + const widget = new Widget({ node: this.expandButton }); |
| 144 | + this.addWidget(widget); |
| 145 | + } |
| 146 | + |
| 147 | + _setupExpandDownButton() { |
| 148 | + const button = this.createButton("Down"); |
| 149 | + button.onclick = (e) => { |
| 150 | + e.preventDefault(); |
| 151 | + this.showLazyCellDown(); |
| 152 | + }; |
| 153 | + this.expandButton.appendChild(button); |
| 154 | + const widget = new Widget({ node: this.expandButton }); |
| 155 | + this.addWidget(widget); |
| 156 | + } |
| 157 | + |
| 158 | + createButton(direction: "Up" | "Down" | "Fold"): HTMLAnchorElement { |
| 159 | + this.expandButton.innerHTML = ""; |
| 160 | + const button = document.createElement("a"); |
| 161 | + button.title = `Expand ${direction}`; |
| 162 | + button.setAttribute("aria-label", `Expand ${direction}`); |
| 163 | + button.innerHTML = this.buttonSvg(direction); |
| 164 | + if (direction === "Up") { |
| 165 | + button.innerHTML = foldUp; |
| 166 | + } else if (direction === "Down") { |
| 167 | + button.innerHTML = foldDown; |
| 168 | + } else { |
| 169 | + button.innerHTML = fold; |
| 170 | + } |
| 171 | + this.expandButtonDisplayed = true; |
| 172 | + return button; |
| 173 | + } |
| 174 | + |
| 175 | + buttonSvg(direction: "Up" | "Down" | "Fold"): string { |
| 176 | + if (direction === "Up") { |
| 177 | + return foldUp; |
| 178 | + } else if (direction === "Down") { |
| 179 | + return foldDown; |
| 180 | + } else { |
| 181 | + return fold; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + showLazyCellUp() { |
| 186 | + this.showLazyCell(); |
| 187 | + this._prev?.expandUp(); |
| 188 | + } |
| 189 | + |
| 190 | + showLazyCellDown() { |
| 191 | + this.showLazyCell(); |
| 192 | + this._next?.expandDown(); |
| 193 | + } |
| 194 | + |
| 195 | + showLazyCell() { |
| 196 | + this.addWidget(this.renderFunc()); |
| 197 | + this._displayed = true; |
| 198 | + this.expandButton.remove(); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +export { LinkedListCell, LazyDisplayLinkedListCell }; |
0 commit comments