2021-10-13 17:36:55 +00:00
|
|
|
import { fromKoSave } from 'app/client/lib/fromKoSave';
|
|
|
|
import { findLinks } from 'app/client/lib/textUtils';
|
|
|
|
import { DataRowModel } from 'app/client/models/DataRowModel';
|
|
|
|
import { ViewFieldRec } from 'app/client/models/entities/ViewFieldRec';
|
2022-08-08 13:32:50 +00:00
|
|
|
import { cssRow } from 'app/client/ui/RightPanelStyles';
|
2022-10-14 10:07:19 +00:00
|
|
|
import { alignmentSelect, cssButtonSelect, makeButtonSelect } from 'app/client/ui2018/buttonSelect';
|
2021-10-13 17:36:55 +00:00
|
|
|
import { colors, testId } from 'app/client/ui2018/cssVars';
|
|
|
|
import { cssIconBackground, icon } from 'app/client/ui2018/icons';
|
|
|
|
import { gristLink } from 'app/client/ui2018/links';
|
|
|
|
import { NewAbstractWidget, Options } from 'app/client/widgets/NewAbstractWidget';
|
2022-10-14 10:07:19 +00:00
|
|
|
import { Computed, dom, DomArg, DomContents, fromKo, Observable, styled } from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TextBox - The most basic widget for displaying text information.
|
|
|
|
*/
|
|
|
|
export class NTextBox extends NewAbstractWidget {
|
|
|
|
protected alignment: Observable<string>;
|
|
|
|
protected wrapping: Observable<boolean>;
|
|
|
|
|
2021-03-02 12:27:08 +00:00
|
|
|
constructor(field: ViewFieldRec, options: Options = {}) {
|
|
|
|
super(field, options);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
this.alignment = fromKo(this.options.prop('alignment'));
|
|
|
|
this.wrapping = fromKo(this.field.wrap);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
this.autoDispose(this.wrapping.addListener(() => {
|
|
|
|
this.field.viewSection().events.trigger('rowHeightChange');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:58:43 +00:00
|
|
|
public buildConfigDom(): DomContents {
|
2022-10-14 10:07:19 +00:00
|
|
|
const toggle = () => {
|
|
|
|
const newValue = !this.field.config.wrap.peek();
|
|
|
|
this.field.config.wrap.setAndSave(newValue).catch(reportError);
|
|
|
|
};
|
|
|
|
const options = this.field.config.options;
|
|
|
|
// Some options might be disabled, as more than one column is selected.
|
|
|
|
// Prop observable is owned by the options object.
|
|
|
|
const alignmentDisabled = Computed.create(this, use => use(options.disabled('alignment')));
|
|
|
|
const wrapDisabled = Computed.create(this, (use) => use(options.disabled('wrap')));
|
2020-10-07 21:58:43 +00:00
|
|
|
return [
|
2020-10-02 15:10:00 +00:00
|
|
|
cssRow(
|
2022-10-14 10:07:19 +00:00
|
|
|
alignmentSelect(
|
|
|
|
fromKoSave(this.field.config.alignment),
|
|
|
|
cssButtonSelect.cls('-disabled', alignmentDisabled),
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom('div', {style: 'margin-left: 8px;'},
|
2022-10-14 10:07:19 +00:00
|
|
|
makeButtonSelect(
|
|
|
|
fromKo(this.field.config.wrap),
|
|
|
|
[{value: true, icon: 'Wrap'}],
|
|
|
|
toggle,
|
|
|
|
cssButtonSelect.cls('-disabled', wrapDisabled),
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('tb-wrap-text')
|
|
|
|
)
|
|
|
|
)
|
2020-10-07 21:58:43 +00:00
|
|
|
];
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
const value = row.cells[this.field.colId.peek()];
|
|
|
|
return dom('div.field_clip',
|
|
|
|
dom.style('text-align', this.alignment),
|
|
|
|
dom.cls('text_wrapping', this.wrapping),
|
2022-01-13 10:04:56 +00:00
|
|
|
dom.domComputed((use) => use(row._isAddRow) ? null : makeLinks(use(this.valueFormatter).formatAny(use(value))))
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 17:36:55 +00:00
|
|
|
|
|
|
|
function makeLinks(text: string) {
|
|
|
|
try {
|
|
|
|
const domElements: DomArg[] = [];
|
|
|
|
for (const {value, isLink} of findLinks(text)) {
|
|
|
|
if (isLink) {
|
|
|
|
// Wrap link with a span to provide hover on and to override wrapping.
|
|
|
|
domElements.push(cssMaybeWrap(
|
|
|
|
gristLink(value,
|
|
|
|
cssIconBackground(
|
|
|
|
icon("FieldLink", testId('tb-link-icon')),
|
|
|
|
dom.cls(cssHoverInText.className),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
linkColor(value),
|
|
|
|
testId("text-link")
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
domElements.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return domElements;
|
|
|
|
} catch(ex) {
|
|
|
|
// In case when something went wrong, simply log and return original text, as showing
|
|
|
|
// links is not that important.
|
|
|
|
console.warn("makeLinks failed", ex);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For links we want to break all the parts, not only words.
|
|
|
|
const cssMaybeWrap = styled('span', `
|
|
|
|
white-space: inherit;
|
|
|
|
.text_wrapping & {
|
|
|
|
word-break: break-all;
|
|
|
|
white-space: pre-wrap;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
// A gentle transition effect on hover in, and the same effect on hover out with a little delay.
|
|
|
|
export const cssHoverIn = (parentClass: string) => styled('span', `
|
|
|
|
--icon-color: var(--grist-actual-cell-color, ${colors.lightGreen});
|
|
|
|
margin: -1px 2px 2px 0;
|
|
|
|
border-radius: 3px;
|
|
|
|
transition-property: background-color;
|
|
|
|
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
|
|
|
|
transition-duration: 150ms;
|
|
|
|
transition-delay: 90ms;
|
|
|
|
.${parentClass}:hover & {
|
|
|
|
--icon-background: ${colors.lightGreen};
|
|
|
|
--icon-color: white;
|
|
|
|
transition-duration: 80ms;
|
|
|
|
transition-delay: 0ms;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssHoverInText = cssHoverIn(cssMaybeWrap.className);
|
|
|
|
|
|
|
|
const linkColor = styled('span', `
|
|
|
|
color: var(--grist-actual-cell-color, ${colors.lightGreen});;
|
|
|
|
`);
|