Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Angular][Editing Integration] Render clientScripts / clientData #1924

Merged
merged 26 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ecdc6fa
raw first drafts
art-alexeyenko Aug 27, 2024
9cefd80
move code blocks into main placeholder
art-alexeyenko Aug 27, 2024
9930321
lint
art-alexeyenko Aug 29, 2024
94bdd72
tests WIP
art-alexeyenko Aug 30, 2024
1349b5b
with abstract classes
art-alexeyenko Sep 3, 2024
55393a7
with exported abstract classes
art-alexeyenko Sep 3, 2024
1f54427
WIP metadata placeholder: use context
art-alexeyenko Sep 3, 2024
4d1837c
lint
art-alexeyenko Sep 3, 2024
5af15c4
Fully working implementation
art-alexeyenko Sep 5, 2024
8069e9c
changelog, upgrade, small changes
art-alexeyenko Sep 5, 2024
f383a7d
last moment sneaky fix
art-alexeyenko Sep 5, 2024
bfb1b54
PR comments
art-alexeyenko Sep 5, 2024
ae84ce9
PR comments 2
art-alexeyenko Sep 5, 2024
43c020e
PR comments 3
art-alexeyenko Sep 5, 2024
8850629
Merge branch 'dev' into feature/jss-3507-editing-placeholder
art-alexeyenko Sep 5, 2024
8975dbb
lint
art-alexeyenko Sep 5, 2024
e808af3
Initial commit
illiakovalenko Sep 6, 2024
2cd7496
Added first unit test
illiakovalenko Sep 9, 2024
7d46a41
Merge branch 'dev' of https://github.com/Sitecore/jss into feature/JS…
illiakovalenko Sep 9, 2024
1b9cc0b
Latest changes
illiakovalenko Sep 9, 2024
926ae8b
Updated CHANGELOG, Upgrade Guide
illiakovalenko Sep 9, 2024
2198401
Removed extra line
illiakovalenko Sep 9, 2024
20cb5a6
Removed old changes
illiakovalenko Sep 9, 2024
34e1ee7
Added explicit check for "Preview" mode
illiakovalenko Sep 9, 2024
d9e7d73
Added explicit check for react EditingScripts
illiakovalenko Sep 9, 2024
0739b49
Updated Upgrade Guide and removed "any"
illiakovalenko Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Our versioning strategy is as follows:
- `scRichTextEmptyFieldEditingTemplate` for _scRichText_
- `scRouterLinkEmptyFieldEditingTemplate` for _scRouterLink_
- `scTextEmptyFieldEditingTemplate` for _scText_
* `[sitecore-jss-angular]` `[templates/angular-xmcloud]` Render clientScripts / clientData. The new `sc-editing-scripts` component is exposed from `sitecore-jss-angular` package and required to be rendered on the page to enable Metadata Edit mode. ([#1924](https://github.com/Sitecore/jss/pull/1924))
* `[sitecore-jss]` GenericFieldValue model is updated to accept Date type ([#1916](https://github.com/Sitecore/jss/pull/1916))

### 🛠 Breaking Change
Expand Down
6 changes: 6 additions & 0 deletions docs/upgrades/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ If you plan to use the Angular SDK with XMCloud, you will need to perform next s
});
```

* Make sure to render new `sc-editing-scripts` component (exposed by JssModule) in your Layout, since it's required for the editing integration:

```html
<sc-editing-scripts></sc-editing-scripts>
```

# @sitecore-jss/sitecore-jss-proxy

* Update the import statement
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ng-container>
<sc-editing-scripts></sc-editing-scripts>
</ng-container>
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';
import { ScriptsComponent } from './scripts.component';
import { JssModule } from '@sitecore-jss/sitecore-jss-angular';

@NgModule({
exports: [ScriptsComponent],
imports: [JssModule],
declarations: [ScriptsComponent],
})
export class ScriptsModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { Component, Renderer2 } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { EditMode, LayoutServicePageState } from '@sitecore-jss/sitecore-jss/layout';
import { PAGES_EDITING_MARKER } from '@sitecore-jss/sitecore-jss/editing';
import { inject } from '@angular/core/testing';
import { JssStateService } from '../services/jss-state.service';
import { EditingScriptsComponent } from './editing-scripts.component';

@Component({
selector: 'test-component',
template: `
<sc-editing-scripts></sc-editing-scripts>
`,
})
class TestComponent {}

fdescribe('<EditingScripts />', () => {
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
let fixture: ComponentFixture<TestComponent>;

const sharedData = {
clientScripts: [
'https://test.com/packages/page-extension/latest/page.js',
'https://test.com/horizon/canvas/horizon.canvas.js',
],
clientData: {
'hrz-canvas-state': {
itemId: '1329a009-1b93-4855-8855-894bc40d7135',
siteName: 'jss',
language: 'en',
},
'hrz-canvas-verification-token': {
token: 'test',
},
},
};

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent, EditingScriptsComponent],
providers: [
Renderer2,
JssStateService,
{ provide: DOCUMENT, useValue: document.implementation.createHTMLDocument() },
],
}).compileComponents();
fixture = TestBed.createComponent(TestComponent);
});

it('should not add editing scripts and client data when edit mode is Chromes', inject(
[JssStateService, DOCUMENT],
(stateService: JssStateService, _document: Document) => {
stateService.setState({
sitecore: {
context: {
editMode: EditMode.Chromes,
pageState: LayoutServicePageState.Edit,
...sharedData,
},
route: null,
},
});

fixture.detectChanges();

expect(_document.body.querySelector(`#${PAGES_EDITING_MARKER}`)).toBeNull();
expect(_document.body.querySelector('#hrz-canvas-state')).toBeNull();
expect(_document.body.querySelector('#hrz-canvas-verification-token')).toBeNull();
expect(
_document.body.querySelector(
'script[src="https://test.com/packages/page-extension/latest/page.js"]'
)
).toBeNull();
expect(
_document.body.querySelector(
'script[src="https://test.com/horizon/canvas/horizon.canvas.js"]'
)
).toBeNull();
}
));

