2021-10-13 17:36:55 +00:00
|
|
|
import { DataRowModel } from 'app/client/models/DataRowModel';
|
|
|
|
import { ViewFieldRec } from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import { constructUrl } from 'app/client/models/gristUrlState';
|
2023-09-21 16:57:58 +00:00
|
|
|
import { testId, theme } from 'app/client/ui2018/cssVars';
|
2024-08-23 03:51:09 +00:00
|
|
|
import { cssIconSpanBackground, iconSpan } from 'app/client/ui2018/icons';
|
2023-08-04 11:32:53 +00:00
|
|
|
import { cssHoverIn, gristLink } from 'app/client/ui2018/links';
|
|
|
|
import { NTextBox } from 'app/client/widgets/NTextBox';
|
2021-10-13 17:36:55 +00:00
|
|
|
import { CellValue } from 'app/common/DocActions';
|
|
|
|
import { Computed, dom, styled } from 'grainjs';
|
2020-10-07 21:58:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2023-09-21 16:57:58 +00:00
|
|
|
super(field, {defaultTextColor: theme.link.toString()});
|
2020-10-07 21:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
const value = row.cells[this.field.colId()];
|
2021-10-13 17:36:55 +00:00
|
|
|
const url = Computed.create(null, (use) => constructUrl(use(value)));
|
2020-10-07 21:58:43 +00:00
|
|
|
return cssFieldClip(
|
2021-10-13 17:36:55 +00:00
|
|
|
dom.autoDispose(url),
|
2020-10-07 21:58:43 +00:00
|
|
|
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)), () =>
|
2021-10-13 17:36:55 +00:00
|
|
|
gristLink(url,
|
2024-08-23 03:51:09 +00:00
|
|
|
cssIconSpanBackground(
|
|
|
|
iconSpan("FieldLink", testId('tb-link-icon')),
|
2021-10-13 17:36:55 +00:00
|
|
|
dom.cls(cssHoverOnField.className),
|
|
|
|
),
|
|
|
|
testId('tb-link'),
|
|
|
|
),
|
2020-10-07 21:58:43 +00:00
|
|
|
),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssFieldClip = styled('div.field_clip', `
|
2023-09-21 16:57:58 +00:00
|
|
|
color: var(--grist-actual-cell-color, ${theme.link});
|
2020-10-07 21:58:43 +00:00
|
|
|
`);
|
|
|
|
|
2021-10-13 17:36:55 +00:00
|
|
|
const cssHoverOnField = cssHoverIn(cssFieldClip.className);
|