Skip to content

Commit

Permalink
recently updated fragments (#110)
Browse files Browse the repository at this point in the history
* latest updated fragments for demo

* style update

* support path param
  • Loading branch information
yesil authored Dec 6, 2024
1 parent d324e82 commit 40f440e
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 16 deletions.
21 changes: 10 additions & 11 deletions studio/src/aem/aem-fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AemFragments extends LitElement {
/**
* @type {AEM}
*/
#aem;
aem;

/**
*
Expand All @@ -79,12 +79,11 @@ class AemFragments extends LitElement {
throw new Error(
'Either the bucket or baseUrl attribute is required.',
);
this.#aem = new AEM(this.bucket, this.baseUrl);
this.style.display = 'none';
this.aem = new AEM(this.bucket, this.baseUrl);
}

async selectFragment(x, y, fragment) {
const latest = await this.#aem.sites.cf.fragments.getById(fragment.id);
const latest = await this.aem.sites.cf.fragments.getById(fragment.id);
Object.assign(fragment, latest);
fragment.refreshFrom(latest);
this.setFragment(fragment);
Expand All @@ -102,7 +101,7 @@ class AemFragments extends LitElement {
}

async getTopFolders() {
const { children } = await this.#aem.folders.list(ROOT);
const { children } = await this.aem.folders.list(ROOT);
const ignore = window.localStorage.getItem('ignore_folders') || [
'images',
];
Expand Down Expand Up @@ -184,7 +183,7 @@ class AemFragments extends LitElement {
bubbles: true,
}),
);
let fragmentData = await this.#aem.sites.cf.fragments.getById(
let fragmentData = await this.aem.sites.cf.fragments.getById(
this.searchText,
);
if (this.tags) {
Expand Down Expand Up @@ -231,22 +230,22 @@ class AemFragments extends LitElement {
if (this.isFragmentId(this.searchText)) {
await this.searchFragmentByUUID();
} else {
const cursor = await this.#aem.sites.cf.fragments.search(
const cursor = await this.aem.sites.cf.fragments.search(
this.#search,
);
await this.processFragments(cursor, search);
}
}

async saveFragment() {
let fragment = await this.#aem.sites.cf.fragments.save(this.fragment);
let fragment = await this.aem.sites.cf.fragments.save(this.fragment);
if (!fragment) throw new Error('Failed to save fragment');
aemFragmentCache.get(fragment.id)?.refreshFrom(fragment);
}

async copyFragment() {
const oldFragment = this.fragment;
const fragment = await this.#aem.sites.cf.fragments.copy(oldFragment);
const fragment = await this.aem.sites.cf.fragments.copy(oldFragment);
const newFragment = new Fragment(fragment, this);
aemFragmentCache?.add(newFragment);
if (this.searchText) {
Expand All @@ -259,11 +258,11 @@ class AemFragments extends LitElement {
}

async publishFragment() {
await this.#aem.sites.cf.fragments.publish(this.fragment);
await this.aem.sites.cf.fragments.publish(this.fragment);
}

async deleteFragment() {
await this.#aem.sites.cf.fragments.delete(this.fragment);
await this.aem.sites.cf.fragments.delete(this.fragment);
if (this.searchText) {
const fragmentIndex = this.#searchResult.indexOf(this.fragment);
this.#searchResult.splice(fragmentIndex, 1);
Expand Down
6 changes: 5 additions & 1 deletion studio/src/aem/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AEM {
* @param {string} [params.query] - The search query
* @returns A generator function that fetches all the matching data using a cursor that is returned by the search API
*/
async *searchFragment({ path, query = '', tags = [], sort }) {
async *searchFragment({ path, query = '', tags = [], sort }, limit) {
const filter = {
path,
};
Expand All @@ -80,6 +80,10 @@ class AEM {
query: JSON.stringify(searchQuery),
};

if (limit) {
params.limit = limit;
}

let cursor;
while (true) {
if (cursor) {
Expand Down
2 changes: 1 addition & 1 deletion studio/src/aem/content-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ContentNavigation extends LitElement {
if (!this.inSelection) {
this.source.clearSelection();
}
this.toggleTopFoldersDisabled(this.inSelection);
// this.toggleTopFoldersDisabled(this.inSelection);
this.notify();
}

Expand Down
99 changes: 99 additions & 0 deletions studio/src/mas-recently-updated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { html, LitElement, nothing } from 'lit';

import { Fragment } from './aem/fragment.js';
import { AEM } from './aem/aem.js';

class MasRecentlyUpdated extends LitElement {
static get properties() {
return {
baseUrl: { type: String, attribute: 'base-url' },
bucket: { type: String },
fragments: { type: Array, state: true },
loading: { type: Boolean, reflect: true },
path: { type: String },
source: { type: String },
};
}

createRenderRoot() {
return this;
}

constructor() {
super();
this.fragments = [];
this.loading = true;
this.renderItem = this.renderItem.bind(this);
}

updated(changedProperties) {
if (changedProperties.has('path')) this.loadFragments();
}

connectedCallback() {
super.connectedCallback();
this.aem = new AEM(this.bucket, this.baseUrl);
this.source = document.getElementById(this.source);
this.loadFragments();
}

handleClick(e) {
clearTimeout(this.tooltipTimeout);
const currentTarget = e.currentTarget;
this.tooltipTimeout = setTimeout(() => {
currentTarget.classList.add('has-tooltip');
}, 500);
}

handleMouseLeave(e) {
clearTimeout(this.tooltipTimeout);
e.currentTarget.classList.remove('has-tooltip');
}

handleDoubleClick(e, fragment) {
clearTimeout(this.tooltipTimeout);
e.currentTarget.classList.remove('has-tooltip');
this.source.selectFragment(e.clientX, e.clientY, fragment);
}

async loadFragments() {
this.loading = true;
this.fragments = [];
const cursor = await this.aem.sites.cf.fragments.search(
{
sort: [{ on: 'modifiedOrCreated', order: 'DESC' }],
path: `/content/dam/mas/${this.path}`,
// tags: ['mas:status/DEMO']
},
6,
);
const result = await cursor.next();
this.fragments = result.value.map((item) => new Fragment(item, this));
this.source.addToCache(this.fragments);
this.loading = false;
}

renderItem(fragment) {
return html`<merch-card
@click="${this.handleClick}"
@mouseleave="${this.handleMouseLeave}"
@dblclick="${(e) => this.handleDoubleClick(e, fragment)}"
>
<aem-fragment fragment="${fragment.id}" ims author></aem-fragment>
<sp-status-light
size="l"
variant="${fragment.statusVariant}"
></sp-status-light>
</merch-card>`;
}

render() {
if (!this.path) return nothing;
return html` <h2>Recently Updated</h2>
<div class="container">
${this.fragments.map(this.renderItem)}
</div>`;
}
}

customElements.define('mas-recently-updated', MasRecentlyUpdated);
14 changes: 11 additions & 3 deletions studio/src/studio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './editors/merch-card-editor.js';
import './rte/rte-field.js';
import './rte/rte-link-editor.js';
import './mas-top-nav.js';
import './mas-recently-updated.js';
import './mas-folder-picker.js';
import { contentIcon } from './img/content-icon.js';
import { promosIcon } from './img/promos-icon.js';
Expand Down Expand Up @@ -340,6 +341,15 @@ class MasStudio extends LitElement {
`;
}

get recentlyUpdated() {
return html`<mas-recently-updated
source="aem"
base-url="${this.baseUrl}"
path="${this.path}"
>
</mas-recently-updated>`;
}

customRenderItem(item) {
if (!item) return html`<sp-table-cell></sp-table-cell>`;
return html` <sp-table-cell>${item.variant}</sp-table-cell>`;
Expand Down Expand Up @@ -390,9 +400,7 @@ class MasStudio extends LitElement {
</div>
</div>
</div>
<div class="recently-updated">
<h2>Recently Updated</h2>
</div>
<div class="recently-updated">${this.recentlyUpdated}</div>
</div>
`;
}
Expand Down
15 changes: 15 additions & 0 deletions studio/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ rte-field {
border-radius: var(--spectrum-corner-radius-100);
}

merch-card:not([variant]) {
display: none;
}

merch-card {
cursor: pointer;
user-select: none;
Expand Down Expand Up @@ -277,6 +281,17 @@ rte-field {
margin-inline-end: 16px;
}

mas-recently-updated[loading] {
visibility: hidden;
}

mas-recently-updated > .container {
display: flex;
gap: 16px;
flex-wrap: wrap;
padding-inline-start: 0;
}

#splash-container h1 {
font-size: 32px;
font-weight: bold;
Expand Down

0 comments on commit 40f440e

Please sign in to comment.