| 1 |
import { PromiseDelegate } from '@lumino/coreutils'; |
| 2 |
import { |
| 3 |
AsyncCellRenderer, |
| 4 |
CellRenderer, |
| 5 |
GraphicsContext, |
| 6 |
TextRenderer, |
| 7 |
} from '@lumino/datagrid'; |
| 8 |
import { ISignal, Signal } from '@lumino/signaling'; |
| 9 |
import { Theme } from '../utils'; |
| 10 |
|
| 11 |
// Workaround for Jupyter Lab 3 / ipywidget 7 compatibility |
| 12 |
let exportedClass: any; |
| 13 |
|
| 14 |
/** |
| 15 |
* A cell renderer which renders data values as htmls. |
| 16 |
*/ |
| 17 |
if (AsyncCellRenderer) { |
| 18 |
exportedClass = class HtmlRenderer extends AsyncCellRenderer { |
| 19 |
static dataCache = new Map<string, HTMLImageElement | undefined>(); |
| 20 |
private _imgLoaded = new Signal<this, void>(this); |
| 21 |
|
| 22 |
/** |
| 23 |
* Construct a new html renderer. |
| 24 |
* |
| 25 |
* @param options - The options for initializing the renderer. |
| 26 |
*/ |
| 27 |
constructor(options: HtmlRenderer.IOptions = {}) { |
| 28 |
super(); |
| 29 |
this.font = options.font || '12px'; |
| 30 |
this.placeholder = options.placeholder || '...'; |
| 31 |
this.textColor = options.textColor || Theme.getFontColor(); |
| 32 |
this.backgroundColor = |
| 33 |
options.backgroundColor || Theme.getBackgroundColor(); |
| 34 |
this.verticalAlignment = options.verticalAlignment || 'bottom'; |
| 35 |
this.horizontalAlignment = options.horizontalAlignment || 'left'; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* The size of the font in px. |
| 40 |
*/ |
| 41 |
readonly font: CellRenderer.ConfigOption<string>; |
| 42 |
|
| 43 |
/** |
| 44 |
* The placeholder text. |
| 45 |
*/ |
| 46 |
readonly placeholder: CellRenderer.ConfigOption<string>; |
| 47 |
|
| 48 |
/** |
| 49 |
* The color of the text. |
| 50 |
*/ |
| 51 |
readonly textColor: CellRenderer.ConfigOption<string>; |
| 52 |
|
| 53 |
/** |
| 54 |
* The color of the background. |
| 55 |
*/ |
| 56 |
readonly backgroundColor: CellRenderer.ConfigOption<string>; |
| 57 |
|
| 58 |
/** |
| 59 |
* The vertical alignment, default is 'bottom'. |
| 60 |
*/ |
| 61 |
readonly verticalAlignment: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>; |
| 62 |
|
| 63 |
/** |
| 64 |
* The horizontal alignment, default is 'left'. |
| 65 |
*/ |
| 66 |
readonly horizontalAlignment: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>; |
| 67 |
|
| 68 |
isReady(config: CellRenderer.CellConfig): boolean { |
| 69 |
return ( |
| 70 |
!config.value || HtmlRenderer.dataCache.get(config.value) !== undefined |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
get imageLoaded(): ISignal<this, void> { |
| 75 |
return this._imgLoaded; |
| 76 |
} |
| 77 |
|
| 78 |
async load(config: CellRenderer.CellConfig): Promise<void> { |
| 79 |
if (!config.value) { |
| 80 |
return; |
| 81 |
} |
| 82 |
const value = config.value; |
| 83 |
const loadedPromise = new PromiseDelegate<void>(); |
| 84 |
|
| 85 |
HtmlRenderer.dataCache.set(value, undefined); |
| 86 |
|
| 87 |
const img = new Image(); |
| 88 |
|
| 89 |
img.src = this.htmlToSvg(config); |
| 90 |
img.width = config.width; |
| 91 |
img.height = config.height; |
| 92 |
|
| 93 |
img.onload = () => { |
| 94 |
HtmlRenderer.dataCache.set(value, img); |
| 95 |
loadedPromise.resolve(); |
| 96 |
this._imgLoaded.emit(); |
| 97 |
}; |
| 98 |
|
| 99 |
return loadedPromise.promise; |
| 100 |
} |
| 101 |
|
| 102 |
paintPlaceholder( |
| 103 |
gc: GraphicsContext, |
| 104 |
config: CellRenderer.CellConfig, |
| 105 |
): void { |
| 106 |
this.drawBackground(gc, config); |
| 107 |
this.drawPlaceholder(gc, config); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Paint the content for a cell. |
| 112 |
* |
| 113 |
* @param gc - The graphics context to use for drawing. |
| 114 |
* |
| 115 |
* @param config - The configuration data for the cell. |
| 116 |
*/ |
| 117 |
paint(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 118 |
this.drawBackground(gc, config); |
| 119 |
this.drawImage(gc, config); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Draw the background for the cell. |
| 124 |
* |
| 125 |
* @param gc - The graphics context to use for drawing. |
| 126 |
* |
| 127 |
* @param config - The configuration data for the cell. |
| 128 |
*/ |
| 129 |
drawBackground(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 130 |
// Resolve the background color for the cell. |
| 131 |
const color = CellRenderer.resolveOption(this.backgroundColor, config); |
| 132 |
|
| 133 |
// Bail if there is no background color to draw. |
| 134 |
if (!color) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
// Fill the cell with the background color. |
| 139 |
gc.fillStyle = color; |
| 140 |
gc.fillRect(config.x, config.y, config.width, config.height); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Draw the placeholder for the cell. |
| 145 |
* |
| 146 |
* @param gc - The graphics context to use for drawing. |
| 147 |
* |
| 148 |
* @param config - The configuration data for the cell. |
| 149 |
*/ |
| 150 |
drawPlaceholder( |
| 151 |
gc: GraphicsContext, |
| 152 |
config: CellRenderer.CellConfig, |
| 153 |
): void { |
| 154 |
const placeholder = CellRenderer.resolveOption(this.placeholder, config); |
| 155 |
const color = CellRenderer.resolveOption(this.textColor, config); |
| 156 |
|
| 157 |
const textX = config.x + config.width / 2; |
| 158 |
const textY = config.y + config.height / 2; |
| 159 |
|
| 160 |
// Draw the placeholder. |
| 161 |
gc.fillStyle = color; |
| 162 |
gc.fillText(placeholder, textX, textY); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Draw the html. |
| 167 |
* |
| 168 |
* @param gc - The graphics context to use for drawing. |
| 169 |
* |
| 170 |
* @param config - The configuration data for the cell. |
| 171 |
*/ |
| 172 |
drawImage(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 173 |
// Bail early if there is nothing to draw |
| 174 |
if (!config.value) { |
| 175 |
return; |
| 176 |
} |
| 177 |
const img = HtmlRenderer.dataCache.get(config.value); |
| 178 |
|
| 179 |
// If it's not loaded yet, show the placeholder |
| 180 |
if (!img) { |
| 181 |
return this.drawPlaceholder(gc, config); |
| 182 |
} |
| 183 |
|
| 184 |
const font = CellRenderer.resolveOption(this.font, config); |
| 185 |
const textColor = CellRenderer.resolveOption(this.textColor, config); |
| 186 |
const textHeight = TextRenderer.measureFontHeight(font); |
| 187 |
const vAlign = CellRenderer.resolveOption(this.verticalAlignment, config); |
| 188 |
const hAlign = CellRenderer.resolveOption( |
| 189 |
this.horizontalAlignment, |
| 190 |
config, |
| 191 |
); |
| 192 |
const boxHeight = config.height - (vAlign === 'center' ? 1 : 2); |
| 193 |
|
| 194 |
if (boxHeight <= 0) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
gc.textBaseline = 'bottom'; |
| 199 |
gc.textAlign = hAlign; |
| 200 |
gc.font = font; |
| 201 |
gc.fillStyle = textColor; |
| 202 |
|
| 203 |
if (textHeight > boxHeight) { |
| 204 |
gc.beginPath(); |
| 205 |
gc.rect(config.x, config.y, config.width, config.height - 1); |
| 206 |
gc.clip(); |
| 207 |
} |
| 208 |
|
| 209 |
gc.drawImage(img, config.x, config.y); |
| 210 |
} |
| 211 |
|
| 212 |
htmlToSvg(config: CellRenderer.CellConfig): string { |
| 213 |
const font = CellRenderer.resolveOption(this.font, config); |
| 214 |
const textColor = CellRenderer.resolveOption(this.textColor, config); |
| 215 |
const vAlign = CellRenderer.resolveOption(this.verticalAlignment, config); |
| 216 |
const hAlign = CellRenderer.resolveOption( |
| 217 |
this.horizontalAlignment, |
| 218 |
config, |
| 219 |
); |
| 220 |
const width = CellRenderer.resolveOption(config.width, config); |
| 221 |
const height = CellRenderer.resolveOption(config.height, config); |
| 222 |
const text = CellRenderer.resolveOption(config.value, config); |
| 223 |
|
| 224 |
const html = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}px" height="${height}px"> |
| 225 |
<foreignObject width="${width}px" height="${height}px"> |
| 226 |
<div |
| 227 |
xmlns="http://www.w3.org/1999/xhtml" |
| 228 |
style="display: table-cell; font: ${font}; width: ${width}px; height: ${height}px; color: ${textColor}; vertical-align: ${ |
| 229 |
vAlign === 'center' ? 'middle' : vAlign |
| 230 |
}; text-align: ${hAlign}" |
| 231 |
> |
| 232 |
<div style="display: inline-block; padding: 0 2px">${text}</div> |
| 233 |
</div> |
| 234 |
</foreignObject> |
| 235 |
</svg>`; |
| 236 |
|
| 237 |
return 'data:image/svg+xml,' + encodeURIComponent(html); |
| 238 |
} |
| 239 |
}; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* The namespace for the `HtmlRenderer` class statics. |
| 244 |
*/ |
| 245 |
export namespace HtmlRenderer { |
| 246 |
/** |
| 247 |
* An options object for initializing a html renderer. |
| 248 |
*/ |
| 249 |
export interface IOptions extends TextRenderer.IOptions { |
| 250 |
/** |
| 251 |
|
| 252 |
* The font for drawing the cell text. |
| 253 |
* |
| 254 |
* The default is `'12px sans-serif'`. |
| 255 |
*/ |
| 256 |
font?: CellRenderer.ConfigOption<string>; |
| 257 |
|
| 258 |
/** |
| 259 |
* The string to be displayed when placeholder is painted |
| 260 |
* |
| 261 |
* The default is `'...'`. |
| 262 |
*/ |
| 263 |
placeholder?: CellRenderer.ConfigOption<string>; |
| 264 |
|
| 265 |
/** |
| 266 |
* The color for the drawing the cell text. |
| 267 |
* |
| 268 |
* The default `'#000000'`. |
| 269 |
*/ |
| 270 |
textColor?: CellRenderer.ConfigOption<string>; |
| 271 |
|
| 272 |
/** |
| 273 |
* The vertical alignment for the cell text. |
| 274 |
* |
| 275 |
* The default is `'center'`. |
| 276 |
*/ |
| 277 |
verticalAlignment?: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>; |
| 278 |
/** |
| 279 |
* The horizontal alignment for the cell text. |
| 280 |
* |
| 281 |
* The default is `'left'`. |
| 282 |
*/ |
| 283 |
horizontalAlignment?: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>; |
| 284 |
/** |
| 285 |
* The format function for the renderer. |
| 286 |
* |
| 287 |
* The default is `TextRenderer.formatGeneric()`. |
| 288 |
*/ |
| 289 |
// format?: FormatFunc; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
export { exportedClass }; |