Skip to content
Draft
11 changes: 8 additions & 3 deletions blocks/browse/da-browse/da-browse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LitElement, html, nothing } from 'da-lit';
import { DA_ORIGIN } from '../../shared/constants.js';
import { daFetch, getFirstSheet } from '../../shared/utils.js';
import { DA_HLX } from '../../shared/constants.js';
import { getFirstSheet } from '../../shared/utils.js';
import { daApi } from '../../shared/da-api.js';
import { getNx, sanitizePathParts } from '../../../scripts/utils.js';

// Components
Expand Down Expand Up @@ -82,8 +83,12 @@ export default class DaBrowse extends LitElement {
async getEditor(reFetch) {
const DEF_EDIT = '/edit#';

if (DA_HLX) { // TODO handle editor path for hlx6
return DEF_EDIT;
}

if (reFetch) {
const resp = await daFetch(`${DA_ORIGIN}/config/${this.details.owner}/`);
const resp = await daApi.getConfig(`/${this.details.owner}/`);
if (!resp.ok) return DEF_EDIT;
const json = await resp.json();

Expand Down
16 changes: 6 additions & 10 deletions blocks/browse/da-list-item/da-list-item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LitElement, html, nothing, until } from 'da-lit';
import { DA_ORIGIN } from '../../shared/constants.js';
import { daFetch, aemAdmin } from '../../shared/utils.js';
import { aemAdmin } from '../../shared/utils.js';
import { daApi } from '../../shared/da-api.js';
import { getNx } from '../../../scripts/utils.js';
import getEditPath from '../shared.js';
import { formatDate } from '../../edit/da-versions/helpers.js';
Expand Down Expand Up @@ -73,7 +73,7 @@ export default class DaListItem extends LitElement {
}

async updateDAStatus() {
const resp = await daFetch(`${DA_ORIGIN}/versionlist${this.path}`);
const resp = await daApi.getVersionList(this.path);
if (!resp.ok) return;
const json = await resp.json();
if (json.length === 0) {
Expand Down Expand Up @@ -131,18 +131,14 @@ export default class DaListItem extends LitElement {
this._preview = null;
this._live = null;

const formData = new FormData();
formData.append('destination', newPath);
const opts = { body: formData, method: 'POST' };

this.name = newName;
this.path = newPath;
this.rename = false;
this._isRenaming = true;
this.date = Date.now();

const showStatus = setTimeout(() => { this.setStatus('Renaming', 'Please be patient. Renaming items with many children can take time.'); }, 5000);
const resp = await daFetch(`${DA_ORIGIN}/move${oldPath}`, opts);
const resp = await daApi.move(oldPath, newPath);

if (resp.status === 204) {
clearTimeout(showStatus);
Expand Down Expand Up @@ -204,7 +200,7 @@ export default class DaListItem extends LitElement {
let externalUrlPromise;
if (this.ext === 'link') {
path = nothing;
externalUrlPromise = daFetch(`${DA_ORIGIN}/source${this.path}`)
externalUrlPromise = daApi.getSource(this.path)
.then((response) => response.json())
.then((data) => data.externalUrl);
}
Expand All @@ -216,8 +212,8 @@ export default class DaListItem extends LitElement {
</span>
` : html`
<span class="da-item-list-item-type ${this.ext ? 'da-item-list-item-type-file' : 'da-item-list-item-type-folder'} ${this.ext ? `da-item-list-item-icon-${this.ext}` : ''}">
</span>
`}
</span>
<div class="da-item-list-item-name">${this.name}</div>
<div class="da-item-list-item-date">${this.ext === 'link' ? nothing : this.renderDate()}</div>
</a>`;
Expand Down
63 changes: 42 additions & 21 deletions blocks/browse/da-list/da-list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { LitElement, html, repeat, nothing } from 'da-lit';
import { DA_ORIGIN } from '../../shared/constants.js';
import { DA_HLX } from '../../shared/constants.js';
import { getNx, sanitizePathParts } from '../../../scripts/utils.js';
import { daFetch, aemAdmin } from '../../shared/utils.js';
import { aemAdmin } from '../../shared/utils.js';
import { daApi } from '../../shared/da-api.js';

import '../da-list-item/da-list-item.js';

Expand Down Expand Up @@ -97,12 +98,33 @@ export default class DaList extends LitElement {
this.dispatchEvent(event);
}

transformList(json) {
if (!DA_HLX) return json;

/*
* name without extension
* ext is the extension
* lastModified is last-modified in millis
* path is full plus original name with extension
*/

const transformed = [];
for (const item of json) {
const [name, ext] = item.name.split('.');
const lastModified = new Date(item['last-modified']).getTime();
const path = `${this.fullpath}/${item.name}`;

transformed.push({ name, ext, lastModified, path });
}
return transformed;
}

async getList() {
try {
const resp = await daFetch(`${DA_ORIGIN}/list${this.fullpath}`);
const resp = await daApi.getList(this.fullpath);
if (resp.permissions) this.handlePermissions(resp.permissions);
const json = await resp.json();
return json;
return this.transformList(json);
} catch {
this._emptyMessage = 'Not permitted';
return [];
Expand Down Expand Up @@ -212,24 +234,23 @@ export default class DaList extends LitElement {
const moveToTrash = api === 'move' && !item.path.includes('/.trash/') && item.destination.includes('/.trash/');

try {
while (continuation) {
let body;

if (type !== 'delete') {
body = new FormData();
body.append('destination', item.destination);
if (continuationToken) body.append('continuation-token', continuationToken);
}

const opts = { method, body };
const resp = await daFetch(`${DA_ORIGIN}/${api}${item.path}`, opts);
if (resp.status === 204) {
continuation = false;
break;
if (type === 'delete') {
const resp = await daApi.deleteSource(item.path);
if (resp.status !== 204) throw new Error('Could not delete');
} else {
while (continuation) {
const resp = await (api === 'move'
? daApi.move(item.path, item.destination, continuationToken)
: daApi.copy(item.path, item.destination, continuationToken));

if (resp.status === 204) {
continuation = false;
break;
}
const json = await resp.json();
({ continuationToken } = json);
if (!continuationToken) continuation = false;
}
const json = await resp.json();
({ continuationToken } = json);
if (!continuationToken) continuation = false;
}

item.isChecked = false;
Expand Down
9 changes: 3 additions & 6 deletions blocks/browse/da-list/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SUPPORTED_FILES, DA_ORIGIN } from '../../../shared/constants.js';
import { SUPPORTED_FILES } from '../../../shared/constants.js';
import { sanitizePath, sanitizePathParts } from '../../../../scripts/utils.js';
import { daFetch } from '../../../shared/utils.js';
import { daApi } from '../../../shared/da-api.js';

const MAX_DEPTH = 1000;

Expand Down Expand Up @@ -84,14 +84,11 @@ export async function getFullEntryList(entries) {

export async function handleUpload(list, fullpath, file) {
const { data, path } = file;
const formData = new FormData();
formData.append('data', data);
const opts = { method: 'POST', body: formData };
const sanitizedPath = sanitizePath(path);
const postpath = `${fullpath}${sanitizedPath}`;

try {
await daFetch(`${DA_ORIGIN}/source${postpath}`, opts);
await daApi.saveSource(postpath, { blob: data, method: 'POST' });
file.imported = true;

const [displayName] = sanitizedPath.split('/').slice(1);
Expand Down
10 changes: 9 additions & 1 deletion blocks/browse/da-new/da-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LitElement, html } from 'da-lit';
import { saveToDa } from '../../shared/utils.js';
import { getNx } from '../../../scripts/utils.js';
import getEditPath from '../shared.js';
import { daApi } from '../../shared/da-api.js';

// Styles & Icons
const { default: getStyle } = await import(`${getNx()}/utils/styles.js`);
Expand Down Expand Up @@ -68,6 +69,7 @@ export default class DaNew extends LitElement {

let ext;
let formData;
let method = 'PUT';
switch (this._createType) {
case 'document':
ext = 'html';
Expand All @@ -83,6 +85,12 @@ export default class DaNew extends LitElement {
new Blob([JSON.stringify({ externalUrl: this._externalUrl })], { type: 'application/json' }),
);
break;
case 'folder':
if (daApi.isHlx) {
this._createName += '/';
method = 'POST';
}
break;
default:
break;
}
Expand All @@ -92,7 +100,7 @@ export default class DaNew extends LitElement {
if (ext && ext !== 'link') {
window.location = editPath;
} else {
await saveToDa({ path, formData });
await saveToDa({ path, formData, method });
const item = { name: this._createName, path };
if (ext) item.ext = ext;
this.sendNewItem(item);
Expand Down
12 changes: 4 additions & 8 deletions blocks/browse/da-search/da-search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LitElement, html, nothing } from 'da-lit';
import { DA_ORIGIN } from '../../shared/constants.js';
import { getNx } from '../../../scripts/utils.js';
import { daFetch } from '../../shared/utils.js';
import { daApi } from '../../shared/da-api.js';

const { crawl, Queue } = await import(`${getNx()}/public/utils/tree.js`);

Expand Down Expand Up @@ -69,7 +68,7 @@ export default class DaSearch extends LitElement {
let match;

try {
const resp = await daFetch(`${DA_ORIGIN}/source${file.path}`);
const resp = await daApi.getSource(file.path);
const text = await resp.text();
// Log empty files
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -163,14 +162,11 @@ export default class DaSearch extends LitElement {
let retryCount = prevRetry;

const getFile = async () => {
const getResp = await daFetch(`${DA_ORIGIN}/source${file.path}`);
const getResp = await daApi.getSource(file.path);
const text = await getResp.text();
const replacedText = text.replaceAll(this._term, replace.value);
const blob = new Blob([replacedText], { type: 'text/html' });
const formData = new FormData();
formData.append('data', blob);
const opts = { method: 'PUT', body: formData };
const postResp = await daFetch(`${DA_ORIGIN}/source${file.path}`, opts);
const postResp = await daApi.saveSource(file.path, { blob });
if (!postResp.ok) return { error: 'Error saving file' };
this._matches += 1;
return file;
Expand Down
6 changes: 3 additions & 3 deletions blocks/edit/da-assets/da-assets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DOMParser as proseDOMParser, Fragment } from 'da-y-wrapper';
import { getNx } from '../../../scripts/utils.js';
import { DA_ORIGIN } from '../../shared/constants.js';
import { daFetch, getFirstSheet } from '../../shared/utils.js';
import { daApi } from '../../shared/da-api.js';
import getPathDetails from '../../shared/pathDetails.js';

const { loadStyle } = await import(`${getNx()}/scripts/nexter.js`);
Expand All @@ -15,7 +15,7 @@ const CONFS = {};

async function fetchConf(path) {
if (CONFS[path]) return CONFS[path];
const resp = await daFetch(`${DA_ORIGIN}/config${path}`);
const resp = await daApi.getConfig(path);
if (!resp.ok) return null;

fullConfJsons[path] = await resp.json();
Expand All @@ -37,7 +37,7 @@ async function fetchValue(path, key) {
}

function constructConfigPaths(owner, repo) {
return [`/${owner}/${repo}/`, `/${owner}/`];
return [`/${owner}/${repo}/`];
}

// Note: this is called externally to determine if the button should be visible.
Expand Down
5 changes: 2 additions & 3 deletions blocks/edit/da-content/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DA_ORIGIN } from '../../../shared/constants.js';
import { daFetch } from '../../../shared/utils.js';
import { daApi } from '../../../shared/da-api.js';

async function getConfSheet(org) {
const resp = await daFetch(`${DA_ORIGIN}/config/${org}/`);
const resp = await daApi.getConfig(`/${org}/`);
if (!resp.ok) return null;
const json = await resp.json();
return json?.data?.data || json?.data;
Expand Down
34 changes: 29 additions & 5 deletions blocks/edit/da-library/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// eslint-disable-next-line import/no-unresolved
import { DOMParser } from 'da-y-wrapper';
import { getDaAdmin } from '../../../shared/constants.js';
import getPathDetails from '../../../shared/pathDetails.js';
import { daFetch, getFirstSheet } from '../../../shared/utils.js';
import { daApi } from '../../../shared/da-api.js';
import { getConfKey, openAssets } from '../../da-assets/da-assets.js';
import { fetchKeyAutocompleteData } from '../../prose/plugins/slashMenu/keyAutocomplete.js';
import { sanitizeName } from '../../../../scripts/utils.js';

const DA_ORIGIN = getDaAdmin();
const REPLACE_CONTENT = '<content>';
const DA_CONFIG = '/.da/config.json';
const DA_PLUGINS = [
Expand Down Expand Up @@ -68,7 +67,7 @@ export async function getItems(sources, listType, format) {
}

async function getDaLibraries(owner, repo) {
const resp = await daFetch(`${DA_ORIGIN}/source/${owner}/${repo}${DA_CONFIG}`);
const resp = await daApi.getSource(`/${owner}/${repo}${DA_CONFIG}`);
if (!resp.ok) return [];

const json = await resp.json();
Expand Down Expand Up @@ -159,7 +158,7 @@ function calculateSources(org, repo, sheetPath) {
}

async function getConfigLibraries(org, repo) {
const resp = await daFetch(`${DA_ORIGIN}/config/${org}/${repo}/`);
const resp = await daApi.getConfig(`/${org}/${repo}/`);
if (!resp.ok) return null;
const { library } = await resp.json();
if (!library) return null;
Expand Down Expand Up @@ -256,6 +255,25 @@ export function getPreviewUrl(previewUrl) {
const [, , org, site, ...split] = url.pathname.split('/');
return `https://main--${site}--${org}.aem.page/${split.join('/')}`;
}
if (url.origin === daApi.origin) {
if (daApi.isHlx) {
// HLX6: /org/sites/repo/source/path...
// Assuming previewUrl is sourceUrl.
// If path is /org/sites/repo/source/rest
// Site is repo. Org is org.
const parts = url.pathname.split('/');
// parts: ['', org, 'sites', repo, 'source', ...rest]
if (parts[2] === 'sites' && parts[4] === 'source') {
const org = parts[1];
const repo = parts[3];
const rest = parts.slice(5).join('/');
return `https://main--${repo}--${org}.aem.page/${rest}`;
}
} else {
const [, , org, site, ...split] = url.pathname.split('/');
return `https://main--${site}--${org}.aem.page/${split.join('/')}`;
}
}
} catch {
return false;
}
Expand All @@ -275,7 +293,13 @@ export function getEdsUrlVars(url) {
const [, org, site] = urlObj.pathname.split('/');
return [org, site, 'main'];
}
if (urlObj.origin.includes('admin.da.live')) {
if (urlObj.origin.includes('admin.da.live') || urlObj.origin === daApi.origin) {
if (daApi.isHlx && urlObj.origin === daApi.origin) {
const parts = urlObj.pathname.split('/');
if (parts[2] === 'sites' && parts[4] === 'source') {
return [parts[1], parts[3], 'main'];
}
}
const [, , org, site] = urlObj.pathname.split('/');
return [org, site, 'main'];
}
Expand Down
Loading
Loading