-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathshell.js
More file actions
87 lines (79 loc) · 2.7 KB
/
shell.js
File metadata and controls
87 lines (79 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Shell module for handling iframe-based content loading and communication
* @module shell
*/
import { loadIms } from '../../utils/ims.js';
const IMS_DETAILS = await loadIms();
const CHANNEL = new MessageChannel();
/**
* Parses the current URL to extract view, organization, repository, reference, path, search, and hash information
* @returns {Object} Object containing parsed URL components
* @property {string} view - The view type (defaults to 'fullscreen')
* @property {string} org - Organization name from URL
* @property {string} repo - Repository name from URL
* @property {string} ref - Reference/branch name (defaults to 'main')
* @property {string} path - Path components joined with '/'
* @property {string} search - Original search query string
* @property {string} hash - Original hash fragment from the URL
*/
function getParts() {
// Get path parts
const view = 'fullscreen';
const { pathname, search, hash } = window.location;
const pathSplit = pathname.split('/');
pathSplit.splice(0, 2);
const [org, repo, ...path] = pathSplit;
const ref = new URLSearchParams(search).get('ref') || 'main';
return {
view,
org,
repo,
ref,
path: path.join('/'),
search,
hash,
};
}
/**
* Constructs the appropriate URL based on the reference type, forwarding parent
* search params to the iframe
* @returns {string} The constructed URL for the iframe
*/
function getUrl() {
const { org, repo, ref, path, search, hash } = getParts();
const hashPart = hash ? `#${hash}` : '';
if (ref === 'local') return `http://localhost:3000/${path}.html${search}${hashPart}`;
return `https://${ref}--${repo}--${org}.aem.live/${path}.html${search}${hashPart}`;
}
/**
* Handles iframe load event and sets up message channel communication
* @param {Object} event - Load event object
* @param {HTMLIFrameElement} event.target - The loaded iframe element
*/
function handleLoad({ target }) {
CHANNEL.port1.onmessage = (e) => {
if (e.data.action === 'setTitle') {
document.title = e.data.details;
}
};
const message = {
ready: true,
token: IMS_DETAILS.accessToken?.token,
context: getParts(),
};
setTimeout(() => {
target.contentWindow.postMessage(message, '*', [CHANNEL.port2]);
}, 750);
}
/**
* Initializes the shell by creating, configuring, and appending an iframe
* @param {HTMLElement} el - The container element for the iframe
*/
export default function init(el) {
if (!document.querySelector('header')) document.body.classList.add('no-shell');
const iframe = document.createElement('iframe');
iframe.setAttribute('allow', 'clipboard-write *');
iframe.addEventListener('load', handleLoad);
iframe.src = getUrl();
el.append(iframe);
}