Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: baseURL for image paths #855

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -53,7 +53,7 @@
/**
* The tooltip handler function.
*/
private tooltipHandler(handler: any, event: MouseEvent, item: any, value: any) {

Check warning on line 56 in src/Handler.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type

Check warning on line 56 in src/Handler.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type

Check warning on line 56 in src/Handler.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
// console.log(handler, event, item, value);

// append a div element that we use as a tooltip unless it already exists
Expand All @@ -74,7 +74,12 @@
}

// 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 @@
* 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 All @@ -64,7 +70,7 @@
*
* @param value A value to convert to string and HTML-escape.
*/
export function escapeHTML(value: any): string {

Check warning on line 73 in src/defaults.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
return String(value).replace(/&/g, '&amp;').replace(/</g, '&lt;');
}

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 @@
* @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 @@ -14,21 +19,21 @@
if (isObject(value)) {
let content = '';

const {title, image, ...rest} = value as any;

Check warning on line 22 in src/formatValue.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type

if (title) {
content += `<h2>${valueToHtml(title)}</h2>`;
}

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

const keys = Object.keys(rest);
if (keys.length > 0) {
content += '<table>';
for (const key of keys) {
let val = (rest as any)[key];

Check warning on line 36 in src/formatValue.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type

// ignore undefined properties
if (val === undefined) {
Expand All @@ -51,9 +56,9 @@
}

export function replacer(maxDepth: number) {
const stack: any[] = [];

Check warning on line 59 in src/formatValue.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type

return function (this: any, key: string, value: any) {

Check warning on line 61 in src/formatValue.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected any. Specify a different type
if (typeof value !== 'object' || value === null) {
return value;
}
Expand Down