Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions blocks/browse/da-browse/da-browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ export default class DaBrowse extends LitElement {
return this.shadowRoot.querySelector('da-new');
}

get browseListItems() {
// eslint-disable-next-line no-underscore-dangle
return this.shadowRoot.querySelector('.da-list-type-browse')?._listItems || [];
}

isRootFolder(path) {
return path.split('/').length <= 2;
}

renderNew() {
return html`
<da-new
Expand All @@ -139,7 +148,12 @@ export default class DaBrowse extends LitElement {
}

renderSearch() {
return html`<da-search @updated=${this.handleSearch} fullpath="${this.details.fullpath}"></da-search>`;
return html`
<da-search
@updated=${this.handleSearch}
fullpath="${this.details.fullpath}"
.browseItems="${this.browseListItems}">
</da-search>`;
}

renderList(type, fullpath, select, sort, drag) {
Expand All @@ -157,16 +171,21 @@ export default class DaBrowse extends LitElement {
render() {
return html`
<div class="da-tablist" role="tablist" aria-label="Dark Alley content">
${this._tabItems.map((tab, idx) => html`
<button
id="tab-${tab.id}"
type="button"
role="tab"
aria-selected="${tab.selected}"
aria-controls="tabpanel-${tab.id}"
@click=${() => { this.handleTabClick(idx); }}>
<span class="focus">${tab.title}</span>
</button>`)}
${this._tabItems.map((tab, idx) => {
if (tab.id === 'search' && this.isRootFolder(this.details.fullpath)) {
return nothing;
}
return html`
<button
id="tab-${tab.id}"
type="button"
role="tab"
aria-selected="${tab.selected}"
aria-controls="tabpanel-${tab.id}"
@click=${() => { this.handleTabClick(idx); }}>
<span class="focus">${tab.title}</span>
</button>`;
})}
</div>
<div class="da-list-header context-${this.context}">
<da-breadcrumbs fullpath="${this.details.fullpath}" depth="${this.details.depth}"></da-breadcrumbs>
Expand Down
55 changes: 54 additions & 1 deletion blocks/browse/da-search/da-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@ const { crawl, Queue } = await import(`${getNx()}/public/utils/tree.js`);
const { default: getStyle } = await import(`${getNx()}/utils/styles.js`);
const STYLE = await getStyle(import.meta.url);

const DEFAULT_LOCALES = ['langstore'];

function getLocales(translate) {
const locales = new Set(DEFAULT_LOCALES);

translate?.languages?.data?.forEach((lang) => {
lang.locales?.split(',').forEach((loc) => {
const dir = loc.split('/').find((part) => part?.trim() !== '');
if (dir) {
locales.add(dir.trim());
}
});
});

return locales;
}

export default class DaSearch extends LitElement {
static properties = {
fullpath: { type: String },
browseItems: { type: Array },
_term: { state: true },
_total: { state: true },
_matches: { state: true },
Expand Down Expand Up @@ -54,6 +72,40 @@ export default class DaSearch extends LitElement {
this._time = null;
}

async getSearchScope(startPath) {
const isSiteFolder = startPath.split('/').length === 3;
if (!isSiteFolder) {
return { paths: [startPath], files: [] };
}

const resp = await daFetch(`${DA_ORIGIN}/source${startPath}/.da/translate.json`);
if (!resp.ok) {
return { paths: [startPath], files: [] };
}

const translate = await resp.json();
const locales = getLocales(translate);

if (!locales.size || !this.browseItems?.length) {
return { paths: [startPath], files: [] };
}

const paths = [];
const files = [];

this.browseItems.forEach((item) => {
if (!locales.has(item.name)) {
if (item.ext) {
files.push(item);
} else {
paths.push(item.path);
}
}
});

return { paths, files };
}

async getMatches(startPath, term) {
const searchTypes = ['.html', '.json', '.svg'];

Expand Down Expand Up @@ -97,7 +149,8 @@ export default class DaSearch extends LitElement {
}
};

const { results } = crawl({ path: startPath, callback: searchFile, throttle: 10 });
const { paths, files } = await this.getSearchScope(startPath);
const { results } = crawl({ path: paths, callback: searchFile, throttle: 10, files });
await results;
}

Expand Down
Loading
Loading