@matterandmotion/emdash-comics 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +114 -2
- package/dist/batches.d.ts +84 -0
- package/dist/batches.d.ts.map +1 -0
- package/dist/batches.js +101 -0
- package/dist/batches.js.map +1 -0
- package/dist/blocks.d.ts +26 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +45 -0
- package/dist/blocks.js.map +1 -0
- package/dist/id.d.ts +3 -0
- package/dist/id.d.ts.map +1 -0
- package/dist/id.js +3 -0
- package/dist/id.js.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +122 -0
- package/dist/index.js.map +1 -0
- package/dist/legacy-redirects.d.ts +15 -0
- package/dist/legacy-redirects.d.ts.map +1 -0
- package/dist/legacy-redirects.js +58 -0
- package/dist/legacy-redirects.js.map +1 -0
- package/dist/media.d.ts +26 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +48 -0
- package/dist/media.js.map +1 -0
- package/dist/transcripts.d.ts +10 -0
- package/dist/transcripts.d.ts.map +1 -0
- package/dist/transcripts.js +22 -0
- package/dist/transcripts.js.map +1 -0
- package/package.json +67 -3
- package/profile/index.d.mts +46 -0
- package/profile/index.mjs +197 -0
- package/src/admin/BulkUploadPage.tsx +1004 -0
- package/src/admin/ComicPagesField.tsx +202 -0
- package/src/admin/bulk/api.ts +215 -0
- package/src/admin/bulk/config.ts +87 -0
- package/src/admin/bulk/files.ts +76 -0
- package/src/admin/bulk/history.ts +45 -0
- package/src/admin/bulk/plan.ts +446 -0
- package/src/admin/fingerprint.ts +26 -0
- package/src/admin/index.tsx +19 -0
- package/src/admin/media.ts +75 -0
- package/src/admin/pickers.tsx +132 -0
- package/src/admin/segments.ts +36 -0
- package/src/admin/slice.ts +46 -0
- package/src/admin/types.ts +47 -0
- package/src/astro/AgeGate.astro +85 -0
- package/src/astro/ArchiveGrid.astro +79 -0
- package/src/astro/CastGrid.astro +63 -0
- package/src/astro/ChapterList.astro +57 -0
- package/src/astro/CharacterCard.astro +54 -0
- package/src/astro/ComicEmbed.astro +55 -0
- package/src/astro/ComicNav.astro +131 -0
- package/src/astro/ComicReader.astro +193 -0
- package/src/astro/ContentWarnings.astro +61 -0
- package/src/astro/Transcript.astro +43 -0
- package/src/astro/astro-modules.d.ts +6 -0
- package/src/astro/blocks.ts +11 -0
- package/src/astro/index.ts +20 -0
- package/src/batches.ts +154 -0
- package/src/blocks.ts +46 -0
- package/src/id.ts +2 -0
- package/src/index.ts +152 -0
- package/src/legacy-redirects.ts +59 -0
- package/src/media.ts +60 -0
- package/src/transcripts.ts +20 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A comic's pages.
|
|
4
|
+
*
|
|
5
|
+
* - `layout="vertical"`: pages (or strip segments) stacked edge to edge, for
|
|
6
|
+
* webtoon-style strips. The default when `direction` is "vertical".
|
|
7
|
+
* - `layout="stack"`: pages stacked with a small gap. The default otherwise.
|
|
8
|
+
* - `layout="paged"`: one page at a time, with page buttons; clicking a page
|
|
9
|
+
* turns it. Without JavaScript every page shows, stacked.
|
|
10
|
+
* - `direction="rtl"`: right-to-left reading (manga): page order and the page
|
|
11
|
+
* buttons flip.
|
|
12
|
+
*
|
|
13
|
+
* Clicking the last page goes to `nextHref`, which is also prefetched. GIF pages
|
|
14
|
+
* are served untransformed so they keep animating.
|
|
15
|
+
*/
|
|
16
|
+
import { Image } from "emdash/ui";
|
|
17
|
+
import { isAnimatedImage, originalMediaUrl } from "../media";
|
|
18
|
+
|
|
19
|
+
interface ComicPage {
|
|
20
|
+
image?: unknown;
|
|
21
|
+
alt?: string;
|
|
22
|
+
hovertext?: string;
|
|
23
|
+
transcript?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Props {
|
|
27
|
+
title: string;
|
|
28
|
+
pages: ComicPage[];
|
|
29
|
+
layout?: "vertical" | "stack" | "paged";
|
|
30
|
+
direction?: "ltr" | "rtl" | "vertical";
|
|
31
|
+
/** The next comic, linked from the last page. */
|
|
32
|
+
nextHref?: string | undefined;
|
|
33
|
+
/** Responsive image sizes. */
|
|
34
|
+
sizes?: string;
|
|
35
|
+
/** CSS max-width of the reader. */
|
|
36
|
+
maxWidth?: string;
|
|
37
|
+
class?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const {
|
|
41
|
+
title,
|
|
42
|
+
pages,
|
|
43
|
+
direction = "ltr",
|
|
44
|
+
layout = direction === "vertical" ? "vertical" : "stack",
|
|
45
|
+
nextHref,
|
|
46
|
+
sizes = "(min-width: 900px) 900px, 100vw",
|
|
47
|
+
maxWidth = "900px",
|
|
48
|
+
class: className,
|
|
49
|
+
} = Astro.props;
|
|
50
|
+
|
|
51
|
+
const resolveMediaUrl = (Astro.locals as { emdash?: { getPublicMediaUrl?: (key: string) => string } })
|
|
52
|
+
.emdash?.getPublicMediaUrl;
|
|
53
|
+
const total = pages.length;
|
|
54
|
+
const rtl = direction === "rtl";
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
<div
|
|
58
|
+
class:list={["mm-comic", className]}
|
|
59
|
+
data-mm-comic
|
|
60
|
+
data-layout={layout}
|
|
61
|
+
data-direction={direction}
|
|
62
|
+
style={`--mm-comic-max-width: ${maxWidth}`}
|
|
63
|
+
>
|
|
64
|
+
<div class="mm-comic__pages" dir={rtl ? "rtl" : undefined}>
|
|
65
|
+
{
|
|
66
|
+
pages.map((page, index) => {
|
|
67
|
+
const alt = page.alt?.trim() || (total > 1 ? `${title}, page ${index + 1} of ${total}` : title);
|
|
68
|
+
const last = index === total - 1;
|
|
69
|
+
const media = page.image as { width?: number; height?: number } | undefined;
|
|
70
|
+
const picture = isAnimatedImage(page.image) ? (
|
|
71
|
+
<img
|
|
72
|
+
src={originalMediaUrl(page.image, resolveMediaUrl) ?? ""}
|
|
73
|
+
alt={alt}
|
|
74
|
+
title={page.hovertext || undefined}
|
|
75
|
+
width={media?.width}
|
|
76
|
+
height={media?.height}
|
|
77
|
+
loading={index === 0 ? "eager" : "lazy"}
|
|
78
|
+
decoding="async"
|
|
79
|
+
/>
|
|
80
|
+
) : (
|
|
81
|
+
<Image
|
|
82
|
+
image={page.image as never}
|
|
83
|
+
alt={alt}
|
|
84
|
+
title={page.hovertext || undefined}
|
|
85
|
+
sizes={sizes}
|
|
86
|
+
priority={index === 0}
|
|
87
|
+
/>
|
|
88
|
+
);
|
|
89
|
+
return (
|
|
90
|
+
<figure class="mm-comic__page" data-page={index}>
|
|
91
|
+
{last && nextHref ? (
|
|
92
|
+
<a href={nextHref} class="mm-comic__next-link" rel="next">
|
|
93
|
+
{picture}
|
|
94
|
+
</a>
|
|
95
|
+
) : (
|
|
96
|
+
picture
|
|
97
|
+
)}
|
|
98
|
+
</figure>
|
|
99
|
+
);
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
</div>
|
|
103
|
+
{
|
|
104
|
+
layout === "paged" && total > 1 && (
|
|
105
|
+
<div class="mm-comic__pager" data-pager hidden>
|
|
106
|
+
<button type="button" data-step={rtl ? "1" : "-1"}>
|
|
107
|
+
{rtl ? "Next page" : "Previous page"}
|
|
108
|
+
</button>
|
|
109
|
+
<span data-status aria-live="polite" />
|
|
110
|
+
<button type="button" data-step={rtl ? "-1" : "1"}>
|
|
111
|
+
{rtl ? "Previous page" : "Next page"}
|
|
112
|
+
</button>
|
|
113
|
+
</div>
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
</div>
|
|
117
|
+
{nextHref && <link rel="prefetch" href={nextHref} />}
|
|
118
|
+
|
|
119
|
+
<style>
|
|
120
|
+
.mm-comic {
|
|
121
|
+
max-width: var(--mm-comic-max-width, 900px);
|
|
122
|
+
margin-inline: auto;
|
|
123
|
+
}
|
|
124
|
+
.mm-comic__pages {
|
|
125
|
+
display: grid;
|
|
126
|
+
gap: var(--mm-comic-page-gap, 1rem);
|
|
127
|
+
}
|
|
128
|
+
.mm-comic[data-layout="vertical"] .mm-comic__pages {
|
|
129
|
+
gap: 0;
|
|
130
|
+
}
|
|
131
|
+
.mm-comic__page {
|
|
132
|
+
margin: 0;
|
|
133
|
+
}
|
|
134
|
+
.mm-comic__page :global(img) {
|
|
135
|
+
display: block;
|
|
136
|
+
width: 100%;
|
|
137
|
+
height: auto;
|
|
138
|
+
}
|
|
139
|
+
.mm-comic__next-link {
|
|
140
|
+
display: block;
|
|
141
|
+
}
|
|
142
|
+
.mm-comic.is-paged .mm-comic__page:not(.is-current) {
|
|
143
|
+
display: none;
|
|
144
|
+
}
|
|
145
|
+
.mm-comic.is-paged .mm-comic__page {
|
|
146
|
+
cursor: pointer;
|
|
147
|
+
}
|
|
148
|
+
.mm-comic__pager {
|
|
149
|
+
display: flex;
|
|
150
|
+
align-items: center;
|
|
151
|
+
justify-content: center;
|
|
152
|
+
gap: 1rem;
|
|
153
|
+
padding-block: 0.75rem;
|
|
154
|
+
}
|
|
155
|
+
.mm-comic__pager[hidden] {
|
|
156
|
+
display: none;
|
|
157
|
+
}
|
|
158
|
+
</style>
|
|
159
|
+
|
|
160
|
+
<script>
|
|
161
|
+
// Paged layout: show one page at a time. Clicking a page turns it; on the
|
|
162
|
+
// last page a click follows the link to the next comic.
|
|
163
|
+
for (const root of document.querySelectorAll<HTMLElement>("[data-mm-comic][data-layout='paged']")) {
|
|
164
|
+
const pages = [...root.querySelectorAll<HTMLElement>(".mm-comic__page")];
|
|
165
|
+
const pager = root.querySelector<HTMLElement>("[data-pager]");
|
|
166
|
+
const status = root.querySelector<HTMLElement>("[data-status]");
|
|
167
|
+
if (pages.length < 2 || !pager) continue;
|
|
168
|
+
let current = 0;
|
|
169
|
+
const show = (index: number) => {
|
|
170
|
+
current = Math.max(0, Math.min(pages.length - 1, index));
|
|
171
|
+
pages.forEach((page, i) => page.classList.toggle("is-current", i === current));
|
|
172
|
+
if (status) status.textContent = `${current + 1} / ${pages.length}`;
|
|
173
|
+
};
|
|
174
|
+
root.classList.add("is-paged");
|
|
175
|
+
pager.hidden = false;
|
|
176
|
+
show(0);
|
|
177
|
+
for (const button of pager.querySelectorAll<HTMLButtonElement>("[data-step]")) {
|
|
178
|
+
button.addEventListener("click", () => show(current + Number(button.dataset.step)));
|
|
179
|
+
}
|
|
180
|
+
pages.forEach((page, i) => {
|
|
181
|
+
if (i === pages.length - 1) return;
|
|
182
|
+
page.addEventListener("click", () => show(current + 1));
|
|
183
|
+
});
|
|
184
|
+
root.addEventListener("mm-comic:page", (event) => {
|
|
185
|
+
const step = (event as CustomEvent<number>).detail;
|
|
186
|
+
const next = current + step;
|
|
187
|
+
if (next >= 0 && next < pages.length) {
|
|
188
|
+
event.preventDefault();
|
|
189
|
+
show(next);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
</script>
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Content warnings for a comic. With `gate`, the slot (the comic) stays
|
|
4
|
+
* collapsed until the reader chooses to continue; without JavaScript the
|
|
5
|
+
* native <details> element does the same.
|
|
6
|
+
*/
|
|
7
|
+
interface Props {
|
|
8
|
+
warnings: string[];
|
|
9
|
+
/** Hide the slotted content behind the warning. */
|
|
10
|
+
gate?: boolean;
|
|
11
|
+
label?: string;
|
|
12
|
+
continueLabel?: string;
|
|
13
|
+
class?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
warnings,
|
|
18
|
+
gate = false,
|
|
19
|
+
label = "Content warnings",
|
|
20
|
+
continueLabel = "Show the comic",
|
|
21
|
+
class: className,
|
|
22
|
+
} = Astro.props;
|
|
23
|
+
const list = warnings.map((warning) => warning.trim()).filter(Boolean);
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
{
|
|
27
|
+
list.length === 0 ? (
|
|
28
|
+
<slot />
|
|
29
|
+
) : gate ? (
|
|
30
|
+
<details class:list={["mm-warnings", "mm-warnings--gate", className]}>
|
|
31
|
+
<summary>
|
|
32
|
+
<strong>{label}:</strong> {list.join(", ")}. <span class="mm-warnings__action">{continueLabel}</span>
|
|
33
|
+
</summary>
|
|
34
|
+
<slot />
|
|
35
|
+
</details>
|
|
36
|
+
) : (
|
|
37
|
+
<>
|
|
38
|
+
<p class:list={["mm-warnings", className]} role="note">
|
|
39
|
+
<strong>{label}:</strong> {list.join(", ")}.
|
|
40
|
+
</p>
|
|
41
|
+
<slot />
|
|
42
|
+
</>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
<style>
|
|
47
|
+
.mm-warnings {
|
|
48
|
+
padding: 0.75rem 1rem;
|
|
49
|
+
border: 1px solid currentColor;
|
|
50
|
+
border-radius: 0.5rem;
|
|
51
|
+
}
|
|
52
|
+
.mm-warnings--gate > summary {
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
}
|
|
55
|
+
.mm-warnings__action {
|
|
56
|
+
text-decoration: underline;
|
|
57
|
+
}
|
|
58
|
+
.mm-warnings--gate[open] > summary .mm-warnings__action {
|
|
59
|
+
display: none;
|
|
60
|
+
}
|
|
61
|
+
</style>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* The comic's transcript, page by page, in a collapsible section. Renders
|
|
4
|
+
* nothing when no page has a transcript.
|
|
5
|
+
*/
|
|
6
|
+
interface Props {
|
|
7
|
+
pages: Array<{ transcript?: string }>;
|
|
8
|
+
label?: string;
|
|
9
|
+
/** Start expanded. */
|
|
10
|
+
open?: boolean;
|
|
11
|
+
class?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { pages, label = "Transcript", open = false, class: className } = Astro.props;
|
|
15
|
+
const transcripts = pages
|
|
16
|
+
.map((page, index) => ({ index, text: page.transcript?.trim() ?? "" }))
|
|
17
|
+
.filter((page) => page.text);
|
|
18
|
+
const numbered = pages.length > 1;
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
{
|
|
22
|
+
transcripts.length > 0 && (
|
|
23
|
+
<details class:list={["mm-transcript", className]} open={open}>
|
|
24
|
+
<summary>{label}</summary>
|
|
25
|
+
{transcripts.map((page) => (
|
|
26
|
+
<section class="mm-transcript__page">
|
|
27
|
+
{numbered && <h3 class="mm-transcript__heading">Page {page.index + 1}</h3>}
|
|
28
|
+
<p>{page.text}</p>
|
|
29
|
+
</section>
|
|
30
|
+
))}
|
|
31
|
+
</details>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
<style>
|
|
36
|
+
.mm-transcript p {
|
|
37
|
+
white-space: pre-line;
|
|
38
|
+
}
|
|
39
|
+
.mm-transcript__heading {
|
|
40
|
+
font-size: 0.9em;
|
|
41
|
+
margin-block: 1em 0.25em;
|
|
42
|
+
}
|
|
43
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `componentsEntry` module: Astro renderers for this plugin's Portable
|
|
3
|
+
* Text blocks, keyed by block type. EmDash's `<PortableText>` picks them up.
|
|
4
|
+
*/
|
|
5
|
+
import CharacterCard from "./CharacterCard.astro";
|
|
6
|
+
import ComicEmbed from "./ComicEmbed.astro";
|
|
7
|
+
|
|
8
|
+
export const blockComponents = {
|
|
9
|
+
"comic-embed": ComicEmbed,
|
|
10
|
+
"character-card": CharacterCard,
|
|
11
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Site-side helpers. Import components by path, for example
|
|
3
|
+
* `@matterandmotion/emdash-comics/astro/ComicReader.astro`.
|
|
4
|
+
*/
|
|
5
|
+
export { legacyRedirects } from "../legacy-redirects.js";
|
|
6
|
+
export { asMedia, isAnimatedImage, type MediaLike, originalMediaUrl } from "../media.js";
|
|
7
|
+
export { transcriptText } from "../transcripts.js";
|
|
8
|
+
|
|
9
|
+
/** A comic as the template reads it from the `comics` collection. */
|
|
10
|
+
export interface ComicPage {
|
|
11
|
+
image?: unknown;
|
|
12
|
+
alt?: string;
|
|
13
|
+
transcript?: string;
|
|
14
|
+
hovertext?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The canonical comic URL in the webcomic kit: /<series>/<slug>. */
|
|
18
|
+
export function comicHref(comic: { series: string; slug: string }): string {
|
|
19
|
+
return `/${encodeURIComponent(comic.series)}/${encodeURIComponent(comic.slug)}`;
|
|
20
|
+
}
|
package/src/batches.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bulk upload history, kept in the plugin's storage so a recent batch can be
|
|
3
|
+
* undone from any browser, by anyone who can create comics. A batch records
|
|
4
|
+
* which comics an upload created and who ran it. Whether those comics are
|
|
5
|
+
* still there or in the trash is read from the content itself, so the history
|
|
6
|
+
* can't go out of date when someone restores or deletes one by hand.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** How long a batch can be undone from the bulk upload page. */
|
|
10
|
+
export const BATCH_RETENTION_DAYS = 30;
|
|
11
|
+
/** How many recent batches the page lists. */
|
|
12
|
+
export const BATCH_LIST_LIMIT = 20;
|
|
13
|
+
/** More comics than any one upload should create. */
|
|
14
|
+
export const MAX_BATCH_ENTRIES = 2000;
|
|
15
|
+
|
|
16
|
+
export interface BatchEntry {
|
|
17
|
+
id: string;
|
|
18
|
+
title: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A stored batch. The storage ID is the batch ID, which the browser picks. */
|
|
22
|
+
export interface BatchRecord {
|
|
23
|
+
collection: string;
|
|
24
|
+
/** Series slug, when there is one. */
|
|
25
|
+
series?: string;
|
|
26
|
+
createdAt: string;
|
|
27
|
+
/** Who ran the upload: their user ID, and their name when they have one. */
|
|
28
|
+
by: { id: string; name?: string };
|
|
29
|
+
entries: BatchEntry[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Batch extends BatchRecord {
|
|
33
|
+
id: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface BatchInput {
|
|
37
|
+
id: string;
|
|
38
|
+
collection: string;
|
|
39
|
+
series?: string;
|
|
40
|
+
entries: BatchEntry[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The storage calls this module makes (EmDash's `StorageCollection`). */
|
|
44
|
+
export interface BatchStore {
|
|
45
|
+
get(id: string): Promise<unknown>;
|
|
46
|
+
put(id: string, data: BatchRecord): Promise<void>;
|
|
47
|
+
query(options: {
|
|
48
|
+
where?: Record<string, string | { gte?: string; lt?: string }>;
|
|
49
|
+
orderBy?: Record<string, "asc" | "desc">;
|
|
50
|
+
limit?: number;
|
|
51
|
+
}): Promise<{ items: Array<{ id: string; data: unknown }> }>;
|
|
52
|
+
deleteMany(ids: string[]): Promise<number>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Storage indexes the queries below need. */
|
|
56
|
+
export const BATCH_INDEXES = ["createdAt", ["collection", "createdAt"]];
|
|
57
|
+
|
|
58
|
+
const BATCH_ID = /^[A-Za-z0-9-]{8,64}$/;
|
|
59
|
+
const SLUG = /^[A-Za-z0-9_-]{1,100}$/;
|
|
60
|
+
|
|
61
|
+
function text(value: unknown, max: number): string | undefined {
|
|
62
|
+
return typeof value === "string" ? value.slice(0, max) : undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Checks a save request's body. Returns the batch, or why it's not acceptable. */
|
|
66
|
+
export function parseBatchInput(input: unknown): BatchInput | { error: string } {
|
|
67
|
+
if (!input || typeof input !== "object") return { error: "Expected a JSON object" };
|
|
68
|
+
const body = input as Record<string, unknown>;
|
|
69
|
+
if (typeof body.id !== "string" || !BATCH_ID.test(body.id)) return { error: "Invalid batch id" };
|
|
70
|
+
if (typeof body.collection !== "string" || !SLUG.test(body.collection)) {
|
|
71
|
+
return { error: "Invalid collection" };
|
|
72
|
+
}
|
|
73
|
+
if (body.series !== undefined && (typeof body.series !== "string" || !SLUG.test(body.series))) {
|
|
74
|
+
return { error: "Invalid series" };
|
|
75
|
+
}
|
|
76
|
+
if (!Array.isArray(body.entries) || body.entries.length > MAX_BATCH_ENTRIES) {
|
|
77
|
+
return { error: `Expected up to ${MAX_BATCH_ENTRIES} entries` };
|
|
78
|
+
}
|
|
79
|
+
const entries: BatchEntry[] = [];
|
|
80
|
+
for (const item of body.entries) {
|
|
81
|
+
const entry = item as Record<string, unknown> | null;
|
|
82
|
+
const id = text(entry?.id, 100);
|
|
83
|
+
if (!id) return { error: "Each entry needs an id" };
|
|
84
|
+
entries.push({ id, title: text(entry?.title, 300) ?? "" });
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
id: body.id,
|
|
88
|
+
collection: body.collection,
|
|
89
|
+
...(typeof body.series === "string" ? { series: body.series } : {}),
|
|
90
|
+
entries,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isRecord(value: unknown): value is BatchRecord {
|
|
95
|
+
if (!value || typeof value !== "object") return false;
|
|
96
|
+
const record = value as Partial<BatchRecord>;
|
|
97
|
+
return (
|
|
98
|
+
typeof record.collection === "string" &&
|
|
99
|
+
typeof record.createdAt === "string" &&
|
|
100
|
+
typeof record.by?.id === "string" &&
|
|
101
|
+
Array.isArray(record.entries)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function cutoff(now: Date): string {
|
|
106
|
+
return new Date(now.getTime() - BATCH_RETENTION_DAYS * 86_400_000).toISOString();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Records a batch, or updates it as more comics are created. Only the person
|
|
111
|
+
* who started a batch can change it. Batches past the retention period are
|
|
112
|
+
* removed on the way.
|
|
113
|
+
*/
|
|
114
|
+
export async function saveBatch(
|
|
115
|
+
store: BatchStore,
|
|
116
|
+
input: BatchInput,
|
|
117
|
+
user: { id: string; name?: string | null },
|
|
118
|
+
now = new Date(),
|
|
119
|
+
): Promise<{ ok: true; batch: Batch } | { ok: false; forbidden: true }> {
|
|
120
|
+
const stored = await store.get(input.id);
|
|
121
|
+
const previous = isRecord(stored) ? stored : null;
|
|
122
|
+
if (previous && previous.by.id !== user.id) return { ok: false, forbidden: true };
|
|
123
|
+
const record: BatchRecord = {
|
|
124
|
+
collection: input.collection,
|
|
125
|
+
...(input.series ? { series: input.series } : {}),
|
|
126
|
+
createdAt: previous?.createdAt ?? now.toISOString(),
|
|
127
|
+
by: previous?.by ?? { id: user.id, ...(user.name ? { name: user.name } : {}) },
|
|
128
|
+
entries: input.entries,
|
|
129
|
+
};
|
|
130
|
+
await store.put(input.id, record);
|
|
131
|
+
await pruneBatches(store, now);
|
|
132
|
+
return { ok: true, batch: { id: input.id, ...record } };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A collection's recent batches, newest first. */
|
|
136
|
+
export async function listBatches(
|
|
137
|
+
store: BatchStore,
|
|
138
|
+
collection: string,
|
|
139
|
+
now = new Date(),
|
|
140
|
+
): Promise<Batch[]> {
|
|
141
|
+
const result = await store.query({
|
|
142
|
+
where: { collection, createdAt: { gte: cutoff(now) } },
|
|
143
|
+
orderBy: { createdAt: "desc" },
|
|
144
|
+
limit: BATCH_LIST_LIMIT,
|
|
145
|
+
});
|
|
146
|
+
return result.items.flatMap(({ id, data }) => (isRecord(data) ? [{ id, ...data }] : []));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Deletes batches past the retention period, a hundred at a time. */
|
|
150
|
+
export async function pruneBatches(store: BatchStore, now = new Date()): Promise<number> {
|
|
151
|
+
const old = await store.query({ where: { createdAt: { lt: cutoff(now) } }, limit: 100 });
|
|
152
|
+
if (old.items.length === 0) return 0;
|
|
153
|
+
return store.deleteMany(old.items.map((item) => item.id));
|
|
154
|
+
}
|
package/src/blocks.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portable Text blocks for posts and pages: embed a comic, or show a cast
|
|
3
|
+
* member's card. Editors pick them from the slash menu and fill in a slug;
|
|
4
|
+
* the renderers (`./blocks`) look the entry up when the page renders.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const COMIC_EMBED_BLOCK = "comic-embed";
|
|
8
|
+
export const CHARACTER_CARD_BLOCK = "character-card";
|
|
9
|
+
|
|
10
|
+
export const portableTextBlocks = [
|
|
11
|
+
{
|
|
12
|
+
type: COMIC_EMBED_BLOCK,
|
|
13
|
+
label: "Comic",
|
|
14
|
+
icon: "image",
|
|
15
|
+
description: "Show a comic's first page, linked to the comic.",
|
|
16
|
+
category: "Comics",
|
|
17
|
+
fields: [
|
|
18
|
+
{
|
|
19
|
+
type: "text_input" as const,
|
|
20
|
+
action_id: "comic",
|
|
21
|
+
label: "Comic slug",
|
|
22
|
+
placeholder: "the-first-page",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
type: "text_input" as const,
|
|
26
|
+
action_id: "caption",
|
|
27
|
+
label: "Caption (optional)",
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: CHARACTER_CARD_BLOCK,
|
|
33
|
+
label: "Character card",
|
|
34
|
+
icon: "user",
|
|
35
|
+
description: "Show a cast member's portrait and introduction.",
|
|
36
|
+
category: "Comics",
|
|
37
|
+
fields: [
|
|
38
|
+
{
|
|
39
|
+
type: "text_input" as const,
|
|
40
|
+
action_id: "character",
|
|
41
|
+
label: "Cast member slug",
|
|
42
|
+
placeholder: "captain-ada",
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
];
|
package/src/id.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { definePlugin, type PluginDescriptor, PluginRouteError, type RouteContext } from "emdash";
|
|
2
|
+
import {
|
|
3
|
+
BATCH_INDEXES,
|
|
4
|
+
type BatchStore,
|
|
5
|
+
listBatches,
|
|
6
|
+
parseBatchInput,
|
|
7
|
+
saveBatch,
|
|
8
|
+
} from "./batches.js";
|
|
9
|
+
import { portableTextBlocks } from "./blocks.js";
|
|
10
|
+
import { PLUGIN_ID } from "./id.js";
|
|
11
|
+
import { transcriptText } from "./transcripts.js";
|
|
12
|
+
|
|
13
|
+
export { BATCH_RETENTION_DAYS, type Batch, type BatchEntry } from "./batches.js";
|
|
14
|
+
export { CHARACTER_CARD_BLOCK, COMIC_EMBED_BLOCK } from "./blocks.js";
|
|
15
|
+
export { PLUGIN_ID } from "./id.js";
|
|
16
|
+
export { isAnimatedImage, type MediaLike, originalMediaUrl } from "./media.js";
|
|
17
|
+
export { MAX_TRANSCRIPT_LENGTH, transcriptText } from "./transcripts.js";
|
|
18
|
+
/** Kept in step with package.json by scripts/sync-versions.mjs (a test checks it). */
|
|
19
|
+
export const VERSION = "0.1.1";
|
|
20
|
+
const PACKAGE = "@matterandmotion/emdash-comics";
|
|
21
|
+
|
|
22
|
+
export interface ComicsOptions {
|
|
23
|
+
/** The comics collection. Default "comics". */
|
|
24
|
+
collection?: string;
|
|
25
|
+
/** The repeater holding the comic's pages. Default "pages". */
|
|
26
|
+
pagesField?: string;
|
|
27
|
+
/** The searchable text field transcripts are copied into. Default "transcript_text". */
|
|
28
|
+
transcriptField?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The admin sidebar folder for the bulk upload page. It joins collections
|
|
31
|
+
* with the same `group`. Default "Comics".
|
|
32
|
+
*/
|
|
33
|
+
adminGroup?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ResolvedOptions {
|
|
37
|
+
collection: string;
|
|
38
|
+
pagesField: string;
|
|
39
|
+
transcriptField: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function resolve(options: ComicsOptions): ResolvedOptions {
|
|
43
|
+
return {
|
|
44
|
+
collection: options.collection ?? "comics",
|
|
45
|
+
pagesField: options.pagesField ?? "pages",
|
|
46
|
+
transcriptField: options.transcriptField ?? "transcript_text",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The plugin's admin pages; the admin entry exports a component for each path. */
|
|
51
|
+
export function adminPages(options: Pick<ComicsOptions, "adminGroup"> = {}) {
|
|
52
|
+
return [
|
|
53
|
+
{
|
|
54
|
+
path: "/bulk-upload",
|
|
55
|
+
label: "Bulk upload",
|
|
56
|
+
icon: "upload",
|
|
57
|
+
group: options.adminGroup ?? "Comics",
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Register in `astro.config.mjs`: `emdash({ plugins: [comics()] })`. It's a
|
|
64
|
+
* native plugin, so it runs in-process and can't be sandboxed.
|
|
65
|
+
*/
|
|
66
|
+
export function comics(options: ComicsOptions = {}): PluginDescriptor<ComicsOptions> {
|
|
67
|
+
return {
|
|
68
|
+
id: PLUGIN_ID,
|
|
69
|
+
version: VERSION,
|
|
70
|
+
displayName: "Comics",
|
|
71
|
+
description:
|
|
72
|
+
"Comic page editor, bulk upload, comic blocks, searchable transcripts and reader components.",
|
|
73
|
+
format: "native",
|
|
74
|
+
entrypoint: PACKAGE,
|
|
75
|
+
adminEntry: `${PACKAGE}/admin`,
|
|
76
|
+
componentsEntry: `${PACKAGE}/blocks`,
|
|
77
|
+
adminPages: adminPages(options),
|
|
78
|
+
options,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Copies page transcripts into the transcript field when a comic's pages are saved. */
|
|
83
|
+
export function withTranscript(
|
|
84
|
+
content: Record<string, unknown>,
|
|
85
|
+
options: Pick<ResolvedOptions, "pagesField" | "transcriptField">,
|
|
86
|
+
): Record<string, unknown> | undefined {
|
|
87
|
+
// Partial updates that don't touch the pages leave the transcript alone.
|
|
88
|
+
if (!(options.pagesField in content)) return undefined;
|
|
89
|
+
const text = transcriptText(content[options.pagesField]);
|
|
90
|
+
if (content[options.transcriptField] === text) return undefined;
|
|
91
|
+
return { ...content, [options.transcriptField]: text };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function batchStore(ctx: RouteContext): BatchStore {
|
|
95
|
+
const store = ctx.storage.batches;
|
|
96
|
+
if (!store) throw PluginRouteError.internal("The batches storage collection is missing");
|
|
97
|
+
return store as BatchStore;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Called by EmDash's native plugin loader. */
|
|
101
|
+
export function createPlugin(options: ComicsOptions = {}) {
|
|
102
|
+
const settings = resolve(options);
|
|
103
|
+
return definePlugin({
|
|
104
|
+
id: PLUGIN_ID,
|
|
105
|
+
version: VERSION,
|
|
106
|
+
capabilities: ["content:write"],
|
|
107
|
+
storage: {
|
|
108
|
+
// Bulk upload history (see batches.ts).
|
|
109
|
+
batches: { indexes: BATCH_INDEXES },
|
|
110
|
+
},
|
|
111
|
+
routes: {
|
|
112
|
+
/** Recent bulk uploads into a collection: GET ?collection=comics */
|
|
113
|
+
batches: {
|
|
114
|
+
permission: "content:create",
|
|
115
|
+
methods: ["GET"],
|
|
116
|
+
handler: async (ctx) => {
|
|
117
|
+
const input = (ctx.input ?? {}) as Record<string, unknown>;
|
|
118
|
+
const collection = typeof input.collection === "string" ? input.collection : "";
|
|
119
|
+
if (!collection) throw PluginRouteError.badRequest("collection is required");
|
|
120
|
+
return { batches: await listBatches(batchStore(ctx), collection) };
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
/** Records a bulk upload, or adds the comics it has created since. */
|
|
124
|
+
"save-batch": {
|
|
125
|
+
permission: "content:create",
|
|
126
|
+
methods: ["POST"],
|
|
127
|
+
handler: async (ctx) => {
|
|
128
|
+
if (!ctx.user) throw PluginRouteError.unauthorized();
|
|
129
|
+
const input = parseBatchInput(ctx.input);
|
|
130
|
+
if ("error" in input) throw PluginRouteError.badRequest(input.error);
|
|
131
|
+
const result = await saveBatch(batchStore(ctx), input, ctx.user);
|
|
132
|
+
if (!result.ok)
|
|
133
|
+
throw PluginRouteError.forbidden("This upload was started by someone else");
|
|
134
|
+
return { batch: result.batch };
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
hooks: {
|
|
139
|
+
"content:beforeSave": {
|
|
140
|
+
priority: 60,
|
|
141
|
+
timeout: 2000,
|
|
142
|
+
handler: async (event) => {
|
|
143
|
+
if (event.collection !== settings.collection) return;
|
|
144
|
+
return withTranscript(event.content, settings);
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
admin: { entry: `${PACKAGE}/admin`, portableTextBlocks, pages: adminPages(options) },
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export default comics;
|