@cosmicdrift/kumiko-samples 0.394.0 → 0.396.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/packages/bundled-features/package.json +1 -1
- package/packages/bundled-features/src/inbound-provider-imap/changes.json +6 -0
- package/packages/bundled-features/src/inbound-provider-imap/feature.ts +3 -104
- package/packages/bundled-features/src/inbound-provider-imap/watch-mailbox.ts +109 -0
- package/packages/bundled-features/src/managed-pages/changes.json +7 -0
- package/packages/bundled-features/src/managed-pages/constants.ts +7 -0
- package/packages/bundled-features/src/managed-pages/feature.ts +6 -4
- package/packages/bundled-features/src/managed-pages/handlers/by-slug.query.ts +2 -0
- package/packages/bundled-features/src/managed-pages/handlers/page-body-guard.ts +61 -0
- package/packages/bundled-features/src/managed-pages/handlers/page-body-issues.ts +26 -0
- package/packages/bundled-features/src/managed-pages/handlers/page-crud.ts +17 -10
- package/packages/bundled-features/src/managed-pages/handlers/set.write.ts +15 -4
- package/packages/bundled-features/src/managed-pages/render-page-body.ts +28 -0
- package/packages/bundled-features/src/managed-pages/screens/page-screens.ts +6 -2
- package/packages/bundled-features/src/managed-pages/seeding.ts +13 -2
- package/packages/bundled-features/src/managed-pages/table.ts +6 -2
- package/packages/bundled-features/src/managed-pages/web/client-plugin.tsx +6 -4
- package/packages/bundled-features/src/managed-pages/web/constants.ts +14 -0
- package/packages/bundled-features/src/managed-pages/web/i18n.ts +13 -0
- package/packages/bundled-features/src/managed-pages/web/lossy-markdown.ts +71 -0
- package/packages/bundled-features/src/managed-pages/web/markdown-to-document.ts +27 -0
- package/packages/bundled-features/src/managed-pages/web/page-body-section.tsx +73 -0
- package/packages/bundled-features/src/template-resolver/README.md +2 -2
- package/packages/bundled-features/src/template-resolver/changes.json +8 -0
- package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +16 -16
- package/packages/bundled-features/src/template-resolver/feature.ts +14 -1
- package/packages/bundled-features/src/template-resolver/handlers/collection-set.write.ts +24 -6
- package/packages/bundled-features/src/template-resolver/handlers/collection-shared.ts +7 -0
- package/packages/bundled-features/src/template-resolver/handlers/set.write.ts +1 -0
- package/packages/bundled-features/src/template-resolver/handlers/shared.ts +9 -3
- package/packages/bundled-features/src/template-resolver/web/client-plugin.tsx +8 -576
- package/packages/bundled-features/src/template-resolver/web/collection-entry-create-form.tsx +105 -0
- package/packages/bundled-features/src/template-resolver/web/collection-lookup.ts +33 -0
- package/packages/bundled-features/src/template-resolver/web/constants.ts +3 -0
- package/packages/bundled-features/src/template-resolver/web/content-field.tsx +61 -0
- package/packages/bundled-features/src/template-resolver/web/content-tree-provider.ts +256 -0
- package/packages/bundled-features/src/template-resolver/web/document-reference-name.ts +12 -0
- package/packages/bundled-features/src/template-resolver/web/document-schema.ts +3 -11
- package/packages/bundled-features/src/template-resolver/web/i18n.ts +8 -0
- package/packages/bundled-features/src/template-resolver/web/legacy-variable-text.ts +30 -0
- package/packages/bundled-features/src/template-resolver/web/tenant-override.ts +7 -0
- package/packages/bundled-features/src/template-resolver/web/text-block-editor.tsx +92 -0
- package/packages/bundled-features/src/template-resolver/web/use-collection-editor-context.ts +42 -0
- package/packages/bundled-features/src/template-resolver/web/use-collection-entry-create.ts +92 -0
- package/packages/bundled-features/src/template-resolver/web/use-content-write.ts +81 -0
- package/packages/bundled-features/src/template-resolver/web/use-text-block-editor.ts +127 -0
- package/packages/bundled-features/src/tier-engine/changes.json +6 -0
- package/packages/bundled-features/src/tier-engine/feature.ts +9 -1
- package/samples/apps/use-all-bundled/feature-manifest.json +2 -2
- package/samples/apps/use-all-bundled/src/app/client.tsx +6 -1
- package/samples/apps/use-all-bundled/src/app/seed.ts +13 -1
- package/samples/apps/use-all-bundled/src/run-config.ts +12 -6
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { TargetRef } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { FieldIssue } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import { FormScreenLayout, usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
import { ContentField } from "./content-field.tsx";
|
|
6
|
+
import { useCollectionEntryCreate } from "./use-collection-entry-create.ts";
|
|
7
|
+
|
|
8
|
+
const SLUG_INVALID_ISSUE: FieldIssue = {
|
|
9
|
+
path: "slug",
|
|
10
|
+
code: "slug_invalid",
|
|
11
|
+
i18nKey: "template-resolver.editor.slugInvalid",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// A new entry in the app's own locale; the tree then lists it under that locale like any other.
|
|
15
|
+
// `tenantId` is the plugin's SystemAdmin tenant, the one the collection tree lists, so a new entry
|
|
16
|
+
// lands where the tree looks. Same rule as the tree: per-user collections stay on the session tenant.
|
|
17
|
+
export function createCollectionEntryCreateForm(
|
|
18
|
+
tenantId: string | undefined,
|
|
19
|
+
): (props: { readonly target: TargetRef; readonly onClose: () => void }) => ReactNode {
|
|
20
|
+
return function BoundCollectionEntryCreateForm(props) {
|
|
21
|
+
return <CollectionEntryCreateForm {...props} tenantId={tenantId} />;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type CreateState = ReturnType<typeof useCollectionEntryCreate>;
|
|
26
|
+
|
|
27
|
+
function CreateFields({ state }: { readonly state: CreateState }): ReactNode {
|
|
28
|
+
const { Field, Input, Banner } = usePrimitives();
|
|
29
|
+
const t = useTranslation();
|
|
30
|
+
const { canWrite } = state.editorContext;
|
|
31
|
+
return (
|
|
32
|
+
<>
|
|
33
|
+
{!canWrite && <Banner variant="info">{t("template-resolver.editor.readOnly")}</Banner>}
|
|
34
|
+
<Field
|
|
35
|
+
id="create-entry-slug"
|
|
36
|
+
label={t("template-resolver.editor.slugLabel")}
|
|
37
|
+
required
|
|
38
|
+
{...(state.slugInvalid && { issues: [SLUG_INVALID_ISSUE] })}
|
|
39
|
+
>
|
|
40
|
+
<Input
|
|
41
|
+
kind="text"
|
|
42
|
+
id="create-entry-slug"
|
|
43
|
+
name="create-entry-slug"
|
|
44
|
+
value={state.slug}
|
|
45
|
+
onChange={state.setSlug}
|
|
46
|
+
disabled={state.disabled}
|
|
47
|
+
required
|
|
48
|
+
/>
|
|
49
|
+
</Field>
|
|
50
|
+
<Field id="create-entry-title" label={t("template-resolver.editor.titleLabel")} required>
|
|
51
|
+
<Input
|
|
52
|
+
kind="text"
|
|
53
|
+
id="create-entry-title"
|
|
54
|
+
name="create-entry-title"
|
|
55
|
+
value={state.title}
|
|
56
|
+
onChange={state.setTitle}
|
|
57
|
+
disabled={state.disabled}
|
|
58
|
+
required
|
|
59
|
+
/>
|
|
60
|
+
</Field>
|
|
61
|
+
<ContentField
|
|
62
|
+
editorContext={state.editorContext}
|
|
63
|
+
value={state.content}
|
|
64
|
+
onChange={state.setContent}
|
|
65
|
+
disabled={state.disabled}
|
|
66
|
+
/>
|
|
67
|
+
{state.error !== null && <Banner variant="error">{state.error}</Banner>}
|
|
68
|
+
</>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function CollectionEntryCreateForm({
|
|
73
|
+
target,
|
|
74
|
+
tenantId,
|
|
75
|
+
}: {
|
|
76
|
+
readonly target: TargetRef;
|
|
77
|
+
readonly onClose: () => void;
|
|
78
|
+
readonly tenantId?: string | undefined;
|
|
79
|
+
}): ReactNode {
|
|
80
|
+
const { Button, PageHeader, Section } = usePrimitives();
|
|
81
|
+
const t = useTranslation();
|
|
82
|
+
const state = useCollectionEntryCreate(target, tenantId);
|
|
83
|
+
return (
|
|
84
|
+
<>
|
|
85
|
+
{PageHeader !== undefined && <PageHeader title={state.headerTitle} />}
|
|
86
|
+
<FormScreenLayout
|
|
87
|
+
onSubmit={() => {
|
|
88
|
+
void state.create();
|
|
89
|
+
}}
|
|
90
|
+
testId="collection-entry-create"
|
|
91
|
+
actions={
|
|
92
|
+
state.editorContext.canWrite ? (
|
|
93
|
+
<Button type="submit" loading={state.submitting} disabled={state.createDisabled}>
|
|
94
|
+
{t("template-resolver.editor.create")}
|
|
95
|
+
</Button>
|
|
96
|
+
) : undefined
|
|
97
|
+
}
|
|
98
|
+
>
|
|
99
|
+
<Section>
|
|
100
|
+
<CreateFields state={state} />
|
|
101
|
+
</Section>
|
|
102
|
+
</FormScreenLayout>
|
|
103
|
+
</>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isUiAccessGranted } from "@cosmicdrift/kumiko-framework/ui-types";
|
|
2
|
+
import type { FeatureSchema } from "@cosmicdrift/kumiko-renderer";
|
|
3
|
+
import { DEFAULT_COLLECTION_ACCESS } from "./constants.ts";
|
|
4
|
+
|
|
5
|
+
export type ContentCollectionSchema = NonNullable<FeatureSchema["contentCollections"]>[number];
|
|
6
|
+
|
|
7
|
+
// A node without a collectionId (the hand-wired text-block tree) has no
|
|
8
|
+
// collection to look up — contentFormat/variableSchema then fall back to
|
|
9
|
+
// their own callers' defaults.
|
|
10
|
+
export function findCollection(
|
|
11
|
+
features: readonly FeatureSchema[],
|
|
12
|
+
collectionId?: string,
|
|
13
|
+
): ContentCollectionSchema | undefined {
|
|
14
|
+
if (collectionId === undefined) return undefined;
|
|
15
|
+
for (const feature of features) {
|
|
16
|
+
const match = feature.contentCollections?.find((c) => c.id === collectionId);
|
|
17
|
+
if (match !== undefined) return match;
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Who may save follows the server: a collection's set handler carries the
|
|
23
|
+
// collection's own access rule (one rule for list, item and set), the public
|
|
24
|
+
// text-block pair stays TenantAdmin/SystemAdmin. Other users see the form read-only.
|
|
25
|
+
export function canWriteContent(
|
|
26
|
+
collection: Pick<ContentCollectionSchema, "access"> | undefined,
|
|
27
|
+
roles: readonly string[] | undefined,
|
|
28
|
+
): boolean {
|
|
29
|
+
if (collection !== undefined) {
|
|
30
|
+
return isUiAccessGranted(collection.access ?? DEFAULT_COLLECTION_ACCESS, roles);
|
|
31
|
+
}
|
|
32
|
+
return roles?.includes("TenantAdmin") === true || roles?.includes("SystemAdmin") === true;
|
|
33
|
+
}
|
|
@@ -63,6 +63,9 @@ export function isCollectionRefusedKind(
|
|
|
63
63
|
return isOneOf(COLLECTION_REFUSED_KINDS, kind);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
export const SLUG_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
|
|
67
|
+
export const SLUG_MAX_LENGTH = 80;
|
|
68
|
+
|
|
66
69
|
export const CONTENT_FORMATS = ["markdown", "mjml", "html", "plain"] as const;
|
|
67
70
|
export type ContentFormat = (typeof CONTENT_FORMATS)[number];
|
|
68
71
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ContentPreview, usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
|
|
2
|
+
import { type ReactNode, useId, useState } from "react";
|
|
3
|
+
import { DocumentBlocksContext } from "./document-editor.tsx";
|
|
4
|
+
import type { CollectionEditorContext } from "./use-collection-editor-context.ts";
|
|
5
|
+
|
|
6
|
+
export type ContentFieldProps = {
|
|
7
|
+
readonly editorContext: CollectionEditorContext;
|
|
8
|
+
readonly value: string;
|
|
9
|
+
readonly onChange: (value: string) => void;
|
|
10
|
+
readonly disabled: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// The content input shared by the edit and create forms: the collection's editor
|
|
14
|
+
// plus a toggle to its preview with the example variables substituted.
|
|
15
|
+
export function ContentField({
|
|
16
|
+
editorContext,
|
|
17
|
+
value,
|
|
18
|
+
onChange,
|
|
19
|
+
disabled,
|
|
20
|
+
}: ContentFieldProps): ReactNode {
|
|
21
|
+
const { Field, Button } = usePrimitives();
|
|
22
|
+
const t = useTranslation();
|
|
23
|
+
const contentEditorId = useId();
|
|
24
|
+
const [previewing, setPreviewing] = useState(false);
|
|
25
|
+
const { ContentEditor, contentFormat, documentBlocks, variableExamples, variables } =
|
|
26
|
+
editorContext;
|
|
27
|
+
return (
|
|
28
|
+
<Field
|
|
29
|
+
id={contentEditorId}
|
|
30
|
+
label={t("template-resolver.editor.contentLabel")}
|
|
31
|
+
labelAppendix={
|
|
32
|
+
<Button
|
|
33
|
+
type="button"
|
|
34
|
+
variant="secondary"
|
|
35
|
+
size="sm"
|
|
36
|
+
onClick={() => setPreviewing((p) => !p)}
|
|
37
|
+
>
|
|
38
|
+
{previewing ? t("kumiko.contentEditor.editMode") : t("kumiko.contentEditor.preview")}
|
|
39
|
+
</Button>
|
|
40
|
+
}
|
|
41
|
+
>
|
|
42
|
+
{previewing ? (
|
|
43
|
+
<ContentPreview
|
|
44
|
+
content={value}
|
|
45
|
+
variables={variableExamples}
|
|
46
|
+
contentFormat={contentFormat}
|
|
47
|
+
/>
|
|
48
|
+
) : (
|
|
49
|
+
<DocumentBlocksContext.Provider value={documentBlocks}>
|
|
50
|
+
<ContentEditor
|
|
51
|
+
id={contentEditorId}
|
|
52
|
+
value={value}
|
|
53
|
+
onChange={onChange}
|
|
54
|
+
variables={variables}
|
|
55
|
+
readOnly={disabled}
|
|
56
|
+
/>
|
|
57
|
+
</DocumentBlocksContext.Provider>
|
|
58
|
+
)}
|
|
59
|
+
</Field>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// Content tree behind the text-block and collection navs: loads the blocks from the
|
|
2
|
+
// by-tenant (or a collection's list) query, groups them by their `folder` field and
|
|
3
|
+
// emits them as TreeNode[].
|
|
4
|
+
//
|
|
5
|
+
// **Folder grouping**: block.folder !== null puts the node under a container
|
|
6
|
+
// node labelled with the folder; folder === null keeps it at the root.
|
|
7
|
+
//
|
|
8
|
+
// **State**: TreeNode.state = "filled" when content is set, otherwise "stub"
|
|
9
|
+
// (a hint that the slug exists but is empty).
|
|
10
|
+
|
|
11
|
+
import type { TreeChildrenSubscribe, TreeNode } from "@cosmicdrift/kumiko-framework/engine";
|
|
12
|
+
import { CSRF_HEADER_NAME, readCsrfToken } from "@cosmicdrift/kumiko-renderer/live";
|
|
13
|
+
import type { ContentCollectionSchema } from "./collection-lookup.ts";
|
|
14
|
+
import { collectionQueryName, TemplateResolverQueries } from "./qualified-names.ts";
|
|
15
|
+
import { TENANT_OVERRIDE_DENIED_REASON } from "./tenant-override.ts";
|
|
16
|
+
|
|
17
|
+
export type BlockSummary = {
|
|
18
|
+
readonly slug: string;
|
|
19
|
+
readonly locale: string;
|
|
20
|
+
readonly title: string | null;
|
|
21
|
+
readonly content: string | null;
|
|
22
|
+
readonly folder: string | null;
|
|
23
|
+
readonly modifiedAt: string | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type ByTenantResponse = {
|
|
27
|
+
readonly data: { readonly blocks: readonly BlockSummary[] };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Multi-level folder splitting: `folder` is a `/`-separated path, the tree
|
|
31
|
+
// splits per segment and creates nested folder nodes:
|
|
32
|
+
// block.folder = null → root leaf
|
|
33
|
+
// block.folder = "page" → folder page > leaf
|
|
34
|
+
// block.folder = "page/marketing" → folder page > folder marketing > leaf
|
|
35
|
+
//
|
|
36
|
+
// **Folder/leaf collision**: a block with `folder=null, slug="page"` and
|
|
37
|
+
// another with `folder="page"` both live on the same level — the folder with
|
|
38
|
+
// children plus a clickable leaf. Visually distinct (chevron + folder icon),
|
|
39
|
+
// no crash, no surprise.
|
|
40
|
+
|
|
41
|
+
type FolderNode = {
|
|
42
|
+
readonly leaves: TreeNode[];
|
|
43
|
+
readonly subFolders: Map<string, FolderNode>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function newFolderNode(): FolderNode {
|
|
47
|
+
return { leaves: [], subFolders: new Map() };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function attachBlock(
|
|
51
|
+
root: FolderNode,
|
|
52
|
+
block: BlockSummary,
|
|
53
|
+
tenantIdOverride?: string,
|
|
54
|
+
collectionId?: string,
|
|
55
|
+
): void {
|
|
56
|
+
const leaf: TreeNode = {
|
|
57
|
+
label: block.title || block.slug,
|
|
58
|
+
icon: "file",
|
|
59
|
+
target: {
|
|
60
|
+
featureId: "template-resolver",
|
|
61
|
+
action: "edit",
|
|
62
|
+
// tenantIdOverride and collectionId travel with the node → the editor
|
|
63
|
+
// reads and writes through the same tenant and the same collection the
|
|
64
|
+
// tree was loaded from. Omitted → session tenant, public text-blocks.
|
|
65
|
+
args: {
|
|
66
|
+
slug: block.slug,
|
|
67
|
+
locale: block.locale,
|
|
68
|
+
...(tenantIdOverride !== undefined && { tenantIdOverride }),
|
|
69
|
+
...(collectionId !== undefined && { collectionId }),
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
state: block.content ? "filled" : "stub",
|
|
73
|
+
};
|
|
74
|
+
let cur = root;
|
|
75
|
+
for (const segment of block.folder === null ? [] : block.folder.split("/")) {
|
|
76
|
+
let next = cur.subFolders.get(segment);
|
|
77
|
+
if (next === undefined) {
|
|
78
|
+
next = newFolderNode();
|
|
79
|
+
cur.subFolders.set(segment, next);
|
|
80
|
+
}
|
|
81
|
+
cur = next;
|
|
82
|
+
}
|
|
83
|
+
cur.leaves.push(leaf);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function renderFolderNode(node: FolderNode): TreeNode[] {
|
|
87
|
+
// Leaves first (root items), then folders alphabetically.
|
|
88
|
+
const result: TreeNode[] = [...node.leaves];
|
|
89
|
+
for (const folderName of [...node.subFolders.keys()].sort()) {
|
|
90
|
+
const subNode = node.subFolders.get(folderName);
|
|
91
|
+
if (subNode === undefined) continue;
|
|
92
|
+
result.push({
|
|
93
|
+
label: folderName,
|
|
94
|
+
icon: "folder",
|
|
95
|
+
state: "filled",
|
|
96
|
+
children: renderFolderNode(subNode),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function groupBlocksByFolder(
|
|
103
|
+
blocks: readonly BlockSummary[],
|
|
104
|
+
tenantIdOverride?: string,
|
|
105
|
+
collectionId?: string,
|
|
106
|
+
): readonly TreeNode[] {
|
|
107
|
+
const root = newFolderNode();
|
|
108
|
+
for (const block of blocks) {
|
|
109
|
+
attachBlock(root, block, tenantIdOverride, collectionId);
|
|
110
|
+
}
|
|
111
|
+
const rendered = renderFolderNode(root);
|
|
112
|
+
if (rendered.length === 0) return [];
|
|
113
|
+
return [
|
|
114
|
+
{
|
|
115
|
+
label: "Content",
|
|
116
|
+
icon: "folder",
|
|
117
|
+
state: "filled",
|
|
118
|
+
children: rendered,
|
|
119
|
+
},
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// A save in the editor re-fires the tree of the collection it wrote to. The
|
|
124
|
+
// SSE refresh does not cover every collection: per-user entries carry no
|
|
125
|
+
// entity signal, because naming their ids to every collection reader would
|
|
126
|
+
// reveal when colleagues change their own entries.
|
|
127
|
+
export const TEXT_BLOCK_TREE_KEY = "";
|
|
128
|
+
const treeRefreshListeners = new Map<string, Set<() => void>>();
|
|
129
|
+
|
|
130
|
+
function onTreeRefresh(treeKey: string, listener: () => void): () => void {
|
|
131
|
+
const listeners = treeRefreshListeners.get(treeKey) ?? new Set();
|
|
132
|
+
listeners.add(listener);
|
|
133
|
+
treeRefreshListeners.set(treeKey, listeners);
|
|
134
|
+
return () => listeners.delete(listener);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function refreshTree(treeKey: string): void {
|
|
138
|
+
for (const listener of treeRefreshListeners.get(treeKey) ?? []) listener();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
type BlockListResult =
|
|
142
|
+
| { readonly ok: true; readonly blocks: readonly BlockSummary[] }
|
|
143
|
+
| { readonly ok: false; readonly status: number; readonly body: string };
|
|
144
|
+
|
|
145
|
+
async function postBlockList(
|
|
146
|
+
queryType: string,
|
|
147
|
+
tenantIdOverride: string | undefined,
|
|
148
|
+
): Promise<BlockListResult> {
|
|
149
|
+
// CSRF header is mandatory on authenticated requests (auth-middleware
|
|
150
|
+
// double-submit pattern). Anonymous/pre-login has no token → header
|
|
151
|
+
// omitted → the server takes the anonymous path.
|
|
152
|
+
const headers: Record<string, string> = { "content-type": "application/json" };
|
|
153
|
+
const csrf = readCsrfToken();
|
|
154
|
+
if (csrf !== undefined) headers[CSRF_HEADER_NAME] = csrf;
|
|
155
|
+
// kumiko-lint-ignore no-raw-hooks not yet converted to the framework hook set
|
|
156
|
+
const response = await fetch("/api/query", {
|
|
157
|
+
method: "POST",
|
|
158
|
+
headers,
|
|
159
|
+
body: JSON.stringify({
|
|
160
|
+
type: queryType,
|
|
161
|
+
payload: tenantIdOverride !== undefined ? { tenantIdOverride } : {},
|
|
162
|
+
}),
|
|
163
|
+
});
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
status: response.status,
|
|
168
|
+
body: await response.text().catch(() => response.statusText),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
const data: ByTenantResponse = await response.json();
|
|
172
|
+
return { ok: true, blocks: data.data.blocks };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// The dispatcher accepts tenantIdOverride from a SystemAdmin only, and the
|
|
176
|
+
// provider cannot see the session roles. Everyone else gets this exact denial
|
|
177
|
+
// and reads their session tenant instead, which is the only tenant they may
|
|
178
|
+
// edit anyway.
|
|
179
|
+
function isTenantOverrideDenied(result: BlockListResult): boolean {
|
|
180
|
+
return !result.ok && result.status === 403 && result.body.includes(TENANT_OVERRIDE_DENIED_REASON);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function loadBlocks(
|
|
184
|
+
queryType: string,
|
|
185
|
+
tenantIdOverride: string | undefined,
|
|
186
|
+
): Promise<{ readonly blocks: readonly BlockSummary[]; readonly tenantIdOverride?: string }> {
|
|
187
|
+
let result = await postBlockList(queryType, tenantIdOverride);
|
|
188
|
+
let effectiveOverride = tenantIdOverride;
|
|
189
|
+
if (tenantIdOverride !== undefined && isTenantOverrideDenied(result)) {
|
|
190
|
+
result = await postBlockList(queryType, undefined);
|
|
191
|
+
effectiveOverride = undefined;
|
|
192
|
+
}
|
|
193
|
+
if (!result.ok) {
|
|
194
|
+
// 403 (CSRF/auth) or 5xx — explicit error instead of silent empty.
|
|
195
|
+
throw new Error(`text-block load failed: ${result.status} ${result.body}`);
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
blocks: result.blocks,
|
|
199
|
+
...(effectiveOverride !== undefined && { tenantIdOverride: effectiveOverride }),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// tenantIdOverride (SystemAdmin-only) lets an app point the content tree at a
|
|
204
|
+
// tenant other than the session's — publicstatus seeds marketing/legal blocks
|
|
205
|
+
// on SYSTEM_TENANT_ID, so its SystemAdmin must read that tenant, not their own.
|
|
206
|
+
// Without a collection: the anonymous-capable by-tenant query, so a public
|
|
207
|
+
// page keeps its content sidebar without a session. With one: that
|
|
208
|
+
// collection's own list handler, which carries the collection's access rule.
|
|
209
|
+
export function makeTreeProvider(
|
|
210
|
+
tenantIdOverride?: string,
|
|
211
|
+
collection?: Pick<ContentCollectionSchema, "id" | "ownership">,
|
|
212
|
+
): TreeChildrenSubscribe {
|
|
213
|
+
const collectionId = collection?.id;
|
|
214
|
+
const queryType =
|
|
215
|
+
collectionId === undefined
|
|
216
|
+
? TemplateResolverQueries.byTenant
|
|
217
|
+
: collectionQueryName(collectionId, "list");
|
|
218
|
+
// A per-user entry belongs to its author in the session tenant; pointing the
|
|
219
|
+
// tree at another tenant would only hide the author's own entries.
|
|
220
|
+
const requestedOverride = collection?.ownership === "user" ? undefined : tenantIdOverride;
|
|
221
|
+
return () => (emit, emitError) => {
|
|
222
|
+
let active = true;
|
|
223
|
+
// Each denial logs a client-fault warning on the server, so a reload after a
|
|
224
|
+
// save does not ask again once this subscription learned the answer.
|
|
225
|
+
let override = requestedOverride;
|
|
226
|
+
const load = (): void => {
|
|
227
|
+
loadBlocks(queryType, override)
|
|
228
|
+
.then(({ blocks, tenantIdOverride: effectiveOverride }) => {
|
|
229
|
+
// skip: subscription was cancelled while the request was in flight
|
|
230
|
+
if (!active) return;
|
|
231
|
+
override = effectiveOverride;
|
|
232
|
+
// The app-side r.nav node IS the "Content" container → the provider's
|
|
233
|
+
// children are the folders/leaves below it, not the wrapper.
|
|
234
|
+
const content = groupBlocksByFolder(blocks, effectiveOverride, collectionId)[0];
|
|
235
|
+
emit(content !== undefined && Array.isArray(content.children) ? content.children : []);
|
|
236
|
+
})
|
|
237
|
+
.catch((e) => {
|
|
238
|
+
// skip: subscription was cancelled while the request was in flight
|
|
239
|
+
if (!active) return;
|
|
240
|
+
// Explicit error signal via emitError; ProviderBranch shows a banner
|
|
241
|
+
// plus retry. Falls back to emit([]) for consumers without emitError.
|
|
242
|
+
if (emitError) {
|
|
243
|
+
emitError(e instanceof Error ? e : new Error(String(e)));
|
|
244
|
+
} else {
|
|
245
|
+
emit([]);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
load();
|
|
250
|
+
const unsubscribe = onTreeRefresh(collectionId ?? TEXT_BLOCK_TREE_KEY, load);
|
|
251
|
+
return () => {
|
|
252
|
+
active = false;
|
|
253
|
+
unsubscribe();
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const REFERENCE_NAME = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/;
|
|
2
|
+
const FORBIDDEN_REFERENCE_SEGMENTS = new Set(["__proto__", "prototype", "constructor"]);
|
|
3
|
+
|
|
4
|
+
// Segments that hit the prototype chain are refused: the renderer later walks
|
|
5
|
+
// the dotted path into a data object. Kept apart from document-schema so client code
|
|
6
|
+
// can validate a name without loading tiptap.
|
|
7
|
+
export function isDocumentReferenceName(value: string): boolean {
|
|
8
|
+
return (
|
|
9
|
+
REFERENCE_NAME.test(value) &&
|
|
10
|
+
!value.split(".").some((segment) => FORBIDDEN_REFERENCE_SEGMENTS.has(segment))
|
|
11
|
+
);
|
|
12
|
+
}
|
|
@@ -8,20 +8,21 @@ import { Link } from "@tiptap/extension-link";
|
|
|
8
8
|
import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table";
|
|
9
9
|
import StarterKit from "@tiptap/starter-kit";
|
|
10
10
|
import { createDocumentImagesNode } from "./document-images.ts";
|
|
11
|
+
import { isDocumentReferenceName } from "./document-reference-name.ts";
|
|
11
12
|
import { createDocumentStepsNode } from "./document-steps.ts";
|
|
12
13
|
|
|
13
14
|
const PLACEHOLDER_HREF = /^\{\{(.+)\}\}$/;
|
|
14
|
-
const REFERENCE_NAME = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/;
|
|
15
15
|
const RESOURCE_KEY = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
|
|
16
16
|
const ALLOWED_LINK_SCHEMES = ["https://", "http://", "mailto:", "tel:"] as const;
|
|
17
17
|
|
|
18
|
+
export { isDocumentReferenceName };
|
|
19
|
+
|
|
18
20
|
export function isDocumentPlaceholderHref(href: string): boolean {
|
|
19
21
|
const name = PLACEHOLDER_HREF.exec(href)?.[1];
|
|
20
22
|
return name !== undefined && isDocumentReferenceName(name);
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
const UNSAFE_URL_CHARACTERS = new Set(["<", ">", '"']);
|
|
24
|
-
const FORBIDDEN_REFERENCE_SEGMENTS = new Set(["__proto__", "prototype", "constructor"]);
|
|
25
26
|
|
|
26
27
|
function hasUnsafeUrlCharacter(value: string): boolean {
|
|
27
28
|
return Array.from(value).some((character) => {
|
|
@@ -53,15 +54,6 @@ export function isAllowedDocumentLinkHref(href: string): boolean {
|
|
|
53
54
|
return hasAllowedLinkScheme(href) || isDocumentPlaceholderHref(href);
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
// Segments that hit the prototype chain are refused: the renderer later walks
|
|
57
|
-
// the dotted path into a data object.
|
|
58
|
-
export function isDocumentReferenceName(value: string): boolean {
|
|
59
|
-
return (
|
|
60
|
-
REFERENCE_NAME.test(value) &&
|
|
61
|
-
!value.split(".").some((segment) => FORBIDDEN_REFERENCE_SEGMENTS.has(segment))
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
57
|
export function isDocumentResourceKey(value: string): boolean {
|
|
66
58
|
return RESOURCE_KEY.test(value);
|
|
67
59
|
}
|
|
@@ -186,6 +186,14 @@ export const defaultTranslations: TranslationsByLocale = {
|
|
|
186
186
|
"template-resolver.editor.networkError": "Network error while saving.",
|
|
187
187
|
"template-resolver.editor.loading": "Loading current version…",
|
|
188
188
|
"template-resolver.editor.loadFailed": "Could not load block",
|
|
189
|
+
"template-resolver.editor.create": "Create",
|
|
190
|
+
"template-resolver.editor.newEntry": "New entry",
|
|
191
|
+
"template-resolver.editor.newEntryTitle": "New {label}",
|
|
192
|
+
"template-resolver.editor.slugLabel": "Slug",
|
|
193
|
+
"template-resolver.editor.slugInvalid":
|
|
194
|
+
"Use lowercase letters, digits and dashes only; start with a letter or digit.",
|
|
195
|
+
"templateResolver.errors.collection.slugExists":
|
|
196
|
+
"An entry with the slug “{detail}” already exists. Pick another one.",
|
|
189
197
|
"template-resolver.editor.readOnly": "Read-only. Your role does not allow changes here.",
|
|
190
198
|
},
|
|
191
199
|
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { escapeHtmlAttr } from "@cosmicdrift/kumiko-renderer/headless";
|
|
2
|
+
import { isDocumentReferenceName } from "./document-reference-name.ts";
|
|
3
|
+
|
|
4
|
+
// Matches tags with quoted attribute values, so a `>` inside an attribute does not end the tag.
|
|
5
|
+
// The unquoted branch excludes `<` so a scan stops at the next tag start (no quadratic backtracking).
|
|
6
|
+
const HTML_TAG = /(<(?:"[^"]*"|'[^']*'|[^"'<>])*>)/;
|
|
7
|
+
// Triple braces are raw output and `{{else}}`/`{{this}}` belong to blocks; neither is a variable.
|
|
8
|
+
const TEXT_VARIABLE = /(?<!\{)\{\{\s*([^{}\s]+)\s*\}\}(?!\})/g;
|
|
9
|
+
const BLOCK_KEYWORDS: ReadonlySet<string> = new Set(["else", "this"]);
|
|
10
|
+
|
|
11
|
+
function isLegacyVariableName(name: string): boolean {
|
|
12
|
+
return isDocumentReferenceName(name) && !BLOCK_KEYWORDS.has(name);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Content saved by the old editors holds `{{name}}` as plain text; the document editor
|
|
16
|
+
// keeps variables as data-variable nodes and would show it as literal text.
|
|
17
|
+
export function convertLegacyVariableText(html: string): string {
|
|
18
|
+
return html
|
|
19
|
+
.split(HTML_TAG)
|
|
20
|
+
.map((part, index) =>
|
|
21
|
+
index % 2 === 1
|
|
22
|
+
? part
|
|
23
|
+
: part.replace(TEXT_VARIABLE, (match, name: string) =>
|
|
24
|
+
isLegacyVariableName(name)
|
|
25
|
+
? `<span data-variable="${escapeHtmlAttr(name)}"></span>`
|
|
26
|
+
: match,
|
|
27
|
+
),
|
|
28
|
+
)
|
|
29
|
+
.join("");
|
|
30
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DispatcherError } from "@cosmicdrift/kumiko-renderer/headless";
|
|
2
|
+
|
|
3
|
+
export const TENANT_OVERRIDE_DENIED_REASON = "tenant_override_requires_system_admin";
|
|
4
|
+
|
|
5
|
+
export function isTenantOverrideDeniedError(error: DispatcherError): boolean {
|
|
6
|
+
return error.details?.["reason"] === TENANT_OVERRIDE_DENIED_REASON;
|
|
7
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { TargetRef } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { FormScreenLayout, usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { ContentField } from "./content-field.tsx";
|
|
5
|
+
import { useTextBlockEditor } from "./use-text-block-editor.ts";
|
|
6
|
+
|
|
7
|
+
type EditorState = ReturnType<typeof useTextBlockEditor>;
|
|
8
|
+
|
|
9
|
+
function LoadBanners({ state }: { readonly state: EditorState }): ReactNode {
|
|
10
|
+
const { Banner } = usePrimitives();
|
|
11
|
+
const t = useTranslation();
|
|
12
|
+
return (
|
|
13
|
+
<>
|
|
14
|
+
{state.loading && <Banner variant="loading">{t("template-resolver.editor.loading")}</Banner>}
|
|
15
|
+
{state.loadError !== null && (
|
|
16
|
+
<Banner variant="error">
|
|
17
|
+
{t("template-resolver.editor.loadFailed")}: {state.loadError.code}
|
|
18
|
+
</Banner>
|
|
19
|
+
)}
|
|
20
|
+
{!state.canWrite && !state.loading && (
|
|
21
|
+
<Banner variant="info">{t("template-resolver.editor.readOnly")}</Banner>
|
|
22
|
+
)}
|
|
23
|
+
</>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function SaveBanners({ state }: { readonly state: EditorState }): ReactNode {
|
|
28
|
+
const { Banner } = usePrimitives();
|
|
29
|
+
return (
|
|
30
|
+
<>
|
|
31
|
+
{state.saveError !== null && <Banner variant="error">{state.saveError}</Banner>}
|
|
32
|
+
{state.savedMsg !== null && <Banner variant="info">{state.savedMsg}</Banner>}
|
|
33
|
+
</>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function TextBlockEditor({
|
|
38
|
+
target,
|
|
39
|
+
}: {
|
|
40
|
+
readonly target: TargetRef;
|
|
41
|
+
// onClose stays part of the resolver contract (the visual panel passes it)
|
|
42
|
+
// but is unused: tree selection navigates, there is no close button.
|
|
43
|
+
readonly onClose: () => void;
|
|
44
|
+
}): ReactNode {
|
|
45
|
+
const { Button, Field, Input, PageHeader, Section } = usePrimitives();
|
|
46
|
+
const t = useTranslation();
|
|
47
|
+
const state = useTextBlockEditor(target);
|
|
48
|
+
const headerTitle = state.title || state.slug || undefined;
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<>
|
|
52
|
+
{/* The editor opens beside the routed screen, so without this the shell header keeps that screen's title. */}
|
|
53
|
+
{PageHeader !== undefined && headerTitle !== undefined && <PageHeader title={headerTitle} />}
|
|
54
|
+
<FormScreenLayout
|
|
55
|
+
onSubmit={() => {
|
|
56
|
+
void state.save();
|
|
57
|
+
}}
|
|
58
|
+
testId="text-block-editor"
|
|
59
|
+
description={state.locale !== "" ? `${state.slug} · (${state.locale})` : state.slug}
|
|
60
|
+
actions={
|
|
61
|
+
state.canWrite ? (
|
|
62
|
+
<Button type="submit" loading={state.submitting} disabled={state.disabled}>
|
|
63
|
+
{t(state.saveLabelKey)}
|
|
64
|
+
</Button>
|
|
65
|
+
) : undefined
|
|
66
|
+
}
|
|
67
|
+
>
|
|
68
|
+
<Section>
|
|
69
|
+
<LoadBanners state={state} />
|
|
70
|
+
<Field id="text-block-title" label={t("template-resolver.editor.titleLabel")} required>
|
|
71
|
+
<Input
|
|
72
|
+
kind="text"
|
|
73
|
+
id="text-block-title"
|
|
74
|
+
name="text-block-title"
|
|
75
|
+
value={state.title}
|
|
76
|
+
onChange={state.setTitle}
|
|
77
|
+
disabled={state.disabled}
|
|
78
|
+
required
|
|
79
|
+
/>
|
|
80
|
+
</Field>
|
|
81
|
+
<ContentField
|
|
82
|
+
editorContext={state.editorContext}
|
|
83
|
+
value={state.content}
|
|
84
|
+
onChange={state.setContent}
|
|
85
|
+
disabled={state.disabled}
|
|
86
|
+
/>
|
|
87
|
+
<SaveBanners state={state} />
|
|
88
|
+
</Section>
|
|
89
|
+
</FormScreenLayout>
|
|
90
|
+
</>
|
|
91
|
+
);
|
|
92
|
+
}
|