Skip to content

Commit

Permalink
feat: sort element templates by name
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhnm committed Jul 29, 2024
1 parent 32c3844 commit 1e53018
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/miranum-modeler/src/adapter/out/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class VsCodeBpmnWebviewAdapter implements BpmnUiOutPort {

async setElementTemplates(
editorId: string,
elementTemplates: string[],
elementTemplates: any[],
): Promise<boolean> {
const elementTemplatesQuery = new ElementTemplatesQuery(elementTemplates);
return postMessage(editorId, elementTemplatesQuery);
Expand Down
2 changes: 1 addition & 1 deletion apps/miranum-modeler/src/application/ports/out.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface BpmnUiOutPort extends EditorComponent {
* @returns true if the message was sent successfully, false otherwise
* @throws {Error} if the editorId does not match the active editor
*/
setElementTemplates(editorId: string, elementTemplates: string[]): Promise<boolean>;
setElementTemplates(editorId: string, elementTemplates: any[]): Promise<boolean>;

/**
* Sends the form keys to the active webview.
Expand Down
24 changes: 17 additions & 7 deletions apps/miranum-modeler/src/application/useCases/displayEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ abstract class GetArtifact {
protected abstract readArtifacts(
artifacts: string[],
extension?: string,
): Promise<string[]>;
): Promise<string[] | any[]>;

private async getMiranumConfig(documentPath: string): Promise<string | undefined> {
try {
Expand Down Expand Up @@ -437,11 +437,14 @@ export class SetElementTemplatesUseCase
.join("/");

const artifacts = (await this.getArtifacts(documentDir))[0];
const elementTemplates = (await this.readArtifacts(artifacts)).flat();
const sortedElementTemplates =
this.sortElementTemplatesByName(elementTemplates);

if (
await this.bpmnUiOutPort.setElementTemplates(
editorId,
await this.readArtifacts(artifacts),
sortedElementTemplates,
)
) {
this.logMessageOutPort.info(
Expand All @@ -458,15 +461,22 @@ export class SetElementTemplatesUseCase
}
}

protected async readArtifacts(
artifacts: string[],
extension?: string,
): Promise<string[]> {
protected async readArtifacts(artifacts: string[]): Promise<any[]> {
return Promise.all(
artifacts.map(async (artifact) => this.fileSystemOutPort.readFile(artifact)),
artifacts.map(async (artifact) =>
JSON.parse(await this.fileSystemOutPort.readFile(artifact)),
),
);
}

private sortElementTemplatesByName(elementTemplates: any[]): any[] {
return elementTemplates.sort((a, b) => {
const aName = a.name as string;
const bName = b.name as string;
return aName.localeCompare(bName);
});
}

private handleError(error: Error): boolean {
this.logMessageOutPort.error(error as Error);
return false;
Expand Down
4 changes: 2 additions & 2 deletions libs/vscode/miranum-vscode-webview/src/lib/miranum-modeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export class FormKeysQuery extends Query {
export class ElementTemplatesQuery extends Query {
public readonly elementTemplates: JSON[];

constructor(elementTemplates: string[]) {
constructor(elementTemplates: any[]) {
super("ElementTemplatesQuery");
this.elementTemplates = elementTemplates.map((it) => JSON.parse(it)).flat();
this.elementTemplates = elementTemplates;
}
}

Expand Down

0 comments on commit 1e53018

Please sign in to comment.