Skip to content

Commit 904d20c

Browse files
sadaf895sleidig
andauthored
fix: display long texts (e.g. note texts) with linebreaks in the table (#2568)
closes #2547 --------- Co-authored-by: Sebastian Leidig <[email protected]>
1 parent c06a23a commit 904d20c

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
2+
3+
import { DisplayLongTextComponent } from "./display-long-text.component";
4+
import { Entity } from "app/core/entity/model/entity";
5+
6+
describe("DisplayLongTextComponent", () => {
7+
let component: DisplayLongTextComponent;
8+
let fixture: ComponentFixture<DisplayLongTextComponent>;
9+
10+
beforeEach(async () => {
11+
await TestBed.configureTestingModule({
12+
imports: [DisplayLongTextComponent],
13+
}).compileComponents();
14+
});
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(DisplayLongTextComponent);
18+
component = fixture.componentInstance;
19+
component.entity = new Entity();
20+
component.id = "text";
21+
component.value = "this is some long text abcde\nefgh";
22+
fixture.detectChanges();
23+
});
24+
25+
it("should create", () => {
26+
expect(component).toBeTruthy();
27+
});
28+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Component, Input, OnInit } from "@angular/core";
2+
import { ViewDirective } from "app/core/entity/default-datatype/view.directive";
3+
import { DynamicComponent } from "app/core/config/dynamic-components/dynamic-component.decorator";
4+
5+
/**
6+
* Config for details of how a long-text field should be displayed.
7+
* (define as "additional" on the entity field)
8+
*/
9+
export interface LongTextFieldConfig {
10+
/** Maximum number of lines to show */
11+
maxLines?: number;
12+
13+
/** Maximum number of characters to show */
14+
maxCharacters?: number;
15+
}
16+
17+
@DynamicComponent("DisplayLongText")
18+
@Component({
19+
selector: "app-display-long-text",
20+
template: `<div [innerHTML]="formattedValue"></div>`,
21+
standalone: true,
22+
})
23+
export class DisplayLongTextComponent
24+
extends ViewDirective<string, LongTextFieldConfig>
25+
implements OnInit
26+
{
27+
@Input() declare config: LongTextFieldConfig;
28+
29+
formattedValue: string = "";
30+
31+
ngOnInit(): void {
32+
if (this.value === undefined) {
33+
this.formattedValue = "";
34+
return;
35+
}
36+
37+
const maxLines = this.config?.maxLines ?? 3;
38+
const maxCharacters = this.config?.maxCharacters ?? 250;
39+
const text =
40+
this.value.length > maxCharacters
41+
? this.value.slice(0, maxCharacters) + "..."
42+
: this.value;
43+
const lines = text.split("\n");
44+
this.formattedValue =
45+
lines.length > maxLines
46+
? lines.slice(0, maxLines).join("<br>") + "..."
47+
: lines.join("<br>");
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { applicationConfig, Meta, StoryFn } from "@storybook/angular";
2+
import { StorybookBaseModule } from "app/utils/storybook-base.module";
3+
import { DisplayLongTextComponent } from "./display-long-text.component";
4+
import { importProvidersFrom } from "@angular/core";
5+
6+
export default {
7+
title: "Core/Entities/Properties/string/DisplayLongText",
8+
component: DisplayLongTextComponent,
9+
decorators: [
10+
applicationConfig({
11+
providers: [importProvidersFrom(StorybookBaseModule)],
12+
}),
13+
],
14+
} as Meta;
15+
16+
const Template: StoryFn<DisplayLongTextComponent> = (
17+
args: DisplayLongTextComponent,
18+
) => ({
19+
props: args,
20+
});
21+
22+
export const Basic = Template.bind({});
23+
Basic.args = {
24+
value: `Lorem ipsum dolor sit amet,
25+
26+
consectetur adipiscing elit. Nullam nec
27+
purus nec nunc ultricies ultricies.
28+
`,
29+
};

src/app/core/basic-datatypes/string/long-text.datatype.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export class LongTextDatatype extends StringDatatype {
99
static override dataType = "long-text";
1010
static override label: string = $localize`:datatype-label:text (long)`;
1111

12+
override viewComponent = "DisplayLongText";
1213
override editComponent = "EditLongText";
1314
}

src/app/core/core-components.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ export const coreComponents: ComponentTuple[] = [
127127
"./basic-datatypes/string/display-text/display-text.component"
128128
).then((c) => c.DisplayTextComponent),
129129
],
130+
[
131+
"DisplayLongText",
132+
() =>
133+
import(
134+
"./basic-datatypes/string/display-long-text/display-long-text.component"
135+
).then((c) => c.DisplayLongTextComponent),
136+
],
137+
130138
[
131139
"DisplayDate",
132140
() =>

0 commit comments

Comments
 (0)