Skip to content

Commit bf82997

Browse files
committed
core: support customizable monograph title
Signed-off-by: 01zulfi <[email protected]>
1 parent 33c3bf0 commit bf82997

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

apps/web/src/components/publish-view/index.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function PublishView(props: PublishViewProps) {
5656
current: number;
5757
}>();
5858
const passwordInput = useRef<HTMLInputElement>(null);
59+
const titleInput = useRef<HTMLInputElement>(null);
5960
const publishNote = useStore((store) => store.publish);
6061
const unpublishNote = useStore((store) => store.unpublish);
6162
const [monograph, setMonograph] = useState(props.monograph);
@@ -125,6 +126,26 @@ function PublishView(props: PublishViewProps) {
125126
borderRadius: "default"
126127
}}
127128
>
129+
<Flex
130+
sx={{
131+
alignItems: "center",
132+
justifyContent: "space-between",
133+
px: 1,
134+
height: 30,
135+
136+
"& label": { width: "auto", flexShrink: 0 }
137+
}}
138+
>
139+
<Text variant="body">{strings.title()} *</Text>
140+
<Input
141+
ref={titleInput}
142+
type="text"
143+
variant="clean"
144+
placeholder={strings.enterTitle()}
145+
defaultValue={monograph ? monograph.title : note.title}
146+
sx={{ textAlign: "right", p: 0 }}
147+
/>
148+
</Flex>
128149
{monograph?.publishedAt ? (
129150
<Flex
130151
sx={{
@@ -286,8 +307,14 @@ function PublishView(props: PublishViewProps) {
286307
try {
287308
setStatus({ action: "publish" });
288309
const password = passwordInput.current?.value;
310+
const title = titleInput.current?.value;
311+
312+
if (!title || title.trim().length === 0) {
313+
showToast("error", "Title cannot be empty.");
314+
return;
315+
}
289316

290-
await publishNote(note.id, {
317+
await publishNote(note.id, title, {
291318
selfDestruct,
292319
password
293320
});
@@ -425,6 +452,7 @@ type ResolvedMonograph = {
425452
selfDestruct: boolean;
426453
publishedAt?: number;
427454
password?: string;
455+
title: string;
428456
};
429457

430458
async function resolveMonograph(
@@ -436,6 +464,7 @@ async function resolveMonograph(
436464
id: monographId,
437465
selfDestruct: !!monograph.selfDestruct,
438466
publishedAt: monograph.datePublished,
467+
title: monograph.title,
439468
password: monograph.password
440469
? await db.monographs.decryptPassword(monograph.password)
441470
: undefined

apps/web/src/stores/monograph-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class MonographStore extends BaseStore<MonographStore> {
3333
this.set({ monographs: grouping });
3434
};
3535

36-
publish = async (noteId: string, opts: PublishOptions) => {
37-
const publishId = await db.monographs.publish(noteId, opts);
36+
publish = async (noteId: string, title: string, opts: PublishOptions) => {
37+
const publishId = await db.monographs.publish(noteId, title, opts);
3838
await this.get().refresh();
3939
await noteStore.refreshContext();
4040
return publishId;

packages/core/__e2e__/monographs.test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ test(
5151
await login(db);
5252
await db.monographs.refresh();
5353

54-
const monographId = await db.monographs.publish(id);
54+
const title = "mono";
55+
const monographId = await db.monographs.publish(id, title);
5556

5657
expect(await db.monographs.all.has(id)).toBeTruthy();
5758

5859
const monograph = await db.monographs.get(monographId);
59-
const note = await db.notes.note(id);
6060
expect(monograph.id).toBe(monographId);
61-
expect(monograph.title).toBe(note.title);
61+
expect(monograph.title).toBe(title);
6262

6363
await logout(db);
6464
}),
@@ -72,14 +72,13 @@ test(
7272
await login(db);
7373
await db.monographs.refresh();
7474

75-
const monographId = await db.monographs.publish(id);
75+
const title = "mono";
76+
const monographId = await db.monographs.publish(id, title);
7677
let monograph = await db.monographs.get(monographId);
77-
const note = await db.notes.note(id);
78-
expect(monograph.title).toBe(note.title);
78+
expect(monograph.title).toBe(title);
7979

80-
const editedTitle = "EDITED TITLE OF MY NOTE!";
81-
await db.notes.add({ id, title: editedTitle });
82-
await db.monographs.publish(id);
80+
const editedTitle = "monograph";
81+
await db.monographs.publish(id, editedTitle);
8382
monograph = await db.monographs.get(monographId);
8483
expect(monograph.title).toBe(editedTitle);
8584

packages/core/src/api/monographs.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export type MonographAnalytics = {
4141
totalViews: number;
4242
};
4343

44-
export type PublishOptions = { password?: string; selfDestruct?: boolean };
44+
export type PublishOptions = {
45+
password?: string;
46+
selfDestruct?: boolean;
47+
};
4548
export class Monographs {
4649
monographs: string[] = [];
4750
constructor(private readonly db: Database) {}
@@ -73,7 +76,9 @@ export class Monographs {
7376
/**
7477
* Publish a note as a monograph
7578
*/
76-
async publish(noteId: string, opts: PublishOptions = {}) {
79+
async publish(noteId: string, title: string, opts: PublishOptions = {}) {
80+
if (title === "") throw new Error("Title cannot be empty.");
81+
7782
if (!this.monographs.length) await this.refresh();
7883

7984
const update = !!this.isPublished(noteId);
@@ -102,7 +107,7 @@ export class Monographs {
102107
const monographPasswordsKey = await this.db.user.getMonographPasswordsKey();
103108
const monograph: MonographApiRequest = {
104109
id: noteId,
105-
title: note.title,
110+
title,
106111
userId: user.id,
107112
selfDestruct: opts.selfDestruct || false,
108113
...(opts.password

packages/intl/locale/en.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,10 @@ msgstr "Enter the gift code to redeem your subscription."
25762576
msgid "Enter the recovery code to continue logging in"
25772577
msgstr "Enter the recovery code to continue logging in"
25782578

2579+
#: src/strings.ts:2613
2580+
msgid "Enter title"
2581+
msgstr "Enter title"
2582+
25792583
#: src/strings.ts:1492
25802584
msgid "Enter verification code sent to your new email"
25812585
msgstr "Enter verification code sent to your new email"

packages/intl/locale/pseudo-LOCALE.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,10 @@ msgstr ""
25652565
msgid "Enter the recovery code to continue logging in"
25662566
msgstr ""
25672567

2568+
#: src/strings.ts:2613
2569+
msgid "Enter title"
2570+
msgstr ""
2571+
25682572
#: src/strings.ts:1492
25692573
msgid "Enter verification code sent to your new email"
25702574
msgstr ""

packages/intl/src/strings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2609,5 +2609,6 @@ Use this if changes from other devices are not appearing on this device. This wi
26092609
views: () => t`Views`,
26102610
clickToUpdate: () => t`Click to update`,
26112611
noPassword: () => t`No password`,
2612-
publishToTheWeb: () => t`Publish to the web`
2612+
publishToTheWeb: () => t`Publish to the web`,
2613+
enterTitle: () => t`Enter title`
26132614
};

0 commit comments

Comments
 (0)