summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/init_gitlab_web_ide.js
blob: 140f2895a29d5012199a639d8f333497f58e2803 (plain)
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
import { cleanTrailingSlash } from './stores/utils';

export const initGitlabWebIDE = async (el) => {
  const { start } = await import('@gitlab/web-ide');

  const { gitlab_url: gitlabUrl } = window.gon;
  const baseUrl = new URL(process.env.GITLAB_WEB_IDE_PUBLIC_PATH, window.location.origin);

  // what: Pull what we need from the element. We will replace it soon.
  const { cspNonce: nonce, branchName: ref, projectPath } = el.dataset;

  // what: Clean up the element, but preserve id.
  // why:  This way we don't inherit any `ide-loading` side-effects. This
  //       mirrors the behavior of Vue when it mounts to an element.
  const newEl = document.createElement(el.tagName);
  newEl.id = el.id;
  newEl.classList.add('gl--flex-center', 'gl-relative', 'gl-h-full');

  el.replaceWith(newEl);

  // what: Trigger start on our new mounting element
  await start(newEl, {
    baseUrl: cleanTrailingSlash(baseUrl.href),
    projectPath,
    gitlabUrl,
    ref,
    nonce,
  });
};