Skip to content

Commit b2db7e0

Browse files
committed
feature: enhance editor UI
1 parent 58b1d47 commit b2db7e0

File tree

16 files changed

+183
-47
lines changed

16 files changed

+183
-47
lines changed

src/TestSite.14/Views/home.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
</div>
2222
</div>
2323
</div>
24-
</div>
24+
</div>

src/TestSite.14/uSync/v14/Content/home.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<Published Culture="sv">true</Published>
1717
</Published>
1818
<Schedule />
19-
<Template Key="7d801d9a-ac91-44cc-8466-d8237b447e63">home</Template>
19+
<Template Key="335e55b1-1a8b-4d79-8099-09c62b555bda">home</Template>
2020
</Info>
2121
<Properties>
2222
<gridContent>

src/TestSite.14/uSync/v14/ContentTypes/home.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</Compositions>
2020
<DefaultTemplate>home</DefaultTemplate>
2121
<AllowedTemplates>
22-
<Template Key="7d801d9a-ac91-44cc-8466-d8237b447e63">home</Template>
22+
<Template Key="335e55b1-1a8b-4d79-8099-09c62b555bda">home</Template>
2323
</AllowedTemplates>
2424
</Info>
2525
<Structure />
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Template Key="7d801d9a-ac91-44cc-8466-d8237b447e63" Alias="home" Level="1">
3-
<Name>Home</Name>
2+
<Template Key="335e55b1-1a8b-4d79-8099-09c62b555bda" Alias="home" Level="1">
3+
<Name>home</Name>
44
<Parent />
55
</Template>

