Skip to content

🔥 JoinPageRoom()からpatch()以外の編集関数を消す #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 2 additions & 62 deletions browser/websocket/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,14 @@ import {
socketIO,
wrap,
} from "../../deps/socket.ts";
import { createNewLineId, getProjectId, getUserId } from "./id.ts";
import { getProjectId, getUserId } from "./id.ts";
import { diffToChanges } from "./patch.ts";
import { applyCommit } from "./applyCommit.ts";
import type { Line } from "../../deps/scrapbox.ts";
import { ensureEditablePage, pushCommit, pushWithRetry } from "./_fetch.ts";
import { ensureEditablePage, pushCommit } from "./_fetch.ts";
export type { CommitNotification };

export interface JoinPageRoomResult {
/** 特定の位置にテキストを挿入する
*
* @param text - 挿入したいテキスト (複数行も可)
* @param beforeId - この行IDが指し示す行の上に挿入する。末尾に挿入する場合は`_end`を指定する
*/
insert: (text: string, beforeId: string) => Promise<void>;
/** 特定の行を削除する
*
* @param lineId 削除したい行の行ID
*/
remove: (lineId: string) => Promise<void>;
/** 特定の位置のテキストを書き換える
*
* @param text - 書き換え後のテキスト (改行は不可)
* @param lineId - 書き換えたい行の行ID
*/
update: (text: string, lineId: string) => Promise<void>;

/** ページ全体を書き換える
*
* `update()`で現在の本文から書き換え後の本文を作ってもらう。
Expand Down Expand Up @@ -86,49 +68,7 @@ export async function joinPageRoom(
}
})();

async function push(changes: Change[], retry = 3) {
// 変更後のlinesを計算する
const changedLines = applyCommit(lines, changes, {
userId,
});
// タイトルの変更チェック
// 空ページの場合もタイトル変更commitを入れる
if (lines[0].text !== changedLines[0].text || !created) {
changes.push({ title: changedLines[0].text });
}
// サムネイルの変更チェック
const oldDescriptions = lines.slice(1, 6).map((line) => line.text);
const newDescriptions = changedLines.slice(1, 6).map((lines) => lines.text);
if (oldDescriptions.join("\n") !== newDescriptions.join("\n")) {
changes.push({ descriptions: newDescriptions });
}

// serverにpushする
parentId = await pushWithRetry(request, changes, {
parentId,
projectId,
pageId,
userId,
project,
title,
retry,
});
// pushに成功したら、localにも変更を反映する
created = true;
lines = changedLines;
}

return {
insert: async (text: string, beforeId = "_end") => {
const changes = text.split(/\n|\r\n/).map((line) => ({
_insert: beforeId,
lines: { text: line, id: createNewLineId(userId) },
}));
await push(changes);
},
remove: (lineId: string) => push([{ _delete: lineId, lines: -1 }]),
update: (text: string, lineId: string) =>
push([{ _update: lineId, lines: { text } }]),
patch: async (update: (before: Line[]) => string[] | Promise<string[]>) => {
const tryPush = async () => {
const pending = update(lines);
Expand Down