diff --git a/.vscode/launch.json b/.vscode/launch.json index c8ec8010070..69ef39fd038 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,20 @@ // 'm365 spo site get --url /', you'd use: // "args": ["spo", "site", "get", "--url", "/"] // after debugging, revert changes so that they won't end up in your PR - "args": [], + "args": [ + "spo", + "page", + "text", + "add", + "--webUrl", + "https://contoso.sharepoint.com/sites/audienceTest", + "--pageName", + "Test5.aspx", + "--text", + "Divider", + "--section", + "1" + ], "console": "integratedTerminal", "env": { "NODE_OPTIONS": "--enable-source-maps" diff --git a/src/m365/spo/commands/page/clientsidepages.ts b/src/m365/spo/commands/page/clientsidepages.ts index 032a448b905..273e8b0cd67 100644 --- a/src/m365/spo/commands/page/clientsidepages.ts +++ b/src/m365/spo/commands/page/clientsidepages.ts @@ -265,7 +265,8 @@ function reindex(collection?: { order: number, columns?: { order: number }[], co */ export class ClientSidePage { public sections: CanvasSection[] = []; - + public pageSettings?: PageSettings; + public backgroundSettings?: BackgroundSettings; /** * Converts a json object to an escaped string appropriate for use in attributes when storing client-side controls * @@ -324,9 +325,17 @@ export class ClientSidePage { html.push(this.sections[i].toHtml()); } - html.push(""); + if (this.backgroundSettings) { + html.push(this.backgroundSettings.toHtml()); + } - return html.join(""); + if (this.pageSettings) { + html.push(this.pageSettings.toHtml()); + } + + html.push(""); + const result = html.join(""); + return result; } /** @@ -344,20 +353,27 @@ export class ClientSidePage { getBoundedDivMarkup(html, /
Hello world<\/p><\/div><\/div><\/div>/; + + assert.match(postStub.lastCall.args[0].data.CanvasContent1, regex); }); it('adds text to an empty modern page (debug)', async () => { sinon.stub(request, 'get').callsFake(async (opts) => { - if ((opts.url as string).indexOf(`https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/SitePages/page.aspx')?$expand=ListItemAllFields/ClientSideApplicationId`) > -1) { + if (opts.url === `https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/SitePages/page.aspx')?$expand=ListItemAllFields/ClientSideApplicationId`) { return { ListItemAllFields: { CommentsDisabled: false, @@ -227,9 +228,9 @@ describe(commands.PAGE_TEXT_ADD, () => { }); sinon.stub(request, 'post').callsFake(async (opts) => { - if ((opts.url as string).indexOf(`https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/sitepages/page.aspx')/ListItemAllFields`) > -1 && + if (opts.url === `https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/sitepages/page.aspx')/ListItemAllFields` && JSON.stringify(opts.data).indexOf(`","position":{"controlIndex":1,"sectionFactor":12,"sectionIndex":1,"zoneIndex":1}}\\">
Hello world
Hello world
Hello world<\/p><\/div><\/div>
Hello world<\/p><\/div><\/div><\/div>/; + + assert.match(postStub.lastCall.args[0].data.CanvasContent1, regex); }); it('adds text in the specified order to a modern page which already had some text', async () => { @@ -496,9 +500,9 @@ describe(commands.PAGE_TEXT_ADD, () => { throw 'Invalid request'; }); - sinon.stub(request, 'post').callsFake(async (opts) => { + const postStub = sinon.stub(request, 'post').callsFake(async (opts) => { if (opts.url === "https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/sitepages/page.aspx')/ListItemAllFields") { - return {}; + return; } throw 'Invalid request'; @@ -513,7 +517,10 @@ describe(commands.PAGE_TEXT_ADD, () => { order: 2 } }); - assert(loggerLogSpy.notCalled); + + const regex = /
Hello world<\/p><\/div><\/div>
Hello world 1.1<\/p><\/div><\/div>
Hello world 2<\/p><\/div><\/div><\/div>/; + + assert.match(postStub.lastCall.args[0].data.CanvasContent1, regex); }); it('adds text to a modern page without specifying the page file extension', async () => { @@ -587,9 +594,9 @@ describe(commands.PAGE_TEXT_ADD, () => { throw 'Invalid request'; }); - sinon.stub(request, 'post').callsFake(async (opts) => { + const postStub = sinon.stub(request, 'post').callsFake(async (opts) => { if (opts.url === "https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/sitepages/page.aspx')/ListItemAllFields") { - return {}; + return; } throw 'Invalid request'; @@ -603,12 +610,192 @@ describe(commands.PAGE_TEXT_ADD, () => { text: 'Hello world' } }); - assert(loggerLogSpy.notCalled); + + const regex = /
Hello world<\/p><\/div><\/div><\/div>/; + + assert.match(postStub.lastCall.args[0].data.CanvasContent1, regex); + }); + + it('adds text to a modern page with existing empty collapsable section', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === + `https://contoso.sharepoint.com/sites/team-a/_api/web/GetFileByServerRelativePath(DecodedUrl='/sites/team-a/SitePages/page.aspx')?$expand=ListItemAllFields/ClientSideApplicationId`) { + return { + ListItemAllFields: { + CommentsDisabled: false, + FileSystemObjectType: 0, + Id: 1, + ServerRedirectedEmbedUri: null, + ServerRedirectedEmbedUrl: '', + ContentTypeId: '0x0101009D1CB255DA76424F860D91F20E6C41180062FDF2882AB3F745ACB63105A3C623C9', + FileLeafRef: 'Home.aspx', + ComplianceAssetId: null, + WikiField: null, + Title: 'Home', + ClientSideApplicationId: 'b6917cb1-93a0-4b97-a84d-7cf49975d4ec', + PageLayoutType: 'Home', + CanvasContent1: '
Hello world<\/p><\/div><\/div>
text
text<\/p><\/div><\/div>
Hello world<\/p><\/div><\/div>