src/jcdcdev.Umbraco.ReadingTime.Client/src/api/schemas.gen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
export const $ReadingTimeResponse = {
4-
required: ['readingTime'],
4+
required: ['readingTime', 'updateDate'],
55
type: 'object',
66
properties: {
7+
updateDate: {
8+
type: 'string',
9+
format: 'date-time'
10+
},
711
readingTime: {
812
type: 'string'
913
}

src/jcdcdev.Umbraco.ReadingTime.Client/src/api/types.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
export type ReadingTimeResponse = {
4+
updateDate: string;
45
readingTime: string;
56
};
67

src/jcdcdev.Umbraco.ReadingTime.Client/src/context/reading-time.context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export class ReadingTimeContext extends UmbControllerBase {
1414
this.#repository = new ReadingTimeRepository(this);
1515
}
1616

17-
async getReadingTime(contentKey: string, dataTypeKey: string, culture: string = ""): Promise<UmbDataSourceResponse<ReadingTimeResponse>> {
17+
async getReadingTime(contentKey: string, dataTypeKey: string, culture?: string): Promise<UmbDataSourceResponse<ReadingTimeResponse>> {
1818
const query = {
1919
contentKey: contentKey,
2020
dataTypeKey: dataTypeKey,
21-
culture: culture
21+
culture: culture ?? ""
2222
};
2323
return await this.#repository.getReadingTime(query);
2424
}

src/jcdcdev.Umbraco.ReadingTime.Client/src/editors/reading-time.editor.ts

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,84 @@ import {UMB_PROPERTY_CONTEXT} from "@umbraco-cms/backoffice/property";
77
import {UMB_CONTENT_PROPERTY_CONTEXT} from "@umbraco-cms/backoffice/content";
88
import {ReadingTimeResponse} from "../api";
99
import {ReadingTimeContext} from "../context/reading-time.context.ts";
10-
import {PropertyValues} from "lit";
10+
import {css, nothing, PropertyValues} from "lit";
11+
import {UMB_ACTION_EVENT_CONTEXT} from "@umbraco-cms/backoffice/action";
12+
import {UmbRequestReloadStructureForEntityEvent} from "@umbraco-cms/backoffice/entity-action";
1113

1214
@customElement('reading-time-property-editor-ui')
1315
export default class MySuggestionsPropertyEditorUIElement extends UmbElementMixin(LitElement) implements UmbPropertyEditorUiElement {
16+
1417
@property({type: String})
1518
public value = "";
16-
@state()
17-
private _hideVariationWarning: boolean = false;
1819

1920
#readingTimeContext?: ReadingTimeContext;
2021

2122
@state()
22-
private initialised: boolean = false;
23+
private hideVariationWarning: boolean = false;
24+
@state()
25+
private loading: boolean = false;
2326
@state()
24-
private contentKey: string | null | undefined;
27+
private contentKey?: string;
2528
@state()
26-
private dataTypeKey: string | null | undefined;
29+
private dataTypeKey?: string;
2730
@state()
28-
private culture: string | null | undefined;
31+
private culture?: string;
2932
@state()
30-
private data: ReadingTimeResponse | null = null;
33+
private data?: ReadingTimeResponse;
3134
@state()
32-
private valid: boolean = false;
35+
private initialised: boolean = false
36+
static styles = [css`
37+
.alert {
38+
background-color: darkgoldenrod;
39+
padding: 5px;
40+
}
41+
42+
.icon-container {
43+
display: flex;
44+
align-items: center;
45+
}
46+
47+
.icon{
48+
margin-right: 5px;
49+
}
50+
`]
3351

3452
constructor() {
3553
super();
3654
this.#readingTimeContext = new ReadingTimeContext(this);
3755
this.consumeContext(UMB_ENTITY_CONTEXT, (context) => {
38-
this.contentKey = context.getUnique();
56+
this.contentKey = context.getUnique() ?? undefined;
3957
});
4058

4159
this.consumeContext(UMB_PROPERTY_CONTEXT, (context) => {
42-
this.culture = context.getVariantId()?.culture;
60+
this.culture = context.getVariantId()?.culture ?? undefined;
61+
});
62+
63+
this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (context) => {
64+
context.addEventListener(UmbRequestReloadStructureForEntityEvent.TYPE, (e) => {
65+
if (!this.initialised) {
66+
return;
67+
}
68+
this.loading = true;
69+
const interval = setInterval(async () => {
70+
if (!(this.contentKey && this.dataTypeKey)) {
71+
return;
72+
}
73+
74+
const response = await this.#readingTimeContext?.getReadingTime(this.contentKey, this.dataTypeKey, this.culture);
75+
if (!response || !response.data?.updateDate) {
76+
return;
77+
}
78+
79+
if (response.data.updateDate === this.data?.updateDate) {
80+
return;
81+
}
82+
83+
this.data = response.data;
84+
this.loading = false;
85+
clearInterval(interval);
86+
}, 2500);
87+
});
4388
});
4489

4590
this.consumeContext(UMB_CONTENT_PROPERTY_CONTEXT, (context) => {
@@ -51,52 +96,74 @@ export default class MySuggestionsPropertyEditorUIElement extends UmbElementMixi
5196

5297
@property({attribute: false})
5398
public set config(config: UmbPropertyEditorConfigCollection) {
54-
this._hideVariationWarning = config.getValueByAlias<boolean>("hideVariationWarning") ?? false;
99+
this.hideVariationWarning = config.getValueByAlias<boolean>("hideVariationWarning") ?? false;
55100
}
56101

57102
render() {
58-
if (!this.initialised) {
103+
if (this.loading) {
59104
return html
60105
`
61-
<div>Loading...</div>
106+
<uui-loader></uui-loader>
62107
`;
63108
}
64-
if (!this.valid) {
65-
const message = this.contentKey ? "No data available" : "Save and publish to calculate reading time";
109+
110+
if (!this.data) {
66111
return html
67112
`
68-
<div>${message}</div>
113+
<div>Save and publish to calculate reading time</div>
69114
`;
70115
}
71-
const message = this.data?.readingTime;
72-
const alert = this._hideVariationWarning || this.culture !== "" ? "" : html
73-
`
74-
<div>Reading time is calculated based on the content of the default culture</div>
75-
`;
76116

117+
const alert = this.renderVariationAlert();
77118
return html
78119
`
79120
<div>
80-
${message}
81121
${alert}
122+
${this.data.readingTime}
123+
</div>
124+
`;
125+
}
126+
127+
renderVariationAlert() {
128+
debugger
129+
if (this.hideVariationWarning || this.culture) {
130+
return nothing;
131+
}
132+
133+
return html
134+
`
135+
<div class="alert">
136+
<div class="icon-container">
137+
<uui-icon name="alert" class="icon"></uui-icon>
138+
<span>Language specific properties are not used in this calculation</span>
139+
</div>
82140
</div>
83141
`;
84142
}
85143

86144
protected updated(_changedProperties: PropertyValues) {
87-
if (this.contentKey && this.dataTypeKey && !this.initialised) {
88-
this.init();
145+
if (!this.initialised) {
146+
if (this.contentKey && this.dataTypeKey) {
147+
this.init();
148+
}
89149
}
90150
}
91151

92152
private async init() {
153+
if (this.initialised) {
154+
return;
155+
}
156+
157+
this.loading = true;
93158
const result = await this.#readingTimeContext?.getReadingTime(this.contentKey!, this.dataTypeKey!, this.culture!);
159+
this.loading = false;
160+
this.initialised = true;
161+
94162
if (!result) {
95163
return;
96164
}
97-
this.initialised = true;
98-
this.valid = result.data !== undefined;
99-
this.data = result.data!;
165+
166+
this.data = result.data;
100167
}
101168
}
102169

src/jcdcdev.Umbraco.ReadingTime/Core/Models/ReadingTimeDto.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class ReadingTimeDto
99
public Guid Key { get; init; }
1010
public int DataTypeId { get; init; }
1111
public Guid DataTypeKey { get; set; }
12+
public DateTime UpdateDate { get; set; }
1213

1314
public ReadingTimeVariantDto? Value(string? culture = null) => Data.FirstOrDefault(x => x?.Culture.InvariantEquals(culture) ?? false);
1415
}

src/jcdcdev.Umbraco.ReadingTime/Infrastructure/Migrations/0.3.1/RebuildDatabase.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,48 @@
22
using jcdcdev.Umbraco.ReadingTime.Infrastructure.Persistence;
33
using Microsoft.Extensions.Logging;
44
using Umbraco.Cms.Infrastructure.Migrations;
5+
using Umbraco.Cms.Infrastructure.Persistence;
56

67
namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;
78

89
public class RebuildDatabase : MigrationBase
910
{
10-
private readonly IReadingTimeService _readingTimeService;
11-
12-
public RebuildDatabase(IMigrationContext context, IReadingTimeService readingTimeService) : base(context)
11+
public RebuildDatabase(IMigrationContext context) : base(context)
1312
{
14-
_readingTimeService = readingTimeService;
1513
}
1614

1715
protected override void Migrate()
1816
{
1917
Logger.LogInformation("Rebuilding ReadingTime database");
2018
if (TableExists(Constants.TableName))
2119
{
22-
Delete.ForeignKey("FK_jcdcdevReadingTime_umbracoNode_uniqueId").OnTable(Constants.TableName).Do();
20+
// Check if foreign key exists
21+
var fak = "FK_jcdcdevReadingTime_umbracoNode_uniqueId";
22+
var tableName = Constants.TableName;
23+
if (ConstraintExists(Context.Database, tableName, fak))
24+
{
25+
Delete.ForeignKey(fak).OnTable(Constants.TableName).Do();
26+
}
27+
2328
Delete.Table(Constants.TableName).Do();
2429
}
2530

2631
Create.Table<ReadingTimePoco>().Do();
32+
}
33+
34+
private static bool ConstraintExists(IUmbracoDatabase database, string tableName, string key)
35+
{
36+
string sql;
37+
if (database.SqlContext.DatabaseType == NPoco.DatabaseType.SQLite)
38+
{
39+
sql = $"SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = '{key}' AND tbl_name = '{tableName}'";
40+
}
41+
else
42+
{
43+
sql = $"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = '{key}' AND TABLE_NAME = '{tableName}'";
44+
}
2745

28-
_readingTimeService.ScanAll();
46+
var count = database.ExecuteScalar<int>(sql);
47+
return count > 0;
2948
}
3049
}

0 commit comments

Comments
 (0)