Skip to content

Commit 5d9c2d7

Browse files
authored
Merge pull request #19 from takker99/feature-delete
✨ Delete pages when the callback returns an empty array
2 parents 7e9d615 + 1b2d247 commit 5d9c2d7

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

browser/websocket/room.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { applyCommit } from "./applyCommit.ts";
44
import { makeChanges } from "./makeChanges.ts";
55
import { HeadData, pull } from "./pull.ts";
66
import type { Line } from "../../deps/scrapbox.ts";
7-
import { pushCommit } from "./_fetch.ts";
7+
import { pushCommit, pushWithRetry } from "./_fetch.ts";
88
export type { CommitNotification };
99

1010
export interface JoinPageRoomResult {
@@ -13,7 +13,7 @@ export interface JoinPageRoomResult {
1313
* `update()`で現在の本文から書き換え後の本文を作ってもらう。
1414
* serverには書き換え前後の差分だけを送信する
1515
*
16-
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される
16+
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される
1717
*/
1818
patch: (
1919
update: (before: Line[], metadata: HeadData) => string[],
@@ -74,11 +74,22 @@ export async function joinPageRoom(
7474
try {
7575
const pending = update(head.lines, head);
7676
const newLines = pending instanceof Promise ? await pending : pending;
77+
78+
if (newLines.length === 0) {
79+
await pushWithRetry(request, [{ deleted: true }], {
80+
projectId,
81+
pageId: head.pageId,
82+
parentId: head.commitId,
83+
userId,
84+
project,
85+
title,
86+
});
87+
}
88+
7789
const changes = makeChanges(head.lines, newLines, {
7890
userId,
7991
head,
8092
});
81-
8293
const { commitId } = await pushCommit(request, changes, {
8394
parentId: head.commitId,
8495
projectId,

browser/websocket/shortcuts.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function deletePage(
5050
*
5151
* @param project 書き換えたいページのproject
5252
* @param title 書き換えたいページのタイトル
53-
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される
53+
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される。空配列を返すとページが削除される
5454
*/
5555
export async function patch(
5656
project: string,
@@ -78,8 +78,19 @@ export async function patch(
7878
try {
7979
const pending = update(head.lines, head);
8080
const newLines = pending instanceof Promise ? await pending : pending;
81-
const changes = makeChanges(head.lines, newLines, { userId, head });
8281

82+
if (newLines.length === 0) {
83+
await pushWithRetry(request, [{ deleted: true }], {
84+
projectId,
85+
pageId: head.pageId,
86+
parentId: head.commitId,
87+
userId,
88+
project,
89+
title,
90+
});
91+
}
92+
93+
const changes = makeChanges(head.lines, newLines, { userId, head });
8394
await pushCommit(request, changes, {
8495
parentId: head.commitId,
8596
projectId,

0 commit comments

Comments
 (0)