Skip to content

Commit

Permalink
feat: baseURL for image paths (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz authored Dec 18, 2023
1 parent 31c3b54 commit 136e38c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 4 additions & 2 deletions docs/APIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ var view = new vega.View(runtime)
| `disableDefaultStyle` | Boolean | Disable the default style completely. |
| `sanitize` | Function | Function to convert value to string, and sanitize the HTML. |
| `maxDepth` | Number | The maximum recursion depth when printing objects in the tooltip. |
| `formatTooltip` | Function | Function to specify custom HTML inside tooltip element. It is highly recommended to `sanitize` any data from `value` before returning an HTML string. |
| `formatTooltip` | Function | Function to specify custom HTML inside tooltip element. It is highly recommended to `sanitize` any data from `value` before returning an HTML string. |
| `baseURL` | String | The base URL to use for resolving relative URLs for images. |


The default values are:
Expand All @@ -79,7 +80,8 @@ var DEFAULT_OPTIONS =
theme: 'light',
disableDefaultStyle: false,
sanitize: (value) => String(value).replace(/&/g, '&amp;').replace(/</g, '&lt;'),
formatTooltip: vegaTooltip.formatValue
formatTooltip: vegaTooltip.formatValue,
baseURL: ''
};
```

Expand Down
7 changes: 6 additions & 1 deletion src/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export class Handler {
}

// set the tooltip content
this.el.innerHTML = this.options.formatTooltip(value, this.options.sanitize, this.options.maxDepth);
this.el.innerHTML = this.options.formatTooltip(
value,
this.options.sanitize,
this.options.maxDepth,
this.options.baseURL,
);

// make the tooltip visible
this.el.classList.add('visible', `${this.options.theme}-theme`);
Expand Down
6 changes: 6 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ export const DEFAULT_OPTIONS = {
* A function to customize the rendered HTML of the tooltip.
* @param value A value string, or object of value strings keyed by field
* @param sanitize The `sanitize` function from `options.sanitize`
* @param baseURL The `baseURL` from `options.baseURL`
* @returns {string} The returned string will become the `innerHTML` of the tooltip element
*/
formatTooltip: formatValue,

/**
* The baseurl to use in image paths.
*/
baseURL: '',
};

export type Options = Partial<typeof DEFAULT_OPTIONS>;
Expand Down
9 changes: 7 additions & 2 deletions src/formatValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {isArray, isObject, isString} from 'vega-util';
* @param value The value to show in the tooltip.
* @param valueToHtml Function to convert a single cell value to an HTML string
*/
export function formatValue(value: any, valueToHtml: (value: any) => string, maxDepth: number): string {
export function formatValue(
value: any,

Check warning on line 10 in src/formatValue.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
valueToHtml: (value: any) => string,

Check warning on line 11 in src/formatValue.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
maxDepth: number,
baseURL: string,
): string {
if (isArray(value)) {
return `[${value.map((v) => valueToHtml(isString(v) ? v : stringify(v, maxDepth))).join(', ')}]`;
}
Expand All @@ -21,7 +26,7 @@ export function formatValue(value: any, valueToHtml: (value: any) => string, max
}

if (image) {
content += `<img src="${valueToHtml(image)}">`;
content += `<img src="${new URL(valueToHtml(image), baseURL || location.href).href}">`;
}

const keys = Object.keys(rest);
Expand Down

0 comments on commit 136e38c

Please sign in to comment.