Skip to content

Commit 950ce0f

Browse files
committed
align code across content workspaces
1 parent ec67a1e commit 950ce0f

File tree

12 files changed

+183
-77
lines changed

12 files changed

+183
-77
lines changed

src/packages/block/block-type/workspace/block-type-workspace.context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
UmbSubmittableWorkspaceContextBase,
1212
UmbInvariantWorkspacePropertyDatasetContext,
1313
UmbWorkspaceIsNewRedirectController,
14+
UmbWorkspaceIsNewRedirectControllerAlias,
1415
} from '@umbraco-cms/backoffice/workspace';
1516
import { UmbObjectState, appendToFrozenArray } from '@umbraco-cms/backoffice/observable-api';
1617
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
@@ -70,7 +71,7 @@ export class UmbBlockTypeWorkspaceContext<BlockTypeData extends UmbBlockTypeWith
7071
protected override resetState() {
7172
super.resetState();
7273
this.#data.setValue(undefined);
73-
this.removeUmbControllerByAlias('isNewRedirectController');
74+
this.removeUmbControllerByAlias(UmbWorkspaceIsNewRedirectControllerAlias);
7475
}
7576

7677
createPropertyDatasetContext(host: UmbControllerHost): UmbPropertyDatasetContext {

src/packages/block/block/workspace/block-workspace.context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type UmbRoutableWorkspaceContext,
77
UmbWorkspaceIsNewRedirectController,
88
type ManifestWorkspace,
9+
UmbWorkspaceIsNewRedirectControllerAlias,
910
} from '@umbraco-cms/backoffice/workspace';
1011
import {
1112
UmbBooleanState,
@@ -205,7 +206,7 @@ export class UmbBlockWorkspaceContext<LayoutDataType extends UmbBlockLayoutBaseM
205206
this.#initialSettings = undefined;
206207
this.content.reset();
207208
this.settings.reset();
208-
this.removeUmbControllerByAlias('isNewRedirectController');
209+
this.removeUmbControllerByAlias(UmbWorkspaceIsNewRedirectControllerAlias);
209210
}
210211

211212
async load(unique: string) {

src/packages/core/property-type/workspace/property-type-workspace.context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
UmbSubmittableWorkspaceContextBase,
1111
UmbInvariantWorkspacePropertyDatasetContext,
1212
UmbWorkspaceIsNewRedirectController,
13+
UmbWorkspaceIsNewRedirectControllerAlias,
1314
} from '@umbraco-cms/backoffice/workspace';
1415
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
1516
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
@@ -87,7 +88,7 @@ export class UmbPropertyTypeWorkspaceContext<PropertyTypeData extends UmbPropert
8788
protected override resetState() {
8889
super.resetState();
8990
this.#data.setValue(undefined);
90-
this.removeUmbControllerByAlias('isNewRedirectController');
91+
this.removeUmbControllerByAlias(UmbWorkspaceIsNewRedirectControllerAlias);
9192
this.removeUmbControllerByAlias('observePropertyTypeData');
9293
}
9394

src/packages/core/workspace/controllers/workspace-is-new-redirect.controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
44
import type { UmbRouterSlotElement } from '@umbraco-cms/backoffice/router';
55
import { ensurePathEndsWithSlash, umbUrlPatternToString } from '@umbraco-cms/backoffice/utils';
66

