Skip to content

Commit

Permalink
IFrame SDK for widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Sep 18, 2023
1 parent 869177d commit 0386b8f
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 18 deletions.
5 changes: 3 additions & 2 deletions backend/src/Squidex/wwwroot/scripts/editor-context.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
textarea {
box-sizing: border-box;
resize: none;
overflow: hidden;
overflow-x: hidden;
overflow-y: hidden;
width: 100%;
}
</style>
Expand All @@ -25,7 +26,7 @@
}
</script>

<textarea oninput="grow(this)" name="content" id="editor"></textarea>
<textarea oninput="grow(this)" name="content" id="editor"></textarea>

<script>
var element = document.getElementById('editor');
Expand Down
21 changes: 20 additions & 1 deletion backend/src/Squidex/wwwroot/scripts/editor-sdk.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare class EditorPlugin {
declare class SquidexSidebar {
/**
* Get the current context.
*/
Expand Down Expand Up @@ -29,6 +29,25 @@ declare class EditorPlugin {
clean(): void;
}

declare class SquidexWidget {
/**
* Get the current context.
*/
getContext(): any;

/**
* Register an function that is called when the sidebar is initialized.
*
* @param {function} callback: The callback to invoke.
*/
onInit(callback: () => void): void;
/**
* Clean the editor SDK.
*/
clean(): void;
}


declare class SquidexFormField {
/**
* Get the current value.
Expand Down
77 changes: 70 additions & 7 deletions backend/src/Squidex/wwwroot/scripts/editor-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function isArrayOfStrings(value) {
return true;
}

function SquidexPlugin() {
function SquidexSidebar() {
var initHandler;
var initCalled = false;
var contentHandler;
Expand Down Expand Up @@ -94,7 +94,7 @@ function SquidexPlugin() {

timer = measureAndNotifyParent();

var editor = {
var plugin = {
/**
* Get the current context.
*/
Expand Down Expand Up @@ -153,7 +153,70 @@ function SquidexPlugin() {
}
};

return editor;
return plugin;
}

function SquidexWidget() {
var initHandler;
var initCalled = false;
var context;

document.body.style.margin = '0';
document.body.style.padding = '0';

function raiseInit() {
if (initHandler && !initCalled && context) {
initHandler(context);
initCalled = true;
}
}

function eventListener(event) {
if (event.source !== window) {
var type = event.data.type;

if (type === 'init') {
context = event.data.context;

raiseInit();
}
}
}

window.addEventListener('message', eventListener, false);

var plugin = {
/**
* Get the current context.
*/
getContext: function () {
return context;
},

/**
* Register an function that is called when the sidebar is initialized.
*
* @param {function} callback: The callback to invoke.
*/
onInit: function (callback) {
if (!isFunction(callback)) {
return;
}

initHandler = callback;

raiseInit();
},

/**
* Clean the editor SDK.
*/
clean: function () {
window.removeEventListener('message', eventListener);
}
};

return plugin;
}

function SquidexFormField() {
Expand Down Expand Up @@ -310,7 +373,7 @@ function SquidexFormField() {

timer = measureAndNotifyParent();

var editor = {
var plugin = {
/**
* Get the current value.
*/
Expand Down Expand Up @@ -634,13 +697,13 @@ function SquidexFormField() {
* Clean the editor SDK.
*/
clean: function () {
if (timer) {
window.removeEventListener('message', eventListener);
window.removeEventListener('message', eventListener);

if (timer) {
timer();
}
}
};

return editor;
return plugin;
};
2 changes: 1 addition & 1 deletion backend/src/Squidex/wwwroot/scripts/sidebar-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
</script>

<textarea oninput="grow(this)" name="content" id="editor"></textarea>
<textarea oninput="grow(this)" name="content" id="editor"></textarea>

<script>
var element = document.getElementById('editor');
Expand Down
5 changes: 3 additions & 2 deletions backend/src/Squidex/wwwroot/scripts/sidebar-context.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
border: 0;
border-radius: 0;
resize: none;
overflow: hidden;
overflow-x: hidden;
overflow-y: hidden;
width: 100%;
}
</style>
Expand All @@ -27,7 +28,7 @@
}
</script>

<textarea oninput="grow(this)" name="content" id="editor"></textarea>
<textarea oninput="grow(this)" name="content" id="editor"></textarea>

<script>
var element = document.getElementById('editor');
Expand Down
45 changes: 45 additions & 0 deletions backend/src/Squidex/wwwroot/scripts/widget-context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">

<!-- Load the editor sdk from the local folder or https://cloud.squidex.io/scripts/editor-sdk.js -->
<script src="editor-sdk.js"></script>

<style>
textarea {
border-radius: 0;
border: 0;
bottom: 0;
box-sizing: border-box;
left: 0;
position: fixed;
resize: none;
right: 0;
top: 0;
}
</style>
</head>

<body>
<textarea name="content" id="editor"></textarea>

<script>
var element = document.getElementById('editor');

// When the field is instantiated it notifies the UI that it has been loaded.
//
// Furthermore it sends the current size to the parent.
var field = new SquidexFormField();

// Init is called once with a context that contains the app name, schema name and authentication information.
field.onInit(function (context) {
element.innerHTML = JSON.stringify(context, null, 2);

grow(element);
});
</script>
</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class DashboardConfigComponent {
}

if (changes.app) {
this.uiState.getAppUser('dashboard.grid', this.configDefaults).pipe(take(1))
this.uiState.getAppShared('dashboard.grid', this.configDefaults).pipe(take(1))
.subscribe(dto => {
this.setConfig(dto);
});
Expand Down Expand Up @@ -91,7 +91,6 @@ export class DashboardConfigComponent {

public resetConfig() {
this.setConfig(Types.clone(this.configDefaults));

this.saveConfig();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1 class="dashboard-title">{{ 'dashboard.welcomeTitle' | sqxTranslate: { user:
<sqx-content-summary-card [app]="app" [options]="item"></sqx-content-summary-card>
</ng-container>
<ng-container *ngSwitchCase="'iframe'">
<sqx-iframe-card [options]="item"></sqx-iframe-card>
<sqx-iframe-card [app]="app" [options]="item"></sqx-iframe-card>
</ng-container>
</ng-container>
</gridster-item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1 class="dashboard-title">{{ 'dashboard.welcomeTitle' | sqxTranslate: { user:
<sqx-support-card></sqx-support-card>
</ng-container>
<ng-container *ngSwitchCase="'iframe'">
<sqx-iframe-card [options]="item"></sqx-iframe-card>
<sqx-iframe-card [team]="team" [options]="item"></sqx-iframe-card>
</ng-container>
</ng-container>
</gridster-item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/

import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Input, ViewChild } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, HostListener, Input, ViewChild } from '@angular/core';
import { ApiUrlConfig, Types } from '@app/framework';
import { AppDto, AuthService, TeamDto } from '@app/shared/internal';

@Component({
selector: 'sqx-iframe-card',
Expand All @@ -14,13 +16,67 @@ import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Input, V
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class IFrameCardComponent implements AfterViewInit {
private readonly context: any;
private isInitialized = false;

@Input()
public options: any;

@ViewChild('iframe', { static: false })
public iframe!: ElementRef<HTMLIFrameElement>;

@Input()
public set team(value: TeamDto | undefined | null) {
if (value) {
this.context.teamId = value.id;
this.context.teamName = value.name;
}
}

@Input()
public set app(value: AppDto | undefined | null) {
if (value) {
this.context.appId = value.id;
this.context.appName = value.name;
}
}

constructor(apiUrl: ApiUrlConfig, authService: AuthService) {
this.context = { apiUrl: apiUrl.buildUrl('api'), user: authService.user };
}

public ngAfterViewInit() {
this.iframe.nativeElement.src = this.options?.src;
}

@HostListener('window:message', ['$event'])
public onWindowMessage(event: MessageEvent) {
if (event.source === this.iframe.nativeElement.contentWindow) {
const { type } = event.data;

if (type === 'started') {
this.isInitialized = true;

this.sendInit();
}
}
}

private sendInit() {
this.sendMessage('init', { context: this.context });
}

private sendMessage(type: string, payload: any) {
if (!this.iframe) {
return;
}

const iframe = this.iframe.nativeElement;

if (this.isInitialized && iframe.contentWindow && Types.isFunction(iframe.contentWindow.postMessage)) {
const message = { type, ...payload };

iframe.contentWindow.postMessage(message, '*');
}
}
}

0 comments on commit 0386b8f

Please sign in to comment.