it('should not add editing scripts and client data when page state is Normal', inject(
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
[JssStateService, DOCUMENT],
(stateService: JssStateService, _document: Document) => {
stateService.setState({
sitecore: {
context: {
editMode: EditMode.Metadata,
pageState: LayoutServicePageState.Normal,
...sharedData,
},
route: null,
},
});

fixture.detectChanges();

expect(_document.body.querySelector(`#${PAGES_EDITING_MARKER}`)).toBeNull();
expect(_document.body.querySelector('#hrz-canvas-state')).toBeNull();
expect(_document.body.querySelector('#hrz-canvas-verification-token')).toBeNull();
expect(
_document.body.querySelector(
'script[src="https://test.com/packages/page-extension/latest/page.js"]'
)
).toBeNull();
expect(
_document.body.querySelector(
'script[src="https://test.com/horizon/canvas/horizon.canvas.js"]'
)
).toBeNull();
}
));

it('should add editing scripts and client data when edit mode is Metadata', inject(
[JssStateService, DOCUMENT],
(stateService: JssStateService, _document: Document) => {
stateService.setState({
sitecore: {
context: {
editMode: EditMode.Metadata,
pageState: LayoutServicePageState.Edit,
...sharedData,
},
route: null,
},
});

fixture.detectChanges();

expect(_document.body.querySelector(`#${PAGES_EDITING_MARKER}`)).toBeDefined();
expect(_document.body.querySelector('#hrz-canvas-state')?.innerHTML).toEqual(
'{"itemId":"1329a009-1b93-4855-8855-894bc40d7135","siteName":"jss","language":"en"}'
);
expect(_document.body.querySelector('#hrz-canvas-verification-token')?.innerHTML).toEqual(
'{"token":"test"}'
);
expect(
_document.body.querySelector(
'script[src="https://test.com/packages/page-extension/latest/page.js"]'
)
).toBeTruthy();
expect(
_document.body.querySelector(
'script[src="https://test.com/horizon/canvas/horizon.canvas.js"]'
)
).toBeTruthy();
}
));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Component, OnInit, Renderer2, Inject } from '@angular/core';
import { getJssPagesClientData } from '@sitecore-jss/sitecore-jss/editing';
import { JssStateService } from '../services/jss-state.service';
import { DOCUMENT } from '@angular/common';
import { EditMode, LayoutServicePageState } from '@sitecore-jss/sitecore-jss/layout';

/**
* Component that renders editing scripts and client data for the current page in Sitecore Editor.
* Only renders scripts when Metadata mode is used.
*/
@Component({
selector: 'sc-editing-scripts',
template: '',
})
export class EditingScriptsComponent implements OnInit {
constructor(
private renderer: Renderer2,
private stateService: JssStateService,
@Inject(DOCUMENT) private document: Document
) {}

ngOnInit(): void {
const state = this.stateService.stateValue;
const { pageState, editMode, clientData, clientScripts } = state.sitecore?.context || {};

// Don't render anything if not in editing/preview mode
if (pageState === LayoutServicePageState.Normal) return;
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved

if (editMode === EditMode.Metadata) {
const jssClientData = { ...clientData, ...getJssPagesClientData() };
clientScripts?.forEach((src: string) => {
const scriptElement = this.renderer.createElement('script');
scriptElement.src = src;
this.renderer.appendChild(this.document.body, scriptElement);
});

Object.keys(jssClientData).forEach((id: string) => {
const scriptElement = this.renderer.createElement('script');
scriptElement.id = id;
scriptElement.type = 'application/json';
scriptElement.innerHTML = JSON.stringify((jssClientData as any)[id]);
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
this.renderer.appendChild(this.document.body, scriptElement);
});
}
}
}
3 changes: 3 additions & 0 deletions packages/sitecore-jss-angular/src/lib.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { RouterLinkDirective } from './components/router-link.directive';
import { TextDirective } from './components/text.directive';
import { JssComponentFactoryService } from './services/jss-component-factory.service';
import { JssStateService } from './services/jss-state.service';
import { EditingScriptsComponent } from './components/editing-scripts.component';

@NgModule({
imports: [CommonModule],
Expand All @@ -54,6 +55,7 @@ import { JssStateService } from './services/jss-state.service';
MissingComponentComponent,
HiddenRenderingComponent,
EditFrameComponent,
EditingScriptsComponent,
],
exports: [
FileDirective,
Expand All @@ -71,6 +73,7 @@ import { JssStateService } from './services/jss-state.service';
RichTextDirective,
TextDirective,
EditFrameComponent,
EditingScriptsComponent,
],
})
export class JssModule {
Expand Down
1 change: 1 addition & 0 deletions packages/sitecore-jss-angular/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { RenderEmptyDirective } from './components/render-empty.directive';
export { RenderComponentComponent } from './components/render-component.component';
export { DateDirective } from './components/date.directive';
export { EditFrameComponent } from './components/editframe.component';
export { EditingScriptsComponent } from './components/editing-scripts.component';
export {
ComponentNameAndType,
DYNAMIC_COMPONENT,
Expand Down