schemas as a tree replacing the isEntry behavior

This commit is contained in:
2024-10-05 00:35:38 +03:00
parent e06e1db671
commit 4ef16f3630
10 changed files with 150 additions and 46 deletions
+39
View File
@@ -0,0 +1,39 @@
<script>
import {createEventDispatcher, getContext, onMount} from "svelte";
import Icon from "../common/Icon.svelte";
import Folder from "./Folder.svelte";
const channel = getContext("channel");
export let folder;
export let schema;
export let expanded = folder.shoudlExpand;
function toggleExpand() {
expanded = !expanded;
}
</script>
<div class="sidebar-folder">
{#if folder.name !== ""}
<button class="sidebar-header" tabindex="0" on:click={toggleExpand}>
{folder.name ?? "Main"}
{#if expanded}
<Icon icon="circle-chevron-up"></Icon>
{:else}
<Icon icon="circle-chevron-down"></Icon>
{/if}
</button>
{/if}
{#if expanded}
{#each folder.folders as aFolder}
<Folder folder={aFolder} schema={schema} ></Folder>
{/each}
{#each folder.files as aSchema}
<a class="sidebar-item" class:active={aSchema.name === schema?.name}
aria-current="page"
href="{channel.lucentUrl}/content/{aSchema.name}">{aSchema.label}</a>
{/each}
{/if}
</div>
+48 -19
View File
@@ -1,13 +1,39 @@
<script>
import NavbarMenu from "./NavbarMenu.svelte";
import {getContext} from "svelte";
import Folder from "./Folder.svelte";
export let schema;
const channel = getContext("channel");
const readableSchemas = getContext("readableSchemas");
const fileSchemas = readableSchemas.filter((sc) => sc.type === "files");
const otherSchemas = readableSchemas.filter((sc) => !sc.isEntry && sc.type === "collection");
function addToFolder(tree, folderPath, aSchema) {
let shoudlExpand = aSchema.name === schema?.name;
if (folderPath === "") {
tree.files.push(aSchema)
return tree
}
const folderNames = folderPath.split(".");
folderNames.forEach(folderName => {
let queriedFolder = tree.folders.find(folder => folder.name === folderName)
if (!queriedFolder) {
queriedFolder = {name: folderName, files: [], folders: [], shoudlExpand: shoudlExpand};
}
folderNames.shift()
let remainingFolderPath = folderNames.join(".");
queriedFolder = addToFolder(queriedFolder, remainingFolderPath, aSchema)
tree.folders.push(queriedFolder);
})
return tree;
}
const schemaTree = readableSchemas.reduce((carry, schema) => {
carry = addToFolder(carry, schema.folder,schema)
return carry;
}, {name: "", files: [], folders: [], shoudlExpand:true});
// const fileSchemas = readableSchemas.filter((sc) => sc.type === "files");
// const otherSchemas = readableSchemas.filter((sc) => !sc.isEntry && sc.type === "collection");
</script>
<div class="sidebar-top">
@@ -18,22 +44,25 @@
<div class="sidebar">
<NavbarMenu
title="Content"
schemas={ readableSchemas.filter((sc) => sc.isEntry)}
schema={schema}
expanded={true}
/>
<Folder folder={schemaTree} {schema} ></Folder>
<NavbarMenu
title="Files"
schemas={ fileSchemas}
schema={schema}
/>
<NavbarMenu
title="Other"
schemas={ otherSchemas}
schema={schema}
/>
<!-- <NavbarMenu-->
<!-- title="Content"-->
<!-- schemas={ readableSchemas.filter((sc) => sc.isEntry)}-->
<!-- schema={schema}-->
<!-- expanded={true}-->
<!-- />-->
<!-- <NavbarMenu-->
<!-- title="Files"-->
<!-- schemas={ fileSchemas}-->
<!-- schema={schema}-->
<!-- />-->
<!-- <NavbarMenu-->
<!-- title="Other"-->
<!-- schemas={ otherSchemas}-->
<!-- schema={schema}-->
<!-- />-->
</div>
+7 -2
View File
@@ -3,8 +3,7 @@
import Icon from "../common/Icon.svelte";
const channel = getContext("channel");
export let schemas;
export let title;
export let folder;
export let schema;
export let expanded = false;
@@ -31,4 +30,10 @@
aria-current="page"
href="{channel.lucentUrl}/content/{aschema.name}">{aschema.label}</a>
{/each}
{#each schemas as aschema}
<a class="sidebar-item" class:active={aschema.name === schema?.name}
aria-current="page"
href="{channel.lucentUrl}/content/{aschema.name}">{aschema.label}</a>
{/each}
{/if}
+9
View File
@@ -21,8 +21,16 @@
flex-direction: column;
gap: 3px;
}
.sidebar-folder{
width: 100%;
margin: 3px 12px 3px;
.sidebar-folder{
margin-left: 5px;
}
}
.sidebar-header {
width: 100%;
display: flex;
cursor: pointer;
justify-content: space-between;
@@ -30,6 +38,7 @@
background: var(--p30);
font-size: 16px;
padding: 3px 12px 3px;
color: var(--text);
border: none;
border-radius: 12px;
+39 -20
View File
@@ -21,33 +21,20 @@ class CompileSchemas extends Command
$configDir = base_path(config('lucent.schemas_path'));
$schemasDirIterator = new DirectoryIterator($configDir);
$schemas = [];
$schemas = $this->scanDir([], $schemasDirIterator);
foreach ($schemasDirIterator as $file) {
if ($file->getExtension() !== "json") {
continue;
}
$schemaJson = file_get_contents($configDir . "/" . $file->getFilename());
$schema = json_decode($schemaJson, true);
if (empty($schema)) {
$this->error("Invalid JSON " . $file->getFilename());
return 0;
}
$schemas[] = $schema;
}
$schemas = collect($schemas)->sortBy("label")->values();
$roles = $schemas
->map([$schemaService, 'fromArray'])
->whereIn("type", [Type::COLLECTION, Type::FILES])
->reduce(fn($carry, Schema $schema) => array_merge(
$carry,
$schema->read,
$schema->write,
config("lucent.canInvite") ?? [],
config("lucent.canBuild") ?? [],
$carry,
$schema->read,
$schema->write,
config("lucent.canInvite") ?? [],
config("lucent.canBuild") ?? [],
), []);
$json = [
@@ -64,4 +51,36 @@ class CompileSchemas extends Command
$this->info("Lucent Schemas were updated");
}
private function scanDir(array $schemas, DirectoryIterator $directoryIterator, $dirName = ""): array
{
foreach ($directoryIterator as $file) {
if ($file->isDir()) {
if ($file->isDot()) {
continue;
}
$newDirName = $dirName.'.'.$file->getBasename();
$schemas = $this->scanDir($schemas, new DirectoryIterator($file->getPathname()), $newDirName);
continue;
}
if ($file->getExtension() !== "json") {
continue;
}
$schemaJson = file_get_contents($file->getPathname());
$schema = json_decode($schemaJson, true);
if (empty($schema)) {
$this->error("Invalid JSON " . $file->getFilename());
die;
}
$schema["folder"] = trim($dirName,".");
$schemas[] = $schema;
}
return $schemas;
}
}
@@ -23,6 +23,7 @@ class GenerateCollectionSchema extends Command
visible: [],
groups: [],
fields: Collection::make(),
folder: "",
);
$json = json_encode($schema, JSON_PRETTY_PRINT);
+2 -1
View File
@@ -23,7 +23,8 @@ class GenerateFileSchema extends Command
fields: Collection::make(),
disk: "lucent",
path: $name,
groups: []
groups: [],
folder: "",
);
$json = json_encode($schema, JSON_PRETTY_PRINT);
+2 -1
View File
@@ -15,10 +15,11 @@ class CollectionSchema implements Schema
function __construct(
public string $name,
public string $label,
public array $visible,
public array $groups,
public Collection $fields,
public bool $isEntry = false,
public string $folder = "",
public string $color = "",
public string $sortBy = "-_sys.updatedAt",
public ?string $cardTitle = null,
+1 -1
View File
@@ -20,7 +20,7 @@ class FilesSchema implements Schema
public string $disk,
public string $path,
public array $groups,
public bool $isEntry = false,
public string $folder = "",
public string $sortBy = "-_sys.updatedAt",
public string $color = "",
public ?string $cardTitle = null,
+2 -2
View File
@@ -23,7 +23,7 @@ class SchemaService
visible: $schemaArr["visible"] ?? [],
groups: $schemaArr["groups"] ?? [],
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
isEntry: $schemaArr["isEntry"] ?? false,
folder: $schemaArr["folder"] ?? "",
color: $schemaArr["color"] ?? "",
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
cardTitle: $schemaArr["titleTemplate"] ?? $schemaArr["cardTitle"] ?? null,
@@ -39,7 +39,7 @@ class SchemaService
disk: $schemaArr["disk"] ?? "lucent",
path: $schemaArr["path"] ?? $schemaArr["name"],
groups: $schemaArr["groups"] ?? [],
isEntry: $schemaArr["isEntry"] ?? false,
folder: $schemaArr["folder"] ?? "",
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
color: $schemaArr["color"] ?? "",
cardTitle: $schemaArr["titleTemplate"] ?? $schemaArr["cardTitle"] ?? null,