From b41f860b0bc231b25a8a86921ecf598c4aa9021d Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Thu, 31 Aug 2023 16:52:48 +0530 Subject: [PATCH 1/8] Introducing feature flag & consuming prevoew-engine --- package.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/package.json b/package.json index 430ea276..42a8c2c7 100644 --- a/package.json +++ b/package.json @@ -433,6 +433,11 @@ "type": "boolean", "markdownDescription": "Enable multiple file view in Visual Studio Code (Web extension only)", "default": true + }, + "powerPlatform.experimental.enableCSRPreviewFeature": { + "type": "boolean", + "markdownDescription": "Enable CSR preview in Visual Studio Code (Desktop extension only)", + "default": false } } }, @@ -967,6 +972,7 @@ "yargs": "^16.2.0" }, "dependencies": { + "@maker-studio/powerportals-preview-engine": "^3.7.20", "@microsoft/generator-powerpages": "1.21.19", "@types/jwt-decode": "2.2.0", "@types/node-fetch": "^2.6.2", From e96331a60394089e7da041e7e959ab9b80f5f776 Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Mon, 11 Sep 2023 12:41:33 +0530 Subject: [PATCH 2/8] PreviewEngineContext class --- package.json | 3 +- src/client/PreviewEngineContext.ts | 935 +++++++++++++++++++++++++++++ 2 files changed, 937 insertions(+), 1 deletion(-) create mode 100644 src/client/PreviewEngineContext.ts diff --git a/package.json b/package.json index 42a8c2c7..5bf9977b 100644 --- a/package.json +++ b/package.json @@ -972,8 +972,9 @@ "yargs": "^16.2.0" }, "dependencies": { - "@maker-studio/powerportals-preview-engine": "^3.7.20", + "@maker-studio/powerportals-preview-engine": "^3.7.24", "@microsoft/generator-powerpages": "1.21.19", + "@types/js-yaml": "^4.0.5", "@types/jwt-decode": "2.2.0", "@types/node-fetch": "^2.6.2", "@vscode/extension-telemetry": "^0.6.2", diff --git a/src/client/PreviewEngineContext.ts b/src/client/PreviewEngineContext.ts new file mode 100644 index 00000000..23102c55 --- /dev/null +++ b/src/client/PreviewEngineContext.ts @@ -0,0 +1,935 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +import { IPreviewEngineContext } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/IDataResolver'; +import { ContentSnippet } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/ContentSnippet'; +import { IEntityAttributeMetadata } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/EntityAttributeMetadata'; +import { EntityForm } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/EntityForm'; +import { EntityList } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/EntityList'; +import { PageTemplate } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/PageTemplate'; +import { SiteMarker } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/SiteMarker'; +import { SiteSetting } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/SiteSettings'; +import { WebForm } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/WebForm'; +import { WebTemplate } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/WebTemplate'; +import { Weblink } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/Weblink'; +import { WeblinkSet } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/WeblinkSet'; +import { Webpage } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/Webpage'; +import { Website } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/Website'; +import { PortalWebView } from './PortalWebView'; +import { load } from 'js-yaml'; +import * as vscode from 'vscode'; + +const fallbackURI = vscode.Uri.file(''); +/** + * Generates and refreshes preview context + */ +export class PreviewEngineContext { + + private previewEngineContext: IPreviewEngineContext; + private websiteRecord: Website; + private rootPath: vscode.Uri | null; + + constructor() { + this.previewEngineContext = {}; + this.rootPath = PortalWebView.getPortalRootFolder(); + this.websiteRecord = {} as Website; + } + + public createContext = async () => { + this.websiteRecord = await this.getWebsite(); + this.previewEngineContext = await this.createEngineContext(); + } + + public getPreviewEngineContext = () => { + return this.previewEngineContext; + } + + private createEngineContext = async (): Promise => { + + if (this.rootPath) { + return { + webpages: await this.getWebpages(), + contentSnippets: await this.getContentSnippets(), + webTemplates: await this.getWebTemplates(), + siteMarkers: await this.getSiteMarker(), + siteSettings: await this.getSiteSetting(), + entityLists: await this.getEntityLists(), + entityForms: await this.getEntityForms(), + webForms: await this.getWebForms(), + weblinks: await this.getWeblinks(), + weblinkSets: await this.getWeblinkSets(), + website: this.websiteRecord, + pageTemplates: await this.getPageTemplates(), + dataResolverExtras: {}, + resx: {}, + featureConfig: new Map(), + entityAttributeMetadata: [] as IEntityAttributeMetadata[], + lcid: '' as string, + isBootstrapV5: false as boolean, + } + } else return {} + } + + private getWebsite = async (): Promise => { + const website = await vscode.workspace.fs.readFile(this.rootPath?.with({ path: this.rootPath.path + '/website.yml' }) || fallbackURI); + const websiteYaml = load(new TextDecoder().decode(website)); + return websiteYaml as Website; + } + + private webPageHelper = async (pageUri: vscode.Uri): Promise => { + const webpageYaml = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.yml' })); + const webpageJS = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.custom_javascript.js' })); + const webpageCSS = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.custom_css.css' })); + const webpageCopy = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.copy.html' })); + const webpageSummary = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.summary.html' })); + const webpageRecord = load(new TextDecoder().decode(webpageYaml)) as Webpage; + webpageRecord.adx_customcss = new TextDecoder().decode(webpageCSS); + webpageRecord.adx_customjavascript = new TextDecoder().decode(webpageJS); + webpageRecord.adx_copy = new TextDecoder().decode(webpageCopy); + webpageRecord.adx_summary = new TextDecoder().decode(webpageSummary); + webpageRecord.adx_websiteid = this.websiteRecord.adx_websiteid; + return webpageRecord; + } + + + private getWebpages = async (): Promise => { + const webpagesDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/web-pages' }) || fallbackURI); + + const webpageArray: Webpage[] = []; + for (const webpage of webpagesDirectory) { + webpageArray.push(await this.webPageHelper(this.rootPath?.with({ path: this.rootPath.path + '/web-pages/' + webpage[0] + '/' + webpage[0] }) || fallbackURI)); + + const contentPageDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/web-pages/' + webpage[0] + '/content-pages' }) || fallbackURI); + for (const page of contentPageDirectory) { + if (page[0].endsWith(ymlPattern)) { + const pageName = page[0].slice(0, -12); + webpageArray.push(await this.webPageHelper(this.rootPath?.with({ path: this.rootPath.path + '/web-pages/' + webpage[0] + '/content-pages/' + pageName }) || fallbackURI)); + } + } + } + return webpageArray; + } + + private getWeblinks = async (): Promise => { + const weblinksDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets' }) || fallbackURI); + + const weblinksArray: Weblink[] = []; + for (const link of weblinksDirectory) { + const linkSubDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + link[0] }) || fallbackURI); + for (const sublink of linkSubDirectory) { + if (sublink[0].endsWith(weblinkPattern)) { + const weblinkYaml = await vscode.workspace.fs.readFile(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + link[0] + `/${sublink[0]}` }) || fallbackURI); + const weblinkRecord = load(new TextDecoder().decode(weblinkYaml)) as Weblink[] + weblinksArray.push(...weblinkRecord); + } + } + } + return weblinksArray; + } + + private webLinkSetHelper = async (fileUri: vscode.Uri): Promise => { + const weblinkSetYaml = await vscode.workspace.fs.readFile(fileUri); + const weblinkSetRecord = load(new TextDecoder().decode(weblinkSetYaml)) as WeblinkSet; + weblinkSetRecord.adx_websiteid = this.websiteRecord.adx_websiteid; + return weblinkSetRecord; + }; + + private getWeblinkSets = async (): Promise => { + const weblinkSetsDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets' }) || fallbackURI); + + const weblinkSetsArray: WeblinkSet[] = []; + for (const weblinkSet of weblinkSetsDirectory) { + const linkSubDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + weblinkSet[0] }) || fallbackURI); + for (const sublink of linkSubDirectory) { + if (!sublink[0].endsWith(weblinkPattern)) { + weblinkSetsArray.push(await this.webLinkSetHelper(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + weblinkSet[0] + `/${sublink[0]}` }) || fallbackURI)); // adx_title not in pac data but is manadatory, studio sends as undefined. + } + } + } + return weblinkSetsArray; + } + + + private contentSnippetHelper = async (fileUri: vscode.Uri): Promise => { + const snippetYaml = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.contentsnippet.yml' })); + const snippetValue = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.contentsnippet.value.html' })); + const snippetRecord = load(new TextDecoder().decode(snippetYaml)) as ContentSnippet + snippetRecord.adx_value = new TextDecoder().decode(snippetValue); + snippetRecord.adx_websiteid = '92d6c1b4-d84b-ee11-be6e-0022482d5cfb'; + snippetRecord.stateCode = 0; //check with PAC SME on how to get this field + return snippetRecord; + }; + + + private getContentSnippets = async (): Promise => { + const contentSnippetsDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/content-snippets' }) || fallbackURI); + + const contentSnippetsArray: ContentSnippet[] = []; + for (const contentSnippet of contentSnippetsDirectory) { + const snippetSubDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/content-snippets/' + contentSnippet[0] }) || fallbackURI); + for (const snippet of snippetSubDirectory) { + if (snippet[0].endsWith(ymlPattern)) { + contentSnippetsArray.push(await this.contentSnippetHelper(this.rootPath?.with({ path: this.rootPath.path + '/content-snippets/' + contentSnippet[0] + `/${snippet[0].slice(0, -19)}` }) || fallbackURI)); + } + } + } + return contentSnippetsArray; + } + + private pageTemplateHelper = async (fileUri: vscode.Uri): Promise => { + const pageTemplateYaml = await vscode.workspace.fs.readFile(fileUri); + const pageTemplateRecord = load(new TextDecoder().decode(pageTemplateYaml)) as PageTemplate; + return pageTemplateRecord; + }; + + + private getPageTemplates = async (): Promise => { + const pageTemplatesDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/page-templates' }) || fallbackURI); + + const pageTemplatesArray: PageTemplate[] = []; + for (const pageTemplate of pageTemplatesDirectory) { + pageTemplatesArray.push(await this.pageTemplateHelper(this.rootPath?.with({ path: this.rootPath.path + '/page-templates/' + pageTemplate[0] }) || fallbackURI)); + } + return pageTemplatesArray; + } + + private webTemplateHelper = async (fileUri: vscode.Uri): Promise => { + const webTemplateYaml = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.webtemplate.yml' })); + const webTemplateSource = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.webtemplate.source.html' })); + const webTemplateSourceHTML = new TextDecoder().decode(webTemplateSource); + const webTemplateRecord = load(new TextDecoder().decode(webTemplateYaml)) as WebTemplate; + webTemplateRecord.adx_source = webTemplateSourceHTML; + webTemplateRecord.adx_websiteid = this.websiteRecord.adx_websiteid; + return webTemplateRecord; + }; + + private getWebTemplates = async (): Promise => { + const webTemplatesDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/web-templates' }) || fallbackURI); + + const webTemplatesArray: WebTemplate[] = []; + for (const webTemplate of webTemplatesDirectory) { + webTemplatesArray.push(await this.webTemplateHelper(this.rootPath?.with({ path: this.rootPath.path + '/web-templates/' + webTemplate[0] + `/${webTemplate[0]}` }) || fallbackURI)); + } + return webTemplatesArray; + } + + private entityFormHelper = async (fileUri: vscode.Uri): Promise => { + const entityFormYaml = await vscode.workspace.fs.readFile(fileUri); + const entityFormRecord = load(new TextDecoder().decode(entityFormYaml)) as EntityForm; + entityFormRecord.adx_websiteid = this.websiteRecord.adx_websiteid; + return entityFormRecord; + }; + + private getEntityForms = async (): Promise => { + const entityFormsDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/basic-forms' }) || fallbackURI); + + const entityFormsArray: EntityForm[] = []; + for (const entityForm of entityFormsDirectory) { + entityFormsArray.push(await this.entityFormHelper(this.rootPath?.with({ path: this.rootPath.path + '/basic-forms/' + entityForm[0] + `/${entityForm[0]}.basicform.yml` }) || fallbackURI)); + } + return entityFormsArray; + } + + private entityListHelper = async (fileUri: vscode.Uri): Promise => { + const entityListYaml = await vscode.workspace.fs.readFile(fileUri); + const entityListRecord = load(new TextDecoder().decode(entityListYaml)) as EntityList; + entityListRecord.adx_websiteid = this.websiteRecord.adx_websiteid; + return entityListRecord; + }; + + private getEntityLists = async (): Promise => { + const entityListsDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/lists' }) || fallbackURI); + + const entityListsArray: EntityList[] = []; + for (const entityList of entityListsDirectory) { + if (entityList[0].endsWith(ymlPattern)) { + entityListsArray.push(await this.entityListHelper(this.rootPath?.with({ path: this.rootPath.path + '/lists/' + entityList[0] }) || fallbackURI)); + } + } + return entityListsArray; + } + + private webFormHelper = async (fileUri: vscode.Uri): Promise => { + const webFormYaml = await vscode.workspace.fs.readFile(fileUri); + const webFormRecord = load(new TextDecoder().decode(webFormYaml)) as WebForm; + webFormRecord.adx_websiteid = this.websiteRecord.adx_websiteid; + return webFormRecord; + }; + + + private getWebForms = async (): Promise => { + const webFormsDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/advanced-forms' }) || fallbackURI); + + const webFormsArray: WebForm[] = []; + for (const webForm of webFormsDirectory) { + webFormsArray.push(await this.webFormHelper(this.rootPath?.with({ path: this.rootPath.path + '/advanced-forms/' + webForm[0] + `/${webForm[0]}.advancedform.yml` }) || fallbackURI)); + } + return webFormsArray; + } + + private getSiteSetting = async (): Promise => { + const siteSetting = await vscode.workspace.fs.readFile(this.rootPath?.with({ path: this.rootPath.path + '/sitesetting.yml' }) || fallbackURI); + const siteSettingYaml = load(new TextDecoder().decode(siteSetting)) as SiteSetting[]; + const siteSettingRecords = siteSettingYaml.map((siteSettingRecord) => { + return { + adx_websiteid: this.websiteRecord.adx_websiteid, + adx_name: siteSettingRecord.adx_name, + adx_value: siteSettingRecord.adx_value, + adx_sitesettingid: siteSettingRecord.adx_sitesettingid, + } + }); + return siteSettingRecords; + } + + private getSiteMarker = async (): Promise => { + const siteMarker = await vscode.workspace.fs.readFile(this.rootPath?.with({ path: this.rootPath.path + '/sitemarker.yml' }) || fallbackURI); + const siteMarkerYaml = load(new TextDecoder().decode(siteMarker)) as SiteMarker[]; + const siteMarkerRecords = siteMarkerYaml.map((siteMarkerRecord) => { + return { + adx_name: siteMarkerRecord.adx_name as string, + adx_pageid: siteMarkerRecord.adx_pageid as string, + adx_sitemarkerid: siteMarkerRecord.adx_sitemarkerid, + adx_websiteid: this.websiteRecord.adx_websiteid, + + } + + }); + return siteMarkerRecords; + } + + public updateContext = async () => { + //check entity type + const fileName = getFileName(vscode.window.activeTextEditor?.document.fileName ?? ''); + const fileUri = vscode.window.activeTextEditor?.document.uri || fallbackURI; + const entityType: ContextProperty = this.getEntityType(fileName); + + switch (entityType) { + case ContextProperty.SITE_MARKER: + this.previewEngineContext.siteMarkers = await this.getSiteMarker(); + break; + case ContextProperty.SITE_SETTING: + this.previewEngineContext.siteSettings = await this.getSiteSetting(); + break; + case ContextProperty.WEBSITE: + // update private website record too + this.previewEngineContext.website = await this.getWebsite(); + break; + case ContextProperty.WEB_LINK: + this.previewEngineContext.weblinks = await this.getWeblinks(); + break; + case ContextProperty.WEB_LINK_SET: { + const obj = await this.webLinkSetHelper(fileUri); + const value = obj[ContextPropertyKey.WEB_LINK_SET as unknown as keyof WeblinkSet]; + const index = findObjectIndexByProperty(this.previewEngineContext.weblinkSets, ContextPropertyKey.WEB_LINK_SET, value); + if (index != -1 && this.previewEngineContext.weblinkSets) { + this.previewEngineContext.weblinkSets[index] = obj; + } else { + this.previewEngineContext.weblinkSets = await this.getWeblinkSets(); + } + break; + } + case ContextProperty.ENTITY_FORM: + { + const obj = await this.entityFormHelper(fileUri); + const value = obj[ContextPropertyKey.ENTITY_FORM as unknown as keyof EntityForm]; + const index = findObjectIndexByProperty(this.previewEngineContext.entityForms, ContextPropertyKey.ENTITY_FORM, value); + if (index != -1 && this.previewEngineContext.entityForms) { + this.previewEngineContext.entityForms[index] = obj; + } else { + this.previewEngineContext.entityForms = await this.getEntityForms(); + } + break; + } + case ContextProperty.ENTITY_LIST: + { + const obj = await this.entityListHelper(fileUri); + const value = obj[ContextPropertyKey.ENTITY_LIST as unknown as keyof EntityList]; + const index = findObjectIndexByProperty(this.previewEngineContext.entityLists, ContextPropertyKey.ENTITY_LIST, value); + if (index != -1 && this.previewEngineContext.entityLists) { + this.previewEngineContext.entityLists[index] = obj; + } else { + this.previewEngineContext.entityLists = await this.getEntityLists(); + } + break; + } + case ContextProperty.WEB_FORM: + { + const obj = await this.webFormHelper(fileUri); + const value = obj[ContextPropertyKey.WEB_FORM as unknown as keyof WebForm]; + const index = findObjectIndexByProperty(this.previewEngineContext.webForms, ContextPropertyKey.WEB_FORM, value); + if (index != -1 && this.previewEngineContext.webForms) { + this.previewEngineContext.webForms[index] = obj; + } else { + this.previewEngineContext.webForms = await this.getWebForms(); + } + break; + } + case ContextProperty.PAGE_TEMPLATE: + { + const obj = await this.pageTemplateHelper(fileUri); + const value = obj[ContextPropertyKey.PAGE_TEMPLATE as unknown as keyof PageTemplate]; + const index = findObjectIndexByProperty(this.previewEngineContext.pageTemplates, ContextPropertyKey.PAGE_TEMPLATE, value); + if (index != -1 && this.previewEngineContext.pageTemplates) { + this.previewEngineContext.pageTemplates[index] = obj; + } else { + this.previewEngineContext.pageTemplates = await this.getPageTemplates(); + } + break; + } + case ContextProperty.WEBPAGE_YAML: + case ContextProperty.WEBPAGE_COPY: + case ContextProperty.WEBPAGE_CSS: + case ContextProperty.WEBPAGE_JS: + case ContextProperty.WEBPAGE_SUMMARY: + { + const obj = await this.webPageHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); + const value = obj[ContextPropertyKey.WEBPAGE as unknown as keyof Webpage]; + const index = findObjectIndexByProperty(this.previewEngineContext.webpages, ContextPropertyKey.WEBPAGE, value);//default-offline-page + if (index != -1 && this.previewEngineContext.webpages) { + this.previewEngineContext.webpages[index] = obj; + } else { + this.previewEngineContext.webpages = await this.getWebpages(); + } + break; + } + case ContextProperty.WEB_TEMPLATE_YAML: + case ContextProperty.WEB_TEMPLATE_SOURCE: + { + const obj = await this.webTemplateHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); + const value = obj[ContextPropertyKey.WEB_TEMPLATE as unknown as keyof WebTemplate]; + const index = findObjectIndexByProperty(this.previewEngineContext.webTemplates, ContextPropertyKey.WEB_TEMPLATE, value);//breadcrumb + if (index != -1 && this.previewEngineContext.webTemplates) { + this.previewEngineContext.webTemplates[index] = obj; + } else { + this.previewEngineContext.webTemplates = await this.getWebTemplates(); + } + break; + } + case ContextProperty.CONTENT_SNIPPET_YAML: + case ContextProperty.CONTENT_SNIPPET_VALUE: + { + const obj = await this.contentSnippetHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); + const value = obj[ContextPropertyKey.CONTENT_SNIPPET as unknown as keyof ContentSnippet]; + const index = findObjectIndexByProperty(this.previewEngineContext.contentSnippets, ContextPropertyKey.CONTENT_SNIPPET, value);//confirm email - hindi + if (index != -1 && this.previewEngineContext.contentSnippets) { + this.previewEngineContext.contentSnippets[index] = obj; + } else { + this.previewEngineContext.contentSnippets = await this.getContentSnippets(); + } + break; + } + default: + break; + } + } + + private getEntityType = (filename: string): ContextProperty => { + + if (filename.endsWith(ContextProperty.WEBSITE)) { + return ContextProperty.WEBSITE; + } else if (filename.endsWith(ContextProperty.SITE_SETTING)) { + return ContextProperty.SITE_SETTING; + } else if (filename.endsWith(ContextProperty.SITE_MARKER)) { + return ContextProperty.SITE_MARKER; + } else if (filename.endsWith(ContextProperty.ENTITY_FORM)) { + return ContextProperty.ENTITY_FORM; + } else if (filename.endsWith(ContextProperty.ENTITY_LIST)) { + return ContextProperty.ENTITY_LIST; + } else if (filename.endsWith(ContextProperty.WEB_FORM)) { + return ContextProperty.WEB_FORM; + } else if (filename.endsWith(ContextProperty.WEB_LINK)) { + return ContextProperty.WEB_LINK; + } else if (filename.endsWith(ContextProperty.WEB_LINK_SET)) { + return ContextProperty.WEB_LINK_SET; + } else if (filename.endsWith(ContextProperty.PAGE_TEMPLATE)) { + return ContextProperty.PAGE_TEMPLATE; + } else if (filename.endsWith(ContextProperty.WEB_TEMPLATE_YAML)) { + return ContextProperty.WEB_TEMPLATE_YAML; + } else if (filename.endsWith(ContextProperty.WEB_TEMPLATE_SOURCE)) { + return ContextProperty.WEB_TEMPLATE_SOURCE; + } else if (filename.endsWith(ContextProperty.WEBPAGE_YAML)) { + return ContextProperty.WEBPAGE_YAML; + } else if (filename.endsWith(ContextProperty.WEBPAGE_COPY)) { + return ContextProperty.WEBPAGE_COPY; + } else if (filename.endsWith(ContextProperty.WEBPAGE_CSS)) { + return ContextProperty.WEBPAGE_CSS; + } else if (filename.endsWith(ContextProperty.WEBPAGE_JS)) { + return ContextProperty.WEBPAGE_JS; + } else if (filename.endsWith(ContextProperty.WEBPAGE_SUMMARY)) { + return ContextProperty.WEBPAGE_SUMMARY; + } else if (filename.endsWith(ContextProperty.CONTENT_SNIPPET_YAML)) { + return ContextProperty.CONTENT_SNIPPET_YAML; + } else if (filename.endsWith(ContextProperty.CONTENT_SNIPPET_VALUE)) { + return ContextProperty.CONTENT_SNIPPET_VALUE; + } else { + return ContextProperty.UNKNOWN_PROPERTY; + } + } +} + +// let rootPath: vscode.Uri | null; + +// const WEBSITE_ID = 'WEBSITE_ID'; +const ymlPattern = '.yml'; // /\.(yml)$/i; +const weblinkPattern = 'weblink.yml'; // /\.weblink\.yml$/i; + +// export const getRenderingContext = async (): Promise => { +// rootPath = PortalWebView.getPortalRootFolder(); + +// console.log(rootPath); +// const map = new Map(); +// map.set('isFetchXmlForCSREnabled', true) +// if (rootPath) { +// return { +// webpages: await getWebpages(), +// contentSnippets: await getContentSnippets(), +// webTemplates: await getWebTemplates(), +// siteMarkers: await getSiteMarker(), +// siteSettings: await getSiteSetting(), +// entityLists: await getEntityLists(), +// entityForms: await getEntityForms(), +// webForms: await getWebForms(), +// weblinks: await getWeblinks(), +// weblinkSets: await getWeblinkSets(), +// website: await getWebsite(), +// pageTemplates: await getPageTemplates(), +// dataResolverExtras: {}, +// resx: {}, +// featureConfig: map, +// entityAttributeMetadata: [] as IEntityAttributeMetadata[], +// lcid: '' as string, +// isBootstrapV5: false as boolean, +// } +// } +// else { +// return {} +// } + + +// } + +enum ContextProperty { + WEBPAGE_YAML = '.webpage.yml', + WEBPAGE_COPY = '.webpage.copy.html', + WEBPAGE_CSS = '.webpage.custom_css.css', + WEBPAGE_JS = '.webpage.custom_javascript.js', + WEBPAGE_SUMMARY = '.webpage.summary.html', + CONTENT_SNIPPET_YAML = '.contentsnippet.yml', + CONTENT_SNIPPET_VALUE = '.contentsnippet.value.html', + WEB_TEMPLATE_YAML = '.webtemplate.yml', + WEB_TEMPLATE_SOURCE = '.webtemplate.source.html', + SITE_MARKER = 'sitemarker.yml', + SITE_SETTING = 'sitesetting.yml', + ENTITY_LIST = '.list.yml', + ENTITY_FORM = '.basicform.yml', + WEB_FORM = '.advancedform.yml', + WEB_LINK = '.weblink.yml', + WEB_LINK_SET = '.weblinkset.yml', + WEBSITE = 'website.yml', + PAGE_TEMPLATE = '.pagetemplate.yml', + UNKNOWN_PROPERTY = '' +} +enum ContextPropertyKey { + WEBPAGE = 'adx_webpageid', + CONTENT_SNIPPET = 'adx_contentsnippetid', + WEB_TEMPLATE = 'adx_webtemplateid', + SITE_MARKER = 'adx_sitemarkerid', + SITE_SETTING = 'adx_sitesettingid', + ENTITY_LIST = 'adx_entitylistid', + ENTITY_FORM = 'adx_entityformid', + WEB_FORM = 'adx_webformid', + WEB_LINK = 'adx_weblinkid', + WEB_LINK_SET = 'adx_weblinksetid', + WEBSITE = 'adx_websiteid', + PAGE_TEMPLATE = 'adx_pagetemplateid', +} + + + +const getFileName = (fileName: string) => { + const lastSlashIndex = fileName.lastIndexOf('/'); + if (lastSlashIndex !== -1) { + return fileName.substring(lastSlashIndex + 1); + } else { + // If there is no forward slash in the input string, return the original string. + return fileName; + } +} + +const findObjectIndexByProperty = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + array: any, + key: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value: any +): number => { + for (let i = 0; i < array.length; i++) { + if (array[i][key] === value) { + return i; + } + } + return -1; +} + +const removeExtension = (fileName: string, extn: string): string => { + // Using slice to remove the extension from the end + return fileName.slice(0, -extn.length); +} + + +// export const updateContext = async (previewEngineContext: IPreviewEngineContext) => { +// //check entity type +// const fileName = getFileName(vscode.window.activeTextEditor?.document.fileName ?? ''); +// const fileUri = vscode.window.activeTextEditor?.document.uri || fallbackURI; +// const entityType: ContextProperty = getEntityType(fileName); + +// switch (entityType) { +// case ContextProperty.SITE_MARKER: +// previewEngineContext.siteMarkers = await getSiteMarker(); +// break; +// case ContextProperty.SITE_SETTING: +// previewEngineContext.siteSettings = await getSiteSetting(); +// break; +// case ContextProperty.WEBSITE: +// // update private website record too +// previewEngineContext.website = await getWebsite(); +// break; +// case ContextProperty.WEB_LINK: +// previewEngineContext.weblinks = await getWeblinks(); +// break; +// case ContextProperty.WEB_LINK_SET: { +// const obj = await webLinkSetHelper(fileUri); +// const value = obj[ContextPropertyKey.WEB_LINK_SET as unknown as keyof WeblinkSet]; +// const index = findObjectIndexByProperty(previewEngineContext.weblinkSets, ContextPropertyKey.WEB_LINK_SET, value); +// if (index != -1 && previewEngineContext.weblinkSets) { +// previewEngineContext.weblinkSets[index] = obj; +// } else { +// previewEngineContext.weblinkSets = await getWeblinkSets(); +// } +// break; +// } +// case ContextProperty.ENTITY_FORM: +// { +// const obj = await entityFormHelper(fileUri); +// const value = obj[ContextPropertyKey.ENTITY_FORM as unknown as keyof EntityForm]; +// const index = findObjectIndexByProperty(previewEngineContext.entityForms, ContextPropertyKey.ENTITY_FORM, value); +// if (index != -1 && previewEngineContext.entityForms) { +// previewEngineContext.entityForms[index] = obj; +// } else { +// previewEngineContext.entityForms = await getEntityForms(); +// } +// break; +// } +// case ContextProperty.ENTITY_LIST: +// { +// const obj = await entityListHelper(fileUri); +// const value = obj[ContextPropertyKey.ENTITY_LIST as unknown as keyof EntityList]; +// const index = findObjectIndexByProperty(previewEngineContext.entityLists, ContextPropertyKey.ENTITY_LIST, value); +// if (index != -1 && previewEngineContext.entityLists) { +// previewEngineContext.entityLists[index] = obj; +// } else { +// previewEngineContext.entityLists = await getEntityLists(); +// } +// break; +// } +// case ContextProperty.WEB_FORM: +// { +// const obj = await webFormHelper(fileUri); +// const value = obj[ContextPropertyKey.WEB_FORM as unknown as keyof WebForm]; +// const index = findObjectIndexByProperty(previewEngineContext.webForms, ContextPropertyKey.WEB_FORM, value); +// if (index != -1 && previewEngineContext.webForms) { +// previewEngineContext.webForms[index] = obj; +// } else { +// previewEngineContext.webForms = await getWebForms(); +// } +// break; +// } +// case ContextProperty.PAGE_TEMPLATE: +// { +// const obj = await pageTemplateHelper(fileUri); +// const value = obj[ContextPropertyKey.PAGE_TEMPLATE as unknown as keyof PageTemplate]; +// const index = findObjectIndexByProperty(previewEngineContext.pageTemplates, ContextPropertyKey.PAGE_TEMPLATE, value); +// if (index != -1 && previewEngineContext.pageTemplates) { +// previewEngineContext.pageTemplates[index] = obj; +// } else { +// previewEngineContext.pageTemplates = await getPageTemplates(); +// } +// break; +// } +// case ContextProperty.WEBPAGE_YAML: +// case ContextProperty.WEBPAGE_COPY: +// case ContextProperty.WEBPAGE_CSS: +// case ContextProperty.WEBPAGE_JS: +// case ContextProperty.WEBPAGE_SUMMARY: +// { +// const obj = await webPageHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); +// const value = obj[ContextPropertyKey.WEBPAGE as unknown as keyof Webpage]; +// const index = findObjectIndexByProperty(previewEngineContext.webpages, ContextPropertyKey.WEBPAGE, value);//default-offline-page +// if (index != -1 && previewEngineContext.webpages) { +// previewEngineContext.webpages[index] = obj; +// } else { +// previewEngineContext.webpages = await getWebpages(); +// } +// break; +// } +// case ContextProperty.WEB_TEMPLATE_YAML: +// case ContextProperty.WEB_TEMPLATE_SOURCE: +// { +// const obj = await webTemplateHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); +// const value = obj[ContextPropertyKey.WEB_TEMPLATE as unknown as keyof WebTemplate]; +// const index = findObjectIndexByProperty(previewEngineContext.webTemplates, ContextPropertyKey.WEB_TEMPLATE, value);//breadcrumb +// if (index != -1 && previewEngineContext.webTemplates) { +// previewEngineContext.webTemplates[index] = obj; +// } else { +// previewEngineContext.webTemplates = await getWebTemplates(); +// } +// break; +// } +// case ContextProperty.CONTENT_SNIPPET_YAML: +// case ContextProperty.CONTENT_SNIPPET_VALUE: +// { +// const obj = await contentSnippetHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); +// const value = obj[ContextPropertyKey.CONTENT_SNIPPET as unknown as keyof ContentSnippet]; +// const index = findObjectIndexByProperty(previewEngineContext.contentSnippets, ContextPropertyKey.CONTENT_SNIPPET, value);//confirm email - hindi +// if (index != -1 && previewEngineContext.contentSnippets) { +// previewEngineContext.contentSnippets[index] = obj; +// } else { +// previewEngineContext.contentSnippets = await getContentSnippets(); +// } +// break; +// } +// default: +// break; +// } +// } + +// const webPageHelper = async (pageUri: vscode.Uri): Promise => { +// const webpageYaml = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.yml' })); +// const webpageJS = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.custom_javascript.js' })); +// const webpageCSS = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.custom_css.css' })); +// const webpageCopy = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.copy.html' })); +// const webpageSummary = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.summary.html' })); +// const webpageRecord = load(new TextDecoder().decode(webpageYaml)) as Webpage +// webpageRecord.adx_customcss = new TextDecoder().decode(webpageCSS) +// webpageRecord.adx_customjavascript = new TextDecoder().decode(webpageJS) +// webpageRecord.adx_copy = new TextDecoder().decode(webpageCopy) +// webpageRecord.adx_summary = new TextDecoder().decode(webpageSummary) +// webpageRecord.adx_websiteid = WEBSITE_ID +// return webpageRecord; +// } + +// const getWebpages = async (): Promise => { +// const webpagesDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/web-pages' }) || fallbackURI); + +// const webpageArray: Webpage[] = []; +// for (const webpage of webpagesDirectory) { +// webpageArray.push(await webPageHelper(rootPath?.with({ path: rootPath.path + '/web-pages/' + webpage[0] + '/' + webpage[0] }) || fallbackURI)); + +// const contentPageDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/web-pages/' + webpage[0] + '/content-pages' }) || fallbackURI); +// for (const page of contentPageDirectory) { +// if (page[0].endsWith(ymlPattern)) { +// const pageName = page[0].slice(0, -12); +// webpageArray.push(await webPageHelper(rootPath?.with({ path: rootPath.path + '/web-pages/' + webpage[0] + '/content-pages/' + pageName }) || fallbackURI)); +// } +// } +// } +// return webpageArray; +// } + + +// const getWeblinks = async (): Promise => { +// const weblinksDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets' }) || fallbackURI); + +// const weblinksArray: Weblink[] = []; +// for (const link of weblinksDirectory) { +// const linkSubDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + link[0] }) || fallbackURI); +// for (const sublink of linkSubDirectory) { +// if (sublink[0].endsWith(weblinkPattern)) { +// const weblinkYaml = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + link[0] + `/${sublink[0]}` }) || fallbackURI); +// const weblinkRecord = load(new TextDecoder().decode(weblinkYaml)) as Weblink[] +// weblinksArray.push(...weblinkRecord); +// } +// } +// } +// return weblinksArray; +// } + +// const webLinkSetHelper = async (fileUri: vscode.Uri): Promise => { +// const weblinkSetYaml = await vscode.workspace.fs.readFile(fileUri); +// const weblinkSetRecord = load(new TextDecoder().decode(weblinkSetYaml)) as WeblinkSet; +// weblinkSetRecord.adx_websiteid = WEBSITE_ID; +// return weblinkSetRecord; +// }; + + +// const getWeblinkSets = async (): Promise => { +// const weblinkSetsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets' }) || fallbackURI); + +// const weblinkSetsArray: WeblinkSet[] = []; +// for (const weblinkSet of weblinkSetsDirectory) { +// const linkSubDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + weblinkSet[0] }) || fallbackURI); +// for (const sublink of linkSubDirectory) { +// if (!sublink[0].endsWith(weblinkPattern)) { +// weblinkSetsArray.push(await webLinkSetHelper(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + weblinkSet[0] + `/${sublink[0]}` }) || fallbackURI)); // adx_title not in pac data but is manadatory, studio sends as undefined. +// } +// } +// } +// return weblinkSetsArray; +// } + +// const contentSnippetHelper = async (fileUri: vscode.Uri): Promise => { +// const snippetYaml = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + 'yml' })); +// const snippetValue = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + 'value.html' })); +// const snippetRecord = load(new TextDecoder().decode(snippetYaml)) as ContentSnippet +// snippetRecord.adx_value = new TextDecoder().decode(snippetValue); +// snippetRecord.adx_websiteid = '92d6c1b4-d84b-ee11-be6e-0022482d5cfb'; +// snippetRecord.stateCode = 0; //check with PAC SME on how to get this field +// return snippetRecord; +// }; + + +// const getContentSnippets = async (): Promise => { +// const contentSnippetsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/content-snippets' }) || fallbackURI); + +// const contentSnippetsArray: ContentSnippet[] = []; +// for (const contentSnippet of contentSnippetsDirectory) { +// const snippetSubDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/content-snippets/' + contentSnippet[0] }) || fallbackURI); +// for (const snippet of snippetSubDirectory) { +// if (snippet[0].endsWith(ymlPattern)) { +// contentSnippetsArray.push(await contentSnippetHelper(rootPath?.with({ path: rootPath.path + '/content-snippets/' + contentSnippet[0] + `/${snippet[0].slice(0, -3)}` }) || fallbackURI)); +// } +// } +// } +// return contentSnippetsArray; +// } + +// const pageTemplateHelper = async (fileUri: vscode.Uri): Promise => { +// const pageTemplateYaml = await vscode.workspace.fs.readFile(fileUri); +// const pageTemplateRecord = load(new TextDecoder().decode(pageTemplateYaml)) as PageTemplate; +// return pageTemplateRecord; +// }; + +// const getPageTemplates = async (): Promise => { +// const pageTemplatesDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/page-templates' }) || fallbackURI); + +// const pageTemplatesArray: PageTemplate[] = []; +// for (const pageTemplate of pageTemplatesDirectory) { +// pageTemplatesArray.push(await pageTemplateHelper(rootPath?.with({ path: rootPath.path + '/page-templates/' + pageTemplate[0] }) || fallbackURI)); +// } +// return pageTemplatesArray; +// } + +// const webTemplateHelper = async (fileUri: vscode.Uri): Promise => { +// const webTemplateYaml = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.webtemplate.yml' })); +// const webTemplateSource = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.webtemplate.source.html' })); +// const webTemplateSourceHTML = new TextDecoder().decode(webTemplateSource); +// const webTemplateRecord = load(new TextDecoder().decode(webTemplateYaml)) as WebTemplate; +// webTemplateRecord.adx_source = webTemplateSourceHTML; +// webTemplateRecord.adx_websiteid = WEBSITE_ID; +// return webTemplateRecord; +// }; + + +// const getWebTemplates = async (): Promise => { +// const webTemplatesDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/web-templates' }) || fallbackURI); + +// const webTemplatesArray: WebTemplate[] = []; +// for (const webTemplate of webTemplatesDirectory) { +// webTemplatesArray.push(await webTemplateHelper(rootPath?.with({ path: rootPath.path + '/web-templates/' + webTemplate[0] + `/${webTemplate[0]}` }) || fallbackURI)); +// } +// return webTemplatesArray; +// } + +// const entityFormHelper = async (fileUri: vscode.Uri): Promise => { +// const entityFormYaml = await vscode.workspace.fs.readFile(fileUri); +// const entityFormRecord = load(new TextDecoder().decode(entityFormYaml)) as EntityForm; +// entityFormRecord.adx_websiteid = WEBSITE_ID; +// return entityFormRecord; +// }; + +// const getEntityForms = async (): Promise => { +// const entityFormsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/basic-forms' }) || fallbackURI); + +// const entityFormsArray: EntityForm[] = []; +// for (const entityForm of entityFormsDirectory) { +// entityFormsArray.push(await entityFormHelper(rootPath?.with({ path: rootPath.path + '/basic-forms/' + entityForm[0] + `/${entityForm[0]}.basicform.yml` }) || fallbackURI)); +// } +// return entityFormsArray; +// } + +// const entityListHelper = async (fileUri: vscode.Uri): Promise => { +// const entityListYaml = await vscode.workspace.fs.readFile(fileUri); +// const entityListRecord = load(new TextDecoder().decode(entityListYaml)) as EntityList; +// entityListRecord.adx_websiteid = WEBSITE_ID; +// return entityListRecord; +// }; + +// const getEntityLists = async (): Promise => { +// const entityListsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/lists' }) || fallbackURI); + +// const entityListsArray: EntityList[] = []; +// for (const entityList of entityListsDirectory) { +// if (entityList[0].endsWith(ymlPattern)) { +// entityListsArray.push(await entityListHelper(rootPath?.with({ path: rootPath.path + '/lists/' + entityList[0] }) || fallbackURI)); +// } +// } +// return entityListsArray; +// } + +// const webFormHelper = async (fileUri: vscode.Uri): Promise => { +// const webFormYaml = await vscode.workspace.fs.readFile(fileUri); +// const webFormRecord = load(new TextDecoder().decode(webFormYaml)) as WebForm; +// webFormRecord.adx_websiteid = WEBSITE_ID; +// return webFormRecord; +// }; + +// const getWebForms = async (): Promise => { +// const webFormsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/advanced-forms' }) || fallbackURI); + +// const webFormsArray: WebForm[] = []; +// for (const webForm of webFormsDirectory) { +// webFormsArray.push(await webFormHelper(rootPath?.with({ path: rootPath.path + '/advanced-forms/' + webForm[0] + `/${webForm[0]}.advancedform.yml` }) || fallbackURI)); +// } +// return webFormsArray; +// } + +// const getWebsite = async (): Promise => { +// const website = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/website.yml' }) || fallbackURI); +// const websiteYaml = load(new TextDecoder().decode(website)); +// return websiteYaml as Website; +// } + +// const getSiteSetting = async (): Promise => { +// const siteSetting = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/sitesetting.yml' }) || fallbackURI); +// const siteSettingYaml = load(new TextDecoder().decode(siteSetting)) as SiteSetting[]; +// const siteSettingRecords = siteSettingYaml.map((siteSettingRecord) => { +// return { +// adx_websiteid: siteSettingRecord.adx_websiteid,//should we use static id? +// adx_name: siteSettingRecord.adx_name, +// adx_value: siteSettingRecord.adx_value, +// adx_sitesettingid: siteSettingRecord.adx_sitesettingid, + +// } + +// }); +// return siteSettingRecords; +// } + +// const getSiteMarker = async (): Promise => { +// const siteMarker = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/sitemarker.yml' }) || fallbackURI); +// const siteMarkerYaml = load(new TextDecoder().decode(siteMarker)) as SiteMarker[]; +// const siteMarkerRecords = siteMarkerYaml.map((siteMarkerRecord) => { +// return { +// adx_name: siteMarkerRecord.adx_name as string, +// adx_pageid: siteMarkerRecord.adx_pageid as string, +// adx_sitemarkerid: siteMarkerRecord.adx_sitemarkerid, +// adx_websiteid: WEBSITE_ID, + +// } + +// }); +// return siteMarkerRecords; +// } + From cdcdf9ab8488b1326b4de37bc777730647913218 Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Mon, 11 Sep 2023 13:33:07 +0530 Subject: [PATCH 3/8] Refactoring --- src/client/power-pages/commonUtility.ts | 20 + .../preview}/PreviewEngineContext.ts | 490 +----------------- .../power-pages/preview/Utils/Constants.ts | 40 ++ 3 files changed, 76 insertions(+), 474 deletions(-) rename src/client/{ => power-pages/preview}/PreviewEngineContext.ts (52%) create mode 100644 src/client/power-pages/preview/Utils/Constants.ts diff --git a/src/client/power-pages/commonUtility.ts b/src/client/power-pages/commonUtility.ts index cdb8a8fd..cde5fe3e 100644 --- a/src/client/power-pages/commonUtility.ts +++ b/src/client/power-pages/commonUtility.ts @@ -197,3 +197,23 @@ export function getRegExPattern(fileNameArray: string[]): RegExp[] { return patterns; } + +export const findObjectIndexByProperty = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + array: any, + key: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value: any +): number => { + for (let i = 0; i < array.length; i++) { + if (array[i][key] === value) { + return i; + } + } + return -1; +} + +export const removeExtension = (fileName: string, extn: string): string => { + // Using slice to remove the extension from the end + return fileName.slice(0, -extn.length); +} diff --git a/src/client/PreviewEngineContext.ts b/src/client/power-pages/preview/PreviewEngineContext.ts similarity index 52% rename from src/client/PreviewEngineContext.ts rename to src/client/power-pages/preview/PreviewEngineContext.ts index 23102c55..be254d86 100644 --- a/src/client/PreviewEngineContext.ts +++ b/src/client/power-pages/preview/PreviewEngineContext.ts @@ -17,11 +17,14 @@ import { Weblink } from '@maker-studio/powerportals-preview-engine/lib/Rendering import { WeblinkSet } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/WeblinkSet'; import { Webpage } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/Webpage'; import { Website } from '@maker-studio/powerportals-preview-engine/lib/RenderingEngine/Types/Entity/Website'; -import { PortalWebView } from './PortalWebView'; +import { PortalWebView } from '../../PortalWebView'; import { load } from 'js-yaml'; import * as vscode from 'vscode'; +import { ContextProperty, ContextPropertyKey } from './Utils/Constants'; +import { findObjectIndexByProperty, getFileProperties, removeExtension } from '../commonUtility'; const fallbackURI = vscode.Uri.file(''); + /** * Generates and refreshes preview context */ @@ -103,7 +106,7 @@ export class PreviewEngineContext { const contentPageDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/web-pages/' + webpage[0] + '/content-pages' }) || fallbackURI); for (const page of contentPageDirectory) { - if (page[0].endsWith(ymlPattern)) { + if (page[0].endsWith(ContextProperty.WEBPAGE_YAML)) { const pageName = page[0].slice(0, -12); webpageArray.push(await this.webPageHelper(this.rootPath?.with({ path: this.rootPath.path + '/web-pages/' + webpage[0] + '/content-pages/' + pageName }) || fallbackURI)); } @@ -119,7 +122,7 @@ export class PreviewEngineContext { for (const link of weblinksDirectory) { const linkSubDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + link[0] }) || fallbackURI); for (const sublink of linkSubDirectory) { - if (sublink[0].endsWith(weblinkPattern)) { + if (sublink[0].endsWith(ContextProperty.WEB_LINK)) { const weblinkYaml = await vscode.workspace.fs.readFile(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + link[0] + `/${sublink[0]}` }) || fallbackURI); const weblinkRecord = load(new TextDecoder().decode(weblinkYaml)) as Weblink[] weblinksArray.push(...weblinkRecord); @@ -143,7 +146,7 @@ export class PreviewEngineContext { for (const weblinkSet of weblinkSetsDirectory) { const linkSubDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + weblinkSet[0] }) || fallbackURI); for (const sublink of linkSubDirectory) { - if (!sublink[0].endsWith(weblinkPattern)) { + if (sublink[0].endsWith(ContextProperty.WEB_LINK_SET)) { weblinkSetsArray.push(await this.webLinkSetHelper(this.rootPath?.with({ path: this.rootPath.path + '/weblink-sets/' + weblinkSet[0] + `/${sublink[0]}` }) || fallbackURI)); // adx_title not in pac data but is manadatory, studio sends as undefined. } } @@ -170,7 +173,7 @@ export class PreviewEngineContext { for (const contentSnippet of contentSnippetsDirectory) { const snippetSubDirectory = await vscode.workspace.fs.readDirectory(this.rootPath?.with({ path: this.rootPath.path + '/content-snippets/' + contentSnippet[0] }) || fallbackURI); for (const snippet of snippetSubDirectory) { - if (snippet[0].endsWith(ymlPattern)) { + if (snippet[0].endsWith(ContextProperty.CONTENT_SNIPPET_YAML)) { contentSnippetsArray.push(await this.contentSnippetHelper(this.rootPath?.with({ path: this.rootPath.path + '/content-snippets/' + contentSnippet[0] + `/${snippet[0].slice(0, -19)}` }) || fallbackURI)); } } @@ -244,7 +247,7 @@ export class PreviewEngineContext { const entityListsArray: EntityList[] = []; for (const entityList of entityListsDirectory) { - if (entityList[0].endsWith(ymlPattern)) { + if (entityList[0].endsWith(ContextProperty.ENTITY_LIST)) { entityListsArray.push(await this.entityListHelper(this.rootPath?.with({ path: this.rootPath.path + '/lists/' + entityList[0] }) || fallbackURI)); } } @@ -300,11 +303,15 @@ export class PreviewEngineContext { } public updateContext = async () => { - //check entity type - const fileName = getFileName(vscode.window.activeTextEditor?.document.fileName ?? ''); const fileUri = vscode.window.activeTextEditor?.document.uri || fallbackURI; + const fileName = getFileProperties(fileUri.path).fileCompleteName; + if (!fileName) { + // TODO: Handle this scenario + return; + } + // Check entity type const entityType: ContextProperty = this.getEntityType(fileName); - + switch (entityType) { case ContextProperty.SITE_MARKER: this.previewEngineContext.siteMarkers = await this.getSiteMarker(); @@ -468,468 +475,3 @@ export class PreviewEngineContext { } } } - -// let rootPath: vscode.Uri | null; - -// const WEBSITE_ID = 'WEBSITE_ID'; -const ymlPattern = '.yml'; // /\.(yml)$/i; -const weblinkPattern = 'weblink.yml'; // /\.weblink\.yml$/i; - -// export const getRenderingContext = async (): Promise => { -// rootPath = PortalWebView.getPortalRootFolder(); - -// console.log(rootPath); -// const map = new Map(); -// map.set('isFetchXmlForCSREnabled', true) -// if (rootPath) { -// return { -// webpages: await getWebpages(), -// contentSnippets: await getContentSnippets(), -// webTemplates: await getWebTemplates(), -// siteMarkers: await getSiteMarker(), -// siteSettings: await getSiteSetting(), -// entityLists: await getEntityLists(), -// entityForms: await getEntityForms(), -// webForms: await getWebForms(), -// weblinks: await getWeblinks(), -// weblinkSets: await getWeblinkSets(), -// website: await getWebsite(), -// pageTemplates: await getPageTemplates(), -// dataResolverExtras: {}, -// resx: {}, -// featureConfig: map, -// entityAttributeMetadata: [] as IEntityAttributeMetadata[], -// lcid: '' as string, -// isBootstrapV5: false as boolean, -// } -// } -// else { -// return {} -// } - - -// } - -enum ContextProperty { - WEBPAGE_YAML = '.webpage.yml', - WEBPAGE_COPY = '.webpage.copy.html', - WEBPAGE_CSS = '.webpage.custom_css.css', - WEBPAGE_JS = '.webpage.custom_javascript.js', - WEBPAGE_SUMMARY = '.webpage.summary.html', - CONTENT_SNIPPET_YAML = '.contentsnippet.yml', - CONTENT_SNIPPET_VALUE = '.contentsnippet.value.html', - WEB_TEMPLATE_YAML = '.webtemplate.yml', - WEB_TEMPLATE_SOURCE = '.webtemplate.source.html', - SITE_MARKER = 'sitemarker.yml', - SITE_SETTING = 'sitesetting.yml', - ENTITY_LIST = '.list.yml', - ENTITY_FORM = '.basicform.yml', - WEB_FORM = '.advancedform.yml', - WEB_LINK = '.weblink.yml', - WEB_LINK_SET = '.weblinkset.yml', - WEBSITE = 'website.yml', - PAGE_TEMPLATE = '.pagetemplate.yml', - UNKNOWN_PROPERTY = '' -} -enum ContextPropertyKey { - WEBPAGE = 'adx_webpageid', - CONTENT_SNIPPET = 'adx_contentsnippetid', - WEB_TEMPLATE = 'adx_webtemplateid', - SITE_MARKER = 'adx_sitemarkerid', - SITE_SETTING = 'adx_sitesettingid', - ENTITY_LIST = 'adx_entitylistid', - ENTITY_FORM = 'adx_entityformid', - WEB_FORM = 'adx_webformid', - WEB_LINK = 'adx_weblinkid', - WEB_LINK_SET = 'adx_weblinksetid', - WEBSITE = 'adx_websiteid', - PAGE_TEMPLATE = 'adx_pagetemplateid', -} - - - -const getFileName = (fileName: string) => { - const lastSlashIndex = fileName.lastIndexOf('/'); - if (lastSlashIndex !== -1) { - return fileName.substring(lastSlashIndex + 1); - } else { - // If there is no forward slash in the input string, return the original string. - return fileName; - } -} - -const findObjectIndexByProperty = ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - array: any, - key: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - value: any -): number => { - for (let i = 0; i < array.length; i++) { - if (array[i][key] === value) { - return i; - } - } - return -1; -} - -const removeExtension = (fileName: string, extn: string): string => { - // Using slice to remove the extension from the end - return fileName.slice(0, -extn.length); -} - - -// export const updateContext = async (previewEngineContext: IPreviewEngineContext) => { -// //check entity type -// const fileName = getFileName(vscode.window.activeTextEditor?.document.fileName ?? ''); -// const fileUri = vscode.window.activeTextEditor?.document.uri || fallbackURI; -// const entityType: ContextProperty = getEntityType(fileName); - -// switch (entityType) { -// case ContextProperty.SITE_MARKER: -// previewEngineContext.siteMarkers = await getSiteMarker(); -// break; -// case ContextProperty.SITE_SETTING: -// previewEngineContext.siteSettings = await getSiteSetting(); -// break; -// case ContextProperty.WEBSITE: -// // update private website record too -// previewEngineContext.website = await getWebsite(); -// break; -// case ContextProperty.WEB_LINK: -// previewEngineContext.weblinks = await getWeblinks(); -// break; -// case ContextProperty.WEB_LINK_SET: { -// const obj = await webLinkSetHelper(fileUri); -// const value = obj[ContextPropertyKey.WEB_LINK_SET as unknown as keyof WeblinkSet]; -// const index = findObjectIndexByProperty(previewEngineContext.weblinkSets, ContextPropertyKey.WEB_LINK_SET, value); -// if (index != -1 && previewEngineContext.weblinkSets) { -// previewEngineContext.weblinkSets[index] = obj; -// } else { -// previewEngineContext.weblinkSets = await getWeblinkSets(); -// } -// break; -// } -// case ContextProperty.ENTITY_FORM: -// { -// const obj = await entityFormHelper(fileUri); -// const value = obj[ContextPropertyKey.ENTITY_FORM as unknown as keyof EntityForm]; -// const index = findObjectIndexByProperty(previewEngineContext.entityForms, ContextPropertyKey.ENTITY_FORM, value); -// if (index != -1 && previewEngineContext.entityForms) { -// previewEngineContext.entityForms[index] = obj; -// } else { -// previewEngineContext.entityForms = await getEntityForms(); -// } -// break; -// } -// case ContextProperty.ENTITY_LIST: -// { -// const obj = await entityListHelper(fileUri); -// const value = obj[ContextPropertyKey.ENTITY_LIST as unknown as keyof EntityList]; -// const index = findObjectIndexByProperty(previewEngineContext.entityLists, ContextPropertyKey.ENTITY_LIST, value); -// if (index != -1 && previewEngineContext.entityLists) { -// previewEngineContext.entityLists[index] = obj; -// } else { -// previewEngineContext.entityLists = await getEntityLists(); -// } -// break; -// } -// case ContextProperty.WEB_FORM: -// { -// const obj = await webFormHelper(fileUri); -// const value = obj[ContextPropertyKey.WEB_FORM as unknown as keyof WebForm]; -// const index = findObjectIndexByProperty(previewEngineContext.webForms, ContextPropertyKey.WEB_FORM, value); -// if (index != -1 && previewEngineContext.webForms) { -// previewEngineContext.webForms[index] = obj; -// } else { -// previewEngineContext.webForms = await getWebForms(); -// } -// break; -// } -// case ContextProperty.PAGE_TEMPLATE: -// { -// const obj = await pageTemplateHelper(fileUri); -// const value = obj[ContextPropertyKey.PAGE_TEMPLATE as unknown as keyof PageTemplate]; -// const index = findObjectIndexByProperty(previewEngineContext.pageTemplates, ContextPropertyKey.PAGE_TEMPLATE, value); -// if (index != -1 && previewEngineContext.pageTemplates) { -// previewEngineContext.pageTemplates[index] = obj; -// } else { -// previewEngineContext.pageTemplates = await getPageTemplates(); -// } -// break; -// } -// case ContextProperty.WEBPAGE_YAML: -// case ContextProperty.WEBPAGE_COPY: -// case ContextProperty.WEBPAGE_CSS: -// case ContextProperty.WEBPAGE_JS: -// case ContextProperty.WEBPAGE_SUMMARY: -// { -// const obj = await webPageHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); -// const value = obj[ContextPropertyKey.WEBPAGE as unknown as keyof Webpage]; -// const index = findObjectIndexByProperty(previewEngineContext.webpages, ContextPropertyKey.WEBPAGE, value);//default-offline-page -// if (index != -1 && previewEngineContext.webpages) { -// previewEngineContext.webpages[index] = obj; -// } else { -// previewEngineContext.webpages = await getWebpages(); -// } -// break; -// } -// case ContextProperty.WEB_TEMPLATE_YAML: -// case ContextProperty.WEB_TEMPLATE_SOURCE: -// { -// const obj = await webTemplateHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); -// const value = obj[ContextPropertyKey.WEB_TEMPLATE as unknown as keyof WebTemplate]; -// const index = findObjectIndexByProperty(previewEngineContext.webTemplates, ContextPropertyKey.WEB_TEMPLATE, value);//breadcrumb -// if (index != -1 && previewEngineContext.webTemplates) { -// previewEngineContext.webTemplates[index] = obj; -// } else { -// previewEngineContext.webTemplates = await getWebTemplates(); -// } -// break; -// } -// case ContextProperty.CONTENT_SNIPPET_YAML: -// case ContextProperty.CONTENT_SNIPPET_VALUE: -// { -// const obj = await contentSnippetHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); -// const value = obj[ContextPropertyKey.CONTENT_SNIPPET as unknown as keyof ContentSnippet]; -// const index = findObjectIndexByProperty(previewEngineContext.contentSnippets, ContextPropertyKey.CONTENT_SNIPPET, value);//confirm email - hindi -// if (index != -1 && previewEngineContext.contentSnippets) { -// previewEngineContext.contentSnippets[index] = obj; -// } else { -// previewEngineContext.contentSnippets = await getContentSnippets(); -// } -// break; -// } -// default: -// break; -// } -// } - -// const webPageHelper = async (pageUri: vscode.Uri): Promise => { -// const webpageYaml = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.yml' })); -// const webpageJS = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.custom_javascript.js' })); -// const webpageCSS = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.custom_css.css' })); -// const webpageCopy = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.copy.html' })); -// const webpageSummary = await vscode.workspace.fs.readFile(pageUri?.with({ path: pageUri.path + '.webpage.summary.html' })); -// const webpageRecord = load(new TextDecoder().decode(webpageYaml)) as Webpage -// webpageRecord.adx_customcss = new TextDecoder().decode(webpageCSS) -// webpageRecord.adx_customjavascript = new TextDecoder().decode(webpageJS) -// webpageRecord.adx_copy = new TextDecoder().decode(webpageCopy) -// webpageRecord.adx_summary = new TextDecoder().decode(webpageSummary) -// webpageRecord.adx_websiteid = WEBSITE_ID -// return webpageRecord; -// } - -// const getWebpages = async (): Promise => { -// const webpagesDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/web-pages' }) || fallbackURI); - -// const webpageArray: Webpage[] = []; -// for (const webpage of webpagesDirectory) { -// webpageArray.push(await webPageHelper(rootPath?.with({ path: rootPath.path + '/web-pages/' + webpage[0] + '/' + webpage[0] }) || fallbackURI)); - -// const contentPageDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/web-pages/' + webpage[0] + '/content-pages' }) || fallbackURI); -// for (const page of contentPageDirectory) { -// if (page[0].endsWith(ymlPattern)) { -// const pageName = page[0].slice(0, -12); -// webpageArray.push(await webPageHelper(rootPath?.with({ path: rootPath.path + '/web-pages/' + webpage[0] + '/content-pages/' + pageName }) || fallbackURI)); -// } -// } -// } -// return webpageArray; -// } - - -// const getWeblinks = async (): Promise => { -// const weblinksDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets' }) || fallbackURI); - -// const weblinksArray: Weblink[] = []; -// for (const link of weblinksDirectory) { -// const linkSubDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + link[0] }) || fallbackURI); -// for (const sublink of linkSubDirectory) { -// if (sublink[0].endsWith(weblinkPattern)) { -// const weblinkYaml = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + link[0] + `/${sublink[0]}` }) || fallbackURI); -// const weblinkRecord = load(new TextDecoder().decode(weblinkYaml)) as Weblink[] -// weblinksArray.push(...weblinkRecord); -// } -// } -// } -// return weblinksArray; -// } - -// const webLinkSetHelper = async (fileUri: vscode.Uri): Promise => { -// const weblinkSetYaml = await vscode.workspace.fs.readFile(fileUri); -// const weblinkSetRecord = load(new TextDecoder().decode(weblinkSetYaml)) as WeblinkSet; -// weblinkSetRecord.adx_websiteid = WEBSITE_ID; -// return weblinkSetRecord; -// }; - - -// const getWeblinkSets = async (): Promise => { -// const weblinkSetsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets' }) || fallbackURI); - -// const weblinkSetsArray: WeblinkSet[] = []; -// for (const weblinkSet of weblinkSetsDirectory) { -// const linkSubDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + weblinkSet[0] }) || fallbackURI); -// for (const sublink of linkSubDirectory) { -// if (!sublink[0].endsWith(weblinkPattern)) { -// weblinkSetsArray.push(await webLinkSetHelper(rootPath?.with({ path: rootPath.path + '/weblink-sets/' + weblinkSet[0] + `/${sublink[0]}` }) || fallbackURI)); // adx_title not in pac data but is manadatory, studio sends as undefined. -// } -// } -// } -// return weblinkSetsArray; -// } - -// const contentSnippetHelper = async (fileUri: vscode.Uri): Promise => { -// const snippetYaml = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + 'yml' })); -// const snippetValue = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + 'value.html' })); -// const snippetRecord = load(new TextDecoder().decode(snippetYaml)) as ContentSnippet -// snippetRecord.adx_value = new TextDecoder().decode(snippetValue); -// snippetRecord.adx_websiteid = '92d6c1b4-d84b-ee11-be6e-0022482d5cfb'; -// snippetRecord.stateCode = 0; //check with PAC SME on how to get this field -// return snippetRecord; -// }; - - -// const getContentSnippets = async (): Promise => { -// const contentSnippetsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/content-snippets' }) || fallbackURI); - -// const contentSnippetsArray: ContentSnippet[] = []; -// for (const contentSnippet of contentSnippetsDirectory) { -// const snippetSubDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/content-snippets/' + contentSnippet[0] }) || fallbackURI); -// for (const snippet of snippetSubDirectory) { -// if (snippet[0].endsWith(ymlPattern)) { -// contentSnippetsArray.push(await contentSnippetHelper(rootPath?.with({ path: rootPath.path + '/content-snippets/' + contentSnippet[0] + `/${snippet[0].slice(0, -3)}` }) || fallbackURI)); -// } -// } -// } -// return contentSnippetsArray; -// } - -// const pageTemplateHelper = async (fileUri: vscode.Uri): Promise => { -// const pageTemplateYaml = await vscode.workspace.fs.readFile(fileUri); -// const pageTemplateRecord = load(new TextDecoder().decode(pageTemplateYaml)) as PageTemplate; -// return pageTemplateRecord; -// }; - -// const getPageTemplates = async (): Promise => { -// const pageTemplatesDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/page-templates' }) || fallbackURI); - -// const pageTemplatesArray: PageTemplate[] = []; -// for (const pageTemplate of pageTemplatesDirectory) { -// pageTemplatesArray.push(await pageTemplateHelper(rootPath?.with({ path: rootPath.path + '/page-templates/' + pageTemplate[0] }) || fallbackURI)); -// } -// return pageTemplatesArray; -// } - -// const webTemplateHelper = async (fileUri: vscode.Uri): Promise => { -// const webTemplateYaml = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.webtemplate.yml' })); -// const webTemplateSource = await vscode.workspace.fs.readFile(fileUri?.with({ path: fileUri.path + '.webtemplate.source.html' })); -// const webTemplateSourceHTML = new TextDecoder().decode(webTemplateSource); -// const webTemplateRecord = load(new TextDecoder().decode(webTemplateYaml)) as WebTemplate; -// webTemplateRecord.adx_source = webTemplateSourceHTML; -// webTemplateRecord.adx_websiteid = WEBSITE_ID; -// return webTemplateRecord; -// }; - - -// const getWebTemplates = async (): Promise => { -// const webTemplatesDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/web-templates' }) || fallbackURI); - -// const webTemplatesArray: WebTemplate[] = []; -// for (const webTemplate of webTemplatesDirectory) { -// webTemplatesArray.push(await webTemplateHelper(rootPath?.with({ path: rootPath.path + '/web-templates/' + webTemplate[0] + `/${webTemplate[0]}` }) || fallbackURI)); -// } -// return webTemplatesArray; -// } - -// const entityFormHelper = async (fileUri: vscode.Uri): Promise => { -// const entityFormYaml = await vscode.workspace.fs.readFile(fileUri); -// const entityFormRecord = load(new TextDecoder().decode(entityFormYaml)) as EntityForm; -// entityFormRecord.adx_websiteid = WEBSITE_ID; -// return entityFormRecord; -// }; - -// const getEntityForms = async (): Promise => { -// const entityFormsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/basic-forms' }) || fallbackURI); - -// const entityFormsArray: EntityForm[] = []; -// for (const entityForm of entityFormsDirectory) { -// entityFormsArray.push(await entityFormHelper(rootPath?.with({ path: rootPath.path + '/basic-forms/' + entityForm[0] + `/${entityForm[0]}.basicform.yml` }) || fallbackURI)); -// } -// return entityFormsArray; -// } - -// const entityListHelper = async (fileUri: vscode.Uri): Promise => { -// const entityListYaml = await vscode.workspace.fs.readFile(fileUri); -// const entityListRecord = load(new TextDecoder().decode(entityListYaml)) as EntityList; -// entityListRecord.adx_websiteid = WEBSITE_ID; -// return entityListRecord; -// }; - -// const getEntityLists = async (): Promise => { -// const entityListsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/lists' }) || fallbackURI); - -// const entityListsArray: EntityList[] = []; -// for (const entityList of entityListsDirectory) { -// if (entityList[0].endsWith(ymlPattern)) { -// entityListsArray.push(await entityListHelper(rootPath?.with({ path: rootPath.path + '/lists/' + entityList[0] }) || fallbackURI)); -// } -// } -// return entityListsArray; -// } - -// const webFormHelper = async (fileUri: vscode.Uri): Promise => { -// const webFormYaml = await vscode.workspace.fs.readFile(fileUri); -// const webFormRecord = load(new TextDecoder().decode(webFormYaml)) as WebForm; -// webFormRecord.adx_websiteid = WEBSITE_ID; -// return webFormRecord; -// }; - -// const getWebForms = async (): Promise => { -// const webFormsDirectory = await vscode.workspace.fs.readDirectory(rootPath?.with({ path: rootPath.path + '/advanced-forms' }) || fallbackURI); - -// const webFormsArray: WebForm[] = []; -// for (const webForm of webFormsDirectory) { -// webFormsArray.push(await webFormHelper(rootPath?.with({ path: rootPath.path + '/advanced-forms/' + webForm[0] + `/${webForm[0]}.advancedform.yml` }) || fallbackURI)); -// } -// return webFormsArray; -// } - -// const getWebsite = async (): Promise => { -// const website = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/website.yml' }) || fallbackURI); -// const websiteYaml = load(new TextDecoder().decode(website)); -// return websiteYaml as Website; -// } - -// const getSiteSetting = async (): Promise => { -// const siteSetting = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/sitesetting.yml' }) || fallbackURI); -// const siteSettingYaml = load(new TextDecoder().decode(siteSetting)) as SiteSetting[]; -// const siteSettingRecords = siteSettingYaml.map((siteSettingRecord) => { -// return { -// adx_websiteid: siteSettingRecord.adx_websiteid,//should we use static id? -// adx_name: siteSettingRecord.adx_name, -// adx_value: siteSettingRecord.adx_value, -// adx_sitesettingid: siteSettingRecord.adx_sitesettingid, - -// } - -// }); -// return siteSettingRecords; -// } - -// const getSiteMarker = async (): Promise => { -// const siteMarker = await vscode.workspace.fs.readFile(rootPath?.with({ path: rootPath.path + '/sitemarker.yml' }) || fallbackURI); -// const siteMarkerYaml = load(new TextDecoder().decode(siteMarker)) as SiteMarker[]; -// const siteMarkerRecords = siteMarkerYaml.map((siteMarkerRecord) => { -// return { -// adx_name: siteMarkerRecord.adx_name as string, -// adx_pageid: siteMarkerRecord.adx_pageid as string, -// adx_sitemarkerid: siteMarkerRecord.adx_sitemarkerid, -// adx_websiteid: WEBSITE_ID, - -// } - -// }); -// return siteMarkerRecords; -// } - diff --git a/src/client/power-pages/preview/Utils/Constants.ts b/src/client/power-pages/preview/Utils/Constants.ts new file mode 100644 index 00000000..9706fd93 --- /dev/null +++ b/src/client/power-pages/preview/Utils/Constants.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +export enum ContextProperty { + WEBPAGE_YAML = '.webpage.yml', + WEBPAGE_COPY = '.webpage.copy.html', + WEBPAGE_CSS = '.webpage.custom_css.css', + WEBPAGE_JS = '.webpage.custom_javascript.js', + WEBPAGE_SUMMARY = '.webpage.summary.html', + CONTENT_SNIPPET_YAML = '.contentsnippet.yml', + CONTENT_SNIPPET_VALUE = '.contentsnippet.value.html', + WEB_TEMPLATE_YAML = '.webtemplate.yml', + WEB_TEMPLATE_SOURCE = '.webtemplate.source.html', + SITE_MARKER = 'sitemarker.yml', + SITE_SETTING = 'sitesetting.yml', + ENTITY_LIST = '.list.yml', + ENTITY_FORM = '.basicform.yml', + WEB_FORM = '.advancedform.yml', + WEB_LINK = '.weblink.yml', + WEB_LINK_SET = '.weblinkset.yml', + WEBSITE = 'website.yml', + PAGE_TEMPLATE = '.pagetemplate.yml', + UNKNOWN_PROPERTY = '' +} +export enum ContextPropertyKey { + WEBPAGE = 'adx_webpageid', + CONTENT_SNIPPET = 'adx_contentsnippetid', + WEB_TEMPLATE = 'adx_webtemplateid', + SITE_MARKER = 'adx_sitemarkerid', + SITE_SETTING = 'adx_sitesettingid', + ENTITY_LIST = 'adx_entitylistid', + ENTITY_FORM = 'adx_entityformid', + WEB_FORM = 'adx_webformid', + WEB_LINK = 'adx_weblinkid', + WEB_LINK_SET = 'adx_weblinksetid', + WEBSITE = 'adx_websiteid', + PAGE_TEMPLATE = 'adx_pagetemplateid', +} \ No newline at end of file From cb1cbf94e697220b057cdb43dc08eb80e7c060a8 Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Mon, 11 Sep 2023 14:17:51 +0530 Subject: [PATCH 4/8] Bootstrap v5 flag --- src/client/power-pages/preview/PreviewEngineContext.ts | 9 +++++++-- src/client/power-pages/preview/Utils/Constants.ts | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/client/power-pages/preview/PreviewEngineContext.ts b/src/client/power-pages/preview/PreviewEngineContext.ts index be254d86..f013cfd6 100644 --- a/src/client/power-pages/preview/PreviewEngineContext.ts +++ b/src/client/power-pages/preview/PreviewEngineContext.ts @@ -20,7 +20,7 @@ import { Website } from '@maker-studio/powerportals-preview-engine/lib/Rendering import { PortalWebView } from '../../PortalWebView'; import { load } from 'js-yaml'; import * as vscode from 'vscode'; -import { ContextProperty, ContextPropertyKey } from './Utils/Constants'; +import { BootstrapSiteSetting, ContextProperty, ContextPropertyKey } from './Utils/Constants'; import { findObjectIndexByProperty, getFileProperties, removeExtension } from '../commonUtility'; const fallbackURI = vscode.Uri.file(''); @@ -33,8 +33,10 @@ export class PreviewEngineContext { private previewEngineContext: IPreviewEngineContext; private websiteRecord: Website; private rootPath: vscode.Uri | null; + private isBootstrapV5: boolean; constructor() { + this.isBootstrapV5 = false; this.previewEngineContext = {}; this.rootPath = PortalWebView.getPortalRootFolder(); this.websiteRecord = {} as Website; @@ -70,7 +72,7 @@ export class PreviewEngineContext { featureConfig: new Map(), entityAttributeMetadata: [] as IEntityAttributeMetadata[], lcid: '' as string, - isBootstrapV5: false as boolean, + isBootstrapV5: this.isBootstrapV5, } } else return {} } @@ -276,6 +278,9 @@ export class PreviewEngineContext { const siteSetting = await vscode.workspace.fs.readFile(this.rootPath?.with({ path: this.rootPath.path + '/sitesetting.yml' }) || fallbackURI); const siteSettingYaml = load(new TextDecoder().decode(siteSetting)) as SiteSetting[]; const siteSettingRecords = siteSettingYaml.map((siteSettingRecord) => { + if (siteSettingRecord.adx_name === BootstrapSiteSetting) { + this.isBootstrapV5 = siteSettingRecord.adx_value ? String(siteSettingRecord.adx_value).toLowerCase() === 'true' : false; + } return { adx_websiteid: this.websiteRecord.adx_websiteid, adx_name: siteSettingRecord.adx_name, diff --git a/src/client/power-pages/preview/Utils/Constants.ts b/src/client/power-pages/preview/Utils/Constants.ts index 9706fd93..4a9cbdcf 100644 --- a/src/client/power-pages/preview/Utils/Constants.ts +++ b/src/client/power-pages/preview/Utils/Constants.ts @@ -37,4 +37,6 @@ export enum ContextPropertyKey { WEB_LINK_SET = 'adx_weblinksetid', WEBSITE = 'adx_websiteid', PAGE_TEMPLATE = 'adx_pagetemplateid', -} \ No newline at end of file +} + +export const BootstrapSiteSetting = 'Site/BootstrapV5Enabled'; \ No newline at end of file From 1187b973726d5c7dd8aca4dc4647351a03c2ad0d Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Wed, 13 Sep 2023 13:26:26 +0530 Subject: [PATCH 5/8] Handle update for change in adx_website id --- .../power-pages/preview/PreviewEngineContext.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/client/power-pages/preview/PreviewEngineContext.ts b/src/client/power-pages/preview/PreviewEngineContext.ts index f013cfd6..feadb111 100644 --- a/src/client/power-pages/preview/PreviewEngineContext.ts +++ b/src/client/power-pages/preview/PreviewEngineContext.ts @@ -325,9 +325,17 @@ export class PreviewEngineContext { this.previewEngineContext.siteSettings = await this.getSiteSetting(); break; case ContextProperty.WEBSITE: - // update private website record too - this.previewEngineContext.website = await this.getWebsite(); - break; + { + const websiteObj = await this.getWebsite(); + if (websiteObj?.adx_websiteid === this.websiteRecord?.adx_websiteid) { + this.websiteRecord = websiteObj; + } + else { + this.websiteRecord = websiteObj; + this.previewEngineContext = await this.createEngineContext(); + } + break; + } case ContextProperty.WEB_LINK: this.previewEngineContext.weblinks = await this.getWeblinks(); break; From 102e490b1096bd3a175aa8566c63df56304ff44e Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Wed, 13 Sep 2023 13:27:44 +0530 Subject: [PATCH 6/8] Removing feature flag --- package.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/package.json b/package.json index 5bf9977b..ee110277 100644 --- a/package.json +++ b/package.json @@ -433,11 +433,6 @@ "type": "boolean", "markdownDescription": "Enable multiple file view in Visual Studio Code (Web extension only)", "default": true - }, - "powerPlatform.experimental.enableCSRPreviewFeature": { - "type": "boolean", - "markdownDescription": "Enable CSR preview in Visual Studio Code (Desktop extension only)", - "default": false } } }, From e1e29548811a10106595170b175353961b73dadf Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Sun, 17 Sep 2023 21:47:54 +0530 Subject: [PATCH 7/8] Removing comments --- src/client/power-pages/preview/PreviewEngineContext.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client/power-pages/preview/PreviewEngineContext.ts b/src/client/power-pages/preview/PreviewEngineContext.ts index feadb111..b35eb88a 100644 --- a/src/client/power-pages/preview/PreviewEngineContext.ts +++ b/src/client/power-pages/preview/PreviewEngineContext.ts @@ -406,7 +406,7 @@ export class PreviewEngineContext { { const obj = await this.webPageHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); const value = obj[ContextPropertyKey.WEBPAGE as unknown as keyof Webpage]; - const index = findObjectIndexByProperty(this.previewEngineContext.webpages, ContextPropertyKey.WEBPAGE, value);//default-offline-page + const index = findObjectIndexByProperty(this.previewEngineContext.webpages, ContextPropertyKey.WEBPAGE, value); if (index != -1 && this.previewEngineContext.webpages) { this.previewEngineContext.webpages[index] = obj; } else { @@ -419,7 +419,7 @@ export class PreviewEngineContext { { const obj = await this.webTemplateHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); const value = obj[ContextPropertyKey.WEB_TEMPLATE as unknown as keyof WebTemplate]; - const index = findObjectIndexByProperty(this.previewEngineContext.webTemplates, ContextPropertyKey.WEB_TEMPLATE, value);//breadcrumb + const index = findObjectIndexByProperty(this.previewEngineContext.webTemplates, ContextPropertyKey.WEB_TEMPLATE, value); if (index != -1 && this.previewEngineContext.webTemplates) { this.previewEngineContext.webTemplates[index] = obj; } else { @@ -432,7 +432,7 @@ export class PreviewEngineContext { { const obj = await this.contentSnippetHelper(fileUri?.with({ path: removeExtension(fileUri.path, entityType) })); const value = obj[ContextPropertyKey.CONTENT_SNIPPET as unknown as keyof ContentSnippet]; - const index = findObjectIndexByProperty(this.previewEngineContext.contentSnippets, ContextPropertyKey.CONTENT_SNIPPET, value);//confirm email - hindi + const index = findObjectIndexByProperty(this.previewEngineContext.contentSnippets, ContextPropertyKey.CONTENT_SNIPPET, value); if (index != -1 && this.previewEngineContext.contentSnippets) { this.previewEngineContext.contentSnippets[index] = obj; } else { From 7607f7d755fd14e3b7d67b7d2c8686351686b552 Mon Sep 17 00:00:00 2001 From: Shivika Gupta Date: Sun, 17 Sep 2023 21:51:06 +0530 Subject: [PATCH 8/8] Adding npmrc entry for consuming preview-engine package --- .npmrc | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..16b55840 --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +registry=https://msazure.pkgs.visualstudio.com/_packaging/power-platform-ux/npm/registry/ +always-auth=true \ No newline at end of file