2021-05-12 21:00:55 +00:00
|
|
|
import {CellValue} from 'app/common/DocActions';
|
2020-10-07 21:58:43 +00:00
|
|
|
import {DataRowModel} from 'app/client/models/DataRowModel';
|
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {colors, testId} from 'app/client/ui2018/cssVars';
|
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
|
|
|
import {NTextBox} from 'app/client/widgets/NTextBox';
|
|
|
|
import {dom, styled} from 'grainjs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a widget for displaying links. Links can entered directly or following a title.
|
|
|
|
* The last entry following a space is used as the url.
|
|
|
|
* ie 'google https://www.google.com' would apears as 'google' to the user but link to the url.
|
|
|
|
*/
|
|
|
|
export class HyperLinkTextBox extends NTextBox {
|
|
|
|
constructor(field: ViewFieldRec) {
|
2021-03-02 12:27:08 +00:00
|
|
|
super(field, {defaultTextColor: colors.lightGreen.value});
|
2020-10-07 21:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
const value = row.cells[this.field.colId()];
|
|
|
|
return cssFieldClip(
|
|
|
|
dom.style('text-align', this.alignment),
|
|
|
|
dom.cls('text_wrapping', this.wrapping),
|
2021-05-12 21:00:55 +00:00
|
|
|
dom.maybe((use) => Boolean(use(value)), () =>
|
2020-10-07 21:58:43 +00:00
|
|
|
dom('a',
|
|
|
|
dom.attr('href', (use) => _constructUrl(use(value))),
|
|
|
|
dom.attr('target', '_blank'),
|
|
|
|
// As per Google and Mozilla recommendations to prevent opened links
|
|
|
|
// from running on the same process as Grist:
|
|
|
|
// https://developers.google.com/web/tools/lighthouse/audits/noopener
|
|
|
|
dom.attr('rel', 'noopener noreferrer'),
|
2021-02-09 10:12:53 +00:00
|
|
|
cssLinkIcon('FieldLink', testId('tb-link-icon')),
|
2020-10-07 21:58:43 +00:00
|
|
|
testId('tb-link')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
dom.text((use) => _formatValue(use(value))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats value like `foo bar baz` by discarding `baz` and returning `foo bar`.
|
|
|
|
*/
|
2021-05-12 21:00:55 +00:00
|
|
|
function _formatValue(value: CellValue): string {
|
2020-10-07 21:58:43 +00:00
|
|
|
if (typeof value !== 'string') { return ''; }
|
|
|
|
const index = value.lastIndexOf(' ');
|
|
|
|
return index >= 0 ? value.slice(0, index) : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given value like `foo bar baz`, constructs URL by checking if `baz` is a valid URL and,
|
|
|
|
* if not, prepending `http://`.
|
|
|
|
*/
|
2021-05-12 21:00:55 +00:00
|
|
|
function _constructUrl(value: CellValue): string {
|
|
|
|
if (typeof value !== 'string') { return ''; }
|
2020-10-07 21:58:43 +00:00
|
|
|
const url = value.slice(value.lastIndexOf(' ') + 1);
|
|
|
|
try {
|
|
|
|
// Try to construct a valid URL
|
|
|
|
return (new URL(url)).toString();
|
|
|
|
} catch (e) {
|
|
|
|
// Not a valid URL, so try to prefix it with http
|
|
|
|
return 'http://' + url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssFieldClip = styled('div.field_clip', `
|
2021-02-09 10:12:53 +00:00
|
|
|
color: var(--grist-actual-cell-color, ${colors.lightGreen});
|
2020-10-07 21:58:43 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssLinkIcon = styled(icon, `
|
2021-02-09 10:12:53 +00:00
|
|
|
background-color: var(--grist-actual-cell-color, ${colors.lightGreen});
|
2020-10-07 21:58:43 +00:00
|
|
|
margin: -1px 2px 2px 0;
|
|
|
|
`);
|