7+
export const UmbWorkspaceIsNewRedirectControllerAlias = Symbol('IsNewRedirectControllerAlias');
8+
79
/**
810
* Observe the workspace context to see if the entity is new or not.
911
* If that changes redirect to the edit url.
@@ -19,7 +21,7 @@ export class UmbWorkspaceIsNewRedirectController extends UmbControllerBase {
1921
workspaceContext: UmbSubmittableWorkspaceContextBase<unknown>,
2022
router: UmbRouterSlotElement,
2123
) {
22-
super(host, 'isNewRedirectController');
24+
super(host, UmbWorkspaceIsNewRedirectControllerAlias);
2325

2426
// Navigate to edit route when language is created:
2527
this.observe(workspaceContext.isNew, (isNew) => {
@@ -37,5 +39,7 @@ export class UmbWorkspaceIsNewRedirectController extends UmbControllerBase {
3739
}
3840
}
3941
});
42+
43+
// TODO: If workspace route changes cause of other reasons then this controller should be destroyed.
4044
}
4145
}

src/packages/documents/document-blueprints/entity.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ export const UMB_DOCUMENT_BLUEPRINT_FOLDER_ENTITY_TYPE = 'document-blueprint-fol
55
export type UmbDocumentBlueprintRootEntityType = typeof UMB_DOCUMENT_BLUEPRINT_ROOT_ENTITY_TYPE;
66
export type UmbDocumentBlueprintEntityType = typeof UMB_DOCUMENT_BLUEPRINT_ENTITY_TYPE;
77
export type UmbDocumentBlueprintFolderEntityType = typeof UMB_DOCUMENT_BLUEPRINT_FOLDER_ENTITY_TYPE;
8+
9+
export type UmbDocumentBlueprintEntityTypeUnion =
10+
| UmbDocumentBlueprintRootEntityType
11+
| UmbDocumentBlueprintEntityType
12+
| UmbDocumentBlueprintFolderEntityType;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { UMB_DOCUMENTS_SECTION_PATHNAME } from '../section/paths.js';
2+
import { UMB_DOCUMENT_BLUEPRINT_ENTITY_TYPE, type UmbDocumentBlueprintEntityTypeUnion } from './entity.js';
3+
import type { UmbEntityUnique } from '@umbraco-cms/backoffice/entity';
4+
import { UmbPathPattern } from '@umbraco-cms/backoffice/router';
5+
import { UMB_WORKSPACE_PATH_PATTERN } from '@umbraco-cms/backoffice/workspace';
6+
7+
export const UMB_DOCUMENT_BLUEPRINT_WORKSPACE_PATH = UMB_WORKSPACE_PATH_PATTERN.generateAbsolute({
8+
sectionName: UMB_DOCUMENTS_SECTION_PATHNAME,
9+
entityType: UMB_DOCUMENT_BLUEPRINT_ENTITY_TYPE,
10+
});
11+
12+
export const UMB_CREATE_DOCUMENT_BLUEPRINT_WORKSPACE_PATH_PATTERN = new UmbPathPattern<{
13+
parentEntityType: UmbDocumentBlueprintEntityTypeUnion;
14+
parentUnique?: UmbEntityUnique;
15+
documentTypeUnique: string;
16+
}>('create/parent/:parentEntityType/:parentUnique/:documentTypeUnique', UMB_DOCUMENT_BLUEPRINT_WORKSPACE_PATH);
17+
18+
export const UMB_EDIT_BLUEPRINT_DOCUMENT_WORKSPACE_PATH_PATTERN = new UmbPathPattern<{ unique: string }>(
19+
'edit/:unique',
20+
UMB_DOCUMENT_BLUEPRINT_WORKSPACE_PATH,
21+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { UmbDocumentBlueprintVariantOptionModel } from './types.js';
2+
3+
type VariantType = UmbDocumentBlueprintVariantOptionModel;
4+
5+
export const sortVariants = (a: VariantType, b: VariantType) => {
6+
const compareDefault = (a: VariantType, b: VariantType) =>
7+
(a.language?.isDefault ? -1 : 1) - (b.language?.isDefault ? -1 : 1);
8+
9+
// Make sure mandatory variants goes on top.
10+
const compareMandatory = (a: VariantType, b: VariantType) =>
11+
(a.language?.isMandatory ? -1 : 1) - (b.language?.isMandatory ? -1 : 1);
12+
13+
const compareName = (a: VariantType, b: VariantType) => a.variant?.name.localeCompare(b.variant?.name || '') || 99;
14+
15+
return compareDefault(a, b) || compareMandatory(a, b) || compareName(a, b);
16+
};

src/packages/documents/document-blueprints/workspace/document-blueprint-workspace.context.ts

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
UmbDocumentBlueprintVariantModel,
88
UmbDocumentBlueprintVariantOptionModel,
99
} from '../types.js';
10+
import { sortVariants } from '../utils.js';
11+
import { UMB_CREATE_DOCUMENT_BLUEPRINT_WORKSPACE_PATH_PATTERN } from '../paths.js';
1012
import { UMB_DOCUMENT_BLUEPRINT_WORKSPACE_ALIAS } from './manifests.js';
1113
import {
1214
appendToFrozenArray,
@@ -17,6 +19,7 @@ import {
1719
import {
1820
UmbSubmittableWorkspaceContextBase,
1921
UmbWorkspaceIsNewRedirectController,
22+
UmbWorkspaceIsNewRedirectControllerAlias,
2023
UmbWorkspaceSplitViewManager,
2124
} from '@umbraco-cms/backoffice/workspace';
2225
import { UmbContentTypeStructureManager } from '@umbraco-cms/backoffice/content-type';
@@ -35,8 +38,15 @@ import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
3538
import type { UmbLanguageDetailModel } from '@umbraco-cms/backoffice/language';
3639
import { UmbContentWorkspaceDataManager, type UmbContentWorkspaceContext } from '@umbraco-cms/backoffice/content';
3740
import { UmbReadOnlyVariantStateManager } from '@umbraco-cms/backoffice/utils';
38-
import { UMB_DOCUMENT_DETAIL_MODEL_VARIANT_SCAFFOLD } from '@umbraco-cms/backoffice/document';
41+
import {
42+
UMB_DOCUMENT_COLLECTION_ALIAS,
43+
UMB_DOCUMENT_DETAIL_MODEL_VARIANT_SCAFFOLD,
44+
UMB_EDIT_DOCUMENT_WORKSPACE_PATH_PATTERN,
45+
} from '@umbraco-cms/backoffice/document';
3946
import { UmbDataTypeItemRepositoryManager } from '@umbraco-cms/backoffice/data-type';
47+
import { Observable, map } from '@umbraco-cms/backoffice/external/rxjs';
48+
import { UmbEntityContext, type UmbEntityModel } from '@umbraco-cms/backoffice/entity';
49+
import { UMB_SETTINGS_SECTION_PATH } from '@umbraco-cms/backoffice/settings';
4050

4151
type EntityModel = UmbDocumentBlueprintDetailModel;
4252

@@ -119,27 +129,46 @@ export class UmbDocumentBlueprintWorkspaceContext
119129
}
120130
return [];
121131
},
122-
);
132+
).pipe(map((results) => results.sort(sortVariants)));
133+
134+
// TODO: this should be set up for all entity workspace contexts in a base class
135+
#entityContext = new UmbEntityContext(this);
123136

124137
constructor(host: UmbControllerHost) {
125138
super(host, UMB_DOCUMENT_BLUEPRINT_WORKSPACE_ALIAS);
126139

127-
this.observe(this.contentTypeUnique, (unique) => this.structure.loadType(unique));
128-
129-
this.observe(this.variesByCulture, (varies) => {
130-
this.#data.setVariesByCulture(varies);
131-
this.#variesByCulture = varies;
132-
});
133-
this.observe(this.variesBySegment, (varies) => {
134-
this.#data.setVariesBySegment(varies);
135-
this.#variesBySegment = varies;
136-
});
137-
this.observe(this.varies, (varies) => (this.#varies = varies));
138-
139-
this.observe(this.structure.contentTypeDataTypeUniques, (dataTypeUniques: Array<string>) => {
140-
this.#dataTypeItemManager.setUniques(dataTypeUniques);
141-
});
142-
140+
this.observe(this.contentTypeUnique, (unique) => this.structure.loadType(unique), null);
141+
this.observe(
142+
this.varies,
143+
(varies) => {
144+
this.#data.setVaries(varies);
145+
this.#varies = varies;
146+
},
147+
null,
148+
);
149+
this.observe(
150+
this.variesByCulture,
151+
(varies) => {
152+
this.#data.setVariesByCulture(varies);
153+
this.#variesByCulture = varies;
154+
},
155+
null,
156+
);
157+
this.observe(
158+
this.variesBySegment,
159+
(varies) => {
160+
this.#data.setVariesBySegment(varies);
161+
this.#variesBySegment = varies;
162+
},
163+
null,
164+
);
165+
this.observe(
166+
this.structure.contentTypeDataTypeUniques,
167+
(dataTypeUniques: Array<string>) => {
168+
this.#dataTypeItemManager.setUniques(dataTypeUniques);
169+
},
170+
null,
171+
);
143172
this.observe(this.#dataTypeItemManager.items, (dataTypes) => {
144173
// Make a map of the data type unique and editorAlias:
145174
this.#dataTypeSchemaAliasMap = new Map(
@@ -148,14 +177,15 @@ export class UmbDocumentBlueprintWorkspaceContext
148177
}),
149178
);
150179
});
180+
151181
this.loadLanguages();
152182

153183
this.routes.setRoutes([
154184
{
155-
path: 'create/parent/:entityType/:parentUnique/:documentTypeUnique',
185+
path: UMB_CREATE_DOCUMENT_BLUEPRINT_WORKSPACE_PATH_PATTERN.toString(),
156186
component: () => import('./document-blueprint-workspace-editor.element.js'),
157187
setup: async (_component, info) => {
158-
const parentEntityType = info.match.params.entityType;
188+
const parentEntityType = info.match.params.parentEntityType;
159189
const parentUnique = info.match.params.parentUnique === 'null' ? null : info.match.params.parentUnique;
160190
const documentTypeUnique = info.match.params.documentTypeUnique;
161191
this.create({ entityType: parentEntityType, unique: parentUnique }, documentTypeUnique);
@@ -168,10 +198,10 @@ export class UmbDocumentBlueprintWorkspaceContext
168198
},
169199
},
170200
{
171-
path: 'edit/:unique',
201+
path: UMB_EDIT_DOCUMENT_WORKSPACE_PATH_PATTERN.toString(),
172202
component: () => import('./document-blueprint-workspace-editor.element.js'),
173203
setup: (_component, info) => {
174-
this.removeUmbControllerByAlias('isNewRedirectController');
204+
this.removeUmbControllerByAlias(UmbWorkspaceIsNewRedirectControllerAlias);
175205
const unique = info.match.params.unique;
176206
this.load(unique);
177207
},
@@ -181,8 +211,7 @@ export class UmbDocumentBlueprintWorkspaceContext
181211

182212
override resetState() {
183213
super.resetState();
184-
this.#data.setPersisted(undefined);
185-
this.#data.setCurrent(undefined);
214+
this.#data.clear();
186215
}
187216

188217
async loadLanguages() {
@@ -194,42 +223,50 @@ export class UmbDocumentBlueprintWorkspaceContext
194223
async load(unique: string) {
195224
this.resetState();
196225
this.#getDataPromise = this.repository.requestByUnique(unique);
197-
const { data, asObservable } = await this.repository.requestByUnique(unique);
226+
type GetDataType = Awaited<ReturnType<UmbDocumentBlueprintDetailRepository['requestByUnique']>>;
227+
const { data, asObservable } = (await this.#getDataPromise) as GetDataType;
198228

199229
if (data) {
230+
this.#entityContext.setEntityType(UMB_DOCUMENT_BLUEPRINT_ENTITY_TYPE);
231+
this.#entityContext.setUnique(unique);
200232
this.setIsNew(false);
201233
this.#data.setPersisted(data);
202234
this.#data.setCurrent(data);
203235
}
204236

205-
if (asObservable) {
206-
this.observe(asObservable(), (entity) => this.#onStoreChange(entity), 'UmbDocumentBlueprintStoreObserver');
207-
}
237+
this.observe(asObservable(), (entity) => this.#onStoreChange(entity), 'UmbDocumentBlueprintStoreObserver');
208238
}
209239

210240
#onStoreChange(entity: EntityModel | undefined) {
211241
if (!entity) {
212242
//TODO: This solution is alright for now. But reconsider when we introduce signal-r
213-
history.pushState(null, '', 'section/document-blueprint');
243+
history.pushState(null, '', UMB_SETTINGS_SECTION_PATH);
214244
}
215245
}
216246

217-
async create(parent: { entityType: string; unique: string | null }, documentTypeUnique: string) {
247+
async create(parent: UmbEntityModel, documentTypeUnique: string) {
218248
this.resetState();
219249
this.#parent.setValue(parent);
220250

221-
const { data } = await this.repository.createScaffold({
251+
this.#getDataPromise = this.repository.createScaffold({
222252
documentType: { unique: documentTypeUnique, collection: null },
223253
});
224254

255+
const { data } = await this.#getDataPromise;
225256
if (!data) return undefined;
226257

258+
this.#entityContext.setEntityType(UMB_DOCUMENT_BLUEPRINT_ENTITY_TYPE);
259+
this.#entityContext.setUnique(data.unique);
227260
this.setIsNew(true);
228261
this.#data.setPersisted(undefined);
229262
this.#data.setCurrent(data);
230263
return data;
231264
}
232265

266+
getCollectionAlias() {
267+
return UMB_DOCUMENT_COLLECTION_ALIAS;
268+
}
269+
233270
getData() {
234271
return this.#data.getCurrent();
235272
}
@@ -294,7 +331,10 @@ export class UmbDocumentBlueprintWorkspaceContext
294331
* @returns {Promise<Observable<ReturnType | undefined> | undefined>}
295332
* @description Get an Observable for the value of this property.
296333
*/
297-
async propertyValueByAlias<PropertyValueType = unknown>(propertyAlias: string, variantId?: UmbVariantId) {
334+
async propertyValueByAlias<PropertyValueType = unknown>(
335+
propertyAlias: string,
336+
variantId?: UmbVariantId,
337+
): Promise<Observable<PropertyValueType | undefined> | undefined> {
298338
return this.#data.createObservablePartOfCurrent(
299339
(data) =>
300340
data?.values?.find((x) => x?.alias === propertyAlias && (variantId ? variantId.compare(x as any) : true))

src/packages/documents/document-types/workspace/document-type-workspace.context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import {
1919
UmbSubmittableWorkspaceContextBase,
2020
UmbWorkspaceIsNewRedirectController,
21+
UmbWorkspaceIsNewRedirectControllerAlias,
2122
} from '@umbraco-cms/backoffice/workspace';
2223
import { UmbTemplateDetailRepository } from '@umbraco-cms/backoffice/template';
2324
import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action';
@@ -132,7 +133,7 @@ export class UmbDocumentTypeWorkspaceContext
132133
path: UMB_EDIT_DOCUMENT_TYPE_WORKSPACE_PATH_PATTERN.toString(),
133134
component: UmbDocumentTypeWorkspaceEditorElement,
134135
setup: (_component, info) => {
135-
this.removeUmbControllerByAlias('isNewRedirectController');
136+
this.removeUmbControllerByAlias(UmbWorkspaceIsNewRedirectControllerAlias);
136137
const unique = info.match.params.unique;
137138
this.load(unique);
138139
},

0 commit comments

Comments
 (0)