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
10 changes: 6 additions & 4 deletions blocks/edit/da-library/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line import/no-unresolved
import { DOMParser } from 'da-y-wrapper';
import getPathDetails from '../../../shared/pathDetails.js';
import { daFetch, aemAdmin, fetchDaConfigs, getFirstSheet } from '../../../shared/utils.js';
import { daFetch, aemAdmin, fetchDaConfigs, getFirstSheet, getSheetByName } from '../../../shared/utils.js';
import { openAssets } from '../../da-assets/da-assets.js';
import { fetchKeyAutocompleteData } from '../../prose/plugins/slashMenu/keyAutocomplete.js';
import { sanitizeName } from '../../../../scripts/utils.js';
Expand Down Expand Up @@ -109,9 +109,11 @@ function calculateSources(org, repo, sheetPath) {

async function fetchLibraryConfig(org, site) {
const configs = await fetchDaConfigs({ org, site });
const { library } = await configs[1];
if (!library) return [];
return library.data.reduce((acc, row) => {
const config = await configs[1];
if (!config) return [];
const libraryData = getSheetByName(config, 'library');
if (!libraryData) return [];
return libraryData.reduce((acc, row) => {
// Determine if a plugin should be visible based on query param
const allowed = getIsPluginAllowed(row.ref);
if (allowed) {
Expand Down
7 changes: 7 additions & 0 deletions blocks/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export const getSheetByIndex = (json, index = 0) => {
return json[Object.keys(json)[index]]?.data;
};

export const getSheetByName = (json, name) => {
if (json[':type'] !== 'multi-sheet') {
return json[':sheetname'] === name ? json.data : undefined;
}
return json[name]?.data;
};

export const getFirstSheet = (json) => getSheetByIndex(json, 0);

export async function contentLogin(owner, repo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('imageFocalPoint Plugin', () => {
return {
ok: true,
json: async () => ({
':type': 'multi-sheet',
library: {
data: [
{ title: 'Blocks', path: '/blocks.json', ref: 'main' },
Expand Down
33 changes: 33 additions & 0 deletions test/unit/blocks/shared/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
aemAdmin,
saveToDa,
getSheetByIndex,
getSheetByName,
getFirstSheet,
delay,
getSidekickConfig,
Expand Down Expand Up @@ -41,6 +42,38 @@ describe('getSheetByIndex', () => {
});
});

describe('getSheetByName', () => {
it('Returns data for the named sheet in a multi-sheet config', () => {
const json = { ':type': 'multi-sheet', library: { data: [{ title: 'Blocks', path: '/blocks' }] }, settings: { data: [{ key: 'x' }] } };
expect(getSheetByName(json, 'library')).to.deep.equal([{ title: 'Blocks', path: '/blocks' }]);
});

it('Returns undefined when the named sheet does not exist in a multi-sheet config', () => {
const json = { ':type': 'multi-sheet', settings: { data: [{ key: 'x' }] } };
expect(getSheetByName(json, 'library')).to.equal(undefined);
});

it('Returns data for a single-sheet config when :sheetname matches', () => {
const json = { ':type': 'sheet', ':sheetname': 'library', data: [{ title: 'Blocks', path: '/blocks' }] };
expect(getSheetByName(json, 'library')).to.deep.equal([{ title: 'Blocks', path: '/blocks' }]);
});

it('Returns undefined for a single-sheet config when :sheetname does not match', () => {
const json = { ':type': 'sheet', ':sheetname': 'settings', data: [{ title: 'Blocks', path: '/blocks' }] };
expect(getSheetByName(json, 'library')).to.equal(undefined);
});

it('Returns undefined for a single-sheet config with no :sheetname', () => {
const json = { ':type': 'sheet', data: [{ title: 'Blocks', path: '/blocks' }] };
expect(getSheetByName(json, 'library')).to.equal(undefined);
});

it('Returns undefined for a single-sheet config with matching :sheetname but no data', () => {
const json = { ':type': 'sheet', ':sheetname': 'library' };
expect(getSheetByName(json, 'library')).to.equal(undefined);
});
});

describe('getFirstSheet', () => {
it('Returns undefined for multi-sheet (index 0 is the type key)', () => {
// getFirstSheet calls getSheetByIndex with index 0, which is ':type'
Expand Down
Loading