| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2019, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { CellRenderer } from './cellrenderer'; |
| 11 |
|
| 12 |
import { GraphicsContext } from './graphicscontext'; |
| 13 |
|
| 14 |
import { TextRenderer } from './textrenderer'; |
| 15 |
|
| 16 |
/** |
| 17 |
* A cell renderer which renders data values as text. |
| 18 |
*/ |
| 19 |
export class HyperlinkRenderer extends TextRenderer { |
| 20 |
/** |
| 21 |
* Construct a new text renderer. |
| 22 |
* |
| 23 |
* @param options - The options for initializing the renderer. |
| 24 |
*/ |
| 25 |
constructor(options: HyperlinkRenderer.IOptions = {}) { |
| 26 |
// Set default parameters before passing over the super. |
| 27 |
options.textColor = options.textColor || 'navy'; |
| 28 |
options.font = options.font || 'bold 12px sans-serif'; |
| 29 |
super(options); |
| 30 |
this.url = options.url; |
| 31 |
this.urlName = options.urlName; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* The URL address. |
| 36 |
*/ |
| 37 |
readonly url: CellRenderer.ConfigOption<string> | undefined; |
| 38 |
|
| 39 |
/** |
| 40 |
* The friendly link name. |
| 41 |
*/ |
| 42 |
readonly urlName: CellRenderer.ConfigOption<string> | undefined; |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the full text to be rendered by the cell. |
| 46 |
*/ |
| 47 |
getText(config: CellRenderer.CellConfig): string { |
| 48 |
let urlName = CellRenderer.resolveOption(this.urlName, config); |
| 49 |
|
| 50 |
// If we have a friendly URL name, use that. |
| 51 |
if (urlName) { |
| 52 |
return this.format({ |
| 53 |
...config, |
| 54 |
value: urlName |
| 55 |
} as CellRenderer.CellConfig); |
| 56 |
} |
| 57 |
|
| 58 |
// Otherwise use the raw value attribute. |
| 59 |
return this.format(config); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Draw the text for the cell. |
| 64 |
* |
| 65 |
* @param gc - The graphics context to use for drawing. |
| 66 |
* |
| 67 |
* @param config - The configuration data for the cell. |
| 68 |
*/ |
| 69 |
drawText(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 70 |
// Resolve the font for the cell. |
| 71 |
let font = CellRenderer.resolveOption(this.font, config); |
| 72 |
|
| 73 |
// Bail if there is no font to draw. |
| 74 |
if (!font) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Resolve the text color for the cell. |
| 79 |
let color = CellRenderer.resolveOption(this.textColor, config); |
| 80 |
|
| 81 |
// Bail if there is no text color to draw. |
| 82 |
if (!color) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
let text = this.getText(config); |
| 87 |
|
| 88 |
// Bail if there is no text to draw. |
| 89 |
if (!text) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Resolve the vertical and horizontal alignment. |
| 94 |
let vAlign = CellRenderer.resolveOption(this.verticalAlignment, config); |
| 95 |
let hAlign = CellRenderer.resolveOption(this.horizontalAlignment, config); |
| 96 |
|
| 97 |
// Resolve the elision direction |
| 98 |
let elideDirection = CellRenderer.resolveOption( |
| 99 |
this.elideDirection, |
| 100 |
config |
| 101 |
); |
| 102 |
|
| 103 |
// Resolve the text wrapping flag |
| 104 |
let wrapText = CellRenderer.resolveOption(this.wrapText, config); |
| 105 |
|
| 106 |
// Compute the padded text box height for the specified alignment. |
| 107 |
let boxHeight = config.height - (vAlign === 'center' ? 1 : 2); |
| 108 |
|
| 109 |
// Bail if the text box has no effective size. |
| 110 |
if (boxHeight <= 0) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
// Compute the text height for the gc font. |
| 115 |
let textHeight = HyperlinkRenderer.measureFontHeight(font); |
| 116 |
|
| 117 |
// Set up the text position variables. |
| 118 |
let textX: number; |
| 119 |
let textY: number; |
| 120 |
let boxWidth: number; |
| 121 |
|
| 122 |
// Compute the Y position for the text. |
| 123 |
switch (vAlign) { |
| 124 |
case 'top': |
| 125 |
textY = config.y + 2 + textHeight; |
| 126 |
break; |
| 127 |
case 'center': |
| 128 |
textY = config.y + config.height / 2 + textHeight / 2; |
| 129 |
break; |
| 130 |
case 'bottom': |
| 131 |
textY = config.y + config.height - 2; |
| 132 |
break; |
| 133 |
default: |
| 134 |
throw 'unreachable'; |
| 135 |
} |
| 136 |
|
| 137 |
// Compute the X position for the text. |
| 138 |
switch (hAlign) { |
| 139 |
case 'left': |
| 140 |
textX = config.x + 8; |
| 141 |
boxWidth = config.width - 14; |
| 142 |
break; |
| 143 |
case 'center': |
| 144 |
textX = config.x + config.width / 2; |
| 145 |
boxWidth = config.width; |
| 146 |
break; |
| 147 |
case 'right': |
| 148 |
textX = config.x + config.width - 8; |
| 149 |
boxWidth = config.width - 14; |
| 150 |
break; |
| 151 |
default: |
| 152 |
throw 'unreachable'; |
| 153 |
} |
| 154 |
|
| 155 |
// Clip the cell if the text is taller than the text box height. |
| 156 |
if (textHeight > boxHeight) { |
| 157 |
gc.beginPath(); |
| 158 |
gc.rect(config.x, config.y, config.width, config.height - 1); |
| 159 |
gc.clip(); |
| 160 |
} |
| 161 |
|
| 162 |
// Set the gc state. |
| 163 |
gc.font = font; |
| 164 |
gc.fillStyle = color; |
| 165 |
gc.textAlign = hAlign; |
| 166 |
gc.textBaseline = 'bottom'; |
| 167 |
|
| 168 |
// Terminate call here if we're not eliding or wrapping text |
| 169 |
if (elideDirection === 'none' && !wrapText) { |
| 170 |
gc.fillText(text, textX, textY); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
// The current text width in pixels. |
| 175 |
let textWidth = gc.measureText(text).width; |
| 176 |
|
| 177 |
// Apply text wrapping if enabled. |
| 178 |
if (wrapText && textWidth > boxWidth) { |
| 179 |
// Make sure box clipping happens. |
| 180 |
gc.beginPath(); |
| 181 |
gc.rect(config.x, config.y, config.width, config.height - 1); |
| 182 |
gc.clip(); |
| 183 |
|
| 184 |
// Split column name to words based on |
| 185 |
// whitespace preceding a word boundary. |
| 186 |
// "Hello world" --> ["Hello ", "world"] |
| 187 |
const wordsInColumn = text.split(/\s(?=\b)/); |
| 188 |
|
| 189 |
// Y-coordinate offset for any additional lines |
| 190 |
let curY = textY; |
| 191 |
let textInCurrentLine = wordsInColumn.shift()!; |
| 192 |
|
| 193 |
// Single word. Applying text wrap on word by splitting |
| 194 |
// it into characters and fitting the maximum number of |
| 195 |
// characters possible per line (box width). |
| 196 |
if (wordsInColumn.length === 0) { |
| 197 |
let curLineTextWidth = gc.measureText(textInCurrentLine).width; |
| 198 |
while (curLineTextWidth > boxWidth && textInCurrentLine !== '') { |
| 199 |
// Iterating from the end of the string until we find a |
| 200 |
// substring (0,i) which has a width less than the box width. |
| 201 |
for (let i = textInCurrentLine.length; i > 0; i--) { |
| 202 |
const curSubString = textInCurrentLine.substring(0, i); |
| 203 |
const curSubStringWidth = gc.measureText(curSubString).width; |
| 204 |
if (curSubStringWidth < boxWidth || curSubString.length === 1) { |
| 205 |
// Found a substring which has a width less than the current |
| 206 |
// box width. Rendering that substring on the current line |
| 207 |
// and setting the remainder of the parent string as the next |
| 208 |
// string to iterate on for the next line. |
| 209 |
const nextLineText = textInCurrentLine.substring( |
| 210 |
i, |
| 211 |
textInCurrentLine.length |
| 212 |
); |
| 213 |
textInCurrentLine = nextLineText; |
| 214 |
curLineTextWidth = gc.measureText(textInCurrentLine).width; |
| 215 |
gc.fillText(curSubString, textX, curY); |
| 216 |
curY += textHeight; |
| 217 |
// No need to continue iterating after we identified |
| 218 |
// an index to break the string on. |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// Multiple words in column header. Fitting maximum |
| 226 |
// number of words possible per line (box width). |
| 227 |
else { |
| 228 |
while (wordsInColumn.length !== 0) { |
| 229 |
// Processing the next word in the queue. |
| 230 |
const curWord = wordsInColumn.shift(); |
| 231 |
// Joining that word with the existing text for |
| 232 |
// the current line. |
| 233 |
const incrementedText = [textInCurrentLine, curWord].join(' '); |
| 234 |
const incrementedTextWidth = gc.measureText(incrementedText).width; |
| 235 |
if (incrementedTextWidth > boxWidth) { |
| 236 |
// If the newly combined text has a width larger than |
| 237 |
// the box width, we render the line before the current |
| 238 |
// word was added. We set the current word as the next |
| 239 |
// line. |
| 240 |
gc.fillText(textInCurrentLine, textX, curY); |
| 241 |
curY += textHeight; |
| 242 |
textInCurrentLine = curWord!; |
| 243 |
} else { |
| 244 |
// The combined text hasd a width less than the box width. We |
| 245 |
// set the the current line text to be the new combined text. |
| 246 |
textInCurrentLine = incrementedText; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
gc.fillText(textInCurrentLine!, textX, curY); |
| 251 |
// Terminating the call here as we don't want |
| 252 |
// to apply text eliding when wrapping is active. |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
// Elide text that is too long |
| 257 |
let elide = '\u2026'; |
| 258 |
|
| 259 |
// Compute elided text |
| 260 |
if (elideDirection === 'right') { |
| 261 |
while (textWidth > boxWidth && text.length > 1) { |
| 262 |
if (text.length > 4 && textWidth >= 2 * boxWidth) { |
| 263 |
// If text width is substantially bigger, take half the string |
| 264 |
text = text.substring(0, text.length / 2 + 1) + elide; |
| 265 |
} else { |
| 266 |
// Otherwise incrementally remove the last character |
| 267 |
text = text.substring(0, text.length - 2) + elide; |
| 268 |
} |
| 269 |
textWidth = gc.measureText(text).width; |
| 270 |
} |
| 271 |
} else { |
| 272 |
while (textWidth > boxWidth && text.length > 1) { |
| 273 |
if (text.length > 4 && textWidth >= 2 * boxWidth) { |
| 274 |
// If text width is substantially bigger, take half the string |
| 275 |
text = elide + text.substring(text.length / 2); |
| 276 |
} else { |
| 277 |
// Otherwise incrementally remove the last character |
| 278 |
text = elide + text.substring(2); |
| 279 |
} |
| 280 |
textWidth = gc.measureText(text).width; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Draw the text for the cell. |
| 285 |
gc.fillText(text, textX, textY); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
export namespace HyperlinkRenderer { |
| 290 |
/** |
| 291 |
* A type alias for the supported vertical alignment modes. |
| 292 |
*/ |
| 293 |
export type VerticalAlignment = 'top' | 'center' | 'bottom'; |
| 294 |
|
| 295 |
/** |
| 296 |
* A type alias for the supported horizontal alignment modes. |
| 297 |
*/ |
| 298 |
export type HorizontalAlignment = 'left' | 'center' | 'right'; |
| 299 |
|
| 300 |
/** |
| 301 |
* A type alias for the supported ellipsis sides. |
| 302 |
*/ |
| 303 |
export type ElideDirection = 'left' | 'right' | 'none'; |
| 304 |
|
| 305 |
/** |
| 306 |
* An options object for initializing a text renderer. |
| 307 |
*/ |
| 308 |
export interface IOptions extends TextRenderer.IOptions { |
| 309 |
/** |
| 310 |
* The URL address |
| 311 |
*/ |
| 312 |
url?: CellRenderer.ConfigOption<string> | undefined; |
| 313 |
/** |
| 314 |
* The friendly link name. |
| 315 |
* |
| 316 |
* The default is the URL itself. |
| 317 |
*/ |
| 318 |
urlName?: CellRenderer.ConfigOption<string> | undefined; |
| 319 |
} |
| 320 |
} |